Skip to content

Commit 3714c00

Browse files
perf: runtime improvements
1 parent 7539a2c commit 3714c00

1 file changed

Lines changed: 31 additions & 48 deletions

File tree

packages/utils/src/dom.rs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use web_sys::{
44
CssStyleDeclaration, Document, Element, HtmlElement, Node, ShadowRoot, Window, css,
5-
js_sys::Object, wasm_bindgen::JsCast, window,
5+
js_sys::Object,
6+
wasm_bindgen::{JsCast, JsValue},
7+
window,
68
};
79

810
use crate::ElementOrWindow;
@@ -134,7 +136,6 @@ pub fn is_html_element(node: &Node) -> bool {
134136
}
135137

136138
const OVERFLOW_VALUES: [&str; 5] = ["auto", "scroll", "overlay", "hidden", "clip"];
137-
const INVALID_OVERFLOW_DISPLAY_VALUES: [&str; 2] = ["inline", "contents"];
138139

139140
pub fn is_overflow_element(element: &Element) -> bool {
140141
let style = get_computed_style(element);
@@ -148,29 +149,19 @@ pub fn is_overflow_element(element: &Element) -> bool {
148149
OVERFLOW_VALUES
149150
.into_iter()
150151
.any(|s| overflow_combined.contains(s))
151-
&& !INVALID_OVERFLOW_DISPLAY_VALUES
152-
.into_iter()
153-
.any(|s| display == s)
152+
&& display != "inline"
153+
&& display != "contents"
154154
}
155155

156-
const TABLE_ELEMENTS: [&str; 3] = ["table", "td", "th"];
157-
158156
pub fn is_table_element(element: &Element) -> bool {
159157
let node_name = get_node_name(element.into());
160-
TABLE_ELEMENTS.into_iter().any(|s| node_name == s)
158+
node_name == "table" || node_name == "td" || node_name == "th"
161159
}
162160

163-
const TOP_LAYER_SELECTORS: [&str; 2] = [":popover-open", ":modal"];
164-
165161
pub fn is_top_layer(element: &Element) -> bool {
166-
TOP_LAYER_SELECTORS
167-
.into_iter()
168-
.any(|selector| element.matches(selector).unwrap_or(false))
162+
element.matches(":popover-open").unwrap_or(false) || element.matches(":modal").unwrap_or(false)
169163
}
170164

171-
const TRANSFORM_PROPERTIES: [&str; 5] =
172-
["transform", "translate", "scale", "rotate", "perspective"];
173-
174165
const WILL_CHANGE_VALUES: [&str; 6] = [
175166
"transform",
176167
"translate",
@@ -205,41 +196,36 @@ impl From<CssStyleDeclaration> for ElementOrCss<'_> {
205196
}
206197
}
207198

199+
fn is_not_none(value: Result<String, JsValue>) -> bool {
200+
value.is_ok_and(|value| value != "normal")
201+
}
202+
208203
pub fn is_containing_block(element: ElementOrCss) -> bool {
209-
let webkit = is_web_kit();
210204
let css = match element {
211205
ElementOrCss::Element(element) => get_computed_style(element),
212206
ElementOrCss::Css(css) => css,
213207
};
214208

215209
// https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
216210
// https://drafts.csswg.org/css-transforms-2/#individual-transforms
217-
TRANSFORM_PROPERTIES.into_iter().any(|property| {
218-
css.get_property_value(property)
219-
.map(|value| value != "none")
220-
.unwrap_or(false)
221-
}) || css
222-
.get_property_value("container-type")
223-
.map(|value| value != "normal")
224-
.unwrap_or(false)
225-
|| (!webkit
226-
&& css
227-
.get_property_value("backdrop-filter")
228-
.map(|value| value != "none")
229-
.unwrap_or(false))
230-
|| (!webkit
231-
&& css
232-
.get_property_value("filter")
233-
.map(|value| value != "none")
234-
.unwrap_or(false))
235-
|| css
236-
.get_property_value("will-change")
237-
.map(|value| WILL_CHANGE_VALUES.into_iter().any(|v| v == value))
238-
.unwrap_or(false)
239-
|| css
240-
.get_property_value("contain")
241-
.map(|value| CONTAIN_VALUES.into_iter().any(|v| v == value))
242-
.unwrap_or(false)
211+
is_not_none(css.get_property_value("transform"))
212+
|| is_not_none(css.get_property_value("translate"))
213+
|| is_not_none(css.get_property_value("scale"))
214+
|| is_not_none(css.get_property_value("rotate"))
215+
|| is_not_none(css.get_property_value("perspective"))
216+
|| (!is_web_kit()
217+
&& (is_not_none(css.get_property_value("backdrop-filter"))
218+
|| is_not_none(css.get_property_value("filter"))))
219+
|| WILL_CHANGE_VALUES.contains(
220+
&css.get_property_value("will-change")
221+
.unwrap_or_default()
222+
.as_str(),
223+
)
224+
|| CONTAIN_VALUES.contains(
225+
&css.get_property_value("contain")
226+
.unwrap_or_default()
227+
.as_str(),
228+
)
243229
}
244230

245231
pub fn get_containing_block(element: &Element) -> Option<HtmlElement> {
@@ -269,13 +255,10 @@ pub fn is_web_kit() -> bool {
269255
css::supports_with_value("-webkit-backdrop-filter", "none").unwrap_or(false)
270256
}
271257

272-
const LAST_TRAVERSABLE_NODE_NAMES: [&str; 3] = ["html", "body", "#document"];
273-
274258
pub fn is_last_traversable_node(node: &Node) -> bool {
275259
let node_name = get_node_name(node.into());
276-
LAST_TRAVERSABLE_NODE_NAMES
277-
.into_iter()
278-
.any(|s| node_name == s)
260+
261+
node_name == "html" || node_name == "body" || node_name == "#document"
279262
}
280263

281264
pub fn get_computed_style(element: &Element) -> CssStyleDeclaration {

0 commit comments

Comments
 (0)