X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/8f3efc2235f836dd2b624d569d97a7ae0dad77ff..f098ff9873de73d1fd6d71ce64b83ae318e8bd7e:/src/editor/plugins/core/core.test.js diff --git a/src/editor/plugins/core/core.test.js b/src/editor/plugins/core/core.test.js index 940e22e..ed61328 100644 --- a/src/editor/plugins/core/core.test.js +++ b/src/editor/plugins/core/core.test.js @@ -4,6 +4,7 @@ define(function(require) { /* globals describe, it */ var chai = require('libs/chai'), + sinon = require('libs/sinon'), wlxml = require('wlxml/wlxml'), corePlugin = require('./core.js'), expect = chai.expect; @@ -14,6 +15,40 @@ var getDocumentFromXML = function(xml, options) { return doc; }; +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('Document extensions', function() { describe('break content', function() { @@ -53,6 +88,49 @@ describe('Document extensions', function() { expect(result.emptyText.getText()).to.equal(''); }); }); + + describe('mergin text with preceding content', function() { + it('does nothing if text node parent has no preceding element', function() { + var doc = getDocumentFromXML('
some text
'), + text = getTextNode('some text', doc), + spy = sinon.spy(); + + doc.on('change', spy); + text.mergeContentUp(); + expect(spy.callCount).to.equal(0); + }); + it('does nothing if text node parent is precedeed by text node', function() { + var doc = getDocumentFromXML('
another text
some text
'), + text = getTextNode('some text', doc), + spy = sinon.spy(); + + doc.on('change', spy); + text.mergeContentUp(); + expect(spy.callCount).to.equal(0); + }); + it('does nothing if text node is not first child of its parent', function() { + var doc = getDocumentFromXML('
some text
'), + text = getTextNode('some text', doc), + spy = sinon.spy(); + + doc.on('change', spy); + text.mergeContentUp(); + expect(spy.callCount).to.equal(0); + }); + it('moves text node and its siblings to the block element preceding text node parent', function() { + var doc = getDocumentFromXML('
some textis here!
'), + text = getTextNode('some text', doc); + + text.mergeContentUp(); + + var contents = doc.root.contents(); + expect(contents.length).to.equal(1); + expect(contents[0].contents().length).to.equal(3); + expect(contents[0].contents()[0].getText()).to.equal('some text'); + expect(contents[0].contents()[1].getTagName()).to.equal('span'); + expect(contents[0].contents()[2].getText()).to.equal(' here!'); + }); + }); });