-
Notifications
You must be signed in to change notification settings - Fork 46
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]" | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")); | ||
} | ||
} |
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); | ||
}); |
There was a problem hiding this comment.
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 utilizeswitch
/case