script/dom/html/
htmlsourceelement.rs1use 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 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 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 fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
132 self.super_type().unwrap().bind_to_tree(context, can_gc);
133
134 let parent = self.upcast::<Node>().GetParentNode().unwrap();
136
137 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 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 fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
159 self.super_type().unwrap().unbind_from_tree(context, can_gc);
160
161 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 make_url_getter!(Src, "src");
178
179 make_url_setter!(SetSrc, "src");
181
182 make_getter!(Type, "type");
184
185 make_setter!(SetType, "type");
187
188 make_url_getter!(Srcset, "srcset");
190
191 make_url_setter!(SetSrcset, "srcset");
193
194 make_getter!(Sizes, "sizes");
196
197 make_setter!(SetSizes, "sizes");
199
200 make_getter!(Media, "media");
202
203 make_setter!(SetMedia, "media");
205
206 make_dimension_uint_getter!(Width, "width");
208
209 make_dimension_uint_setter!(SetWidth, "width");
211
212 make_dimension_uint_getter!(Height, "height");
214
215 make_dimension_uint_setter!(SetHeight, "height");
217}