Update CSS parser to track skipped at-rules and refine insertRule logic

This commit is contained in:
Adrià Arrufat
2026-03-24 00:54:20 +09:00
parent ff0fbb6b41
commit 5972630e95
2 changed files with 7 additions and 4 deletions

View File

@@ -306,6 +306,7 @@ pub fn parseStylesheet(input: []const u8) RulesIterator {
pub const RulesIterator = struct { pub const RulesIterator = struct {
input: []const u8, input: []const u8,
stream: TokenStream, stream: TokenStream,
has_skipped_at_rule: bool = false,
pub fn init(input: []const u8) RulesIterator { pub fn init(input: []const u8) RulesIterator {
return .{ return .{
@@ -358,6 +359,7 @@ pub const RulesIterator = struct {
} }
if (peeked.token == .at_keyword) { if (peeked.token == .at_keyword) {
self.has_skipped_at_rule = true;
self.skipAtRule(); self.skipAtRule();
selector_start = null; selector_start = null;
selector_end = null; selector_end = null;

View File

@@ -76,10 +76,11 @@ pub fn insertRule(self: *CSSStyleSheet, rule: []const u8, maybe_index: ?u32, pag
const index = maybe_index orelse 0; const index = maybe_index orelse 0;
var it = Parser.parseStylesheet(rule); var it = Parser.parseStylesheet(rule);
const parsed_rule = it.next() orelse { const parsed_rule = it.next() orelse {
const trimmed = std.mem.trimLeft(u8, rule, &std.ascii.whitespace); if (it.has_skipped_at_rule) {
if (std.mem.startsWith(u8, trimmed, "@")) { // Lightpanda currently skips at-rules (e.g., @keyframes, @media) in its
// At-rules (like @keyframes) are currently skipped by the parser. // CSS parser. To prevent JS apps (like Expo/Reanimated) from crashing
// Returning the index simulates successful insertion without crashing. // during initialization, we simulate a successful insertion by returning
// the requested index.
return index; return index;
} }
return error.SyntaxError; return error.SyntaxError;