/
githubmirror
/
servo
Обзор
Документация
Войти
/
githubmirror
/
servo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
components/script/dom/html/htmlformcontrolscollection.rs
130 строк
5 KB
Narfinger
script: Use unrooted iterators for HTMLCollections (#45582)
16 июн 2026, 12:09
Не верифицирован
16 июн 2026, 12:09
728e768
Код
Авторство
О чём код?
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use dom_struct::dom_struct; use js::context::{JSContext, NoGC}; use script_bindings::reflector::reflect_dom_object_with_cx; use stylo_atoms::Atom; use crate::dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods; use crate::dom::bindings::codegen::Bindings::HTMLFormControlsCollectionBinding::HTMLFormControlsCollectionMethods; use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods}; use crate::dom::bindings::codegen::UnionTypes::RadioNodeListOrElement; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::DomGlobal; use crate::dom::bindings::root::{Dom, DomRoot}; use crate::dom::bindings::str::DOMString; use crate::dom::element::Element; use crate::dom::html::htmlcollection::{CollectionFilter, HTMLCollection}; use crate::dom::html::htmlformelement::HTMLFormElement; use crate::dom::node::Node; use crate::dom::radionodelist::RadioNodeList; use crate::dom::window::Window; #[dom_struct] pub(crate) struct HTMLFormControlsCollection { collection: HTMLCollection, form: Dom<HTMLFormElement>, } impl HTMLFormControlsCollection { fn new_inherited( form: &HTMLFormElement, filter: Box<dyn CollectionFilter + 'static>, ) -> HTMLFormControlsCollection { let root_of_form = form .upcast::<Node>() .GetRootNode(&GetRootNodeOptions::empty()); HTMLFormControlsCollection { collection: HTMLCollection::new_inherited(&root_of_form, filter), form: Dom::from_ref(form), } } pub(crate) fn new( cx: &mut JSContext, window: &Window, form: &HTMLFormElement, filter: Box<dyn CollectionFilter + 'static>, ) -> DomRoot<HTMLFormControlsCollection> { reflect_dom_object_with_cx( Box::new(HTMLFormControlsCollection::new_inherited(form, filter)), window, cx, ) } } impl HTMLFormControlsCollectionMethods<crate::DomTypeHolder> for HTMLFormControlsCollection { // FIXME: This shouldn't need to be implemented here since HTMLCollection (the parent of // HTMLFormControlsCollection) implements Length /// <https://dom.spec.whatwg.org/#dom-htmlcollection-length> fn Length(&self, cx: &JSContext) -> u32 { self.collection.Length(cx) } /// <https://html.spec.whatwg.org/multipage/#dom-htmlformcontrolscollection-nameditem> fn NamedItem(&self, cx: &mut JSContext, name: DOMString) -> Option<RadioNodeListOrElement> { // Step 1 if name.is_empty() { return None; } let name = Atom::from(name); { let mut filter_map = self .collection .elements_iter(cx.no_gc()) .filter_map(|elem| { if elem.get_name().is_some_and(|n| n == name) || elem.get_id().is_some_and(|i| i == name) { Some(elem) } else { None } }) .peekable(); // This if is more complicated because of lifetimes. // Step 2 if filter_map.peek().is_none() { return None; } else { let elem = filter_map.next().unwrap(); if filter_map.peek().is_none() { return Some(RadioNodeListOrElement::Element(elem.as_rooted())); } } } // Step 4-5 let global = self.global(); let window = global.as_window(); // There is only one way to get an HTMLCollection, // specifically HTMLFormElement::Elements(), // and the collection filter excludes image inputs. Some(RadioNodeListOrElement::RadioNodeList( RadioNodeList::new_controls_except_image_inputs(cx, window, &self.form, &name), )) } /// <https://html.spec.whatwg.org/multipage/#dom-htmlformcontrolscollection-nameditem> fn NamedGetter(&self, cx: &mut JSContext, name: DOMString) -> Option<RadioNodeListOrElement> { self.NamedItem(cx, name) } /// <https://html.spec.whatwg.org/multipage/#the-htmlformcontrolscollection-interface:supported-property-names> fn SupportedPropertyNames(&self, no_gc: &NoGC) -> Vec<DOMString> { self.collection.SupportedPropertyNames(no_gc) } // FIXME: This shouldn't need to be implemented here since HTMLCollection (the parent of // HTMLFormControlsCollection) implements IndexedGetter. // https://github.com/servo/servo/issues/5875 // /// <https://dom.spec.whatwg.org/#dom-htmlcollection-item> fn IndexedGetter(&self, cx: &JSContext, index: u32) -> Option<DomRoot<Element>> { self.collection.IndexedGetter(cx, index) } }