Merge pull request #879 from lightpanda-io/css-parser-error

Fix parser identifier with escaped string
This commit is contained in:
Pierre Tachoire
2025-07-10 16:10:14 -07:00
committed by GitHub
3 changed files with 41 additions and 1 deletions

View File

@@ -204,7 +204,7 @@ pub const Parser = struct {
} }
const c = p.s[p.i]; const c = p.s[p.i];
if (!nameStart(c) or c == '\\') { if (!(nameStart(c) or c == '\\')) {
return ParseError.ExpectedSelector; return ParseError.ExpectedSelector;
} }
@@ -950,3 +950,36 @@ test "parser.parseString" {
}; };
} }
} }
test "parser.parse" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const testcases = [_]struct {
s: []const u8, // given value
exp: Selector, // expected value
err: bool = false,
}{
.{ .s = "root", .exp = .{ .tag = "root" } },
.{ .s = ".root", .exp = .{ .class = "root" } },
.{ .s = ":root", .exp = .{ .pseudo_class = .root } },
.{ .s = ".\\:bar", .exp = .{ .class = ":bar" } },
.{ .s = ".foo\\:bar", .exp = .{ .class = "foo:bar" } },
};
for (testcases) |tc| {
var p = Parser{ .s = tc.s, .opts = .{} };
const sel = p.parse(alloc) catch |e| {
// if error was expected, continue.
if (tc.err) continue;
std.debug.print("test case {s}\n", .{tc.s});
return e;
};
std.testing.expectEqualDeep(tc.exp, sel) catch |e| {
std.debug.print("test case {s} : {}\n", .{ tc.s, sel });
return e;
};
}
}

View File

@@ -469,6 +469,9 @@ test "Browser.DOM.Document" {
, ,
"1", "1",
}, },
.{ "document.querySelectorAll('.\\\\:popover-open').length", "0" },
.{ "document.querySelectorAll('.foo\\\\:bar').length", "0" },
}, .{}); }, .{});
try runner.testCases(&.{ try runner.testCases(&.{

View File

@@ -336,4 +336,8 @@ test "JS: primitive types" {
.{ "p.returnFloat32()", "1.100000023841858,-200.03500366210938,0.0003000000142492354" }, .{ "p.returnFloat32()", "1.100000023841858,-200.03500366210938,0.0003000000142492354" },
.{ "p.returnFloat64()", "8881.22284,-4928.3838122,-0.00004" }, .{ "p.returnFloat64()", "8881.22284,-4928.3838122,-0.00004" },
}, .{}); }, .{});
try runner.testCases(&.{
.{ "'foo\\\\:bar'", "foo\\:bar" },
}, .{});
} }