Ticket #749 - word wrapping for xml error
[redakcja.git] / redakcja / static / js / wiki / xslt.js
index dda12a1..86c6d36 100644 (file)
@@ -19,6 +19,7 @@ function withStylesheets(code_block, onError)
        $.ajax({
                url: STATIC_URL + 'xsl/wl2html_client.xsl',
                dataType: 'xml',
        $.ajax({
                url: STATIC_URL + 'xsl/wl2html_client.xsl',
                dataType: 'xml',
+               timeout: 10000,
                success: function(data) {
                xml2htmlStylesheet = createXSLT(data);
                 $.unblockUI();
                success: function(data) {
                xml2htmlStylesheet = createXSLT(data);
                 $.unblockUI();
@@ -34,6 +35,30 @@ function withStylesheets(code_block, onError)
 }
 
 
 }
 
 
+// Wykonuje block z załadowanymi kanonicznymi motywami
+function withThemes(code_block, onError)
+{
+    if (typeof withThemes.canon == 'undefined') {
+        $.ajax({
+            url: '/themes',
+            dataType: 'text',
+            success: function(data) {
+                withThemes.canon = data.split('\n');
+                code_block(withThemes.canon);
+            },
+            error: function() {
+                withThemes.canon = null;
+                code_block(withThemes.canon);
+            }
+        })
+    }
+    else {
+        code_block(withThemes.canon);
+    }
+}
+
+
+
 function xml2html(options) {
     withStylesheets(function() {
         var xml = options.xml.replace(/\/\s+/g, '<br />');
 function xml2html(options) {
     withStylesheets(function() {
         var xml = options.xml.replace(/\/\s+/g, '<br />');
@@ -44,12 +69,12 @@ function xml2html(options) {
 
         if (error.length == 0) {
             doc = xml2htmlStylesheet.transformToFragment(doc, document);
 
         if (error.length == 0) {
             doc = xml2htmlStylesheet.transformToFragment(doc, document);
-                       console.log(doc.firstChild);
+            console.log(doc.firstChild);
 
 
-                       if(doc.firstChild === null) {
-                               options.error("Błąd w przetwarzaniu XML.");
-                               return;
-                       }
+        if(doc.firstChild === null) {
+            options.error("Błąd w przetwarzaniu XML.");
+                return;
+            }
 
             error = $('parsererror', doc);
         }
 
             error = $('parsererror', doc);
         }
@@ -58,6 +83,20 @@ function xml2html(options) {
             options.error(error.text());
         } else {
             options.success(doc.firstChild);
             options.error(error.text());
         } else {
             options.success(doc.firstChild);
+
+            withThemes(function(canonThemes) {
+                if (canonThemes != null) {
+                    $('.theme-text-list').addClass('canon').each(function(){
+                        var themes = $(this).html().split(',');
+                        for (i in themes) {
+                            themes[i] = $.trim(themes[i]);
+                            if (canonThemes.indexOf(themes[i]) == -1)
+                                themes[i] = '<span x-pass-thru="true" class="noncanon">' + themes[i] + "</span>"
+                        }
+                        $(this).html(themes.join(', '));
+                    });
+                }
+            });
         }
     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
 }
         }
     }, function() { options.error && options.error('Nie udało się załadować XSLT'); });
 }
@@ -218,7 +257,8 @@ HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
 
                switch(token.node.nodeType) {
                        case ELEMENT_NODE:
 
                switch(token.node.nodeType) {
                        case ELEMENT_NODE:
-                               if(token.node.hasAttribute('x-pass-thru')) {
+                               if(token.node.hasAttribute('x-pass-thru')
+                                || token.node.hasAttribute('data-pass-thru')) {
                                        self._pushChildren(token.node);
                                        break;
                                }
                                        self._pushChildren(token.node);
                                        break;
                                }
@@ -248,7 +288,11 @@ HTMLSerializer.prototype.serialize = function(rootElement, stripOuter)
                                self._serializeElement(token.node);
                                break;
                        case TEXT_NODE:
                                self._serializeElement(token.node);
                                break;
                        case TEXT_NODE:
-                               self.result += token.node.nodeValue;
+                               // collapse previous element's padding
+                               var i = 0;
+                               while (token.node.nodeValue[i] == '\n' && self.result[self.result.length - 1] == '\n')
+                                 i ++;
+                               self.result += token.node.nodeValue.substr(i);
                                break;
                };
        };
                                break;
                };
        };
@@ -332,7 +376,9 @@ HTMLSerializer.prototype._serializeElement = function(node) {
        };
 
        /* print out */
        };
 
        /* print out */
-       if (getPadding(tagName))
+
+       // at least one newline before padded elements
+       if (getPadding(tagName) && self.result[self.result.length - 1] != '\n')
                self.result += '\n';
 
        self.result += '<' + tagName;
                self.result += '\n';
 
        self.result += '<' + tagName;