Annotation of kupu/tests/test_kupuhelpers.js, revision 1.1.1.1
1.1 dwinter 1: /*****************************************************************************
2: *
3: * Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
4: *
5: * This software is distributed under the terms of the Kupu
6: * License. See LICENSE.txt for license text. For a list of Kupu
7: * Contributors see CREDITS.txt.
8: *
9: *****************************************************************************/
10:
11: // $Id: test_kupuhelpers.js 9982 2005-03-21 09:53:57Z yuppie $
12:
13: function SelectionTestCase() {
14:
15: this.assertEquals = function(var1, var2, message) {
16: /* assert whether 2 vars have the same value */
17: // XXX: this should be removed again when issue 188 is resolved
18: // toSource() of Mozilla objects returns always the same
19: if (!message) {
20: message = '';
21: } else {
22: message = "'" + message + "' ";
23: }
24: if (var1 != var2) {
25: throw('Assertion '+message+'failed: ' + var1 + ' != ' + var2);
26: };
27: };
28:
29: var visibleEmptyElements = new Array('IMG', 'BR', 'HR');
30: var blockElements = new Array('P', 'DIV');
31:
32: this.setUp = function() {
33: var iframe = document.getElementById('iframe');
34: this.doc = iframe.contentWindow.document;
35: this.body = this.doc.getElementsByTagName('body')[0];
36: this.kupudoc = new KupuDocument(iframe);
37: this.selection = this.kupudoc.getSelection();
38: this.kupudoc.getWindow().focus();
39: };
40:
41: this._MozillaPosition = function(element, offset, lastnode) {
42: // this does not skip invisible whitespace
43: var node = element.firstChild;
44: for (var i=0; i < element.childNodes.length; i++) {
45: if (node.nodeType == node.TEXT_NODE) {
46: var endcounts = !node.nextSibling &&
47: blockElements.contains(element.tagName.toUpperCase());
48: if ((offset < node.length) ||
49: ((offset == node.length) && (!lastnode || endcounts))) {
50: return [node, offset];
51: };
52: if (endcounts) {
53: offset -= 1;
54: };
55: offset -= node.length;
56: } else if (node.nodeType == node.ELEMENT_NODE) {
57: if (visibleEmptyElements.contains(node.tagName.toUpperCase())) {
58: if (offset > 0 && !node.nextSibling) {
59: offset -= 1;
60: i += 1;
61: };
62: if (offset == 0) {
63: return [element, i];
64: };
65: offset -= 1;
66: } else {
67: position = this._MozillaPosition(node, offset, lastnode);
68: if (position[0]) {
69: return position;
70: };
71: offset = position[1];
72: };
73: };
74: node = node.nextSibling;
75: };
76: return [node, offset];
77: };
78:
79: this._setSelection = function(startOffset, startNextNode, endOffset,
80: endNextNode, verificationString) {
81: var element = this.body;
82: var innerSelection = this.selection.selection;
83: if (_SARISSA_IS_IE) {
84: var range = innerSelection.createRange();
85: var endrange = innerSelection.createRange();
86: range.moveToElementText(element);
87: range.moveStart('character', startOffset);
88: endrange.moveToElementText(element);
89: endrange.moveStart('character', endOffset);
90: range.setEndPoint('EndToStart', endrange);
91: range.select();
92: } else {
93: var position = this._MozillaPosition(element, startOffset,
94: startNextNode);
95: innerSelection.collapse(position[0], position[1]);
96: if (startOffset != endOffset) {
97: var position = this._MozillaPosition(element, endOffset,
98: endNextNode);
99: innerSelection.extend(position[0], position[1]);
100: };
101: };
102: this.assertEquals('"'+this.selection.toString().replace(/\r|\n/g, '')+'"',
103: '"'+verificationString+'"');
104: };
105:
106: this._cleanHtml = function(s) {
107: s = s.toLowerCase().replace(/[\r\n]/g, "");
108: s = s.replace(/\>[ ]+\</g, "><");
109: return s;
110: };
111:
112: this.tearDown = function() {
113: this.body.innerHTML = '';
114: };
115: };
116:
117: SelectionTestCase.prototype = new TestCase;
118:
119: function KupuHelpersTestCase() {
120: this.name = 'KupuHelpersTestCase';
121:
122: this.setUp = function() {
123: var iframe = document.getElementById('iframe');
124: this.doc = iframe.contentWindow.document;
125: this.body = this.doc.getElementsByTagName('body')[0];
126: this._testdiv = document.getElementById('testdiv');
127: };
128:
129: this.testSelectSelectItem = function() {
130: var select = this.doc.createElement('select');
131: this.body.appendChild(select);
132: var option = this.doc.createElement('option');
133: option.value = 'foo';
134: select.appendChild(option);
135: var option2 = this.doc.createElement('option');
136: option2.value = 'bar';
137: select.appendChild(option2);
138:
139: this.assertEquals(select.selectedIndex, 0);
140: var ret = selectSelectItem(select, 'bar');
141: this.assertEquals(select.selectedIndex, 1);
142: var ret = selectSelectItem(select, 'baz');
143: this.assertEquals(select.selectedIndex, 0);
144: };
145:
146: this.testArrayContains = function() {
147: var array = new Array(1, 2, 3);
148: this.assert(array.contains(1));
149: this.assert(array.contains(2));
150: this.assertFalse(array.contains(4));
151: this.assert(array.contains('1'));
152: this.assertFalse(array.contains('1', 1));
153: };
154:
155: this.testStringStrip = function() {
156: // an empty string
157: var str = "";
158: this.assertEquals(str.strip(), str);
159: // a string only containg whitespace
160: str = " \n \t ";
161: this.assertEquals(str.strip(), "");
162: // a string not containg any whitespaces
163: str = "foo"
164: this.assertEquals(str.strip(), str);
165: // a word wrapped around whitespace
166: str = "\n foo \t ";
167: // a single character wrapped in whitespace
168: str = "\n\t a \t\n";
169: this.assertEquals(str.strip(), "a");
170: // a string containing whitespace in the middle
171: str = "foo bar baz";
172: this.assertEquals(str.strip(), str);
173: // a string containing spaces around it and in it
174: str = " \t foo bar\n baz ";
175: this.assertEquals(str.strip(), "foo bar\n baz");
176: str = " tu quoque Brute filie mee ";
177: this.assertEquals(str.strip(), "tu quoque Brute filie mee");
178: };
179:
180: this.testLoadDictFromXML = function() {
181: var dict = loadDictFromXML(document, 'xmlisland');
182: this.assertEquals(dict['foo'], 'bar');
183: this.assertEquals(dict['sna'], 'fu');
184: for (var attr in dict) {
185: this.assert(attr == 'foo' || attr == 'sna' ||
186: attr == 'some_int' || attr == 'nested' ||
187: attr == 'list');
188: };
189: this.assertEquals(dict['some_int'], 1);
190: this.assertEquals(dict['nested']['foo'], 'bar');
191: this.assertEquals(dict['list'][0], 0);
192: this.assertEquals(dict['list'].length, 2);
193: };
194:
195: this.testGetFromSelector = function() {
196: data = '<div><span id="xspan" class="xyzzy"></span></div>';
197: this._testdiv.innerHTML = data;
198: node = getFromSelector("xspan");
199: this.assertEquals(node && node.id, "xspan");
200: node = getFromSelector("#testdiv span.xyzzy");
201: this.assertEquals(node && node.id, "xspan");
202: data = '<div><button class="xyzzy"></button><span id="xspan" class="foo xyzzy bar"></span></div>';
203: this._testdiv.innerHTML = data;
204: node = getFromSelector("#testdiv span.xyzzy");
205: this.assertEquals(node && node.id, "xspan");
206: };
207:
208: this.tearDown = function() {
209: this.body.innerHTML = '';
210: this._testdiv.innerHTML = '';
211: };
212: };
213:
214: KupuHelpersTestCase.prototype = new TestCase;
215:
216: function KupuSelectionTestCase() {
217:
218: this.testReplaceWithNode = function() {
219: this.body.innerHTML = '<p>foo bar baz</p>';
220: // select |bar|
221: this._setSelection(4, null, 7, null, 'bar');
222: node = this.doc.createElement('img');
223: this.selection.replaceWithNode(node, true);
224: this.assertEquals(this.body.innerHTML.toLowerCase(), '<p>foo <img> baz</p>');
225: };
226:
227: this.testReplaceWithNodeTwice = function() {
228: this.body.innerHTML = '<p>foo bar baz</p>';
229: // select |bar|
230: this._setSelection(4, null, 7, null, 'bar');
231: node = this.doc.createElement('img');
232: this.selection.replaceWithNode(node, true);
233: this.selection.replaceWithNode(node, true);
234: this.assertEquals(this.body.innerHTML.toLowerCase(), '<p>foo <img> baz</p>');
235: };
236:
237: this.testParentElementMissing = function() {
238: this.body.innerHTML = '<p>foo <b>bar</b><img><img> baz</p>';
239: // remove selection
240: var selection = this.selection.selection;
241: _SARISSA_IS_IE ? selection.empty() : selection.removeAllRanges();
242: node = this.doc.getElementsByTagName('p')[0];
243: this.assertEquals(this.selection.parentElement(), node);
244: };
245:
246: this.testParentElementBold = function() {
247: this.body.innerHTML = '<p>foo <b>bar</b><img/><img/> baz</p>';
248: // select |bar|
249: this._setSelection(4, true, 7, false, 'bar');
250: node = this.doc.getElementsByTagName('b')[0];
251: this.assertEquals(this.selection.parentElement(), node);
252: };
253:
254: this.testParentElementImg = function() {
255: this.body.innerHTML = '<p>foo <b>bar</b><img/><img/> baz</p>';
256: // select |<img/>|
257: this._setSelection(7, true, 8, false, '');
258: node = this.doc.getElementsByTagName('img')[0];
259: this.assertEquals(this.selection.parentElement(), node);
260: };
261:
262: this.testParentElementImgSpecial = function() {
263: this.body.innerHTML = '<p>foo <a><img/></a></p>';
264: // select |<img/>|
265: this._setSelection(4, true, 5, null, '');
266: node = this.doc.getElementsByTagName('img')[0];
267: foo = this.selection.parentElement();
268: this.assertEquals(this.selection.parentElement(), node);
269: };
270:
271: this.testParentElementMixed = function() {
272: this.body.innerHTML = '<p>foo <b>bar</b><img><img> baz</p>';
273: // select |ar</b><img><img> b|
274: this._setSelection(5, null, 11, null, 'ar b');
275: node = this.doc.getElementsByTagName('p')[0];
276: this.assertEquals(this.selection.parentElement(), node);
277: };
278:
279: this.testParentElement_r9516 = function() {
280: this.body.innerHTML = '<p>foo <b>bar</b><img/></p><p>baz</p>';
281: // select |<img/></p><p>baz|
282: this._setSelection(7, true, 12, false, 'baz');
283: node = this.doc.getElementsByTagName('body')[0];
284: this.assertEquals(this.selection.parentElement(), node);
285: };
286:
287: this.testGetContentLength = function() {
288: this.body.innerHTML = '<p>foo bar baz</p>';
289: // select |bar|
290: this._setSelection(4, null, 7, null, 'bar');
291: this.assertEquals(this.selection.getContentLength(), 3);
292: };
293:
294: this.testToString = function() {
295: this.body.innerHTML = '<p>foo <b>bar</b> baz</p>';
296: var selection = this.kupudoc.getSelection();
297: selection.selectNodeContents(this.body);
298: this.assertEquals(selection.toString(), 'foo bar baz');
299: selection.selectNodeContents(this.body.firstChild.childNodes[1]);
300: this.assertEquals(selection.toString(), 'bar');
301: };
302: };
303:
304: KupuSelectionTestCase.prototype = new SelectionTestCase;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>