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 {
input: []const u8,
stream: TokenStream,
has_skipped_at_rule: bool = false,
pub fn init(input: []const u8) RulesIterator {
return .{
@@ -358,6 +359,7 @@ pub const RulesIterator = struct {
}
if (peeked.token == .at_keyword) {
self.has_skipped_at_rule = true;
self.skipAtRule();
selector_start = 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;
var it = Parser.parseStylesheet(rule);
const parsed_rule = it.next() orelse {
const trimmed = std.mem.trimLeft(u8, rule, &std.ascii.whitespace);
if (std.mem.startsWith(u8, trimmed, "@")) {
// At-rules (like @keyframes) are currently skipped by the parser.
// Returning the index simulates successful insertion without crashing.
if (it.has_skipped_at_rule) {
// Lightpanda currently skips at-rules (e.g., @keyframes, @media) in its
// CSS parser. To prevent JS apps (like Expo/Reanimated) from crashing
// during initialization, we simulate a successful insertion by returning
// the requested index.
return index;
}
return error.SyntaxError;