From d3b7c1547785fb57c92ff6b5d309190a337b6b5e Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 20 Oct 2016 17:54:58 +0200 Subject: [PATCH] Fixed handling certain HTML string --- .../redactor2/plugins/WoltLabClean.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js index 6d30544dad..0fe2d1766b 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js @@ -248,6 +248,49 @@ $.Redactor.prototype.WoltLabClean = function() { html = div.innerHTML; } + // reconvert links as the regex can be overly greedy sometimes + if (data.links) { + // prevent the original regex from being executed again + data.links = false; + + // We try to avoid the regex to consume too many parts + // by having it work from the back to the front. Please + // keep in mind that the parts are reversed, so they + // actually appear in inverted order + var tmp = html.split(/(###\/a###)/gi).reverse(); + var part, nextPart; + for (var i = 1, length = tmp.length; i < length; i++) { + if (i + 1 === length) { + break; + } + + part = tmp[i]; + nextPart = tmp[i + 1]; + + // split by '###a' to find all possible start positions + var anchors = nextPart.split(/(###a)/gi).reverse(); + var composite; + for (var j = 1, innerLength = anchors.length; j < innerLength; j++) { + if (j + 1 === innerLength) { + break; + } + + composite = anchors[j] + anchors[j - 1]; + if (composite.match(/###a(.*?)href="(.*?)"(.*?)###(.*?)/i)) { + anchors[j] = ''; + anchors[j - 1] = composite.replace(/###a(.*?)href="(.*?)"(.*?)###(.*?)/i, '$4'); + + tmp[i] = ''; + tmp[i + 1] = anchors.reverse().join(''); + + break; + } + } + } + + html = tmp.reverse().join(''); + } + return mpReconvertTags.call(this, html, data); }).bind(this); -- 2.20.1