script/dom/html/
htmlsourceelement.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, local_name};
7use js::rust::HandleObject;
8use style::attr::AttrValue;
9
10use crate::dom::attr::Attr;
11use crate::dom::bindings::codegen::Bindings::HTMLSourceElementBinding::HTMLSourceElementMethods;
12use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::{Dom, DomRoot, Root};
15use crate::dom::bindings::str::{DOMString, USVString};
16use crate::dom::document::Document;
17use crate::dom::element::AttributeMutation;
18use crate::dom::html::htmlelement::HTMLElement;
19use crate::dom::html::htmlimageelement::HTMLImageElement;
20use crate::dom::html::htmlmediaelement::HTMLMediaElement;
21use crate::dom::html::htmlpictureelement::HTMLPictureElement;
22use crate::dom::node::{BindContext, Node, NodeDamage, UnbindContext};
23use crate::dom::virtualmethods::VirtualMethods;
24use crate::script_runtime::CanGc;
25
26#[dom_struct]
27pub(crate) struct HTMLSourceElement {
28    htmlelement: HTMLElement,
29}
30
31impl HTMLSourceElement {
32    fn new_inherited(
33        local_name: LocalName,
34        prefix: Option<Prefix>,
35        document: &Document,
36    ) -> HTMLSourceElement {
37        HTMLSourceElement {
38            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
39        }
40    }
41
42    pub(crate) fn new(
43        local_name: LocalName,
44        prefix: Option<Prefix>,
45        document: &Document,
46        proto: Option<HandleObject>,
47        can_gc: CanGc,
48    ) -> DomRoot<HTMLSourceElement> {
49        Node::reflect_node_with_proto(
50            Box::new(HTMLSourceElement::new_inherited(
51                local_name, prefix, document,
52            )),
53            document,
54            proto,
55            can_gc,
56        )
57    }
58
59    fn iterate_next_html_image_element_siblings(
60        next_siblings_iterator: impl Iterator<Item = Root<Dom<Node>>>,
61        callback: impl Fn(&HTMLImageElement),
62    ) {
63        for next_sibling in next_siblings_iterator {
64            if let Some(html_image_element_sibling) = next_sibling.downcast::<HTMLImageElement>() {
65                callback(html_image_element_sibling);
66            }
67        }
68    }
69}
70
71impl VirtualMethods for HTMLSourceElement {
72    fn super_type(&self) -> Option<&dyn VirtualMethods> {
73        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
74    }
75
76    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
77        self.super_type()
78            .unwrap()
79            .attribute_mutated(attr, mutation, can_gc);
80
81        match attr.local_name() {
82            &local_name!("srcset") |
83            &local_name!("sizes") |
84            &local_name!("media") |
85            &local_name!("type") => {
86                // <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
87                // The element's parent is a picture element and a source element that is a previous
88                // sibling has its srcset, sizes, media, type attributes set, changed, or removed.
89                if let Some(parent) = self.upcast::<Node>().GetParentElement() {
90                    if parent.is::<HTMLPictureElement>() {
91                        let next_sibling_iterator = self.upcast::<Node>().following_siblings();
92                        HTMLSourceElement::iterate_next_html_image_element_siblings(
93                            next_sibling_iterator,
94                            |image| image.update_the_image_data(can_gc),
95                        );
96                    }
97                }
98            },
99            &local_name!("width") | &local_name!("height") => {
100                // Note: Despite being explicitly stated in the specification that any width or
101                // height attributes changes (set, changed, removed) of the source element should be
102                // counted as relevant mutation for the sibling image element, these attributes
103                // affect only the style presentational hints of the image element.
104                if let Some(parent) = self.upcast::<Node>().GetParentElement() {
105                    if parent.is::<HTMLPictureElement>() {
106                        let next_sibling_iterator = self.upcast::<Node>().following_siblings();
107                        HTMLSourceElement::iterate_next_html_image_element_siblings(
108                            next_sibling_iterator,
109                            |image| image.upcast::<Node>().dirty(NodeDamage::Other),
110                        );
111                    }
112                }
113            },
114            _ => {},
115        }
116    }
117
118    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
119        match name {
120            &local_name!("width") | &local_name!("height") => {
121                AttrValue::from_dimension(value.into())
122            },
123            _ => self
124                .super_type()
125                .unwrap()
126                .parse_plain_attribute(name, value),
127        }
128    }
129
130    /// <https://html.spec.whatwg.org/multipage/#the-source-element:html-element-insertion-steps>
131    fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
132        self.super_type().unwrap().bind_to_tree(context, can_gc);
133
134        // Step 1. Let parent be insertedNode's parent.
135        let parent = self.upcast::<Node>().GetParentNode().unwrap();
136
137        // Step 2. If parent is a media element that has no src attribute and whose networkState has
138        // the value NETWORK_EMPTY, then invoke that media element's resource selection algorithm.
139        if parent.is::<HTMLMediaElement>() && std::ptr::eq(&*parent, context.parent) {
140            parent
141                .downcast::<HTMLMediaElement>()
142                .unwrap()
143                .handle_source_child_insertion(self, can_gc);
144        }
145
146        // Step 3. If parent is a picture element, then for each child of parent's children, if
147        // child is an img element, then count this as a relevant mutation for child.
148        if parent.is::<HTMLPictureElement>() && std::ptr::eq(&*parent, context.parent) {
149            let next_sibling_iterator = self.upcast::<Node>().following_siblings();
150            HTMLSourceElement::iterate_next_html_image_element_siblings(
151                next_sibling_iterator,
152                |image| image.update_the_image_data(can_gc),
153            );
154        }
155    }
156
157    /// <https://html.spec.whatwg.org/multipage/#the-source-element:html-element-removing-steps>
158    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
159        self.super_type().unwrap().unbind_from_tree(context, can_gc);
160
161        // Step 1. If oldParent is a picture element, then for each child of oldParent's children,
162        // if child is an img element, then count this as a relevant mutation for child.
163        if context.parent.is::<HTMLPictureElement>() && !self.upcast::<Node>().has_parent() {
164            if let Some(next_sibling) = context.next_sibling {
165                let next_sibling_iterator = next_sibling.inclusively_following_siblings();
166                HTMLSourceElement::iterate_next_html_image_element_siblings(
167                    next_sibling_iterator,
168                    |image| image.update_the_image_data(can_gc),
169                );
170            }
171        }
172    }
173}
174
175impl HTMLSourceElementMethods<crate::DomTypeHolder> for HTMLSourceElement {
176    // https://html.spec.whatwg.org/multipage/#dom-source-src
177    make_url_getter!(Src, "src");
178
179    // https://html.spec.whatwg.org/multipage/#dom-source-src
180    make_url_setter!(SetSrc, "src");
181
182    // https://html.spec.whatwg.org/multipage/#dom-source-type
183    make_getter!(Type, "type");
184
185    // https://html.spec.whatwg.org/multipage/#dom-source-type
186    make_setter!(SetType, "type");
187
188    // https://html.spec.whatwg.org/multipage/#dom-source-srcset
189    make_url_getter!(Srcset, "srcset");
190
191    // https://html.spec.whatwg.org/multipage/#dom-source-srcset
192    make_url_setter!(SetSrcset, "srcset");
193
194    // https://html.spec.whatwg.org/multipage/#dom-source-sizes
195    make_getter!(Sizes, "sizes");
196
197    // https://html.spec.whatwg.org/multipage/#dom-source-sizes
198    make_setter!(SetSizes, "sizes");
199
200    // https://html.spec.whatwg.org/multipage/#dom-source-media
201    make_getter!(Media, "media");
202
203    // https://html.spec.whatwg.org/multipage/#dom-source-media
204    make_setter!(SetMedia, "media");
205
206    // <https://html.spec.whatwg.org/multipage/#dom-source-width>
207    make_dimension_uint_getter!(Width, "width");
208
209    // <https://html.spec.whatwg.org/multipage/#dom-source-width>
210    make_dimension_uint_setter!(SetWidth, "width");
211
212    // <https://html.spec.whatwg.org/multipage/#dom-source-height>
213    make_dimension_uint_getter!(Height, "height");
214
215    // <https://html.spec.whatwg.org/multipage/#dom-source-height>
216    make_dimension_uint_setter!(SetHeight, "height");
217}