1 define(function(require) {
4 /* global describe, it */
8 var chai = require('libs/chai'),
9 smartxml = require('./smartxml.js');
12 var expect = chai.expect;
14 var getDocumentFromXML = function(xml) {
15 return smartxml.documentFromXML(xml);
18 describe('Fragments API', function() {
19 describe('node fragment', function() {
20 it('describes a single node', function() {
21 var doc = getDocumentFromXML('<section></section');
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;
29 describe('caret fragment', function() {
30 it('describes place in a text', function() {
31 var doc = getDocumentFromXML('<section>Alice</section>');
33 var fragment = doc.createFragment(doc.CaretFragment, {node: doc.root.contents()[0], offset: 1});
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);
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];
47 var fragment = doc.createFragment(doc.TextRangeFragment, {
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);
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];
66 var fragment = doc.createFragment(doc.TextRangeFragment, {
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);