Propagate CanGc arguments through HTMLCollection constructors (#36180)

Signed-off-by: dericko681 <abonghoderick@gmail.com>
This commit is contained in:
Kunga Derick Abongho 2025-03-29 14:12:14 +01:00 committed by GitHub
parent 83da63f638
commit 40133ce29a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 125 additions and 53 deletions

View file

@ -4979,8 +4979,12 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
if let Some(entry) = self.tag_map.borrow_mut().get(&qualified_name) { if let Some(entry) = self.tag_map.borrow_mut().get(&qualified_name) {
return DomRoot::from_ref(entry); return DomRoot::from_ref(entry);
} }
let result = let result = HTMLCollection::by_qualified_name(
HTMLCollection::by_qualified_name(&self.window, self.upcast(), qualified_name.clone()); &self.window,
self.upcast(),
qualified_name.clone(),
CanGc::note(),
);
self.tag_map self.tag_map
.borrow_mut() .borrow_mut()
.insert(qualified_name, Dom::from_ref(&*result)); .insert(qualified_name, Dom::from_ref(&*result));
@ -4999,7 +5003,12 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
if let Some(collection) = self.tagns_map.borrow().get(&qname) { if let Some(collection) = self.tagns_map.borrow().get(&qname) {
return DomRoot::from_ref(collection); return DomRoot::from_ref(collection);
} }
let result = HTMLCollection::by_qual_tag_name(&self.window, self.upcast(), qname.clone()); let result = HTMLCollection::by_qual_tag_name(
&self.window,
self.upcast(),
qname.clone(),
CanGc::note(),
);
self.tagns_map self.tagns_map
.borrow_mut() .borrow_mut()
.insert(qname, Dom::from_ref(&*result)); .insert(qname, Dom::from_ref(&*result));
@ -5012,8 +5021,12 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
if let Some(collection) = self.classes_map.borrow().get(&class_atoms) { if let Some(collection) = self.classes_map.borrow().get(&class_atoms) {
return DomRoot::from_ref(collection); return DomRoot::from_ref(collection);
} }
let result = let result = HTMLCollection::by_atomic_class_name(
HTMLCollection::by_atomic_class_name(&self.window, self.upcast(), class_atoms.clone()); &self.window,
self.upcast(),
class_atoms.clone(),
CanGc::note(),
);
self.classes_map self.classes_map
.borrow_mut() .borrow_mut()
.insert(class_atoms, Dom::from_ref(&*result)); .insert(class_atoms, Dom::from_ref(&*result));
@ -5489,18 +5502,24 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
// https://html.spec.whatwg.org/multipage/#dom-document-images // https://html.spec.whatwg.org/multipage/#dom-document-images
fn Images(&self) -> DomRoot<HTMLCollection> { fn Images(&self) -> DomRoot<HTMLCollection> {
self.images.or_init(|| { self.images.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLImageElement>() &self.window,
}) self.upcast(),
|element, _| element.is::<HTMLImageElement>(),
CanGc::note(),
)
}) })
} }
// https://html.spec.whatwg.org/multipage/#dom-document-embeds // https://html.spec.whatwg.org/multipage/#dom-document-embeds
fn Embeds(&self) -> DomRoot<HTMLCollection> { fn Embeds(&self) -> DomRoot<HTMLCollection> {
self.embeds.or_init(|| { self.embeds.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLEmbedElement>() &self.window,
}) self.upcast(),
|element, _| element.is::<HTMLEmbedElement>(),
CanGc::note(),
)
}) })
} }
@ -5512,44 +5531,60 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
// https://html.spec.whatwg.org/multipage/#dom-document-links // https://html.spec.whatwg.org/multipage/#dom-document-links
fn Links(&self) -> DomRoot<HTMLCollection> { fn Links(&self) -> DomRoot<HTMLCollection> {
self.links.or_init(|| { self.links.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
(element.is::<HTMLAnchorElement>() || element.is::<HTMLAreaElement>()) && &self.window,
element.has_attribute(&local_name!("href")) self.upcast(),
}) |element, _| {
(element.is::<HTMLAnchorElement>() || element.is::<HTMLAreaElement>()) &&
element.has_attribute(&local_name!("href"))
},
CanGc::note(),
)
}) })
} }
// https://html.spec.whatwg.org/multipage/#dom-document-forms // https://html.spec.whatwg.org/multipage/#dom-document-forms
fn Forms(&self) -> DomRoot<HTMLCollection> { fn Forms(&self) -> DomRoot<HTMLCollection> {
self.forms.or_init(|| { self.forms.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLFormElement>() &self.window,
}) self.upcast(),
|element, _| element.is::<HTMLFormElement>(),
CanGc::note(),
)
}) })
} }
// https://html.spec.whatwg.org/multipage/#dom-document-scripts // https://html.spec.whatwg.org/multipage/#dom-document-scripts
fn Scripts(&self) -> DomRoot<HTMLCollection> { fn Scripts(&self) -> DomRoot<HTMLCollection> {
self.scripts.or_init(|| { self.scripts.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLScriptElement>() &self.window,
}) self.upcast(),
|element, _| element.is::<HTMLScriptElement>(),
CanGc::note(),
)
}) })
} }
// https://html.spec.whatwg.org/multipage/#dom-document-anchors // https://html.spec.whatwg.org/multipage/#dom-document-anchors
fn Anchors(&self) -> DomRoot<HTMLCollection> { fn Anchors(&self) -> DomRoot<HTMLCollection> {
self.anchors.or_init(|| { self.anchors.or_init(|| {
HTMLCollection::new_with_filter_fn(&self.window, self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLAnchorElement>() && element.has_attribute(&local_name!("href")) &self.window,
}) self.upcast(),
|element, _| {
element.is::<HTMLAnchorElement>() && element.has_attribute(&local_name!("href"))
},
CanGc::note(),
)
}) })
} }
// https://html.spec.whatwg.org/multipage/#dom-document-applets // https://html.spec.whatwg.org/multipage/#dom-document-applets
fn Applets(&self) -> DomRoot<HTMLCollection> { fn Applets(&self) -> DomRoot<HTMLCollection> {
self.applets self.applets
.or_init(|| HTMLCollection::always_empty(&self.window, self.upcast())) .or_init(|| HTMLCollection::always_empty(&self.window, self.upcast(), CanGc::note()))
} }
// https://html.spec.whatwg.org/multipage/#dom-document-location // https://html.spec.whatwg.org/multipage/#dom-document-location
@ -5563,7 +5598,7 @@ impl DocumentMethods<crate::DomTypeHolder> for Document {
// https://dom.spec.whatwg.org/#dom-parentnode-children // https://dom.spec.whatwg.org/#dom-parentnode-children
fn Children(&self) -> DomRoot<HTMLCollection> { fn Children(&self) -> DomRoot<HTMLCollection> {
HTMLCollection::children(&self.window, self.upcast()) HTMLCollection::children(&self.window, self.upcast(), CanGc::note())
} }
// https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild // https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild

View file

@ -78,7 +78,7 @@ impl DocumentFragmentMethods<crate::DomTypeHolder> for DocumentFragment {
// https://dom.spec.whatwg.org/#dom-parentnode-children // https://dom.spec.whatwg.org/#dom-parentnode-children
fn Children(&self) -> DomRoot<HTMLCollection> { fn Children(&self) -> DomRoot<HTMLCollection> {
let window = self.owner_window(); let window = self.owner_window();
HTMLCollection::children(&window, self.upcast()) HTMLCollection::children(&window, self.upcast(), CanGc::note())
} }
// https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid // https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid

View file

@ -2616,7 +2616,12 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
// https://dom.spec.whatwg.org/#dom-element-getelementsbytagname // https://dom.spec.whatwg.org/#dom-element-getelementsbytagname
fn GetElementsByTagName(&self, localname: DOMString) -> DomRoot<HTMLCollection> { fn GetElementsByTagName(&self, localname: DOMString) -> DomRoot<HTMLCollection> {
let window = self.owner_window(); let window = self.owner_window();
HTMLCollection::by_qualified_name(&window, self.upcast(), LocalName::from(&*localname)) HTMLCollection::by_qualified_name(
&window,
self.upcast(),
LocalName::from(&*localname),
CanGc::note(),
)
} }
// https://dom.spec.whatwg.org/#dom-element-getelementsbytagnamens // https://dom.spec.whatwg.org/#dom-element-getelementsbytagnamens
@ -2626,13 +2631,13 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
localname: DOMString, localname: DOMString,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
let window = self.owner_window(); let window = self.owner_window();
HTMLCollection::by_tag_name_ns(&window, self.upcast(), localname, maybe_ns) HTMLCollection::by_tag_name_ns(&window, self.upcast(), localname, maybe_ns, CanGc::note())
} }
// https://dom.spec.whatwg.org/#dom-element-getelementsbyclassname // https://dom.spec.whatwg.org/#dom-element-getelementsbyclassname
fn GetElementsByClassName(&self, classes: DOMString) -> DomRoot<HTMLCollection> { fn GetElementsByClassName(&self, classes: DOMString) -> DomRoot<HTMLCollection> {
let window = self.owner_window(); let window = self.owner_window();
HTMLCollection::by_class_name(&window, self.upcast(), classes) HTMLCollection::by_class_name(&window, self.upcast(), classes, CanGc::note())
} }
// https://drafts.csswg.org/cssom-view/#dom-element-getclientrects // https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
@ -3112,7 +3117,7 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
// https://dom.spec.whatwg.org/#dom-parentnode-children // https://dom.spec.whatwg.org/#dom-parentnode-children
fn Children(&self) -> DomRoot<HTMLCollection> { fn Children(&self) -> DomRoot<HTMLCollection> {
let window = self.owner_window(); let window = self.owner_window();
HTMLCollection::children(&window, self.upcast()) HTMLCollection::children(&window, self.upcast(), CanGc::note())
} }
// https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild // https://dom.spec.whatwg.org/#dom-parentnode-firstelementchild

View file

@ -87,7 +87,7 @@ impl HTMLCollection {
} }
/// Returns a collection which is always empty. /// Returns a collection which is always empty.
pub(crate) fn always_empty(window: &Window, root: &Node) -> DomRoot<Self> { pub(crate) fn always_empty(window: &Window, root: &Node, can_gc: CanGc) -> DomRoot<Self> {
#[derive(JSTraceable)] #[derive(JSTraceable)]
struct NoFilter; struct NoFilter;
impl CollectionFilter for NoFilter { impl CollectionFilter for NoFilter {
@ -96,7 +96,7 @@ impl HTMLCollection {
} }
} }
Self::new(window, root, Box::new(NoFilter), CanGc::note()) Self::new(window, root, Box::new(NoFilter), can_gc)
} }
#[cfg_attr(crown, allow(crown::unrooted_must_root))] #[cfg_attr(crown, allow(crown::unrooted_must_root))]
@ -114,6 +114,7 @@ impl HTMLCollection {
window: &Window, window: &Window,
root: &Node, root: &Node,
filter_function: fn(&Element, &Node) -> bool, filter_function: fn(&Element, &Node) -> bool,
can_gc: CanGc,
) -> DomRoot<Self> { ) -> DomRoot<Self> {
#[derive(JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
pub(crate) struct StaticFunctionFilter( pub(crate) struct StaticFunctionFilter(
@ -132,7 +133,7 @@ impl HTMLCollection {
window, window,
root, root,
Box::new(StaticFunctionFilter(filter_function)), Box::new(StaticFunctionFilter(filter_function)),
CanGc::note(), can_gc,
) )
} }
@ -176,6 +177,7 @@ impl HTMLCollection {
window: &Window, window: &Window,
root: &Node, root: &Node,
qualified_name: LocalName, qualified_name: LocalName,
_can_gc: CanGc,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
// case 1 // case 1
if qualified_name == local_name!("*") { if qualified_name == local_name!("*") {
@ -231,17 +233,19 @@ impl HTMLCollection {
root: &Node, root: &Node,
tag: DOMString, tag: DOMString,
maybe_ns: Option<DOMString>, maybe_ns: Option<DOMString>,
can_gc: CanGc,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
let local = LocalName::from(tag); let local = LocalName::from(tag);
let ns = namespace_from_domstring(maybe_ns); let ns = namespace_from_domstring(maybe_ns);
let qname = QualName::new(None, ns, local); let qname = QualName::new(None, ns, local);
HTMLCollection::by_qual_tag_name(window, root, qname) HTMLCollection::by_qual_tag_name(window, root, qname, can_gc)
} }
pub(crate) fn by_qual_tag_name( pub(crate) fn by_qual_tag_name(
window: &Window, window: &Window,
root: &Node, root: &Node,
qname: QualName, qname: QualName,
_can_gc: CanGc,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
#[derive(JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
struct TagNameNSFilter { struct TagNameNSFilter {
@ -263,15 +267,17 @@ impl HTMLCollection {
window: &Window, window: &Window,
root: &Node, root: &Node,
classes: DOMString, classes: DOMString,
can_gc: CanGc,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
let class_atoms = split_html_space_chars(&classes).map(Atom::from).collect(); let class_atoms = split_html_space_chars(&classes).map(Atom::from).collect();
HTMLCollection::by_atomic_class_name(window, root, class_atoms) HTMLCollection::by_atomic_class_name(window, root, class_atoms, can_gc)
} }
pub(crate) fn by_atomic_class_name( pub(crate) fn by_atomic_class_name(
window: &Window, window: &Window,
root: &Node, root: &Node,
classes: Vec<Atom>, classes: Vec<Atom>,
can_gc: CanGc,
) -> DomRoot<HTMLCollection> { ) -> DomRoot<HTMLCollection> {
#[derive(JSTraceable, MallocSizeOf)] #[derive(JSTraceable, MallocSizeOf)]
struct ClassNameFilter { struct ClassNameFilter {
@ -292,17 +298,20 @@ impl HTMLCollection {
} }
if classes.is_empty() { if classes.is_empty() {
return HTMLCollection::always_empty(window, root); return HTMLCollection::always_empty(window, root, can_gc);
} }
let filter = ClassNameFilter { classes }; let filter = ClassNameFilter { classes };
HTMLCollection::create(window, root, Box::new(filter)) HTMLCollection::create(window, root, Box::new(filter))
} }
pub(crate) fn children(window: &Window, root: &Node) -> DomRoot<HTMLCollection> { pub(crate) fn children(window: &Window, root: &Node, can_gc: CanGc) -> DomRoot<HTMLCollection> {
HTMLCollection::new_with_filter_fn(window, root, |element, root| { HTMLCollection::new_with_filter_fn(
root.is_parent_of(element.upcast()) window,
}) root,
|element, root| root.is_parent_of(element.upcast()),
can_gc,
)
} }
pub(crate) fn elements_iter_after<'a>( pub(crate) fn elements_iter_after<'a>(

View file

@ -54,8 +54,11 @@ impl HTMLDataListElement {
impl HTMLDataListElementMethods<crate::DomTypeHolder> for HTMLDataListElement { impl HTMLDataListElementMethods<crate::DomTypeHolder> for HTMLDataListElement {
// https://html.spec.whatwg.org/multipage/#dom-datalist-options // https://html.spec.whatwg.org/multipage/#dom-datalist-options
fn Options(&self) -> DomRoot<HTMLCollection> { fn Options(&self) -> DomRoot<HTMLCollection> {
HTMLCollection::new_with_filter_fn(&self.owner_window(), self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLOptionElement>() &self.owner_window(),
}) self.upcast(),
|element, _| element.is::<HTMLOptionElement>(),
CanGc::note(),
)
} }
} }

View file

@ -88,11 +88,16 @@ impl HTMLFieldSetElement {
impl HTMLFieldSetElementMethods<crate::DomTypeHolder> for HTMLFieldSetElement { impl HTMLFieldSetElementMethods<crate::DomTypeHolder> for HTMLFieldSetElement {
// https://html.spec.whatwg.org/multipage/#dom-fieldset-elements // https://html.spec.whatwg.org/multipage/#dom-fieldset-elements
fn Elements(&self) -> DomRoot<HTMLCollection> { fn Elements(&self) -> DomRoot<HTMLCollection> {
HTMLCollection::new_with_filter_fn(&self.owner_window(), self.upcast(), |element, _| { HTMLCollection::new_with_filter_fn(
element &self.owner_window(),
.downcast::<HTMLElement>() self.upcast(),
.is_some_and(HTMLElement::is_listed_element) |element, _| {
}) element
.downcast::<HTMLElement>()
.is_some_and(HTMLElement::is_listed_element)
},
CanGc::note(),
)
} }
// https://html.spec.whatwg.org/multipage/#dom-fieldset-disabled // https://html.spec.whatwg.org/multipage/#dom-fieldset-disabled

View file

@ -318,6 +318,7 @@ impl HTMLTableElementMethods<crate::DomTypeHolder> for HTMLTableElement {
element.local_name() == &local_name!("tbody") && element.local_name() == &local_name!("tbody") &&
element.upcast::<Node>().GetParentNode().as_deref() == Some(root) element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
}, },
CanGc::note(),
) )
}) })
} }

View file

@ -93,6 +93,7 @@ impl HTMLTableRowElementMethods<crate::DomTypeHolder> for HTMLTableRowElement {
(element.is::<HTMLTableCellElement>()) && (element.is::<HTMLTableCellElement>()) &&
element.upcast::<Node>().GetParentNode().as_deref() == Some(root) element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
}, },
CanGc::note(),
) )
}) })
} }

View file

@ -64,10 +64,15 @@ impl HTMLTableSectionElement {
impl HTMLTableSectionElementMethods<crate::DomTypeHolder> for HTMLTableSectionElement { impl HTMLTableSectionElementMethods<crate::DomTypeHolder> for HTMLTableSectionElement {
// https://html.spec.whatwg.org/multipage/#dom-tbody-rows // https://html.spec.whatwg.org/multipage/#dom-tbody-rows
fn Rows(&self) -> DomRoot<HTMLCollection> { fn Rows(&self) -> DomRoot<HTMLCollection> {
HTMLCollection::new_with_filter_fn(&self.owner_window(), self.upcast(), |element, root| { HTMLCollection::new_with_filter_fn(
element.is::<HTMLTableRowElement>() && &self.owner_window(),
element.upcast::<Node>().GetParentNode().as_deref() == Some(root) self.upcast(),
}) |element, root| {
element.is::<HTMLTableRowElement>() &&
element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
},
CanGc::note(),
)
} }
// https://html.spec.whatwg.org/multipage/#dom-tbody-insertrow // https://html.spec.whatwg.org/multipage/#dom-tbody-insertrow

View file

@ -2128,6 +2128,7 @@ impl ScriptThread {
pipeline_id, pipeline_id,
selector, selector,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::FindElementsCSS(selector, reply) => { WebDriverScriptCommand::FindElementsCSS(selector, reply) => {
@ -2153,6 +2154,7 @@ impl ScriptThread {
pipeline_id, pipeline_id,
selector, selector,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::FindElementElementCSS(selector, element_id, reply) => { WebDriverScriptCommand::FindElementElementCSS(selector, element_id, reply) => {
@ -2184,6 +2186,7 @@ impl ScriptThread {
element_id, element_id,
selector, selector,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::FindElementElementsCSS(selector, element_id, reply) => { WebDriverScriptCommand::FindElementElementsCSS(selector, element_id, reply) => {
@ -2215,6 +2218,7 @@ impl ScriptThread {
element_id, element_id,
selector, selector,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::FocusElement(element_id, reply) => { WebDriverScriptCommand::FocusElement(element_id, reply) => {

View file

@ -551,6 +551,7 @@ pub(crate) fn handle_find_element_tag_name(
pipeline: PipelineId, pipeline: PipelineId,
selector: String, selector: String,
reply: IpcSender<Result<Option<String>, ErrorStatus>>, reply: IpcSender<Result<Option<String>, ErrorStatus>>,
_can_gc: CanGc,
) { ) {
reply reply
.send( .send(
@ -618,6 +619,7 @@ pub(crate) fn handle_find_elements_tag_name(
pipeline: PipelineId, pipeline: PipelineId,
selector: String, selector: String,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>, reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
_can_gc: CanGc,
) { ) {
reply reply
.send( .send(
@ -675,6 +677,7 @@ pub(crate) fn handle_find_element_element_tag_name(
element_id: String, element_id: String,
selector: String, selector: String,
reply: IpcSender<Result<Option<String>, ErrorStatus>>, reply: IpcSender<Result<Option<String>, ErrorStatus>>,
_can_gc: CanGc,
) { ) {
reply reply
.send( .send(
@ -737,6 +740,7 @@ pub(crate) fn handle_find_element_elements_tag_name(
element_id: String, element_id: String,
selector: String, selector: String,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>, reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
_can_gc: CanGc,
) { ) {
reply reply
.send( .send(