smartxml: wrapping part of text in TextElement
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 24 Oct 2013 13:30:43 +0000 (15:30 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 24 Oct 2013 13:30:43 +0000 (15:30 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index a396d3e..d8b6bf0 100644 (file)
@@ -149,8 +149,9 @@ $.extend(ElementNode.prototype, {
         return toret;
     },
 
         return toret;
     },
 
-    append: function(documentNode) {
-        this._$.append(documentNode.nativeNode);
+    append: function(node) {
+        node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
+        this._$.append(node.nativeNode);
     },
 
     unwrapContent: function() {
     },
 
     unwrapContent: function() {
@@ -237,6 +238,20 @@ $.extend(TextNode.prototype, {
         this.triggerTextChangeEvent();
     },
 
         this.triggerTextChangeEvent();
     },
 
+    wrapWith: function(desc) {
+        if(typeof desc.start === 'number' && typeof desc.end === 'number') {
+            return this.document._wrapText({
+                inside: this.parent(),
+                textNodeIdx: this.parent().indexOf(this),
+                offsetStart: Math.min(desc.start, desc.end),
+                offsetEnd: Math.max(desc.start, desc.end),
+                _with: {tag: desc.tagName, attrs: desc.attrs}
+            });
+        } else {
+            return DocumentNode.prototype.wrapWith.call(this, desc);
+        }
+    },
+
     triggerTextChangeEvent: function() {
         var event = new events.ChangeEvent('nodeTextChange', {node: this});
         this.document.trigger('change', event);
     triggerTextChangeEvent: function() {
         var event = new events.ChangeEvent('nodeTextChange', {node: this});
         this.document.trigger('change', event);
@@ -261,7 +276,13 @@ $.extend(Document.prototype, Backbone.Events, {
             if(from.text) {
                 from = document.createTextNode(from.text);
             } else {
             if(from.text) {
                 from = document.createTextNode(from.text);
             } else {
-                from = $('<' + from.tagName + '>')[0];
+                var node = $('<' + from.tagName + '>');
+
+                _.keys(from.attrs || {}).forEach(function(key) {
+                    node.attr(key, from.attrs[key]);
+                });
+
+                from = node[0];
             }
         }
         return new this.ElementNodeFactory(from, this);
             }
         }
         return new this.ElementNodeFactory(from, this);
@@ -281,6 +302,52 @@ $.extend(Document.prototype, Backbone.Events, {
 
     toXML: function() {
         return this.root.toXML();
 
     toXML: function() {
         return this.root.toXML();
+    },
+
+    _wrapText: function(params) {
+        params = _.extend({textNodeIdx: 0}, params);
+        if(typeof params.textNodeIdx === 'number') {
+            params.textNodeIdx = [params.textNodeIdx];
+        }
+        
+        var contentsInside = params.inside.contents(),
+            idx1 = Math.min.apply(Math, params.textNodeIdx),
+            idx2 = Math.max.apply(Math, params.textNodeIdx),
+            textNode1 = contentsInside[idx1],
+            textNode2 = contentsInside[idx2],
+            sameNode = textNode1.sameNode(textNode2),
+            prefixOutside = textNode1.getText().substr(0, params.offsetStart),
+            prefixInside = textNode1.getText().substr(params.offsetStart),
+            suffixInside = textNode2.getText().substr(0, params.offsetEnd),
+            suffixOutside = textNode2.getText().substr(params.offsetEnd)
+        ;
+        
+        var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
+        textNode1.after(wrapperElement);
+        textNode1.detach();
+        
+        if(prefixOutside.length > 0) {
+            wrapperElement.before({text:prefixOutside});
+        }
+        if(sameNode) {
+            var core = textNode1.getText().substr(params.offsetStart, params.offsetEnd - params.offsetStart);
+            wrapperElement.append({text: core});
+        } else {
+            textNode2.detach();
+            if(prefixInside.length > 0) {
+                wrapperElement.append({text: prefixInside});
+            }
+            for(var i = idx1 + 1; i < idx2; i++) {
+                wrapperElement.append(contentsInside[i]);
+            }
+            if(suffixInside.length > 0) {
+                wrapperElement.append({text: suffixInside});
+            }
+        }
+        if(suffixOutside.length > 0) {
+            wrapperElement.after({text: suffixOutside});
+        }
+        return wrapperElement;
     }
 });
 
     }
 });
 
index 31d854a..a7f3fca 100644 (file)
@@ -207,6 +207,44 @@ describe('smartxml', function() {
                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
                 expect(returned.getTagName()).to.equal('header');
             });
                 expect(returned.sameNode(parent2)).to.be.equal(true, 'wrapper has a correct parent');
                 expect(returned.getTagName()).to.equal('header');
             });
+
+            describe('wrapping part of DocumentTextElement', function() {
+                [{start: 5, end: 12}, {start: 12, end: 5}].forEach(function(offsets) {
+                    it('wraps in the middle ' + offsets.start + '/' + offsets.end, function() {
+                        var node = elementNodeFromXML('<section>Alice has a cat</section>'),
+                            textNode = node.contents()[0];
+                        
+                        var returned = textNode.wrapWith({tagName: 'header', attrs: {'attr1': 'value1'}, start: offsets.start, end: offsets.end}),
+                            contents = node.contents();
+
+                        expect(contents.length).to.equal(3);
+                        
+                        expect(contents[0].nodeType).to.be.equal(Node.TEXT_NODE, 'first node is text node');
+                        expect(contents[0].getText()).to.equal('Alice');
+
+                        expect(contents[1].sameNode(returned)).to.be.true;
+                        expect(returned.getTagName()).to.equal('header');
+                        expect(returned.getAttr('attr1')).to.equal('value1');
+                        expect(contents[1].contents().length).to.equal(1, 'wrapper has one node inside');
+                        expect(contents[1].contents()[0].getText()).to.equal(' has a ');
+
+                        expect(contents[2].nodeType).to.be.equal(Node.TEXT_NODE, 'third node is text node');
+                        expect(contents[2].getText()).to.equal('cat');
+                    });
+                });
+
+                it('wraps whole text inside DocumentTextElement if offsets span entire content', function() {
+                    var node = elementNodeFromXML('<section>Alice has a cat</section>'),
+                         textNode = node.contents()[0];
+                     
+                    textNode.wrapWith({tagName: 'header', start: 0, end: 15});
+                    
+                    var contents = node.contents();
+                    expect(contents.length).to.equal(1);
+                    expect(contents[0].getTagName()).to.equal('header');
+                    expect(contents[0].contents()[0].getText()).to.equal('Alice has a cat');
+                });
+            });
         });
 
     });
         });
 
     });