1 define(function(require) {
4 /* globals describe, it */
6 var chai = require('libs/chai'),
7 wlxml = require('wlxml/wlxml'),
8 corePlugin = require('./core.js'),
11 var getDocumentFromXML = function(xml, options) {
12 var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
13 doc.registerExtension(corePlugin.documentExtension);
17 var getTextNodes = function(text, doc) {
20 var search = function(node) {
21 node.contents().forEach(function(node) {
22 if(node.nodeType === Node.TEXT_NODE) {
23 if(node.getText() === text) {
35 var getTextNode = function(text, doc) {
36 var nodes = getTextNodes(text, doc),
38 if(nodes.length === 0) {
39 error = 'Text not found';
40 } else if(nodes.length > 1) {
41 error = 'Text not unique';
42 } else if(nodes[0].getText() !== text) {
43 error = 'I was trying to cheat your test :(';
46 throw new Error(error);
52 describe('Document extensions', function() {
53 describe('break content', function() {
54 it('break text into two nodes', function() {
55 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
56 textNode = doc.root.contents()[0].contents()[0];
58 var result = textNode.breakContent({offset:3});
60 var section = doc.root;
61 expect(section.contents().length).to.equal(2);
62 expect(section.contents()[0].contents()[0].getText()).to.equal('Ali');
63 expect(section.contents()[1].contents()[0].getText()).to.equal('ce');
65 expect(result.first.sameNode(section.contents()[0])).to.equal(true);
66 expect(result.second.sameNode(section.contents()[1])).to.equal(true);
67 expect(result.emptyText).to.equal(undefined, 'no new text node created');
69 it('puts empty text node when breaking at the very beginning', function() {
70 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
71 textNode = doc.root.contents()[0].contents()[0];
73 var result = textNode.breakContent({offset:0}),
74 firstNode = doc.root.contents()[0];
76 expect(result.emptyText.sameNode(firstNode.contents()[0]));
77 expect(result.emptyText.getText()).to.equal('');
79 it('puts empty text node when breaking at the very end', function() {
80 var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
81 textNode = doc.root.contents()[0].contents()[0];
83 var result = textNode.breakContent({offset:5}),
84 secondNode = doc.root.contents()[1];
86 expect(result.emptyText.sameNode(secondNode.contents()[0]));
87 expect(result.emptyText.getText()).to.equal('');
91 describe('merging with adjacent content', function() {
93 describe('when text preceded by element', function() {
94 describe('when text followed by element', function() {
95 it('appends text to the preceding element, following elements stays in place', function() {
96 var doc = getDocumentFromXML('<section><a>A</a>text<b>B</b></section>'),
97 text = getTextNode('text', doc);
99 text.mergeContentUp();
100 var contents = doc.root.contents();
102 expect(contents.length).to.equal(2);
103 expect(contents[0].getTagName()).to.equal('a');
104 expect(contents[0].contents()[0].getText()).to.equal('Atext');
105 expect(contents[1].getTagName()).to.equal('b');
108 describe('when text is a last child', function() {
109 it('appends text to the preceding element', function() {
110 var doc = getDocumentFromXML('<section><a>A</a>text</section>'),
111 text = getTextNode('text', doc);
113 text.mergeContentUp();
114 var contents = doc.root.contents();
116 expect(contents.length).to.equal(1);
117 expect(contents[0].getTagName()).to.equal('a');
118 expect(contents[0].contents()[0].getText()).to.equal('Atext');
123 describe('when text is a first child', function() {
124 describe('when text followed by element', function() {
125 it('appends text and its siblings to the parent preceding element', function() {
126 var doc = getDocumentFromXML('<section><b>B</b><div>text<a>A</a></div></section>'),
127 text = getTextNode('text', doc);
129 text.mergeContentUp();
130 var contents = doc.root.contents();
132 expect(contents.length).to.equal(3);
133 expect(contents[0].getTagName()).to.equal('b');
134 expect(contents[1].getText()).to.equal('text');
135 expect(contents[2].getTagName()).to.equal('a');
137 it('appends text and its siblings after the parent preceding text', function() {
138 var doc = getDocumentFromXML('<section>B<div>text<a>A</a></div></section>'),
139 text = getTextNode('text', doc);
141 text.mergeContentUp();
142 var contents = doc.root.contents();
144 expect(contents.length).to.equal(2);
145 expect(contents[0].getText()).to.equal('Btext');
146 expect(contents[1].getTagName()).to.equal('a');