From: Aleksander Ɓukasz Date: Thu, 18 Jul 2013 12:45:00 +0000 (+0200) Subject: xml formatting wip: some inline element cases X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/b60fde6f2bb4d1e10bd4187d54601315aa2e3435 xml formatting wip: some inline element cases --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 46dfd6d..8905d3e 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -55,32 +55,40 @@ $.extend(Canvas.prototype, { spanBefore = el.prev().length > 0 && $(el.prev()[0]).attr('wlxml-tag') === 'span', spanAfter = el.next().length > 0 && $(el.next()[0]).attr('wlxml-tag') === 'span'; + var oldText = this.data, + oldLength = this.data.length; + var parent = el.parent(), + parentContents = parent.contents(), + idx = parentContents.index(this), + next = idx < parentContents.length - 1 ? parentContents[idx+1] : null; + + var addInfo = function() { + if(next) { + $(next).data('orig-before', oldText); + } else { + parent.data('orig-append', oldText); + } + } + if(spanParent || spanBefore || spanAfter) { var startSpace = /\s/g.test(this.data.substr(0,1)); var endSpace = /\s/g.test(this.data.substr(-1)) && this.data.length > 1; var trimmed = $.trim(this.data); - this.data = (startSpace && (spanParent || spanBefore) ? ' ' : '') + var newText = (startSpace && (spanParent || spanBefore) ? ' ' : '') + trimmed + (endSpace && (spanParent || spanAfter) ? ' ' : ''); - + if(newText !== oldText) { + this.data = newText; + addInfo(); + } } else { - var oldText = this.data, - oldLength = this.data.length; + this.data = $.trim(this.data); if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1) this.data = ' '; if(this.data.length === 0) { - var parent = $(this).parent(), - parentContents = parent.contents(), - idx = parentContents.index(this), - next = idx < parentContents.length - 1 ? parentContents[idx+1] : null; - - if(next) { - $(next).data('orig-before', oldText); - } else { - parent.data('orig-append', oldText); - } - $(this).remove(); + addInfo(); + el.remove(); return true; // continue } diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index df95a78..a68e66b 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -1122,7 +1122,18 @@ describe('Canvas', function() { expect(partsIn).to.deep.equal(partsOut); }); - it('nests new children elements', function() { + it('keeps white space between XML nodes - inline case', function() { + var xmlIn = '
\n\n\n\n\n\n\n\n\n
', + c = canvas.fromXML(xmlIn), + xmlOut = c.toXML(); + + var partsIn = xmlIn.split('\n\n\n'), + partsOut = xmlOut.split('\n\n\n'); + + expect(partsIn).to.deep.equal(partsOut); + }); + + it('nests new children block elements', function() { var c = canvas.fromXML('
'); c.doc().append({tag: 'header'}); @@ -1132,6 +1143,16 @@ describe('Canvas', function() { expect(xmlOut.split('\n').slice(-1)[0]).to.equal('', 'nesting end ok'); }); + + it('doesn\'t nest new children inline elements', function() { + var c = canvas.fromXML('
'); + + c.doc().append({tag: 'span'}); + + var xmlOut = c.toXML(); + expect(xmlOut).to.equal('
'); + }); + }) }) }); diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 03f224a..f5a022b 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -167,13 +167,13 @@ $.extend(DocumentNodeElement.prototype, { if(this.data('orig-before') !== undefined) { toret.append(document.createTextNode(this.data('orig-before'))); - } else if(level) { + } 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')); //toret = toret.prepend(document.createTextNode(this.data('orig-prepend'))); - } else { + } else if(this.getWlxmlTag() !== 'span'){ node.append('\n' + (new Array(level * 2 + 1)).join(' ')); } @@ -181,7 +181,8 @@ $.extend(DocumentNodeElement.prototype, { return toret.contents(); }, append: function(params) { - this.data('orig-append', undefined); + if(params.tag !== 'span') + this.data('orig-append', undefined); return manipulate(this, params, 'append'); }, before: function(params) {