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