| OLD | NEW |
| (Empty) |
| 1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | |
| 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.'); | |
| 11 | |
| 12 jsTestIsAsync = true; | |
| 13 | |
| 14 var testRoot = document.createElement("div"); | |
| 15 document.body.insertBefore(testRoot, document.body.firstChild); | |
| 16 | |
| 17 var testTextArea = document.createElement("textarea"); | |
| 18 testRoot.appendChild(testTextArea); | |
| 19 | |
| 20 var testInput = document.createElement("input"); | |
| 21 testInput.setAttribute("type", "text"); | |
| 22 testRoot.appendChild(testInput); | |
| 23 | |
| 24 var testEditable = document.createElement("div"); | |
| 25 testEditable.setAttribute("contentEditable", "true"); | |
| 26 testRoot.appendChild(testEditable); | |
| 27 | |
| 28 var testSourcePlain = document.createElement("div"); | |
| 29 testSourcePlain.innerHTML = "You has the right."; | |
| 30 testRoot.appendChild(testSourcePlain); | |
| 31 | |
| 32 var testSourceDecorated = document.createElement("div"); | |
| 33 testSourceDecorated.innerHTML = "I have a<b>n ki</b>wi. I have no idea."; | |
| 34 testRoot.appendChild(testSourceDecorated); | |
| 35 | |
| 36 var testSourceMulti = document.createElement("div"); | |
| 37 testSourceMulti.innerHTML = "I have an grape. I have an muscat. I don't know."; | |
| 38 testRoot.appendChild(testSourceMulti); | |
| 39 | |
| 40 var sel = window.getSelection(); | |
| 41 | |
| 42 var tests = []; | |
| 43 | |
| 44 function done() | |
| 45 { | |
| 46 var next = tests.shift(); | |
| 47 if (next) | |
| 48 return window.setTimeout(next, 0); | |
| 49 testRoot.style.display = "none"; | |
| 50 finishJSTest(); | |
| 51 } | |
| 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 | |
| 77 function verifyMarker(node, expectedMarked) | |
| 78 { | |
| 79 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) | |
| 80 node.focus(); | |
| 81 else | |
| 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; | |
| 92 } | |
| 93 | |
| 94 var nodeContent = node instanceof HTMLInputElement || node instanceof HTMLTe
xtAreaElement ? node.value : node.innerHTML; | |
| 95 testPassed(node.tagName + " ungrammatical phrase '" + expectedMarked + "' on
'" + nodeContent + "'"); | |
| 96 | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 var destination = null; | |
| 101 var expectedMarked = null; | |
| 102 function pasteAndVerify(source, dest, expectedMarked) | |
| 103 { | |
| 104 sel.selectAllChildren(source); | |
| 105 document.execCommand("Copy"); | |
| 106 if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement)
{ | |
| 107 dest.value = ""; | |
| 108 dest.focus(); | |
| 109 } else { | |
| 110 dest.innerHTML = ""; | |
| 111 sel.selectAllChildren(dest); | |
| 112 } | |
| 113 document.execCommand("Paste"); | |
| 114 | |
| 115 if (window.internals) { | |
| 116 destination = dest; | |
| 117 ungrammaticalPhrase = expectedMarked; | |
| 118 shouldBecomeEqual('verifyMarker(destination, ungrammaticalPhrase)', 'tru
e', done); | |
| 119 } | |
| 120 }; | |
| 121 | |
| 122 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, ["has"]);
}); | |
| 123 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, ["a"])
; }); // Checks only 'a'. | |
| 124 tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, ["an", "an
"]); }); | |
| 125 | |
| 126 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, ["has"]); }); | |
| 127 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, ["an"]);
}); | |
| 128 tests.push(function() { pasteAndVerify(testSourceMulti, testInput, ["an", "an"])
; }); | |
| 129 | |
| 130 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, ["has"]);
}); | |
| 131 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, ["an"]
); }); | |
| 132 tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, ["an", "an
"]); }); | |
| 133 | |
| 134 done(); | |
| 135 | |
| 136 var successfullyParsed = true; | |
| 137 </script> | |
| 138 </body> | |
| 139 </html> | |
| OLD | NEW |