Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement HTMLInputElement #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"imports": {
"assert": "jsr:@std/[email protected]",
"expect": "jsr:@std/[email protected]",
"fs": "jsr:@std/[email protected]",
"path": "jsr:@std/[email protected]"
},
Expand Down
12 changes: 12 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DocumentType } from "./dom/document.ts";
import { DocumentFragment } from "./dom/document-fragment.ts";
import { HTMLTemplateElement } from "./dom/elements/html-template-element.ts";
import { Element } from "./dom/element.ts";
import { HTMLInputElement } from "./dom/elements/html-input-element.ts";

export function nodesFromString(html: string): Node {
const parsed = JSON.parse(parse(html));
Expand Down Expand Up @@ -47,6 +48,13 @@ function nodeFromArray(data: any, parentNode: Node | null): Node {
contentFrag,
);
}
if (data[1] === "input") {
return new HTMLInputElement(
parentNode,
data[2],
CTOR_KEY,
);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this and the above if to utilize switch/case


const elm = new Element(data[1], parentNode, data[2], CTOR_KEY);
const childNodes = elm._getChildNodesMutator();
Expand Down
6 changes: 6 additions & 0 deletions src/dom/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HTMLTemplateElement } from "./elements/html-template-element.ts";
import { getSelectorEngine, SelectorApi } from "./selectors/selectors.ts";
import { getElementsByClassName } from "./utils.ts";
import UtilTypes from "./utils-types.ts";
import { HTMLInputElement } from "./elements/html-input-element.ts";

export class DOMImplementation {
constructor(key: typeof CTOR_KEY) {
Expand Down Expand Up @@ -224,6 +225,11 @@ export class Document extends Node {
elm._setOwnerDocument(this);
return elm;
}
case "INPUT": {
const elm = new HTMLInputElement(null, [], CTOR_KEY);
elm._setOwnerDocument(this);
return elm;
}

default: {
const elm = new Element(tagName, null, [], CTOR_KEY);
Expand Down
34 changes: 34 additions & 0 deletions src/dom/elements/html-input-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Node } from "../node.ts";
import type { CTOR_KEY } from "../../constructor-lock.ts";
import { Element } from "../element.ts";

export class HTMLInputElement extends Element {
constructor(
parentNode: Node | null,
attributes: [string, string][],
key: typeof CTOR_KEY,
) {
super(
"INPUT",
parentNode,
attributes,
key,
);
}

get value() {
return this.getAttribute("value") ?? "";
}
set value(value: any) {
this.setAttribute("value", value);
this.dispatchEvent(new Event("input"));
Copy link
Owner

@b-fuze b-fuze Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Programmatically setting the input values does not actually trigger DOM change events. You can check the very last two notes in the input element eventing behaviour section of the standard specification.

}

get checked() {
return this.hasAttribute("checked");
}
set checked(value: boolean) {
this.toggleAttribute("checked", value);
this.dispatchEvent(new Event("input"));
}
}
32 changes: 32 additions & 0 deletions test/units/HTMLElement-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DOMParser } from "../../deno-dom-wasm.ts";
import { expect, fn } from "expect";
import type { HTMLInputElement } from "../../src/dom/elements/html-input-element.ts";

Deno.test("HTMLInputElement reacts to value and checked properties changes", () => {
const doc = new DOMParser().parseFromString(
`<input value="foo" type="checkbox">`,
"text/html",
);
const listener = fn() as unknown as EventListenerObject;
const input = doc.querySelector<HTMLInputElement>("input")!;

expect(input.value).toBe("foo");
expect(input.checked).toBe(false);

input.addEventListener("input", listener);

input.value = "bar";
expect(input.value).toBe("bar");
expect(input.getAttribute("value")).toBe("bar");
expect(listener).toBeCalledTimes(1);

input.checked = true;
expect(input.checked).toBe(true);
expect(input.hasAttribute("checked")).toBe(true);
expect(listener).toBeCalledTimes(2);

input.checked = false;
expect(input.checked).toBe(false);
expect(input.hasAttribute("checked")).toBe(false);
expect(listener).toBeCalledTimes(3);
});
Loading