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);
});
})
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() {
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())
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')
$.extend(DocumentTextElement.prototype, {
toXML: function(parent) {
- addParts(this.getText(), parent);
+ return this.getText();
},
_setupDOMHandler: function(htmlElement) {
var $element = $(htmlElement);