Skip to main content

stylo_dom/
lib.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//! DOM types to be shared between Rust and C++.
6
7use bitflags::bitflags;
8use malloc_size_of::malloc_size_of_is_0;
9
10pub const HEADING_LEVEL_OFFSET: usize = 57;
11
12bitflags! {
13    /// Event-based element states.
14    #[repr(C)]
15    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
16    pub struct ElementState: u64 {
17        /// The mouse is down on this element.
18        /// <https://html.spec.whatwg.org/multipage/#selector-active>
19        /// FIXME(#7333): set/unset this when appropriate
20        const ACTIVE = 1 << 0;
21        /// This element has focus.
22        /// <https://html.spec.whatwg.org/multipage/#selector-focus>
23        const FOCUS = 1 << 1;
24        /// The mouse is hovering over this element.
25        /// <https://html.spec.whatwg.org/multipage/#selector-hover>
26        const HOVER = 1 << 2;
27        /// Content is enabled (and can be disabled).
28        /// <http://www.whatwg.org/html/#selector-enabled>
29        const ENABLED = 1 << 3;
30        /// Content is disabled.
31        /// <http://www.whatwg.org/html/#selector-disabled>
32        const DISABLED = 1 << 4;
33        /// Content is checked.
34        /// <https://html.spec.whatwg.org/multipage/#selector-checked>
35        const CHECKED = 1 << 5;
36        /// <https://html.spec.whatwg.org/multipage/#selector-indeterminate>
37        const INDETERMINATE = 1 << 6;
38        /// <https://html.spec.whatwg.org/multipage/#selector-placeholder-shown>
39        const PLACEHOLDER_SHOWN = 1 << 7;
40        /// <https://html.spec.whatwg.org/multipage/#selector-target>
41        const URLTARGET = 1 << 8;
42        /// <https://fullscreen.spec.whatwg.org/#%3Afullscreen-pseudo-class>
43        const FULLSCREEN = 1 << 9;
44        /// <https://html.spec.whatwg.org/multipage/#selector-valid>
45        const VALID = 1 << 10;
46        /// <https://html.spec.whatwg.org/multipage/#selector-invalid>
47        const INVALID = 1 << 11;
48        /// <https://drafts.csswg.org/selectors-4/#user-valid-pseudo>
49        const USER_VALID = 1 << 12;
50        /// <https://drafts.csswg.org/selectors-4/#user-invalid-pseudo>
51        const USER_INVALID = 1 << 13;
52        /// All the validity bits at once.
53        const VALIDITY_STATES = Self::VALID.bits() | Self::INVALID.bits() | Self::USER_VALID.bits() | Self::USER_INVALID.bits();
54        /// Non-standard: https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-broken
55        const BROKEN = 1 << 14;
56        /// <https://html.spec.whatwg.org/multipage/#selector-required>
57        const REQUIRED = 1 << 15;
58        /// <https://html.spec.whatwg.org/multipage/#selector-optional>
59        /// We use an underscore to workaround a silly windows.h define.
60        const OPTIONAL_ = 1 << 16;
61        /// <https://html.spec.whatwg.org/multipage/#selector-defined>
62        const DEFINED = 1 << 17;
63        /// <https://html.spec.whatwg.org/multipage/#selector-visited>
64        const VISITED = 1 << 18;
65        /// <https://html.spec.whatwg.org/multipage/#selector-link>
66        const UNVISITED = 1 << 19;
67        /// <https://drafts.csswg.org/selectors-4/#the-any-link-pseudo>
68        const VISITED_OR_UNVISITED = Self::VISITED.bits() | Self::UNVISITED.bits();
69        /// Non-standard: https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-drag-over
70        const DRAGOVER = 1 << 20;
71        /// <https://html.spec.whatwg.org/multipage/#selector-in-range>
72        const INRANGE = 1 << 21;
73        /// <https://html.spec.whatwg.org/multipage/#selector-out-of-range>
74        const OUTOFRANGE = 1 << 22;
75        /// <https://html.spec.whatwg.org/multipage/#selector-read-only>
76        const READONLY = 1 << 23;
77        /// <https://html.spec.whatwg.org/multipage/#selector-read-write>
78        const READWRITE = 1 << 24;
79        /// <https://html.spec.whatwg.org/multipage/#selector-default>
80        const DEFAULT = 1 << 25;
81        /// Non-standard & undocumented.
82        const OPTIMUM = 1 << 26;
83        /// Non-standard & undocumented.
84        const SUB_OPTIMUM = 1 << 27;
85        /// Non-standard & undocumented.
86        const SUB_SUB_OPTIMUM = 1 << 28;
87        /// All the above <meter> bits in one place.
88        const METER_OPTIMUM_STATES = Self::OPTIMUM.bits() | Self::SUB_OPTIMUM.bits() | Self::SUB_SUB_OPTIMUM.bits();
89        /// Non-standard & undocumented.
90        const INCREMENT_SCRIPT_LEVEL = 1 << 29;
91        /// <https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo>
92        const FOCUSRING = 1 << 30;
93        /// <https://drafts.csswg.org/selectors-4/#the-focus-within-pseudo>
94        const FOCUS_WITHIN = 1u64 << 31;
95        /// :dir matching; the states are used for dynamic change detection.
96        /// State that elements that match :dir(ltr) are in.
97        const LTR = 1u64 << 32;
98        /// State that elements that match :dir(rtl) are in.
99        const RTL = 1u64 << 33;
100        /// State that HTML elements that have a "dir" attr are in.
101        const HAS_DIR_ATTR = 1u64 << 34;
102        /// State that HTML elements with dir="ltr" (or something
103        /// case-insensitively equal to "ltr") are in.
104        const HAS_DIR_ATTR_LTR = 1u64 << 35;
105        /// State that HTML elements with dir="rtl" (or something
106        /// case-insensitively equal to "rtl") are in.
107        const HAS_DIR_ATTR_RTL = 1u64 << 36;
108        /// State that HTML <bdi> elements without a valid-valued "dir" attr or
109        /// any HTML elements (including <bdi>) with dir="auto" (or something
110        /// case-insensitively equal to "auto") are in.
111        const HAS_DIR_ATTR_LIKE_AUTO = 1u64 << 37;
112        /// Non-standard & undocumented.
113        const AUTOFILL = 1u64 << 38;
114        /// Non-standard & undocumented.
115        const AUTOFILL_PREVIEW = 1u64 << 39;
116        /// State for modal elements:
117        /// <https://drafts.csswg.org/selectors-4/#modal-state>
118        const MODAL = 1u64 << 40;
119        /// <https://html.spec.whatwg.org/multipage/#inert-subtrees>
120        const INERT = 1u64 << 41;
121        /// State for the topmost modal element in top layer
122        const TOPMOST_MODAL = 1u64 << 42;
123        /// Initially used for the devtools highlighter, but now somehow only
124        /// used for the devtools accessibility inspector.
125        const DEVTOOLS_HIGHLIGHTED = 1u64 << 43;
126        /// Used for the devtools style editor. Probably should go away.
127        const STYLEEDITOR_TRANSITIONING = 1u64 << 44;
128        /// For :-moz-value-empty (to show widgets like the reveal password
129        /// button or the clear button).
130        const VALUE_EMPTY = 1u64 << 45;
131        /// For :-moz-revealed.
132        const REVEALED = 1u64 << 46;
133        /// https://html.spec.whatwg.org/#selector-popover-open
134        /// Match element's popover visibility state of showing
135        const POPOVER_OPEN = 1u64 << 47;
136        /// https://drafts.csswg.org/css-scoping-1/#the-has-slotted-pseudo
137        /// Match whether a slot element has assigned nodes
138        const HAS_SLOTTED = 1u64 << 48;
139        /// https://drafts.csswg.org/selectors-4/#open-state
140        /// Match whether an openable element is currently open
141        const OPEN = 1u64 << 49;
142        /// For :active-view-transition.
143        /// <https://www.w3.org/TR/css-view-transitions-2/#the-active-view-transition-pseudo>
144        const ACTIVE_VIEW_TRANSITION = 1u64 << 50;
145        /// For :-moz-suppress-for-print-selection.
146        const SUPPRESS_FOR_PRINT_SELECTION = 1u64 << 51;
147        /// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-paused
148        const PAUSED = 1u64 << 52;
149        /// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-seeking
150        const SEEKING = 1u64 << 53;
151        /// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-buffering
152        const BUFFERING = 1u64 << 54;
153        /// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-stalled
154        const STALLED = 1u64 << 55;
155        /// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-muted
156        const MUTED = 1u64 << 56;
157        /// This element is fullscreen and was requested to have keyboard lock.
158        const FULLSCREEN_KEYBOARD_LOCK = 1u64 << 57;
159        /// https://drafts.csswg.org/selectors-5/#headings
160        /// These 4 bits are used to pack the elements heading level into the element state
161        /// Heading levels can be from 1-9 so 4 bits allows us to express the full range.
162        const HEADING_LEVEL_BITS = 0b1111u64 << HEADING_LEVEL_OFFSET;
163        /// https://w3c.github.io/picture-in-picture/#css-pseudo-class
164        const PICTURE_IN_PICTURE = 1u64 << 61;
165
166        /// Some convenience unions.
167        const DIR_STATES = Self::LTR.bits() | Self::RTL.bits();
168
169        const DIR_ATTR_STATES = Self::HAS_DIR_ATTR.bits() |
170                                Self::HAS_DIR_ATTR_LTR.bits() |
171                                Self::HAS_DIR_ATTR_RTL.bits() |
172                                Self::HAS_DIR_ATTR_LIKE_AUTO.bits();
173
174        const DISABLED_STATES = Self::DISABLED.bits() | Self::ENABLED.bits();
175
176        const REQUIRED_STATES = Self::REQUIRED.bits() | Self::OPTIONAL_.bits();
177    }
178}
179
180bitflags! {
181    /// Event-based document states.
182    #[repr(C)]
183    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
184    pub struct DocumentState: u64 {
185        /// Window activation status
186        const WINDOW_INACTIVE = 1 << 0;
187        /// RTL locale: specific to the XUL localedir attribute
188        const RTL_LOCALE = 1 << 1;
189        /// LTR locale: specific to the XUL localedir attribute
190        const LTR_LOCALE = 1 << 2;
191
192        const ALL_LOCALEDIR_BITS = Self::LTR_LOCALE.bits() | Self::RTL_LOCALE.bits();
193    }
194}
195
196malloc_size_of_is_0!(ElementState, DocumentState);