LibWeb: Add more document tests, add comment, text and mixin tests

Also adds a TypeScript definition file for the test runner object.
This commit is contained in:
Luke 2020-08-17 21:04:46 +01:00 committed by Andreas Kling
parent 8b807e65d7
commit c2a2552e46
Notes: sideshowbarker 2024-07-19 03:28:13 +09:00
18 changed files with 224 additions and 15 deletions

View file

@ -34,6 +34,7 @@ set(SOURCES
DOM/EventListener.cpp
DOM/EventTarget.cpp
DOM/Node.cpp
DOM/ParentNode.cpp
DOM/Position.cpp
DOM/TagNames.cpp
DOM/Text.cpp

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -31,13 +31,6 @@
namespace Web::DOM {
void ParentNode::remove_all_children()
{
while (RefPtr<Node> child = first_child()) {
remove_child(*child);
}
}
RefPtr<Element> ParentNode::query_selector(const StringView& selector_text)
{
auto selector = parse_selector(CSS::ParsingContext(*this), selector_text);

View file

@ -0,0 +1,15 @@
loadPage("file:///home/anon/web-tests/Pages/Comment.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
const comment = document.body.firstChild.nextSibling;
expect(comment).not.toBeNull();
// FIXME: Add this in once Comment's constructor is implemented.
//expect(comment).toBeInstanceOf(Comment);
expect(comment.nodeName).toBe("#comment");
expect(comment.data).toBe("This is a comment");
expect(comment).toHaveLength(17);
});
});

View file

@ -2,7 +2,12 @@ loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
var title = document.getElementsByTagName("title")[0];
const title = document.getElementsByTagName("title")[0];
expect(title).toBeDefined();
// FIXME: Add this in once Text's constructor is implemented.
//expect(title.firstChild).toBeInstanceOf(Text);
expect(title.firstChild.nodeName).toBe("#text");
expect(title.firstChild.data).toBe("Blank");
expect(title.firstChild.length).toBe(5);

View file

@ -0,0 +1,13 @@
loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
const comment = document.createComment("Create Comment Test");
// FIXME: Add this in once Comment's constructor is implemented.
//expect(comment).toBeInstanceOf(Comment);
expect(comment.nodeName).toBe("#comment");
expect(comment.data).toBe("Create Comment Test");
expect(comment.length).toBe(19);
});
});

View file

@ -0,0 +1,11 @@
loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
const fragment = document.createDocumentFragment();
// FIXME: Add this in once DocumentFragment's constructor is implemented.
//expect(fragment).toBeInstanceOf(DocumentFragment);
expect(fragment.nodeName).toBe("#document-fragment");
});
});

View file

@ -0,0 +1,13 @@
loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
const text = document.createTextNode("Create Text Test");
// FIXME: Add this in once Text's constructor is implemented.
//expect(text).toBeInstanceOf(Text);
expect(text.nodeName).toBe("#text");
expect(text.data).toBe("Create Text Test");
expect(text.length).toBe(16);
});
});

View file

@ -3,7 +3,7 @@ loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
expect(document.compatMode).toBe("CSS1Compat");
expect(document.doctype).not.toBe(null);
expect(document.doctype).not.toBeNull();
expect(document.doctype.name).toBe("html");
expect(document.doctype.publicId).toBe("");
expect(document.doctype.systemId).toBe("");
@ -13,6 +13,6 @@ afterInitialPageLoad(() => {
test("Quirks mode", () => {
expect(document.compatMode).toBe("BackCompat");
expect(document.doctype).toBe(null);
expect(document.doctype).toBeNull();
});
});

View file

@ -2,7 +2,15 @@ loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
expect(document.documentElement).not.toBe(null);
expect(document.documentElement).not.toBeNull();
// FIXME: Add this in once HTMLHtmlElement's constructor is implemented.
//expect(document.documentElement).toBeInstanceOf(HTMLHtmlElement);
expect(document.documentElement.nodeName).toBe("html");
});
// FIXME: Add this in once removeChild is implemented.
test.skip("Nullable", () => {
document.removeChild(document.documentElement);
expect(document.documentElement).toBeNull();
});
});

View file

@ -0,0 +1,19 @@
loadPage("file:///home/anon/web-tests/Pages/ParentNode.html");
afterInitialPageLoad(() => {
test("getElementById basics", () => {
const unique = document.getElementById("unique");
expect(unique).not.toBeNull();
expect(unique.nodeName).toBe("div");
expect(unique.id).toBe("unique");
const caseSensitive = document.getElementById("Unique");
expect(caseSensitive).toBeNull();
const firstDuplicate = document.getElementById("dupeId");
expect(firstDuplicate).not.toBeNull();
expect(firstDuplicate.nodeName).toBe("div");
expect(firstDuplicate.id).toBe("dupeId");
expect(firstDuplicate.innerHTML).toBe("First ID");
});
});

View file

@ -0,0 +1,25 @@
loadPage("file:///home/anon/web-tests/Pages/ParentNode.html");
afterInitialPageLoad(() => {
test("querySelector basics", () => {
const firstDuplicateElement = document.querySelector(".duplicate");
expect(firstDuplicateElement).not.toBeNull();
expect(firstDuplicateElement.nodeName).toBe("div");
expect(firstDuplicateElement.innerHTML).toBe("First");
const noElement = document.querySelector(".nonexistent");
expect(noElement).toBeNull();
});
test("querySelectorAll basics", () => {
const allDuplicates = document.querySelectorAll(".duplicate");
expect(allDuplicates).toHaveLength(2);
expect(allDuplicates[0].nodeName).toBe("div");
expect(allDuplicates[0].innerHTML).toBe("First");
expect(allDuplicates[1].nodeName).toBe("div");
expect(allDuplicates[1].innerHTML).toBe("Second");
const noElements = document.querySelectorAll(".nonexistent");
expect(noElements).toHaveLength(0);
});
});

View file

@ -0,0 +1,55 @@
loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
expect(document.body).not.toBeNull();
// FIXME: Add this in once HTMLBodyElement's constructor is implemented.
//expect(document.body).toBeInstanceOf(HTMLBodyElement);
expect(document.body.nodeName).toBe("body");
});
// FIXME: Add this in once set_body is fully implemented.
test.skip("Setting body to a new body element", () => {
// Add something to body to see if it's gone afterwards
const p = document.createElement("p");
document.body.appendChild(p);
expect(document.body.firstChild).toBe(p);
const newBody = document.createElement("body");
document.body = newBody;
expect(document.body).not.toBeNull();
expect(document.body.nodeName).toBe("body");
// FIXME: Add this in once HTMLBodyElement's constructor is implemented.
//expect(document.body).toBeInstanceOf(HTMLBodyElement);
expect(document.body.firstChild).toBeNull();
});
// FIXME: Add this in once set_body is fully implemented.
test.skip("Setting body to a new frameset element", () => {
const newFrameSet = document.createElement("frameset");
document.body = newFrameSet;
expect(document.body).not.toBeNull();
expect(document.body.nodeName).toBe("frameset");
// FIXME: Add this in once HTMLFrameSetElement's constructor is implemented.
//expect(document.body).toBeInstanceOf(HTMLFrameSetElement);
});
// FIXME: Add this in once set_body is fully implemented.
test.skip("Setting body to an element that isn't body/frameset", () => {
expect(() => {
document.body = document.createElement("div");
}).toThrow(DOMException);
});
// FIXME: Add this in once removeChild is implemented.
test.skip("Nullable", () => {
document.documentElement.removeChild(document.body);
expect(document.body).toBeNull();
});
});

View file

@ -0,0 +1,16 @@
loadPage("file:///res/html/misc/blank.html");
afterInitialPageLoad(() => {
test("Basic functionality", () => {
expect(document.head).not.toBeNull();
// FIXME: Add this in once HTMLHeadElement's constructor is implemented.
//expect(document.head).toBeInstanceOf(HTMLHeadElement);
expect(document.head.nodeName).toBe("head");
});
// FIXME: Add this in once removeChild is implemented.
test.skip("Nullable", () => {
document.documentElement.removeChild(document.head);
expect(document.head).toBeNull();
});
});

View file

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!--This is a comment-->
</body>
</html>

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="duplicate">First</div>
<div class="duplicate">Second</div>
<div id="unique"></div>
<div id="dupeId">First ID</div>
<div id="dupeId">Second ID</div>
</body>
</html>

View file

@ -0,0 +1,16 @@
// NOTE: This file is only for syntax highlighting, documentation, etc.
interface LibwebTester {
/**
* Changes the page to the specified URL. Everything afterwards will refer to the new page.
* @param url Page to load.
*/
changePage(url: string): void;
}
interface Window {
/**
* Special test object used to ease test development for LibWeb.
*/
readonly libweb_tester: LibwebTester;
}

View file

@ -1,8 +1,7 @@
// NOTE: The tester loads in LibJS's test-common to prevent duplication.
// NOTE: "window.libweb_tester" is set to a special tester object.
// The object currently provides the following functions:
// - changePage(url) - change page to given URL. Everything afterwards will refer to the new page.
// See libweb_tester.d.ts for definitions.
let __PageToLoad__;

View file

@ -193,7 +193,7 @@ static Vector<String> get_test_paths(const String& test_root)
Vector<String> paths;
iterate_directory_recursively(test_root, [&](const String& file_path) {
if (!file_path.ends_with("test-common.js"))
if (!file_path.ends_with("test-common.js") && !file_path.ends_with(".html") && !file_path.ends_with(".ts"))
paths.append(file_path);
});