Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(html): implement autocorrect semantics
ldm04ec96e67714c
diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rsindex 046e06f36..91d9a249f 100644--- a/moli-renderer-v8/src/native_bridge/element.rs+++ b/moli-renderer-v8/src/native_bridge/element.rs@@ -435,6 +435,7 @@ pub(super) use global_attributes::{node_access_key_label_getter_function, node_access_key_setter_function,node_allow_fullscreen_getter_function, node_allow_fullscreen_setter_function,node_autocapitalize_getter_function, node_autocapitalize_setter_function,+ node_autocorrect_getter_function, node_autocorrect_setter_function,node_autofocus_getter_function, node_autofocus_setter_function,node_content_editable_getter_function, node_content_editable_setter_function,node_credentialless_getter_function, node_credentialless_setter_function,@@ -1426,6 +1427,13 @@ struct HtmlElementStandardPrototypeDeclaration {setter = node_autocapitalize_setter_function)]autocapitalize: (),+ #[webapi(+ accessor_property,+ enumerable,+ getter = node_autocorrect_getter_function,+ setter = node_autocorrect_setter_function+ )]+ autocorrect: (),#[webapi(accessor_property,enumerable,diff --git a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs b/moli-renderer-v8/src/native_bridge/element/global_attributes.rsindex d5711eaff..b22b1dac4 100644--- a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs+++ b/moli-renderer-v8/src/native_bridge/element/global_attributes.rs@@ -1824,7 +1824,7 @@ fn canonical_autocapitalize_value(raw: Option<&str>) -> &'static str {}}-fn autocapitalize_inherits_from_form(local_name: &str) -> bool {+fn text_input_hint_inherits_from_form(local_name: &str) -> bool {matches!(local_name,"button" | "fieldset" | "input" | "output" | "select" | "textarea"@@ -1855,7 +1855,7 @@ pub(in crate::native_bridge) fn node_autocapitalize_getter_function<'s>(let own_value = element.attribute_ns("", "autocapitalize");let raw = if own_value.is_some_and(|value| !value.is_empty())- || !autocapitalize_inherits_from_form(element.local_name())+ || !text_input_hint_inherits_from_form(element.local_name()){own_value} else {@@ -1900,6 +1900,75 @@ pub(in crate::native_bridge) fn node_autocapitalize_setter_function<'s>(rv.set_undefined();}+pub(in crate::native_bridge) fn node_autocorrect_getter_function<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ args: v8::FunctionCallbackArguments<'s>,+ mut rv: v8::ReturnValue<'s, v8::Value>,+) {+ let Ok((runtime_ptr, handle)) =+ node_runtime_and_handle_from_object_or_detached(scope, args.this())+ else {+ throw_incompatible_getter_receiver(scope, "HTMLElement", "autocorrect");+ return;+ };+ let runtime = unsafe { &*runtime_ptr };+ let Some(element) = runtime+ .dom_host()+ .node(handle)+ .and_then(Node::as_element)+ .filter(|element| element.namespace() == "http://www.w3.org/1999/xhtml")+ else {+ throw_incompatible_getter_receiver(scope, "HTMLElement", "autocorrect");+ return;+ };++ if element.is_html_input()+ && matches!(element.input_type().as_str(), "email" | "password" | "url")+ {+ rv.set_bool(false);+ return;+ }+ let own_value = element.attribute_ns("", "autocorrect");+ let raw = if own_value.is_some() || !text_input_hint_inherits_from_form(element.local_name()) {+ own_value+ } else {+ form_associated_form_owner(runtime, handle)+ .and_then(|form| runtime.dom_host().node(form))+ .and_then(Node::as_element)+ .and_then(|form| form.attribute_ns("", "autocorrect"))+ };+ rv.set_bool(!raw.is_some_and(|value| value.eq_ignore_ascii_case("off")));+}++pub(in crate::native_bridge) fn node_autocorrect_setter_function<'s>(+ scope: &mut v8::PinScope<'s, '_>,+ args: v8::FunctionCallbackArguments<'s>,+ mut rv: v8::ReturnValue<'s, v8::Value>,+) {+ let Ok((runtime_ptr, handle)) =+ node_runtime_and_handle_from_object_or_detached(scope, args.this())+ else {+ throw_incompatible_setter_receiver(scope, "HTMLElement", "autocorrect");+ return;+ };+ let is_html_element = unsafe { &*runtime_ptr }+ .dom_host()+ .node(handle)+ .and_then(Node::as_element)+ .is_some_and(|element| element.namespace() == "http://www.w3.org/1999/xhtml");+ if !is_html_element {+ throw_incompatible_setter_receiver(scope, "HTMLElement", "autocorrect");+ return;+ }+ let value = if args.get(0).boolean_value(scope) {+ "on"+ } else {+ "off"+ };+ set_reflected_attribute(scope, runtime_ptr, handle, "autocorrect", value);+ rv.set_undefined();+}+pub(in crate::native_bridge) fn node_translate_getter_function<'s>(scope: &mut v8::PinScope<'s, '_>,args: v8::FunctionCallbackArguments<'s>,diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rsindex e76bdc871..e00860dac 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs@@ -2541,6 +2541,7 @@ fn detached_global_html_attributes_use_html_element_prototype_accessors() {"title","lang","autocapitalize",+ "autocorrect","translate","dir","hidden",@@ -2563,6 +2564,7 @@ fn detached_global_html_attributes_use_html_element_prototype_accessors() {descriptors.title.set.call(element, "Title");descriptors.lang.set.call(element, "en");descriptors.autocapitalize.set.call(element, "WORDS");+ descriptors.autocorrect.set.call(element, false);descriptors.dir.set.call(element, "rtl");descriptors.accessKey.set.call(element, "k");descriptors.enterKeyHint.set.call(element, "send");@@ -2577,6 +2579,7 @@ fn detached_global_html_attributes_use_html_element_prototype_accessors() {assert(descriptors.title.get.call(element) === "Title", `${label}.title`);assert(descriptors.lang.get.call(element) === "en", `${label}.lang`);assert(descriptors.autocapitalize.get.call(element) === "words", `${label}.autocapitalize`);+ assert(descriptors.autocorrect.get.call(element) === false, `${label}.autocorrect`);assert(descriptors.dir.get.call(element) === "rtl", `${label}.dir`);assert(descriptors.accessKey.get.call(element) === "k", `${label}.accessKey`);assert(descriptors.enterKeyHint.get.call(element) === "send", `${label}.enterKeyHint`);diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rsindex f78434ffe..65919d2a9 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs@@ -3195,6 +3195,95 @@ fn detached_native_xml_text_content_queues_child_list_record() {assert_eq!(result, "4|foo|3|foo");}+#[test]+fn autocorrect_reflects_boolean_and_inherits_from_form_owner() {+ let mut vm = new_parsed_test_vm(+ "https://autocorrect.test/",+ "<!doctype html><html><body></body></html>",+ );++ let result = vm+ .eval(+ r#"+(() => {+ const div = document.createElement('div');+ const canonical = [null, 'on', 'ON', 'off', 'OFF', 'invalid', ''].map(value => {+ if (value === null) div.removeAttribute('autocorrect');+ else div.setAttribute('autocorrect', value);+ return div.autocorrect;+ });++ div.autocorrect = 'hello';+ const truthy = [div.autocorrect, div.getAttribute('autocorrect')];+ div.autocorrect = 0;+ const falsy = [div.autocorrect, div.getAttribute('autocorrect')];++ const form = document.createElement('form');+ form.id = 'owner';+ form.setAttribute('autocorrect', 'off');+ document.body.appendChild(form);+ const inheritedNames = ['button', 'fieldset', 'input', 'output', 'select', 'textarea'];+ const inherited = inheritedNames.map(name => {+ const child = document.createElement(name);+ form.appendChild(child);+ const external = document.createElement(name);+ external.setAttribute('form', 'owner');+ external.setAttributeNS('urn:test', 'autocorrect', 'on');+ document.body.appendChild(external);+ const values = [child.autocorrect, external.autocorrect];+ child.setAttribute('autocorrect', '');+ values.push(child.autocorrect);+ child.setAttribute('autocorrect', 'off');+ values.push(child.autocorrect);+ return values.join(':');+ });++ const nonInherited = ['img', 'object'].map(name => {+ const element = document.createElement(name);+ form.appendChild(element);+ return element.autocorrect;+ });+ const forcedOff = ['password', 'email', 'url'].map(type => {+ const input = document.createElement('input');+ input.type = type;+ input.autocorrect = true;+ return input.autocorrect;+ });++ const descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'autocorrect');+ const incompatible = operation => {+ try {+ operation(document.createElementNS('urn:test', 'div'));+ return 'none';+ } catch (error) {+ return error.name;+ }+ };++ return JSON.stringify({+ descriptor: [descriptor.enumerable, descriptor.configurable],+ canonical,+ truthy,+ falsy,+ inherited,+ nonInherited,+ forcedOff,+ incompatible: [+ incompatible(receiver => descriptor.get.call(receiver)),+ incompatible(receiver => descriptor.set.call(receiver, true))+ ]+ });+})()+"#,+ )+ .expect("autocorrect semantics should evaluate");++ assert_eq!(+ result,+ r#"{"descriptor":[true,true],"canonical":[true,true,true,false,false,true,true],"truthy":[true,"on"],"falsy":[false,"off"],"inherited":["false:false:true:false","false:false:true:false","false:false:true:false","false:false:true:false","false:false:true:false","false:false:true:false"],"nonInherited":[true,true],"forcedOff":[false,false,false],"incompatible":["TypeError","TypeError"]}"#+ );+}+#[test]fn set_range_text_preserve_mode_adjusts_selection_by_replacement_delta() {let mut vm = new_storage_test_vm("https://forms-selection-set-range-text.test/");diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rsindex 83d42fc82..54f4f9102 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs@@ -711,6 +711,7 @@ fn htmlelement_standard_accessors_live_on_owner_prototypes() {"title","lang","autocapitalize",+ "autocorrect","translate","dir","hidden",@@ -752,6 +753,7 @@ fn htmlelement_standard_accessors_live_on_owner_prototypes() {div.title = "hello";div.lang = "en-US";div.autocapitalize = "WORDS";+ div.autocorrect = false;div.translate = false;div.dir = "RTL";div.hidden = true;@@ -769,6 +771,7 @@ fn htmlelement_standard_accessors_live_on_owner_prototypes() {assert(div.title === "hello", "title behavior");assert(div.lang === "en-US", "lang behavior");assert(div.autocapitalize === "words" && div.getAttribute("autocapitalize") === "WORDS", "autocapitalize behavior");+ assert(div.autocorrect === false && div.getAttribute("autocorrect") === "off", "autocorrect behavior");assert(div.translate === false && div.getAttribute("translate") === "no", "translate behavior");assert(div.dir === "rtl", "dir behavior");assert(div.hidden === true && div.hasAttribute("hidden"), "hidden behavior");