3 <META HTTP-EQUIV=
"CONTENT-TYPE" CONTENT=
"text/html; charset=UTF-8">
4 <title>Test for Range.expand()
</title>
8 var li
= document
.createElement("li");
9 li
.appendChild(document
.createTextNode(str
));
10 var console
= document
.getElementById("console");
11 console
.appendChild(li
);
14 function assertEqual(test_name
, actual
, expected
)
16 if (actual
!= expected
) {
17 log("==================================");
18 log("FAILED test " + test_name
);
19 log("actual: " + actual
);
20 log("expected: " + expected
);
21 if (actual
.length
!= expected
.length
) {
22 log("length !=" + "; actual.length: " + actual
.length
+ "; expected.length: " + expected
.length
);
23 log("escaped actual: " + escape(actual
));
24 log("escaped expected: " + escape(expected
));
25 var length
= actual
.length
;
26 if (actual
.length
> expected
.length
)
27 length
= expected
.length
;
28 for (var i
=0; i
<length
; ++i
) {
29 log("i: " + i
+ "; actual: " + actual
[i
] + "; expected: " + expected
[i
]);
30 if (actual
[i
] != expected
[i
])
35 log("actual: " + escape(actual
));
36 log("expected: " + escape(expected
));
37 for (var i
=0; i
<actual
.length
; ++i
) {
38 log("i: " + i
+ "; actual: " + actual
[i
] + "; expected: " + expected
[i
]);
39 if (actual
[i
] != expected
[i
])
47 function expandRangeString(range
, startId
, startOffset
, endId
, endOffset
, expandUnit
, firstChild
)
49 var startNode
= document
.getElementById(startId
);
50 if (firstChild
== true)
51 startNode
= startNode
.firstChild
;
52 var endNode
= document
.getElementById(endId
);
53 if (firstChild
== true)
54 endNode
= endNode
.firstChild
;
55 range
.setStart(startNode
, startOffset
);
56 range
.setEnd(endNode
, endOffset
);
57 range
.expand(expandUnit
);
58 return range
.toString();
63 if (window
.testRunner
)
64 testRunner
.dumpAsText();
66 // test 1 - Expand to document.
67 var range
= document
.createRange();
68 var actual
= expandRangeString(range
, 'myspan', 0, 'myspan', 0, 'document', false);
69 var expected
= 'This is the begin of a block. A block is a collection of sentences. Each sentence begins with capital\x0Aletter and ends with a punctuation.\x0A\x0ANow, a sentence\x0Ais divided into\x0Amultiple lines.\x0A\x0ARoll the mouse around the page. The coordinates\x0Aof the mouse pointer are currently atop an elementwhose ID \x0Ais:"".\x0A\x0A\x0A\x0A Cell A1\x0A Cell B1\x0A\x0A\x0A Cell A2\x0A Cell B2\x0A\x0A\x0ASection 1\x0Atext.\x0ASection 2\x0Atext.\x0A\x0AHere is a positioned element.\x0A在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。\x0Aבלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"'
70 assertEqual("document1", actual
, expected
);
72 // test 2 - Expand to block.
73 actual
= expandRangeString(range
, 'td_A1', 0, 'td_A1', 0, 'block', false);
74 assertEqual("block2", actual
, "Cell A1");
76 // test 3 - Expand to block, different range start and end offset.
77 actual
= expandRangeString(range
, 'td_A1', 1, 'td_A1', 2, 'block', true);
78 assertEqual("block3", actual
, "Cell A1");
80 // test 4 - Expand to block, different range start and end node.
81 actual
= expandRangeString(range
, 'td_A1', 0, 'td_B1', 0, 'block', false);
82 assertEqual("block4", actual
, "Cell A1\x0A Cell B1");
84 // test 5 - Expand to sentence.
85 actual
= expandRangeString(range
, 'instructions', 0, 'instructions', 0, 'sentence', false);
86 assertEqual("sentence5", actual
, "Roll the mouse around the page. ");
88 // test 6 - Expand to sentence, different range start and end offset.
89 actual
= expandRangeString(range
, 'instructions', 1, 'instructions', 2, 'sentence', true);
90 assertEqual("sentence6", actual
, "Roll the mouse around the page. ");
92 // test 7 - Expand to sentence, different range start and end node.
93 actual
= expandRangeString(range
, 'instructions', 0, 'mybr', 0, 'sentence', false);
94 assertEqual("sentence7", actual
, "Roll the mouse around the page. The coordinates\x0Aof the mouse pointer are currently atop an element");
96 // test 8 - Sentence does not go upstream?
97 actual
= expandRangeString(range
, 'multilineSentence', 0, 'multilineSentence', 0, 'sentence', false);
98 assertEqual("sentence8", actual
, "Now, a sentence\x0Ais divided into\x0Amultiple lines.");
100 actual
= expandRangeString(range
, 'multilineSentence', 1, 'multilineSentence', 5, 'sentence', true);
101 assertEqual("sentence9", actual
, "Now, a sentence\x0Ais divided into\x0Amultiple lines.");
103 actual
= expandRangeString(range
, 'multilineSentence', 20, 'multilineSentence', 20, 'sentence', true);
104 assertEqual("sentence10", actual
, "is divided into\x0Amultiple lines.");
106 actual
= expandRangeString(range
, 'multilineSentence', 40, 'multilineSentence', 40, 'sentence', true);
107 assertEqual("sentence11", actual
, "multiple lines.");
110 // Same range start and end, both at the begin of word.
111 actual
= expandRangeString(range
, 'mydiv', 0, 'mydiv', 0, 'word', false);
112 assertEqual("word1", actual
, "Here");
114 // Same range start and end, both at the begin of a word.
115 actual
= expandRangeString(range
, 'mydiv', 1, 'mydiv', 1, 'word', true);
116 assertEqual("word2", actual
, "Here");
118 // Same range start and end, both at the middle of a word.
119 actual
= expandRangeString(range
, 'mydiv', 3, 'mydiv', 3, 'word', true);
120 assertEqual("word3", actual
, "Here");
122 // Same range start and end, both at the end of a word.
123 actual
= expandRangeString(range
, 'mydiv', 4, 'mydiv', 4, 'word', true);
124 assertEqual("word4", actual
, "Here");
126 // Different range start and end offset, both at the middle of a word.
127 actual
= expandRangeString(range
, 'mydiv', 2, 'mydiv', 3, 'word', true);
128 assertEqual("word5", actual
, "Here");
130 // Different range start and end offset, start at the begin of a word,
131 // and end at the end of a word.
132 actual
= expandRangeString(range
, 'mydiv', 5, 'mydiv', 4, 'word', true);
133 assertEqual("word6", actual
, "Here");
135 // Different range start and end offset, start at the begin of a word,
136 // and end at the middle of a word.
137 actual
= expandRangeString(range
, 'mydiv', 1, 'mydiv', 3, 'word', true);
138 assertEqual("word7", actual
, "Here");
140 // Different range start and end offset, start at the middle of a word,
141 // and end at the end of a word.
142 actual
= expandRangeString(range
, 'mydiv', 2, 'mydiv', 4, 'word', true);
143 assertEqual("word8", actual
, "Here");
145 // Across more than 1 word.
146 // Start at the begin of 1st word, end at the end of last word.
147 actual
= expandRangeString(range
, 'mydiv', 1, 'mydiv', 7, 'word', true);
148 assertEqual("word9", actual
, "Here is");
150 // Start at the middle of 1st word, end at the middle of last word.
151 actual
= expandRangeString(range
, 'mydiv', 2, 'mydiv', 6, 'word', true);
152 assertEqual("word10", actual
, "Here is");
154 // Start at the begin of 1st word, end at the middle of last word.
155 actual
= expandRangeString(range
, 'mydiv', 1, 'mydiv', 6, 'word', true);
156 assertEqual("word11", actual
, "Here is");
158 // Start at the middle of 1st word, end at the end of last word.
159 actual
= expandRangeString(range
, 'mydiv', 2, 'mydiv', 7, 'word', true);
160 assertEqual("word12", actual
, "Here is");
163 actual
= expandRangeString(range
, 'mydiv', 4, 'mydiv', 5, 'word', true);
164 assertEqual("word13", actual
, "Here ");
166 // Word across different nodes.
167 actual
= expandRangeString(range
, 'mydiv', 1, 'he-div', 0, 'word', true);
168 assertEqual("word14", actual
, "Here is a positioned element.\x0A在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。\x0Aבלשכת");
170 // Test for Chinese. No Chinese segmentation in platform/mac.
171 actual
= expandRangeString(range
, 'zh-CN-div', 0, 'zh-CN-div', 0, 'word', true);
172 assertEqual("Chinese word0", actual
, "在");
173 actual
= expandRangeString(range
, 'zh-CN-div', 1, 'zh-CN-div', 1, 'word', true);
174 assertEqual("Chinese word1", actual
, "甲");
175 actual
= expandRangeString(range
, 'zh-CN-div', 2, 'zh-CN-div', 2, 'word', true);
176 assertEqual("Chinese word1", actual
, "型");
178 actual
= expandRangeString(range
, 'zh-CN-div', 3, 'zh-CN-div', 3, 'sentence', true);
179 assertEqual("Chinese sentence: ", actual
, "在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。");
181 actual
= expandRangeString(range
, 'zh-CN-div', 3, 'zh-CN-div', 3, 'block', true);
182 assertEqual("Chinese block: ", actual
, "在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省2009-2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。");
185 actual
= expandRangeString(range
, 'he-div', 1, 'he-div', 1, 'word', true);
186 assertEqual("Hebrew word", actual
, "בלשכת");
188 actual
= expandRangeString(range
, 'he-div', 1, 'he-div', 1, 'sentence', true);
189 assertEqual("Hebrew sentence", actual
, 'בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"' );
191 actual
= expandRangeString(range
, 'he-div', 0, 'he-div', 0, 'sentence', false);
192 assertEqual("Hebrew block", actual
, 'בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"' );
195 <body onload=
"test()">
196 <p>This is the begin of a block. A block is a collection of sentences. Each sentence begins with capital
197 letter and ends with a punctuation.
199 <p id=
"multilineSentence">Now, a sentence
203 <p id=
"instructions">Roll the mouse around the page. The coordinates
204 of the mouse pointer are currently atop an element
<br id=
"mybr">whose ID
205 is:
"<span id="myspan
" style="font-weight:bold
"></span>".
</p>
207 <table border=
1 id=
"myTable">
209 <td id=
"td_A1">Cell A1
</td>
210 <td id=
"td_B1">Cell B1
</td>
213 <td id=
"td_A2">Cell A2
</td>
214 <td id=
"td_B2">Cell B2
</td>
217 <h2 id=
"sec1">Section
1</h2>
219 <h2 id=
"sec2">Section
2</h2>
221 <div id=
"mydiv" style=
"position:absolute; top:340; left:300; background-color:yellow">
222 Here is a positioned element.
</div>
223 <div id=
"zh-CN-div">在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省
2009-
2010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。
</div>
224 <div id=
"he-div">בלשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי:
"המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"</div>
225 <ul id=
"console"></ul>