Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: a backslash before a newline ends an identifier

Lucien Coffe53afdb1349d5
diff --git a/src/browser/tests/element/attribute_value_escapes.html b/src/browser/tests/element/attribute_value_escapes.html
index 7ac97772a..a7b0e20cd 100644
--- a/src/browser/tests/element/attribute_value_escapes.html
+++ b/src/browser/tests/element/attribute_value_escapes.html
@@ -73,6 +73,8 @@
testing.expectEqual('plain', ids('[x=abc]'));
testing.expectEqual('digit', ids('[x=1]'));
testing.expectEqual('tel', ids('[data-v="a:b"]'));
+ // In a quoted value, a backslash and a newline are a line continuation.
+ testing.expectEqual('tel', ids('[data-v="a\\\n:b"]'));
testing.expectError("SyntaxError", () => root.querySelectorAll('[x=a:b]'));
}
</script>
diff --git a/src/browser/tests/element/selector_invalid.html b/src/browser/tests/element/selector_invalid.html
index 7112ab85a..2d636c3fe 100644
--- a/src/browser/tests/element/selector_invalid.html
+++ b/src/browser/tests/element/selector_invalid.html
@@ -58,3 +58,20 @@
testing.expectError("SyntaxError", () => container.querySelector('p ~'));
}
</script>
+
+<script id="escaped_newline">
+{
+ const container = $('#container');
+
+ // A backslash followed by a newline is not a valid escape, so it ends an
+ // identifier and leaves a stray backslash.
+ // https://drafts.csswg.org/css-syntax/#starts-with-a-valid-escape
+ testing.expectError("SyntaxError", () => container.querySelector('.a\\\nb'));
+ testing.expectError("SyntaxError", () => container.querySelector('#a\\\nb'));
+ testing.expectError("SyntaxError", () => container.querySelector('[a\\\nb]'));
+ testing.expectError("SyntaxError", () => container.querySelector('[x=a\\\nb]'));
+ testing.expectError("SyntaxError", () => container.querySelector('[x=a\\\rb]'));
+ testing.expectError("SyntaxError", () => container.querySelector('[x=a\\\fb]'));
+ testing.expectError("SyntaxError", () => container.querySelector('[x=\\\n]'));
+}
+</script>
diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zig
index ec765215d..91a3e7199 100644
--- a/src/browser/webapi/selector/Parser.zig
+++ b/src/browser/webapi/selector/Parser.zig
@@ -981,6 +981,12 @@ fn consumeEscapedIdentTail(self: *Parser, arena: Allocator, prefix_len: usize, e
const b = input[j];
if (b == '\\') {
+ if (j + 1 < input.len) switch (input[j + 1]) {
+ // Not a valid escape: the ident ends before the backslash.
+ // https://drafts.csswg.org/css-syntax/#starts-with-a-valid-escape
+ '\n', '\r', '\x0C' => break,
+ else => {},
+ };
j += 1;
const escape_result = try parseEscape(input[j..], arena);
try result.appendSlice(arena, escape_result.bytes);
@@ -1988,6 +1994,69 @@ test "Selector: Parser.attributeValue" {
}
}
+test "Selector: Parser escaped newline ends an identifier" {
+ const arena = testing.arena_allocator;
+
+ // A backslash followed by a newline is not a valid escape: the ident
+ // ends before the backslash.
+ // https://drafts.csswg.org/css-syntax/#starts-with-a-valid-escape
+ {
+ var parser = Parser{ .input = ".a\\\nb" };
+ try testing.expectEqual("a", try parser.class(arena));
+ try testing.expectEqual("\\\nb", parser.input);
+ }
+
+ {
+ var parser = Parser{ .input = "#a\\\x0Cb" };
+ try testing.expectEqual("a", try parser.id(arena));
+ try testing.expectEqual("\\\x0Cb", parser.input);
+ }
+
+ {
+ var parser = Parser{ .input = "a\\\nb]" };
+ try testing.expectEqual("a", try parser.attributeName(arena));
+ try testing.expectEqual("\\\nb]", parser.input);
+ }
+
+ {
+ var parser = Parser{ .input = "a\\\rb]" };
+ try testing.expectEqual("a", try parser.attributeValue(arena));
+ try testing.expectEqual("\\\rb]", parser.input);
+ }
+
+ // Nothing before the backslash: no ident at all.
+ {
+ var parser = Parser{ .input = ".\\\nb" };
+ try testing.expectError(error.InvalidClassSelector, parser.class(arena));
+ }
+
+ {
+ var parser = Parser{ .input = "\\\n]" };
+ try testing.expectError(error.InvalidAttributeSelector, parser.attributeValue(arena));
+ }
+
+ // The stray backslash makes the whole selector invalid.
+ const invalid = [_][]const u8{
+ ".a\\\nb",
+ "#a\\\nb",
+ "[a\\\nb]",
+ "[x=a\\\nb]",
+ "[x=a\\\r\nb]",
+ "[x=a\\\x0Cb]",
+ };
+ for (invalid) |input| {
+ if (parseList(arena, input)) |_| {
+ return error.TestExpectedError;
+ } else |_| {}
+ }
+
+ // In a quoted value, a backslash and a newline are a line continuation.
+ {
+ const selectors = try parseList(arena, "[x=\"a\\\nb\"]");
+ try testing.expectEqual(1, selectors.len);
+ }
+}
+
test "Selector: Parser.attribute unquoted escaped values" {
const arena = testing.arena_allocator;