script/dom/
virtualmethods.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 html5ever::LocalName;
6use js::context::JSContext;
7use script_bindings::root::DomRoot;
8use script_bindings::script_runtime::CanGc;
9use style::attr::AttrValue;
10
11use crate::dom::attr::Attr;
12use crate::dom::bindings::inheritance::{
13    Castable, DocumentFragmentTypeId, ElementTypeId, HTMLElementTypeId, HTMLMediaElementTypeId,
14    NodeTypeId, SVGElementTypeId, SVGGraphicsElementTypeId,
15};
16use crate::dom::bindings::str::DOMString;
17use crate::dom::document::Document;
18use crate::dom::documentfragment::DocumentFragment;
19use crate::dom::element::{AttributeMutation, Element};
20use crate::dom::event::Event;
21use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
22use crate::dom::html::htmlareaelement::HTMLAreaElement;
23use crate::dom::html::htmlbaseelement::HTMLBaseElement;
24use crate::dom::html::htmlbodyelement::HTMLBodyElement;
25use crate::dom::html::htmlbuttonelement::HTMLButtonElement;
26use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
27use crate::dom::html::htmldetailselement::HTMLDetailsElement;
28use crate::dom::html::htmlelement::HTMLElement;
29use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
30use crate::dom::html::htmlfontelement::HTMLFontElement;
31use crate::dom::html::htmlformelement::HTMLFormElement;
32use crate::dom::html::htmlheadelement::HTMLHeadElement;
33use crate::dom::html::htmlhrelement::HTMLHRElement;
34use crate::dom::html::htmliframeelement::HTMLIFrameElement;
35use crate::dom::html::htmlimageelement::HTMLImageElement;
36use crate::dom::html::htmlinputelement::HTMLInputElement;
37use crate::dom::html::htmllabelelement::HTMLLabelElement;
38use crate::dom::html::htmllielement::HTMLLIElement;
39use crate::dom::html::htmllinkelement::HTMLLinkElement;
40use crate::dom::html::htmlmediaelement::HTMLMediaElement;
41use crate::dom::html::htmlmetaelement::HTMLMetaElement;
42use crate::dom::html::htmlmeterelement::HTMLMeterElement;
43use crate::dom::html::htmlobjectelement::HTMLObjectElement;
44use crate::dom::html::htmloptgroupelement::HTMLOptGroupElement;
45use crate::dom::html::htmloptionelement::HTMLOptionElement;
46use crate::dom::html::htmloutputelement::HTMLOutputElement;
47use crate::dom::html::htmlpreelement::HTMLPreElement;
48use crate::dom::html::htmlprogresselement::HTMLProgressElement;
49use crate::dom::html::htmlscriptelement::HTMLScriptElement;
50use crate::dom::html::htmlselectelement::HTMLSelectElement;
51use crate::dom::html::htmlslotelement::HTMLSlotElement;
52use crate::dom::html::htmlsourceelement::HTMLSourceElement;
53use crate::dom::html::htmlstyleelement::HTMLStyleElement;
54use crate::dom::html::htmltablecellelement::HTMLTableCellElement;
55use crate::dom::html::htmltablecolelement::HTMLTableColElement;
56use crate::dom::html::htmltableelement::HTMLTableElement;
57use crate::dom::html::htmltablerowelement::HTMLTableRowElement;
58use crate::dom::html::htmltablesectionelement::HTMLTableSectionElement;
59use crate::dom::html::htmltemplateelement::HTMLTemplateElement;
60use crate::dom::html::htmltextareaelement::HTMLTextAreaElement;
61use crate::dom::html::htmltitleelement::HTMLTitleElement;
62use crate::dom::html::htmlvideoelement::HTMLVideoElement;
63use crate::dom::htmlbuttonelement::CommandState;
64use crate::dom::htmldialogelement::HTMLDialogElement;
65use crate::dom::node::{
66    BindContext, ChildrenMutation, CloneChildrenFlag, MoveContext, Node, UnbindContext,
67};
68use crate::dom::shadowroot::ShadowRoot;
69use crate::dom::svg::svgelement::SVGElement;
70use crate::dom::svg::svgimageelement::SVGImageElement;
71use crate::dom::svg::svgsvgelement::SVGSVGElement;
72
73/// Trait to allow DOM nodes to opt-in to overriding (or adding to) common
74/// behaviours. Replicates the effect of C++ virtual methods.
75pub(crate) trait VirtualMethods {
76    /// Returns self as the superclass of the implementation for this trait,
77    /// if any.
78    fn super_type(&self) -> Option<&dyn VirtualMethods>;
79
80    /// Called when attributes of a node are mutated.
81    /// <https://dom.spec.whatwg.org/#attribute-is-set>
82    /// <https://dom.spec.whatwg.org/#attribute-is-removed>
83    fn attribute_mutated(
84        &self,
85        cx: &mut js::context::JSContext,
86        attr: &Attr,
87        mutation: AttributeMutation,
88    ) {
89        if let Some(s) = self.super_type() {
90            s.attribute_mutated(cx, attr, mutation);
91        }
92    }
93
94    /// Returns `true` if given attribute `attr` affects style of the
95    /// given element.
96    fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
97        match self.super_type() {
98            Some(s) => s.attribute_affects_presentational_hints(attr),
99            None => false,
100        }
101    }
102
103    /// Returns the right AttrValue variant for the attribute with name `name`
104    /// on this element.
105    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
106        match self.super_type() {
107            Some(s) => s.parse_plain_attribute(name, value),
108            _ => AttrValue::String(value.into()),
109        }
110    }
111
112    /// Invoked during a DOM tree mutation after a node becomes connected, once all
113    /// related DOM tree mutations have been applied.
114    /// <https://dom.spec.whatwg.org/#concept-node-post-connection-ext>
115    fn post_connection_steps(&self, cx: &mut JSContext) {
116        if let Some(s) = self.super_type() {
117            s.post_connection_steps(cx);
118        }
119    }
120
121    /// <https://dom.spec.whatwg.org/#concept-node-move-ext>
122    fn moving_steps(&self, context: &MoveContext, can_gc: CanGc) {
123        if let Some(s) = self.super_type() {
124            s.moving_steps(context, can_gc);
125        }
126    }
127
128    /// Called when a Node is appended to a tree.
129    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
130        if let Some(s) = self.super_type() {
131            s.bind_to_tree(cx, context);
132        }
133    }
134
135    /// Called when a Node is removed from a tree.
136    /// Implements removing steps:
137    /// <https://dom.spec.whatwg.org/#concept-node-remove-ext>
138    fn unbind_from_tree(&self, context: &UnbindContext, can_gc: CanGc) {
139        if let Some(s) = self.super_type() {
140            s.unbind_from_tree(context, can_gc);
141        }
142    }
143
144    /// Called on the parent when its children are changed.
145    fn children_changed(&self, cx: &mut JSContext, mutation: &ChildrenMutation) {
146        if let Some(s) = self.super_type() {
147            s.children_changed(cx, mutation);
148        }
149    }
150
151    /// Called during event dispatch after the bubbling phase completes.
152    fn handle_event(&self, event: &Event, can_gc: CanGc) {
153        if let Some(s) = self.super_type() {
154            s.handle_event(event, can_gc);
155        }
156    }
157
158    /// <https://html.spec.whatwg.org/multipage/#is-valid-command-steps>
159    fn is_valid_command_steps(&self, command: CommandState) -> bool {
160        self.super_type()
161            .is_some_and(|super_type| super_type.is_valid_command_steps(command))
162    }
163
164    /// <https://html.spec.whatwg.org/multipage/#command-steps>
165    fn command_steps(
166        &self,
167        button: DomRoot<HTMLButtonElement>,
168        command: CommandState,
169        can_gc: CanGc,
170    ) -> bool {
171        self.super_type()
172            .is_some_and(|super_type| super_type.command_steps(button, command, can_gc))
173    }
174
175    /// <https://dom.spec.whatwg.org/#concept-node-adopt-ext>
176    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
177        if let Some(s) = self.super_type() {
178            s.adopting_steps(cx, old_doc);
179        }
180    }
181
182    /// <https://dom.spec.whatwg.org/#concept-node-clone-ext>
183    fn cloning_steps(
184        &self,
185        cx: &mut JSContext,
186        copy: &Node,
187        maybe_doc: Option<&Document>,
188        clone_children: CloneChildrenFlag,
189    ) {
190        if let Some(s) = self.super_type() {
191            s.cloning_steps(cx, copy, maybe_doc, clone_children);
192        }
193    }
194
195    /// Called on an element when it is popped off the stack of open elements
196    /// of a parser.
197    fn pop(&self) {
198        if let Some(s) = self.super_type() {
199            s.pop();
200        }
201    }
202}
203
204/// Obtain a VirtualMethods instance for a given Node-derived object. Any
205/// method call on the trait object will invoke the corresponding method on the
206/// concrete type, propagating up the parent hierarchy unless otherwise
207/// interrupted.
208pub(crate) fn vtable_for(node: &Node) -> &dyn VirtualMethods {
209    match node.type_id() {
210        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAnchorElement)) => {
211            node.downcast::<HTMLAnchorElement>().unwrap() as &dyn VirtualMethods
212        },
213        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAreaElement)) => {
214            node.downcast::<HTMLAreaElement>().unwrap() as &dyn VirtualMethods
215        },
216        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBaseElement)) => {
217            node.downcast::<HTMLBaseElement>().unwrap() as &dyn VirtualMethods
218        },
219        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBodyElement)) => {
220            node.downcast::<HTMLBodyElement>().unwrap() as &dyn VirtualMethods
221        },
222        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLButtonElement)) => {
223            node.downcast::<HTMLButtonElement>().unwrap() as &dyn VirtualMethods
224        },
225        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLCanvasElement)) => {
226            node.downcast::<HTMLCanvasElement>().unwrap() as &dyn VirtualMethods
227        },
228        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLDetailsElement)) => {
229            node.downcast::<HTMLDetailsElement>().unwrap() as &dyn VirtualMethods
230        },
231        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLDialogElement)) => {
232            node.downcast::<HTMLDialogElement>().unwrap() as &dyn VirtualMethods
233        },
234        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFieldSetElement)) => {
235            node.downcast::<HTMLFieldSetElement>().unwrap() as &dyn VirtualMethods
236        },
237        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFontElement)) => {
238            node.downcast::<HTMLFontElement>().unwrap() as &dyn VirtualMethods
239        },
240        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFormElement)) => {
241            node.downcast::<HTMLFormElement>().unwrap() as &dyn VirtualMethods
242        },
243        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLHeadElement)) => {
244            node.downcast::<HTMLHeadElement>().unwrap() as &dyn VirtualMethods
245        },
246        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLHRElement)) => {
247            node.downcast::<HTMLHRElement>().unwrap() as &dyn VirtualMethods
248        },
249        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLImageElement)) => {
250            node.downcast::<HTMLImageElement>().unwrap() as &dyn VirtualMethods
251        },
252        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLIFrameElement)) => {
253            node.downcast::<HTMLIFrameElement>().unwrap() as &dyn VirtualMethods
254        },
255        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) => {
256            node.downcast::<HTMLInputElement>().unwrap() as &dyn VirtualMethods
257        },
258        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLabelElement)) => {
259            node.downcast::<HTMLLabelElement>().unwrap() as &dyn VirtualMethods
260        },
261        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLIElement)) => {
262            node.downcast::<HTMLLIElement>().unwrap() as &dyn VirtualMethods
263        },
264        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLinkElement)) => {
265            node.downcast::<HTMLLinkElement>().unwrap() as &dyn VirtualMethods
266        },
267        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMediaElement(
268            media_el,
269        ))) => match media_el {
270            HTMLMediaElementTypeId::HTMLVideoElement => {
271                node.downcast::<HTMLVideoElement>().unwrap() as &dyn VirtualMethods
272            },
273            _ => node.downcast::<HTMLMediaElement>().unwrap() as &dyn VirtualMethods,
274        },
275        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMetaElement)) => {
276            node.downcast::<HTMLMetaElement>().unwrap() as &dyn VirtualMethods
277        },
278        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMeterElement)) => {
279            node.downcast::<HTMLMeterElement>().unwrap() as &dyn VirtualMethods
280        },
281        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLObjectElement)) => {
282            node.downcast::<HTMLObjectElement>().unwrap() as &dyn VirtualMethods
283        },
284        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOptGroupElement)) => {
285            node.downcast::<HTMLOptGroupElement>().unwrap() as &dyn VirtualMethods
286        },
287        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOptionElement)) => {
288            node.downcast::<HTMLOptionElement>().unwrap() as &dyn VirtualMethods
289        },
290        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOutputElement)) => {
291            node.downcast::<HTMLOutputElement>().unwrap() as &dyn VirtualMethods
292        },
293        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLPreElement)) => {
294            node.downcast::<HTMLPreElement>().unwrap() as &dyn VirtualMethods
295        },
296        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLProgressElement)) => {
297            node.downcast::<HTMLProgressElement>().unwrap() as &dyn VirtualMethods
298        },
299        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLScriptElement)) => {
300            node.downcast::<HTMLScriptElement>().unwrap() as &dyn VirtualMethods
301        },
302        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSelectElement)) => {
303            node.downcast::<HTMLSelectElement>().unwrap() as &dyn VirtualMethods
304        },
305        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSourceElement)) => {
306            node.downcast::<HTMLSourceElement>().unwrap() as &dyn VirtualMethods
307        },
308        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSlotElement)) => {
309            node.downcast::<HTMLSlotElement>().unwrap() as &dyn VirtualMethods
310        },
311        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLStyleElement)) => {
312            node.downcast::<HTMLStyleElement>().unwrap() as &dyn VirtualMethods
313        },
314        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableElement)) => {
315            node.downcast::<HTMLTableElement>().unwrap() as &dyn VirtualMethods
316        },
317        NodeTypeId::Element(ElementTypeId::HTMLElement(
318            HTMLElementTypeId::HTMLTableCellElement,
319        )) => node.downcast::<HTMLTableCellElement>().unwrap() as &dyn VirtualMethods,
320        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableColElement)) => {
321            node.downcast::<HTMLTableColElement>().unwrap() as &dyn VirtualMethods
322        },
323        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableRowElement)) => {
324            node.downcast::<HTMLTableRowElement>().unwrap() as &dyn VirtualMethods
325        },
326        NodeTypeId::Element(ElementTypeId::HTMLElement(
327            HTMLElementTypeId::HTMLTableSectionElement,
328        )) => node.downcast::<HTMLTableSectionElement>().unwrap() as &dyn VirtualMethods,
329        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTemplateElement)) => {
330            node.downcast::<HTMLTemplateElement>().unwrap() as &dyn VirtualMethods
331        },
332        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTextAreaElement)) => {
333            node.downcast::<HTMLTextAreaElement>().unwrap() as &dyn VirtualMethods
334        },
335        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTitleElement)) => {
336            node.downcast::<HTMLTitleElement>().unwrap() as &dyn VirtualMethods
337        },
338        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
339            SVGGraphicsElementTypeId::SVGImageElement,
340        ))) => node.downcast::<SVGImageElement>().unwrap() as &dyn VirtualMethods,
341        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
342            SVGGraphicsElementTypeId::SVGSVGElement,
343        ))) => node.downcast::<SVGSVGElement>().unwrap() as &dyn VirtualMethods,
344        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGElement)) => {
345            node.downcast::<SVGElement>().unwrap() as &dyn VirtualMethods
346        },
347        NodeTypeId::Element(ElementTypeId::Element) => {
348            node.downcast::<Element>().unwrap() as &dyn VirtualMethods
349        },
350        NodeTypeId::Element(_) => node.downcast::<HTMLElement>().unwrap() as &dyn VirtualMethods,
351        NodeTypeId::DocumentFragment(DocumentFragmentTypeId::ShadowRoot) => {
352            node.downcast::<ShadowRoot>().unwrap() as &dyn VirtualMethods
353        },
354        NodeTypeId::DocumentFragment(_) => {
355            node.downcast::<DocumentFragment>().unwrap() as &dyn VirtualMethods
356        },
357        _ => node as &dyn VirtualMethods,
358    }
359}