Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: match HTML's case-insensitive attribute values

Jay Hemnani3bef36bc1f4f
diff --git a/src/browser/tests/document/query_selector_attributes.html b/src/browser/tests/document/query_selector_attributes.html
index 522638e92..823c8143c 100644
--- a/src/browser/tests/document/query_selector_attributes.html
+++ b/src/browser/tests/document/query_selector_attributes.html
@@ -123,3 +123,19 @@
const el2 = document.querySelector("link[imagesrcset='url1.png 1x, url2.png 2x']");
testing.expectEqual('preload-link', el2.id);
</script>
+
+<form id="caseForm" method="POST"><input id="caseCheckbox" type="Checkbox"><input name="Q"></form>
+<svg><g id="caseSvg" type="Matrix"></g></svg>
+
+<script id="valueCase">
+ // Values of some HTML attributes (method, type, ...) match case-insensitively
+ testing.expectEqual('caseForm', document.querySelector('form[method=post]')?.id);
+ 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);
+
+ // Other attributes and foreign elements stay case-sensitive
+ testing.expectEqual(null, document.querySelector('[name=q]'));
+ testing.expectEqual(null, document.querySelector('#caseSvg[type=matrix]'));
+ testing.expectEqual('caseSvg', document.querySelector('#caseSvg[type=matrix i]')?.id);
+</script>
diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zig
index 2978bd0f6..68cc53ef3 100644
--- a/src/browser/webapi/selector/List.zig
+++ b/src/browser/webapi/selector/List.zig
@@ -444,28 +444,31 @@ 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()));
+
switch (attr.matcher) {
.presence => return true,
.exact => |expected| {
- return if (attr.case_insensitive)
+ return if (case_insensitive)
std.ascii.eqlIgnoreCase(value, expected)
else
std.mem.eql(u8, value, expected);
},
.substring => |expected| {
- return if (attr.case_insensitive)
+ return if (case_insensitive)
std.ascii.findIgnoreCase(value, expected) != null
else
std.mem.find(u8, value, expected) != null;
},
.starts_with => |expected| {
- return if (attr.case_insensitive)
+ return if (case_insensitive)
std.ascii.startsWithIgnoreCase(value, expected)
else
std.mem.startsWith(u8, value, expected);
},
.ends_with => |expected| {
- return if (attr.case_insensitive)
+ return if (case_insensitive)
std.ascii.endsWithIgnoreCase(value, expected)
else
std.mem.endsWith(u8, value, expected);
@@ -474,7 +477,7 @@ fn matchesAttribute(el: *Node.Element, attr: Selector.Attribute) bool {
// Space-separated word match (like class names)
var it = std.mem.tokenizeAny(u8, value, &std.ascii.whitespace);
while (it.next()) |word| {
- const same = if (attr.case_insensitive)
+ const same = if (case_insensitive)
std.ascii.eqlIgnoreCase(word, expected)
else
std.mem.eql(u8, word, expected);
@@ -485,7 +488,7 @@ fn matchesAttribute(el: *Node.Element, attr: Selector.Attribute) bool {
},
.prefix_dash => |expected| {
// Matches value or value- prefix (for language codes like en, en-US)
- if (attr.case_insensitive) {
+ if (case_insensitive) {
if (std.ascii.eqlIgnoreCase(value, expected)) return true;
if (value.len > expected.len and value[expected.len] == '-') {
return std.ascii.eqlIgnoreCase(value[0..expected.len], expected);
@@ -501,6 +504,58 @@ 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 2a4345a8a..1179987de 100644
--- a/src/browser/webapi/selector/Parser.zig
+++ b/src/browser/webapi/selector/Parser.zig
@@ -1081,7 +1081,7 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {
else
lp.String.wrap(attr_name);
- var case_insensitive = false;
+ var case_insensitive: ?bool = null;
_ = self.skipSpaces();
// Unexpected EOF closes all open constructs per CSS Syntax, so a
diff --git a/src/browser/webapi/selector/Selector.zig b/src/browser/webapi/selector/Selector.zig
index 8e0768fa2..919c222d9 100644
--- a/src/browser/webapi/selector/Selector.zig
+++ b/src/browser/webapi/selector/Selector.zig
@@ -273,7 +273,8 @@ pub const Attribute = struct {
// foreign (SVG/MathML) elements, whose attributes are stored unnormalized.
original_name: String,
matcher: AttributeMatcher,
- case_insensitive: bool,
+ // From an explicit `i` or `s` flag; null when the selector has none.
+ case_insensitive: ?bool,
};
pub const AttributeMatcher = union(enum) {