style/dom.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
5//! Types and traits used to access the DOM from style calculation.
6
7#![allow(unsafe_code)]
8#![deny(missing_docs)]
9
10use crate::applicable_declarations::ApplicableDeclarationBlock;
11#[cfg(feature = "gecko")]
12use crate::context::UpdateAnimationsTasks;
13use crate::context::{SharedStyleContext, TreeCountingCaches};
14use crate::data::{ElementData, ElementDataMut, ElementDataRef};
15use crate::device::Device;
16use crate::properties::{AnimationDeclarations, ComputedValues, PropertyDeclarationBlock};
17use crate::selector_map::PrecomputedHashMap;
18use crate::selector_parser::{AttrValue, Lang, PseudoElement, RestyleDamage, SelectorImpl};
19use crate::shared_lock::{Locked, SharedRwLock};
20use crate::stylesheets::scope_rule::ImplicitScopeRoot;
21use crate::stylist::CascadeData;
22use crate::values::AtomIdent;
23use crate::values::computed::{Display, TreeCountingResult};
24use crate::{LocalName, Namespace, WeakAtom};
25use dom::ElementState;
26use selectors::matching::{ElementSelectorFlags, QuirksMode, VisitedHandlingMode};
27use selectors::sink::Push;
28use selectors::{Element as SelectorsElement, OpaqueElement};
29use servo_arc::{Arc, ArcBorrow};
30use smallvec::SmallVec;
31use std::fmt;
32use std::fmt::Debug;
33use std::hash::Hash;
34use std::ops::Deref;
35
36pub use style_traits::dom::OpaqueNode;
37
38/// Simple trait to provide basic information about the type of an element.
39///
40/// We avoid exposing the full type id, since computing it in the general case
41/// would be difficult for Gecko nodes.
42pub trait NodeInfo {
43 /// Whether this node is an element.
44 fn is_element(&self) -> bool;
45 /// Whether this node is a text node.
46 fn is_text_node(&self) -> bool;
47}
48
49/// A node iterator that only returns node that don't need layout.
50pub struct LayoutIterator<T>(pub T);
51
52impl<T, N> Iterator for LayoutIterator<T>
53where
54 T: Iterator<Item = N>,
55 N: NodeInfo,
56{
57 type Item = N;
58
59 fn next(&mut self) -> Option<N> {
60 loop {
61 let n = self.0.next()?;
62 // Filter out nodes that layout should ignore.
63 if n.is_text_node() || n.is_element() {
64 return Some(n);
65 }
66 }
67 }
68}
69
70/// An iterator over the DOM children of a node.
71pub struct DomChildren<N>(Option<N>);
72impl<N> Iterator for DomChildren<N>
73where
74 N: TNode,
75{
76 type Item = N;
77
78 fn next(&mut self) -> Option<N> {
79 let n = self.0.take()?;
80 self.0 = n.next_sibling();
81 Some(n)
82 }
83}
84
85/// An iterator over the DOM descendants of a node in pre-order.
86pub struct DomDescendants<N> {
87 previous: Option<N>,
88 scope: N,
89}
90
91impl<N> DomDescendants<N>
92where
93 N: TNode,
94{
95 /// Returns the next element ignoring all of our subtree.
96 #[inline]
97 pub fn next_skipping_children(&mut self) -> Option<N> {
98 let prev = self.previous.take()?;
99 self.previous = prev.next_in_preorder_skipping_children(self.scope);
100 self.previous
101 }
102}
103
104impl<N> Iterator for DomDescendants<N>
105where
106 N: TNode,
107{
108 type Item = N;
109
110 #[inline]
111 fn next(&mut self) -> Option<N> {
112 let prev = self.previous.take()?;
113 self.previous = prev.next_in_preorder(self.scope);
114 self.previous
115 }
116}
117
118/// The `TDocument` trait, to represent a document node.
119pub trait TDocument: Sized + Copy + Clone {
120 /// The concrete `TNode` type.
121 type ConcreteNode: TNode<ConcreteDocument = Self>;
122
123 /// Get this document as a `TNode`.
124 fn as_node(&self) -> Self::ConcreteNode;
125
126 /// Returns whether this document is an HTML document.
127 fn is_html_document(&self) -> bool;
128
129 /// Returns the quirks mode of this document.
130 fn quirks_mode(&self) -> QuirksMode;
131
132 /// Get a list of elements with a given ID in this document, sorted by
133 /// tree position.
134 ///
135 /// Can return an error to signal that this list is not available, or also
136 /// return an empty slice.
137 fn elements_with_id<'a>(
138 &self,
139 _id: &AtomIdent,
140 ) -> Result<&'a [<Self::ConcreteNode as TNode>::ConcreteElement], ()>
141 where
142 Self: 'a,
143 {
144 Err(())
145 }
146
147 /// This document's shared lock.
148 fn shared_lock(&self) -> &SharedRwLock;
149}
150
151/// The `TNode` trait. This is the main generic trait over which the style
152/// system can be implemented.
153pub trait TNode: Sized + Copy + Clone + Debug + NodeInfo + PartialEq {
154 /// The concrete `TElement` type.
155 type ConcreteElement: TElement<ConcreteNode = Self>;
156
157 /// The concrete `TDocument` type.
158 type ConcreteDocument: TDocument<ConcreteNode = Self>;
159
160 /// The concrete `TShadowRoot` type.
161 type ConcreteShadowRoot: TShadowRoot<ConcreteNode = Self>;
162
163 /// Get this node's parent node.
164 fn parent_node(&self) -> Option<Self>;
165
166 /// Get this node's first child.
167 fn first_child(&self) -> Option<Self>;
168
169 /// Get this node's last child.
170 fn last_child(&self) -> Option<Self>;
171
172 /// Get this node's previous sibling.
173 fn prev_sibling(&self) -> Option<Self>;
174
175 /// Get this node's next sibling.
176 fn next_sibling(&self) -> Option<Self>;
177
178 /// Get the owner document of this node.
179 fn owner_doc(&self) -> Self::ConcreteDocument;
180
181 /// Iterate over the DOM children of a node.
182 #[inline(always)]
183 fn dom_children(&self) -> DomChildren<Self> {
184 DomChildren(self.first_child())
185 }
186
187 /// Returns whether the node is attached to a document.
188 fn is_in_document(&self) -> bool;
189
190 /// Iterate over the DOM children of a node, in preorder.
191 #[inline(always)]
192 fn dom_descendants(&self) -> DomDescendants<Self> {
193 DomDescendants {
194 previous: Some(*self),
195 scope: *self,
196 }
197 }
198
199 /// Returns the next node after this one, in a pre-order tree-traversal of
200 /// the subtree rooted at scoped_to.
201 #[inline]
202 fn next_in_preorder(&self, scoped_to: Self) -> Option<Self> {
203 if let Some(c) = self.first_child() {
204 return Some(c);
205 }
206 self.next_in_preorder_skipping_children(scoped_to)
207 }
208
209 /// Returns the next node in tree order, skipping the children of the current node.
210 ///
211 /// This is useful when we know that a subtree cannot contain matches, allowing us
212 /// to skip entire subtrees during traversal.
213 #[inline]
214 fn next_in_preorder_skipping_children(&self, scoped_to: Self) -> Option<Self> {
215 let mut current = *self;
216 loop {
217 if current == scoped_to {
218 return None;
219 }
220
221 if let Some(s) = current.next_sibling() {
222 return Some(s);
223 }
224
225 debug_assert!(
226 current.parent_node().is_some(),
227 "Not a descendant of the scope?"
228 );
229 current = current.parent_node()?;
230 }
231 }
232
233 /// Returns the depth of this node in the DOM.
234 fn depth(&self) -> usize {
235 let mut depth = 0;
236 let mut curr = *self;
237 while let Some(parent) = curr.traversal_parent() {
238 depth += 1;
239 curr = parent.as_node();
240 }
241 depth
242 }
243
244 /// Get this node's parent element from the perspective of a restyle
245 /// traversal.
246 fn traversal_parent(&self) -> Option<Self::ConcreteElement>;
247
248 /// Get this node's parent element if present.
249 fn parent_element(&self) -> Option<Self::ConcreteElement> {
250 self.parent_node().and_then(|n| n.as_element())
251 }
252
253 /// Get this node's parent element, or shadow host if it's a shadow root.
254 fn parent_element_or_host(&self) -> Option<Self::ConcreteElement> {
255 let parent = self.parent_node()?;
256 if let Some(e) = parent.as_element() {
257 return Some(e);
258 }
259 if let Some(root) = parent.as_shadow_root() {
260 return Some(root.host());
261 }
262 None
263 }
264
265 /// Converts self into an `OpaqueNode`.
266 fn opaque(&self) -> OpaqueNode;
267
268 /// A debug id, only useful, mm... for debugging.
269 fn debug_id(self) -> usize;
270
271 /// Get this node as an element, if it's one.
272 fn as_element(&self) -> Option<Self::ConcreteElement>;
273
274 /// Get this node as a document, if it's one.
275 fn as_document(&self) -> Option<Self::ConcreteDocument>;
276
277 /// Get this node as a ShadowRoot, if it's one.
278 fn as_shadow_root(&self) -> Option<Self::ConcreteShadowRoot>;
279}
280
281/// Wrapper to output the subtree rather than the single node when formatting
282/// for Debug.
283pub struct ShowSubtree<N: TNode>(pub N);
284impl<N: TNode> Debug for ShowSubtree<N> {
285 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
286 writeln!(f, "DOM Subtree:")?;
287 fmt_subtree(f, &|f, n| write!(f, "{:?}", n), self.0, 1)
288 }
289}
290
291/// Wrapper to output the subtree along with the ElementData when formatting
292/// for Debug.
293pub struct ShowSubtreeData<N: TNode>(pub N);
294impl<N: TNode> Debug for ShowSubtreeData<N> {
295 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
296 writeln!(f, "DOM Subtree:")?;
297 fmt_subtree(f, &|f, n| fmt_with_data(f, n), self.0, 1)
298 }
299}
300
301/// Wrapper to output the subtree along with the ElementData and primary
302/// ComputedValues when formatting for Debug. This is extremely verbose.
303#[cfg(feature = "servo")]
304pub struct ShowSubtreeDataAndPrimaryValues<N: TNode>(pub N);
305#[cfg(feature = "servo")]
306impl<N: TNode> Debug for ShowSubtreeDataAndPrimaryValues<N> {
307 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
308 writeln!(f, "DOM Subtree:")?;
309 fmt_subtree(f, &|f, n| fmt_with_data_and_primary_values(f, n), self.0, 1)
310 }
311}
312
313fn fmt_with_data<N: TNode>(f: &mut fmt::Formatter, n: N) -> fmt::Result {
314 if let Some(el) = n.as_element() {
315 write!(
316 f,
317 "{:?} dd={} aodd={} data={:?}",
318 el,
319 el.has_dirty_descendants(),
320 el.has_animation_only_dirty_descendants(),
321 el.borrow_data(),
322 )
323 } else {
324 write!(f, "{:?}", n)
325 }
326}
327
328#[cfg(feature = "servo")]
329fn fmt_with_data_and_primary_values<N: TNode>(f: &mut fmt::Formatter, n: N) -> fmt::Result {
330 if let Some(el) = n.as_element() {
331 let dd = el.has_dirty_descendants();
332 let aodd = el.has_animation_only_dirty_descendants();
333 let data = el.borrow_data();
334 let values = data.as_ref().and_then(|d| d.styles.get_primary());
335 write!(
336 f,
337 "{:?} dd={} aodd={} data={:?} values={:?}",
338 el, dd, aodd, &data, values
339 )
340 } else {
341 write!(f, "{:?}", n)
342 }
343}
344
345fn fmt_subtree<F, N: TNode>(f: &mut fmt::Formatter, stringify: &F, n: N, indent: u32) -> fmt::Result
346where
347 F: Fn(&mut fmt::Formatter, N) -> fmt::Result,
348{
349 for _ in 0..indent {
350 write!(f, " ")?;
351 }
352 stringify(f, n)?;
353 if let Some(e) = n.as_element() {
354 for kid in e.traversal_children() {
355 writeln!(f)?;
356 fmt_subtree(f, stringify, kid, indent + 1)?;
357 }
358 }
359
360 Ok(())
361}
362
363/// The ShadowRoot trait.
364pub trait TShadowRoot: Sized + Copy + Clone + Debug + PartialEq {
365 /// The concrete node type.
366 type ConcreteNode: TNode<ConcreteShadowRoot = Self>;
367
368 /// Get this ShadowRoot as a node.
369 fn as_node(&self) -> Self::ConcreteNode;
370
371 /// Get the shadow host that hosts this ShadowRoot.
372 fn host(&self) -> <Self::ConcreteNode as TNode>::ConcreteElement;
373
374 /// Get the style data for this ShadowRoot.
375 fn style_data<'a>(&self) -> Option<&'a CascadeData>
376 where
377 Self: 'a;
378
379 /// Get the list of shadow parts for this shadow root.
380 fn parts<'a>(&self) -> &[<Self::ConcreteNode as TNode>::ConcreteElement]
381 where
382 Self: 'a,
383 {
384 &[]
385 }
386
387 /// Get a list of elements with a given ID in this shadow root, sorted by
388 /// tree position.
389 ///
390 /// Can return an error to signal that this list is not available, or also
391 /// return an empty slice.
392 fn elements_with_id<'a>(
393 &self,
394 _id: &AtomIdent,
395 ) -> Result<&'a [<Self::ConcreteNode as TNode>::ConcreteElement], ()>
396 where
397 Self: 'a,
398 {
399 Err(())
400 }
401
402 /// Get the implicit scope for a stylesheet in given index.
403 fn implicit_scope_for_sheet(&self, _sheet_index: usize) -> Option<ImplicitScopeRoot> {
404 None
405 }
406}
407
408/// The element trait, the main abstraction the style crate acts over.
409pub trait TElement:
410 Eq
411 + PartialEq
412 + Debug
413 + Hash
414 + Sized
415 + Copy
416 + Clone
417 + SelectorsElement<Impl = SelectorImpl>
418 + ElementContext
419{
420 /// The concrete node type.
421 type ConcreteNode: TNode<ConcreteElement = Self>;
422
423 /// A concrete children iterator type in order to iterate over the `Node`s.
424 ///
425 /// TODO(emilio): We should eventually replace this with the `impl Trait`
426 /// syntax.
427 type TraversalChildrenIterator: Iterator<Item = Self::ConcreteNode>;
428
429 /// Get this element as a node.
430 fn as_node(&self) -> Self::ConcreteNode;
431
432 /// A debug-only check that the device's owner doc matches the actual doc
433 /// we're the root of.
434 ///
435 /// Otherwise we may set document-level state incorrectly, like the root
436 /// font-size used for rem units.
437 fn owner_doc_matches_for_testing(&self, _: &Device) -> bool {
438 true
439 }
440
441 /// Whether this element should match user and content rules.
442 ///
443 /// We use this for Native Anonymous Content in Gecko.
444 fn matches_user_and_content_rules(&self) -> bool {
445 true
446 }
447
448 /// Get this node's parent element from the perspective of a restyle
449 /// traversal.
450 fn traversal_parent(&self) -> Option<Self> {
451 self.as_node().traversal_parent()
452 }
453
454 /// Get this node's children from the perspective of a restyle traversal.
455 fn traversal_children(&self) -> LayoutIterator<Self::TraversalChildrenIterator>;
456
457 /// Returns the parent element we should inherit from.
458 ///
459 /// This is pretty much always the parent element itself, except in the case
460 /// of Gecko's Native Anonymous Content, which uses the traversal parent
461 /// (i.e. the flattened tree parent) and which also may need to find the
462 /// closest non-NAC ancestor.
463 fn inheritance_parent(&self) -> Option<Self> {
464 self.parent_element()
465 }
466
467 /// Execute `f` for each anonymous content child (apart from ::before and
468 /// ::after) whose originating element is `self`.
469 fn each_anonymous_content_child<F>(&self, _f: F)
470 where
471 F: FnMut(Self),
472 {
473 }
474
475 /// Return whether this element is an element in the HTML namespace.
476 fn is_html_element(&self) -> bool;
477
478 /// Return whether this element is an element in the MathML namespace.
479 fn is_mathml_element(&self) -> bool;
480
481 /// Return whether this element is an element in the SVG namespace.
482 fn is_svg_element(&self) -> bool;
483
484 /// Return whether this element is an element in the XUL namespace.
485 fn is_xul_element(&self) -> bool {
486 false
487 }
488
489 /// Return whether this element is an HTML <video> or <audio> element.
490 fn is_html_media_element(&self) -> bool {
491 false
492 }
493
494 /// Return the list of slotted nodes of this node.
495 fn slotted_nodes(&self) -> &[Self::ConcreteNode] {
496 &[]
497 }
498
499 /// Get this element's style attribute.
500 fn style_attribute(&self) -> Option<ArcBorrow<'_, Locked<PropertyDeclarationBlock>>>;
501
502 /// Get this element's SMIL override declarations.
503 fn smil_override(&self) -> Option<ArcBorrow<'_, Locked<PropertyDeclarationBlock>>> {
504 None
505 }
506
507 /// Get the combined animation and transition rules.
508 ///
509 /// FIXME(emilio): Is this really useful?
510 fn animation_declarations(&self, context: &SharedStyleContext) -> AnimationDeclarations {
511 if !self.may_have_animations() {
512 return Default::default();
513 }
514
515 AnimationDeclarations {
516 animations: self.animation_rule(context),
517 transitions: self.transition_rule(context),
518 }
519 }
520
521 /// Get this element's animation rule.
522 fn animation_rule(
523 &self,
524 _: &SharedStyleContext,
525 ) -> Option<Arc<Locked<PropertyDeclarationBlock>>>;
526
527 /// Get this element's transition rule.
528 fn transition_rule(
529 &self,
530 context: &SharedStyleContext,
531 ) -> Option<Arc<Locked<PropertyDeclarationBlock>>>;
532
533 /// Get this element's state, for non-tree-structural pseudos.
534 fn state(&self) -> ElementState;
535
536 /// Returns whether this element has a `part` attribute.
537 fn has_part_attr(&self) -> bool;
538
539 /// Returns whether this element exports any part from its shadow tree.
540 fn exports_any_part(&self) -> bool;
541
542 /// The ID for this element.
543 fn id(&self) -> Option<&WeakAtom>;
544
545 /// Internal iterator for the classes of this element.
546 fn each_class<F>(&self, callback: F)
547 where
548 F: FnMut(&AtomIdent);
549
550 /// Internal iterator for the classes of this element.
551 fn each_custom_state<F>(&self, callback: F)
552 where
553 F: FnMut(&AtomIdent);
554
555 /// Internal iterator for the part names of this element.
556 fn each_part<F>(&self, _callback: F)
557 where
558 F: FnMut(&AtomIdent),
559 {
560 }
561
562 /// Internal iterator for the attribute names of this element.
563 fn each_attr_name<F>(&self, callback: F)
564 where
565 F: FnMut(&LocalName);
566
567 /// Internal iterator for the part names that this element exports for a
568 /// given part name.
569 fn each_exported_part<F>(&self, _name: &AtomIdent, _callback: F)
570 where
571 F: FnMut(&AtomIdent),
572 {
573 }
574
575 /// Whether a given element may generate a pseudo-element.
576 ///
577 /// This is useful to avoid computing, for example, pseudo styles for
578 /// `::-first-line` or `::-first-letter`, when we know it won't affect us.
579 ///
580 /// TODO(emilio, bz): actually implement the logic for it.
581 fn may_generate_pseudo(&self, pseudo: &PseudoElement, _primary_style: &ComputedValues) -> bool {
582 // ::before/::after are always supported for now, though we could try to
583 // optimize out leaf elements.
584
585 // ::first-letter and ::first-line are only supported for block-inside
586 // things, and only in Gecko, not Servo. Unfortunately, Gecko has
587 // block-inside things that might have any computed display value due to
588 // things like fieldsets, legends, etc. Need to figure out how this
589 // should work.
590 debug_assert!(
591 pseudo.is_eager(),
592 "Someone called may_generate_pseudo with a non-eager pseudo."
593 );
594 !pseudo.is_first_line() || crate::pref!("layout.css.first-line.enabled", gecko = true)
595 }
596
597 /// Returns true if this element may have a descendant needing style processing.
598 ///
599 /// Note that we cannot guarantee the existence of such an element, because
600 /// it may have been removed from the DOM between marking it for restyle and
601 /// the actual restyle traversal.
602 fn has_dirty_descendants(&self) -> bool;
603
604 /// Returns whether state or attributes that may change style have changed
605 /// on the element, and thus whether the element has been snapshotted to do
606 /// restyle hint computation.
607 fn has_snapshot(&self) -> bool;
608
609 /// Returns whether the current snapshot if present has been handled.
610 fn handled_snapshot(&self) -> bool;
611
612 /// Flags this element as having handled already its snapshot.
613 unsafe fn set_handled_snapshot(&self);
614
615 /// A unique value for the element to use in the generation of random base values
616 /// for element-scoped CSS `random()` functions.
617 fn random_seed(&self) -> u64 {
618 // By default, use the element's own address as the random-influencing value.
619 self.opaque().to_ptr().as_ptr() as u64
620 }
621
622 /// Returns whether the element's styles are up-to-date after traversal
623 /// (i.e. in post traversal).
624 fn has_current_styles(&self, data: &ElementData) -> bool {
625 if self.has_snapshot() && !self.handled_snapshot() {
626 return false;
627 }
628
629 data.has_styles() &&
630 // TODO(hiro): When an animating element moved into subtree of
631 // contenteditable element, there remains animation restyle hints in
632 // post traversal. It's generally harmless since the hints will be
633 // processed in a next styling but ideally it should be processed soon.
634 //
635 // Without this, we get failures in:
636 // layout/style/crashtests/1383319.html
637 // layout/style/crashtests/1383001.html
638 //
639 // https://bugzilla.mozilla.org/show_bug.cgi?id=1389675 tracks fixing
640 // this.
641 !data.hint.has_non_animation_invalidations()
642 }
643
644 /// Flag that this element has a descendant for style processing.
645 ///
646 /// Only safe to call with exclusive access to the element.
647 unsafe fn set_dirty_descendants(&self);
648
649 /// Flag that this element has no descendant for style processing.
650 ///
651 /// Only safe to call with exclusive access to the element.
652 unsafe fn unset_dirty_descendants(&self);
653
654 /// Similar to the dirty_descendants but for representing a descendant of
655 /// the element needs to be updated in animation-only traversal.
656 fn has_animation_only_dirty_descendants(&self) -> bool {
657 false
658 }
659
660 /// Flag that this element has a descendant for animation-only restyle
661 /// processing.
662 ///
663 /// Only safe to call with exclusive access to the element.
664 unsafe fn set_animation_only_dirty_descendants(&self) {}
665
666 /// Flag that this element has no descendant for animation-only restyle processing.
667 ///
668 /// Only safe to call with exclusive access to the element.
669 unsafe fn unset_animation_only_dirty_descendants(&self) {}
670
671 /// Clear all bits related describing the dirtiness of descendants.
672 ///
673 /// In Gecko, this corresponds to the regular dirty descendants bit, the
674 /// animation-only dirty descendants bit, and the lazy frame construction
675 /// descendants bit.
676 unsafe fn clear_descendant_bits(&self) {
677 unsafe {
678 self.unset_dirty_descendants();
679 }
680 }
681
682 /// Returns true if this element is a visited link.
683 ///
684 /// Servo doesn't support visited styles yet.
685 fn is_visited_link(&self) -> bool {
686 false
687 }
688
689 /// Returns the pseudo-element implemented by this element, if any.
690 ///
691 /// Gecko traverses pseudo-elements during the style traversal, and we need
692 /// to know this so we can properly grab the pseudo-element style from the
693 /// parent element.
694 ///
695 /// Note that we still need to compute the pseudo-elements before-hand,
696 /// given otherwise we don't know if we need to create an element or not.
697 fn implemented_pseudo_element(&self) -> Option<PseudoElement> {
698 None
699 }
700
701 /// Atomically stores the number of children of this node that we will
702 /// need to process during bottom-up traversal.
703 fn store_children_to_process(&self, n: isize);
704
705 /// Atomically notes that a child has been processed during bottom-up
706 /// traversal. Returns the number of children left to process.
707 fn did_process_child(&self) -> isize;
708
709 /// Gets a reference to the ElementData container, or creates one.
710 ///
711 /// Unsafe because it can race to allocate and leak if not used with
712 /// exclusive access to the element.
713 unsafe fn ensure_data(&self) -> ElementDataMut<'_>;
714
715 /// Clears the element data reference, if any.
716 ///
717 /// Unsafe following the same reasoning as ensure_data.
718 unsafe fn clear_data(&self);
719
720 /// Whether there is an ElementData container.
721 fn has_data(&self) -> bool;
722
723 /// Immutably borrows the ElementData.
724 fn borrow_data(&self) -> Option<ElementDataRef<'_>>;
725
726 /// Mutably borrows the ElementData.
727 fn mutate_data(&self) -> Option<ElementDataMut<'_>>;
728
729 /// Whether we should skip any root- or item-based display property
730 /// blockification on this element. (This function exists so that Gecko
731 /// native anonymous content can opt out of this style fixup.)
732 fn skip_item_display_fixup(&self) -> bool;
733
734 /// Element has a flag that represents whether the element may have any type
735 /// of animations or not to bail out animation stuff early.
736 fn may_have_animations(&self) -> bool;
737
738 /// Called when an animation or transition set is stored for this element or one of its
739 /// pseudo-elements. Embedder should set `may_have_animations` flag on the element to true.
740 /// Gecko manages animation state itself, so only Servo embedders implement it.
741 #[cfg(feature = "servo")]
742 fn set_may_have_animations(&self);
743
744 /// Creates a task to update various animation state on a given (pseudo-)element.
745 #[cfg(feature = "gecko")]
746 fn update_animations(
747 &self,
748 before_change_style: Option<Arc<ComputedValues>>,
749 tasks: UpdateAnimationsTasks,
750 );
751
752 /// Returns true if the element has relevant animations. Relevant
753 /// animations are those animations that are affecting the element's style
754 /// or are scheduled to do so in the future.
755 fn has_animations(&self, context: &SharedStyleContext) -> bool;
756
757 /// Returns true if the element has a CSS animation. The `context` and `pseudo_element`
758 /// arguments are only used by Servo, since it stores animations globally and pseudo-elements
759 /// are not in the DOM.
760 fn has_css_animations(
761 &self,
762 context: &SharedStyleContext,
763 pseudo_element: Option<PseudoElement>,
764 ) -> bool;
765
766 /// Returns true if the element has a CSS transition (including running transitions and
767 /// completed transitions). The `context` and `pseudo_element` arguments are only used
768 /// by Servo, since it stores animations globally and pseudo-elements are not in the DOM.
769 fn has_css_transitions(
770 &self,
771 context: &SharedStyleContext,
772 pseudo_element: Option<PseudoElement>,
773 ) -> bool;
774
775 /// Returns true if the element has animation restyle hints.
776 fn has_animation_restyle_hints(&self) -> bool {
777 let data = match self.borrow_data() {
778 Some(d) => d,
779 None => return false,
780 };
781 data.hint.has_animation_hint()
782 }
783
784 /// Called when a highlight pseudo-element (::selection, ::highlight,
785 /// ::target-text) style is invalidated. These pseudos need explicit repaint
786 /// triggering since their styles are resolved lazily during painting.
787 fn note_highlight_pseudo_style_invalidated(&self) {}
788
789 /// The shadow root this element is a host of.
790 fn shadow_root(&self) -> Option<<Self::ConcreteNode as TNode>::ConcreteShadowRoot>;
791
792 /// The shadow root which roots the subtree this element is contained in.
793 fn containing_shadow(&self) -> Option<<Self::ConcreteNode as TNode>::ConcreteShadowRoot>;
794
795 /// If this element is not a pseudo-element, return self. Otherwise,
796 /// return the ultimate originating element [1]. This is the element
797 /// used to look up rules in the selector maps.
798 /// [1]: https://drafts.csswg.org/selectors-4/#ultimate-originating-element
799 fn ultimate_originating_element(&self) -> Self {
800 let mut cur = *self;
801 while cur.is_pseudo_element() {
802 cur = cur
803 .pseudo_element_originating_element()
804 .expect("Trying to collect rules for a detached pseudo-element")
805 }
806 cur
807 }
808
809 /// Executes the callback for each applicable style rule data which isn't
810 /// the main document's data (which stores UA / author rules).
811 ///
812 /// The element passed to the callback is the containing shadow host for the
813 /// data if it comes from Shadow DOM.
814 ///
815 /// Returns whether normal document author rules should apply.
816 ///
817 /// TODO(emilio): We could separate the invalidation data for elements
818 /// matching in other scopes to avoid over-invalidation.
819 fn each_applicable_non_document_style_rule_data<'a, F>(&self, mut f: F) -> bool
820 where
821 Self: 'a,
822 F: FnMut(&'a CascadeData, Self),
823 {
824 use crate::rule_collector::containing_shadow_ignoring_svg_use;
825
826 let target = self.ultimate_originating_element();
827 let matches_user_and_content_rules = target.matches_user_and_content_rules();
828 let mut doc_rules_apply = matches_user_and_content_rules;
829
830 // Use the same rules to look for the containing host as we do for rule
831 // collection.
832 if let Some(shadow) = containing_shadow_ignoring_svg_use(target) {
833 doc_rules_apply = false;
834 if let Some(data) = shadow.style_data() {
835 f(data, shadow.host());
836 }
837 }
838
839 if let Some(shadow) = target.shadow_root()
840 && let Some(data) = shadow.style_data()
841 {
842 f(data, shadow.host());
843 }
844
845 let mut current = target.assigned_slot();
846 while let Some(slot) = current {
847 // Slots can only have assigned nodes when in a shadow tree.
848 let shadow = slot.containing_shadow().unwrap();
849 if let Some(data) = shadow.style_data()
850 && data.any_slotted_rule()
851 {
852 f(data, shadow.host());
853 }
854 current = slot.assigned_slot();
855 }
856
857 if target.has_part_attr()
858 && let Some(mut inner_shadow) = target.containing_shadow()
859 {
860 loop {
861 let inner_shadow_host = inner_shadow.host();
862 match inner_shadow_host.containing_shadow() {
863 Some(shadow) => {
864 if let Some(data) = shadow.style_data()
865 && data.any_part_rule()
866 {
867 f(data, shadow.host())
868 }
869 // TODO: Could be more granular.
870 if !inner_shadow_host.exports_any_part() {
871 break;
872 }
873 inner_shadow = shadow;
874 },
875 None => {
876 // TODO(emilio): Should probably distinguish with
877 // MatchesDocumentRules::{No,Yes,IfPart} or something so that we could
878 // skip some work.
879 doc_rules_apply = matches_user_and_content_rules;
880 break;
881 },
882 }
883 }
884 }
885
886 doc_rules_apply
887 }
888
889 /// Returns true if one of the transitions needs to be updated on this element. We check all
890 /// the transition properties to make sure that updating transitions is necessary.
891 /// This method should only be called if might_needs_transitions_update returns true when
892 /// passed the same parameters.
893 #[cfg(feature = "gecko")]
894 fn needs_transitions_update(
895 &self,
896 before_change_style: &ComputedValues,
897 after_change_style: &ComputedValues,
898 ) -> bool;
899
900 /// Returns the value of the `xml:lang=""` attribute (or, if appropriate,
901 /// the `lang=""` attribute) on this element.
902 fn lang_attr(&self) -> Option<AttrValue>;
903
904 /// Returns whether this element's language matches the language tag
905 /// `value`. If `override_lang` is not `None`, it specifies the value
906 /// of the `xml:lang=""` or `lang=""` attribute to use in place of
907 /// looking at the element and its ancestors. (This argument is used
908 /// to implement matching of `:lang()` against snapshots.)
909 fn match_element_lang(&self, override_lang: Option<Option<AttrValue>>, value: &Lang) -> bool;
910
911 /// Returns whether this element is the main body element of the HTML
912 /// document it is on.
913 fn is_html_document_body_element(&self) -> bool;
914
915 /// Whether this element's presentational hints prevent style sharing.
916 fn has_unique_presentational_hints(&self) -> bool {
917 false
918 }
919
920 /// Generate the proper applicable declarations due to presentational hints,
921 /// and insert them into `hints`.
922 fn synthesize_presentational_hints_for_legacy_attributes<'a, V>(
923 &self,
924 visited_handling: VisitedHandlingMode,
925 hints: &mut V,
926 ) where
927 V: Push<ApplicableDeclarationBlock<'a>>,
928 Self: 'a;
929
930 /// Generate the proper applicable declarations due to view transition dynamic rules, and
931 /// insert them into `rules`.
932 /// https://drafts.csswg.org/css-view-transitions-1/#document-dynamic-view-transition-style-sheet
933 fn synthesize_view_transition_dynamic_rules<'a, V>(&self, _rules: &mut V)
934 where
935 V: Push<ApplicableDeclarationBlock<'a>>,
936 Self: 'a,
937 {
938 }
939
940 /// Returns element's local name.
941 fn local_name(&self) -> &<SelectorImpl as selectors::parser::SelectorImpl>::BorrowedLocalName;
942
943 /// Returns element's namespace.
944 fn namespace(&self)
945 -> &<SelectorImpl as selectors::parser::SelectorImpl>::BorrowedNamespaceUrl;
946
947 /// Returns the size of the element to be used in container size queries.
948 /// This will usually be the size of the content area of the primary box,
949 /// but can be None if there is no box or if some axis lacks size containment.
950 fn query_container_size(
951 &self,
952 display: &Display,
953 ) -> euclid::default::Size2D<Option<app_units::Au>>;
954
955 /// Returns true if the element has all of specified selector flags.
956 fn has_selector_flags(&self, flags: ElementSelectorFlags) -> bool;
957
958 /// Returns the search direction for relative selector invalidation, if it is on the search path.
959 fn relative_selector_search_direction(&self) -> ElementSelectorFlags;
960
961 /// Returns the implicit scope root for given sheet index and host.
962 fn implicit_scope_for_sheet_in_shadow_root(
963 _opaque_host: OpaqueElement,
964 _sheet_index: usize,
965 ) -> Option<ImplicitScopeRoot> {
966 None
967 }
968
969 /// Compute the damage incurred by the change from the `_old` to `_new`.
970 fn compute_layout_damage(_old: &ComputedValues, _new: &ComputedValues) -> RestyleDamage {
971 Default::default()
972 }
973
974 /// Return the value of the given custom attribute if it exists.
975 fn get_attr(&self, attr: &LocalName, namespace: &Namespace) -> Option<String>;
976
977 /// Traverse the siblings of the element, returning the element's sibling-index()
978 /// and sibling-count(). Also populates `caches` with the sibling-index() and
979 /// sibling-count() values for all siblings of this element.
980 fn get_tree_counting_result(&self, caches: &mut TreeCountingCaches) -> TreeCountingResult {
981 let Some(parent) = self.as_node().parent_node() else {
982 return TreeCountingResult::default();
983 };
984
985 let mut curr = parent.first_child();
986 let mut index = 0u32;
987 let mut count = 0u32;
988 while let Some(node) = curr {
989 if let Some(element) = node.as_element() {
990 count += 1;
991 if *self == element {
992 index = count;
993 }
994 caches.sibling_index.insert(element.opaque(), count);
995 }
996 curr = node.next_sibling();
997 }
998 caches.sibling_count.insert(parent.opaque(), count);
999
1000 TreeCountingResult::new(index, count)
1001 }
1002}
1003
1004/// Provides element-level context needed during style computation.
1005pub trait ElementContext {
1006 /// Opaque handle to the element.
1007 fn opaque_element(&self) -> Option<OpaqueElement>;
1008
1009 /// Opaque handle to the element's parent node.
1010 fn opaque_parent(&self) -> Option<OpaqueNode>;
1011
1012 /// A unique value for the element to use in the generation of random base values
1013 /// for element-scoped `random()` functions.
1014 fn random_seed(&self) -> Option<u64>;
1015
1016 /// Return the value of the given custom attribute if it exists.
1017 fn get_attr(&self, attr: &LocalName, namespace: &Namespace) -> Option<String>;
1018
1019 /// Traverse the siblings of the element, returning the element's sibling-index()
1020 /// and sibling-count(). Also populates `caches` with the sibling-index() and
1021 /// sibling-count() values for all siblings of this element.
1022 fn get_tree_counting_result(&self, caches: &mut TreeCountingCaches) -> TreeCountingResult;
1023}
1024
1025impl<T: TElement> ElementContext for T {
1026 fn opaque_element(&self) -> Option<OpaqueElement> {
1027 Some(self.opaque())
1028 }
1029
1030 fn opaque_parent(&self) -> Option<OpaqueNode> {
1031 self.as_node().parent_node().map(|n| n.opaque())
1032 }
1033
1034 fn random_seed(&self) -> Option<u64> {
1035 Some(TElement::random_seed(self))
1036 }
1037
1038 fn get_attr(&self, attr: &LocalName, namespace: &Namespace) -> Option<String> {
1039 TElement::get_attr(self, attr, namespace)
1040 }
1041
1042 fn get_tree_counting_result(&self, caches: &mut TreeCountingCaches) -> TreeCountingResult {
1043 TElement::get_tree_counting_result(self, caches)
1044 }
1045}
1046
1047/// A set of the attributes used to compute a style that uses `attr()`
1048pub type AttributeReferences = Option<Box<PrecomputedHashMap<LocalName, SmallVec<[Namespace; 1]>>>>;
1049
1050/// A data structure to keep track of the names queried from an element.
1051pub struct AttributeTracker<'a> {
1052 /// The element that queries for attributes.
1053 pub context: &'a dyn ElementContext,
1054 /// The set of attributes we have queried.
1055 pub references: AttributeReferences,
1056}
1057
1058impl<'a> AttributeTracker<'a> {
1059 /// Construct a new attribute tracker trivially.
1060 pub fn new(context: &'a dyn ElementContext) -> Self {
1061 Self {
1062 context,
1063 references: None,
1064 }
1065 }
1066
1067 /// Construct a new dummy attribute tracker
1068 pub fn new_dummy() -> Self {
1069 Self {
1070 context: &DummyElementContext {},
1071 references: None,
1072 }
1073 }
1074
1075 /// Extract the queried references and consume self
1076 pub fn finalize(self) -> AttributeReferences {
1077 self.references
1078 }
1079
1080 /// Query the value and save the name of the attribtue.
1081 pub fn query(&mut self, name: &LocalName, namespace: &Namespace) -> Option<String> {
1082 // We need to save namespaces in case we are thinking of sharing this element's
1083 // style with another.
1084 // i.e if elment a has ns1::attr="blue"
1085 // and element b has ns2::attr="blue"
1086 // a and b can only share style if ns1 and ns2 resolve to the same namespace.
1087 self.references
1088 .get_or_insert_default()
1089 .entry_ref(name)
1090 .or_default()
1091 .push(namespace.clone());
1092 self.context.get_attr(name, namespace)
1093 }
1094}
1095
1096/// A dummy ElementContext that returns default values to any query.
1097#[derive(Clone, Debug, PartialEq)]
1098pub struct DummyElementContext;
1099
1100impl ElementContext for DummyElementContext {
1101 fn get_attr(&self, _attr: &LocalName, _namespace: &Namespace) -> Option<String> {
1102 None
1103 }
1104
1105 fn opaque_element(&self) -> Option<OpaqueElement> {
1106 None
1107 }
1108
1109 fn opaque_parent(&self) -> Option<OpaqueNode> {
1110 None
1111 }
1112
1113 fn random_seed(&self) -> Option<u64> {
1114 None
1115 }
1116
1117 fn get_tree_counting_result(&self, _: &mut TreeCountingCaches) -> TreeCountingResult {
1118 TreeCountingResult::default()
1119 }
1120}
1121
1122/// TNode and TElement aren't Send because we want to be careful and explicit
1123/// about our parallel traversal. However, there are certain situations
1124/// (including but not limited to the traversal) where we need to send DOM
1125/// objects to other threads.
1126///
1127/// That's the reason why `SendNode` exists.
1128#[derive(Clone, Debug, PartialEq)]
1129pub struct SendNode<N: TNode>(N);
1130unsafe impl<N: TNode> Send for SendNode<N> {}
1131impl<N: TNode> SendNode<N> {
1132 /// Unsafely construct a SendNode.
1133 pub unsafe fn new(node: N) -> Self {
1134 SendNode(node)
1135 }
1136}
1137impl<N: TNode> Deref for SendNode<N> {
1138 type Target = N;
1139 fn deref(&self) -> &N {
1140 &self.0
1141 }
1142}
1143
1144/// Same reason as for the existence of SendNode, SendElement does the proper
1145/// things for a given `TElement`.
1146#[derive(Debug, Eq, Hash, PartialEq)]
1147pub struct SendElement<E: TElement>(E);
1148unsafe impl<E: TElement> Send for SendElement<E> {}
1149impl<E: TElement> SendElement<E> {
1150 /// Unsafely construct a SendElement.
1151 pub unsafe fn new(el: E) -> Self {
1152 SendElement(el)
1153 }
1154}
1155impl<E: TElement> Deref for SendElement<E> {
1156 type Target = E;
1157 fn deref(&self) -> &E {
1158 &self.0
1159 }
1160}