From 393685bcd5f1b3cf31cdea0cf36c2ac7e44f31c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 18 Jul 2013 14:43:29 +0200 Subject: [PATCH 1/1] formatting xml output wip --- modules/documentCanvas/canvas/canvas.js | 41 +++++++++++++++-- modules/documentCanvas/canvas/canvas.test3.js | 46 +++++++++++++++++++ .../documentCanvas/canvas/documentElement.js | 36 +++++++++++++++ 3 files changed, 119 insertions(+), 4 deletions(-) diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 7143b79..46dfd6d 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -31,14 +31,17 @@ $.extend(Canvas.prototype, { meta[attr.name.substr(5)] = attr.value; } - var element = documentElement.DocumentNodeElement.createDOM({ + var element = documentElement.DocumentNodeElement.create({ tag: currentTag.prop('tagName').toLowerCase(), klass: currentTag.attr('class'), meta: meta }); - element.append(currentTag.contents()); - return element; + element.dom().append(currentTag.contents()); + ['orig-before', 'orig-append'].forEach(function(attr) { + element.data(attr, ''); + }); + return element.dom(); }); this.wrapper.find(':not(iframe)').addBack().contents() @@ -61,12 +64,24 @@ $.extend(Canvas.prototype, { + (endSpace && (spanParent || spanAfter) ? ' ' : ''); } else { - var oldLength = this.data.length; + 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(); + return true; // continue } } @@ -273,6 +288,24 @@ $.extend(Canvas.prototype, { setCursorPosition: function(position) { if(position.element) this._moveCaretToTextElement(position.element, position.offset); + }, + + toXML: function() { + 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)); + if(isElementNode) { + var dom = $(parent.children()[0]), + elementChildren = element.children(); + for(var i = elementChildren.length - 1; i >= 0; i--) { + d(elementChildren[i], dom, level + 1); + } + } + } + var parent = $('
'); + d(this.doc(), parent, 0); + return parent.html(); } }); diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 23061d9..df95a78 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -1088,6 +1088,52 @@ describe('Canvas', function() { }) }); + + describe('Serializing document to WLXML', function() { + it('keeps document intact when no changes have been made', function() { + var xmlIn = '
Alice
has
a cat!
', + c = canvas.fromXML(xmlIn), + xmlOut = c.toXML(); + + var parser = new DOMParser(), + input = parser.parseFromString(xmlIn, "application/xml").childNodes[0], + output = parser.parseFromString(xmlOut, "application/xml").childNodes[0]; + + expect(input.isEqualNode(output)).to.be.true; + }); + + describe('formatting output xml', function() { + /*it('keeps white spaces at the edges of input xml', function() { + var xmlIn = '
', + c = canvas.fromXML(xmlIn), + xmlOut = c.toXML(); + + expect(xmlOut.substr(4)).to.equal(' <', 'start'); + expect(xmlOut.substr(-2)).to.equal('> ', 'end'); + });*/ + it('keeps white space between XML nodes', 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 elements', function() { + var c = canvas.fromXML('
'); + + c.doc().append({tag: 'header'}); + + var xmlOut = c.toXML(); + expect(xmlOut.split('\n ')[0]).to.equal('
', 'nesting start ok'); + expect(xmlOut.split('\n').slice(-1)[0]).to.equal('
', 'nesting end ok'); + + }); + }) + }) }); diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 230e987..03f224a 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -148,7 +148,40 @@ var manipulate = function(e, params, action) { DocumentNodeElement.prototype = new DocumentElement(); $.extend(DocumentNodeElement.prototype, { + data: function() { + var dom = this.dom(), + args = Array.prototype.slice.call(arguments, 0); + if(args.length === 2 && args[1] === undefined) + return dom.removeData(args[1]); + return dom.data.apply(dom, arguments); + }, + toXML: function(level) { + var node = $('<' + this.getWlxmlTag() + '>'), + toret = $('
'); + if(this.getWlxmlClass()) + node.attr('class', this.getWlxmlClass()); + var meta = this.getWlxmlMetaAttrs(); + meta.forEach(function(attr) { + node.attr('meta-' + attr.name, attr.value); + }); + + if(this.data('orig-before') !== undefined) { + toret.append(document.createTextNode(this.data('orig-before'))); + } else if(level) { + 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 { + node.append('\n' + (new Array(level * 2 + 1)).join(' ')); + } + + toret.append(node); + return toret.contents(); + }, append: function(params) { + this.data('orig-append', undefined); return manipulate(this, params, 'append'); }, before: function(params) { @@ -260,6 +293,9 @@ $.extend(DocumentTextElement, { DocumentTextElement.prototype = new DocumentElement(); $.extend(DocumentTextElement.prototype, { + toXML: function() { + return this.getText(); + }, _setupDOMHandler: function(htmlElement) { var $element = $(htmlElement); if(htmlElement.nodeType === Node.TEXT_NODE) -- 2.20.1