Lightpanda: the headless browser designed for AI and automation
`findElement`: accept the flags of a JavaScript regex literal
Adrià Arrufat5710f1d6840e
diff --git a/src/Regex.zig b/src/Regex.zigindex 6353467c1..96c6d1eca 100644--- a/src/Regex.zig+++ b/src/Regex.zig@@ -42,6 +42,10 @@ pub const Options = struct {/// works beyond ASCII. An invalid sequence in the subject fails to match/// rather than erroring. `\b` and `\w` stay ASCII, as in JavaScript.unicode: bool = false,+ /// JavaScript's `s`: `.` also matches a newline.+ dot_all: bool = false,+ /// JavaScript's `m`: `^` and `$` also match around newlines.+ multiline: bool = false,};/// Why a compile failed, for the caller to log or show.@@ -106,6 +110,8 @@ pub const Context = struct {var flags: u32 = 0;if (options.case_insensitive) flags |= pcre2.PCRE2_CASELESS;if (options.unicode) flags |= pcre2.PCRE2_UTF | pcre2.PCRE2_MATCH_INVALID_UTF;+ if (options.dot_all) flags |= pcre2.PCRE2_DOTALL;+ if (options.multiline) flags |= pcre2.PCRE2_MULTILINE;var err_code: c_int = 0;var err_offset: usize = 0;@@ -241,6 +247,25 @@ test "Regex: invalid patterns are errors, runaway ones no match" {try testing.expect(runaway.matches("a" ** 64));}+test "Regex: dot_all and multiline follow the JavaScript flags" {+ const context: *Context = try .init(testing.allocator);+ defer context.deinit();++ const dot = try context.compile("a.b", .{}, null);+ defer dot.deinit();+ try testing.expect(!dot.matches("a\nb"));+ const dot_all = try context.compile("a.b", .{ .dot_all = true }, null);+ defer dot_all.deinit();+ try testing.expect(dot_all.matches("a\nb"));++ const line = try context.compile("^b$", .{}, null);+ defer line.deinit();+ try testing.expect(!line.matches("a\nb"));+ const multiline = try context.compile("^b$", .{ .multiline = true }, null);+ defer multiline.deinit();+ try testing.expect(multiline.matches("a\nb"));+}+test "Regex: a diagnostic names the fault and where it is" {const context: *Context = try .init(testing.allocator);defer context.deinit();diff --git a/src/browser/tools.zig b/src/browser/tools.zigindex 35690099c..eac470790 100644--- a/src/browser/tools.zig+++ b/src/browser/tools.zig@@ -683,7 +683,7 @@ pub const Tool = enum {\\ "type": "object",\\ "properties": {\\ "role": { "type": "string", "description": "Optional ARIA role to match (e.g. 'button', 'link', 'textbox', 'checkbox')." },- \\ "name": { "type": "string", "description": "Optional accessible name to match, case-insensitive: a substring, or a JavaScript-syntax regex written as /.../ (unanchored: use ^...$ for the whole name; prefix (?-i) for case-sensitive)." }+ \\ "name": { "type": "string", "description": "Optional accessible name to match, case-insensitive: a substring, or a JavaScript regex literal such as /sign (in|up)/ (unanchored; flags i, m, s, u accepted; case-insensitive even without i, prefix (?-i) to make it case-sensitive)." }\\ }\\}),@@ -2073,23 +2073,32 @@ fn execFindElement(arena: std.mem.Allocator, session: *lp.Session, registry: *Noconst page = try requireFrame(session);- const pattern: ?[]const u8 = if (args.name) |name| regexBody(name) else null;+ const literal: ?RegexLiteral = if (args.name) |name| regexLiteral(name) else null;var diag: lp.Regex.Diagnostic = .{};- const name_regex: ?lp.Regex = if (pattern) |p|- session.browser.app.regex_context.compile(p, .{ .case_insensitive = true, .unicode = true }, &diag) catch |err| switch (err) {+ const name_regex: ?lp.Regex = if (literal) |lit| blk: {+ var options: lp.Regex.Options = .{ .case_insensitive = true, .unicode = true };+ for (lit.flags) |flag| switch (flag) {+ 'i', 'u' => {},+ 's' => options.dot_all = true,+ 'm' => options.multiline = true,+ else => return .{+ .text = try std.fmt.allocPrint(arena, "findElement: unsupported regex flag '{c}' in '{s}'", .{ flag, args.name.? }),+ .is_error = true,+ },+ };+ break :blk session.browser.app.regex_context.compile(lit.body, options, &diag) catch |err| switch (err) {error.OutOfMemory => return error.OutOfMemory,error.InvalidRegex => return .{- .text = try std.fmt.allocPrint(arena, "findElement: invalid name regex '{s}': {s} at offset {d}", .{ p, diag.message(), diag.offset }),+ .text = try std.fmt.allocPrint(arena, "findElement: invalid name regex '{s}': {s} at offset {d}", .{ lit.body, diag.message(), diag.offset }),.is_error = true,},- }- else- null;+ };+ } else null;defer if (name_regex) |re| re.deinit();const matched = lp.interactive.findInteractiveElements(page.document.asNode(), arena, page, .{.role = args.role,- .name = if (pattern == null) args.name else null,+ .name = if (literal == null) args.name else null,.name_regex = name_regex,}) catch return ToolError.InternalError;@@ -2098,11 +2107,22 @@ fn execFindElement(arena: std.mem.Allocator, session: *lp.Session, registry: *Noreturn .{ .text = try renderJson(arena, matched) };}-/// The body of a `/.../` literal, the spelling adblock lists use for a regex-/// too. Unanchored, so a name that really is written that way still matches.-fn regexBody(text: []const u8) ?[]const u8 {- if (text.len > 2 and text[0] == '/' and text[text.len - 1] == '/') return text[1 .. text.len - 1];- return null;+const RegexLiteral = struct {+ body: []const u8,+ flags: []const u8,+};++/// A `/body/flags` literal as JavaScript writes it, and the spelling adblock+/// lists use for a regex too. Anything after the closing slash that is not a+/// letter makes the whole thing plain text again. Matching is unanchored, so+/// a name that really is written as `/foo/` still matches itself.+fn regexLiteral(text: []const u8) ?RegexLiteral {+ if (text.len < 3 or text[0] != '/') return null;+ const close = 1 + (std.mem.lastIndexOfScalar(u8, text[1..], '/') orelse return null);+ if (close == 1) return null;+ const flags = text[close + 1 ..];+ for (flags) |flag| if (!std.ascii.isLower(flag)) return null;+ return .{ .body = text[1..close], .flags = flags };}fn execGetEnv(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 {diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex 0c3b59a03..47caf0aff 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -1431,6 +1431,35 @@ test "MCP - findElement" {try testing.expect(std.mem.indexOf(u8, out.written(), "missing closing parenthesis at offset 1") != null);out.clearRetainingCapacity();}++ {+ const msg =+ \\{"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"findElement","arguments":{"name":"/PREVENT.DEFAULT/i"}}}+ ;+ try router.handleMessage(server, aa, msg);+ try testing.expect(std.mem.indexOf(u8, out.written(), "Prevent Default") != null);+ out.clearRetainingCapacity();+ }++ {+ const msg =+ \\{"jsonrpc":"2.0","id":8,"method":"tools/call","params":{"name":"findElement","arguments":{"name":"/prevent/g"}}}+ ;+ try router.handleMessage(server, aa, msg);+ try testing.expect(std.mem.indexOf(u8, out.written(), "\"isError\":true") != null);+ try testing.expect(std.mem.indexOf(u8, out.written(), "unsupported regex flag 'g' in '/prevent/g'") != null);+ out.clearRetainingCapacity();+ }++ {+ // Text after the closing slash that is not a flag is a plain substring.+ const msg =+ \\{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"findElement","arguments":{"name":"/prevent/ default"}}}+ ;+ try router.handleMessage(server, aa, msg);+ try testing.expect(std.mem.indexOf(u8, out.written(), "[]") != null);+ out.clearRetainingCapacity();+ }}test "MCP - waitForSelector: existing element" {diff --git a/src/script/skill.zig b/src/script/skill.zigindex 0792a91f5..6aadce0d4 100644--- a/src/script/skill.zig+++ b/src/script/skill.zig@@ -203,7 +203,7 @@ fn note(tool: browser_tools.Tool) []const u8 {.screenshot => "`path` is required: writes a PNG of the text layout.",.press => "Selector first! `page.press(\"Enter\")` binds \"Enter\" to `selector` and fails — use `page.press(null, \"Enter\")` or `page.press({ key: \"Enter\" })`.",.click, .fill, .scroll, .hover, .selectOption, .setChecked => "",- .findElement => "`name` is a case-insensitive substring, or a JS-syntax regex written as `/.../`.",+ .findElement => "`name` is a case-insensitive substring, or a JS regex literal like `/sign (in|up)/i` (case-insensitive even without `i`).",.search, .markdown, .html, .links, .tree, .nodeDetails, .interactiveElements, .structuredData, .detectForms, .consoleLogs, .getUrl, .getCookies, .getEnv => "",};}