smartxml: Wrapping text spanning multiple sibling TextNodes
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 25 Oct 2013 07:54:13 +0000 (09:54 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 25 Oct 2013 07:54:13 +0000 (09:54 +0200)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index d8b6bf0..56e78af 100644 (file)
@@ -200,6 +200,10 @@ $.extend(ElementNode.prototype, {
         };
     },
 
         };
     },
 
+    wrapText: function(params) {
+        return this.document._wrapText(_.extend({inside: this}, params));
+    },
+
     toXML: function() {
         var wrapper = $('<div>');
         wrapper.append(this._getXMLDOMToDump());
     toXML: function() {
         var wrapper = $('<div>');
         wrapper.append(this._getXMLDOMToDump());
@@ -321,6 +325,10 @@ $.extend(Document.prototype, Backbone.Events, {
             suffixInside = textNode2.getText().substr(0, params.offsetEnd),
             suffixOutside = textNode2.getText().substr(params.offsetEnd)
         ;
             suffixInside = textNode2.getText().substr(0, params.offsetEnd),
             suffixOutside = textNode2.getText().substr(params.offsetEnd)
         ;
+
+        if(!(textNode1.parent().sameNode(textNode2.parent()))) {
+            throw new Error('Wrapping text in non-sibling text nodes not supported.');
+        }
         
         var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
         textNode1.after(wrapperElement);
         
         var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs});
         textNode1.after(wrapperElement);
index a7f3fca..7880017 100644 (file)
@@ -278,6 +278,32 @@ describe('smartxml', function() {
             expect(node.contents()[2].getText()).to.equal(' a cat!');
         });
 
             expect(node.contents()[2].getText()).to.equal(' a cat!');
         });
 
+        describe('Wrapping text', function() {
+            it('wraps text spanning multiple sibling TextNodes', function() {
+                var section = elementNodeFromXML('<section>Alice has a <span>small</span> cat</section>'),
+                    wrapper = section.wrapText({
+                        _with: {tag: 'span', attrs: {'attr1': 'value1'}},
+                        offsetStart: 6,
+                        offsetEnd: 4,
+                        textNodeIdx: [0,2]
+                    });
+
+                expect(section.contents().length).to.equal(2);
+                expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE);
+                expect(section.contents()[0].getText()).to.equal('Alice ');
+
+                var wrapper2 = section.contents()[1];
+                expect(wrapper2.sameNode(wrapper)).to.be.true;
+
+                var wrapperContents = wrapper.contents();
+                expect(wrapperContents.length).to.equal(3);
+                expect(wrapperContents[0].getText()).to.equal('has a ');
+
+                expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE);
+                expect(wrapperContents[1].contents().length).to.equal(1);
+                expect(wrapperContents[1].contents()[0].getText()).to.equal('small');
+            });
+        });
     });
 
     describe('Serializing document to WLXML', function() {
     });
 
     describe('Serializing document to WLXML', function() {