add comments
[fnpeditor.git] / src / smartxml / fragments.test.js
1 define(function(require) {
2     
3 'use strict';
4 /* global describe, it */
5 /* jshint expr:true */
6
7
8 var chai = require('libs/chai'),
9     smartxml = require('./smartxml.js');
10
11
12 var expect = chai.expect;
13
14 var getDocumentFromXML = function(xml) {
15     return smartxml.documentFromXML(xml);
16 };
17
18 describe('Fragments API', function() {
19     describe('node fragment', function() {
20         it('describes a single node', function() {
21             var doc = getDocumentFromXML('<section></section');
22
23             var fragment = doc.createFragment(doc.NodeFragment, {node:doc.root});
24             expect(fragment instanceof fragment.NodeFragment).to.be.true;
25             expect(fragment.node.sameNode(doc.root)).to.be.true;
26         });
27     });
28
29     describe('caret fragment', function() {
30         it('describes place in a text', function() {
31             var doc = getDocumentFromXML('<section>Alice</section>');
32
33             var fragment = doc.createFragment(doc.CaretFragment, {node: doc.root.contents()[0], offset: 1});
34
35             expect(fragment instanceof fragment.CaretFragment).to.be.true;
36             expect(fragment instanceof fragment.NodeFragment).to.be.true;
37             expect(fragment.node.getText()).to.equal('Alice');
38             expect(fragment.offset).to.equal(1);
39         });
40     });
41
42     describe('text range fragment', function() {
43         it('describes fragment of a text node', function() {
44             var doc = getDocumentFromXML('<section>Alice</section>'),
45                 textNode = doc.root.contents()[0];
46
47             var fragment = doc.createFragment(doc.TextRangeFragment, {
48                 node1: textNode,
49                 offset1: 4,
50                 node2: textNode,
51                 offset2: 1
52             });
53
54             expect(fragment instanceof fragment.TextRangeFragment).to.be.true;
55             expect(fragment instanceof fragment.RangeFragment).to.be.true;
56             expect(fragment.startNode.getText()).to.equal('Alice');
57             expect(fragment.startOffset).to.equal(1);
58             expect(fragment.endNode.getText()).to.equal('Alice');
59             expect(fragment.endOffset).to.equal(4);
60         });
61         it('describes text spanning multiple nodes', function() {
62             var doc = getDocumentFromXML('<section>Alice <span>has</span> a cat!</section>'),
63                 textNode1 = doc.root.contents()[0],
64                 textNode2 = doc.root.contents()[2];
65
66             var fragment = doc.createFragment(doc.TextRangeFragment, {
67                 node1: textNode2,
68                 offset1: 4,
69                 node2: textNode1,
70                 offset2: 1
71             });
72
73             expect(fragment instanceof fragment.TextRangeFragment).to.be.true;
74             expect(fragment.startNode.getText()).to.equal('Alice ');
75             expect(fragment.startOffset).to.equal(1);
76             expect(fragment.endNode.getText()).to.equal(' a cat!');
77             expect(fragment.endOffset).to.equal(4);
78         });
79     });
80 });
81
82 });