Fixing some xml output formatting problems
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 25 Jul 2013 12:34:18 +0000 (14:34 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 25 Jul 2013 12:34:18 +0000 (14:34 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 8882c1c..19c2a3f 100644 (file)
@@ -320,8 +320,9 @@ $.extend(Canvas.prototype, {
     },
 
     toXML: function() {
-        var parent = $('<div>');
-        this.doc().toXML(parent, 0)
+        var parent = $('<div>'),
+            parts = this.doc().toXML(0);
+        parent.append(parts);
         return parent.html();
     }
 });
index 51f4f0a..50f03e9 100644 (file)
@@ -1185,7 +1185,31 @@ describe('Canvas', function() {
             var xmlOut = c.toXML();
             console.log(xmlOut);
             expect(xmlOut).to.equal(xmlIn);
+            });
+
+            it('keeps white space around text node', function() {
+                var xmlIn = '<section>\
+                <header>header1</header>\
+                Some text surrounded by white space\
+                <header>header2</header>\
+            </section>',
+                    c = canvas.fromXML(xmlIn);
 
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal(xmlIn);
+            });
+
+            it('keeps white space around text node - last node case', function() {
+                var xmlIn = '<section>\
+                <header>header</header>\
+                    \
+                Some text surrounded by white space\
+                    \
+            </section>',
+                    c = canvas.fromXML(xmlIn);
+
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal(xmlIn);
             });
 
         })
index 9178a5a..9359134 100644 (file)
@@ -148,14 +148,6 @@ var manipulate = function(e, params, action) {
 
 DocumentNodeElement.prototype = new DocumentElement();
 
-var addParts = function(parts, parent) {
-    var parentChildren = parent.contents();
-    if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) {
-        $(parentChildren[0]).after(parts);
-    } else {
-        parent.prepend(parts);
-    }
-}
 
 $.extend(DocumentNodeElement.prototype, {
     data: function() {
@@ -165,7 +157,7 @@ $.extend(DocumentNodeElement.prototype, {
             return dom.removeData(args[1]);
         return dom.data.apply(dom, arguments);
     },
-    toXML: function(parent, level) {
+    toXML: function(level) {
         var node = $('<' + this.getWlxmlTag() + '>');
 
         if(this.getWlxmlClass())
@@ -204,12 +196,30 @@ $.extend(DocumentNodeElement.prototype, {
 
         var parts = addFormatting(node);
         
-        var children = this.children();
+        var children = this.children(),
+            childParts,
+            prevChildParts;
+
         for(var i = children.length - 1; i >= 0; i--) {
-            children[i].toXML(node, level + 1);
-        }
+            childParts = children[i].toXML(level + 1);
+            
+            if(i === children.length - 1 && node.contents().length === 2) {
+                $(node.contents()[0]).after(childParts);
+                prevChildParts = childParts;
+                continue;
+            }
+
+            if(prevChildParts && prevChildParts.length > 1 && prevChildParts[0].nodeType === Node.TEXT_NODE && prevChildParts[1].nodeType === Node.TEXT_NODE) {
+                $(node.contents()[0]).after(childParts);
+                prevChildParts = childParts;
+                continue;
+            }
 
-        addParts(parts, parent);
+            node.prepend(childParts);
+
+            prevChildParts = childParts;
+        }
+        return parts;
     },
     append: function(params) {
         if(params.tag !== 'span')
@@ -326,7 +336,7 @@ DocumentTextElement.prototype = new DocumentElement();
 
 $.extend(DocumentTextElement.prototype, {
     toXML: function(parent) {
-        addParts(this.getText(), parent);
+        return this.getText();
     },
     _setupDOMHandler: function(htmlElement) {
         var $element = $(htmlElement);