+ expect(node1.contents()[1].sameNode(node3)).to.be.true;
+ });
+
+ it('prepends element node to another element node', function() {
+ var node1 = elementNodeFromParams({tag: 'div'}),
+ node2 = elementNodeFromParams({tag: 'a'}),
+ node3 = elementNodeFromParams({tag: 'p'});
+ node1.prepend(node2);
+ node1.prepend(node3);
+ expect(node1.contents()[0].sameNode(node3)).to.be.true;
+ expect(node1.contents()[1].sameNode(node2)).to.be.true;
+ });
+
+ describe('adding text nodes', function() {
+ it('merges text nodes on append', function() {
+ var doc = getDocumentFromXML('<root>text1</root>'),
+ returned;
+ returned = doc.root.append({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(1);
+ expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
+ expect(doc.root.contents()[0].getText()).to.equal('text1text2');
+ });
+
+ it('merges text nodes on prepend', function() {
+ var doc = getDocumentFromXML('<root>text1</root>'),
+ returned;
+ returned = doc.root.prepend({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(1);
+ expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
+ expect(doc.root.contents()[0].getText()).to.equal('text2text1');
+ });
+
+ it('merges text nodes on before text node', function() {
+ var doc = getDocumentFromXML('<root>text1</root>'),
+ textNode = doc.root.contents()[0],
+ returned;
+ returned = textNode.before({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(1);
+ expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
+ expect(doc.root.contents()[0].getText()).to.equal('text2text1');
+ });
+
+ it('merges text nodes on after text node', function() {
+ var doc = getDocumentFromXML('<root>text1</root>'),
+ textNode = doc.root.contents()[0],
+ returned;
+ returned = textNode.after({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(1);
+ expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
+ expect(doc.root.contents()[0].getText()).to.equal('text1text2');
+ });
+
+ it('merges text nodes on before element node', function() {
+ var doc = getDocumentFromXML('<root>text1<div></div></root>'),
+ textNode = doc.root.contents()[0],
+ div = doc.root.contents()[1],
+ returned;
+ returned = div.before({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(2);
+ expect(returned.sameNode(doc.root.contents()[0])).to.equal(true, 'modified node returned');
+ expect(textNode.getText()).to.equal('text1text2');
+ });
+
+ it('merges text nodes on after element node', function() {
+ var doc = getDocumentFromXML('<root><div></div>text1</root>'),
+ textNode = doc.root.contents()[1],
+ div = doc.root.contents()[0],
+ returned;
+ returned = div.after({text: 'text2'});
+ expect(doc.root.contents().length).to.equal(2);
+ expect(returned.sameNode(doc.root.contents()[1])).to.equal(true, 'modified node returned');
+ expect(textNode.getText()).to.equal('text2text1');
+ });
+ });
+
+ it('wraps root element node with another element node', function() {
+ var node = elementNodeFromXML('<div></div>'),
+ wrapper = elementNodeFromXML('<wrapper></wrapper>');
+
+ node.wrapWith(wrapper);
+ expect(node.parent().sameNode(wrapper)).to.be.true;
+ expect(node.document.root.sameNode(wrapper)).to.be.true;
+ });
+
+ it('wraps element node with another element node', function() {
+ var doc = getDocumentFromXML('<section><div></div></section>'),
+ div = doc.root.contents()[0];
+
+ var wrapper = div.wrapWith({tagName: 'wrapper'});
+ expect(wrapper.sameNode(doc.root.contents()[0])).to.equal(true, '1');
+ expect(div.parent().sameNode(wrapper)).to.equal(true, '2');
+ expect(wrapper.contents()[0].sameNode(div)).to.equal(true, '3');
+ });
+
+ it('wraps element outside of document tree', function() {
+ var doc = getDocumentFromXML('<section><div></div></section>'),
+ node = doc.createDocumentNode({tagName: 'node'});
+
+ node.wrapWith({tagName: 'wrapper'});
+ expect(node.parent().getTagName()).to.equal('wrapper');
+ expect(node.parent().contents()[0].sameNode(node)).to.be.true;
+ expect(doc.root.getTagName()).to.equal('section');
+ });
+
+ it('unwraps element node contents', function() {
+ var node = elementNodeFromXML('<div>Alice <div>has <span>propably</span> a cat</div>!</div>'),
+ outerDiv = node.contents()[1];
+
+ outerDiv.unwrapContent();
+
+ expect(node.contents().length).to.equal(3);
+ expect(node.contents()[0].getText()).to.equal('Alice has ');
+ expect(node.contents()[1].getTagName()).to.equal('span');
+ expect(node.contents()[2].getText()).to.equal(' a cat!');
+ });
+
+ it('removes parent-describing sibling nodes of unwrapped node', function() {
+ var doc = getDocumentFromXML('<root><div><a></a><x></x><a></a></div></root>');
+
+ doc.registerExtension({documentNode: {methods: {
+ object: {
+ describesParent: function() {
+ return this.getTagName() === 'x';
+ }
+ }
+ }}});
+
+ var div = doc.root.contents()[0],
+ x = div.contents()[1];
+
+ div.unwrapContent();
+ expect(doc.root.contents().length).to.equal(2);
+ expect(x.isInDocument()).to.be.false;
+ });
+
+ it('unwrap single element node from its parent', function() {
+ var doc = getDocumentFromXML('<div><a><b></b></a></div>'),
+ div = doc.root,
+ a = div.contents()[0],
+ b = a.contents()[0];
+
+ var parent = b.unwrap();
+
+ expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
+ expect(div.contents()).to.have.length(1, 'root contains only one node');
+ expect(div.contents()[0].sameNode(b)).to.equal(true, 'node got unwrapped');
+ });
+
+ it('unwrap single text node from its parent', function() {
+ var doc = getDocumentFromXML('<div>Some <span>text</span>!</div>'),
+ div = doc.root,
+ span = div.contents()[1],
+ text = span.contents()[0];
+
+ var parent = text.unwrap();
+
+ expect(parent.sameNode(div)).to.equal(true, 'returns new parent');
+ expect(div.contents()).to.have.length(1, 'root contains only one node');
+ expect(div.contents()[0].getText()).to.equal('Some text!');
+ });
+
+ 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: {tagName: '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;
+ expect(wrapper.getTagName()).to.equal('span');
+
+ 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');
+ });
+
+ it('keeps parent-describing nodes in place', function() {
+ var doc = getDocumentFromXML('<root>Alice <x></x> probably <y></y> has a cat</root>');
+
+ doc.registerExtension({documentNode: {methods: {
+ object: {
+ describesParent: function() {
+ /* globals Node */
+ return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
+ }
+ }
+ }}});
+
+ var root = doc.root,
+ x = root.contents()[1],
+ y = root.contents()[3];
+
+ root.wrapText({
+ _with: {tagName: 'span', attrs: {'attr1': 'value1'}},
+ offsetStart: 1,
+ offsetEnd: 4,
+ textNodeIdx: [0,4]
+ });
+
+ expect(x.parent().sameNode(root)).to.be.true;
+ expect(y.parent().getTagName()).to.equal('span');
+ });
+ });
+
+ describe('Wrapping Nodes', function() {
+ it('wraps multiple sibling nodes', function() {
+ var section = elementNodeFromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
+ aliceText = section.contents()[0],
+ firstDiv = section.contents()[1],
+ lastDiv = section.contents()[section.contents().length -1];
+
+ var returned = section.document.wrapNodes({
+ node1: aliceText,
+ node2: lastDiv,
+ _with: {tagName: 'header'}
+ });
+
+ var sectionContentss = section.contents(),
+ header = sectionContentss[0],
+ headerContents = header.contents();
+
+ expect(sectionContentss).to.have.length(1);
+ expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
+ expect(header.parent().sameNode(section)).to.be.true;
+ expect(headerContents).to.have.length(3);
+ expect(headerContents[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
+ expect(headerContents[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
+ expect(headerContents[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
+ });
+
+ it('wraps multiple sibling Elements - middle case', function() {
+ var section = elementNodeFromXML('<section><div></div><div></div><div></div><div></div></section>'),
+ div2 = section.contents()[1],
+ div3 = section.contents()[2];
+
+ section.document.wrapNodes({
+ node1: div2,
+ node2: div3,
+ _with: {tagName: 'header'}
+ });
+
+ var sectionContentss = section.contents(),
+ header = sectionContentss[1],
+ headerChildren = header.contents();
+
+ expect(sectionContentss).to.have.length(3);
+ expect(headerChildren).to.have.length(2);
+ expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
+ expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
+ });
+
+ it('keeps parent-describing nodes in place', function() {
+ var section = elementNodeFromXML('<section>Alice<x></x><div>a cat</div></section>');
+
+ section.document.registerExtension({documentNode: {methods: {
+ object: {
+ describesParent: function() {
+ return this.nodeType === Node.ELEMENT_NODE && this.getTagName() === 'x';
+ }
+ }
+ }}});
+
+ var aliceText = section.contents()[0],
+ x = section.contents()[1],
+ lastDiv = section.contents()[2];
+
+ section.document.wrapNodes({
+ node1: aliceText,
+ node2: lastDiv,
+ _with: {tagName: 'header'}
+ });
+
+ expect(x.parent().sameNode(section)).to.be.true;
+ expect(aliceText.parent().getTagName()).to.equal('header');
+ expect(lastDiv.parent().getTagName()).to.equal('header');
+ });
+ });
+
+ });
+
+ var getTextNodes = function(text, doc) {
+ /* globals Node */
+ var toret = [];
+ var search = function(node) {
+ node.contents().forEach(function(node) {
+ if(node.nodeType === Node.TEXT_NODE) {
+ if(node.getText() === text) {
+ toret.push(node);
+ }
+ } else {
+ search(node);
+ }
+ });
+ };
+ search(doc.root);
+ return toret;
+ };
+
+ var getTextNode = function(text, doc) {
+ var nodes = getTextNodes(text, doc),
+ error;
+ if(nodes.length === 0) {
+ error = 'Text not found';
+ } else if(nodes.length > 1) {
+ error = 'Text not unique';
+ } else if(nodes[0].getText() !== text) {
+ error = 'I was trying to cheat your test :(';
+ }
+ if(error) {
+ throw new Error(error);
+ }
+ return nodes[0];
+ };
+
+ describe('Removing arbitrary text', function() {
+ it('removes within single text element', function() {
+ var doc = getDocumentFromXML('<div>Alice</div>'),
+ text = getTextNode('Alice', doc);
+ doc.deleteText({
+ from: {
+ node: text,
+ offset: 1
+ },
+ to: {
+ node: text,
+ offset: 4
+ }
+ });
+ expect(doc.root.contents().length).to.equal(1);
+ expect(doc.root.contents()[0].getText()).to.equal('Ae');
+ });
+ it('removes across elements - 1', function() {
+ var doc = getDocumentFromXML('<div><a>aaa</a><b>bbb</b></div>');
+
+ doc.deleteText({
+ from: {
+ node: getTextNode('aaa', doc),
+ offset: 2
+ },
+ to: {
+ node: getTextNode('bbb', doc),
+ offset: 2
+ }
+ });
+
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2);
+ expect(contents[0].contents()[0].getText()).to.equal('aa');
+ expect(contents[1].contents()[0].getText()).to.equal('b');
+ });
+ it('removes across elements - 2', function() {
+ var doc = getDocumentFromXML('<a><b><c>ccc</c></b>xxx</a>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('ccc', doc),
+ offset: 2
+ },
+ to: {
+ node: getTextNode('xxx', doc),
+ offset: 2
+ }
+ });
+
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2);
+ expect(contents[0].getTagName()).to.equal('b');
+ expect(contents[1].getText()).to.equal('x');
+
+ var bContents = contents[0].contents();
+ expect(bContents.length).to.equal(1);
+ expect(bContents[0].getTagName()).to.equal('c');
+ expect(bContents[0].contents().length).to.equal(1);
+ expect(bContents[0].contents()[0].getText()).to.equal('cc');
+ });
+ it('remove across elements - 3 (merged text nodes)', function() {
+ var doc = getDocumentFromXML('<div>Alice <span>has</span> a cat</div>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('Alice ', doc),
+ offset: 1
+ },
+ to: {
+ node: getTextNode(' a cat', doc),
+ offset: 3
+ }
+ });
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(1);
+ expect(contents[0].getText()).to.equal('Acat');
+ });
+ it('remove across elements - 4', function() {
+ var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('Alice ', doc),
+ offset: 1
+ },
+ to: {
+ node: getTextNode(' cat', doc),
+ offset: 1
+ }
+ });
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2);
+ expect(contents[0].getText()).to.equal('A');
+ expect(contents[1].getTagName()).to.equal('div');
+ expect(contents[1].contents().length).to.equal(1);
+ expect(contents[1].contents()[0].getText()).to.equal('cat');
+ });
+ it('removes across elements - 5 (whole document)', function() {
+ var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div>!!!</div>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('Alice ', doc),
+ offset: 0
+ },
+ to: {
+ node: getTextNode('!!!', doc),
+ offset: 3
+ }
+ });
+
+ expect(doc.root.getTagName()).to.equal('div');
+ expect(doc.root.contents().length).to.equal(1);
+ expect(doc.root.contents()[0].getText()).to.equal('');
+ });
+ it('removes nodes in between', function() {
+ var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('aaa', doc),
+ offset: 2
+ },
+ to: {
+ node: getTextNode('bbb', doc),
+ offset: 2
+ }
+ });
+
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2, 'two nodes survived');
+ expect(contents[0].getTagName()).to.equal('a');
+ expect(contents[1].getTagName()).to.equal('b');
+ expect(contents[0].contents().length).to.equal(1);
+ expect(contents[0].contents()[0].getText()).to.equal('aa');
+ expect(contents[1].contents().length).to.equal(1);
+ expect(contents[1].contents()[0].getText()).to.equal('b');
+ });
+ it('removes across elements - 6', function() {
+ var doc = getDocumentFromXML('<root><div>aaa<span>bbb</span>ccc</div><div>ddd</div></root>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('aaa', doc),
+ offset: 1
+ },
+ to: {
+ node: getTextNode('ddd', doc),
+ offset: 1
+ }
+ }, {
+ error: function(e) {throw e;}
+ });
+
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2);
+ expect(contents[0].contents().length).to.equal(1);
+ expect(contents[0].contents()[0].getText()).to.equal('a');
+ expect(contents[1].contents().length).to.equal(1);
+ expect(contents[1].contents()[0].getText()).to.equal('dd');
+ });
+ });
+
+ describe('Splitting text', function() {
+
+ it('splits TextNode\'s parent into two ElementNodes', function() {
+ var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
+ section = doc.root,
+ text = section.contents()[0].contents()[0];
+
+ var returnedValue = text.split({offset: 5});
+ expect(section.contents().length).to.equal(2, 'section has two children');
+
+ var header1 = section.contents()[0];
+ var header2 = section.contents()[1];
+
+ expect(header1.getTagName()).to.equal('header', 'first section child ok');
+ expect(header1.contents().length).to.equal(1, 'first header has one child');
+ expect(header1.contents()[0].getText()).to.equal('Some ', 'first header has correct content');
+ expect(header2.getTagName()).to.equal('header', 'second section child ok');
+ expect(header2.contents().length).to.equal(1, 'second header has one child');
+ expect(header2.contents()[0].getText()).to.equal('header', 'second header has correct content');
+
+ expect(returnedValue.first.sameNode(header1)).to.equal(true, 'first node returned');
+ expect(returnedValue.second.sameNode(header2)).to.equal(true, 'second node returned');
+ });
+
+ it('leaves empty copy of ElementNode if splitting at the very beginning', function() {
+ var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
+ section = doc.root,
+ text = section.contents()[0].contents()[0];
+
+ text.split({offset: 0});
+
+ var header1 = section.contents()[0];
+ var header2 = section.contents()[1];
+
+ expect(header1.contents().length).to.equal(0);
+ expect(header2.contents()[0].getText()).to.equal('Some header');
+ });
+
+ it('leaves empty copy of ElementNode if splitting at the very end', function() {
+ var doc = getDocumentFromXML('<section><header>Some header</header></section>'),
+ section = doc.root,
+ text = section.contents()[0].contents()[0];
+
+ text.split({offset: 11});
+
+ var header1 = section.contents()[0];
+ var header2 = section.contents()[1];
+
+ expect(header1.contents()[0].getText()).to.equal('Some header');
+ expect(header2.contents().length).to.equal(0);
+ });
+
+ it('keeps TextNodes\'s parent\'s children elements intact', function() {
+ var doc = getDocumentFromXML('<section><header>A <span>fancy</span> and <span>nice</span> header</header></section>'),
+ section = doc.root,
+ header = section.contents()[0],
+ textAnd = header.contents()[2];
+
+ textAnd.split({offset: 2});
+
+ var sectionContents = section.contents();
+ expect(sectionContents.length).to.equal(2, 'Section has two children');
+ expect(sectionContents[0].getTagName()).to.equal('header', 'First section node is a header');
+ expect(sectionContents[1].getTagName()).to.equal('header', 'Second section node is a header');
+
+ var firstHeaderContents = sectionContents[0].contents();
+ expect(firstHeaderContents.length).to.equal(3, 'First header has three children');
+ expect(firstHeaderContents[0].getText()).to.equal('A ', 'First header starts with a text');
+ expect(firstHeaderContents[1].getTagName()).to.equal('span', 'First header has span in the middle');
+ expect(firstHeaderContents[2].getText()).to.equal(' a', 'First header ends with text');
+
+ var secondHeaderContents = sectionContents[1].contents();
+ expect(secondHeaderContents.length).to.equal(3, 'Second header has three children');
+ expect(secondHeaderContents[0].getText()).to.equal('nd ', 'Second header starts with text');
+ expect(secondHeaderContents[1].getTagName()).to.equal('span', 'Second header has span in the middle');
+ expect(secondHeaderContents[2].getText()).to.equal(' header', 'Second header ends with text');
+ });
+ });
+
+ describe('Events', function() {
+ it('emits nodeDetached event on node detach', function() {
+ var node = elementNodeFromXML('<div><div></div></div>'),
+ innerNode = node.contents()[0],
+ spy = sinon.spy();
+ node.document.on('change', spy);
+
+ var detached = innerNode.detach(),
+ event = spy.args[0][0];
+
+ expect(event.type).to.equal('nodeDetached');
+ expect(event.meta.node.sameNode(detached, 'detached node in event meta'));
+ expect(event.meta.parent.sameNode(node), 'original parent node in event meta');
+ }),
+
+ 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('emits nodeDetached/nodeAdded events with `move` flag 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);
+
+ var appended = a.append(b),
+ detachedEvent = spy.args[0][0],
+ addedEvent = spy.args[1][0];
+
+ expect(spy.callCount).to.equal(2);
+ expect(detachedEvent.type).to.equal('nodeDetached');
+ expect(detachedEvent.meta.node.sameNode(appended)).to.be.true;
+ expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+ expect(addedEvent.type).to.equal('nodeAdded');
+ expect(addedEvent.meta.node.sameNode(appended)).to.be.true;
+ expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
+
+ });
+
+ 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('emits nodeDetached/nodeAdded events with `move` flag 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);
+
+ var prepended = a.prepend(b),
+ detachedEvent = spy.args[0][0],
+ addedEvent = spy.args[1][0];
+
+ expect(spy.callCount).to.equal(2);
+ expect(detachedEvent.type).to.equal('nodeDetached');
+ expect(detachedEvent.meta.node.sameNode(prepended)).to.be.true;
+ expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+ expect(addedEvent.type).to.equal('nodeAdded');
+ expect(addedEvent.meta.node.sameNode(prepended)).to.be.true;
+ expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
+ });
+
+ 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('emits nodeDetached/nodeAdded events with `move` flag 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);
+ var inserted = b.after(a),
+ detachedEvent = spy.args[0][0],
+ addedEvent = spy.args[1][0];
+
+ expect(spy.callCount).to.equal(2);
+ expect(detachedEvent.type).to.equal('nodeDetached');
+ expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
+ expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+ expect(addedEvent.type).to.equal('nodeAdded');
+ expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
+ expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
+ });
+
+ 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('emits nodeDetached/nodeAdded events with `move` flag 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);
+ var inserted = a.before(b),
+ detachedEvent = spy.args[0][0],
+ addedEvent = spy.args[1][0];
+
+ expect(spy.callCount).to.equal(2);
+ expect(detachedEvent.type).to.equal('nodeDetached');
+ expect(detachedEvent.meta.node.sameNode(inserted)).to.be.true;
+ expect(detachedEvent.meta.move).to.equal(true, 'move flag set to true for nodeDetachedEvent');
+ expect(addedEvent.type).to.equal('nodeAdded');
+ expect(addedEvent.meta.node.sameNode(inserted)).to.be.true;
+ expect(addedEvent.meta.move).to.equal(true, 'move flag set to true for nodeAddedEvent');
+ });
+
+ it('emits nodeDetached and nodeAdded when replacing root node with another', function() {
+ var doc = getDocumentFromXML('<a></a>'),
+ oldRoot = doc.root,
+ spy = sinon.spy();
+
+ doc.on('change', spy);
+
+ doc.root.replaceWith({tagName: 'b'});
+
+ expect(spy.callCount).to.equal(2);
+
+ var event1 = spy.args[0][0],
+ event2 = spy.args[1][0];
+
+ expect(event1.type).to.equal('nodeDetached');
+ expect(event1.meta.node.sameNode(oldRoot)).to.equal(true, 'root node in nodeDetached event metadata');
+ expect(event2.type).to.equal('nodeAdded');
+ expect(event2.meta.node.sameNode(doc.root)).to.equal(true, 'new root node in nodelAdded event meta');
+ });
+
+
+ ['append', 'prepend', 'before', 'after'].forEach(function(insertionMethod) {
+ it('emits nodeDetached for node moved from a document tree to out of document node ' + insertionMethod, function() {
+ var doc = getDocumentFromXML('<div><a></a></div>'),
+ a = doc.root.contents()[0],
+ spy = sinon.spy();
+
+ doc.on('change', spy);
+
+ var newNode = doc.createDocumentNode({tagName: 'b'}),
+ newNodeInner = newNode.append({tagName:'c'});
+
+ newNodeInner[insertionMethod](a);
+
+ var event = spy.args[0][0];
+ expect(event.type).to.equal('nodeDetached');
+ expect(event.meta.node.sameNode(a));
+ });
+
+ it('doesn\'t emit nodeDetached event for already out of document node moved to out of document node' + insertionMethod, function() {
+ var doc = getDocumentFromXML('<div><a></a></div>'),
+ spy = sinon.spy();
+
+ doc.on('change', spy);
+
+ var newNode = doc.createDocumentNode({tagName: 'b'});
+ newNode.append({tagName:'c'});
+
+ expect(spy.callCount).to.equal(0);
+ });
+ });
+
+
+ });
+
+ describe('Traversing', function() {
+ describe('Basic', function() {
+ it('can access node parent', function() {
+ var doc = getDocumentFromXML('<a><b></b></a>'),
+ a = doc.root,
+ b = a.contents()[0];
+
+ expect(a.parent()).to.equal(null, 'parent of a root is null');
+ expect(b.parent().sameNode(a)).to.be.true;
+ });
+ it('can access node parents', function() {
+ var doc = getDocumentFromXML('<a><b><c></c></b></a>'),
+ a = doc.root,
+ b = a.contents()[0],
+ c = b.contents()[0];
+
+ var parents = c.parents();
+ // @@
+ expect(parents[0].sameNode(b)).to.be.true;
+ expect(parents[1].sameNode(a)).to.be.true;
+ });
+ });
+
+ describe('finding sibling parents of two elements', function() {
+ it('returns elements themself if they have direct common parent', function() {
+ var doc = getDocumentFromXML('<section><div><div>A</div><div>B</div></div></section>'),
+ wrappingDiv = doc.root.contents()[0],
+ divA = wrappingDiv.contents()[0],
+ divB = wrappingDiv.contents()[1];
+
+ var siblingParents = doc.getSiblingParents({node1: divA, node2: divB});
+
+ expect(siblingParents.node1.sameNode(divA)).to.equal(true, 'divA');
+ expect(siblingParents.node2.sameNode(divB)).to.equal(true, 'divB');
+ });
+
+ it('returns sibling parents - example 1', function() {
+ var doc = getDocumentFromXML('<section>Alice <span>has a cat</span></section>'),
+ aliceText = doc.root.contents()[0],
+ span = doc.root.contents()[1],
+ spanText = span.contents()[0];
+
+ var siblingParents = doc.getSiblingParents({node1: aliceText, node2: spanText});
+
+ expect(siblingParents.node1.sameNode(aliceText)).to.equal(true, 'aliceText');
+ expect(siblingParents.node2.sameNode(span)).to.equal(true, 'span');
+ });
+
+ it('returns node itself for two same nodes', function() {
+ var doc = getDocumentFromXML('<section><div></div></section>'),
+ div = doc.root.contents()[0];
+
+ var siblingParents = doc.getSiblingParents({node1: div, node2: div});
+ expect(!!siblingParents.node1 && !!siblingParents.node2).to.equal(true, 'nodes defined');
+ expect(siblingParents.node1.sameNode(div)).to.be.equal(true, 'node1');
+ expect(siblingParents.node2.sameNode(div)).to.be.equal(true, 'node2');
+ });
+ });
+ });
+
+ 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>',
+ doc = getDocumentFromXML(xmlIn),
+ xmlOut = doc.toXML();
+
+ var parser = new DOMParser(),
+ input = parser.parseFromString(xmlIn, 'application/xml').childNodes[0],
+ output = parser.parseFromString(xmlOut, 'application/xml').childNodes[0];
+
+ expect(input.isEqualNode(output)).to.be.true;
+ });
+
+ it('keeps entities intact', function() {
+ var xmlIn = '<section>< ></section>',
+ doc = getDocumentFromXML(xmlIn),
+ xmlOut = doc.toXML();
+ expect(xmlOut).to.equal(xmlIn);
+ });
+ it('keeps entities intact when they form html/xml', function() {
+ var xmlIn = '<section><abc></section>',
+ doc = getDocumentFromXML(xmlIn),
+ xmlOut = doc.toXML();
+ expect(xmlOut).to.equal(xmlIn);
+ });
+ });
+
+ describe('Extension API', function() {
+ var doc, extension, elementNode, textNode;
+
+ beforeEach(function() {
+ doc = getDocumentFromXML('<section>Alice<div class="test_class"></div></section>');
+ });
+
+ it('allows adding method to a document', function() {
+ extension = {document: {methods: {
+ testMethod: function() { return this; }
+ }}};
+
+ doc.registerExtension(extension);
+ expect(doc.testMethod()).to.equal(doc, 'context is set to a document instance');
+ });
+
+ it('allows adding transformation to a document', function() {
+ extension = {document: {transformations: {
+ testTransformation: function() { return this; },
+ testTransformation2: {impl: function() { return this;}}
+ }}};
+
+ doc.registerExtension(extension);
+ expect(doc.testTransformation()).to.equal(doc, 'context is set to a document instance');
+ expect(doc.testTransformation2()).to.equal(doc, 'context is set to a document instance');