mirror of
https://github.com/servo/servo.git
synced 2025-08-02 12:10:29 +01: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
|
@ -18,7 +18,7 @@ use crate::dom::bindings::root::{DomRoot, LayoutDom, MutNullableDom};
|
|||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::document::Document;
|
||||
use crate::dom::element::{Element, LayoutElementHelpers};
|
||||
use crate::dom::htmlcollection::{CollectionFilter, HTMLCollection};
|
||||
use crate::dom::htmlcollection::HTMLCollection;
|
||||
use crate::dom::htmlelement::HTMLElement;
|
||||
use crate::dom::htmltablecellelement::HTMLTableCellElement;
|
||||
use crate::dom::htmltableelement::HTMLTableElement;
|
||||
|
@ -27,15 +27,6 @@ use crate::dom::node::{window_from_node, Node};
|
|||
use crate::dom::virtualmethods::VirtualMethods;
|
||||
use crate::script_runtime::CanGc;
|
||||
|
||||
#[derive(JSTraceable)]
|
||||
struct CellsFilter;
|
||||
impl CollectionFilter for CellsFilter {
|
||||
fn filter(&self, elem: &Element, root: &Node) -> bool {
|
||||
(elem.is::<HTMLTableCellElement>()) &&
|
||||
elem.upcast::<Node>().GetParentNode().as_deref() == Some(root)
|
||||
}
|
||||
}
|
||||
|
||||
#[dom_struct]
|
||||
pub struct HTMLTableRowElement {
|
||||
htmlelement: HTMLElement,
|
||||
|
@ -95,9 +86,14 @@ impl HTMLTableRowElementMethods<crate::DomTypeHolder> for HTMLTableRowElement {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-tr-cells
|
||||
fn Cells(&self) -> DomRoot<HTMLCollection> {
|
||||
self.cells.or_init(|| {
|
||||
let window = window_from_node(self);
|
||||
let filter = Box::new(CellsFilter);
|
||||
HTMLCollection::create(&window, self.upcast(), filter)
|
||||
HTMLCollection::new_with_filter_fn(
|
||||
&window_from_node(self),
|
||||
self.upcast(),
|
||||
|element, root| {
|
||||
(element.is::<HTMLTableCellElement>()) &&
|
||||
element.upcast::<Node>().GetParentNode().as_deref() == Some(root)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue