Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: unquoted attribute values decode backslash escapes

Lucien Coffe20791452f2ba
diff --git a/src/browser/tests/element/attribute_value_escapes.html b/src/browser/tests/element/attribute_value_escapes.html
index 3e63675f4..7ac97772a 100644
--- a/src/browser/tests/element/attribute_value_escapes.html
+++ b/src/browser/tests/element/attribute_value_escapes.html
@@ -29,3 +29,50 @@
() => document.querySelector('p[data-random="line one\nline two"]'));
}
</script>
+
+<div id="unquoted">
+ <a id="tel" href="tel:1" data-v="a:b">tel</a>
+ <a id="mail" href="mailto:[email protected]">mail</a>
+ <a id="plain" x="abc">plain</a>
+ <a id="digit" x="1">digit</a>
+ <a id="nul" x="a&#xFFFD;b">nul</a>
+</div>
+
+<script id=unquotedAttributeValueEscapes>
+{
+ // An unquoted attribute value is an <ident-token>, which may hold escapes.
+ // https://drafts.csswg.org/css-syntax/#consume-name
+ const root = document.getElementById('unquoted');
+ const ids = (sel) => Array.from(root.querySelectorAll(sel), (e) => e.id).join(',');
+
+ testing.expectEqual('tel', ids('a[href^=tel\\:]'));
+
+ // Each operator.
+ testing.expectEqual('tel', ids('[data-v=a\\:b]'));
+ testing.expectEqual('tel', ids('[data-v~=a\\:b]'));
+ testing.expectEqual('tel', ids('[data-v|=a\\:b]'));
+ testing.expectEqual('tel', ids('[href^=tel\\:]'));
+ testing.expectEqual('tel', ids('[data-v$=\\:b]'));
+ testing.expectEqual('tel', ids('[href*=l\\:1]'));
+
+ // Hex escape with its terminating space.
+ testing.expectEqual('tel', ids('[href=tel\\3a 1]'));
+ // Escaped ordinary letter.
+ testing.expectEqual('tel', ids('[href^=te\\l]'));
+ // Selector list.
+ testing.expectEqual('tel,mail', ids('a[href^=tel\\:], a[href^=mailto\\:]'));
+
+ testing.expectEqual(true, root.querySelector('a[href^=tel\\:]').matches('[href^=tel\\:]'));
+ testing.expectEqual('tel', root.querySelector('a').closest('[href^=tel\\:]').id);
+
+ // A null in an unquoted value is U+FFFD.
+ testing.expectEqual('nul', ids('[x=a\0b]'));
+
+ // Unchanged forms. A leading digit is not an ident (Chrome rejects [x=1]),
+ // but it has always been accepted here; kept lenient so nothing tightens.
+ testing.expectEqual('plain', ids('[x=abc]'));
+ testing.expectEqual('digit', ids('[x=1]'));
+ testing.expectEqual('tel', ids('[data-v="a:b"]'));
+ testing.expectError("SyntaxError", () => root.querySelectorAll('[x=a:b]'));
+}
+</script>
diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zig
index f6dd3ee7d..ec765215d 100644
--- a/src/browser/webapi/selector/Parser.zig
+++ b/src/browser/webapi/selector/Parser.zig
@@ -1305,6 +1305,12 @@ fn attributeValue(self: *Parser, arena: Allocator) ![]const u8 {
i += 1;
}
+ // An unquoted value is an <ident-token>, which may contain escapes
+ // (and nulls, which become U+FFFD).
+ if (i < input.len and (input[i] == '\\' or input[i] == 0)) {
+ return self.consumeEscapedIdentTail(arena, i, error.InvalidAttributeSelector);
+ }
+
if (i == 0) {
return error.InvalidAttributeSelector;
}
@@ -1909,6 +1915,107 @@ test "Selector: Parser.attributeValue" {
var parser = Parser{ .input = "\"abc\\" };
try testing.expectError(error.InvalidAttributeSelector, parser.attributeValue(arena));
}
+
+ // A leading digit is not an ident (Chrome rejects it), but it has always
+ // been accepted here; kept lenient so no selector that parses stops parsing.
+ {
+ var parser = Parser{ .input = "1]" };
+ try testing.expectEqual("1", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Unquoted values are idents and may hold escapes: tel\: -> tel:
+ {
+ var parser = Parser{ .input = "tel\\:]" };
+ try testing.expectEqual("tel:", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Escape in the middle of an unquoted value.
+ {
+ var parser = Parser{ .input = "a\\:b]" };
+ try testing.expectEqual("a:b", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Escape as the first character of an unquoted value.
+ {
+ var parser = Parser{ .input = "\\:a]" };
+ try testing.expectEqual(":a", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Hex escape in an unquoted value consumes its terminating space.
+ {
+ var parser = Parser{ .input = "tel\\3a 1]" };
+ try testing.expectEqual("tel:1", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Escaped ordinary letter decodes to itself.
+ {
+ var parser = Parser{ .input = "te\\l]" };
+ try testing.expectEqual("tel", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Unquoted value stops before a following flag.
+ {
+ var parser = Parser{ .input = "a\\:b i]" };
+ try testing.expectEqual("a:b", try parser.attributeValue(arena));
+ try testing.expectEqual(" i]", parser.input);
+ }
+
+ // A null in an unquoted value is U+FFFD.
+ {
+ var parser = Parser{ .input = "a\x00b]" };
+ try testing.expectEqual("a\u{FFFD}b", try parser.attributeValue(arena));
+ try testing.expectEqual("]", parser.input);
+ }
+
+ // Backslash at EOF in an unquoted value is U+FFFD.
+ {
+ var parser = Parser{ .input = "abc\\" };
+ try testing.expectEqual("abc\u{FFFD}", try parser.attributeValue(arena));
+ try testing.expectEqual("", parser.input);
+ }
+
+ // An unescaped colon still ends the unquoted value.
+ {
+ var parser = Parser{ .input = "a:b]" };
+ try testing.expectEqual("a", try parser.attributeValue(arena));
+ try testing.expectEqual(":b]", parser.input);
+ }
+}
+
+test "Selector: Parser.attribute unquoted escaped values" {
+ const arena = testing.arena_allocator;
+
+ const cases = [_]struct { []const u8, std.meta.Tag(Selector.AttributeMatcher) }{
+ .{ "[href=tel\\:]", .exact },
+ .{ "[href~=tel\\:]", .word },
+ .{ "[href|=tel\\:]", .prefix_dash },
+ .{ "[href^=tel\\:]", .starts_with },
+ .{ "[href$=tel\\:]", .ends_with },
+ .{ "[href*=tel\\:]", .substring },
+ };
+ for (cases) |case| {
+ var parser = Parser{ .input = case[0] };
+ const attr = try parser.attribute(arena);
+ try testing.expectEqual(case[1], std.meta.activeTag(attr.matcher));
+ const value = switch (attr.matcher) {
+ .presence => unreachable,
+ inline else => |v| v,
+ };
+ try testing.expectEqual("tel:", value);
+ try testing.expectEqual("", parser.input);
+ }
+
+ // An unescaped colon in an unquoted value is still invalid.
+ {
+ var parser = Parser{ .input = "[x=a:b]" };
+ try testing.expectError(error.InvalidAttributeSelector, parser.attribute(arena));
+ }
}
test "Selector: Parser.attributeName" {