From: Aleksander Ɓukasz Date: Thu, 25 Jul 2013 12:34:18 +0000 (+0200) Subject: Fixing some xml output formatting problems X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/42137454f5546e6346ff24374cba0394174321d2?ds=sidebyside Fixing some xml output formatting problems --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 8882c1c..19c2a3f 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -320,8 +320,9 @@ $.extend(Canvas.prototype, { }, toXML: function() { - var parent = $('
'); - this.doc().toXML(parent, 0) + var parent = $('
'), + parts = this.doc().toXML(0); + parent.append(parts); return parent.html(); } }); diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 51f4f0a..50f03e9 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -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 = '
\ +
header1
\ + Some text surrounded by white space\ +
header2
\ +
', + 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 = '
\ +
header
\ + \ + Some text surrounded by white space\ + \ +
', + c = canvas.fromXML(xmlIn); + + var xmlOut = c.toXML(); + expect(xmlOut).to.equal(xmlIn); }); }) diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 9178a5a..9359134 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -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);