Skip to content

Lightpanda: the headless browser designed for AI and automation

selector: match :valid and :invalid on every validity constraint

Jay Hemnanidc5634e01b21
diff --git a/src/browser/tests/document/query_selector_all.html b/src/browser/tests/document/query_selector_all.html
index 460077401..883dd6f39 100644
--- a/src/browser/tests/document/query_selector_all.html
+++ b/src/browser/tests/document/query_selector_all.html
@@ -430,6 +430,41 @@
}
</script>
+<form id="form-constraints">
+ <input id="vc-email" type="email" value="bad">
+ <input id="vc-pattern" pattern="[0-9]+" value="12a">
+ <input id="vc-min" type="number" min="1" value="0">
+ <input id="vc-custom" value="x">
+ <textarea id="vc-textarea" required></textarea>
+ <input id="vc-ok" value="fine">
+ <input id="vc-disabled" required disabled>
+</form>
+<form id="form-disabled-only">
+ <input id="vc-disabled-only" required disabled>
+</form>
+
+<script id=validityConstraints>
+{
+ const ids = (sel) => Array.from(document.querySelectorAll(sel)).map((e) => e.id).join(' ');
+ document.getElementById('vc-custom').setCustomValidity('nope');
+
+ // every constraint the validity API reports, not just a missing value
+ testing.expectEqual('form-constraints vc-email vc-pattern vc-min vc-custom vc-textarea', ids('#form-constraints:invalid, #form-constraints :invalid'));
+ testing.expectEqual('vc-ok', ids('#form-constraints :valid'));
+
+ // disabled controls are barred from constraint validation
+ testing.expectEqual('form-disabled-only', ids('#form-disabled-only:valid, #form-disabled-only :valid, #form-disabled-only :invalid'));
+
+ document.getElementById('vc-email').value = '[email protected]';
+ document.getElementById('vc-pattern').value = '12';
+ document.getElementById('vc-min').value = '3';
+ document.getElementById('vc-custom').setCustomValidity('');
+ document.getElementById('vc-textarea').value = 't';
+ testing.expectEqual(true, document.getElementById('form-constraints').matches(':valid'));
+ testing.expectEqual('', ids('#form-constraints :invalid'));
+}
+</script>
+
<script id=indeterminatePseudo>
{
const cb = document.getElementById('vi-checkbox');
diff --git a/src/browser/webapi/selector/List.zig b/src/browser/webapi/selector/List.zig
index 6cf4ac87c..c52422511 100644
--- a/src/browser/webapi/selector/List.zig
+++ b/src/browser/webapi/selector/List.zig
@@ -23,6 +23,7 @@ const Page = @import("../../Page.zig");
const Frame = @import("../../Frame.zig");
const Node = @import("../Node.zig");
+const ValidityState = @import("../element/html/ValidityState.zig");
const Part = @import("Selector.zig").Part;
const Selector = @import("Selector.zig");
const TreeWalker = @import("../TreeWalker.zig").Full;
@@ -568,40 +569,16 @@ fn matchesPseudoClass(el: *Node.Element, pseudo: Selector.PseudoClass, scope: *N
// Form validation
.valid => {
- if (el.is(Node.Element.Html.Input)) |input| {
- return switch (input._input_type) {
- .hidden, .submit, .reset, .button => false,
- else => !input.getRequired() or input.getValue().len > 0,
- };
- }
- if (el.is(Node.Element.Html.Select)) |select| {
- return !select.getRequired() or select.getValue(frame).len > 0;
- }
if (el.is(Node.Element.Html.Form) != null or el.is(Node.Element.Html.FieldSet) != null) {
return !hasInvalidDescendant(node, frame);
}
- if (el.is(Node.Element.Html.Custom)) |custom| {
- return isCustomValid(custom) orelse false;
- }
- return false;
+ return isControlValid(el, frame) orelse false;
},
.invalid => {
- if (el.is(Node.Element.Html.Input)) |input| {
- return switch (input._input_type) {
- .hidden, .submit, .reset, .button => false,
- else => input.getRequired() and input.getValue().len == 0,
- };
- }
- if (el.is(Node.Element.Html.Select)) |select| {
- return select.getRequired() and select.getValue(frame).len == 0;
- }
if (el.is(Node.Element.Html.Form) != null or el.is(Node.Element.Html.FieldSet) != null) {
return hasInvalidDescendant(node, frame);
}
- if (el.is(Node.Element.Html.Custom)) |custom| {
- return isCustomValid(custom) == false;
- }
- return false;
+ return isControlValid(el, frame) == false;
},
.required => {
return el.hasAttributeInterned("required");
@@ -785,21 +762,34 @@ fn isCustomValid(custom: *Node.Element.Html.Custom) ?bool {
return internals._flags.any() == false;
}
+// null when the element is not a candidate for constraint validation
+fn isControlValid(el: *Node.Element, frame: *Frame) ?bool {
+ const Html = Node.Element.Html;
+ if (el.is(Html.Custom)) |custom| {
+ return isCustomValid(custom);
+ }
+ const will_validate = if (el.is(Html.Input)) |input|
+ input.getWillValidate()
+ else if (el.is(Html.Select)) |select|
+ select.getWillValidate()
+ else if (el.is(Html.TextArea)) |textarea|
+ textarea.getWillValidate()
+ else if (el.is(Html.Button)) |button|
+ button.getWillValidate()
+ else
+ return null;
+ if (!will_validate) {
+ return null;
+ }
+ const validity: ValidityState = .{ ._owner = el };
+ return validity.getValid(frame);
+}
+
fn hasInvalidDescendant(parent: *Node, frame: *Frame) bool {
var child = parent.firstChild();
while (child) |c| {
if (c.is(Node.Element)) |child_el| {
- if (child_el.is(Node.Element.Html.Input)) |input| {
- const invalid = switch (input._input_type) {
- .hidden, .submit, .reset, .button => false,
- else => input.getRequired() and input.getValue().len == 0,
- };
- if (invalid) return true;
- } else if (child_el.is(Node.Element.Html.Select)) |select| {
- if (select.getRequired() and select.getValue(frame).len == 0) return true;
- } else if (child_el.is(Node.Element.Html.Custom)) |custom| {
- if (isCustomValid(custom) == false) return true;
- }
+ if (isControlValid(child_el, frame) == false) return true;
}
if (hasInvalidDescendant(c, frame)) return true;
child = c.nextSibling();