},
after: function(node) {
- node = node instanceof ElementNode ? node : this.document.createElementNode(node);
- this._$.after(node.nativeNode);
- return node;
+ var insertion = this.getNodeInsertion(node);
+ this._$.after(insertion.ofNode.nativeNode);
+ if(insertion.insertsNew) {
+ this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode});
+ }
+ return insertion.ofNode;
},
before: function(node) {
- node = node instanceof ElementNode ? node : this.document.createElementNode(node);
- this._$.before(node.nativeNode);
- return node;
+ var insertion = this.getNodeInsertion(node);
+ this._$.before(insertion.ofNode.nativeNode);
+ if(insertion.insertsNew) {
+ this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode});
+ }
+ return insertion.ofNode;
},
wrapWith: function(node) {
var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {}));
this.document.trigger('change', event);
},
+
+ getNodeInsertion: function(node) {
+ var insertion = {};
+ if(node instanceof DocumentNode) {
+ insertion.ofNode = node;
+ insertion.insertsNew = !this.document.containsNode(node);
+ } else {
+ insertion.ofNode = this.document.createElementNode(node);
+ insertion.insertsNew = true;
+ }
+ return insertion;
+ }
});
var ElementNode = function(nativeNode, document) {
},
append: function(node) {
- node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
- this._$.append(node.nativeNode);
+ var insertion = this.getNodeInsertion(node);
+ this._$.append(insertion.ofNode.nativeNode);
+ if(insertion.insertsNew) {
+ this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode});
+ }
+ return insertion.ofNode;
},
prepend: function(node) {
- node = node instanceof DocumentNode ? node : this.document.createElementNode(node);
- this._$.prepend(node.nativeNode);
+ var insertion = this.getNodeInsertion(node);
+ this._$.prepend(insertion.ofNode.nativeNode);
+ if(insertion.insertsNew) {
+ this.triggerChangeEvent('nodeAdded', {node: insertion.ofNode});
+ }
+ return insertion.ofNode;
},
unwrapContent: function() {
return this.root.toXML();
},
+ containsNode: function(node) {
+ return node._$.parents().index(this.root._$) !== -1;
+ },
+
wrapNodes: function(params) {
if(!(params.element1.parent().sameNode(params.element2.parent()))) {
throw new Error('Wrapping non-sibling nodes not supported.');
});
+ describe('Events', function() {
+ it('emits nodeAdded event when appending new node', function() {
+ var node = elementNodeFromXML('<div></div>'),
+ spy = sinon.spy();
+ node.document.on('change', spy);
+
+ var appended = node.append({tagName:'div'}),
+ event = spy.args[0][0];
+ expect(event.type).to.equal('nodeAdded');
+ expect(event.meta.node.sameNode(appended)).to.be.true;
+ });
+
+ it('doesn\'t emit nodeAdded when appending aready existing node', function() {
+ var node = elementNodeFromXML('<div><a></a><b></b></div>'),
+ a = node.contents()[0],
+ b = node.contents()[1],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+ a.append(b);
+ expect(spy.callCount).to.equal(0);
+ });
+
+ it('emits nodeAdded event when prepending new node', function() {
+ var node = elementNodeFromXML('<div></div>'),
+ spy = sinon.spy();
+ node.document.on('change', spy);
+
+ var prepended = node.prepend({tagName:'div'}),
+ event = spy.args[0][0];
+ expect(event.type).to.equal('nodeAdded');
+ expect(event.meta.node.sameNode(prepended)).to.be.true;
+ });
+
+ it('doesn\'t emit nodeAdded when prepending aready existing node', function() {
+ var node = elementNodeFromXML('<div><a></a><b></b></div>'),
+ a = node.contents()[0],
+ b = node.contents()[1],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+ a.prepend(b);
+ expect(spy.callCount).to.equal(0);
+ });
+
+ it('emits nodeAdded event when inserting node after another', function() {
+ var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+
+ var inserted = node.after({tagName:'div'}),
+ event = spy.args[0][0];
+ expect(event.type).to.equal('nodeAdded');
+ expect(event.meta.node.sameNode(inserted)).to.be.true;
+ });
+
+ it('doesn\'t emit nodeAdded when inserting aready existing node after another', function() {
+ var node = elementNodeFromXML('<div><a></a><b></b></div>'),
+ a = node.contents()[0],
+ b = node.contents()[1],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+ b.after(a);
+ expect(spy.callCount).to.equal(0);
+ });
+
+ it('emits nodeAdded event when inserting node before another', function() {
+ var node = elementNodeFromXML('<div><a></a></div>').contents()[0],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+
+ var inserted = node.before({tagName:'div'}),
+ event = spy.args[0][0];
+ expect(event.type).to.equal('nodeAdded');
+ expect(event.meta.node.sameNode(inserted)).to.be.true;
+ });
+
+ it('doesn\'t emit nodeAdded when inserting aready existing node before another', function() {
+ var node = elementNodeFromXML('<div><a></a><b></b></div>'),
+ a = node.contents()[0],
+ b = node.contents()[1],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+ a.before(b);
+ expect(spy.callCount).to.equal(0);
+ });
+ });
+
describe('Serializing document to WLXML', function() {
it('keeps document intact when no changes have been made', function() {
var xmlIn = '<section>Alice<div>has</div>a <span class="uri" meta-uri="http://cat.com">cat</span>!</section>',