From 36bb9273f472d484a633ec54bb4b0ad3b70d50f2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 19 Jul 2013 19:03:21 +0200 Subject: [PATCH 1/1] Output formatting wip --- modules/documentCanvas/canvas/canvas.js | 48 +++++++++++++++---- modules/documentCanvas/canvas/canvas.test3.js | 2 +- .../documentCanvas/canvas/documentElement.js | 16 +++++-- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index b11432f..ad826c7 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -42,7 +42,7 @@ $.extend(Canvas.prototype, { element.dom().append(currentTag.contents()); ['orig-before', 'orig-append'].forEach(function(attr) { - element.data(attr, ''); + element.data(attr, ['']); }); return element.dom(); }); @@ -82,12 +82,25 @@ $.extend(Canvas.prototype, { + (endSpace && (spanParent || spanAfter) ? ' ' : ''); if(newText !== oldText) { this.data = newText; - if(endSpace) { - var toAdd = oldText.match(/\s+$/g)[0]; - if(newText[newText.length - 1] === ' ' && toAdd[0] === ' ') - toAdd = toAdd.substr(1); - addInfo(toAdd); + var toAdd1, toAdd2; + var toAdd = []; + if(!trimmed) { + toAdd.push(oldText); + } else { + if(endSpace) { + toAdd1 = oldText.match(/\s+$/g)[0]; + if(newText[newText.length - 1] === ' ' && toAdd1[0] === ' ') + toAdd1 = toAdd1.substr(1); + toAdd.push(toAdd1); + } + if(startSpace) { + toAdd2 = oldText.match(/^\s+/g)[0]; + if(newText[0] === ' ' && toAdd2[toAdd2.length-1] === ' ') + toAdd2 = toAdd2.substr(0, toAdd2.length -1); + toAdd.push(toAdd2); + } } + addInfo(toAdd); } } else { @@ -95,7 +108,7 @@ $.extend(Canvas.prototype, { if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1) this.data = ' '; if(this.data.length === 0) { - addInfo(oldText); + addInfo([oldText]); el.remove(); return true; // continue @@ -310,10 +323,25 @@ $.extend(Canvas.prototype, { var d = function(element, parent, level) { console.log(element.getText ? 'text: ' + element.getText() : 'node: ' + element.getWlxmlTag()); var isElementNode = element instanceof documentElement.DocumentNodeElement; - parent.prepend(element.toXML(level)); + var parentChildren = parent.contents(); + var dom; + + var elXML = element.toXML(level); + if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) { + $(parentChildren[0]).after(elXML); + } else { + parent.prepend(elXML); + } + + if(isElementNode) { - var dom = $(parent.children()[0]), - elementChildren = element.children(); + elXML.each(function() { + if(this.nodeType === Node.ELEMENT_NODE) { + dom = $(this); + return; + } + }); + var elementChildren = element.children(); for(var i = elementChildren.length - 1; i >= 0; i--) { d(elementChildren[i], dom, level + 1); } diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index e17c758..51f4f0a 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -1175,7 +1175,7 @@ describe('Canvas', function() { it('keeps original white space at the end of text', function() { - var xmlIn = '
Some text ended with white space \ + var xmlIn = '
Some text ended with white space \ \ Some text some text\ \ diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 94b8422..969d85b 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -170,13 +170,19 @@ $.extend(DocumentNodeElement.prototype, { node.attr(key, this.data('other-attrs')[key]); }, this); - if(this.data('orig-before') !== undefined) { - toret.append(document.createTextNode(this.data('orig-before'))); + if(this.data('orig-before') && this.data('orig-before').length) { + this.data('orig-before').forEach(function(toAdd) { + if(toAdd) + toret.prepend(document.createTextNode(toAdd)); + }); } else if(level && this.getWlxmlTag() !== 'span') { toret.append('\n' + (new Array(level * 2 + 1)).join(' ')); } - if(this.data('orig-append') !== undefined) { - node.append(this.data('orig-append')); + if(this.data('orig-append') && this.data('orig-append').length) { + this.data('orig-append').forEach(function(toAdd) { + if(toAdd) + node.prepend(toAdd); + }); //toret = toret.prepend(document.createTextNode(this.data('orig-prepend'))); } else if(this.getWlxmlTag() !== 'span'){ node.append('\n' + (new Array(level * 2 + 1)).join(' ')); @@ -187,7 +193,7 @@ $.extend(DocumentNodeElement.prototype, { }, append: function(params) { if(params.tag !== 'span') - this.data('orig-append', undefined); + this.data('orig-append', []); return manipulate(this, params, 'append'); }, before: function(params) { -- 2.20.1