Truly independent web browser
LibWeb: Implement three CSS descriptor interfaces in Rust
Andreas Kling62d54bc8527f
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_descriptors.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_descriptors.rsnew file mode 100644index 00000000000..519a7eae346--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_descriptors.rs@@ -0,0 +1,117 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! What the interfaces of the descriptors of an at-rule share. Each is a CSSStyleDeclaration, which LibWeb implements+//! in C++, with an attribute for each descriptor.++use core::ffi::c_void;+use core::ptr::NonNull;++use ak::Utf16String;+use libjs_rust::api::Trace;++use crate::bindings::cpp_cell::CppCell;+use crate::bindings::interface::{Interface, Ref, cell_into_abi};+use crate::bindings::{Exception, WebIdlResult};++unsafe extern "C" {+ fn web_css_descriptors_set_owner(descriptors: *mut c_void, interface_name: u16, owner: *mut c_void);+ fn web_css_descriptors_get(descriptors: *mut c_void, name: *const u8, name_length: usize) -> usize;+ fn web_css_descriptors_set(descriptors: *mut c_void, name: *const u8, name_length: usize, value: usize) -> bool;+}++/// The Web::CSS::RustCSSDescriptors that an object of a descriptors interface owns.+#[derive(Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct CppDescriptors(CppCell);++impl CppDescriptors {+ pub(crate) fn new(cell: CppCell) -> Self {+ Self(cell)+ }++ pub(crate) fn as_cpp_parent(&self) -> Option<NonNull<c_void>> {+ NonNull::new(self.0.as_ptr())+ }++ pub(crate) fn set_owner<T: Interface>(&self, owner: Ref<T>) {+ // SAFETY: The cells are live, and the owner keeps the descriptors alive.+ unsafe { web_css_descriptors_set_owner(self.0.as_ptr(), T::INTERFACE_NAME, cell_into_abi(owner)) };+ }++ pub(crate) fn get(&self, name: &str) -> Utf16String {+ // SAFETY: The cell is live, the name is lent for the call, and LibWeb hands over the string.+ unsafe { Utf16String::from_raw_owned(web_css_descriptors_get(self.0.as_ptr(), name.as_ptr(), name.len())) }+ }++ pub(crate) fn set(&self, name: &str, value: Utf16String) -> WebIdlResult<()> {+ // SAFETY: The cell is live, the name is lent for the call, and the value is handed over.+ if unsafe { web_css_descriptors_set(self.0.as_ptr(), name.as_ptr(), name.len(), value.into_raw()) } {+ return Ok(());+ }+ Err(Exception::Dom(+ "NoModificationAllowedError",+ Utf16String::from_utf8("Cannot modify properties of readonly CSSStyleDeclaration"),+ ))+ }+}++/// An interface of the descriptors of an at-rule, with the entry point through which LibWeb creates its objects, and+/// the getter and the setter of each descriptor.+macro_rules! descriptors_interface {+ ($interface:ident, $create:ident, $(($getter:ident, $setter:ident, $name:literal)),+ $(,)?) => {+ #[derive(libjs_rust::api::Trace)]+ #[gc(crate = libjs_rust)]+ pub(crate) struct $interface {+ descriptors: $crate::css::css_descriptors::CppDescriptors,+ }++ impl $crate::bindings::interface::Wrappable for $interface {+ fn cpp_parent(&self) -> Option<core::ptr::NonNull<core::ffi::c_void>> {+ self.descriptors.as_cpp_parent()+ }+ }++ impl $interface {+ $(+ pub(crate) fn $getter(+ &self,+ _: &mut $crate::bindings::CallContext,+ ) -> $crate::bindings::WebIdlResult<ak::Utf16String> {+ Ok(self.descriptors.get($name))+ }++ pub(crate) fn $setter(+ &self,+ _: &mut $crate::bindings::CallContext,+ value: ak::Utf16String,+ ) -> $crate::bindings::WebIdlResult<()> {+ self.descriptors.set($name, value)+ }+ )++ }++ /// A new object that owns the descriptors, which LibWeb created.+ ///+ /// # Safety+ ///+ /// `vm` must be the live VM, and `descriptors` the live cell of a Web::CSS::RustCSSDescriptors.+ #[unsafe(no_mangle)]+ pub unsafe extern "C" fn $create(vm: *mut core::ffi::c_void, descriptors: *mut core::ffi::c_void) {+ assert!(!vm.is_null(), "C++ passes the VM");+ // SAFETY: The caller passes the live VM and a live cell.+ let (vm, descriptors) = unsafe {+ (&*vm.cast::<libjs_rust::api::Vm>(), $crate::bindings::cpp_cell::CppCell::from_raw(descriptors))+ };+ let object = $crate::bindings::interface::allocate(vm, |_| $interface {+ descriptors: $crate::css::css_descriptors::CppDescriptors::new(descriptors),+ });+ object.descriptors.set_owner(object);+ }+ };+}++pub(crate) use descriptors_interface;diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_font_face_descriptors.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_font_face_descriptors.rsnew file mode 100644index 00000000000..c3e5017a043--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_font_face_descriptors.rs@@ -0,0 +1,40 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! CSSFontFaceDescriptors, https://drafts.csswg.org/css-fonts-4/#cssfontfacedescriptors++use super::css_descriptors::descriptors_interface;++descriptors_interface!(+ CSSFontFaceDescriptors,+ web_css_font_face_descriptors_create,+ (src, set_src, "src"),+ (font_family, set_font_family, "font-family"),+ (font_style, set_font_style, "font-style"),+ (font_weight, set_font_weight, "font-weight"),+ (font_width, set_font_width, "font-width"),+ (unicode_range, set_unicode_range, "unicode-range"),+ (+ font_feature_settings,+ set_font_feature_settings,+ "font-feature-settings"+ ),+ (+ font_variation_settings,+ set_font_variation_settings,+ "font-variation-settings"+ ),+ (font_named_instance, set_font_named_instance, "font-named-instance"),+ (font_display, set_font_display, "font-display"),+ (+ font_language_override,+ set_font_language_override,+ "font-language-override"+ ),+ (ascent_override, set_ascent_override, "ascent-override"),+ (descent_override, set_descent_override, "descent-override"),+ (line_gap_override, set_line_gap_override, "line-gap-override"),+);diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_function_descriptors.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_function_descriptors.rsnew file mode 100644index 00000000000..67b2e062de7--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_function_descriptors.rs@@ -0,0 +1,15 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! CSSFunctionDescriptors, https://drafts.csswg.org/css-mixins-1/#cssfunctiondescriptors++use super::css_descriptors::descriptors_interface;++descriptors_interface!(+ CSSFunctionDescriptors,+ web_css_function_descriptors_create,+ (result, set_result, "result"),+);diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_page_descriptors.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_page_descriptors.rsnew file mode 100644index 00000000000..a239edb0e1d--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_page_descriptors.rs@@ -0,0 +1,23 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! CSSPageDescriptors, https://drafts.csswg.org/cssom/#csspagedescriptors++use super::css_descriptors::descriptors_interface;++descriptors_interface!(+ CSSPageDescriptors,+ web_css_page_descriptors_create,+ (margin, set_margin, "margin"),+ (margin_top, set_margin_top, "margin-top"),+ (margin_right, set_margin_right, "margin-right"),+ (margin_bottom, set_margin_bottom, "margin-bottom"),+ (margin_left, set_margin_left, "margin-left"),+ (size, set_size, "size"),+ (page_orientation, set_page_orientation, "page-orientation"),+ (marks, set_marks, "marks"),+ (bleed, set_bleed, "bleed"),+);diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rsindex 6aa0f520b85..4d30802fd25 100644--- a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs@@ -10,9 +10,12 @@ pub(crate) mod animation_event;pub(crate) mod css_condition_rule;pub(crate) mod css_container_rule;pub(crate) mod css_counter_style_rule;+pub(crate) mod css_descriptors;+pub(crate) mod css_font_face_descriptors;pub(crate) mod css_font_face_rule;pub(crate) mod css_font_feature_values_rule;pub(crate) mod css_function_declarations;+pub(crate) mod css_function_descriptors;pub(crate) mod css_function_rule;pub(crate) mod css_grouping_rule;pub(crate) mod css_image_value;@@ -37,6 +40,7 @@ pub(crate) mod css_namespace_rule;pub(crate) mod css_nested_declarations;pub(crate) mod css_numeric_array;pub(crate) mod css_numeric_value;+pub(crate) mod css_page_descriptors;pub(crate) mod css_page_rule;pub(crate) mod css_perspective;pub(crate) mod css_position_try_rule;diff --git a/Libraries/LibWeb/Bindings/RustCSSDescriptors.cpp b/Libraries/LibWeb/Bindings/RustCSSDescriptors.cppnew file mode 100644index 00000000000..0a621020c32--- /dev/null+++ b/Libraries/LibWeb/Bindings/RustCSSDescriptors.cpp@@ -0,0 +1,95 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibJS/HostClassBuilder.h>+#include <LibJS/Runtime/Realm.h>+#include <LibWeb/Bindings/RustCSSDescriptors.h>+#include <LibWeb/CSS/CSSFontFaceRule.h>+#include <LibWeb/WebIDL/ExceptionOr.h>++namespace Web::CSS {++GC_DEFINE_ALLOCATOR(RustCSSDescriptors);++GC::Ref<RustCSSDescriptors> RustCSSDescriptors::create(AtRuleID at_rule, RustDescriptorBlock descriptors)+{+ return GC::Heap::the().allocate<RustCSSDescriptors>(at_rule, move(descriptors));+}++RustCSSDescriptors::RustCSSDescriptors(AtRuleID at_rule, RustDescriptorBlock descriptors)+ : CSSDescriptors(at_rule, move(descriptors))+ , m_at_rule(at_rule)+{+}++void RustCSSDescriptors::set_owner(Bindings::InterfaceName interface_name, Bindings::RustInterfaceCell& owner)+{+ VERIFY(!m_owner);+ m_owner_interface_name = interface_name;+ m_owner = owner;+}++WebIDL::ExceptionOr<void> RustCSSDescriptors::set_property(Utf16FlyString const& property, Utf16View value, Utf16View priority)+{+ TRY(Base::set_property(property, value, priority));++ if (m_at_rule == AtRuleID::FontFace) {+ if (auto* font_face_rule = as_if<CSSFontFaceRule>(parent_rule().ptr()))+ font_face_rule->handle_descriptor_change(property);+ }++ return {};+}++extern "C" JSObject* web_rust_bindings_wrap(JSVM*, void* realm, u16 interface_name, void* cell);++GC::Ref<JS::HostObject> RustCSSDescriptors::create_wrapper(JS::Realm& realm)+{+ VERIFY(m_owner);+ auto* wrapper = web_rust_bindings_wrap(JS::HostABI::vm_to_abi(realm.vm()), &realm, to_underlying(m_owner_interface_name), m_owner.ptr());+ return as<JS::HostObject>(*JS::HostABI::object_from_abi(wrapper));+}++void RustCSSDescriptors::visit_edges(GC::Cell::Visitor& visitor)+{+ Base::visit_edges(visitor);+ visitor.visit(m_owner);+}++// Declared on the Rust side in Libraries/LibWeb/Bindings/Rust/src/css/css_descriptors.rs.+extern "C" {+void web_css_descriptors_set_owner(void* descriptors, u16 interface_name, void* owner);+FlatPtr web_css_descriptors_get(void* descriptors, u8 const* name, size_t name_length);+bool web_css_descriptors_set(void* descriptors, u8 const* name, size_t name_length, FlatPtr value);+}++static RustCSSDescriptors& descriptors_from_cell(void* cell)+{+ return *static_cast<RustCSSDescriptors*>(static_cast<GC::Cell*>(cell));+}++static Utf16FlyString descriptor_name(u8 const* name, size_t name_length)+{+ return Utf16FlyString::from_utf8_without_validation(StringView { name, name_length });+}++void web_css_descriptors_set_owner(void* descriptors, u16 interface_name, void* owner)+{+ descriptors_from_cell(descriptors).set_owner(static_cast<Bindings::InterfaceName>(interface_name), *static_cast<Bindings::RustInterfaceCell*>(owner));+}++FlatPtr web_css_descriptors_get(void* descriptors, u8 const* name, size_t name_length)+{+ return descriptors_from_cell(descriptors).get_property_value(descriptor_name(name, name_length)).into_raw();+}++bool web_css_descriptors_set(void* descriptors, u8 const* name, size_t name_length, FlatPtr value)+{+ auto text = Utf16String::from_raw(value);+ return !descriptors_from_cell(descriptors).set_property(descriptor_name(name, name_length), text.utf16_view(), u""sv).is_error();+}++}diff --git a/Libraries/LibWeb/Bindings/RustCSSDescriptors.h b/Libraries/LibWeb/Bindings/RustCSSDescriptors.hnew file mode 100644index 00000000000..8f84e876b55--- /dev/null+++ b/Libraries/LibWeb/Bindings/RustCSSDescriptors.h@@ -0,0 +1,39 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <LibWeb/Bindings/RustInterface.h>+#include <LibWeb/CSS/CSSDescriptors.h>++namespace Web::CSS {++// The descriptors that an object of an interface implemented in Rust owns, whose wrappers are the object's+// (Libraries/LibWeb/Bindings/Rust/src/css/css_descriptors.rs).+class RustCSSDescriptors final : public CSSDescriptors {+ WEB_NON_IDL_WRAPPABLE(RustCSSDescriptors, CSSDescriptors);+ GC_DECLARE_ALLOCATOR(RustCSSDescriptors);++public:+ [[nodiscard]] static GC::Ref<RustCSSDescriptors> create(AtRuleID, RustDescriptorBlock);++ void set_owner(Bindings::InterfaceName, Bindings::RustInterfaceCell&);++ virtual WebIDL::ExceptionOr<void> set_property(Utf16FlyString const& property, Utf16View value, Utf16View priority) override;++private:+ RustCSSDescriptors(AtRuleID, RustDescriptorBlock);++ virtual bool is_owned_by_rust_interface_object() const override { return true; }+ virtual GC::Ref<JS::HostObject> create_wrapper(JS::Realm&) override;+ virtual void visit_edges(GC::Cell::Visitor&) override;++ AtRuleID m_at_rule;+ Bindings::InterfaceName m_owner_interface_name { Bindings::InterfaceName::Unknown };+ GC::Ptr<Bindings::RustInterfaceCell> m_owner;+};++}diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex 5033dc03561..06c69840e49 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -29,6 +29,7 @@ set(SOURCESBindings/OptionConstructor.cppBindings/PlatformObject.cppBindings/RustBindingsServices.cpp+ Bindings/RustCSSDescriptors.cppBindings/RustEventTarget.cppBindings/StylePropertyMapReadOnlyIterator.cppBindings/PrincipalHostDefined.cpp@@ -81,16 +82,13 @@ set(SOURCESCSS/CSS.cppCSS/CSSAnimation.cppCSS/CSSDescriptors.cpp- CSS/CSSFontFaceDescriptors.cppCSS/CSSFontFaceRule.cppCSS/CSSFontFeatureValuesMap.cppCSS/CSSFontFeatureValuesRule.cpp- CSS/CSSFunctionDescriptors.cppCSS/CSSGroupingRule.cppCSS/CSSImportRule.cppCSS/CSSKeyframesRule.cppCSS/CSSNestedDeclarations.cpp- CSS/CSSPageDescriptors.cppCSS/CSSPositionTryDescriptors.cppCSS/CSSRule.cppCSS/CSSRuleList.cppdiff --git a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.cpp b/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.cppdeleted file mode 100644index a7108ea20c6..00000000000--- a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.cpp+++ /dev/null@@ -1,183 +0,0 @@-/*- * Copyright (c) 2025, Sam Atkins <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <LibGC/Heap.h>-#include <LibWeb/CSS/CSSFontFaceDescriptors.h>-#include <LibWeb/CSS/CSSFontFaceRule.h>-#include <LibWeb/WebIDL/ExceptionOr.h>--namespace Web::CSS {--GC_DEFINE_ALLOCATOR(CSSFontFaceDescriptors);--GC::Ref<CSSFontFaceDescriptors> CSSFontFaceDescriptors::create(RustDescriptorBlock descriptors)-{- return GC::Heap::the().allocate<CSSFontFaceDescriptors>(move(descriptors));-}--CSSFontFaceDescriptors::CSSFontFaceDescriptors(RustDescriptorBlock descriptors)- : CSSDescriptors(AtRuleID::FontFace, move(descriptors))-{-}--CSSFontFaceDescriptors::~CSSFontFaceDescriptors() = default;--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_property(Utf16FlyString const& property, Utf16View value, Utf16View priority)-{- TRY(Base::set_property(property, value, priority));-- if (auto* font_face_rule = as_if<CSSFontFaceRule>(parent_rule().ptr()))- font_face_rule->handle_descriptor_change(property);-- return {};-}--static Utf16String get_property_value_as_utf16(CSSFontFaceDescriptors const& descriptors, Utf16FlyString const& property)-{- return descriptors.get_property_value(property);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_ascent_override(Utf16View value)-{- return set_property("ascent-override"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::ascent_override() const-{- return get_property_value_as_utf16(*this, "ascent-override"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_descent_override(Utf16View value)-{- return set_property("descent-override"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::descent_override() const-{- return get_property_value_as_utf16(*this, "descent-override"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_display(Utf16View value)-{- return set_property("font-display"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_display() const-{- return get_property_value_as_utf16(*this, "font-display"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_family(Utf16View value)-{- return set_property("font-family"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_family() const-{- return get_property_value_as_utf16(*this, "font-family"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_feature_settings(Utf16View value)-{- return set_property("font-feature-settings"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_feature_settings() const-{- return get_property_value_as_utf16(*this, "font-feature-settings"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_language_override(Utf16View value)-{- return set_property("font-language-override"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_language_override() const-{- return get_property_value_as_utf16(*this, "font-language-override"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_named_instance(Utf16View value)-{- return set_property("font-named-instance"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_named_instance() const-{- return get_property_value_as_utf16(*this, "font-named-instance"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_style(Utf16View value)-{- return set_property("font-style"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_style() const-{- return get_property_value_as_utf16(*this, "font-style"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_variation_settings(Utf16View value)-{- return set_property("font-variation-settings"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_variation_settings() const-{- return get_property_value_as_utf16(*this, "font-variation-settings"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_weight(Utf16View value)-{- return set_property("font-weight"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_weight() const-{- return get_property_value_as_utf16(*this, "font-weight"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_font_width(Utf16View value)-{- return set_property("font-width"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::font_width() const-{- return get_property_value_as_utf16(*this, "font-width"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_line_gap_override(Utf16View value)-{- return set_property("line-gap-override"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::line_gap_override() const-{- return get_property_value_as_utf16(*this, "line-gap-override"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_src(Utf16View value)-{- return set_property("src"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::src() const-{- return get_property_value_as_utf16(*this, "src"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSFontFaceDescriptors::set_unicode_range(Utf16View value)-{- return set_property("unicode-range"_utf16_fly_string, value, u""sv);-}--Utf16String CSSFontFaceDescriptors::unicode_range() const-{- return get_property_value_as_utf16(*this, "unicode-range"_utf16_fly_string);-}--}diff --git a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.h b/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.hdeleted file mode 100644index 7ef5b8d3118..00000000000--- a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.h+++ /dev/null@@ -1,74 +0,0 @@-/*- * Copyright (c) 2025, Sam Atkins <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Utf16String.h>-#include <AK/Utf16View.h>-#include <LibWeb/CSS/CSSDescriptors.h>--namespace Web::CSS {--// https://drafts.csswg.org/css-fonts-4/#cssfontfacedescriptors-class CSSFontFaceDescriptors final- : public CSSDescriptors {- WEB_WRAPPABLE(CSSFontFaceDescriptors, CSSDescriptors);- GC_DECLARE_ALLOCATOR(CSSFontFaceDescriptors);--public:- [[nodiscard]] static GC::Ref<CSSFontFaceDescriptors> create(RustDescriptorBlock);-- virtual ~CSSFontFaceDescriptors() override;-- virtual WebIDL::ExceptionOr<void> set_property(Utf16FlyString const& property, Utf16View value, Utf16View priority) override;-- WebIDL::ExceptionOr<void> set_ascent_override(Utf16View value);- Utf16String ascent_override() const;-- WebIDL::ExceptionOr<void> set_descent_override(Utf16View value);- Utf16String descent_override() const;-- WebIDL::ExceptionOr<void> set_font_display(Utf16View value);- Utf16String font_display() const;-- WebIDL::ExceptionOr<void> set_font_family(Utf16View value);- Utf16String font_family() const;-- WebIDL::ExceptionOr<void> set_font_feature_settings(Utf16View value);- Utf16String font_feature_settings() const;-- WebIDL::ExceptionOr<void> set_font_language_override(Utf16View value);- Utf16String font_language_override() const;-- WebIDL::ExceptionOr<void> set_font_named_instance(Utf16View value);- Utf16String font_named_instance() const;-- WebIDL::ExceptionOr<void> set_font_style(Utf16View value);- Utf16String font_style() const;-- WebIDL::ExceptionOr<void> set_font_variation_settings(Utf16View value);- Utf16String font_variation_settings() const;-- WebIDL::ExceptionOr<void> set_font_weight(Utf16View value);- Utf16String font_weight() const;-- WebIDL::ExceptionOr<void> set_font_width(Utf16View value);- Utf16String font_width() const;-- WebIDL::ExceptionOr<void> set_line_gap_override(Utf16View value);- Utf16String line_gap_override() const;-- WebIDL::ExceptionOr<void> set_src(Utf16View value);- Utf16String src() const;-- WebIDL::ExceptionOr<void> set_unicode_range(Utf16View value);- Utf16String unicode_range() const;--private:- explicit CSSFontFaceDescriptors(RustDescriptorBlock);-};--}diff --git a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.idl b/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.idlindex e110cfd2984..f11e6676e8a 100644--- a/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.idl+++ b/Libraries/LibWeb/CSS/CSSFontFaceDescriptors.idl@@ -1,5 +1,5 @@// https://drafts.csswg.org/css-fonts-4/#cssfontfacedescriptors-[Exposed=Window]+[Exposed=Window, ImplementedIn=Rust]interface CSSFontFaceDescriptors : CSSStyleDeclaration {[LegacyNullToEmptyString] attribute CSSOMString src;[LegacyNullToEmptyString, AttributeCallbackName=font_family_regular, ImplementedAs=font_family] attribute CSSOMString fontFamily;diff --git a/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp b/Libraries/LibWeb/CSS/CSSFontFaceRule.cppindex 5cbc0defdd2..273d0a1606b 100644--- a/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp+++ b/Libraries/LibWeb/CSS/CSSFontFaceRule.cpp@@ -17,9 +17,9 @@namespace Web::CSS {-GC::Ref<CSSFontFaceDescriptors> CSSFontFaceRule::descriptors() const+GC::Ref<CSSDescriptors> CSSFontFaceRule::descriptors() const{- return static_cast<CSSFontFaceDescriptors&>(*declarations());+ return static_cast<CSSDescriptors&>(*declarations());}bool CSSFontFaceRule::is_valid() constdiff --git a/Libraries/LibWeb/CSS/CSSFontFaceRule.h b/Libraries/LibWeb/CSS/CSSFontFaceRule.hindex 50be3b4436d..de2f0190f74 100644--- a/Libraries/LibWeb/CSS/CSSFontFaceRule.h+++ b/Libraries/LibWeb/CSS/CSSFontFaceRule.h@@ -8,7 +8,7 @@#pragma once#include <LibWeb/Bindings/CSSFontFaceRule.h>-#include <LibWeb/CSS/CSSFontFaceDescriptors.h>+#include <LibWeb/CSS/CSSDescriptors.h>#include <LibWeb/CSS/CSSRule.h>namespace Web::CSS {@@ -20,7 +20,7 @@ class CSSFontFaceRule final : public CSSRule {public:bool is_valid() const;- GC::Ref<CSSFontFaceDescriptors> descriptors() const;+ GC::Ref<CSSDescriptors> descriptors() const;RefPtr<FontFaceState> css_connected_font_face() const;void handle_descriptor_change(Utf16FlyString const& property);diff --git a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.cpp b/Libraries/LibWeb/CSS/CSSFunctionDescriptors.cppdeleted file mode 100644index 890216e7e9e..00000000000--- a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.cpp+++ /dev/null@@ -1,31 +0,0 @@-/*- * Copyright (c) 2026, Callum Law <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include "CSSFunctionDescriptors.h"-#include <LibGC/Heap.h>--namespace Web::CSS {--GC_DEFINE_ALLOCATOR(CSSFunctionDescriptors);--GC::Ref<CSSFunctionDescriptors> CSSFunctionDescriptors::create(RustDescriptorBlock descriptors)-{- return GC::Heap::the().allocate<CSSFunctionDescriptors>(move(descriptors));-}--// https://drafts.csswg.org/css-mixins-1/#dom-cssfunctiondescriptors-result-Utf16String CSSFunctionDescriptors::result() const-{- return get_property_value("result"_utf16_fly_string);-}--// https://drafts.csswg.org/css-mixins-1/#dom-cssfunctiondescriptors-result-WebIDL::ExceptionOr<void> CSSFunctionDescriptors::set_result(Utf16View value)-{- return set_property("result"_utf16_fly_string, value, u""sv);-}--}diff --git a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.h b/Libraries/LibWeb/CSS/CSSFunctionDescriptors.hdeleted file mode 100644index eb25af03f1e..00000000000--- a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.h+++ /dev/null@@ -1,35 +0,0 @@-/*- * Copyright (c) 2026, Callum Law <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Utf16String.h>-#include <AK/Utf16View.h>-#include <LibWeb/CSS/CSSDescriptors.h>--namespace Web::CSS {--// https://drafts.csswg.org/css-mixins-1/#cssfunctiondescriptors-class CSSFunctionDescriptors final : public CSSDescriptors {- WEB_WRAPPABLE(CSSFunctionDescriptors, CSSDescriptors);- GC_DECLARE_ALLOCATOR(CSSFunctionDescriptors);--public:- [[nodiscard]] static GC::Ref<CSSFunctionDescriptors> create(RustDescriptorBlock);-- virtual ~CSSFunctionDescriptors() override = default;-- Utf16String result() const;- WebIDL::ExceptionOr<void> set_result(Utf16View value);--private:- explicit CSSFunctionDescriptors(RustDescriptorBlock descriptors)- : CSSDescriptors(AtRuleID::Function, move(descriptors))- {- }-};--}diff --git a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.idl b/Libraries/LibWeb/CSS/CSSFunctionDescriptors.idlindex 57d5e9deb08..3c599bbb52b 100644--- a/Libraries/LibWeb/CSS/CSSFunctionDescriptors.idl+++ b/Libraries/LibWeb/CSS/CSSFunctionDescriptors.idl@@ -1,5 +1,5 @@// https://drafts.csswg.org/css-mixins-1/#cssfunctiondescriptors-[Exposed=Window]+[Exposed=Window, ImplementedIn=Rust]interface CSSFunctionDescriptors : CSSStyleDeclaration {[LegacyNullToEmptyString] attribute CSSOMString result;};diff --git a/Libraries/LibWeb/CSS/CSSPageDescriptors.cpp b/Libraries/LibWeb/CSS/CSSPageDescriptors.cppdeleted file mode 100644index dbeda1d6ad8..00000000000--- a/Libraries/LibWeb/CSS/CSSPageDescriptors.cpp+++ /dev/null@@ -1,122 +0,0 @@-/*- * Copyright (c) 2025, Sam Atkins <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <LibGC/Heap.h>-#include <LibWeb/CSS/CSSPageDescriptors.h>-#include <LibWeb/WebIDL/ExceptionOr.h>--namespace Web::CSS {--GC_DEFINE_ALLOCATOR(CSSPageDescriptors);--GC::Ref<CSSPageDescriptors> CSSPageDescriptors::create(RustDescriptorBlock descriptors)-{- return GC::Heap::the().allocate<CSSPageDescriptors>(move(descriptors));-}--CSSPageDescriptors::CSSPageDescriptors(RustDescriptorBlock descriptors)- : CSSDescriptors(AtRuleID::Page, move(descriptors))-{-}--CSSPageDescriptors::~CSSPageDescriptors() = default;--static Utf16String get_property_value_as_utf16(CSSPageDescriptors const& descriptors, Utf16FlyString const& property)-{- return descriptors.get_property_value(property);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_margin(Utf16View value)-{- return set_property("margin"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::margin() const-{- return get_property_value_as_utf16(*this, "margin"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_margin_top(Utf16View value)-{- return set_property("margin-top"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::margin_top() const-{- return get_property_value_as_utf16(*this, "margin-top"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_margin_right(Utf16View value)-{- return set_property("margin-right"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::margin_right() const-{- return get_property_value_as_utf16(*this, "margin-right"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_margin_bottom(Utf16View value)-{- return set_property("margin-bottom"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::margin_bottom() const-{- return get_property_value_as_utf16(*this, "margin-bottom"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_margin_left(Utf16View value)-{- return set_property("margin-left"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::margin_left() const-{- return get_property_value_as_utf16(*this, "margin-left"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_size(Utf16View value)-{- return set_property("size"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::size() const-{- return get_property_value_as_utf16(*this, "size"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_page_orientation(Utf16View value)-{- return set_property("page-orientation"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::page_orientation() const-{- return get_property_value_as_utf16(*this, "page-orientation"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_marks(Utf16View value)-{- return set_property("marks"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::marks() const-{- return get_property_value_as_utf16(*this, "marks"_utf16_fly_string);-}--WebIDL::ExceptionOr<void> CSSPageDescriptors::set_bleed(Utf16View value)-{- return set_property("bleed"_utf16_fly_string, value, u""sv);-}--Utf16String CSSPageDescriptors::bleed() const-{- return get_property_value_as_utf16(*this, "bleed"_utf16_fly_string);-}--}diff --git a/Libraries/LibWeb/CSS/CSSPageDescriptors.h b/Libraries/LibWeb/CSS/CSSPageDescriptors.hdeleted file mode 100644index 061506ef12e..00000000000--- a/Libraries/LibWeb/CSS/CSSPageDescriptors.h+++ /dev/null@@ -1,56 +0,0 @@-/*- * Copyright (c) 2025, Sam Atkins <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/Utf16String.h>-#include <AK/Utf16View.h>-#include <LibWeb/CSS/CSSDescriptors.h>--namespace Web::CSS {--// https://drafts.csswg.org/cssom/#csspagedescriptors-class CSSPageDescriptors final : public CSSDescriptors {- WEB_WRAPPABLE(CSSPageDescriptors, CSSDescriptors);- GC_DECLARE_ALLOCATOR(CSSPageDescriptors);--public:- [[nodiscard]] static GC::Ref<CSSPageDescriptors> create(RustDescriptorBlock);-- virtual ~CSSPageDescriptors() override;-- WebIDL::ExceptionOr<void> set_margin(Utf16View value);- Utf16String margin() const;-- WebIDL::ExceptionOr<void> set_margin_top(Utf16View value);- Utf16String margin_top() const;-- WebIDL::ExceptionOr<void> set_margin_right(Utf16View value);- Utf16String margin_right() const;-- WebIDL::ExceptionOr<void> set_margin_bottom(Utf16View value);- Utf16String margin_bottom() const;-- WebIDL::ExceptionOr<void> set_margin_left(Utf16View value);- Utf16String margin_left() const;-- WebIDL::ExceptionOr<void> set_size(Utf16View value);- Utf16String size() const;-- WebIDL::ExceptionOr<void> set_page_orientation(Utf16View value);- Utf16String page_orientation() const;-- WebIDL::ExceptionOr<void> set_marks(Utf16View value);- Utf16String marks() const;-- WebIDL::ExceptionOr<void> set_bleed(Utf16View value);- Utf16String bleed() const;--private:- explicit CSSPageDescriptors(RustDescriptorBlock);-};--}diff --git a/Libraries/LibWeb/CSS/CSSPageDescriptors.idl b/Libraries/LibWeb/CSS/CSSPageDescriptors.idlindex 302793bba0c..1d1e5513e85 100644--- a/Libraries/LibWeb/CSS/CSSPageDescriptors.idl+++ b/Libraries/LibWeb/CSS/CSSPageDescriptors.idl@@ -1,5 +1,5 @@// https://drafts.csswg.org/cssom/#csspagedescriptors-[Exposed=Window]+[Exposed=Window, ImplementedIn=Rust]interface CSSPageDescriptors : CSSStyleDeclaration {[LegacyNullToEmptyString] attribute CSSOMString margin;[LegacyNullToEmptyString, AttributeCallbackName=margin_top_regular, ImplementedAs=margin_top] attribute CSSOMString marginTop;diff --git a/Libraries/LibWeb/CSS/CSSRule.cpp b/Libraries/LibWeb/CSS/CSSRule.cppindex defccd41c77..b8664ec53a4 100644--- a/Libraries/LibWeb/CSS/CSSRule.cpp+++ b/Libraries/LibWeb/CSS/CSSRule.cpp@@ -8,13 +8,11 @@#include <LibJS/HostClassBuilder.h>#include <LibJS/Runtime/VM.h>+#include <LibWeb/Bindings/RustCSSDescriptors.h>#include <LibWeb/Bindings/Wrappable.h>#include <LibWeb/Bindings/WrapperWorld.h>-#include <LibWeb/CSS/CSSFontFaceDescriptors.h>#include <LibWeb/CSS/CSSFontFeatureValuesMap.h>#include <LibWeb/CSS/CSSFontFeatureValuesRule.h>-#include <LibWeb/CSS/CSSFunctionDescriptors.h>-#include <LibWeb/CSS/CSSPageDescriptors.h>#include <LibWeb/CSS/CSSPositionTryDescriptors.h>#include <LibWeb/CSS/CSSRule.h>#include <LibWeb/CSS/CSSStyleProperties.h>@@ -40,6 +38,9 @@ void* web_css_rule_parent_rule(void* rule);void* web_css_rule_parent_style_sheet(void* rule);FlatPtr web_css_rule_serialized(void* vm, void* rule);void* web_css_rule_declarations(void* vm, void* rule);+void web_css_font_face_descriptors_create(void* vm, void* descriptors);+void web_css_page_descriptors_create(void* vm, void* descriptors);+void web_css_function_descriptors_create(void* vm, void* descriptors);}// NB: What the implementation of the rules in Rust asks of the C++ code of LibWeb.@@ -104,6 +105,13 @@ GC::Ref<CSSStyleDeclaration> CSSRule::declarations() constreturn *static_cast<CSSStyleDeclaration*>(static_cast<GC::Cell*>(declarations));}+static GC::Ref<CSSStyleDeclaration> create_rust_descriptors(AtRuleID at_rule, Parser::ValueParserFFI::FfiDescriptorBlock const* block, void (*create_owner)(void* vm, void* descriptors))+{+ auto descriptors = RustCSSDescriptors::create(at_rule, RustDescriptorBlock { Parser::ValueParserFFI::rust_descriptor_block_retain(block) });+ create_owner(JS::HostABI::vm_to_abi(JS::VM::the()), static_cast<GC::Cell*>(descriptors.ptr()));+ return descriptors;+}+void* web_css_rule_create_declarations(void* cell){using namespace Parser::ValueParserFFI;@@ -124,11 +132,11 @@ void* web_css_rule_create_declarations(void* cell)case CSSRule::Type::PositionTry:return CSSPositionTryDescriptors::create(RustDeclarationBlock { rust_declaration_block_retain(payload.declarations) });case CSSRule::Type::FontFace:- return CSSFontFaceDescriptors::create(RustDescriptorBlock { rust_descriptor_block_retain(payload.descriptors) });+ return create_rust_descriptors(AtRuleID::FontFace, payload.descriptors, web_css_font_face_descriptors_create);case CSSRule::Type::Page:- return CSSPageDescriptors::create(RustDescriptorBlock { rust_descriptor_block_retain(payload.descriptors) });+ return create_rust_descriptors(AtRuleID::Page, payload.descriptors, web_css_page_descriptors_create);case CSSRule::Type::FunctionDeclarations:- return CSSFunctionDescriptors::create(RustDescriptorBlock { rust_descriptor_block_retain(payload.descriptors) });+ return create_rust_descriptors(AtRuleID::Function, payload.descriptors, web_css_function_descriptors_create);default:VERIFY_NOT_REACHED();}diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex d8721ef5e24..5707a791da4 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -257,11 +257,9 @@ class CounterDefinitionsStyleValue;class CounterStyleStyleValue;class CSSAnimation;class CSSDescriptors;-class CSSFontFaceDescriptors;class CSSFontFaceRule;class CSSFontFeatureValuesMap;class CSSFontFeatureValuesRule;-class CSSFunctionDescriptors;class CSSFunctionRule;class CSSGroupingRule;class CSSImportRule;@@ -269,7 +267,6 @@ class CSSKeyframesRule;class CSSKeywordValue;class CSSNestedDeclarations;class CSSNumericValue;-class CSSPageDescriptors;class CSSPositionTryDescriptors;class CSSRule;class CSSRuleList;diff --git a/Tests/LibWeb/Text/expected/css/descriptors-interfaces.txt b/Tests/LibWeb/Text/expected/css/descriptors-interfaces.txtnew file mode 100644index 00000000000..8800a45c28a--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/descriptors-interfaces.txt@@ -0,0 +1,10 @@+font face: true true+same object: true+length: 3+indexed: font-family src font-display undefined+in: true false+spellings: swap swap+after dashed set: block+after null: "" 2+page: true 1px 1px+parent rule: truediff --git a/Tests/LibWeb/Text/input/css/descriptors-interfaces.html b/Tests/LibWeb/Text/input/css/descriptors-interfaces.htmlnew file mode 100644index 00000000000..86e1b1b3d60--- /dev/null+++ b/Tests/LibWeb/Text/input/css/descriptors-interfaces.html@@ -0,0 +1,27 @@+<!doctype html>+<style>+ @font-face { font-family: "Test"; src: url(test.woff); font-display: swap; }+ @page { margin-top: 1px; }+</style>+<script src="../include.js"></script>+<script>+ test(() => {+ const rules = document.styleSheets[0].cssRules;+ const fontFace = rules[0].style;+ println(`font face: ${fontFace instanceof CSSFontFaceDescriptors} ${fontFace instanceof CSSStyleDeclaration}`);+ println(`same object: ${fontFace === rules[0].style}`);+ println(`length: ${fontFace.length}`);+ println(`indexed: ${fontFace[0]} ${fontFace[1]} ${fontFace[2]} ${fontFace[3]}`);+ println(`in: ${0 in fontFace} ${3 in fontFace}`);+ println(`spellings: ${fontFace.fontDisplay} ${fontFace["font-display"]}`);++ fontFace["font-display"] = "block";+ println(`after dashed set: ${fontFace.fontDisplay}`);+ fontFace.fontDisplay = null;+ println(`after null: "${fontFace.fontDisplay}" ${fontFace.length}`);++ const page = rules[1].style;+ println(`page: ${page instanceof CSSPageDescriptors} ${page.marginTop} ${page["margin-top"]}`);+ println(`parent rule: ${page.parentRule === rules[1]}`);+ });+</script>