Skip to content

Lightpanda: the headless browser designed for AI and automation

move the case sensitivity decision at parse time

Karl Seguinc0c756c1326d
diff --git a/src/browser/tests/document/query_selector_attributes.html b/src/browser/tests/document/query_selector_attributes.html
index 823c8143c..7caad780d 100644
--- a/src/browser/tests/document/query_selector_attributes.html
+++ b/src/browser/tests/document/query_selector_attributes.html
@@ -125,6 +125,7 @@
</script>
<form id="caseForm" method="POST"><input id="caseCheckbox" type="Checkbox"><input name="Q"></form>
+<a id="caseLink" href="/a" target="_Blank" rel="NoFollow" hreflang="EN-us">a</a>
<svg><g id="caseSvg" type="Matrix"></g></svg>
<script id="valueCase">
@@ -133,6 +134,11 @@
testing.expectEqual('caseCheckbox', document.querySelector('#caseForm input[type=checkbox]')?.id);
testing.expectEqual(true, document.getElementById('caseForm').matches('[method^=po]'));
testing.expectEqual('caseForm', document.getElementById('caseCheckbox').closest('form[method="post"]')?.id);
+ testing.expectEqual('caseLink', document.querySelector('a[target=_blank][rel~=nofollow][hreflang|=en]')?.id);
+
+ // An explicit `s` flag keeps them case-sensitive
+ testing.expectEqual(null, document.querySelector('form[method=post s]'));
+ testing.expectEqual(null, document.querySelector('a[rel~=nofollow s]'));
// Other attributes and foreign elements stay case-sensitive
testing.expectEqual(null, document.querySelector('[name=q]'));
diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zig
index 68cc53ef3..587307525 100644
--- a/src/browser/webapi/selector/List.zig
+++ b/src/browser/webapi/selector/List.zig
@@ -444,8 +444,11 @@ fn matchesAttribute(el: *Node.Element, attr: Selector.Attribute) bool {
return false;
};
- const case_insensitive = attr.case_insensitive orelse
- (el._namespace == .html and case_insensitive_html_attributes.has(attr.name.str()));
+ const case_insensitive = switch (attr.value_case) {
+ .sensitive => false,
+ .insensitive => true,
+ .html_legacy => el._namespace == .html,
+ };
switch (attr.matcher) {
.presence => return true,
@@ -504,58 +507,6 @@ fn matchesAttribute(el: *Node.Element, attr: Selector.Attribute) bool {
}
}
-// Attributes whose values match ASCII case-insensitively on HTML elements
-// unless the selector has an `s` flag.
-// https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
-const case_insensitive_html_attributes = std.StaticStringMap(void).initComptime(.{
- .{ "accept", {} },
- .{ "accept-charset", {} },
- .{ "align", {} },
- .{ "alink", {} },
- .{ "axis", {} },
- .{ "bgcolor", {} },
- .{ "charset", {} },
- .{ "checked", {} },
- .{ "clear", {} },
- .{ "codetype", {} },
- .{ "color", {} },
- .{ "compact", {} },
- .{ "declare", {} },
- .{ "defer", {} },
- .{ "dir", {} },
- .{ "direction", {} },
- .{ "disabled", {} },
- .{ "enctype", {} },
- .{ "face", {} },
- .{ "frame", {} },
- .{ "hreflang", {} },
- .{ "http-equiv", {} },
- .{ "lang", {} },
- .{ "language", {} },
- .{ "link", {} },
- .{ "media", {} },
- .{ "method", {} },
- .{ "multiple", {} },
- .{ "nohref", {} },
- .{ "noresize", {} },
- .{ "noshade", {} },
- .{ "nowrap", {} },
- .{ "readonly", {} },
- .{ "rel", {} },
- .{ "rev", {} },
- .{ "rules", {} },
- .{ "scope", {} },
- .{ "scrolling", {} },
- .{ "selected", {} },
- .{ "shape", {} },
- .{ "target", {} },
- .{ "text", {} },
- .{ "type", {} },
- .{ "valign", {} },
- .{ "valuetype", {} },
- .{ "vlink", {} },
-});
-
fn attributeContainsWord(value: []const u8, word: []const u8) bool {
var remaining = value;
while (remaining.len > 0) {
diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zig
index 1179987de..f6dd3ee7d 100644
--- a/src/browser/webapi/selector/Parser.zig
+++ b/src/browser/webapi/selector/Parser.zig
@@ -1081,7 +1081,6 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
else
lp.String.wrap(attr_name);
- var case_insensitive: ?bool = null;
_ = self.skipSpaces();
// Unexpected EOF closes all open constructs per CSS Syntax, so a
@@ -1094,7 +1093,7 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
.name = name,
.matcher = .presence,
.original_name = .wrap(attr_name),
- .case_insensitive = case_insensitive,
+ .value_case = .sensitive,
};
}
@@ -1105,14 +1104,15 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
_ = self.skipSpaces();
// Parse optional case-sensitivity flag
+ var value_case: Selector.AttributeValueCase = if (case_insensitive_html_attributes.has(name.str())) .html_legacy else .sensitive;
if (std.ascii.toLower(self.peek()) == 'i') {
self.input = self.input[1..];
- case_insensitive = true;
+ value_case = .insensitive;
_ = self.skipSpaces();
} else if (std.ascii.toLower(self.peek()) == 's') {
// 's' flag means case-sensitive (explicit)
self.input = self.input[1..];
- case_insensitive = false;
+ value_case = .sensitive;
_ = self.skipSpaces();
}
@@ -1137,10 +1137,62 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
.name = name,
.matcher = matcher,
.original_name = .wrap(attr_name),
- .case_insensitive = case_insensitive,
+ .value_case = value_case,
};
}
+// Attributes whose values match ASCII case-insensitively on HTML elements
+// unless the selector has an `s` flag.
+// https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
+const case_insensitive_html_attributes = std.StaticStringMap(void).initComptime(.{
+ .{ "accept", {} },
+ .{ "accept-charset", {} },
+ .{ "align", {} },
+ .{ "alink", {} },
+ .{ "axis", {} },
+ .{ "bgcolor", {} },
+ .{ "charset", {} },
+ .{ "checked", {} },
+ .{ "clear", {} },
+ .{ "codetype", {} },
+ .{ "color", {} },
+ .{ "compact", {} },
+ .{ "declare", {} },
+ .{ "defer", {} },
+ .{ "dir", {} },
+ .{ "direction", {} },
+ .{ "disabled", {} },
+ .{ "enctype", {} },
+ .{ "face", {} },
+ .{ "frame", {} },
+ .{ "hreflang", {} },
+ .{ "http-equiv", {} },
+ .{ "lang", {} },
+ .{ "language", {} },
+ .{ "link", {} },
+ .{ "media", {} },
+ .{ "method", {} },
+ .{ "multiple", {} },
+ .{ "nohref", {} },
+ .{ "noresize", {} },
+ .{ "noshade", {} },
+ .{ "nowrap", {} },
+ .{ "readonly", {} },
+ .{ "rel", {} },
+ .{ "rev", {} },
+ .{ "rules", {} },
+ .{ "scope", {} },
+ .{ "scrolling", {} },
+ .{ "selected", {} },
+ .{ "shape", {} },
+ .{ "target", {} },
+ .{ "text", {} },
+ .{ "type", {} },
+ .{ "valign", {} },
+ .{ "valuetype", {} },
+ .{ "vlink", {} },
+});
+
fn attributeName(self: *Parser, arena: Allocator) ![]const u8 {
const input = self.input;
if (input.len == 0) {
diff --git a/src/browser/webapi/selector/Selector.zig b/src/browser/webapi/selector/Selector.zig
index 919c222d9..477b86b50 100644
--- a/src/browser/webapi/selector/Selector.zig
+++ b/src/browser/webapi/selector/Selector.zig
@@ -273,8 +273,15 @@ pub const Attribute = struct {
// foreign (SVG/MathML) elements, whose attributes are stored unnormalized.
original_name: String,
matcher: AttributeMatcher,
- // From an explicit `i` or `s` flag; null when the selector has none.
- case_insensitive: ?bool,
+ value_case: AttributeValueCase,
+};
+
+pub const AttributeValueCase = enum {
+ sensitive,
+ insensitive,
+ // No flag on an attribute whose value HTML compares without case: case
+ // insensitive on HTML elements only.
+ html_legacy,
};
pub const AttributeMatcher = union(enum) {