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/. */
5 //! Traits that nodes must implement. Breaks the otherwise-cyclic dependency
6 //! between layout and style.
8 use crate::attr::{AttrSelectorOperation, CaseSensitivity, NamespaceConstraint};
9 use crate::bloom::BloomFilter;
10 use crate::matching::{ElementSelectorFlags, MatchingContext};
11 use crate::parser::SelectorImpl;
13 use std::ptr::NonNull;
15 /// Opaque representation of an Element, for identity comparisons.
16 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
17 pub struct OpaqueElement(NonNull<()>);
19 unsafe impl Send for OpaqueElement {}
22 /// Creates a new OpaqueElement from an arbitrarily-typed pointer.
23 pub fn new<T>(ptr: &T) -> Self {
25 OpaqueElement(NonNull::new_unchecked(
26 ptr as *const T as *const () as *mut (),
32 pub trait Element: Sized + Clone + Debug {
33 type Impl: SelectorImpl;
35 /// Converts self into an opaque representation.
36 fn opaque(&self) -> OpaqueElement;
38 fn parent_element(&self) -> Option<Self>;
40 /// Whether the parent node of this element is a shadow root.
41 fn parent_node_is_shadow_root(&self) -> bool;
43 /// The host of the containing shadow root, if any.
44 fn containing_shadow_host(&self) -> Option<Self>;
46 /// The parent of a given pseudo-element, after matching a pseudo-element
49 /// This is guaranteed to be called in a pseudo-element.
50 fn pseudo_element_originating_element(&self) -> Option<Self> {
51 debug_assert!(self.is_pseudo_element());
55 /// Whether we're matching on a pseudo-element.
56 fn is_pseudo_element(&self) -> bool;
58 /// Skips non-element nodes
59 fn prev_sibling_element(&self) -> Option<Self>;
61 /// Skips non-element nodes
62 fn next_sibling_element(&self) -> Option<Self>;
64 /// Skips non-element nodes
65 fn first_element_child(&self) -> Option<Self>;
67 fn is_html_element_in_html_document(&self) -> bool;
69 fn has_local_name(&self, local_name: &<Self::Impl as SelectorImpl>::BorrowedLocalName) -> bool;
71 /// Empty string for no namespace
72 fn has_namespace(&self, ns: &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl) -> bool;
74 /// Whether this element and the `other` element have the same local name and namespace.
75 fn is_same_type(&self, other: &Self) -> bool;
79 ns: &NamespaceConstraint<&<Self::Impl as SelectorImpl>::NamespaceUrl>,
80 local_name: &<Self::Impl as SelectorImpl>::LocalName,
81 operation: &AttrSelectorOperation<&<Self::Impl as SelectorImpl>::AttrValue>,
84 fn has_attr_in_no_namespace(
86 local_name: &<Self::Impl as SelectorImpl>::LocalName,
89 &NamespaceConstraint::Specific(&crate::parser::namespace_empty_string::<Self::Impl>()),
91 &AttrSelectorOperation::Exists,
95 fn match_non_ts_pseudo_class(
97 pc: &<Self::Impl as SelectorImpl>::NonTSPseudoClass,
98 context: &mut MatchingContext<Self::Impl>,
101 fn match_pseudo_element(
103 pe: &<Self::Impl as SelectorImpl>::PseudoElement,
104 context: &mut MatchingContext<Self::Impl>,
107 /// Sets selector flags on the elemnt itself or the parent, depending on the
108 /// flags, which indicate what kind of work may need to be performed when
109 /// DOM state changes.
110 fn apply_selector_flags(&self, flags: ElementSelectorFlags);
112 /// Whether this element is a `link`.
113 fn is_link(&self) -> bool;
115 /// Returns whether the element is an HTML <slot> element.
116 fn is_html_slot_element(&self) -> bool;
118 /// Returns the assigned <slot> element this element is assigned to.
120 /// Necessary for the `::slotted` pseudo-class.
121 fn assigned_slot(&self) -> Option<Self> {
127 id: &<Self::Impl as SelectorImpl>::Identifier,
128 case_sensitivity: CaseSensitivity,
133 name: &<Self::Impl as SelectorImpl>::Identifier,
134 case_sensitivity: CaseSensitivity,
139 name: &<Self::Impl as SelectorImpl>::Identifier,
142 /// Returns the mapping from the `exportparts` attribute in the reverse
143 /// direction, that is, in an outer-tree -> inner-tree direction.
146 name: &<Self::Impl as SelectorImpl>::Identifier,
147 ) -> Option<<Self::Impl as SelectorImpl>::Identifier>;
149 fn is_part(&self, name: &<Self::Impl as SelectorImpl>::Identifier) -> bool;
151 /// Returns whether this element matches `:empty`.
153 /// That is, whether it does not contain any child element or any non-zero-length text node.
154 /// See http://dev.w3.org/csswg/selectors-3/#empty-pseudo
155 fn is_empty(&self) -> bool;
157 /// Returns whether this element matches `:root`,
158 /// i.e. whether it is the root element of a document.
160 /// Note: this can be false even if `.parent_element()` is `None`
161 /// if the parent node is a `DocumentFragment`.
162 fn is_root(&self) -> bool;
164 /// Returns whether this element should ignore matching nth child
166 fn ignores_nth_child_selectors(&self) -> bool {
170 /// Add hashes unique to this element to the given filter, returning true
171 /// if any got added.
172 fn add_element_unique_hashes(&self, filter: &mut BloomFilter) -> bool;