Coverage Report

Created: 2026-10-28 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/parcel_selectors-0.28.3/tree.rs
Line
Count
Source
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
//! Traits that nodes must implement. Breaks the otherwise-cyclic dependency
6
//! between layout and style.
7
8
use crate::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
9
use crate::matching::{ElementSelectorFlags, MatchingContext};
10
use crate::parser::SelectorImpl;
11
use std::fmt::Debug;
12
use std::ptr::NonNull;
13
14
/// Opaque representation of an Element, for identity comparisons.
15
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
16
pub struct OpaqueElement(NonNull<()>);
17
18
unsafe impl Send for OpaqueElement {}
19
20
impl OpaqueElement {
21
  /// Creates a new OpaqueElement from an arbitrarily-typed pointer.
22
0
  pub fn new<T>(ptr: &T) -> Self {
23
0
    unsafe { OpaqueElement(NonNull::new_unchecked(ptr as *const T as *const () as *mut ())) }
24
0
  }
25
}
26
27
pub trait Element<'i>: Sized + Clone + Debug {
28
  type Impl: SelectorImpl<'i>;
29
30
  /// Converts self into an opaque representation.
31
  fn opaque(&self) -> OpaqueElement;
32
33
  fn parent_element(&self) -> Option<Self>;
34
35
  /// Whether the parent node of this element is a shadow root.
36
  fn parent_node_is_shadow_root(&self) -> bool;
37
38
  /// The host of the containing shadow root, if any.
39
  fn containing_shadow_host(&self) -> Option<Self>;
40
41
  /// The parent of a given pseudo-element, after matching a pseudo-element
42
  /// selector.
43
  ///
44
  /// This is guaranteed to be called in a pseudo-element.
45
0
  fn pseudo_element_originating_element(&self) -> Option<Self> {
46
0
    debug_assert!(self.is_pseudo_element());
47
0
    self.parent_element()
48
0
  }
49
50
  /// Whether we're matching on a pseudo-element.
51
  fn is_pseudo_element(&self) -> bool;
52
53
  /// Skips non-element nodes
54
  fn prev_sibling_element(&self) -> Option<Self>;
55
56
  /// Skips non-element nodes
57
  fn next_sibling_element(&self) -> Option<Self>;
58
59
  fn is_html_element_in_html_document(&self) -> bool;
60
61
  fn has_local_name(&self, local_name: &<Self::Impl as SelectorImpl<'i>>::BorrowedLocalName) -> bool;
62
63
  /// Empty string for no namespace
64
  fn has_namespace(&self, ns: &<Self::Impl as SelectorImpl<'i>>::BorrowedNamespaceUrl) -> bool;
65
66
  /// Whether this element and the `other` element have the same local name and namespace.
67
  fn is_same_type(&self, other: &Self) -> bool;
68
69
  fn attr_matches(
70
    &self,
71
    ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl<'i>>::NamespaceUrl>,
72
    local_name: &<Self::Impl as SelectorImpl<'i>>::LocalName,
73
    operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl<'i>>::AttrValue>,
74
  ) -> bool;
75
76
  fn match_non_ts_pseudo_class<F>(
77
    &self,
78
    pc: &<Self::Impl as SelectorImpl<'i>>::NonTSPseudoClass,
79
    context: &mut MatchingContext<'_, 'i, Self::Impl>,
80
    flags_setter: &mut F,
81
  ) -> bool
82
  where
83
    F: FnMut(&Self, ElementSelectorFlags);
84
85
  fn match_pseudo_element(
86
    &self,
87
    pe: &<Self::Impl as SelectorImpl<'i>>::PseudoElement,
88
    context: &mut MatchingContext<'_, 'i, Self::Impl>,
89
  ) -> bool;
90
91
  /// Whether this element is a `link`.
92
  fn is_link(&self) -> bool;
93
94
  /// Returns whether the element is an HTML <slot> element.
95
  fn is_html_slot_element(&self) -> bool;
96
97
  /// Returns the assigned <slot> element this element is assigned to.
98
  ///
99
  /// Necessary for the `::slotted` pseudo-class.
100
0
  fn assigned_slot(&self) -> Option<Self> {
101
0
    None
102
0
  }
103
104
  fn has_id(&self, id: &<Self::Impl as SelectorImpl<'i>>::Identifier, case_sensitivity: CaseSensitivity) -> bool;
105
106
  fn has_class(
107
    &self,
108
    name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
109
    case_sensitivity: CaseSensitivity,
110
  ) -> bool;
111
112
  /// Returns the mapping from the `exportparts` attribute in the reverse
113
  /// direction, that is, in an outer-tree -> inner-tree direction.
114
  fn imported_part(
115
    &self,
116
    name: &<Self::Impl as SelectorImpl<'i>>::Identifier,
117
  ) -> Option<<Self::Impl as SelectorImpl<'i>>::Identifier>;
118
119
  fn is_part(&self, name: &<Self::Impl as SelectorImpl<'i>>::Identifier) -> bool;
120
121
  /// Returns whether this element matches `:empty`.
122
  ///
123
  /// That is, whether it does not contain any child element or any non-zero-length text node.
124
  /// See http://dev.w3.org/csswg/selectors-3/#empty-pseudo
125
  fn is_empty(&self) -> bool;
126
127
  /// Returns whether this element matches `:root`,
128
  /// i.e. whether it is the root element of a document.
129
  ///
130
  /// Note: this can be false even if `.parent_element()` is `None`
131
  /// if the parent node is a `DocumentFragment`.
132
  fn is_root(&self) -> bool;
133
134
  /// Returns whether this element should ignore matching nth child
135
  /// selector.
136
0
  fn ignores_nth_child_selectors(&self) -> bool {
137
0
    false
138
0
  }
139
}