smartxml: fix ranges returned by unwrapContents
[fnpeditor.git] / src / editor / plugins / core / core.test.js
1 define(function(require) {
2     
3 'use strict';
4 /* globals describe, it */
5
6 var chai = require('libs/chai'),
7     wlxml = require('wlxml/wlxml'),
8     corePlugin = require('./core.js'),
9     expect = chai.expect;
10
11 var getDocumentFromXML = function(xml, options) {
12     var doc = wlxml.WLXMLDocumentFromXML(xml, options || {});
13     doc.registerExtension(corePlugin.documentExtension);
14     return doc;
15 };
16
17
18 describe('Document extensions', function() {
19     describe('break content', function() {
20         it('break text into two nodes', function() {
21             var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
22                 textNode = doc.root.contents()[0].contents()[0];
23             
24             var result = textNode.breakContent({offset:3});
25
26             var section = doc.root;
27             expect(section.contents().length).to.equal(2);
28             expect(section.contents()[0].contents()[0].getText()).to.equal('Ali');
29             expect(section.contents()[1].contents()[0].getText()).to.equal('ce');
30
31             expect(result.first.sameNode(section.contents()[0])).to.equal(true);
32             expect(result.second.sameNode(section.contents()[1])).to.equal(true);
33             expect(result.emptyText).to.equal(undefined, 'no new text node created');
34         });
35         it('puts empty text node when breaking at the very beginning', function() {
36             var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
37                 textNode = doc.root.contents()[0].contents()[0];
38             
39             var result = textNode.breakContent({offset:0}),
40                 firstNode = doc.root.contents()[0];
41
42             expect(result.emptyText.sameNode(firstNode.contents()[0]));
43             expect(result.emptyText.getText()).to.equal('');
44         });
45         it('puts empty text node when breaking at the very end', function() {
46             var doc = getDocumentFromXML('<section><div>Alice</div></section>'),
47             textNode = doc.root.contents()[0].contents()[0];
48             
49             var result = textNode.breakContent({offset:5}),
50                 secondNode = doc.root.contents()[1];
51             
52             expect(result.emptyText.sameNode(secondNode.contents()[0]));
53             expect(result.emptyText.getText()).to.equal('');
54         });
55     });
56 });
57
58
59 });