Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(cssom): parse comments in unicode ranges

ldm03330e0184320
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt
index 425049390..3849307ea 100644
--- a/moli-benchmark/wpt-cross-current/failed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt
@@ -1984,7 +1984,6 @@ css/css-syntax/escaped-eof.html
css/css-syntax/non-ascii-codepoints.html
css/css-syntax/serialize-consecutive-tokens.html
css/css-syntax/unclosed-constructs.html
-css/css-syntax/urange-parsing.html
css/css-text-decor/animations/discrete-no-interpolation.html
css/css-text-decor/animations/text-decoration-thickness-interpolation.html
css/css-text-decor/animations/text-underline-offset-interpolation.html
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index 20b288fab..2e131b70e 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -2583,6 +2583,7 @@ css/css-syntax/serialize-escape-identifiers.html
css/css-syntax/trailing-braces.html
css/css-syntax/unclosed-url-at-eof.html
css/css-syntax/unicode-range-selector.html
+css/css-syntax/urange-parsing.html
css/css-syntax/url-whitespace-consumption.html
css/css-syntax/var-with-blocks.html
css/css-syntax/whitespace.html
diff --git a/moli-css-parse/src/stylo_stylesheet.rs b/moli-css-parse/src/stylo_stylesheet.rs
index 935b57a14..53ffc760a 100644
--- a/moli-css-parse/src/stylo_stylesheet.rs
+++ b/moli-css-parse/src/stylo_stylesheet.rs
@@ -2,7 +2,7 @@
use std::{borrow::Cow, sync::Once};
-use cssparser::{Parser, ParserInput};
+use cssparser::{Parser, ParserInput, ToCss, Token, TokenSerializationType};
pub use style::moli_rule_tree::{
CssConditionRuleView, CssCounterStyleRuleView, CssFontFaceRuleView,
CssFontFeatureValueEntryView, CssFontFeatureValuesRuleView, CssImportRuleView,
@@ -82,6 +82,10 @@ pub fn parse_font_face_descriptor_entry_with_stylo(
{
return None;
}
+ let normalized_value = (name == "unicode-range")
+ .then(|| remove_css_comments_without_retokenizing(value))
+ .flatten();
+ let value = normalized_value.as_deref().unwrap_or(value);
let entry = style::moli_rule_tree::parse_font_face_cssom_descriptor_entry(&name, value)?;
Some(CssFontFaceDescriptorEntryView {
name: entry.name,
@@ -89,6 +93,68 @@ pub fn parse_font_face_descriptor_entry_with_stylo(
})
}
+/// Work around cssparser's raw-slice `<urange>` parser, which cannot parse
+/// comments between otherwise adjacent range tokens. Refuse the rewrite when
+/// removing a comment would merge two tokens, so invalid input stays invalid.
+fn remove_css_comments_without_retokenizing(value: &str) -> Option<String> {
+ let mut input = ParserInput::new(value);
+ let mut input = Parser::new(&mut input);
+ let mut output = String::with_capacity(value.len());
+ let mut original_tokens = Vec::new();
+ let mut removed_comment = false;
+
+ loop {
+ let token_start = input.position();
+ let Ok(token) = input.next_including_whitespace_and_comments().cloned() else {
+ break;
+ };
+ match token {
+ Token::Comment(_) => {
+ removed_comment = true;
+ }
+ Token::Function(_)
+ | Token::ParenthesisBlock
+ | Token::SquareBracketBlock
+ | Token::CurlyBracketBlock => return None,
+ token => {
+ original_tokens.push(css_token_fingerprint(&token)?);
+ output.push_str(input.slice_from(token_start));
+ }
+ }
+ }
+
+ if !removed_comment || original_tokens != css_token_fingerprints(&output)? {
+ return None;
+ }
+ Some(output)
+}
+
+fn css_token_fingerprints(value: &str) -> Option<Vec<(TokenSerializationType, String)>> {
+ let mut input = ParserInput::new(value);
+ let mut input = Parser::new(&mut input);
+ let mut tokens = Vec::new();
+ loop {
+ let Ok(token) = input.next_including_whitespace_and_comments().cloned() else {
+ break;
+ };
+ match token {
+ Token::Comment(_) => {}
+ Token::Function(_)
+ | Token::ParenthesisBlock
+ | Token::SquareBracketBlock
+ | Token::CurlyBracketBlock => return None,
+ token => tokens.push(css_token_fingerprint(&token)?),
+ }
+ }
+ Some(tokens)
+}
+
+fn css_token_fingerprint(token: &Token<'_>) -> Option<(TokenSerializationType, String)> {
+ let mut serialized = String::new();
+ token.to_css(&mut serialized).ok()?;
+ Some((token.serialization_type(), serialized))
+}
+
/// Parse and serialize a CSSOM `@font-face` descriptor block through Stylo.
pub fn parse_font_face_descriptor_block_with_stylo(style_text: &str) -> Option<String> {
style::moli_rule_tree::parse_font_face_cssom_descriptor_block(style_text)
@@ -405,6 +471,7 @@ mod tests {
parse_page_rule_view_with_stylo, parse_property_rule_view_with_stylo,
parse_stylesheet_rule_snapshot_for_insert_with_stylo,
parse_stylesheet_rule_snapshots_with_stylo, parse_stylesheet_rule_texts_with_stylo,
+ remove_css_comments_without_retokenizing,
};
use style::stylesheets::CssRuleType;
@@ -714,6 +781,44 @@ mod tests {
);
}
+ #[test]
+ fn font_face_unicode_range_descriptor_accepts_comments_between_tokens() {
+ for (value, compacted, expected) in [
+ ("u/**/+/**/a/**/?", "u+a?", "U+A0-AF"),
+ ("u/**/+0a/**/?", "u+0a?", "U+A0-AF"),
+ ("u/**/+0/**/?", "u+0?", "U+0-F"),
+ ("u/**/+0/**/-0a", "u+0-0a", "U+0-A"),
+ ("u/**/+0/**/-1", "u+0-1", "U+0-1"),
+ ("u/**/+/**/?", "u+?", "U+0-F"),
+ ] {
+ assert_eq!(
+ remove_css_comments_without_retokenizing(value).as_deref(),
+ Some(compacted)
+ );
+ let entry = parse_font_face_descriptor_entry_with_stylo("unicode-range", value)
+ .unwrap_or_else(|| panic!("comments may occur between tokens in {value}"));
+
+ assert_eq!(entry.value, expected);
+ }
+
+ let entry = parse_font_face_descriptor_entry_with_stylo(
+ "unicode-range",
+ "u/**/+a/**/?, u/**/+0/**/-1",
+ )
+ .expect("commented unicode ranges may occur in a list");
+ assert_eq!(entry.value, "U+A0-AF, U+0-1");
+ }
+
+ #[test]
+ fn font_face_unicode_range_comment_recovery_does_not_merge_tokens() {
+ for invalid in ["u/**/0", "u/**/+a/**/b", "u/**/+0000000"] {
+ assert!(
+ parse_font_face_descriptor_entry_with_stylo("unicode-range", invalid).is_none(),
+ "{invalid} must remain invalid"
+ );
+ }
+ }
+
#[test]
fn font_face_descriptor_block_uses_stylo_descriptor_api() {
assert_eq!(
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom.rs
index 67045e383..9a372ac77 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/cssom.rs
@@ -7337,6 +7337,39 @@ fn css_font_face_descriptor_setter_uses_cssom_value_fragment_eof() {
);
}
+#[test]
+fn css_font_face_unicode_range_setter_accepts_comments_between_tokens() {
+ let mut vm = new_storage_test_vm("https://css-font-face-unicode-range-comments.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const sheet = new CSSStyleSheet();
+ sheet.replaceSync('@font-face { font-family: Foo; src: local(Foo); unicode-range: U+1357; }');
+ const style = sheet.cssRules[0].style;
+ const values = [
+ 'u/**/+/**/a/**/?',
+ 'u/**/+0a/**/?',
+ 'u/**/+0/**/?',
+ 'u/**/+0/**/-0a',
+ 'u/**/+0/**/-1',
+ 'u/**/+/**/?'
+ ].map(value => {
+ style.setProperty('unicode-range', value);
+ return style.getPropertyValue('unicode-range');
+ });
+ style.setProperty('unicode-range', 'u/**/+a/**/b');
+ values.push(style.getPropertyValue('unicode-range'));
+ return values.join('|');
+})()
+"#,
+ )
+ .expect("CSSFontFaceRule unicode-range setter should accept comments between tokens");
+
+ assert_eq!(result, "U+A0-AF|U+A0-AF|U+0-F|U+0-A|U+0-1|U+0-F|U+0-F");
+}
+
#[test]
fn css_font_face_descriptor_dot_accessors_cover_stylo_descriptors() {
let mut vm = new_storage_test_vm("https://css-font-face-descriptor-dot-accessors.test/");