OLD | NEW |
1 | 1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
2 description('For Bug 40092: Spell checking for pasted text.'); | 2 <html> |
| 3 <head> |
| 4 <script src="../../resources/js-test.js"></script> |
| 5 </head> |
| 6 <body> |
| 7 <p id="description"></p> |
| 8 <div id="console"></div> |
| 9 <script> |
| 10 description('Grammar checking for pasted text.'); |
3 | 11 |
4 jsTestIsAsync = true; | 12 jsTestIsAsync = true; |
5 | 13 |
6 var testRoot = document.createElement("div"); | 14 var testRoot = document.createElement("div"); |
7 document.body.insertBefore(testRoot, document.body.firstChild); | 15 document.body.insertBefore(testRoot, document.body.firstChild); |
8 | 16 |
9 var testTextArea = document.createElement("textarea"); | 17 var testTextArea = document.createElement("textarea"); |
10 testRoot.appendChild(testTextArea); | 18 testRoot.appendChild(testTextArea); |
11 | 19 |
12 var testInput = document.createElement("input"); | 20 var testInput = document.createElement("input"); |
13 testInput.setAttribute("type", "text"); | 21 testInput.setAttribute("type", "text"); |
14 testRoot.appendChild(testInput); | 22 testRoot.appendChild(testInput); |
15 | 23 |
16 var testEditable = document.createElement("div"); | 24 var testEditable = document.createElement("div"); |
17 testEditable.setAttribute("contentEditable", "true"); | 25 testEditable.setAttribute("contentEditable", "true"); |
18 testRoot.appendChild(testEditable); | 26 testRoot.appendChild(testEditable); |
19 | 27 |
20 var testSourcePlain = document.createElement("div"); | 28 var testSourcePlain = document.createElement("div"); |
21 testSourcePlain.innerHTML = "zz apple"; | 29 testSourcePlain.innerHTML = "You has the right."; |
22 testRoot.appendChild(testSourcePlain); | 30 testRoot.appendChild(testSourcePlain); |
23 | 31 |
24 var testSourceDecorated = document.createElement("div"); | 32 var testSourceDecorated = document.createElement("div"); |
25 testSourceDecorated.innerHTML = "z<b>z appl</b>e"; | 33 testSourceDecorated.innerHTML = "I have a<b>n ki</b>wi. I have no idea."; |
26 testRoot.appendChild(testSourceDecorated); | 34 testRoot.appendChild(testSourceDecorated); |
27 | 35 |
28 var testSourceMulti = document.createElement("div"); | 36 var testSourceMulti = document.createElement("div"); |
29 testSourceMulti.innerHTML = "zz zz zz"; | 37 testSourceMulti.innerHTML = "I have an grape. I have an muscat. I don't know."; |
30 testRoot.appendChild(testSourceMulti); | 38 testRoot.appendChild(testSourceMulti); |
31 | 39 |
32 var sel = window.getSelection(); | 40 var sel = window.getSelection(); |
33 | 41 |
34 var tests = []; | 42 var tests = []; |
35 | 43 |
36 function done() | 44 function done() |
37 { | 45 { |
38 var next = tests.shift(); | 46 var next = tests.shift(); |
39 if (next) | 47 if (next) |
40 return window.setTimeout(next, 0); | 48 return window.setTimeout(next, 0); |
41 testRoot.style.display = "none"; | 49 testRoot.style.display = "none"; |
42 finishJSTest(); | 50 finishJSTest(); |
43 } | 51 } |
44 | 52 |
| 53 function findFirstTextNode(node) |
| 54 { |
| 55 function iterToFindFirstTextNode(node) |
| 56 { |
| 57 if (node instanceof Text) |
| 58 return node; |
| 59 |
| 60 var childNodes = node.childNodes; |
| 61 for (var i = 0; i < childNodes.length; ++i) { |
| 62 var n = iterToFindFirstTextNode(childNodes[i]); |
| 63 if (n) |
| 64 return n; |
| 65 } |
| 66 |
| 67 return null; |
| 68 } |
| 69 |
| 70 |
| 71 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) |
| 72 return iterToFindFirstTextNode(internals.shadowRoot(node)); |
| 73 else |
| 74 return iterToFindFirstTextNode(node); |
| 75 } |
| 76 |
45 function verifyMarker(node, expectedMarked) | 77 function verifyMarker(node, expectedMarked) |
46 { | 78 { |
47 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement)
{ | 79 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) |
48 node.focus(); | 80 node.focus(); |
49 } else { | 81 else |
50 sel.selectAllChildren(node); | 82 sel.selectAllChildren(node); |
| 83 |
| 84 var textNode = findFirstTextNode(node); |
| 85 var num = internals.markerCountForNode(textNode, "grammar"); |
| 86 if (num != expectedMarked.length) |
| 87 return false; |
| 88 for (var i = 0; i < num; ++i) { |
| 89 var range = internals.markerRangeForNode(textNode, "grammar", i); |
| 90 if (range.toString() != expectedMarked[i]) |
| 91 return false; |
51 } | 92 } |
52 | 93 |
53 var ok = true; | 94 var nodeContent = node instanceof HTMLInputElement || node instanceof HTMLTe
xtAreaElement ? node.value : node.innerHTML; |
54 for (var i = 0; ok && i < expectedMarked.length; ++i) | 95 testPassed(node.tagName + " ungrammatical phrase '" + expectedMarked + "' on
'" + nodeContent + "'"); |
55 ok = internals.hasSpellingMarker(document, expectedMarked[i][0], expecte
dMarked[i][1]); | |
56 | 96 |
57 if (ok) { | 97 return true; |
58 var nodeContent = node instanceof HTMLInputElement || node instanceof HT
MLTextAreaElement ? node.value : node.innerHTML; | |
59 testPassed(node.tagName + " has a marker on '" + nodeContent + "'"); | |
60 } | |
61 | |
62 return ok; | |
63 } | 98 } |
64 | 99 |
65 var destination = null; | 100 var destination = null; |
66 var misspelledLocations = null; | 101 var expectedMarked = null; |
67 function pasteAndVerify(source, dest, expectedMarked) | 102 function pasteAndVerify(source, dest, expectedMarked) |
68 { | 103 { |
69 sel.selectAllChildren(source); | 104 sel.selectAllChildren(source); |
70 document.execCommand("Copy"); | 105 document.execCommand("Copy"); |
71 if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement)
{ | 106 if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement)
{ |
72 dest.value = ""; | 107 dest.value = ""; |
73 dest.focus(); | 108 dest.focus(); |
74 } else { | 109 } else { |
75 dest.innerHTML = ""; | 110 dest.innerHTML = ""; |
76 sel.selectAllChildren(dest); | 111 sel.selectAllChildren(dest); |
77 } | 112 } |
78 document.execCommand("Paste"); | 113 document.execCommand("Paste"); |
79 | 114 |
80 if (window.internals) { | 115 if (window.internals) { |
81 destination = dest; | 116 destination = dest; |
82 misspelledLocations = expectedMarked; | 117 ungrammaticalPhrase = expectedMarked; |
83 shouldBecomeEqual('verifyMarker(destination, misspelledLocations)', 'tru
e', done); | 118 shouldBecomeEqual('verifyMarker(destination, ungrammaticalPhrase)', 'tru
e', done); |
84 } | 119 } |
85 }; | 120 }; |
86 | 121 |
87 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, [[0, 2]]); })
; | 122 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, ["has"]);
}); |
88 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, [[0, 2]])
; }); | 123 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, ["a"])
; }); // Checks only 'a'. |
89 tests.push(function() { pasteAndVerify(testSourceMulti, testInput, [[0, 2], [3,
2]]); }); | 124 tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, ["an", "an
"]); }); |
90 | 125 |
91 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, [[0, 2]]);
}); | 126 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, ["has"]); }); |
92 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, [[0, 2
]]); }); | 127 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, ["an"]);
}); |
93 tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, [[0, 2], [
3, 2]]); }); | 128 tests.push(function() { pasteAndVerify(testSourceMulti, testInput, ["an", "an"])
; }); |
94 | 129 |
95 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, [[0, 2]]);
}); | 130 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, ["has"]);
}); |
96 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, [[0, 1
]]); }); // To check "fo" part of foo. | 131 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, ["an"]
); }); |
97 tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, [[0, 2], [
3, 2]]); }); | 132 tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, ["an", "an
"]); }); |
| 133 |
98 done(); | 134 done(); |
99 | 135 |
100 var successfullyParsed = true; | 136 var successfullyParsed = true; |
| 137 </script> |
| 138 </body> |
| 139 </html> |
OLD | NEW |