script/dom/html/
htmlmapelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix};
7use js::rust::HandleObject;
8
9use crate::dom::bindings::codegen::Bindings::HTMLMapElementBinding::HTMLMapElementMethods;
10use crate::dom::bindings::inheritance::Castable;
11use crate::dom::bindings::root::{DomRoot, MutNullableDom};
12use crate::dom::bindings::str::DOMString;
13use crate::dom::document::Document;
14use crate::dom::html::htmlareaelement::HTMLAreaElement;
15use crate::dom::html::htmlcollection::HTMLCollection;
16use crate::dom::html::htmlelement::HTMLElement;
17use crate::dom::node::{Node, NodeTraits, ShadowIncluding};
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub(crate) struct HTMLMapElement {
22    htmlelement: HTMLElement,
23    /// <https://html.spec.whatwg.org/multipage/#dom-map-areas>
24    areas: MutNullableDom<HTMLCollection>,
25}
26
27impl HTMLMapElement {
28    fn new_inherited(
29        local_name: LocalName,
30        prefix: Option<Prefix>,
31        document: &Document,
32    ) -> HTMLMapElement {
33        HTMLMapElement {
34            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
35            areas: Default::default(),
36        }
37    }
38
39    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    pub(crate) fn new(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<HTMLMapElement> {
47        Node::reflect_node_with_proto(
48            Box::new(HTMLMapElement::new_inherited(local_name, prefix, document)),
49            document,
50            proto,
51            can_gc,
52        )
53    }
54
55    pub(crate) fn get_area_elements(&self) -> Vec<DomRoot<HTMLAreaElement>> {
56        self.upcast::<Node>()
57            .traverse_preorder(ShadowIncluding::No)
58            .filter_map(DomRoot::downcast::<HTMLAreaElement>)
59            .collect()
60    }
61}
62
63impl HTMLMapElementMethods<crate::DomTypeHolder> for HTMLMapElement {
64    // <https://html.spec.whatwg.org/multipage/#dom-map-name>
65    make_getter!(Name, "name");
66
67    // <https://html.spec.whatwg.org/multipage/#dom-map-name>
68    make_atomic_setter!(SetName, "name");
69
70    /// <https://html.spec.whatwg.org/multipage/#dom-map-areas>
71    fn Areas(&self, can_gc: CanGc) -> DomRoot<HTMLCollection> {
72        // The areas attribute must return an HTMLCollection rooted at the map element, whose filter
73        // matches only area elements.
74        self.areas.or_init(|| {
75            HTMLCollection::new_with_filter_fn(
76                &self.owner_window(),
77                self.upcast(),
78                |element, _| element.is::<HTMLAreaElement>(),
79                can_gc,
80            )
81        })
82    }
83}