mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
script: Expose a constructor on HTMLCollection
that takes a static function (#34667)
Expose a new constructor on `HTMLCollection`, `new_with_filter_fn`, that filters elements using a simple static function -- a common pattern. This allows more easily creating this `HTMLCollection` without having to create a new data structure. Since the constructor takes a static function, no data should be captured preventing garbage collection issues. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
d54b68bc96
commit
9d986a8ab3
7 changed files with 90 additions and 135 deletions
|
@ -17,7 +17,7 @@ use crate::dom::bindings::str::DOMString;
|
|||
use crate::dom::customelementregistry::CallbackReaction;
|
||||
use crate::dom::document::Document;
|
||||
use crate::dom::element::{AttributeMutation, Element};
|
||||
use crate::dom::htmlcollection::{CollectionFilter, HTMLCollection};
|
||||
use crate::dom::htmlcollection::HTMLCollection;
|
||||
use crate::dom::htmlelement::HTMLElement;
|
||||
use crate::dom::htmlformelement::{FormControl, HTMLFormElement};
|
||||
use crate::dom::htmllegendelement::HTMLLegendElement;
|
||||
|
@ -88,17 +88,11 @@ impl HTMLFieldSetElement {
|
|||
impl HTMLFieldSetElementMethods<crate::DomTypeHolder> for HTMLFieldSetElement {
|
||||
// https://html.spec.whatwg.org/multipage/#dom-fieldset-elements
|
||||
fn Elements(&self) -> DomRoot<HTMLCollection> {
|
||||
#[derive(JSTraceable, MallocSizeOf)]
|
||||
struct ElementsFilter;
|
||||
impl CollectionFilter for ElementsFilter {
|
||||
fn filter<'a>(&self, elem: &'a Element, _root: &'a Node) -> bool {
|
||||
elem.downcast::<HTMLElement>()
|
||||
.is_some_and(HTMLElement::is_listed_element)
|
||||
}
|
||||
}
|
||||
let filter = Box::new(ElementsFilter);
|
||||
let window = window_from_node(self);
|
||||
HTMLCollection::create(&window, self.upcast(), filter)
|
||||
HTMLCollection::new_with_filter_fn(&window_from_node(self), self.upcast(), |element, _| {
|
||||
element
|
||||
.downcast::<HTMLElement>()
|
||||
.is_some_and(HTMLElement::is_listed_element)
|
||||
})
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-fieldset-disabled
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue