2 * Copyright 2017 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 sync_test("input_selection", function() {
22 var input = document.createElement("input");
25 document.body.appendChild(input);
27 function test_range(start, end) {
28 ok(input.selectionStart === start, "input.selectionStart = " + input.selectionStart + " expected " + start);
29 ok(input.selectionEnd === end, "input.selectionEnd = " + input.selectionEnd + " expected " + end);
34 input.selectionStart = 2;
37 input.selectionStart = -1;
40 input.selectionStart = 10;
43 input.selectionEnd = 2;
46 input.selectionEnd = -1;
49 input.selectionEnd = 10;
52 input.setSelectionRange(2, 3);
55 input.setSelectionRange(-1, 10);
58 input.setSelectionRange(3, 3);
62 sync_test("textContent", function() {
63 var text = document.createTextNode("test");
64 ok(text.textContent === "test", "text.textContent = " + text.textContent);
66 var div = document.createElement("div");
67 document.body.appendChild(div);
68 div.innerHTML = "abc<script>/* */</script><div>text</div>";
69 ok(div.textContent === "abc/* */text", "div.textContent = " + div.textContent);
71 div.textContent = "test";
72 ok(div.textContent === "test", "div.textContent = " + div.textContent);
73 ok(div.childNodes.length === 1, "div.childNodes.length = " + div.childNodes.length);
74 ok(div.firstChild.textContent === "test", "div.firstChild.textContent = " + div.firstChild.textContent);
77 ok(div.textContent === "", "div.textContent = " + div.textContent);
78 ok(div.childNodes.length === 0, "div.childNodes.length = " + div.childNodes.length);
80 div.textContent = null;
81 ok(div.textContent === "", "div.textContent = " + div.textContent);
83 ok(div.textContent === "11", "div.textContent = " + div.textContent);
84 div.textContent = 10.5;
85 ok(div.textContent === "10.5", "div.textContent = " + div.textContent);
87 ok(document.textContent === null, "document.textContent = " + document.textContent);
90 sync_test("ElementTraversal", function() {
91 var div = document.createElement("div");
92 div.innerHTML = "abc<b>bold</b><script>/* */<script><div>text</div>def";
93 ok(div.firstElementChild.outerHTML === "<b>bold</b>",
94 "div.firstElementChild.outerHTML = " + div.firstElementChild.outerHTML);
96 div.innerHTML = "abc";
97 ok(div.firstElementChild === null, "div.firstElementChild = " + div.firstElementChild);
99 ok(!("firstElementChild" in document), "firstElementChild found in document");
102 sync_test("head", function() {
103 var h = document.head;
104 ok(h.tagName === "HEAD", "h.tagName = " + h.tagName);
105 ok(h === document.getElementsByTagName("head")[0], "unexpected head element");
108 async_test("iframe", function() {
109 document.body.innerHTML = '<iframe src="runscript.html?frame.js"></iframe>'
110 var iframe = document.body.firstChild;
112 iframe.onload = guard(function() {
113 var r = iframe.contentWindow.global_object.get_global_value();
114 ok(r === "global value", "get_global_value() returned " + r);
116 var f = iframe.contentWindow.global_object.get_global_value;
117 ok(f() === "global value", "f() returned " + f());
123 async_test("iframe_location", function() {
124 document.body.innerHTML = '<iframe src="emptyfile"></iframe>'
125 var iframe = document.body.firstChild;
127 iframe.onload = function() {
128 ok(iframe.contentWindow.location.pathname === "/emptyfile",
129 "path = " + iframe.contentWindow.location.pathname);
130 iframe.onload = function () {
131 ok(iframe.contentWindow.location.pathname === "/empty/file",
132 "path = " + iframe.contentWindow.location.pathname);
135 iframe.src = "empty/file";
139 sync_test("anchor", function() {
141 { href: "http://www.winehq.org:123/about",
142 protocol: "http:", host: "www.winehq.org:123", path: "/about" },
143 { href: "https://www.winehq.org:123/about",
144 protocol: "https:", host: "www.winehq.org:123", path: "/about" },
145 { href: "about:blank",
146 protocol: "about:", host: "", path: "/blank", todo_pathname: 1 },
147 { href: "unknown:path",
148 protocol: "unknown:", host: "", path: "path" },
150 protocol: "ftp:", host: "", path: "path" },
151 { href: "mailto:path",
152 protocol: "mailto:", host: "", path: "path" },
154 protocol: "ws:", host: "", path: "path" },
156 protocol: "file:", host: "", path: "/path", todo_pathname: 1 },
157 { href: "file:///c:/dir/file.html",
158 protocol: "file:", host: "", path: "/c:/dir/file.html" },
159 { href: "http://www.winehq.org/about",
160 protocol: "http:", host: "www.winehq.org", path: "/about" },
161 { href: "https://www.winehq.org/about",
162 protocol: "https:", host: "www.winehq.org", path: "/about" },
165 for(var i in anchor_tests) {
166 var t = anchor_tests[i];
167 document.body.innerHTML = '<a href="' + t.href + '">';
168 var anchor = document.body.firstChild;
169 ok(anchor.protocol === t.protocol, "anchor(" + t.href + ").protocol = " + anchor.protocol);
170 ok(anchor.host === t.host, "anchor(" + t.href + ").host = " + anchor.host);
171 todo_wine_if("todo_pathname" in t).
172 ok(anchor.pathname === t.path, "anchor(" + t.href + ").pathname = " + anchor.pathname);
176 sync_test("getElementsByClassName", function() {
179 document.body.innerHTML = '<div class="class1">'
180 + '<div class="class1"></div>'
181 + '<a id="class1" class="class2"></a>'
183 + '<script class="class1"></script>';
185 elems = document.getElementsByClassName("class1");
186 ok(elems.length === 3, "elems.length = " + elems.length);
187 ok(elems[0].tagName === "DIV", "elems[0].tagName = " + elems[0].tagName);
188 ok(elems[1].tagName === "DIV", "elems[1].tagName = " + elems[1].tagName);
189 ok(elems[2].tagName === "SCRIPT", "elems[2].tagName = " + elems[2].tagName);
191 elems = document.getElementsByClassName("class2");
192 ok(elems.length === 1, "elems.length = " + elems.length);
193 ok(elems[0].tagName === "A", "elems[0].tagName = " + elems[0].tagName);
195 elems = document.getElementsByClassName("classnotfound");
196 ok(elems.length == 0, "elems.length = " + elems.length);
199 sync_test("createElementNS", function() {
200 var svg_ns = "http://www.w3.org/2000/svg";
203 elem = document.createElementNS(null, "test");
204 ok(elem.tagName === "test", "elem.tagName = " + elem.tagName);
205 ok(elem.namespaceURI === null, "elem.namespaceURI = " + elem.namespaceURI);
207 elem = document.createElementNS(svg_ns, "test");
208 ok(elem.tagName === "test", "elem.tagName = " + elem.tagName);
209 ok(elem.namespaceURI === svg_ns, "elem.namespaceURI = " + elem.namespaceURI);
211 elem = document.createElementNS(svg_ns, "svg");
212 ok(elem.tagName === "svg", "elem.tagName = " + elem.tagName);
213 ok(elem.namespaceURI === svg_ns, "elem.namespaceURI = " + elem.namespaceURI);
215 elem = document.createElementNS("test", "svg");
216 ok(elem.tagName === "svg", "elem.tagName = " + elem.tagName);
217 ok(elem.namespaceURI === "test", "elem.namespaceURI = " + elem.namespaceURI);
220 sync_test("query_selector", function() {
221 document.body.innerHTML = '<div class="class1">'
222 + '<div class="class1"></div>'
223 + '<a id="class1" class="class2"></a>'
225 + '<script class="class1"></script>';
227 var e = document.querySelector("nomatch");
228 ok(e === null, "e = " + e);
229 e = document.body.querySelector("nomatch");
230 ok(e === null, "e = " + e);
232 e = document.querySelector(".class1");
233 ok(e.tagName === "DIV", "e.tagName = " + e.tagName);
234 e = document.body.querySelector(".class1");
235 ok(e.tagName === "DIV", "e.tagName = " + e.tagName);
236 ok(e.msMatchesSelector(".class1") === true, "msMatchesSelector returned " + e.msMatchesSelector(".class1"));
237 ok(e.msMatchesSelector(".class2") === false, "msMatchesSelector returned " + e.msMatchesSelector(".class2"));
239 e = document.querySelector("a");
240 ok(e.tagName === "A", "e.tagName = " + e.tagName);
241 e = document.body.querySelector("a");
242 ok(e.tagName === "A", "e.tagName = " + e.tagName);
245 sync_test("compare_position", function() {
246 document.body.innerHTML = '<div><div></div><div></div></div>';
248 var parent = document.body.firstChild;
249 var child1 = parent.firstChild;
250 var child2 = child1.nextSibling;
251 var elem = document.createElement("div");
253 function compare_position(node1, node2, expected_result, ignore_mask) {
254 var cmp = node1.compareDocumentPosition(node2);
255 ok((cmp & ~ignore_mask) == expected_result,
256 "compareDocumentPosition returned " + cmp + " expected " + expected_result);
259 compare_position(child1, child2, 4);
260 compare_position(child2, child1, 2);
261 compare_position(parent, child1, 0x14);
262 compare_position(parent, child2, 0x14);
263 compare_position(parent, elem, 0x21, 6);
264 compare_position(elem, parent, 0x21, 6);
267 sync_test("rects", function() {
268 document.body.innerHTML = '<div>test</div>';
269 var elem = document.body.firstChild;
270 var rects = elem.getClientRects();
271 var rect = elem.getBoundingClientRect();
273 ok(rects.length === 1, "rect.length = " + rects.length);
274 ok(rects[0].top === rect.top, "rects[0].top = " + rects[0].top + " rect.top = " + rect.top);
275 ok(rects[0].bottom === rect.bottom, "rects[0].bottom = " + rects[0].bottom + " rect.bottom = " + rect.bottom);
277 elem = document.createElement("style");
278 rects = elem.getClientRects();
279 ok(rects.length === 0, "rect.length = " + rects.length);
282 sync_test("document_owner", function() {
285 ok(document.ownerDocument === null, "ownerDocument = " + document.ownerDocument);
286 ok(document.body.ownerDocument === document,
287 "body.ownerDocument = " + document.body.ownerDocument);
288 ok(document.documentElement.ownerDocument === document,
289 "documentElement.ownerDocument = " + document.documentElement.ownerDocument);
291 node = document.createElement("test");
292 ok(node.ownerDocument === document, "element.ownerDocument = " + node.ownerDocument);
294 node = document.createDocumentFragment();
295 ok(node.ownerDocument === document, "fragment.ownerDocument = " + node.ownerDocument);
297 node = document.createTextNode("test");
298 ok(node.ownerDocument === document, "text.ownerDocument = " + node.ownerDocument);
301 sync_test("style_properties", function() {
302 document.body.innerHTML = '<div>test</div><svg></svg>';
303 var elem = document.body.firstChild;
304 var style = elem.style;
305 var current_style = elem.currentStyle;
306 var computed_style = window.getComputedStyle(elem);
309 style.cssFloat = "left";
310 ok(style.cssFloat === "left", "cssFloat = " + style.cssFloat);
311 ok(style.getPropertyValue("float") === "left",
312 'style.getPropertyValue("float") = ' + style.getPropertyValue("float"));
313 ok(style.getPropertyValue("cssFloat") === "",
314 'style.getPropertyValue("cssFloat") = ' + style.getPropertyValue("cssFloat"));
316 val = style.removeProperty("float");
317 ok(val === "left", "removeProperty() returned " + val);
318 ok(style.cssFloat === "", "cssFloat = " + style.cssFloat);
320 style.cssFloat = "left";
321 val = style.removeProperty("FloaT");
322 ok(val === "left", "removeProperty() returned " + val);
323 ok(style.cssFloat === "", "cssFloat = " + style.cssFloat);
325 style.cssFloat = "left";
326 val = style.removeProperty("cssFloat");
327 ok(val === "", "removeProperty() returned " + val);
328 ok(style.cssFloat === "left", "cssFloat = " + style.cssFloat);
329 ok(style["float"] === "left", "float = " + style["float"]);
331 style.testVal = "test";
332 val = style.removeProperty("testVal");
333 ok(val === "", "removeProperty() returned " + val);
334 ok(style.testVal === "test", "testVal = " + style.testVal);
336 style["z-index"] = 1;
337 ok(style.zIndex === 1, "zIndex = " + style.zIndex);
338 ok(style["z-index"] === 1, "z-index = " + style["z-index"]);
339 ok(style.getPropertyValue("z-index") === "1",
340 'style.getPropertyValue("x-index") = ' + style.getPropertyValue("z-index"));
341 ok(style.getPropertyValue("zIndex") === "",
342 'style.getPropertyValue("xIndex") = ' + style.getPropertyValue("zIndex"));
344 style.setProperty("border-width", "5px");
345 ok(style.borderWidth === "5px", "style.borderWidth = " + style.borderWidth);
348 style.setProperty("border-width", 6);
349 ok(style.borderWidth === "5px", "style.borderWidth = " + style.borderWidth);
351 win_skip("skipping setProperty tests on too old IE version");
355 style.setProperty("border-width", "7px", "test");
356 ok(style.borderWidth === "5px", "style.borderWidth = " + style.borderWidth);
358 style.setProperty("border-width", "6px", "");
359 ok(style.borderWidth === "6px", "style.borderWidth = " + style.borderWidth);
361 style.setProperty("border-width", "7px", "important");
362 ok(style.borderWidth === "7px", "style.borderWidth = " + style.borderWidth);
364 style.setProperty("border-width", "8px", undefined);
365 ok(style.borderWidth === "7px", "style.borderWidth = " + style.borderWidth);
367 style.clip = "rect(1px 1px 10px 10px)";
368 ok(style.clip === "rect(1px, 1px, 10px, 10px)", "style.clip = " + style.clip);
369 ok(current_style.clip === "rect(1px, 1px, 10px, 10px)",
370 "current_style.clip = " + current_style.clip);
371 ok(computed_style.clip === "rect(1px, 1px, 10px, 10px)",
372 "computed_style.clip = " + current_style.clip);
375 ok(current_style.zIndex === 2, "current_style.zIndex = " + current_style.zIndex);
376 ok(computed_style.zIndex === 2, "computed_style.zIndex = " + computed_style.zIndex);
379 current_style.zIndex = 1;
380 ok(false, "expected exception");
384 computed_style.zIndex = 1;
385 ok(false, "expected exception");
388 elem = elem.nextSibling;
389 computed_style = window.getComputedStyle(elem);
391 elem.style.zIndex = 4;
392 ok(computed_style.zIndex === 4, "computed_style.zIndex = " + computed_style.zIndex);
394 window.getComputedStyle(elem, null);
397 sync_test("stylesheets", function() {
398 document.body.innerHTML = '<style>.div { margin-right: 1px; }</style>';
399 var elem = document.body.firstChild;
401 ok(document.styleSheets.length === 1, "document.styleSheets.length = " + document.styleSheets.length);
403 var stylesheet = document.styleSheets.item(0);
404 ok(stylesheet.rules.length === 1, "stylesheet.rules.length = " + stylesheet.rules.length);
405 ok(typeof(stylesheet.rules.item(0)) === "object",
406 "typeof(stylesheet.rules.item(0)) = " + typeof(stylesheet.rules.item(0)));
409 stylesheet.rules.item(1);
410 ok(false, "expected exception");
413 ok(stylesheet.href === null, "stylesheet.href = " + stylesheet.href);
415 var id = stylesheet.insertRule(".input { margin-left: 1px; }", 0);
416 ok(id === 0, "id = " + id);
417 ok(document.styleSheets.length === 1, "document.styleSheets.length = " + document.styleSheets.length);
420 stylesheet.insertRule(".input { margin-left: 1px; }", 3);
421 ok(false, "expected exception");
424 id = stylesheet.addRule(".p", "margin-top: 2px");
425 ok(id === 2, "id = " + id);
426 ok(document.styleSheets.length === 1, "document.styleSheets.length = " + document.styleSheets.length);
427 ok(stylesheet.rules.length === 3, "stylesheet.rules.length = " + stylesheet.rules.length);
429 id = stylesheet.addRule(".pre", "border: none", -1);
430 ok(id === 3, "id = " + id);
431 ok(stylesheet.rules.length === 4, "stylesheet.rules.length = " + stylesheet.rules.length);
433 id = stylesheet.addRule(".h1", " ", 0);
434 ok(id === 0, "id = " + id);
435 ok(stylesheet.rules.length === 5, "stylesheet.rules.length = " + stylesheet.rules.length);
437 id = stylesheet.addRule(".h2", "color: black", 8);
438 ok(id === 5, "id = " + id);
439 ok(stylesheet.rules.length === 6, "stylesheet.rules.length = " + stylesheet.rules.length);
442 stylesheet.addRule("", "border: none", 0);
443 ok(false, "expected exception");
446 stylesheet.addRule(".img", "", 0);
447 ok(false, "expected exception");
451 sync_test("storage", function() {
452 ok(typeof(window.sessionStorage) === "object",
453 "typeof(window.sessionStorage) = " + typeof(window.sessionStorage));
454 ok(typeof(window.localStorage) === "object" || typeof(window.localStorage) === "unknown",
455 "typeof(window.localStorage) = " + typeof(window.localStorage));
457 var item = sessionStorage.getItem("nonexisting");
458 ok(item === null, "item = " + item);
461 async_test("animation", function() {
462 document.body.innerHTML =
464 " @keyframes testAnimation {0% { opacity: 0; } 100% { opacity: 1; }}" +
465 " .testAnimation { animation-name: testAnimation; animation-duration: 0.01s; }" +
467 var div = document.createElement("div");
468 div.addEventListener("animationstart", function() {
469 div.addEventListener("animationend", next_test);
471 document.body.appendChild(div);
472 div.className = "testAnimation";
475 sync_test("navigator", function() {
476 ok(typeof(window.navigator) === "object",
477 "typeof(window.navigator) = " + typeof(window.navigator));
479 var v = window.navigator;
480 ok(v === window.navigator, "v != window.navigator");
482 ok(window.navigator.testProp, "window.navigator.testProp = " + window.navigator.testProp);
485 sync_test("elem_props", function() {
486 var elem = document.body;
488 ok(elem.accessKey === "", "accessKey = " + elem.accessKey);
489 elem.accessKey = "q";
490 ok(elem.accessKey === "q", "accessKey = " + elem.accessKey + " expected q");
493 async_test("animation_frame", function() {
494 var id = requestAnimationFrame(function(x) {
495 ok(this === window, "this != window");
496 ok(typeof(x) === "number", "x = " + x);
497 ok(arguments.length === 1, "arguments.length = " + arguments.length);
500 ok(typeof(id) === "number", "id = " + id);
503 sync_test("title", function() {
504 var elem = document.createElement("div");
505 ok(elem.title === "", "div.title = " + elem.title);
506 ok(elem.getAttribute("title") === null, "title attribute = " + elem.getAttribute("title"));
508 ok(elem.title === "test", "div.title = " + elem.title);
509 ok(elem.getAttribute("title") === "test", "title attribute = " + elem.getAttribute("title"));
512 sync_test("disabled", function() {
513 var elem = document.createElement("div");
514 document.body.appendChild(elem);
515 ok(elem.disabled === false, "div.disabled = " + elem.disabled);
516 ok(elem.getAttribute("disabled") === null, "disabled attribute = " + elem.getAttribute("disabled") + " expected null");
518 elem.disabled = true;
519 ok(elem.disabled === true, "div.disabled = " + elem.disabled);
520 ok(elem.getAttribute("disabled") === "", "disabled attribute = " + elem.getAttribute("disabled") + " expected \"\"");
522 elem.disabled = false;
523 ok(elem.disabled === false, "div.disabled = " + elem.disabled);
524 ok(elem.getAttribute("disabled") === null, "disabled attribute = " + elem.getAttribute("disabled") + " expected null");
526 elem.setAttribute("disabled", "false");
527 ok(elem.disabled === true, "div.disabled = " + elem.disabled);
528 ok(elem.getAttribute("disabled") === "false", "disabled attribute = " + elem.getAttribute("disabled"));
530 elem.removeAttribute("disabled");
531 ok(elem.disabled === false, "div.disabled = " + elem.disabled);
532 ok(elem.getAttribute("disabled") === null, "disabled attribute = " + elem.getAttribute("disabled") + " expected null");
535 sync_test("hasAttribute", function() {
536 document.body.innerHTML = '<div attr="test"></div>';
537 var elem = document.body.firstChild, r;
539 r = elem.hasAttribute("attr");
540 ok(r === true, "hasAttribute(attr) returned " + r);
541 r = elem.hasAttribute("attr2");
542 ok(r === false, "hasAttribute(attr2) returned " + r);
544 elem.setAttribute("attr2", "abc");
545 r = elem.hasAttribute("attr2");
546 ok(r === true, "hasAttribute(attr2) returned " + r);
548 elem.removeAttribute("attr");
549 r = elem.hasAttribute("attr");
550 ok(r === false, "hasAttribute(attr) returned " + r);
553 sync_test("classList", function() {
554 var elem = document.createElement("div");
555 var classList = elem.classList;
558 ok(elem.className === "a", "Expected className 'a', got " + elem.className);
561 ok(elem.className === "a b", "Expected className 'a b', got " + elem.className);
564 ok(elem.className === "a b c", "Expected className 'a b c', got " + elem.className);
567 ok(elem.className === "a b c 4", "Expected className 'a b c 4', got " + elem.className);
570 ok(elem.className === "a b c 4", "(2) Expected className 'a b c 4', got " + elem.className);
571 ok(("" + classList) === "a b c 4", "Expected classList value 'a b c 4', got " + classList);
572 ok(classList.toString() === "a b c 4", "Expected classList toString 'a b c 4', got " + classList.toString());
574 var exception = false
584 ok(exception && elem.className === "a b c 4", "Expected exception, className 'a b c 4', got exception "
585 + exception + ", className" + elem.className);
596 ok(exception, "Expected exception for classList.add(\"\")");
601 classList.add("e f");
607 ok(exception, "Expected exception for classList.add(\"e f\")");
609 classList.remove("e");
610 ok(elem.className === "a b c 4", "remove: expected className 'a b c 4', got " + elem.className);
615 classList.remove("e f");
621 ok(exception, "remove: expected exception for classList.remove(\"e f\")");
626 classList.remove("");
632 ok(exception, "remove: expected exception for classList.remove(\"\")");
634 classList.remove("d");
635 ok(elem.className === "a b c 4", "remove: expected className 'a b c 4', got " + elem.className);
637 classList.remove("c");
638 ok(elem.className === "a b 4", "remove: expected className 'a b 4', got " + elem.className);
641 ok(elem.className === "a b", "remove: expected className 'a b', got " + elem.className);
643 classList.remove('a');
644 ok(elem.className === "b", "remove: expected className 'b', got " + elem.className);
646 classList.remove("a");
647 ok(elem.className === "b", "remove (2): expected className 'b', got " + elem.className);
649 classList.remove("b");
650 ok(elem.className === "", "remove: expected className '', got " + elem.className);
652 elem.className = " testclass foobar ";
653 ok(("" + classList) === " testclass foobar ", "Expected classList value ' testclass foobar ', got " + classList);
654 ok(classList.toString() === " testclass foobar ", "Expected classList toString ' testclass foobar ', got " + classList.toString());