script: Pass &mut JSContext to more attribute setters (#44494)

Part of #42812

Testing: it compiles

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
This commit is contained in:
Tim van der Lippe
2026-04-25 14:51:02 +02:00
committed by GitHub
parent 902d5d10d9
commit 6f43bba0f4
7 changed files with 97 additions and 51 deletions

View File

@@ -41,7 +41,7 @@ impl HTMLBodyElement {
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
cx: &mut JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
@@ -73,11 +73,14 @@ impl HTMLBodyElementMethods<crate::DomTypeHolder> for HTMLBodyElement {
make_getter!(Background, "background");
/// <https://html.spec.whatwg.org/multipage/#dom-body-background>
fn SetBackground(&self, input: DOMString, can_gc: CanGc) {
fn SetBackground(&self, cx: &mut JSContext, input: DOMString) {
let value =
AttrValue::from_resolved_url(&self.owner_document().base_url().get_arc(), input.into());
self.upcast::<Element>()
.set_attribute(&local_name!("background"), value, can_gc);
self.upcast::<Element>().set_attribute(
&local_name!("background"),
value,
CanGc::from_cx(cx),
);
}
// https://html.spec.whatwg.org/multipage/#windoweventhandlers
@@ -146,12 +149,7 @@ impl VirtualMethods for HTMLBodyElement {
}
}
fn attribute_mutated(
&self,
cx: &mut js::context::JSContext,
attr: &Attr,
mutation: AttributeMutation,
) {
fn attribute_mutated(&self, cx: &mut JSContext, attr: &Attr, mutation: AttributeMutation) {
let do_super_mutate = match (attr.local_name(), mutation) {
(name, AttributeMutation::Set(..)) if name.starts_with("on") => {
let document = self.owner_document();

View File

@@ -5,6 +5,7 @@
use cssparser::match_ignore_ascii_case;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name, ns};
use js::context::JSContext;
use js::rust::HandleObject;
use style::attr::AttrValue;
use style::color::AbsoluteColor;
@@ -43,7 +44,7 @@ impl HTMLFontElement {
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
cx: &mut JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
@@ -100,15 +101,15 @@ impl HTMLFontElementMethods<crate::DomTypeHolder> for HTMLFontElement {
make_getter!(Face, "face");
// https://html.spec.whatwg.org/multipage/#dom-font-face
make_atomic_setter!(SetFace, "face");
make_atomic_setter!(cx, SetFace, "face");
// https://html.spec.whatwg.org/multipage/#dom-font-size
make_getter!(Size, "size");
/// <https://html.spec.whatwg.org/multipage/#dom-font-size>
fn SetSize(&self, value: DOMString, can_gc: CanGc) {
fn SetSize(&self, cx: &mut JSContext, value: DOMString) {
let element = self.upcast::<Element>();
element.set_attribute(&local_name!("size"), parse_size(&value), can_gc);
element.set_attribute(&local_name!("size"), parse_size(&value), CanGc::from_cx(cx));
}
}

View File

@@ -62,7 +62,7 @@ impl HTMLHRElementMethods<crate::DomTypeHolder> for HTMLHRElement {
make_getter!(Align, "align");
// https://html.spec.whatwg.org/multipage/#dom-hr-align
make_atomic_setter!(SetAlign, "align");
make_atomic_setter!(cx, SetAlign, "align");
// https://html.spec.whatwg.org/multipage/#dom-hr-color
make_getter!(Color, "color");
@@ -74,19 +74,19 @@ impl HTMLHRElementMethods<crate::DomTypeHolder> for HTMLHRElement {
make_bool_getter!(NoShade, "noshade");
// https://html.spec.whatwg.org/multipage/#dom-hr-noshade
make_bool_setter!(SetNoShade, "noshade");
make_bool_setter!(cx, SetNoShade, "noshade");
// https://html.spec.whatwg.org/multipage/#dom-hr-size
make_getter!(Size, "size");
// https://html.spec.whatwg.org/multipage/#dom-hr-size
make_dimension_setter!(SetSize, "size");
make_dimension_setter!(cx, SetSize, "size");
// <https://html.spec.whatwg.org/multipage/#dom-hr-width>
make_getter!(Width, "width");
// <https://html.spec.whatwg.org/multipage/#dom-hr-width>
make_dimension_setter!(SetWidth, "width");
make_dimension_setter!(cx, SetWidth, "width");
}
/// The result of applying the presentational hint for the `size` attribute.

View File

@@ -67,7 +67,7 @@ impl HTMLTableColElementMethods<crate::DomTypeHolder> for HTMLTableColElement {
make_getter!(Width, "width");
// <https://html.spec.whatwg.org/multipage/#dom-col-width>
make_dimension_setter!(SetWidth, "width");
make_dimension_setter!(cx, SetWidth, "width");
}
impl<'dom> LayoutDom<'dom, HTMLTableColElement> {

View File

@@ -471,15 +471,15 @@ impl HTMLTableElementMethods<crate::DomTypeHolder> for HTMLTableElement {
make_nonzero_dimension_setter!(SetWidth, "width");
// <https://html.spec.whatwg.org/multipage/#dom-table-align>
make_setter!(SetAlign, "align");
make_setter!(cx, SetAlign, "align");
make_getter!(Align, "align");
// <https://html.spec.whatwg.org/multipage/#dom-table-cellpadding>
make_setter!(SetCellPadding, "cellpadding");
make_setter!(cx, SetCellPadding, "cellpadding");
make_getter!(CellPadding, "cellpadding");
// <https://html.spec.whatwg.org/multipage/#dom-table-cellspacing>
make_setter!(SetCellSpacing, "cellspacing");
make_setter!(cx, SetCellSpacing, "cellspacing");
make_getter!(CellSpacing, "cellspacing");
}

View File

@@ -263,18 +263,39 @@ macro_rules! make_enumerated_getter(
);
);
macro_rules! make_setter_inner(
( $self:ident, $value:ident, $htmlname:tt, $can_gc:expr ) => (
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = $self.upcast::<Element>();
element.set_string_attribute(&html5ever::local_name!($htmlname), $value, $can_gc)
);
);
// concat_idents! doesn't work for function name positions, so
// we have to specify both the content name and the HTML name here
#[macro_export]
macro_rules! make_setter(
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
make_setter_inner!(self, value, $htmlname, CanGc::deprecated_note());
}
);
( $cx:ident, $attr:ident, $htmlname:tt ) => (
fn $attr(&self, $cx: &mut js::context::JSContext, value: DOMString) {
make_setter_inner!(self, value, $htmlname, CanGc::from_cx($cx));
}
);
);
macro_rules! make_bool_setter_inner(
( $self:ident, $value:ident, $htmlname:tt, $can_gc:expr ) => (
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = self.upcast::<Element>();
element.set_string_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
}
let element = $self.upcast::<Element>();
element.set_bool_attribute(&html5ever::local_name!($htmlname), $value, $can_gc)
);
);
@@ -282,11 +303,12 @@ macro_rules! make_setter(
macro_rules! make_bool_setter(
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: bool) {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = self.upcast::<Element>();
element.set_bool_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
make_bool_setter_inner!(self, value, $htmlname, CanGc::deprecated_note());
}
);
( $cx:ident, $attr:ident, $htmlname:tt ) => (
fn $attr(&self, $cx: &mut js::context::JSContext, value: bool) {
make_bool_setter_inner!(self, value, $htmlname, CanGc::from_cx($cx));
}
);
);
@@ -316,7 +338,7 @@ macro_rules! make_uint_setter(
#[macro_export]
macro_rules! make_clamped_uint_setter(
($attr:ident, $htmlname:tt, $min:expr, $max:expr, $default:expr) => (
fn $attr(&self, value: u32) {
fn $attr(&self, cx: &mut js::context::JSContext, value: u32) {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::dom::values::UNSIGNED_LONG_MAX;
@@ -328,7 +350,7 @@ macro_rules! make_clamped_uint_setter(
};
let element = self.upcast::<Element>();
element.set_uint_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
element.set_uint_attribute(&html5ever::local_name!($htmlname), value, CanGc::from_cx(cx))
}
);
);
@@ -385,28 +407,39 @@ macro_rules! make_atomic_setter(
#[macro_export]
macro_rules! make_legacy_color_setter(
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
fn $attr(&self, cx: &mut js::context::JSContext, value: DOMString) {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use style::attr::AttrValue;
use $crate::script_runtime::CanGc;
let element = self.upcast::<Element>();
let value = AttrValue::from_legacy_color(value.into());
element.set_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
element.set_attribute(&html5ever::local_name!($htmlname), value, CanGc::from_cx(cx))
}
);
);
macro_rules! make_dimension_setter_inner(
( $self:ident, $value:ident, $htmlname:tt, $can_gc:expr ) => (
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = $self.upcast::<Element>();
let value = AttrValue::from_dimension($value.into());
element.set_attribute(&html5ever::local_name!($htmlname), value, $can_gc)
);
);
#[macro_export]
macro_rules! make_dimension_setter(
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = self.upcast::<Element>();
let value = AttrValue::from_dimension(value.into());
element.set_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
make_dimension_setter_inner!(self, value, $htmlname, CanGc::deprecated_note());
}
);
( $cx:ident, $attr:ident, $htmlname:tt ) => (
fn $attr(&self, $cx: &mut js::context::JSContext, value: DOMString) {
make_dimension_setter_inner!(self, value, $htmlname, CanGc::from_cx($cx));
}
);
);
@@ -414,13 +447,13 @@ macro_rules! make_dimension_setter(
#[macro_export]
macro_rules! make_nonzero_dimension_setter(
( $attr:ident, $htmlname:tt ) => (
fn $attr(&self, value: DOMString) {
fn $attr(&self, cx: &mut js::context::JSContext, value: DOMString) {
use $crate::dom::bindings::inheritance::Castable;
use $crate::dom::element::Element;
use $crate::script_runtime::CanGc;
let element = self.upcast::<Element>();
let value = AttrValue::from_nonzero_dimension(value.into());
element.set_attribute(&html5ever::local_name!($htmlname), value, CanGc::deprecated_note())
element.set_attribute(&html5ever::local_name!($htmlname), value, CanGc::from_cx(cx))
}
);
);
@@ -522,7 +555,7 @@ macro_rules! define_window_owned_event_handler(
}
}
fn $setter(&self, listener: Option<::std::rc::Rc<$handler>>) {
fn $setter(&self, _cx: &mut js::context::JSContext, listener: Option<::std::rc::Rc<$handler>>) {
let document = self.owner_document();
if document.has_browsing_context() {
document.window().$setter(listener)

View File

@@ -425,7 +425,7 @@ DOMInterfaces = {
},
"HTMLBodyElement": {
"canGc": ["SetBackground"]
'implicitCxSetters': True,
},
'HTMLButtonElement': {
@@ -480,7 +480,7 @@ DOMInterfaces = {
},
'HTMLFontElement': {
'canGc': ['SetSize']
'implicitCxSetters': True,
},
'HTMLFormControlsCollection': {
@@ -492,10 +492,18 @@ DOMInterfaces = {
'cx': ['CheckValidity', 'ReportValidity'],
},
'HTMLFrameSetElement': {
'implicitCxSetters': True,
},
'HTMLIFrameElement': {
'cx': ['Sandbox', 'SetSrcdoc'],
},
'HTMLHRElement': {
'implicitCxSetters': True,
},
'HTMLImageElement': {
'cx': ['Decode', 'Image', 'ReportValidity', 'SetCrossOrigin'],
},
@@ -584,14 +592,20 @@ DOMInterfaces = {
'DeleteTFoot',
'DeleteTHead',
'InsertRow',
'SetCaption',
'SetTFoot',
'SetTHead'
],
'implicitCxSetters': True,
},
'HTMLTableCellElement': {
'implicitCxSetters': True,
},
'HTMLTableColElement': {
'implicitCxSetters': True,
},
'HTMLTableRowElement': {
'cx': ['DeleteCell', 'InsertCell']
'cx': ['DeleteCell', 'InsertCell'],
'implicitCxSetters': True,
},
'HTMLTableSectionElement': {