Skip to main content

script/dom/node/
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 style::attr::AttrValue;
9
10use crate::dom::bindings::inheritance::{
11    Castable, DocumentFragmentTypeId, ElementTypeId, HTMLElementTypeId, HTMLMediaElementTypeId,
12    NodeTypeId, SVGElementTypeId, SVGGeometryElementTypeId, SVGGradientElementTypeId,
13    SVGGraphicsElementTypeId,
14};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::documentfragment::DocumentFragment;
18use crate::dom::element::attributes::storage::AttrRef;
19use crate::dom::element::{AttributeMutation, Element};
20use crate::dom::event::Event;
21use crate::dom::html::form_controls::htmlinputelement::HTMLInputElement;
22use crate::dom::html::htmlanchorelement::HTMLAnchorElement;
23use crate::dom::html::htmlareaelement::HTMLAreaElement;
24use crate::dom::html::htmlbaseelement::HTMLBaseElement;
25use crate::dom::html::htmlbodyelement::HTMLBodyElement;
26use crate::dom::html::htmlbuttonelement::HTMLButtonElement;
27use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
28use crate::dom::html::htmldetailselement::HTMLDetailsElement;
29use crate::dom::html::htmlelement::HTMLElement;
30use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
31use crate::dom::html::htmlfontelement::HTMLFontElement;
32use crate::dom::html::htmlformelement::HTMLFormElement;
33use crate::dom::html::htmlheadelement::HTMLHeadElement;
34use crate::dom::html::htmlhrelement::HTMLHRElement;
35use crate::dom::html::htmliframeelement::HTMLIFrameElement;
36use crate::dom::html::htmlimageelement::HTMLImageElement;
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::htmltrackelement::HTMLTrackElement;
63use crate::dom::html::htmlvideoelement::HTMLVideoElement;
64use crate::dom::htmlbuttonelement::CommandState;
65use crate::dom::htmldialogelement::HTMLDialogElement;
66use crate::dom::node::{
67    BindContext, ChildrenMutation, CloneChildrenFlag, MoveContext, Node, UnbindContext,
68};
69use crate::dom::shadowroot::ShadowRoot;
70use crate::dom::svg::svgcircleelement::SVGCircleElement;
71use crate::dom::svg::svgdefselement::SVGDefsElement;
72use crate::dom::svg::svgelement::SVGElement;
73use crate::dom::svg::svgellipseelement::SVGEllipseElement;
74use crate::dom::svg::svggelement::SVGGElement;
75use crate::dom::svg::svgimageelement::SVGImageElement;
76use crate::dom::svg::svglineargradientelement::SVGLinearGradientElement;
77use crate::dom::svg::svglineelement::SVGLineElement;
78use crate::dom::svg::svgpathelement::SVGPathElement;
79use crate::dom::svg::svgpolygonelement::SVGPolygonElement;
80use crate::dom::svg::svgpolylineelement::SVGPolylineElement;
81use crate::dom::svg::svgradialgradientelement::SVGRadialGradientElement;
82use crate::dom::svg::svgrectelement::SVGRectElement;
83use crate::dom::svg::svgstopelement::SVGStopElement;
84use crate::dom::svg::svgsvgelement::SVGSVGElement;
85use crate::dom::svg::svgsymbolelement::SVGSymbolElement;
86use crate::dom::svg::svguseelement::SVGUseElement;
87
88/// Trait to allow DOM nodes to opt-in to overriding (or adding to) common
89/// behaviours. Replicates the effect of C++ virtual methods.
90pub(crate) trait VirtualMethods {
91    /// Returns self as the superclass of the implementation for this trait,
92    /// if any.
93    fn super_type(&self) -> Option<&dyn VirtualMethods>;
94
95    /// Called when attributes of a node are mutated.
96    /// <https://dom.spec.whatwg.org/#attribute-is-set>
97    /// <https://dom.spec.whatwg.org/#attribute-is-removed>
98    fn attribute_mutated(
99        &self,
100        cx: &mut js::context::JSContext,
101        attr: AttrRef<'_>,
102        mutation: AttributeMutation,
103    ) {
104        if let Some(s) = self.super_type() {
105            s.attribute_mutated(cx, attr, mutation);
106        }
107    }
108
109    /// Returns `true` if given attribute `attr` affects style of the
110    /// given element.
111    fn attribute_affects_presentational_hints(&self, attr: AttrRef<'_>) -> bool {
112        match self.super_type() {
113            Some(s) => s.attribute_affects_presentational_hints(attr),
114            None => false,
115        }
116    }
117
118    /// Returns the right AttrValue variant for the attribute with name `name`
119    /// on this element.
120    fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
121        match self.super_type() {
122            Some(s) => s.parse_plain_attribute(name, value),
123            _ => AttrValue::String(value.into()),
124        }
125    }
126
127    /// Invoked during a DOM tree mutation after a node becomes connected, once all
128    /// related DOM tree mutations have been applied.
129    /// <https://dom.spec.whatwg.org/#concept-node-post-connection-ext>
130    fn post_connection_steps(&self, cx: &mut JSContext) {
131        if let Some(s) = self.super_type() {
132            s.post_connection_steps(cx);
133        }
134    }
135
136    /// <https://dom.spec.whatwg.org/#concept-node-move-ext>
137    fn moving_steps(&self, cx: &mut JSContext, context: &MoveContext) {
138        if let Some(s) = self.super_type() {
139            s.moving_steps(cx, context);
140        }
141    }
142
143    /// Called when a Node is appended to a tree.
144    fn bind_to_tree(&self, cx: &mut JSContext, context: &BindContext) {
145        if let Some(s) = self.super_type() {
146            s.bind_to_tree(cx, context);
147        }
148    }
149
150    /// Called when a Node is removed from a tree.
151    /// Implements removing steps:
152    /// <https://dom.spec.whatwg.org/#concept-node-remove-ext>
153    fn unbind_from_tree(&self, cx: &mut js::context::JSContext, context: &UnbindContext) {
154        if let Some(s) = self.super_type() {
155            s.unbind_from_tree(cx, context);
156        }
157    }
158
159    /// Called on the parent when its children are changed.
160    fn children_changed(&self, cx: &mut JSContext, mutation: &ChildrenMutation) {
161        if let Some(s) = self.super_type() {
162            s.children_changed(cx, mutation);
163        }
164    }
165
166    /// Called during event dispatch after the bubbling phase completes.
167    fn handle_event(&self, cx: &mut js::context::JSContext, event: &Event) {
168        if let Some(s) = self.super_type() {
169            s.handle_event(cx, event);
170        }
171    }
172
173    /// <https://html.spec.whatwg.org/multipage/#is-valid-command-steps>
174    fn is_valid_command_steps(&self, command: CommandState) -> bool {
175        self.super_type()
176            .is_some_and(|super_type| super_type.is_valid_command_steps(command))
177    }
178
179    /// <https://html.spec.whatwg.org/multipage/#command-steps>
180    fn command_steps(
181        &self,
182        cx: &mut js::context::JSContext,
183        button: DomRoot<HTMLButtonElement>,
184        command: CommandState,
185    ) -> bool {
186        self.super_type()
187            .is_some_and(|super_type| super_type.command_steps(cx, button, command))
188    }
189
190    /// <https://dom.spec.whatwg.org/#concept-node-adopt-ext>
191    fn adopting_steps(&self, cx: &mut JSContext, old_doc: &Document) {
192        if let Some(s) = self.super_type() {
193            s.adopting_steps(cx, old_doc);
194        }
195    }
196
197    /// <https://dom.spec.whatwg.org/#concept-node-clone-ext>
198    fn cloning_steps(
199        &self,
200        cx: &mut JSContext,
201        copy: &Node,
202        maybe_doc: Option<&Document>,
203        clone_children: CloneChildrenFlag,
204    ) {
205        if let Some(s) = self.super_type() {
206            s.cloning_steps(cx, copy, maybe_doc, clone_children);
207        }
208    }
209
210    /// Called on an element when it is popped off the stack of open elements
211    /// of a parser.
212    fn pop(&self, cx: &mut js::context::JSContext) {
213        if let Some(s) = self.super_type() {
214            s.pop(cx);
215        }
216    }
217}
218
219/// Obtain a VirtualMethods instance for a given Node-derived object. Any
220/// method call on the trait object will invoke the corresponding method on the
221/// concrete type, propagating up the parent hierarchy unless otherwise
222/// interrupted.
223pub(crate) fn vtable_for(node: &Node) -> &dyn VirtualMethods {
224    match node.type_id() {
225        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAnchorElement)) => {
226            node.downcast::<HTMLAnchorElement>().unwrap() as &dyn VirtualMethods
227        },
228        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAreaElement)) => {
229            node.downcast::<HTMLAreaElement>().unwrap() as &dyn VirtualMethods
230        },
231        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBaseElement)) => {
232            node.downcast::<HTMLBaseElement>().unwrap() as &dyn VirtualMethods
233        },
234        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLBodyElement)) => {
235            node.downcast::<HTMLBodyElement>().unwrap() as &dyn VirtualMethods
236        },
237        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLButtonElement)) => {
238            node.downcast::<HTMLButtonElement>().unwrap() as &dyn VirtualMethods
239        },
240        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLCanvasElement)) => {
241            node.downcast::<HTMLCanvasElement>().unwrap() as &dyn VirtualMethods
242        },
243        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLDetailsElement)) => {
244            node.downcast::<HTMLDetailsElement>().unwrap() as &dyn VirtualMethods
245        },
246        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLDialogElement)) => {
247            node.downcast::<HTMLDialogElement>().unwrap() as &dyn VirtualMethods
248        },
249        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFieldSetElement)) => {
250            node.downcast::<HTMLFieldSetElement>().unwrap() as &dyn VirtualMethods
251        },
252        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFontElement)) => {
253            node.downcast::<HTMLFontElement>().unwrap() as &dyn VirtualMethods
254        },
255        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLFormElement)) => {
256            node.downcast::<HTMLFormElement>().unwrap() as &dyn VirtualMethods
257        },
258        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLHeadElement)) => {
259            node.downcast::<HTMLHeadElement>().unwrap() as &dyn VirtualMethods
260        },
261        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLHRElement)) => {
262            node.downcast::<HTMLHRElement>().unwrap() as &dyn VirtualMethods
263        },
264        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLImageElement)) => {
265            node.downcast::<HTMLImageElement>().unwrap() as &dyn VirtualMethods
266        },
267        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLIFrameElement)) => {
268            node.downcast::<HTMLIFrameElement>().unwrap() as &dyn VirtualMethods
269        },
270        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLInputElement)) => {
271            node.downcast::<HTMLInputElement>().unwrap() as &dyn VirtualMethods
272        },
273        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLabelElement)) => {
274            node.downcast::<HTMLLabelElement>().unwrap() as &dyn VirtualMethods
275        },
276        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLIElement)) => {
277            node.downcast::<HTMLLIElement>().unwrap() as &dyn VirtualMethods
278        },
279        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLLinkElement)) => {
280            node.downcast::<HTMLLinkElement>().unwrap() as &dyn VirtualMethods
281        },
282        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMediaElement(
283            media_el,
284        ))) => match media_el {
285            HTMLMediaElementTypeId::HTMLVideoElement => {
286                node.downcast::<HTMLVideoElement>().unwrap() as &dyn VirtualMethods
287            },
288            _ => node.downcast::<HTMLMediaElement>().unwrap() as &dyn VirtualMethods,
289        },
290        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMetaElement)) => {
291            node.downcast::<HTMLMetaElement>().unwrap() as &dyn VirtualMethods
292        },
293        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLMeterElement)) => {
294            node.downcast::<HTMLMeterElement>().unwrap() as &dyn VirtualMethods
295        },
296        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLObjectElement)) => {
297            node.downcast::<HTMLObjectElement>().unwrap() as &dyn VirtualMethods
298        },
299        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOptGroupElement)) => {
300            node.downcast::<HTMLOptGroupElement>().unwrap() as &dyn VirtualMethods
301        },
302        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOptionElement)) => {
303            node.downcast::<HTMLOptionElement>().unwrap() as &dyn VirtualMethods
304        },
305        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLOutputElement)) => {
306            node.downcast::<HTMLOutputElement>().unwrap() as &dyn VirtualMethods
307        },
308        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLPreElement)) => {
309            node.downcast::<HTMLPreElement>().unwrap() as &dyn VirtualMethods
310        },
311        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLProgressElement)) => {
312            node.downcast::<HTMLProgressElement>().unwrap() as &dyn VirtualMethods
313        },
314        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLScriptElement)) => {
315            node.downcast::<HTMLScriptElement>().unwrap() as &dyn VirtualMethods
316        },
317        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSelectElement)) => {
318            node.downcast::<HTMLSelectElement>().unwrap() as &dyn VirtualMethods
319        },
320        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSourceElement)) => {
321            node.downcast::<HTMLSourceElement>().unwrap() as &dyn VirtualMethods
322        },
323        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLSlotElement)) => {
324            node.downcast::<HTMLSlotElement>().unwrap() as &dyn VirtualMethods
325        },
326        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLStyleElement)) => {
327            node.downcast::<HTMLStyleElement>().unwrap() as &dyn VirtualMethods
328        },
329        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableElement)) => {
330            node.downcast::<HTMLTableElement>().unwrap() as &dyn VirtualMethods
331        },
332        NodeTypeId::Element(ElementTypeId::HTMLElement(
333            HTMLElementTypeId::HTMLTableCellElement,
334        )) => node.downcast::<HTMLTableCellElement>().unwrap() as &dyn VirtualMethods,
335        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableColElement)) => {
336            node.downcast::<HTMLTableColElement>().unwrap() as &dyn VirtualMethods
337        },
338        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTableRowElement)) => {
339            node.downcast::<HTMLTableRowElement>().unwrap() as &dyn VirtualMethods
340        },
341        NodeTypeId::Element(ElementTypeId::HTMLElement(
342            HTMLElementTypeId::HTMLTableSectionElement,
343        )) => node.downcast::<HTMLTableSectionElement>().unwrap() as &dyn VirtualMethods,
344        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTemplateElement)) => {
345            node.downcast::<HTMLTemplateElement>().unwrap() as &dyn VirtualMethods
346        },
347        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTextAreaElement)) => {
348            node.downcast::<HTMLTextAreaElement>().unwrap() as &dyn VirtualMethods
349        },
350        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTitleElement)) => {
351            node.downcast::<HTMLTitleElement>().unwrap() as &dyn VirtualMethods
352        },
353        NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTrackElement)) => {
354            node.downcast::<HTMLTrackElement>().unwrap() as &dyn VirtualMethods
355        },
356        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
357            SVGGraphicsElementTypeId::SVGGeometryElement(
358                SVGGeometryElementTypeId::SVGCircleElement,
359            ),
360        ))) => node.downcast::<SVGCircleElement>().unwrap() as &dyn VirtualMethods,
361        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
362            SVGGraphicsElementTypeId::SVGGeometryElement(
363                SVGGeometryElementTypeId::SVGEllipseElement,
364            ),
365        ))) => node.downcast::<SVGEllipseElement>().unwrap() as &dyn VirtualMethods,
366        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
367            SVGGraphicsElementTypeId::SVGImageElement,
368        ))) => node.downcast::<SVGImageElement>().unwrap() as &dyn VirtualMethods,
369        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
370            SVGGraphicsElementTypeId::SVGGeometryElement(SVGGeometryElementTypeId::SVGRectElement),
371        ))) => node.downcast::<SVGRectElement>().unwrap() as &dyn VirtualMethods,
372        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
373            SVGGraphicsElementTypeId::SVGGeometryElement(SVGGeometryElementTypeId::SVGLineElement),
374        ))) => node.downcast::<SVGLineElement>().unwrap() as &dyn VirtualMethods,
375        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
376            SVGGraphicsElementTypeId::SVGGeometryElement(
377                SVGGeometryElementTypeId::SVGPolylineElement,
378            ),
379        ))) => node.downcast::<SVGPolylineElement>().unwrap() as &dyn VirtualMethods,
380        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
381            SVGGraphicsElementTypeId::SVGGeometryElement(
382                SVGGeometryElementTypeId::SVGPolygonElement,
383            ),
384        ))) => node.downcast::<SVGPolygonElement>().unwrap() as &dyn VirtualMethods,
385        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
386            SVGGraphicsElementTypeId::SVGGeometryElement(SVGGeometryElementTypeId::SVGPathElement),
387        ))) => node.downcast::<SVGPathElement>().unwrap() as &dyn VirtualMethods,
388        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
389            SVGGraphicsElementTypeId::SVGSVGElement,
390        ))) => node.downcast::<SVGSVGElement>().unwrap() as &dyn VirtualMethods,
391        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
392            SVGGraphicsElementTypeId::SVGGElement,
393        ))) => node.downcast::<SVGGElement>().unwrap() as &dyn VirtualMethods,
394        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
395            SVGGraphicsElementTypeId::SVGUseElement,
396        ))) => node.downcast::<SVGUseElement>().unwrap() as &dyn VirtualMethods,
397        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
398            SVGGraphicsElementTypeId::SVGSymbolElement,
399        ))) => node.downcast::<SVGSymbolElement>().unwrap() as &dyn VirtualMethods,
400        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(
401            SVGGraphicsElementTypeId::SVGDefsElement,
402        ))) => node.downcast::<SVGDefsElement>().unwrap() as &dyn VirtualMethods,
403        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGradientElement(
404            SVGGradientElementTypeId::SVGLinearGradientElement,
405        ))) => node.downcast::<SVGLinearGradientElement>().unwrap() as &dyn VirtualMethods,
406        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGGradientElement(
407            SVGGradientElementTypeId::SVGRadialGradientElement,
408        ))) => node.downcast::<SVGRadialGradientElement>().unwrap() as &dyn VirtualMethods,
409        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGStopElement)) => {
410            node.downcast::<SVGStopElement>().unwrap() as &dyn VirtualMethods
411        },
412        NodeTypeId::Element(ElementTypeId::SVGElement(SVGElementTypeId::SVGElement)) => {
413            node.downcast::<SVGElement>().unwrap() as &dyn VirtualMethods
414        },
415        NodeTypeId::Element(ElementTypeId::Element) => {
416            node.downcast::<Element>().unwrap() as &dyn VirtualMethods
417        },
418        NodeTypeId::Element(_) => node.downcast::<HTMLElement>().unwrap() as &dyn VirtualMethods,
419        NodeTypeId::DocumentFragment(DocumentFragmentTypeId::ShadowRoot) => {
420            node.downcast::<ShadowRoot>().unwrap() as &dyn VirtualMethods
421        },
422        NodeTypeId::DocumentFragment(_) => {
423            node.downcast::<DocumentFragment>().unwrap() as &dyn VirtualMethods
424        },
425        _ => node as &dyn VirtualMethods,
426    }
427}