Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / editing / spelling / grammar-paste.html
blob34907085dd2f4cb17324c334eb81d315d223799c
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.');
12 jsTestIsAsync = true;
14 var testRoot = document.createElement("div");
15 document.body.insertBefore(testRoot, document.body.firstChild);
17 var testTextArea = document.createElement("textarea");
18 testRoot.appendChild(testTextArea);
20 var testInput = document.createElement("input");
21 testInput.setAttribute("type", "text");
22 testRoot.appendChild(testInput);
24 var testEditable = document.createElement("div");
25 testEditable.setAttribute("contentEditable", "true");
26 testRoot.appendChild(testEditable);
28 var testSourcePlain = document.createElement("div");
29 testSourcePlain.innerHTML = "You has the right.";
30 testRoot.appendChild(testSourcePlain);
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);
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);
40 var sel = window.getSelection();
42 var tests = [];
44 function done()
46 var next = tests.shift();
47 if (next)
48 return window.setTimeout(next, 0);
49 testRoot.style.display = "none";
50 finishJSTest();
53 function findFirstTextNode(node)
55 function iterToFindFirstTextNode(node)
57 if (node instanceof Text)
58 return node;
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;
67 return null;
71 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement)
72 return iterToFindFirstTextNode(internals.shadowRoot(node));
73 else
74 return iterToFindFirstTextNode(node);
77 function verifyMarker(node, expectedMarked)
79 if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement)
80 node.focus();
81 else
82 sel.selectAllChildren(node);
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;
94 var nodeContent = node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement ? node.value : node.innerHTML;
95 testPassed(node.tagName + " ungrammatical phrase '" + expectedMarked + "' on '" + nodeContent + "'");
97 return true;
100 var destination = null;
101 var expectedMarked = null;
102 function pasteAndVerify(source, dest, expectedMarked)
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);
113 document.execCommand("Paste");
115 if (window.internals) {
116 destination = dest;
117 ungrammaticalPhrase = expectedMarked;
118 shouldBecomeEqual('verifyMarker(destination, ungrammaticalPhrase)', 'true', done);
122 if (window.internals)
123 internals.settings.setAsynchronousSpellCheckingEnabled(true);
125 tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, ["has"]); });
126 tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, ["a"]); }); // Checks only 'a'.
127 tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, ["an", "an"]); });
129 tests.push(function() { pasteAndVerify(testSourcePlain, testInput, ["has"]); });
130 tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, ["an"]); });
131 tests.push(function() { pasteAndVerify(testSourceMulti, testInput, ["an", "an"]); });
133 tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, ["has"]); });
134 tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, ["an"]); });
135 tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, ["an", "an"]); });
137 done();
139 var successfullyParsed = true;
140 </script>
141 </body>
142 </html>