formatting xml output wip
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 18 Jul 2013 12:43:29 +0000 (14:43 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 18 Jul 2013 12:43:29 +0000 (14:43 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 7143b79..46dfd6d 100644 (file)
@@ -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 = $('<div>');
+        d(this.doc(), parent, 0);
+        return parent.html();
     }
 });
 
index 23061d9..df95a78 100644 (file)
@@ -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 = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',
+                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 = '  <section></section>  ',
+                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 = '<section>\n\n\n<div></div>\n\n\n<div></div>\n\n\n</section>',
+                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('<section></section>');
+    
+                c.doc().append({tag: 'header'});
+
+                var xmlOut = c.toXML();
+                expect(xmlOut.split('\n  ')[0]).to.equal('<section>', 'nesting start ok');
+                expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
+
+            });
+        })
+    })
 });
 
 
index 230e987..03f224a 100644 (file)
@@ -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 = $('<div>');
+        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)