Skip to content

Conversation

@karlseguin
Copy link
Collaborator

Support CDP's Input.dispatchKeyEvent and DOM key events. Currently only keydown is supported and expects every key to be a displayable character.

It turns out that manipulating the DOM via key events isn't great because the behavior really depends on the cursor. So, to do this more accurately, we'd have to introduce some concept of a cursor.

Personally, I don't think we'll run into many pages that are purposefully using keyboard events. But driver (puppeteer/playwright) scripts might be another issue.

return strToData(s);
}

pub fn textareaSetValue(textarea: *TextArea, value: []const u8) !void {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note value has a defaultValue. So likely needs the same care as HtmlInputElement.Value.
It may be implemented correctly here, but it is not obvious to me.

I think it may be best to also implement defaultValue as they considered the same property.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From my testing, both TextArea and Input will return something like value orelse defaultValue if the value hasn't been set. So that's right.

However, they both only have defaultValue if defaultValue is explicitly set. Given this html:

<textarea id=ta>abc</textarea>

Then this JS should return abc:

document.getElementById("ta").value;

But it returns empty.

Only if we explicitly set defaultValue:

document.getElementById("ta").defaultValue = 'abc'

Does everything work as expected.

In other words, I guess when we're parsing the page, we should be looking for any input/textarea and setting their defaultValue as needed since libdom doesn't do it for us.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, generally the logic is something like x orelse defaultX orelse initialX where initialX is the value as defined if not set upon construction, not to be confused with the afterInvalidX and then there are errorXs, which when set throw an error instead of using the afterInvalidX.

Other surprising behavior is that Element.setAttribute(element, 'value', 'abc') will set defaultValue, not value.

<textarea id=ta>abc</textarea>
document.getElementById("ta").value;

That this does not work is surprising, but libDOM struggles a lot with these details.
I'm a fair way towards being able to implement HtmlElements without C in Zig such that we can have much easier control. Mainly I just need to work out which DOM events to send (deprecated for external usage, but we still use them internally). Tho I would need to look into why that abc is dropped in the first example.

karlseguin and others added 4 commits June 12, 2025 09:45
Support CDP's Input.dispatchKeyEvent and DOM key events. Currently only
keydown is supported and expects every key to be a displayable character.

It turns out that manipulating the DOM via key events isn't great because the
behavior really depends on the cursor. So, to do this more accurately, we'd
have to introduce some concept of a cursor.

Personally, I don't think we'll run into many pages that are purposefully
using keyboard events. But driver (puppeteer/playwright) scripts might be
another issue.
Submit form on "Enter" within text input.

Convert "Enter" to "\n" in textarea.
@karlseguin karlseguin merged commit 0de33b3 into main Jun 12, 2025
11 checks passed
@karlseguin karlseguin deleted the keydown_handling branch June 12, 2025 04:32
@github-actions github-actions bot locked and limited conversation to collaborators Jun 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants