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(dom): share native attribute node lookup
ldm0cc9d78f76fb6
diff --git a/moli-renderer-v8/src/native_bridge/document/attributes/attr_object/cache.rs b/moli-renderer-v8/src/native_bridge/document/attributes/attr_object/cache.rsindex 3c58f0775..f5029c46c 100644--- a/moli-renderer-v8/src/native_bridge/document/attributes/attr_object/cache.rs+++ b/moli-renderer-v8/src/native_bridge/document/attributes/attr_object/cache.rs@@ -1,31 +1,10 @@+use super::super::attribute_node::{native_attr_object_by_name, native_attr_object_by_namespace};use super::instance::attr_current_value;use super::*;-use crate::native_bridge::node_runtime_and_handle_from_object;use crate::util::{get_private_value, new_null_prototype_object, set_private_value};const ATTR_OBJECT_CACHE_SLOT: &str = "__moliAttrObjectCache";-fn live_native_attribute_lookup_name<'s>(- scope: &mut v8::PinScope<'s, '_>,- element: v8::Local<'s, v8::Object>,- name: &str,-) -> Option<String> {- let (runtime_ptr, handle) = node_runtime_and_handle_from_object(scope, element).ok()?;- unsafe { &*runtime_ptr }- .dom_host()- .dom()- .normalized_attribute_name(handle, name)-}--fn attribute_lookup_name<'s>(- scope: &mut v8::PinScope<'s, '_>,- element: v8::Local<'s, v8::Object>,- name: &str,-) -> String {- live_native_attribute_lookup_name(scope, element, name)- .unwrap_or_else(|| detached_attribute_name(scope, element, name))-}-fn ensure_object_cache<'s>(scope: &mut v8::PinScope<'s, '_>,object: v8::Local<'s, v8::Object>,@@ -276,42 +255,7 @@ pub(crate) fn live_get_attribute_node_object<'s>(element: v8::Local<'s, v8::Object>,name: &str,) -> Option<v8::Local<'s, v8::Object>> {- let lookup_name = attribute_lookup_name(scope, element, name);- let value = call_object_method(- scope,- element,- "getAttribute",- &[v8_string(scope, &lookup_name)- .map(Into::<v8::Local<'_, v8::Value>>::into)- .unwrap_or_else(|| v8::String::empty(scope).into())],- )?;- if value.is_null_or_undefined() {- return None;- }- let cache = live_attr_cache_object(scope, element)?;- let mut attr = object_property_as_object(scope, cache, &lookup_name)- .or_else(|| cached_attr_by_name(scope, cache, &lookup_name));- if attr.is_none() {- attr = new_attr_object(- scope,- &lookup_name,- "",- Some(element),- None,- None,- None,- &lookup_name,- );- if let Some(attr_object) = attr {- set_attr_cache_entry(scope, cache, &lookup_name, attr_object);- }- }- let attr = attr?;- if let Some(state) = attr_state_object(scope, attr) {- let _ = state.set(scope, v8str(scope, "ownerElement").into(), element.into());- let _ = state.set(scope, v8str(scope, "value").into(), value);- }- Some(attr)+ native_attr_object_by_name(scope, element, name)}pub(in crate::native_bridge) fn live_get_attribute_node_ns_object<'s>(@@ -320,41 +264,5 @@ pub(in crate::native_bridge) fn live_get_attribute_node_ns_object<'s>(namespace_uri: Option<&str>,local_name: &str,) -> Option<v8::Local<'s, v8::Object>> {- let namespace_value = namespace_uri- .and_then(|namespace| v8_string(scope, namespace))- .map(Into::<v8::Local<'_, v8::Value>>::into)- .unwrap_or_else(|| v8::null(scope).into());- let local_name_value = v8_string(scope, local_name)?;- let value = call_object_method(- scope,- element,- "getAttributeNS",- &[namespace_value, local_name_value.into()],- )?;- if value.is_null_or_undefined() {- return None;- }- let cache = live_attr_cache_object(scope, element)?;- let namespace_key = namespace_attr_cache_key(namespace_uri, local_name);- let attr = object_property_as_object(scope, cache, &namespace_key)- .or_else(|| cached_attr_by_namespace(scope, cache, namespace_uri, local_name))- .or_else(|| {- let attr = new_attr_object(- scope,- local_name,- "",- Some(element),- None,- namespace_uri,- None,- local_name,- )?;- set_attr_cache_entry(scope, cache, &namespace_key, attr);- Some(attr)- })?;- if let Some(state) = attr_state_object(scope, attr) {- let _ = state.set(scope, v8str(scope, "ownerElement").into(), element.into());- let _ = state.set(scope, v8str(scope, "value").into(), value);- }- Some(attr)+ native_attr_object_by_namespace(scope, element, namespace_uri, local_name)}diff --git a/moli-renderer-v8/src/native_bridge/document/attributes/attribute_node.rs b/moli-renderer-v8/src/native_bridge/document/attributes/attribute_node.rsindex fd41a981b..f30d3b9a0 100644--- a/moli-renderer-v8/src/native_bridge/document/attributes/attribute_node.rs+++ b/moli-renderer-v8/src/native_bridge/document/attributes/attribute_node.rs@@ -7,6 +7,7 @@ use crate::native_bridge::element::{set_live_element_attribute_appending_to_current_reaction_queue,set_live_element_attribute_ns_appending_to_current_reaction_queue,};+use crate::native_bridge::node::node_runtime_and_handle_from_object_or_detached;use crate::native_bridge::node_runtime_and_handle_from_object;use crate::webidl;@@ -608,24 +609,18 @@ fn live_native_attr_object_from_metadata<'s>(let cache = live_attr_cache_object(scope, element)?;let namespace_key =namespace_attr_cache_key(metadata.namespace_uri.as_deref(), &metadata.local_name);- let attr = object_property_as_object(scope, cache, &namespace_key)- .or_else(|| {- live_attr_metadata_can_alias_qualified_name(metadata)- .then(|| object_property_as_object(scope, cache, &metadata.name))- .flatten()- })- .or_else(|| {- new_attr_object(- scope,- &metadata.name,- &metadata.value,- Some(element),- None,- metadata.namespace_uri.as_deref(),- metadata.prefix.as_deref(),- &metadata.local_name,- )- })?;+ let attr = object_property_as_object(scope, cache, &namespace_key).or_else(|| {+ new_attr_object(+ scope,+ &metadata.name,+ &metadata.value,+ Some(element),+ None,+ metadata.namespace_uri.as_deref(),+ metadata.prefix.as_deref(),+ &metadata.local_name,+ )+ })?;if let Some(state) = attr_state_object(scope, attr) {attach_attr_node(scope, state, element, &metadata.value);}@@ -748,30 +743,32 @@ fn nullable_state_string<'s>(.filter(|value| !value.is_empty())}-fn native_attr_object_by_name<'s>(+pub(in crate::native_bridge::document) fn native_attr_object_by_name<'s>(scope: &mut v8::PinScope<'s, '_>,element: v8::Local<'s, v8::Object>,name: &str,) -> Option<v8::Local<'s, v8::Object>> {- read_detached_native_attribute_snapshot(scope, element)?- .into_iter()- .find(|attribute| attribute.name == name)- .and_then(|attribute| native_attr_object_from_snapshot(scope, element, &attribute))+ let (runtime_ptr, handle) =+ node_runtime_and_handle_from_object_or_detached(scope, element).ok()?;+ let metadata = live_native_attribute_metadata_for_name(unsafe { &*runtime_ptr }, handle, name)?;+ live_native_attr_object_from_metadata(scope, element, &metadata)}-fn native_attr_object_by_namespace<'s>(+pub(in crate::native_bridge::document) fn native_attr_object_by_namespace<'s>(scope: &mut v8::PinScope<'s, '_>,element: v8::Local<'s, v8::Object>,namespace_uri: Option<&str>,local_name: &str,) -> Option<v8::Local<'s, v8::Object>> {- read_detached_native_attribute_snapshot(scope, element)?- .into_iter()- .find(|attribute| {- attribute.namespace_uri.as_deref() == namespace_uri- && attribute.local_name == local_name- })- .and_then(|attribute| native_attr_object_from_snapshot(scope, element, &attribute))+ let (runtime_ptr, handle) =+ node_runtime_and_handle_from_object_or_detached(scope, element).ok()?;+ let metadata = live_native_attribute_metadata_for_namespace(+ unsafe { &*runtime_ptr },+ handle,+ namespace_uri,+ local_name,+ )?;+ live_native_attr_object_from_metadata(scope, element, &metadata)}pub(in crate::native_bridge::document) fn native_attr_object_from_snapshot<'s>(@@ -782,24 +779,18 @@ pub(in crate::native_bridge::document) fn native_attr_object_from_snapshot<'s>(let cache = live_attr_cache_object(scope, element)?;let namespace_key =namespace_attr_cache_key(attribute.namespace_uri.as_deref(), &attribute.local_name);- let attr = object_property_as_object(scope, cache, &namespace_key)- .or_else(|| {- native_attr_can_alias_qualified_name(attribute)- .then(|| object_property_as_object(scope, cache, &attribute.name))- .flatten()- })- .or_else(|| {- new_attr_object(- scope,- &attribute.name,- &attribute.value,- Some(element),- None,- attribute.namespace_uri.as_deref(),- attribute.prefix.as_deref(),- &attribute.local_name,- )- })?;+ let attr = object_property_as_object(scope, cache, &namespace_key).or_else(|| {+ new_attr_object(+ scope,+ &attribute.name,+ &attribute.value,+ Some(element),+ None,+ attribute.namespace_uri.as_deref(),+ attribute.prefix.as_deref(),+ &attribute.local_name,+ )+ })?;if let Some(state) = attr_state_object(scope, attr) {attach_attr_node(scope, state, element, &attribute.value);}@@ -830,12 +821,7 @@ fn cache_attached_attr_node<'s>(let namespace_key =namespace_attr_cache_key(metadata.namespace_uri.as_deref(), &metadata.local_name);set_attr_cache_entry(scope, cache, &namespace_key, attr);- if metadata.namespace_uri.is_none()- || metadata- .prefix- .as_deref()- .is_some_and(|prefix| !prefix.is_empty())- {+ if live_attr_metadata_can_alias_qualified_name(metadata) {set_attr_cache_entry(scope, cache, &metadata.name, attr);}}diff --git a/moli-renderer-v8/src/native_bridge/document/attributes/named_node_map/helpers.rs b/moli-renderer-v8/src/native_bridge/document/attributes/named_node_map/helpers.rsindex 0f8032b0c..8e7576748 100644--- a/moli-renderer-v8/src/native_bridge/document/attributes/named_node_map/helpers.rs+++ b/moli-renderer-v8/src/native_bridge/document/attributes/named_node_map/helpers.rs@@ -193,12 +193,10 @@ fn indexed_attribute_object<'s>(let cache_key = indexed_attribute_cache_key(&attribute);let namespace_key =namespace_attr_cache_key(attribute.namespace_uri.as_deref(), &attribute.local_name);- let name_alias = indexed_attribute_can_alias_qualified_name(&attribute)- .then(|| object_property_as_object(scope, cache, &attribute.name))- .flatten();+ // Qualified names are not unique across namespaces. Only canonical keys+ // identify an Attr; the name alias is an exposed lookup projection.if let Some(attr) = object_property_as_object(scope, cache, &cache_key).or_else(|| object_property_as_object(scope, cache, &namespace_key))- .or(name_alias){if let Some(state) = attr_state_object(scope, attr) {let _ = state.set(scope, v8str(scope, "ownerElement").into(), element.into());diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/attribute_lookup.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/attribute_lookup.rsnew file mode 100644index 000000000..cba2c3aad--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/attribute_lookup.rs@@ -0,0 +1,63 @@+use super::*;++#[test]+fn attribute_node_lookup_uses_native_metadata_and_namespace_identity() {+ let mut vm = new_storage_test_vm("https://attribute-lookup.test/");+ assert_eq!(+ vm.eval(+ r#"(() => {+ const failures = [];+ const check = (value, label) => { if (!value) failures.push(label); };+ let reads = 0;+ const documents = [document, document.implementation.createHTMLDocument(''),+ new DOMParser().parseFromString('<root/>', 'application/xml')];+ for (const [index, doc] of documents.entries()) {+ const element = doc.createElement('section');+ element.setAttribute('data-value', 'first');+ element.setAttributeNS('urn:one', 'p:value', 'one');+ element.setAttributeNS('urn:two', 'p:value', 'two');+ for (const name of ['getAttribute', 'getAttributeNS']) {+ Object.defineProperty(element, name, {configurable: true, get() {+ ++reads;+ throw new Error('author attribute getter');+ }});+ }+ const attr = element.getAttributeNode('data-value');+ check(attr?.ownerElement === element, index + '/owner');+ check(attr === element.attributes.getNamedItem('data-value'), index + '/named-map');+ check(attr === element.getAttributeNodeNS(null, 'data-value'), index + '/null-namespace');+ check(element.getAttributeNode('missing') === null, index + '/missing');+ check(element.getAttributeNodeNS('urn:missing', 'value') === null, index + '/missing-ns');+ if (index < 2) {+ check(element.getAttributeNode('DATA-VALUE') === attr, index + '/html-case');+ } else {+ check(element.getAttributeNode('DATA-VALUE') === null, index + '/xml-case');+ }+ const one = element.getAttributeNodeNS('urn:one', 'value');+ const two = element.getAttributeNodeNS('urn:two', 'value');+ check(one !== two && one.namespaceURI === 'urn:one' && two.namespaceURI === 'urn:two', index + '/ns-identity');+ check(one === element.attributes.getNamedItemNS('urn:one', 'value'), index + '/map-one');+ check(two === element.attributes.getNamedItemNS('urn:two', 'value'), index + '/map-two');+ check(element.getAttributeNode('p:value') === one, index + '/first-qualified');+ check(reads === 0, index + '/lookup-getters');+ // Poison lookup entry points only; value/mutation fallbacks+ // are separate paths from attribute-node lookup.+ delete element.getAttribute;+ delete element.getAttributeNS;+ check(attr.value === 'first', index + '/value');+ check(one.value === 'one' && two.value === 'two', index + '/ns-values');+ element.setAttribute('data-value', 'second');+ check(element.getAttributeNode('data-value') === attr && attr.value === 'second', index + '/update');+ element.removeAttributeNS('urn:one', 'value');+ check(one.ownerElement === null, index + '/detached-owner');+ check(element.getAttributeNode('p:value') === two, index + '/remaining-qualified');+ check(element.getAttributeNodeNS('urn:two', 'value') === two, index + '/retained-ns');+ }+ check(reads === 0, 'public getters not invoked');+ return JSON.stringify(failures);+ })()"#,+ )+ .unwrap(),+ "[]",+ );+}diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/mod.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/mod.rsindex 331e4ae8b..7000e0114 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/mod.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/mod.rs@@ -1,5 +1,6 @@use super::*;+mod attribute_lookup;mod custom_elements;mod detached;mod dom_surface;diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rsindex b83fbaac3..08d2bdb92 100644--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom.rs@@ -6942,7 +6942,7 @@ fn detached_document_adopts_live_namespaced_attributes() {assert_eq!(result,- "native||value|value|true|data-real,a:flag|flag||urn:attr|flag|value"+ "native||value|value|true|data-real,a:flag|a:flag|a|urn:attr|flag|value");}#[test]