mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
Expose CSSImageValue in Window and Worker realms using native interface brands and illegal constructors. Reify Stylo URL images from parse(), declarations and computed styles, and permit typed cross-property writes only when the target accepts a native image value. Cover intrinsic allocation, cross-realm receivers, forged and proxy objects, conversion bypass, atomic list writes, retained snapshots and Worker exposure. Gradient/image-set projections keep their existing opaque value path. Validation: cargo fmt --all; strict workspace/all-targets/all-features clippy; cargo nextest run --no-fail-fast (20,607 passed, 16 skipped; one existing child-document decoding test passed on its CI-configured second attempt).
492 lines
16 KiB
Rust
492 lines
16 KiB
Rust
use crate::interfaces as web_api_interfaces;
|
|
use cssparser::{Parser, Token};
|
|
use moli_v8_util::{
|
|
get_private_object, get_private_value, set_private_value, throw_type_error, v8_string,
|
|
};
|
|
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
|
|
use moli_webidl as webidl;
|
|
|
|
const CSS_STYLE_VALUE_TEXT_SLOT: &str = "__moliCssStyleValueText";
|
|
const CSS_STYLE_VALUE_PROPERTY_SLOT: &str = "__moliCssStyleValueProperty";
|
|
const CSS_KEYWORD_VALUE_VALUE_SLOT: &str = "__moliCssKeywordValueValue";
|
|
const CSS_UNIT_VALUE_VALUE_SLOT: &str = "__moliCssUnitValueValue";
|
|
const CSS_UNIT_VALUE_UNIT_SLOT: &str = "__moliCssUnitValueUnit";
|
|
|
|
pub mod factories;
|
|
use factories::VALID_UNIT_NAMES;
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::CSSStyleValue)]
|
|
struct CssStyleValueObjectDeclaration {
|
|
#[webapi(slot = CSS_STYLE_VALUE_TEXT_SLOT)]
|
|
text: String,
|
|
}
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::CSSKeywordValue)]
|
|
pub struct CssKeywordValueObjectDeclaration {
|
|
#[webapi(slot = CSS_KEYWORD_VALUE_VALUE_SLOT)]
|
|
value: String,
|
|
}
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::CSSUnitValue)]
|
|
pub struct CssUnitValueObjectDeclaration {
|
|
#[webapi(slot = CSS_UNIT_VALUE_VALUE_SLOT)]
|
|
value: f64,
|
|
#[webapi(slot = CSS_UNIT_VALUE_UNIT_SLOT)]
|
|
unit: String,
|
|
}
|
|
|
|
const STYLE_PROPERTY_MAP_STYLE_SLOT: &str = "__moliStylePropertyMapStyle";
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::StylePropertyMapReadOnly)]
|
|
struct ComputedStylePropertyMapDeclaration<'s> {
|
|
#[webapi(slot = STYLE_PROPERTY_MAP_STYLE_SLOT)]
|
|
style: v8::Local<'s, v8::Object>,
|
|
}
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::StylePropertyMap)]
|
|
struct StylePropertyMapDeclaration<'s> {
|
|
#[webapi(slot = STYLE_PROPERTY_MAP_STYLE_SLOT)]
|
|
style: v8::Local<'s, v8::Object>,
|
|
}
|
|
|
|
#[derive(WebApiFunctionTemplate)]
|
|
#[webapi(interface = web_api_interfaces::CSSKeywordValue, enumerable)]
|
|
struct CssKeywordValuePrototypeDeclaration {
|
|
#[webapi(
|
|
accessor_property,
|
|
getter = css_keyword_value_value_getter_callback,
|
|
setter = css_keyword_value_value_setter_callback
|
|
)]
|
|
value: (),
|
|
}
|
|
|
|
#[derive(WebApiFunctionTemplate)]
|
|
#[webapi(interface = web_api_interfaces::CSSStyleValue, enumerable)]
|
|
struct CssStyleValuePrototypeDeclaration {
|
|
#[webapi(method = "toString", callback = css_style_value_to_string_callback, length = 0)]
|
|
to_string: (),
|
|
}
|
|
|
|
#[derive(WebApiFunctionTemplate)]
|
|
#[webapi(interface = web_api_interfaces::CSSUnitValue, enumerable)]
|
|
struct CssUnitValuePrototypeDeclaration {
|
|
#[webapi(
|
|
accessor_property,
|
|
getter = css_unit_value_value_getter_callback,
|
|
setter = css_unit_value_value_setter_callback
|
|
)]
|
|
value: (),
|
|
#[webapi(accessor_property, getter = css_unit_value_unit_getter_callback)]
|
|
unit: (),
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "CSSUnitValue")]
|
|
struct CssUnitValueConstructorArgs {
|
|
#[webidl(required, converter = "double")]
|
|
value: f64,
|
|
#[webidl(required)]
|
|
unit: String,
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "CSSKeywordValue")]
|
|
struct CssKeywordValueConstructorArgs {
|
|
#[webidl(required)]
|
|
keyword: String,
|
|
}
|
|
|
|
pub fn new_style_property_map<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
style: v8::Local<'s, v8::Object>,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
StylePropertyMapDeclaration::new(style)
|
|
.bind(scope)
|
|
.expect("native StylePropertyMap should bind")
|
|
}
|
|
|
|
pub fn new_style_property_map_read_only<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
style: v8::Local<'s, v8::Object>,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
ComputedStylePropertyMapDeclaration::new(style)
|
|
.bind(scope)
|
|
.expect("computed StylePropertyMap declaration should bind")
|
|
}
|
|
|
|
pub fn style_property_map_style<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
map: v8::Local<'s, v8::Object>,
|
|
) -> Option<v8::Local<'s, v8::Object>> {
|
|
get_private_object(scope, map, STYLE_PROPERTY_MAP_STYLE_SLOT)
|
|
}
|
|
|
|
pub fn install_typed_value_template_bindings<'s>(
|
|
scope: &mut v8::PinScope<'s, '_, ()>,
|
|
template: v8::Local<'s, v8::FunctionTemplate>,
|
|
interface_name: &str,
|
|
) {
|
|
let prototype = template.prototype_template(scope);
|
|
match interface_name {
|
|
"CSSStyleValue" => {
|
|
CssStyleValuePrototypeDeclaration::initialize_prototype_template(scope, prototype);
|
|
}
|
|
"CSSKeywordValue" => {
|
|
CssKeywordValuePrototypeDeclaration::initialize_prototype_template(scope, prototype);
|
|
}
|
|
"CSSUnitValue" => {
|
|
CssUnitValuePrototypeDeclaration::initialize_prototype_template(scope, prototype);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
pub fn css_keyword_value_constructor_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
if !args.is_construct_call() {
|
|
throw_type_error(
|
|
scope,
|
|
"Failed to construct 'CSSKeywordValue': Please use the 'new' operator.",
|
|
);
|
|
return;
|
|
}
|
|
let Some(parsed) = webidl::parse_args::<CssKeywordValueConstructorArgs>(scope, &args) else {
|
|
return;
|
|
};
|
|
if parsed.keyword.is_empty() {
|
|
throw_type_error(scope, "CSSKeywordValue does not support empty strings");
|
|
return;
|
|
}
|
|
CssKeywordValueObjectDeclaration::new(parsed.keyword)
|
|
.initialize(scope, args.this())
|
|
.expect("CSSKeywordValue declaration should initialize");
|
|
rv.set(args.this().into());
|
|
}
|
|
|
|
pub fn css_unit_value_constructor_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
if !args.is_construct_call() {
|
|
throw_type_error(
|
|
scope,
|
|
"Failed to construct 'CSSUnitValue': Please use the 'new' operator.",
|
|
);
|
|
return;
|
|
}
|
|
let Some(parsed) = webidl::parse_args::<CssUnitValueConstructorArgs>(scope, &args) else {
|
|
return;
|
|
};
|
|
let Some(unit) = normalize_unit_name(&parsed.unit) else {
|
|
throw_type_error(scope, &format!("Invalid unit: {}", parsed.unit));
|
|
return;
|
|
};
|
|
CssUnitValueObjectDeclaration::new(parsed.value, unit)
|
|
.initialize(scope, args.this())
|
|
.expect("CSSUnitValue declaration should initialize");
|
|
rv.set(args.this().into());
|
|
}
|
|
|
|
pub fn style_value_from_text<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
text: &str,
|
|
allow_keyword: bool,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
if let Some((value, unit)) = parse_single_unit_value(text) {
|
|
return CssUnitValueObjectDeclaration::new(value, unit)
|
|
.bind(scope)
|
|
.expect("CSSUnitValue declaration should bind");
|
|
}
|
|
if allow_keyword && let Some(keyword) = parse_single_keyword_value(text) {
|
|
return CssKeywordValueObjectDeclaration::new(keyword)
|
|
.bind(scope)
|
|
.expect("CSSKeywordValue declaration should bind");
|
|
}
|
|
CssStyleValueObjectDeclaration::new(text.to_owned())
|
|
.bind(scope)
|
|
.expect("CSSStyleValue declaration should bind")
|
|
}
|
|
|
|
pub fn unit_value<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
value: f64,
|
|
unit: String,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
CssUnitValueObjectDeclaration::new(value, unit)
|
|
.bind(scope)
|
|
.expect("CSSUnitValue declaration should bind")
|
|
}
|
|
|
|
pub fn opaque_style_value<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
property: &str,
|
|
text: &str,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
let object = CssStyleValueObjectDeclaration::new(text.to_owned())
|
|
.bind(scope)
|
|
.expect("CSSStyleValue declaration should bind");
|
|
let property = v8_string(scope, property).expect("CSS property name");
|
|
set_private_value(
|
|
scope,
|
|
object,
|
|
CSS_STYLE_VALUE_PROPERTY_SLOT,
|
|
property.into(),
|
|
);
|
|
object
|
|
}
|
|
|
|
fn parse_single_keyword_value(text: &str) -> Option<String> {
|
|
let input = text;
|
|
let mut parser = Parser::new(input);
|
|
parser
|
|
.parse_entirely(|input| match input.next()?.clone() {
|
|
Token::Ident(keyword) => Ok(keyword.to_string()),
|
|
_ => Err(cssparser::ParseError::<()>::custom(())),
|
|
})
|
|
.ok()
|
|
}
|
|
|
|
fn parse_single_unit_value(text: &str) -> Option<(f64, String)> {
|
|
let input = text;
|
|
let mut parser = Parser::new(input);
|
|
parser
|
|
.parse_entirely(|input| {
|
|
let token = input.next()?.clone();
|
|
match token {
|
|
Token::Number { value, .. } => Ok((f64::from(value), "number".to_owned())),
|
|
Token::Percentage { unit_value, .. } => {
|
|
Ok((f64::from(unit_value) * 100.0, "percent".to_owned()))
|
|
}
|
|
Token::Dimension { value, unit, .. } => {
|
|
let unit = unit.to_ascii_lowercase();
|
|
if normalize_unit_name(&unit).is_none() {
|
|
return Err(cssparser::ParseError::<()>::custom(()));
|
|
}
|
|
Ok((f64::from(value), unit))
|
|
}
|
|
_ => Err(cssparser::ParseError::<()>::custom(())),
|
|
}
|
|
})
|
|
.ok()
|
|
}
|
|
|
|
pub fn normalize_unit_name(unit: &str) -> Option<String> {
|
|
let unit = unit.to_ascii_lowercase();
|
|
(VALID_UNIT_NAMES.contains(&unit.as_str()) || unit == "x").then_some(unit)
|
|
}
|
|
|
|
pub fn css_style_value_to_string_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
if let Some(unit) = css_unit_value_unit(scope, args.this()) {
|
|
let value = css_unit_value_number(scope, args.this()).unwrap_or(0.0);
|
|
let number = v8::Number::new(scope, value)
|
|
.to_string(scope)
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
|
.unwrap_or_else(|| value.to_string());
|
|
let text = match unit.as_str() {
|
|
"number" => number,
|
|
"percent" => format!("{number}%"),
|
|
_ => format!("{number}{unit}"),
|
|
};
|
|
if let Some(text) = v8_string(scope, &text) {
|
|
rv.set(text.into());
|
|
}
|
|
return;
|
|
}
|
|
if let Some(keyword) = css_keyword_value(scope, args.this()) {
|
|
if let Some(keyword) = v8_string(scope, &keyword) {
|
|
rv.set(keyword.into());
|
|
}
|
|
return;
|
|
}
|
|
let text = get_private_value(scope, args.this(), CSS_STYLE_VALUE_TEXT_SLOT)
|
|
.and_then(|value| value.to_string(scope))
|
|
.unwrap_or_else(|| v8::String::empty(scope));
|
|
rv.set(text.into());
|
|
}
|
|
|
|
fn css_keyword_value_value_getter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
let Some(keyword) = css_keyword_value(scope, args.this()) else {
|
|
throw_type_error(scope, "Illegal invocation");
|
|
return;
|
|
};
|
|
if let Some(keyword) = v8_string(scope, &keyword) {
|
|
rv.set(keyword.into());
|
|
}
|
|
}
|
|
|
|
fn css_keyword_value_value_setter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
if css_keyword_value(scope, args.this()).is_none() {
|
|
throw_type_error(scope, "Illegal invocation");
|
|
return;
|
|
}
|
|
let keyword = match webidl::convert::<webidl::DomString>(
|
|
scope,
|
|
args.get(0),
|
|
webidl::Context::member("CSSKeywordValue", "value"),
|
|
) {
|
|
Ok(keyword) => keyword.0,
|
|
Err(error) => {
|
|
webidl::throw_error(scope, &error);
|
|
return;
|
|
}
|
|
};
|
|
if keyword.is_empty() {
|
|
throw_type_error(scope, "CSSKeywordValue does not support empty strings");
|
|
return;
|
|
}
|
|
if let Some(keyword) = v8_string(scope, &keyword) {
|
|
set_private_value(
|
|
scope,
|
|
args.this(),
|
|
CSS_KEYWORD_VALUE_VALUE_SLOT,
|
|
keyword.into(),
|
|
);
|
|
}
|
|
rv.set_undefined();
|
|
}
|
|
|
|
fn css_unit_value_value_getter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
let Some(value) = css_unit_value_number(scope, args.this()) else {
|
|
throw_type_error(scope, "Illegal invocation");
|
|
return;
|
|
};
|
|
rv.set(v8::Number::new(scope, value).into());
|
|
}
|
|
|
|
fn css_unit_value_value_setter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
if css_unit_value_unit(scope, args.this()).is_none() {
|
|
throw_type_error(scope, "Illegal invocation");
|
|
return;
|
|
}
|
|
let value = match webidl::convert::<webidl::Double>(
|
|
scope,
|
|
args.get(0),
|
|
webidl::Context::member("CSSUnitValue", "value"),
|
|
) {
|
|
Ok(value) => f64::from(value),
|
|
Err(error) => {
|
|
webidl::throw_error(scope, &error);
|
|
return;
|
|
}
|
|
};
|
|
set_private_value(
|
|
scope,
|
|
args.this(),
|
|
CSS_UNIT_VALUE_VALUE_SLOT,
|
|
v8::Number::new(scope, value).into(),
|
|
);
|
|
rv.set_undefined();
|
|
}
|
|
|
|
fn css_unit_value_unit_getter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
let Some(unit) = css_unit_value_unit(scope, args.this()) else {
|
|
throw_type_error(scope, "Illegal invocation");
|
|
return;
|
|
};
|
|
if let Some(unit) = v8_string(scope, &unit) {
|
|
rv.set(unit.into());
|
|
}
|
|
}
|
|
|
|
pub fn css_unit_value_number<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
receiver: v8::Local<'s, v8::Object>,
|
|
) -> Option<f64> {
|
|
get_private_value(scope, receiver, CSS_UNIT_VALUE_VALUE_SLOT)
|
|
.and_then(|value| value.number_value(scope))
|
|
}
|
|
|
|
pub fn css_keyword_value<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
receiver: v8::Local<'s, v8::Object>,
|
|
) -> Option<String> {
|
|
get_private_value(scope, receiver, CSS_KEYWORD_VALUE_VALUE_SLOT)
|
|
.and_then(|value| value.to_string(scope))
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
|
}
|
|
|
|
pub fn css_unit_value_unit<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
receiver: v8::Local<'s, v8::Object>,
|
|
) -> Option<String> {
|
|
get_private_value(scope, receiver, CSS_UNIT_VALUE_UNIT_SLOT)
|
|
.and_then(|value| value.to_string(scope))
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
|
}
|
|
|
|
pub fn associated_property<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
value: v8::Local<'s, v8::Object>,
|
|
) -> Option<String> {
|
|
get_private_value(scope, value, CSS_STYLE_VALUE_PROPERTY_SLOT)
|
|
.and_then(|value| v8::Local::<v8::String>::try_from(value).ok())
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
|
}
|
|
|
|
pub fn style_value_text<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
value: v8::Local<'s, v8::Object>,
|
|
) -> Option<String> {
|
|
get_private_value(scope, value, CSS_STYLE_VALUE_TEXT_SLOT)
|
|
.and_then(|value| v8::Local::<v8::String>::try_from(value).ok())
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
|
}
|
|
|
|
pub fn keyword_value<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
value: String,
|
|
) -> v8::Local<'s, v8::Object> {
|
|
CssKeywordValueObjectDeclaration::new(value)
|
|
.bind(scope)
|
|
.expect("CSSKeywordValue should bind")
|
|
}
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::CSSImageValue)]
|
|
struct CssImageValueObjectDeclaration {
|
|
#[webapi(slot = CSS_STYLE_VALUE_TEXT_SLOT)]
|
|
text: String,
|
|
}
|
|
|
|
pub fn image_value<'s>(scope: &mut v8::PinScope<'s, '_>, text: &str) -> v8::Local<'s, v8::Object> {
|
|
// Images have no author-visible fields or constructor. Their immutable
|
|
// native serialization is reusable across properties accepting images,
|
|
// unlike property-associated opaque CSSStyleValues.
|
|
CssImageValueObjectDeclaration::new(text.to_owned())
|
|
.bind(scope)
|
|
.expect("CSSImageValue declaration should bind")
|
|
}
|