Skip to content

Truly independent web browser

LibWeb: Implement DOMStringMap in Rust

Shannon Booth5e3ca21aa55f
diff --git a/Libraries/LibWeb/Bindings/Rust/src/dom/element.rs b/Libraries/LibWeb/Bindings/Rust/src/dom/element.rs
index 052449cbe3f..dce11d81670 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/dom/element.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/dom/element.rs
@@ -22,6 +22,12 @@ unsafe extern "C" {
fn web_dom_element_local_name(element: *mut c_void) -> usize;
fn web_dom_element_get_attribute_ns(element: *mut c_void, namespace: usize, name: usize) -> usize;
fn web_dom_element_set_attribute_value(element: *mut c_void, name: usize, value: usize);
+ fn web_dom_element_remove_attribute(element: *mut c_void, name: usize);
+ fn web_dom_element_for_each_attribute(
+ element: *mut c_void,
+ callback: *mut c_void,
+ on_attribute: unsafe extern "C" fn(*mut c_void, usize, usize),
+ );
fn web_dom_element_attribute(
element: *mut c_void,
name: ak::Span<'_, u8>,
@@ -35,6 +41,18 @@ unsafe extern "C" fn on_value(value: *mut c_void, raw_value: usize) {
unsafe { *value.cast::<Option<Utf16String>>() = Some(Utf16String::from_raw_owned(raw_value)) };
}
+unsafe extern "C" fn on_attribute(callback: *mut c_void, name: usize, value: usize) {
+ // SAFETY: callback is the callback of the caller, and LibWeb lends the name and the value for the call.
+ let (callback, name, value) = unsafe {
+ (
+ &mut *callback.cast::<&mut dyn FnMut(&Utf16FlyString, &Utf16String)>(),
+ Utf16FlyString::from_raw_borrowed(name),
+ Utf16String::from_raw_borrowed(value),
+ )
+ };
+ callback(&name, &value);
+}
+
/// https://dom.spec.whatwg.org/#concept-element
#[derive(Clone, Trace)]
#[gc(crate = libjs_rust)]
@@ -71,6 +89,20 @@ impl CppElement {
unsafe { web_dom_element_set_attribute_value(self.0.as_ptr(), name.raw_identity(), value.into_raw()) };
}
+ /// https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
+ pub(crate) fn remove_attribute(&self, name: &Utf16FlyString) {
+ // SAFETY: The element is live and the name is lent for the call.
+ unsafe { web_dom_element_remove_attribute(self.0.as_ptr(), name.raw_identity()) };
+ }
+
+ /// Calls `callback` with the qualified name and the value of each attribute in the element's attribute list, in
+ /// order.
+ pub(crate) fn for_each_attribute(&self, mut callback: impl FnMut(&Utf16FlyString, &Utf16String)) {
+ let mut callback: &mut dyn FnMut(&Utf16FlyString, &Utf16String) = &mut callback;
+ // SAFETY: The element is live, and the callback outlives the call.
+ unsafe { web_dom_element_for_each_attribute(self.0.as_ptr(), (&raw mut callback).cast(), on_attribute) };
+ }
+
/// https://dom.spec.whatwg.org/#concept-element-attributes-get-value
pub(crate) fn attribute(&self, name: &str) -> Option<Utf16String> {
let mut value = None;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/dom_string_map.rs b/Libraries/LibWeb/Bindings/Rust/src/html/dom_string_map.rs
new file mode 100644
index 00000000000..d765c7c4df1
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/dom_string_map.rs
@@ -0,0 +1,286 @@
+/*
+ * Copyright (c) 2021-2023, Luke Wilde <[email protected]>
+ * Copyright (c) 2022, Andreas Kling <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! https://html.spec.whatwg.org/multipage/dom.html#domstringmap
+
+use core::ffi::c_void;
+
+use ak::{Utf16FlyString, Utf16String};
+use libjs_rust::api::{Trace, Utf16StringBuilder, Utf16View, Vm};
+
+use crate::bindings::interface::{Wrappable, allocate, cell_into_abi};
+use crate::bindings::{CallContext, Exception, WebIdlResult};
+use crate::dom::element::{CppElement, is_valid_attribute_local_name};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct DOMStringMap {
+ // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-element
+ associated_element: CppElement,
+}
+
+impl Wrappable for DOMStringMap {}
+
+impl DOMStringMap {
+ // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
+ pub(crate) fn supported_property_names(&self, _: &mut CallContext) -> Vec<Utf16String> {
+ // The supported property names on a DOMStringMap object at any instant are the names of each pair returned from
+ // getting the DOMStringMap's name-value pairs at that instant, in the order returned.
+ //
+ // 1. Let list be an empty list of name-value pairs.
+ let mut names = Vec::new();
+
+ self.associated_element.for_each_attribute(|name, _| {
+ if let Some(property_name) = property_name_from_data_attribute_name(Utf16View::of_fly_string(name)) {
+ names.push(property_name);
+ }
+ });
+
+ // 4. Return list.
+ names
+ }
+
+ // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-nameditem
+ pub(crate) fn determine_value_of_named_property(
+ &self,
+ _: &mut CallContext,
+ name: Utf16String,
+ ) -> WebIdlResult<Utf16String> {
+ // To determine the value of a named property name for a DOMStringMap, return the value component of the
+ // name-value pair whose name component is name in the list returned from getting the DOMStringMap's name-value
+ // pairs.
+ let mut value = None;
+ self.associated_element
+ .for_each_attribute(|attribute_name, attribute_value| {
+ if value.is_some() {
+ return;
+ }
+ if data_attribute_name_matches_property_name(
+ Utf16View::of_fly_string(attribute_name),
+ Utf16View::of_string(&name),
+ ) {
+ value = Some(attribute_value.clone());
+ }
+ });
+
+ Ok(value.expect("a DOMStringMap determines the value of a supported property name"))
+ }
+
+ pub(crate) fn set_value_of_new_named_property(
+ &self,
+ _: &mut CallContext,
+ name: Utf16String,
+ value: Utf16String,
+ ) -> WebIdlResult<()> {
+ self.set_value_of_named_property(&name, value)
+ }
+
+ pub(crate) fn set_value_of_existing_named_property(
+ &self,
+ _: &mut CallContext,
+ name: Utf16String,
+ value: Utf16String,
+ ) -> WebIdlResult<()> {
+ self.set_value_of_named_property(&name, value)
+ }
+
+ // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
+ fn set_value_of_named_property(&self, name: &Utf16String, value: Utf16String) -> WebIdlResult<()> {
+ let mut builder = Utf16StringBuilder::new();
+
+ // 3. Insert the string data- at the front of name.
+ // NB: This is done out of order because the builder cannot prepend.
+ builder.append_ascii("data-");
+
+ let name = Utf16View::of_string(name);
+
+ for character_index in 0..name.length_in_code_units() {
+ // 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a
+ // "SyntaxError" DOMException.
+ let current_character = name.code_unit_at(character_index);
+
+ if current_character == u16::from(b'-') && character_index + 1 < name.length_in_code_units() {
+ let next_character = name.code_unit_at(character_index + 1);
+ if is_ascii_lower_alpha(next_character) {
+ return Err(Exception::syntax_error(Utf16String::from_utf8(
+ "Name cannot contain a '-' followed by a lowercase character.",
+ )));
+ }
+ }
+
+ // 2. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character
+ // and replace the character with the same character converted to ASCII lowercase.
+ if is_ascii_upper_alpha(current_character) {
+ builder.append_ascii("-");
+ builder.append_code_unit(to_ascii_lowercase(current_character));
+ continue;
+ }
+
+ builder.append_code_unit(current_character);
+ }
+
+ // 4. If name is not a valid attribute local name, then throw an "InvalidCharacterError" DOMException.
+ let data_name = builder.to_utf16_string();
+ if !is_valid_attribute_local_name(&data_name) {
+ return Err(Exception::Dom(
+ "InvalidCharacterError",
+ Utf16String::from_utf8("Name is not a valid attribute local name."),
+ ));
+ }
+
+ // 5. Set an attribute value for the DOMStringMap's associated element using name and value.
+ let data_name = Utf16FlyString::from_utf16_string(&data_name);
+ self.associated_element.set_attribute_value(&data_name, value);
+
+ Ok(())
+ }
+
+ // https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
+ pub(crate) fn delete_existing_named_property(&self, _: &mut CallContext, name: Utf16String) -> WebIdlResult<()> {
+ let mut builder = Utf16StringBuilder::new();
+
+ // 2. Insert the string data- at the front of name.
+ // NB: This is done out of order because the builder cannot prepend.
+ builder.append_ascii("data-");
+
+ for character in Utf16View::of_string(&name).code_units() {
+ // 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character
+ // and replace the character with the same character converted to ASCII lowercase.
+ if is_ascii_upper_alpha(character) {
+ builder.append_ascii("-");
+ builder.append_code_unit(to_ascii_lowercase(character));
+ continue;
+ }
+
+ builder.append_code_unit(character);
+ }
+
+ // 3. Remove an attribute by name given name and the DOMStringMap's associated element.
+ let data_name = Utf16FlyString::from_utf16_string(&builder.to_utf16_string());
+ self.associated_element.remove_attribute(&data_name);
+
+ Ok(())
+ }
+}
+
+fn is_ascii_upper_alpha(code_unit: u16) -> bool {
+ u8::try_from(code_unit).is_ok_and(|character| character.is_ascii_uppercase())
+}
+
+fn is_ascii_lower_alpha(code_unit: u16) -> bool {
+ u8::try_from(code_unit).is_ok_and(|character| character.is_ascii_lowercase())
+}
+
+fn to_ascii_uppercase(code_unit: u16) -> u16 {
+ u8::try_from(code_unit).map_or(code_unit, |character| u16::from(character.to_ascii_uppercase()))
+}
+
+fn to_ascii_lowercase(code_unit: u16) -> u16 {
+ u8::try_from(code_unit).map_or(code_unit, |character| u16::from(character.to_ascii_lowercase()))
+}
+
+fn is_data_attribute_name(name: Utf16View<'_>) -> bool {
+ // This implements the attribute-name predicate from getting the DOMStringMap's name-value pairs:
+ //
+ // 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the
+ // string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas, ...
+ if !name.starts_with(Utf16View::Ascii(b"data-")) {
+ return false;
+ }
+
+ let name_after_starting_data = name.substring_view(5, name.length_in_code_units() - 5);
+ !name_after_starting_data.code_units().any(is_ascii_upper_alpha)
+}
+
+fn property_name_from_data_attribute_name(name: Utf16View<'_>) -> Option<Utf16String> {
+ if !is_data_attribute_name(name) {
+ return None;
+ }
+
+ let name_after_starting_data = name.substring_view(5, name.length_in_code_units() - 5);
+
+ // 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the
+ // string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas, in the order
+ // that those attributes are listed in the element's attribute list, add a name-value pair to list whose name is
+ // the attribute's name with the first five characters removed and whose value is the attribute's value.
+ //
+ // 3. For each name in list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by an ASCII
+ // lower alpha, remove the U+002D HYPHEN-MINUS character (-) and replace the character that followed it by the
+ // same character converted to ASCII uppercase.
+ let length = name_after_starting_data.length_in_code_units();
+ let mut builder = Utf16StringBuilder::new();
+ let mut character_index = 0;
+ while character_index < length {
+ let current_character = name_after_starting_data.code_unit_at(character_index);
+
+ if character_index + 1 < length && current_character == u16::from(b'-') {
+ let next_character = name_after_starting_data.code_unit_at(character_index + 1);
+
+ if is_ascii_lower_alpha(next_character) {
+ builder.append_code_unit(to_ascii_uppercase(next_character));
+
+ // Skip the next character.
+ character_index += 2;
+
+ continue;
+ }
+ }
+
+ builder.append_code_unit(current_character);
+ character_index += 1;
+ }
+
+ Some(builder.to_utf16_string())
+}
+
+fn data_attribute_name_matches_property_name(attribute_name: Utf16View<'_>, property_name: Utf16View<'_>) -> bool {
+ if !is_data_attribute_name(attribute_name) {
+ return false;
+ }
+
+ // This performs the name transformation from getting the DOMStringMap's name-value pairs, but compares each UTF-16
+ // code unit directly instead of constructing the transformed name.
+ let name_after_starting_data = attribute_name.substring_view(5, attribute_name.length_in_code_units() - 5);
+ let length = name_after_starting_data.length_in_code_units();
+ let mut property_name_index = 0;
+
+ let mut character_index = 0;
+ while character_index < length {
+ let mut current_character = name_after_starting_data.code_unit_at(character_index);
+
+ if character_index + 1 < length && current_character == u16::from(b'-') {
+ let next_character = name_after_starting_data.code_unit_at(character_index + 1);
+
+ if is_ascii_lower_alpha(next_character) {
+ current_character = to_ascii_uppercase(next_character);
+ character_index += 1;
+ }
+ }
+
+ if property_name_index >= property_name.length_in_code_units() {
+ return false;
+ }
+ if property_name.code_unit_at(property_name_index) != current_character {
+ return false;
+ }
+ property_name_index += 1;
+ character_index += 1;
+ }
+
+ property_name_index == property_name.length_in_code_units()
+}
+
+/// # Safety
+///
+/// The caller passes the live VM and a live Element.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_dom_string_map_create(vm: &Vm, element: *mut c_void) -> *mut c_void {
+ // SAFETY: The caller passes a live Element.
+ let associated_element = unsafe { CppElement::from_raw(element) };
+ cell_into_abi(allocate(vm, |_| DOMStringMap { associated_element }))
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
index 926f8a8f61a..84a19e85a11 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
@@ -58,6 +58,8 @@ pub(crate) mod canvas_transform;
#[cfg(not(test))]
pub(crate) mod dom_string_list;
#[cfg(not(test))]
+pub(crate) mod dom_string_map;
+#[cfg(not(test))]
pub(crate) mod event_source;
#[cfg(not(test))]
pub(crate) mod exports;
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 7ee3f62e5fe..37ae823a280 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -180,6 +180,8 @@ extern "C" {
FlatPtr web_dom_element_local_name(Element const*);
FlatPtr web_dom_element_get_attribute_ns(Element const*, FlatPtr namespace_, FlatPtr name);
void web_dom_element_set_attribute_value(Element*, FlatPtr name, FlatPtr value);
+void web_dom_element_remove_attribute(Element*, FlatPtr name);
+void web_dom_element_for_each_attribute(Element const*, void* callback, void (*on_attribute)(void*, FlatPtr name, FlatPtr value));
void web_dom_element_attribute(Element*, ReadonlyBytes name, void* value, void (*on_value)(void*, FlatPtr));
}
@@ -7172,6 +7174,18 @@ void web_dom_element_set_attribute_value(Element* element, FlatPtr name, FlatPtr
element->set_attribute_value(Utf16FlyString::from_raw(name), Utf16String::adopt_raw(value));
}
+void web_dom_element_remove_attribute(Element* element, FlatPtr name)
+{
+ element->remove_attribute(Utf16FlyString::from_raw(name));
+}
+
+void web_dom_element_for_each_attribute(Element const* element, void* callback, void (*on_attribute)(void*, FlatPtr name, FlatPtr value))
+{
+ element->for_each_attribute([&](Utf16FlyString const& name, Utf16String const& value) {
+ on_attribute(callback, name.raw_identity(), value.raw_identity());
+ });
+}
+
void web_dom_element_attribute(Element* element, ReadonlyBytes name, void* value, void (*on_value)(void*, FlatPtr))
{
auto attribute = element->attribute(Utf16FlyString::from_utf8(StringView { name }));
diff --git a/Libraries/LibWeb/HTML/DOMStringMap.cpp b/Libraries/LibWeb/HTML/DOMStringMap.cpp
index d3f3e880214..1836046586b 100644
--- a/Libraries/LibWeb/HTML/DOMStringMap.cpp
+++ b/Libraries/LibWeb/HTML/DOMStringMap.cpp
@@ -4,228 +4,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/CharacterTypes.h>
-#include <AK/Utf16StringBuilder.h>
-#include <LibWeb/Bindings/DOMStringMap.h>
-#include <LibWeb/Bindings/Intrinsics.h>
-#include <LibWeb/DOM/Document.h>
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/HTML/DOMStringMap.h>
namespace Web::HTML {
-GC_DEFINE_ALLOCATOR(DOMStringMap);
+// Defined in Bindings/Rust/src/html/dom_string_map.rs.
+extern "C" void* web_dom_string_map_create(JSVM*, DOM::Element*);
GC::Ref<DOMStringMap> DOMStringMap::create(DOM::Element& element)
{
- return GC::Heap::the().allocate<DOMStringMap>(element);
-}
-
-DOMStringMap::DOMStringMap(DOM::Element& element)
- : m_associated_element(element)
-{
-}
-
-DOMStringMap::~DOMStringMap() = default;
-
-void DOMStringMap::visit_edges(GC::Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_associated_element);
-}
-
-static bool is_data_attribute_name(Utf16View name)
-{
- // This implements the attribute-name predicate from getting the DOMStringMap's name-value pairs:
- //
- // 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the
- // string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas, ...
- if (!name.starts_with(u"data-"sv))
- return false;
-
- auto name_after_starting_data = name.substring_view(5);
- for (size_t character_index = 0; character_index < name_after_starting_data.length_in_code_units(); ++character_index) {
- auto character = name_after_starting_data.code_unit_at(character_index);
- if (is_ascii_upper_alpha(character))
- return false;
- }
-
- return true;
-}
-
-static Optional<Utf16FlyString> property_name_from_data_attribute_name(Utf16View name)
-{
- if (!is_data_attribute_name(name))
- return {};
-
- auto name_after_starting_data = name.substring_view(5);
-
- // 2. For each content attribute on the DOMStringMap's associated element whose first five characters are the
- // string "data-" and whose remaining characters (if any) do not include any ASCII upper alphas, in the order
- // that those attributes are listed in the element's attribute list, add a name-value pair to list whose name is
- // the attribute's name with the first five characters removed and whose value is the attribute's value.
- //
- // 3. For each name in list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by an ASCII
- // lower alpha, remove the U+002D HYPHEN-MINUS character (-) and replace the character that followed it by the
- // same character converted to ASCII uppercase.
- Utf16StringBuilder builder;
- for (size_t character_index = 0; character_index < name_after_starting_data.length_in_code_units(); ++character_index) {
- auto current_character = name_after_starting_data.code_unit_at(character_index);
-
- if (character_index + 1 < name_after_starting_data.length_in_code_units() && current_character == '-') {
- auto next_character = name_after_starting_data.code_unit_at(character_index + 1);
-
- if (is_ascii_lower_alpha(next_character)) {
- builder.append_code_unit(to_ascii_uppercase(next_character));
-
- // Skip the next character
- ++character_index;
-
- continue;
- }
- }
-
- builder.append_code_unit(current_character);
- }
-
- return Utf16FlyString::from_utf16(builder.view());
-}
-
-static bool data_attribute_name_matches_property_name(Utf16View attribute_name, Utf16View property_name)
-{
- if (!is_data_attribute_name(attribute_name))
- return false;
-
- // This performs the name transformation from getting the DOMStringMap's name-value pairs, but compares each UTF-16
- // code unit directly instead of constructing the transformed name.
- auto name_after_starting_data = attribute_name.substring_view(5);
- size_t property_name_index = 0;
-
- for (size_t character_index = 0; character_index < name_after_starting_data.length_in_code_units(); ++character_index) {
- auto current_character = name_after_starting_data.code_unit_at(character_index);
-
- if (character_index + 1 < name_after_starting_data.length_in_code_units() && current_character == '-') {
- auto next_character = name_after_starting_data.code_unit_at(character_index + 1);
-
- if (is_ascii_lower_alpha(next_character)) {
- current_character = to_ascii_uppercase(next_character);
- ++character_index;
- }
- }
-
- if (property_name_index >= property_name.length_in_code_units())
- return false;
- if (property_name.code_unit_at(property_name_index) != current_character)
- return false;
- ++property_name_index;
- }
-
- return property_name_index == property_name.length_in_code_units();
-}
-
-// https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-pairs
-Vector<Utf16FlyString> DOMStringMap::supported_property_names() const
-{
- // The supported property names on a DOMStringMap object at any instant are the names of each pair returned from
- // getting the DOMStringMap's name-value pairs at that instant, in the order returned.
- //
- // 1. Let list be an empty list of name-value pairs.
- Vector<Utf16FlyString> names;
-
- m_associated_element->for_each_attribute([&](Utf16FlyString const& name, auto const&) {
- auto property_name = property_name_from_data_attribute_name(name.view());
- if (property_name.has_value())
- names.append(property_name.release_value());
- });
-
- // 4. Return list.
- return names;
-}
-
-// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-nameditem
-Utf16String DOMStringMap::determine_value_of_named_property(Utf16View name) const
-{
- // To determine the value of a named property name for a DOMStringMap, return the value component of the name-value pair whose name component is name in the list returned from getting the
- // DOMStringMap's name-value pairs.
- Optional<Utf16String> optional_value;
- m_associated_element->for_each_attribute([&](Utf16FlyString const& attribute_name, Utf16View value) {
- if (optional_value.has_value())
- return;
- if (data_attribute_name_matches_property_name(attribute_name.view(), name))
- optional_value = Utf16String::from_utf16(value);
- });
-
- // NOTE: determine_value_of_named_property is only called if `name` is in supported_property_names.
- VERIFY(optional_value.has_value());
-
- return optional_value.release_value();
-}
-
-// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
-WebIDL::ExceptionOr<void> DOMStringMap::set_value_of_named_property(Utf16FlyString const& name, Utf16String const& value)
-{
- Utf16StringBuilder builder;
-
- // 3. Insert the string data- at the front of name.
- // NOTE: This is done out of order because StringBuilder doesn't have prepend.
- builder.append("data-"_utf16);
-
- auto name_view = name.view();
-
- for (size_t character_index = 0; character_index < name_view.length_in_code_units(); ++character_index) {
- // 1. If name contains a U+002D HYPHEN-MINUS character (-) followed by an ASCII lower alpha, then throw a "SyntaxError" DOMException.
- auto current_character = name_view.code_unit_at(character_index);
-
- if (current_character == '-' && character_index + 1 < name_view.length_in_code_units()) {
- auto next_character = name_view.code_unit_at(character_index + 1);
- if (is_ascii_lower_alpha(next_character))
- return WebIDL::SyntaxError::create("Name cannot contain a '-' followed by a lowercase character."_utf16);
- }
-
- // 2. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
- if (is_ascii_upper_alpha(current_character)) {
- builder.append_ascii('-');
- builder.append_code_unit(to_ascii_lowercase(current_character));
- continue;
- }
-
- builder.append_code_unit(current_character);
- }
-
- // 4. If name is not a valid attribute local name, then throw an "InvalidCharacterError" DOMException.
- if (!DOM::is_valid_attribute_local_name(builder.view()))
- return WebIDL::InvalidCharacterError::create("Name is not a valid attribute local name."_utf16);
-
- // 5. Set an attribute value for the DOMStringMap's associated element using name and value.
- auto data_name = Utf16FlyString::from_utf16(builder.view());
- m_associated_element->set_attribute_value(data_name, value);
-
- return {};
-}
-
-// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-removeitem
-void DOMStringMap::delete_named_property(Utf16FlyString const& name)
-{
- Utf16StringBuilder builder;
-
- // 2. Insert the string data- at the front of name.
- // NOTE: This is done out of order because StringBuilder doesn't have prepend.
- builder.append("data-"_utf16);
-
- for (auto character : name.view()) {
- // 1. For each ASCII upper alpha in name, insert a U+002D HYPHEN-MINUS character (-) before the character and replace the character with the same character converted to ASCII lowercase.
- if (is_ascii_upper_alpha(character)) {
- builder.append_ascii('-');
- builder.append_code_unit(to_ascii_lowercase(character));
- continue;
- }
-
- builder.append_code_point(character);
- }
-
- // Remove an attribute by name given name and the DOMStringMap's associated element.
- auto data_name = Utf16FlyString::from_utf16(builder.view());
- m_associated_element->remove_attribute(data_name);
+ return GC::Ref { *static_cast<DOMStringMap*>(web_dom_string_map_create(JS::HostABI::vm_to_abi(JS::VM::the()), &element)) };
}
}
diff --git a/Libraries/LibWeb/HTML/DOMStringMap.h b/Libraries/LibWeb/HTML/DOMStringMap.h
index 43dac98179a..27bb46857bb 100644
--- a/Libraries/LibWeb/HTML/DOMStringMap.h
+++ b/Libraries/LibWeb/HTML/DOMStringMap.h
@@ -7,37 +7,18 @@
#pragma once
-#include <AK/Utf16View.h>
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/DOMStringMap.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/Forward.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/dom.html#domstringmap
-class DOMStringMap final : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(DOMStringMap, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(DOMStringMap);
+class DOMStringMap final : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(DOMStringMap, dom_string_map)
public:
[[nodiscard]] static GC::Ref<DOMStringMap> create(DOM::Element&);
-
- virtual ~DOMStringMap() override;
-
- Utf16String determine_value_of_named_property(Utf16View) const;
-
- WebIDL::ExceptionOr<void> set_value_of_named_property(Utf16FlyString const&, Utf16String const&);
- void delete_named_property(Utf16FlyString const&);
-
-private:
- explicit DOMStringMap(DOM::Element&);
-
- virtual void visit_edges(GC::Cell::Visitor&) override;
-
- virtual Vector<Utf16FlyString> supported_property_names() const override;
-
- // https://html.spec.whatwg.org/multipage/dom.html#concept-domstringmap-element
- GC::Ref<DOM::Element> m_associated_element;
};
}
diff --git a/Libraries/LibWeb/HTML/DOMStringMap.idl b/Libraries/LibWeb/HTML/DOMStringMap.idl
index d6dbeeab7c5..1534a9e7e65 100644
--- a/Libraries/LibWeb/HTML/DOMStringMap.idl
+++ b/Libraries/LibWeb/HTML/DOMStringMap.idl
@@ -1,5 +1,5 @@
// https://html.spec.whatwg.org/multipage/dom.html#domstringmap
-[Exposed=Window, LegacyOverrideBuiltIns]
+[Exposed=Window, LegacyOverrideBuiltIns, ImplementedIn=Rust]
interface DOMStringMap {
getter DOMString (DOMString name);
diff --git a/Meta/Generators/libweb_bindings/named_and_indexed_properties.py b/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
index 6549e499b6d..a63e4c5fa72 100644
--- a/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
+++ b/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
@@ -190,7 +190,6 @@ def named_property_getter_call(interface: Interface) -> Optional[str]:
return None
method_name_by_interface = {
- "DOMStringMap": "determine_value_of_named_property",
"Storage": "get_item",
}
method_name = method_name_by_interface.get(interface.name, idl_implementation_cpp_name(operation))
@@ -201,8 +200,6 @@ def named_property_getter_call(interface: Interface) -> Optional[str]:
def named_property_getter_value_mode(interface: Interface) -> str:
- if interface.name in ("DOMStringMap",):
- return "value"
if interface.name in ("Storage",):
return "optional_undefined"
if interface.name in ("MimeTypeArray", "Plugin", "PluginArray"):
@@ -532,8 +529,6 @@ def write_named_item_value_implementation(
value_mode = named_property_getter_value_mode(interface)
conversion_type = operation.return_type.clone_with_nullable(False)
value_setup = {
- "value": """ auto named_property_value = {call};
-""",
"optional_undefined": """ auto named_property_value = {call};
if (!named_property_value.has_value())
return JS::js_undefined();