script: Implement CSS.registerProperty (#38682)

The implementation is mostly equivalent to
https://searchfox.org/mozilla-central/source/servo/ports/geckolib/glue.rs#9480.

Testing: New web platform tests start to pass

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-08-14 15:34:02 +02:00 committed by GitHub
parent 3a0e8fefde
commit 43da933247
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 185 additions and 46 deletions

View file

@ -4,6 +4,8 @@
use cssparser::{Parser, ParserInput, serialize_identifier};
use dom_struct::dom_struct;
use layout_api::{PropertyRegistration, RegisterPropertyError};
use script_bindings::codegen::GenericBindings::CSSBinding::PropertyDefinition;
use style::context::QuirksMode;
use style::parser::ParserContext;
use style::stylesheets::supports_rule::{Declaration, parse_condition_or_declaration};
@ -12,7 +14,7 @@ use style_traits::ParsingMode;
use crate::dom::bindings::codegen::Bindings::CSSBinding::CSSMethods;
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::Reflector;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
@ -81,4 +83,32 @@ impl CSSMethods<crate::DomTypeHolder> for CSS {
fn PaintWorklet(win: &Window) -> DomRoot<Worklet> {
win.paint_worklet()
}
/// <https://drafts.css-houdini.org/css-properties-values-api/#the-registerproperty-function>
fn RegisterProperty(window: &Window, property_definition: &PropertyDefinition) -> Fallible<()> {
let property_registration = PropertyRegistration {
name: property_definition.name.str().to_owned(),
inherits: property_definition.inherits,
url_data: UrlExtraData(window.get_url().get_arc()),
initial_value: property_definition
.initialValue
.as_ref()
.map(|value| value.str().to_owned()),
syntax: property_definition.syntax.str().to_owned(),
};
window
.layout_mut()
.register_custom_property(property_registration)
.map_err(|error| match error {
RegisterPropertyError::InvalidName |
RegisterPropertyError::InvalidSyntax |
RegisterPropertyError::InvalidInitialValue |
RegisterPropertyError::NoInitialValue |
RegisterPropertyError::InitialValueNotComputationallyIndependent => Error::Syntax,
RegisterPropertyError::AlreadyRegistered => Error::InvalidModification,
})?;
Ok(())
}
}