Strip CSS comments from style attributes and rule declarations

Found while testing crates/svg/src/import/css.rs:20 (parse_decls / split_decls)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-11-09 02:03:05 +07:00
committed by bflatastic
co-authored by Claude Haiku 5.5
parent c2c92b7d49
commit 194364304b
2 changed files with 18 additions and 4 deletions
+10 -4
View File
@@ -18,7 +18,8 @@ pub(super) type XNode<'a, 'i> = roxmltree::Node<'a, 'i>;
/// `name: value` declarations of a block or `style` attribute, with their `!important` flag. The
/// `font` shorthand becomes its longhands, as usvg reads it for the rest of the drawing (#1557).
pub(super) fn parse_decls(s: &str) -> Vec<(String, String, bool)> {
split_decls(s)
let s = strip_comments(s);
split_decls(&s)
.into_iter()
.filter_map(|d| {
let (k, v) = d.split_once(':')?;
@@ -354,9 +355,8 @@ fn top_level_commas(list: &str) -> impl Iterator<Item = &str> {
})
}
/// Rules from every `<style>` element, in source order.
fn parse_sheet(css: &str, out: &mut Vec<Rule>) {
// Drop comments outside strings.
/// `css` without its `/* … */` comments (those inside quoted strings stay).
fn strip_comments(css: &str) -> String {
let mut text = String::with_capacity(css.len());
let mut chars = css.chars().peekable();
let mut quote = None;
@@ -387,6 +387,12 @@ fn parse_sheet(css: &str, out: &mut Vec<Rule>) {
text.push(c);
}
}
text
}
/// Rules from every `<style>` element, in source order.
fn parse_sheet(css: &str, out: &mut Vec<Rule>) {
let text = strip_comments(css);
let mut s = text.as_str();
loop {
s = s.trim_start();
+8
View File
@@ -333,3 +333,11 @@ fn not_selectors() {
},
);
}
#[test]
fn decl_comments_are_stripped() {
let d = |k: &str, v: &str| (k.to_string(), v.to_string(), false);
assert_eq!(parse_decls("fill:red /* note */; stroke:blue"), [d("fill", "red"), d("stroke", "blue")]);
assert_eq!(parse_decls("fill:red /* a;b */ ; stroke:blue"), [d("fill", "red"), d("stroke", "blue")]);
assert_eq!(parse_decls("font-family:'A /* x */ B'"), [d("font-family", "'A /* x */ B'")]);
}