perspective_viewer/utils/css_vars.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use web_sys::Element;
14
15/// The computed value of custom property `name` on `elem`, trimmed, or
16/// `None` when undefined/empty.
17pub fn read_custom_property(elem: &Element, name: &str) -> Option<String> {
18 let value = web_sys::window()?
19 .get_computed_style(elem)
20 .ok()??
21 .get_property_value(name)
22 .ok()?;
23
24 let value = value.trim();
25 (!value.is_empty()).then(|| value.to_owned())
26}
27
28/// The intl string `--psp-label--{slug}--content` as inherited by `elem`,
29/// unquoted, or `None` when no theme defines it.
30pub fn read_intl_string(elem: &Element, slug: &str) -> Option<String> {
31 let raw = read_custom_property(elem, &format!("--psp-label--{slug}--content"))?;
32 let unquoted = raw
33 .strip_prefix('"')
34 .and_then(|x| x.strip_suffix('"'))
35 .or_else(|| raw.strip_prefix('\'').and_then(|x| x.strip_suffix('\'')))
36 .unwrap_or(&raw);
37
38 Some(unquoted.replace("\\\"", "\""))
39}