mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-30 12:47:13 -06:00
Merge pull request #4553 from TheGalitube/fix/h1650-svg-css-comment-markers-inside-s
Preserve comment markers inside CSS strings
This commit is contained in:
@@ -342,14 +342,37 @@ 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.
|
||||
// Drop comments outside strings.
|
||||
let mut text = String::with_capacity(css.len());
|
||||
let mut rest = css;
|
||||
while let Some(i) = rest.find("/*") {
|
||||
text.push_str(&rest[..i]);
|
||||
rest = rest[i + 2..].find("*/").map_or("", |j| &rest[i + 2 + j + 2..]);
|
||||
let mut chars = css.chars().peekable();
|
||||
let mut quote = None;
|
||||
let mut escaped = false;
|
||||
while let Some(c) = chars.next() {
|
||||
if let Some(q) = quote {
|
||||
text.push(c);
|
||||
if escaped {
|
||||
escaped = false;
|
||||
} else if c == '\\' {
|
||||
escaped = true;
|
||||
} else if c == q {
|
||||
quote = None;
|
||||
}
|
||||
} else if c == '\'' || c == '"' {
|
||||
quote = Some(c);
|
||||
text.push(c);
|
||||
} else if c == '/' && chars.peek() == Some(&'*') {
|
||||
chars.next();
|
||||
let mut prev_star = false;
|
||||
for c in chars.by_ref() {
|
||||
if prev_star && c == '/' {
|
||||
break;
|
||||
}
|
||||
prev_star = c == '*';
|
||||
}
|
||||
} else {
|
||||
text.push(c);
|
||||
}
|
||||
}
|
||||
text.push_str(rest);
|
||||
let mut s = text.as_str();
|
||||
loop {
|
||||
s = s.trim_start();
|
||||
|
||||
@@ -249,6 +249,13 @@ fn font_variation_settings_become_variable_font_settings() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comment_markers_inside_css_strings_are_not_comments() {
|
||||
let d = open(r#"<style>text { font-family: "A/*B"; fill: red }</style><text y="10">x</text>"#);
|
||||
let t = only_text(&d);
|
||||
assert_eq!(t.first_style().fill.color().map(|c| c.to_hex()).as_deref(), Some("#ff0000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn css_selectors_reach_text() {
|
||||
let d = open(
|
||||
|
||||
Reference in New Issue
Block a user