3 'modules/documentCanvas/canvas/canvas',
4 'modules/documentCanvas/canvas/documentElement',
5 'modules/documentCanvas/canvas/utils',
7 ], function(chai, canvas, documentElement, utils, wlxml) {
11 var expect = chai.expect;
14 var nodeFromXML = function(xml) {
15 return wlxml.WLXMLElementNodeFromXML(xml);
18 var getCanvasFromXML = function(xml) {
19 return canvas.fromXMLDocument(wlxml.WLXMLDocumentFromXML(xml));
23 describe('new Canvas', function() {
24 it('abc', function() {
25 var doc = wlxml.WLXMLDocumentFromXML('<section>Alice <span>has</span> a cat!</div>'),
26 c = canvas.fromXMLDocument(doc);
28 expect(c.doc().children()).to.have.length(3)
32 describe('Cursor', function() {
36 var findTextNode = function(inside, text) {
37 var nodes = inside.find(':not(iframe)').addBack().contents().filter(function() {
38 return this.nodeType === Node.TEXT_NODE && this.data === text;
45 beforeEach(function() {
46 getSelection = sinon.stub(window, 'getSelection');
49 afterEach(function() {
50 getSelection.restore();
53 it('returns position when browser selection collapsed', function() {
54 var c = getCanvasFromXML('<section>Alice has a cat</section>'),
56 text = findTextNode(dom, 'Alice has a cat');
58 expect(text.nodeType).to.equal(Node.TEXT_NODE, 'correct node selected');
59 expect($(text).text()).to.equal('Alice has a cat');
61 getSelection.returns({
68 var cursor = c.getCursor(),
69 position = cursor.getPosition();
71 expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything');
72 expect(position.element.getText()).to.equal('Alice has a cat');
73 expect(position.offset).to.equal(5);
74 expect(position.offsetAtEnd).to.equal(false, 'offset is not at end');
76 getSelection.returns({
84 expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end');
87 it('returns boundries of selection when browser selection not collapsed', function() {
88 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
91 alice: findTextNode(dom, 'Alice '),
92 has: findTextNode(dom, 'has'),
93 cat: findTextNode(dom, ' cat')
95 cursor = c.getCursor(),
96 aliceElement = c.getDocumentElement(text.alice),
97 catElement = c.getDocumentElement(text.cat);
100 {focus: text.alice, focusOffset: 1, anchor: text.cat, anchorOffset: 2, selectionAnchor: catElement},
101 {focus: text.cat, focusOffset: 2, anchor: text.alice, anchorOffset: 1, selectionAnchor: aliceElement}
102 ].forEach(function(s, idx) {
103 getSelection.returns({isColapsed: false, anchorNode: s.anchor, anchorOffset: s.anchorOffset, focusNode: s.focus, focusOffset: s.focusOffset});
105 var selectionStart = cursor.getSelectionStart(),
106 selectionEnd = cursor.getSelectionEnd(),
107 selectionAnchor = cursor.getSelectionAnchor();
109 expect(cursor.isSelecting()).to.equal(true, 'cursor is selecting');
110 expect(selectionStart.element.sameNode(aliceElement)).to.equal(true, '"Alice" is the start of the selection ' + idx);
111 expect(selectionStart.offset).to.equal(1, '"Alice" offset ok' + idx);
112 expect(selectionEnd.element.sameNode(catElement)).to.equal(true, '"Cat" is the start of the selection ' + idx);
113 expect(selectionEnd.offset).to.equal(2, '"Cat" offset ok' + idx);
114 expect(selectionAnchor.element.sameNode(s.selectionAnchor)).to.equal(true, 'anchor ok');
115 expect(selectionAnchor.offset).to.equal(s.anchorOffset, 'anchor offset ok');
119 it('recognizes when browser selection boundries lies in sibling DocumentTextElements', function() {
120 var c = getCanvasFromXML('<section>Alice <span>has</span> a <span>big</span> cat</section>'),
123 alice: findTextNode(dom, 'Alice '),
124 has: findTextNode(dom, 'has'),
125 a: findTextNode(dom, ' a '),
126 big: findTextNode(dom, 'big'),
127 cat: findTextNode(dom, ' cat'),
129 cursor = c.getCursor();
131 expect($(text.alice).text()).to.equal('Alice ');
132 expect($(text.has).text()).to.equal('has');
133 expect($(text.a).text()).to.equal(' a ');
134 expect($(text.big).text()).to.equal('big');
135 expect($(text.cat).text()).to.equal(' cat');
137 getSelection.returns({anchorNode: text.alice, focusNode: text.a});
138 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "a" are children');
140 getSelection.returns({anchorNode: text.alice, focusNode: text.cat});
141 expect(cursor.isSelectingSiblings()).to.equal(true, '"Alice" and "cat" are children');
143 getSelection.returns({anchorNode: text.alice, focusNode: text.has});
144 expect(cursor.isSelectingSiblings()).to.equal(false, '"Alice" and "has" are not children');
146 getSelection.returns({anchorNode: text.has, focusNode: text.big});
147 expect(cursor.isSelectingSiblings()).to.equal(false, '"has" and "big" are not children');