Skip to content

Truly independent web browser

Meta+LibWeb: Convert [LegacyNullToEmptyString] in the Rust bindings

Andreas Klingf894596f8e0c
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index c2c21d41829..1ae99ddd009 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -103,6 +103,16 @@ pub(crate) fn to_dom_string(vm: &Vm, value: Value) -> ThrowCompletionOr<Utf16Str
value.to_utf16_string(vm)
}
+/// A JS value converted to a DOMString with [LegacyNullToEmptyString], https://webidl.spec.whatwg.org/#es-DOMString
+pub(crate) fn to_dom_string_treating_null_as_empty(vm: &Vm, value: Value) -> ThrowCompletionOr<Utf16String> {
+ // 1. If V is null and the conversion is to an IDL type associated with the [LegacyNullToEmptyString] extended
+ // attribute, then return the DOMString value that represents the empty string.
+ if value.is_null() {
+ return Ok(Utf16String::default());
+ }
+ to_dom_string(vm, value)
+}
+
/// A JS value converted to a USVString, https://webidl.spec.whatwg.org/#es-USVString
pub(crate) fn to_usv_string(vm: &Vm, value: Value) -> ThrowCompletionOr<UsvString> {
// 1. Let string be the result of converting V to a DOMString.
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 4c9b8170461..275616316a0 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -305,7 +305,12 @@ def write_parameter_conversions(
parameter_what = f"the parameter {parameter.name} of {what}"
if parameter.variadic:
raise UnsupportedByRustBindings(f"{parameter_what} is variadic")
- unsupported = set(parameter.extended_attributes) - {"AllowShared", "AllowResizable", "EnforceRange"}
+ unsupported = set(parameter.extended_attributes) - {
+ "AllowShared",
+ "AllowResizable",
+ "EnforceRange",
+ "LegacyNullToEmptyString",
+ }
if unsupported:
raise UnsupportedByRustBindings(f"{parameter_what} has the extended attributes {sorted(unsupported)}")
name = rust_local_name(parameter.name)
@@ -400,10 +405,10 @@ def write_rust_interface(
use crate::bindings::conversions::{{
ArrayBufferView, BufferSource, BufferSourceRules, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,
implements_cpp_interface, record_to_js, rooted_sequence_to_js, sequence_from_js, sequence_to_js,
- to_array_buffer_view, to_boolean, to_buffer_source, to_cpp_interface, to_dom_string, to_double, to_float,
- to_long, to_long_enforcing_range, to_long_long, to_long_long_enforcing_range, to_typed_array,
- to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_enforcing_range,
- to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
+ to_array_buffer_view, to_boolean, to_buffer_source, to_cpp_interface, to_dom_string,
+ to_dom_string_treating_null_as_empty, to_double, to_float, to_long, to_long_enforcing_range, to_long_long,
+ to_long_long_enforcing_range, to_typed_array, to_unrestricted_double, to_unrestricted_float, to_unsigned_long,
+ to_unsigned_long_enforcing_range, to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
}};
use crate::bindings::interface::{{
Inherits, Interface, Ref, Wrapped, cell_class, construct_arguments, construct_wrapper, define_attribute,
@@ -931,9 +936,10 @@ pub(crate) mod definitions {
dictionary_member, dictionary_object, enumeration_value_to_js, implements_cpp_interface, iterator_method,
record_to_js, rooted_sequence_to_js, sequence_from_iterable, sequence_from_js, sequence_to_js,
throw_no_union_types_matched, to_array_buffer_view, to_boolean, to_buffer_source, to_cpp_interface,
- to_dom_string, to_double, to_enumeration_value, to_float, to_long, to_long_enforcing_range, to_long_long,
- to_long_long_enforcing_range, to_typed_array, to_unrestricted_double, to_unrestricted_float, to_unsigned_long,
- to_unsigned_long_enforcing_range, to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
+ to_dom_string, to_dom_string_treating_null_as_empty, to_double, to_enumeration_value, to_float, to_long,
+ to_long_enforcing_range, to_long_long, to_long_long_enforcing_range, to_typed_array, to_unrestricted_double,
+ to_unrestricted_float, to_unsigned_long, to_unsigned_long_enforcing_range, to_unsigned_long_long,
+ to_unsigned_long_long_enforcing_range, to_usv_string,
};
use crate::bindings::interface::{
Interface, Ref, create_dictionary_object, implementation_of, set_dictionary_member, to_interface,
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index a441be57ff1..6f7f1e4678a 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -72,6 +72,10 @@ def rust_to_idl_value(
raise UnsupportedByRustBindings(
f"{what} has the type {idl_type} with [EnforceRange], which the Rust bindings only handle on integer types"
)
+ if "LegacyNullToEmptyString" in attributes and (idl_type.nullable or name != "DOMString"):
+ raise UnsupportedByRustBindings(
+ f"{what} has the type {idl_type} with [LegacyNullToEmptyString], which applies to DOMString only"
+ )
# https://webidl.spec.whatwg.org/#js-nullable-type
if idl_type.nullable:
inner_type = idl_type.without_nullable()
@@ -90,6 +94,8 @@ def rust_to_idl_value(
f"{what} has the type {idl_type}, which the Rust bindings do not convert to yet"
)
if name == "DOMString":
+ if "LegacyNullToEmptyString" in attributes:
+ return f"to_dom_string_treating_null_as_empty(vm, {value})"
return f"to_dom_string(vm, {value})"
if name == "USVString":
return f"to_usv_string(vm, {value})"
diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.py
index 3b63d730fb0..4acbe9b3a31 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -41,7 +41,7 @@ TYPED_ARRAY_NAMES = {
}
# The extended attributes of a type or a parameter that the Rust bindings handle.
-SUPPORTED_TYPE_EXTENDED_ATTRIBUTES = {"AllowShared", "AllowResizable", "EnforceRange"}
+SUPPORTED_TYPE_EXTENDED_ATTRIBUTES = {"AllowShared", "AllowResizable", "EnforceRange", "LegacyNullToEmptyString"}
# The integer types, with the Rust type of their values.
INTEGER_TYPES = {"long": "i32", "unsigned long": "u32", "long long": "i64", "unsigned long long": "u64"}