From: Marcin Koziej Date: Mon, 19 Nov 2012 16:19:07 +0000 (+0100) Subject: DOM tree modification functions X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/4a1f5b4c9e482dcc399940c027a9fdf6a74ea78a?ds=sidebyside DOM tree modification functions replace dumb wrapping with a dom-walking one fix the bug with aliens in attributes --- diff --git a/redakcja/settings/compress.py b/redakcja/settings/compress.py index 43150e47..31e86b4e 100644 --- a/redakcja/settings/compress.py +++ b/redakcja/settings/compress.py @@ -49,6 +49,7 @@ COMPRESS_JS = { # wiki scripts 'js/wiki/wikiapi.js', 'js/wiki/xslt.js', + 'js/wiki/xml-tools.js', # base UI 'js/wiki/base.js', diff --git a/redakcja/static/js/wiki/xml-tools.coffee b/redakcja/static/js/wiki/xml-tools.coffee new file mode 100644 index 00000000..66d04b3e --- /dev/null +++ b/redakcja/static/js/wiki/xml-tools.coffee @@ -0,0 +1,48 @@ + +window.walk = (node, handler) -> + if handler.text + textHandler = handler.text + else + textHandler = handler + + switch node.nodeType + when 1, 9, 11 + child = node.firstChild + while child + nxt = child.nextSibling + walk(child, handler) + child = nxt + when 3 + textHandler node + + +window.wrapInTag = (regex, tagName) -> + fun = (node) -> + matches = [] + while m = regex.exec(node.nodeValue) + matches.push [regex.lastIndex, m[0]] + + matches.reverse() + + for m in matches + to = m[0] + frm = m[0] - m[1].length + + node_rest = node.splitText(to) + alien = node.splitText(frm) + wrapper = node.ownerDocument.createElement tagName + node.parentNode.insertBefore wrapper, node_rest + wrapper.appendChild alien + node + + +# window._test_xml_tools = -> +# p = new DOMParser() +# dom = p.parseFromString("łukówjakaś jeszcze ~", 'text/xml') + +# walk(dom.firstChild, wrapInTag(ALIEN_REGEX, 'alien')) +# dom + + +# window.ALIEN_REGEX = /[^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+/g +# " just for syntax coloring. diff --git a/redakcja/static/js/wiki/xml-tools.js b/redakcja/static/js/wiki/xml-tools.js new file mode 100644 index 00000000..db5da83d --- /dev/null +++ b/redakcja/static/js/wiki/xml-tools.js @@ -0,0 +1,51 @@ +(function() { + + window.walk = function(node, handler) { + var child, nxt, textHandler, _results; + if (handler.text) { + textHandler = handler.text; + } else { + textHandler = handler; + } + switch (node.nodeType) { + case 1: + case 9: + case 11: + child = node.firstChild; + _results = []; + while (child) { + nxt = child.nextSibling; + walk(child, handler); + _results.push(child = nxt); + } + return _results; + break; + case 3: + return textHandler(node); + } + }; + + window.wrapInTag = function(regex, tagName) { + var fun; + return fun = function(node) { + var alien, frm, m, matches, node_rest, to, wrapper, _i, _len; + matches = []; + while (m = regex.exec(node.nodeValue)) { + matches.push([regex.lastIndex, m[0]]); + } + matches.reverse(); + for (_i = 0, _len = matches.length; _i < _len; _i++) { + m = matches[_i]; + to = m[0]; + frm = m[0] - m[1].length; + node_rest = node.splitText(to); + alien = node.splitText(frm); + wrapper = node.ownerDocument.createElement(tagName); + node.parentNode.insertBefore(wrapper, node_rest); + wrapper.appendChild(alien); + } + return node; + }; + }; + +}).call(this); diff --git a/redakcja/static/js/wiki/xslt.js b/redakcja/static/js/wiki/xslt.js index 1327fc6c..618dcd0e 100644 --- a/redakcja/static/js/wiki/xslt.js +++ b/redakcja/static/js/wiki/xslt.js @@ -59,12 +59,15 @@ function withThemes(code_block, onError) function xml2html(options) { + ALIEN_REGEX = /([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g; + withStylesheets(function() { var xml = options.xml.replace(/\/(\s+)/g, '
$1'); - xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '$1'); +// xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '$1'); var parser = new DOMParser(); var serializer = new XMLSerializer(); var doc = parser.parseFromString(xml, 'text/xml'); + walk(doc.firstChild, wrapInTag(ALIEN_REGEX, 'alien')) var error = $('parsererror', doc); if (error.length == 0) {