if(!(this.document.containsNode(this)) || !insertion.isNew) {
nodeParent = insertion.ofNode.parent();
}
+ if(!insertion.insertsNew && insertion.ofNode.isSurroundedByTextNodes()) {
+ var prev = insertion.ofNode.prev(),
+ next = insertion.ofNode.next();
+ prev.setText(prev.getText()+next.getText());
+ next.detach();
+ }
returned = implementation.call(this, insertion.ofNode);
if(!options.silent && returned.sameNode(insertion.ofNode)) {
this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs});
parentElement.after(newElement);
+ succeedingChildren.reverse().forEach(function(child) {
+ newElement.prepend(child);
+ });
if(suffix.length > 0) {
- newElement.append({text: suffix});
+ newElement.prepend({text: suffix});
}
- succeedingChildren.forEach(function(child) {
- newElement.append(child);
- });
return {first: parentElement, second: newElement};
},
expect(rootContents[0].getText()).to.equal('Alice a cat');
});
+ it('merges adjacent text nodes resulting from moving an element node in between', function() {
+ var doc = getDocumentFromXML('<div><a></a>Alice <span>has</span>a cat</div>'),
+ span = doc.root.contents()[2],
+ a = doc.root.contents()[0];
+
+ a.append(span);
+
+ var rootContents = doc.root.contents();
+ expect(rootContents).to.have.length(2, 'one child left');
+ expect(rootContents[1].getText()).to.equal('Alice a cat');
+ });
+
it('inserts node at index', function() {
var doc = getDocumentFromXML('<div><a></a><b></b><c></c></div>'),
b = doc.root.contents()[1];