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::context::JSContext;
8use js::rust::HandleObject;
9
10use crate::dom::bindings::codegen::Bindings::HTMLMapElementBinding::HTMLMapElementMethods;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::root::{DomRoot, MutNullableDom};
13use crate::dom::bindings::str::DOMString;
14use crate::dom::document::Document;
15use crate::dom::html::htmlareaelement::HTMLAreaElement;
16use crate::dom::html::htmlcollection::HTMLCollection;
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::node::{Node, NodeTraits, ShadowIncluding};
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct HTMLMapElement {
23    htmlelement: HTMLElement,
24    /// <https://html.spec.whatwg.org/multipage/#dom-map-areas>
25    areas: MutNullableDom<HTMLCollection>,
26}
27
28impl HTMLMapElement {
29    fn new_inherited(
30        local_name: LocalName,
31        prefix: Option<Prefix>,
32        document: &Document,
33    ) -> HTMLMapElement {
34        HTMLMapElement {
35            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
36            areas: Default::default(),
37        }
38    }
39
40    pub(crate) fn new(
41        cx: &mut js::context::JSContext,
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45        proto: Option<HandleObject>,
46    ) -> DomRoot<HTMLMapElement> {
47        Node::reflect_node_with_proto(
48            cx,
49            Box::new(HTMLMapElement::new_inherited(local_name, prefix, document)),
50            document,
51            proto,
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!(cx, SetName, "name");
69
70    /// <https://html.spec.whatwg.org/multipage/#dom-map-areas>
71    fn Areas(&self, cx: &mut JSContext) -> 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                CanGc::from_cx(cx),
80            )
81        })
82    }
83}