+ 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');
+ });
+
+ it('allows adding method to a DocumentNode instance', function() {
+ extension = {
+ documentNode: {
+ methods: {
+ testMethod: function() { return this; }
+ }
+ },
+ textNode: {
+ methods: {
+ textTestMethod: function() { return this; }
+ }
+ },
+ elementNode: {
+ methods: {
+ elementTestMethod: function() { return this; }
+ }
+ }
+ };
+
+ doc.registerExtension(extension);
+
+ elementNode = doc.root;
+ textNode = doc.root.contents()[0];
+
+ expect(elementNode.testMethod().sameNode(elementNode)).to.equal(true, 'context is set to a node instance');
+ expect(textNode.testMethod().sameNode(textNode)).to.equal(true, 'context is set to a node instance');
+
+ expect(elementNode.elementTestMethod().sameNode(elementNode)).to.be.true;
+ expect(elementNode.textTestMethod).to.be.undefined;
+
+ expect(textNode.textTestMethod().sameNode(textNode)).to.be.true;
+ expect(textNode.elementTestMethod).to.be.undefined;
+ });
+
+ it('allows adding transformation to a DocumentNode', function() {
+ extension = {
+ documentNode: {
+ transformations: {
+ testTransformation: function() { return this; },
+ testTransformation2: {impl: function() { return this;}}
+ }
+ },
+ textNode: {
+ transformations: {
+ textTestTransformation: function() { return this; }
+ }
+ },
+ elementNode: {
+ transformations: {
+ elementTestTransformation: function() { return this; }
+ }
+ }
+ };
+
+ doc.registerExtension(extension);
+
+ elementNode = doc.root;
+ textNode = doc.root.contents()[0];
+
+ expect(elementNode.testTransformation().sameNode(elementNode)).to.equal(true, '1');
+ expect(elementNode.testTransformation2().sameNode(elementNode)).to.equal(true, '2');
+ expect(textNode.testTransformation().sameNode(textNode)).to.equal(true, '3');
+ expect(textNode.testTransformation2().sameNode(textNode)).to.equal(true, '4');
+
+ expect(elementNode.elementTestTransformation().sameNode(elementNode)).to.be.true;
+ expect(elementNode.textTestTransformation).to.be.undefined;
+
+ expect(textNode.textTestTransformation().sameNode(textNode)).to.be.true;
+ expect(textNode.elementTestTransfomation).to.be.undefined;
+ });
+
+ it('allows text/element node methods and transformations to access node and transormations on document node', function() {
+
+ var doc = getDocumentFromXML('<div>text</div>');