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() {
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);
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);
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;
}
});
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');
+ });
+ });
});
});