| OLD | NEW |
| (Empty) |
| 1 <html> | |
| 2 <head> | |
| 3 <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"> | |
| 4 <title>Test for Range.expand()</title> | |
| 5 <script> | |
| 6 function log(str) | |
| 7 { | |
| 8 var li = document.createElement("li"); | |
| 9 li.appendChild(document.createTextNode(str)); | |
| 10 var console = document.getElementById("console"); | |
| 11 console.appendChild(li); | |
| 12 } | |
| 13 | |
| 14 function assertEqual(test_name, actual, expected) | |
| 15 { | |
| 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: " + expe
cted[i]); | |
| 30 if (actual[i] != expected[i]) | |
| 31 log("!="); | |
| 32 } | |
| 33 } | |
| 34 else { | |
| 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: " + expe
cted[i]); | |
| 39 if (actual[i] != expected[i]) | |
| 40 log("!="); | |
| 41 } | |
| 42 | |
| 43 } | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 function expandRangeString(range, startId, startOffset, endId, endOffset, expand
Unit, firstChild) | |
| 48 { | |
| 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(); | |
| 59 } | |
| 60 | |
| 61 function test() | |
| 62 { | |
| 63 if (window.testRunner) | |
| 64 testRunner.dumpAsText(); | |
| 65 | |
| 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 sen
tences. Each sentence begins with capital\x0Aletter and ends with a punctuation.
\x0A\x0ANow, a sentence\x0Ais divided into\x0Amultiple lines.\x0A\x0ARoll the mo
use 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-201
0年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。\x0Aב
לשכת שר החוץ הגיבו להתקפות עליו בעקבות הנחייתו לדרוש מהמועמדים לקורס צוערים שירו
ת צבאי או לאומי: "המתקיפים צבועים, מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"' | |
| 70 assertEqual("document1", actual, expected); | |
| 71 | |
| 72 // test 2 - Expand to block. | |
| 73 actual = expandRangeString(range, 'td_A1', 0, 'td_A1', 0, 'block', false); | |
| 74 assertEqual("block2", actual, "Cell A1"); | |
| 75 | |
| 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"); | |
| 79 | |
| 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"); | |
| 83 | |
| 84 // test 5 - Expand to sentence. | |
| 85 actual = expandRangeString(range, 'instructions', 0, 'instructions', 0, 'sen
tence', false); | |
| 86 assertEqual("sentence5", actual, "Roll the mouse around the page. "); | |
| 87 | |
| 88 // test 6 - Expand to sentence, different range start and end offset. | |
| 89 actual = expandRangeString(range, 'instructions', 1, 'instructions', 2, 'sen
tence', true); | |
| 90 assertEqual("sentence6", actual, "Roll the mouse around the page. "); | |
| 91 | |
| 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 coordi
nates\x0Aof the mouse pointer are currently atop an element"); | |
| 95 | |
| 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\x0Amult
iple lines."); | |
| 99 | |
| 100 actual = expandRangeString(range, 'multilineSentence', 1, 'multilineSentence
', 5, 'sentence', true); | |
| 101 assertEqual("sentence9", actual, "Now, a sentence\x0Ais divided into\x0Amult
iple lines."); | |
| 102 | |
| 103 actual = expandRangeString(range, 'multilineSentence', 20, 'multilineSentenc
e', 20, 'sentence', true); | |
| 104 assertEqual("sentence10", actual, "is divided into\x0Amultiple lines."); | |
| 105 | |
| 106 actual = expandRangeString(range, 'multilineSentence', 40, 'multilineSentenc
e', 40, 'sentence', true); | |
| 107 assertEqual("sentence11", actual, "multiple lines."); | |
| 108 | |
| 109 // Expand word. | |
| 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"); | |
| 113 | |
| 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"); | |
| 117 | |
| 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"); | |
| 121 | |
| 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"); | |
| 125 | |
| 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"); | |
| 129 | |
| 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"); | |
| 134 | |
| 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"); | |
| 139 | |
| 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"); | |
| 144 | |
| 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"); | |
| 149 | |
| 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"); | |
| 153 | |
| 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"); | |
| 157 | |
| 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"); | |
| 161 | |
| 162 // Word and space. | |
| 163 actual = expandRangeString(range, 'mydiv', 4, 'mydiv', 5, 'word', true); | |
| 164 assertEqual("word13", actual, "Here "); | |
| 165 | |
| 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בלשכת"); | |
| 169 | |
| 170 // Test for Chinese. No Chinese segmentation in platform/mac. | |
| 171 actual = expandRangeString(range, 'zh-CN-div', 0, 'zh-CN-div', 0, 'word', tr
ue); | |
| 172 assertEqual("Chinese word0", actual, "在"); | |
| 173 actual = expandRangeString(range, 'zh-CN-div', 1, 'zh-CN-div', 1, 'word', tr
ue); | |
| 174 assertEqual("Chinese word1", actual, "甲"); | |
| 175 actual = expandRangeString(range, 'zh-CN-div', 2, 'zh-CN-div', 2, 'word', tr
ue); | |
| 176 assertEqual("Chinese word1", actual, "型"); | |
| 177 | |
| 178 actual = expandRangeString(range, 'zh-CN-div', 3, 'zh-CN-div', 3, 'sentence'
, true); | |
| 179 assertEqual("Chinese sentence: ", actual, "在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省200
9-2010年度流感防制研讨会注定与往年不同。"); | |
| 180 | |
| 181 actual = expandRangeString(range, 'zh-CN-div', 3, 'zh-CN-div', 3, 'block', t
rue); | |
| 182 assertEqual("Chinese block: ", actual, "在甲型H1N1流感可能出现大流行的警告声中,昨天召开的江苏省2009-2
010年度流感防制研讨会注定与往年不同。每年都会召开的一次例会,今年足足提前了一个月;会上研讨的主题,已从以前的季节性流感,变成以甲型H1N1流感为重点。"); | |
| 183 | |
| 184 // Test for Hebrew. | |
| 185 actual = expandRangeString(range, 'he-div', 1, 'he-div', 1, 'word', true); | |
| 186 assertEqual("Hebrew word", actual, "בלשכת"); | |
| 187 | |
| 188 actual = expandRangeString(range, 'he-div', 1, 'he-div', 1, 'sentence', true
); | |
| 189 assertEqual("Hebrew sentence", actual, 'בלשכת שר החוץ הגיבו להתקפות עליו בעק
בות הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים,
מי שרוצה לייצג את המדינה רוצה גם לשרת אותה"' ); | |
| 190 | |
| 191 actual = expandRangeString(range, 'he-div', 0, 'he-div', 0, 'sentence', fals
e); | |
| 192 assertEqual("Hebrew block", actual, 'בלשכת שר החוץ הגיבו להתקפות עליו בעקבות
הנחייתו לדרוש מהמועמדים לקורס צוערים שירות צבאי או לאומי: "המתקיפים צבועים, מי
שרוצה לייצג את המדינה רוצה גם לשרת אותה"' ); | |
| 193 } | |
| 194 </script> | |
| 195 <body onload="test()"> | |
| 196 <p>This is the begin of a block. A block is a collection of sentences. Each sent
ence begins with capital | |
| 197 letter and ends with a punctuation. | |
| 198 </p> | |
| 199 <p id="multilineSentence">Now, a sentence | |
| 200 is divided into | |
| 201 multiple lines. | |
| 202 </p> | |
| 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> | |
| 206 | |
| 207 <table border=1 id="myTable"> | |
| 208 <tr id="tr1"> | |
| 209 <td id="td_A1">Cell A1</td> | |
| 210 <td id="td_B1">Cell B1</td> | |
| 211 </tr> | |
| 212 <tr id="tr2"> | |
| 213 <td id="td_A2">Cell A2</td> | |
| 214 <td id="td_B2">Cell B2</td> | |
| 215 </tr> | |
| 216 </table> | |
| 217 <h2 id="sec1">Section 1</h2> | |
| 218 <p id="p1">text.</p> | |
| 219 <h2 id="sec2">Section 2</h2> | |
| 220 <p id="p2">text.</p> | |
| 221 <div id="mydiv" style="position:absolute; top:340; left:300; background-color:ye
llow"> | |
| 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> | |
| 226 | |
| 227 </body> | |
| 228 </html> | |
| OLD | NEW |