Expand description
§css-escape — escape a string as a CSS identifier
A faithful implementation of the CSSOM
CSS.escape() algorithm
(the same one behind the css.escape
npm package): turn an arbitrary string into something safe to use as a CSS
identifier, for example when building a selector from user input. Zero
dependencies and #![no_std].
use css_escape::escape;
// Safely target an element by id, whatever the id contains.
assert_eq!(format!("#{}", escape("a b#c")), "#a\\ b\\#c");
assert_eq!(escape("0first"), "\\30 first"); // a leading digit is escaped
assert_eq!(escape("café"), "café"); // non-ASCII passes throughThis escapes identifiers (selectors, class/id names). Serializing a quoted CSS string value is a different operation — it must also escape newlines and control characters — and is out of scope.
Functions§
- escape
- Escape
valuefor use as a CSS identifier, per the CSSOMCSS.escape()algorithm.