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

index 46dfd6d..8905d3e 100644 (file)
@@ -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
                         }
index df95a78..a68e66b 100644 (file)
@@ -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 = '<section>\n\n\n<span></span>\n\n\n<span></span>\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 block elements', function() {
                 var c = canvas.fromXML('<section></section>');
     
                 c.doc().append({tag: 'header'});
@@ -1132,6 +1143,16 @@ describe('Canvas', function() {
                 expect(xmlOut.split('\n').slice(-1)[0]).to.equal('</section>', 'nesting end ok');
 
             });
+
+            it('doesn\'t nest new children inline elements', function() {
+                var c = canvas.fromXML('<section></section>');
+    
+                c.doc().append({tag: 'span'});
+
+                var xmlOut = c.toXML();
+                expect(xmlOut).to.equal('<section><span></span></section>');
+            });
+
         })
     })
 });
index 03f224a..f5a022b 100644 (file)
@@ -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) {