DOM tree modification functions edumed-xslt
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 19 Nov 2012 16:19:07 +0000 (17:19 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Mon, 19 Nov 2012 16:19:07 +0000 (17:19 +0100)
replace dumb <alien> wrapping with a dom-walking one
fix the bug with aliens in attributes

redakcja/settings/compress.py
redakcja/static/js/wiki/xml-tools.coffee [new file with mode: 0644]
redakcja/static/js/wiki/xml-tools.js [new file with mode: 0644]
redakcja/static/js/wiki/xslt.js

index 43150e4..31e86b4 100644 (file)
@@ -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 (file)
index 0000000..66d04b3
--- /dev/null
@@ -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("<a><b>łuków</b><c>jakaś jeszcze ~</c></a>", '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 (file)
index 0000000..db5da83
--- /dev/null
@@ -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);
index 1327fc6..618dcd0 100644 (file)
@@ -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, '<br />$1');
-        xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
+//        xml = xml.replace(/([^a-zA-Z0-9ąćęłńóśźżĄĆĘŁŃÓŚŹŻ\s<>«»\\*_!,:;?&%."'=#()\/-]+)/g, '<alien>$1</alien>');
         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) {