614 Commits

Author SHA1 Message Date
Karl Seguin
ad54437ca3 zig fmt 2026-03-28 21:43:46 +08:00
Karl Seguin
01ecb296e5 Rework finalizers
This commit involves a number of changes to finalizers, all aimed towards
better consistency and reliability.

A big part of this has to do with v8::Inspector's ability to move objects
across IsolatedWorlds. There has been a few previous efforts on this, the most
significant being https://github.com/lightpanda-io/browser/pull/1901. To recap,
a Zig instance can map to 0-N v8::Objects. Where N is the total number of
IsolatedWorlds. Generally, IsolatedWorlds between origins are...isolated...but
the v8::Inspector isn't bound by this. So a Zig instance cannot be tied to a
Context/Identity/IsolatedWorld...it has to live until all references, possibly
from different IsolatedWorlds, are released (or the page is reset).

Finalizers could previously be managed via reference counting or explicitly
toggling the instance as weak/strong. Now, only reference counting is supported.
weak/strong can essentially be seen as an acquireRef (rc += 1) and
releaseRef (rc -= 1). Explicit setting did make some things easier, like not
having to worry so much about double-releasing (e.g. XHR abort being called
multiple times), but it was only used in a few places AND it simply doesn't work
with objects shared between IsolatedWorlds. It is never a boolean now, as 3
different IsolatedWorlds can each hold a reference.

Temps and Globals are tracked on the Session. Previously, they were tracked on
the Identity, but that makes no sense. If a Zig instance can outlive an Identity,
then any of its Temp references can too. This hasn't been a problem because we've
only seen MutationObserver and IntersectionObserver be used cross-origin,
but the right CDP script can make this crash with a use-after-free (e.g.
`MessageEvent.data` is released when the Identity is done, but `MessageEvent` is
still referenced by a different IsolateWorld).

Rather than deinit with a `comptime shutdown: bool`, there is now an explicit
`releaseRef` and `deinit`.

Bridge registration has been streamlined. Previously, types had to register
their finalizer AND acquireRef/releaseRef/deinit had to be declared on the entire
prototype chain, even if these methods just delegated to their proto. Finalizers
are now automatically enabled if a type has a `acquireRef` function. If a type
has an `acquireRef`, then it must have a `releaseRef` and a `deinit`. So if
there's custom cleanup to do in `deinit`, then you also have to define
`acquireRef` and `releaseRef` which will just delegate to the _proto.

Furthermore these finalizer methods can be defined anywhere on the chain.

Previously:

```zig
const KeywboardEvent = struct {
  _proto: *Event,
  ...

  pub fn deinit(self: *KeyboardEvent, session: *Session) void {
    self._proto.deinit(session);
  }

  pub fn releaseRef(self: *KeyboardEvent, session: *Session) void {
    self._proto.releaseRef(session);
  }
}
```

```zig
const KeyboardEvent = struct {
  _proto: *Event,
  ...
  // no deinit, releaseRef, acquireref
}
```

Since the `KeyboardEvent` doesn't participate in finalization directly, it
doesn't have to define anything. The bridge will detect the most specific place
they are defined and call them there.
2026-03-28 21:11:23 +08:00
Karl Seguin
67bd555e75 Merge pull request #2013 from lightpanda-io/cleanup_dead_code_removal
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Remove unused imports
2026-03-27 13:52:49 +08:00
Adrià Arrufat
a10e533701 Remove more unused imports 2026-03-27 14:24:17 +09:00
Karl Seguin
0065677273 Merge pull request #2011 from lightpanda-io/mcp-fixes
MCP fixes
2026-03-27 13:02:59 +08:00
Karl Seguin
226d9bfc6f zig fmt 2026-03-27 12:47:24 +08:00
Karl Seguin
2e65ae632e Merge pull request #2009 from lightpanda-io/fix/issue-1960
mcp: improve argument parsing error handling
2026-03-27 12:46:34 +08:00
Karl Seguin
ea422075c7 Remove unused imports
And some smaller cleanups.
2026-03-27 12:45:26 +08:00
Adrià Arrufat
1d54e6944b mcp: send error response when message is too long 2026-03-27 11:36:18 +09:00
Adrià Arrufat
de32e5cf34 mcp: handle missing request IDs safely 2026-03-27 11:34:06 +09:00
Adrià Arrufat
c8d8ca5e94 mcp: improve error handling in resources and tools
- Handle failures during HTML, Markdown, and link serialization.
- Return MCP internal errors when result serialization fails.
- Refactor resource reading logic for better clarity and consistency.
2026-03-27 11:28:47 +09:00
Adrià Arrufat
da0828620f mcp: improve argument parsing error handling
Closes #1960
2026-03-27 10:04:45 +09:00
Adrià Arrufat
cdd33621e3 Merge pull request #2005 from lightpanda-io/mcp-lp-node-registry
MCP/CDP: unify node registration
2026-03-27 09:36:08 +09:00
Karl Seguin
8001709506 Merge pull request #2002 from lightpanda-io/nikneym/form-data-event
Support `FormDataEvent`
2026-03-27 08:16:32 +08:00
Karl Seguin
a0ae6b4c92 Merge pull request #2008 from buley/feature/fix-scanner-warnings
chore: fix dead code and error swallowing warnings
2026-03-27 08:10:31 +08:00
Karl Seguin
fdf7f5267a Merge pull request #2001 from lightpanda-io/refactor/mcp-tools-dedup
mcp: extract parseOptionalAndGetPage helper
2026-03-27 07:58:18 +08:00
Taylor
88e0b39d6b chore: fix dead code and error swallowing warnings
Fixes issues reported by polyglot-scanner:
- Removed explicit `return` keywords and trailing semicolons to resolve DEAD_CODE/DEAD_BRANCH warnings.
- Replaced `epoch::advance().unwrap()` and `stats::resident::read().unwrap()` with safer alternatives (`drop` and `unwrap_or(0)`) to resolve ERROR_SWALLOW warnings.
- Replaced `let _ = Box::from_raw(...)` with `drop(Box::from_raw(...))` to correctly drop the box while fixing the ERROR_SWALLOW warning.
2026-03-26 09:58:49 -07:00
Pierre Tachoire
f95396a487 Merge pull request #1998 from lightpanda-io/url_origin_fix
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Improve authority parsing
2026-03-26 17:32:40 +01:00
Pierre Tachoire
d02d05b246 Merge pull request #2004 from lightpanda-io/nikneym/resize-unobserver
`ResizeObserver`: make `unobserve` available in JS context
2026-03-26 16:48:42 +01:00
Pierre Tachoire
7b2d817d0e Merge pull request #2003 from lightpanda-io/nikneym/canvas-access-canvas
`CanvasRenderingContext2D`: make canvas able to access canvas element
2026-03-26 16:48:11 +01:00
Adrià Arrufat
7e778a17d6 MCP/CDP: unify node registration
This fixes a bug in MCP where interactive elements were not assigned
a backendNodeId, preventing agents from clicking or filling them. Also
extracts link collection to a shared browser module.
2026-03-26 23:51:43 +09:00
Pierre Tachoire
a0dd14aaad Merge pull request #1999 from lightpanda-io/wait_until_default
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Fix --wait-until default value.
2026-03-26 15:03:59 +01:00
Halil Durak
d447d1e3c7 ResizeObserver: make unobserve available in JS context 2026-03-26 16:37:17 +03:00
Halil Durak
8684d35394 add tests 2026-03-26 16:35:23 +03:00
Halil Durak
e243f96988 CanvasRenderingContext2D: make canvas able to access canvas element 2026-03-26 16:35:13 +03:00
Pierre Tachoire
7ea8f3f766 Merge pull request #2000 from lightpanda-io/add-pre-version
add a -Dpre_version build flag for custom pre version
2026-03-26 12:06:38 +01:00
Halil Durak
5e6082b5e9 FormDataEvent: add tests 2026-03-26 14:04:03 +03:00
Halil Durak
1befd9a5e8 make comment on SubmitEvent doc-comment 2026-03-26 14:03:51 +03:00
Halil Durak
e103ce0f39 FormDataEvent: initial support 2026-03-26 14:03:33 +03:00
Adrià Arrufat
14fa2da2ad mcp: remove duplicate code in testLoadPage 2026-03-26 19:57:14 +09:00
Pierre Tachoire
28cc60adb0 add a -Dpre_version build flag for custom pre version 2026-03-26 11:52:16 +01:00
Adrià Arrufat
96d24b5dc6 mcp: extract parseOptionalAndGetPage helper
Deduplicate the repeated "parse optional URL, maybe navigate, get page"
pattern across 6 MCP tool handlers (markdown, links, semantic_tree,
interactiveElements, structuredData, detectForms).
2026-03-26 19:44:44 +09:00
Karl Seguin
c14a9ad986 Merge pull request #1992 from navidemad/cdp-page-reload
CDP: implement Page.reload
2026-03-26 18:14:49 +08:00
Karl Seguin
679f2104f4 Fix --wait-until default value.
This was `load`, but it should have been (and was documented as `done`). This
is my fault. Sorry.

Should help with: https://github.com/lightpanda-io/browser/issues/1947#issuecomment-4120597764
2026-03-26 18:06:14 +08:00
Navid EMAD
c6b0c75106 Address review: use arena.dupeZ for URL copy, add try to testing.context()
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 10:09:48 +01:00
Navid EMAD
93485c1ef3 CDP: implement Page.reload
Add `Page.reload` to the CDP Page domain dispatch. Reuses the existing
`page.navigate()` path with `NavigationKind.reload`, matching what
`Location.reload` already does for the JS `location.reload()` API.

Accepts the standard CDP params (`ignoreCache`, `scriptToEvaluateOnLoad`)
per the Chrome DevTools Protocol spec.

The current page URL is copied to the stack before `replacePage()` to
avoid a use-after-free when the old page's arena is freed.

This unblocks CDP clients (Puppeteer, capybara-lightpanda, etc.) that
call `Page.reload` and currently get `UnknownMethod`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 10:09:48 +01:00
Karl Seguin
0324d5c232 Merge pull request #1997 from lightpanda-io/update-zig-v8
build: bump zig-v8 to v0.3.7
2026-03-26 16:01:40 +08:00
Karl Seguin
0588cc374d Improve authority parsing
Only look for @ within the first part of the url (up to the first possible
separator, i.e /, # or ?). This fixes potentially incorrect (and insecure)
getOrigin and getHost, both of which use the new helper.

Also make port parsing IPv6-aware.
2026-03-26 13:22:56 +08:00
Adrià Arrufat
a75c0cf08d build: bump zig-v8 to v0.3.7 2026-03-26 12:34:10 +09:00
Karl Seguin
2812b8f07c Merge pull request #1991 from lightpanda-io/v8_signature
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Set v8::Signature on FunctionTemplates
2026-03-26 09:27:22 +08:00
Karl Seguin
e2afbec29d update v8 dep 2026-03-26 09:17:32 +08:00
Karl Seguin
a45f9cb810 Set v8::Signature on FunctionTemplates
This causes v8 to verify the receiver of a function, and prevents calling an
accessor or function with the wrong receiver, e.g.:

```
const g = Object.getOwnPropertyDescriptor(Window.prototype, 'document').get;
g.call(null);
```

A few other cleanups in this commit:
1 - Define any accessor with a getter as ReadOnly
2 - Ability to define an accessor with the DontDelete attribute
    (window.document and window.location)
3 - Replace v8__ObjectTemplate__SetAccessorProperty__DEFAULTX overloads with
    new v8__ObjectTemplate__SetAccessorProperty__Config
4 - Remove unnecessary @constCast for FunctionTemplate which can be const
    everywhere.
2026-03-26 09:15:33 +08:00
Karl Seguin
cf641ed458 Merge pull request #1990 from lightpanda-io/remove_cdp_generic
Remove cdp generic
2026-03-26 07:49:13 +08:00
Karl Seguin
0fc959dcc5 re-anble unreachable 2026-03-26 07:42:45 +08:00
Karl Seguin
077376ea04 Merge pull request #1985 from lightpanda-io/intersection_observer_root_document
Allow Document to be the root of an intersection observer
2026-03-26 07:41:40 +08:00
Karl Seguin
6ed8d1d201 Merge pull request #1981 from lightpanda-io/window_cross_origin
Window cross origin
2026-03-26 07:41:22 +08:00
Karl Seguin
5207bd4202 Merge pull request #1980 from lightpanda-io/frames_test
Improve async tests
2026-03-26 07:41:05 +08:00
Karl Seguin
11ed95290b Improve async tests
testing.async(...) is pretty lame. It works for simple cases, where the
microtask is very quickly resolved, but otherwise can't block the test from
exiting.

This adds an overload to testing.async and leverages the new Runner
https://github.com/lightpanda-io/browser/pull/1958 to "tick" until completion
(or timeout).

The overloaded version of testing.async() (called without a callback) will
increment a counter which is only decremented with the promise is resolved. The
test runner will now `tick` until the counter == 0.
2026-03-26 07:35:05 +08:00
Pierre Tachoire
a876275828 Merge pull request #1995 from lightpanda-io/ci-wba-test
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
ci: don't run wba test on PR
2026-03-25 18:09:26 +01:00
Pierre Tachoire
e83b8aa36d ci: don't run wba test on PR
wba test requires secrets read to run.
But we don't want to exposes secrets on external contributions.
So it's easier to run it only after PR merged.
2026-03-25 16:55:45 +01:00
Halil Durak
179f9c1169 Merge pull request #1984 from navidemad/fix-submit-event-submitter
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Fix Form.requestSubmit(submitter) not setting SubmitEvent.submitter
2026-03-25 15:39:57 +03:00
Karl Seguin
ca41bb5fa2 fix import casing 2026-03-25 17:54:24 +08:00
Pierre Tachoire
9c37961042 Merge pull request #1989 from lightpanda-io/licensing.md
update LICENSING.md
2026-03-25 10:43:57 +01:00
Karl Seguin
0dd0495ab8 Removes CDPT (generic CDP)
CDPT used to be a generic so that we could inject Browser, Session, Page and
Client. At some point, it [thankfully] became a generic only to inject Client.

This commit removes the generic and bakes the *Server.Client instance in CDP.
It uses a socketpair for testing.

BrowserContext is still generic, but that's generic for a very different reason
and, while I'd like to remove that generic too, it belongs in a different PR.
2026-03-25 17:43:30 +08:00
Pierre Tachoire
c9fa76da0c update LICENSING.md 2026-03-25 10:42:52 +01:00
Halil Durak
7718184e22 Merge pull request #1983 from lightpanda-io/nikneym/crypto-changes
Small `SubtleCrypto` refactor
2026-03-25 11:23:13 +03:00
Karl Seguin
b81b41cbf0 Merge pull request #1987 from lightpanda-io/conn-close
handle Connection: close without TLS close_notify
2026-03-25 16:11:42 +08:00
Pierre Tachoire
3a0cead03a Merge pull request #1917 from lightpanda-io/semantic-versioning
build: automate version resolution in build.zig
2026-03-25 08:46:04 +01:00
Pierre Tachoire
92ce6a916a http: don't check transfer._header_done_called on RecvError 2026-03-25 08:23:23 +01:00
Adrià Arrufat
130bf7ba11 Merge pull request #1951 from mvanhorn/osc/feat-mcp-detect-forms
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
mcp: add detectForms tool for structured form discovery
2026-03-25 14:23:09 +09:00
Adrià Arrufat
2e40354a3a forms: add tests for input values and form defaults 2026-03-25 09:39:52 +09:00
Adrià Arrufat
3074bde2f3 forms: always include required and disabled fields 2026-03-25 09:35:17 +09:00
Adrià Arrufat
ed9f5aae2e docs(forms): clarify arena allocator requirement for collectForms 2026-03-25 09:33:10 +09:00
Adrià Arrufat
8e315e551a forms: extract form node registration logic 2026-03-25 09:30:06 +09:00
Pierre Tachoire
bad690da65 handle Connection: close without TLS close_notify
Some servers (e.g. ec.europa.eu) close the TCP connection without
sending a TLS close_notify alert after responding with Connection: close.
BoringSSL treats this as a fatal error, which libcurl surfaces as
CURLE_RECV_ERROR. If we already received valid HTTP headers and the
response included Connection: close, the connection closure is the
expected end-of-body signal per HTTP/1.1 — treat it as success.

You can reproduce with
```
lightpanda fetch https://ec.europa.eu/commission/presscorner/detail/en/ip_26_614
```
2026-03-24 21:20:59 +01:00
Karl Seguin
ae080f32eb Allow Document to be the root of an intersection observer
We previously only supported an Element. null == viewport, but document means
the entire (scrollable) area, since we don't render anything, treating
document  as null seems ok?
2026-03-24 21:48:38 +08:00
Pierre Tachoire
c5c1d1f2f8 tag next version to 1.0.0 2026-03-24 14:47:20 +01:00
Pierre Tachoire
eb18dc89f6 ci: use -Dversion_string for release (nightly) build 2026-03-24 14:46:48 +01:00
Navid EMAD
afb0c29243 Add submit_event case to Event.Type exhaustive switch
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 14:24:35 +01:00
Navid EMAD
267eee9693 Fix Form.requestSubmit(submitter) not setting SubmitEvent.submitter
Create SubmitEvent type and use it in submitForm() so that
e.submitter is correctly set when requestSubmit(submitter) is called.

Fixes #1982

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 14:19:50 +01:00
Halil Durak
39352a6bda refactor SubtleCrypto
I've been thinking the implementation here is messy (ever since we added support for it) and thought it would be better to separate each algorithm to their respective files in order to maintain in a long run. `digest` is also refactored to prefer libcrypto instead of std.
2026-03-24 16:04:50 +03:00
Halil Durak
0838b510f8 src/crypto.zig -> src/sys/libcrypto.zig
Now that we have `sys/`, I think this makes more sense.
2026-03-24 16:04:49 +03:00
Karl Seguin
b19f30d865 Start allowing some cross-origin scripting.
There are a few things allowed in cross origin scripting, the most important
being window.postMessage and window.parent.

This commit changes window-returning functions (e.g. window.top, window.parent
iframe.contentWindow) from always returning a *Window, to conditionally
returning a *Window or a *CrossOriginWindow. The CrossOriginWindow only allows
a few methods (e.g. postMessage).
2026-03-24 19:27:55 +08:00
Karl Seguin
35be9f897f Improve async tests
testing.async(...) is pretty lame. It works for simple cases, where the
microtask is very quickly resolved, but otherwise can't block the test from
exiting.

This adds an overload to testing.async and leverages the new Runner
https://github.com/lightpanda-io/browser/pull/1958 to "tick" until completion
(or timeout).

The overloaded version of testing.async() (called without a callback) will
increment a counter which is only decremented with the promise is resolved. The
test runner will now `tick` until the counter == 0.
2026-03-24 17:21:39 +08:00
Karl Seguin
d517488158 Merge pull request #1979 from lightpanda-io/dash-command-line-arguments
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Support (and prefer) dash-separated command line arguments
2026-03-24 17:12:09 +08:00
Karl Seguin
fee8fe7830 Merge pull request #1978 from lightpanda-io/eventually_rename_onload
Rename testing.eventually to testing.onload, to make it more clear
2026-03-24 17:11:49 +08:00
Karl Seguin
428190aecc Merge pull request #1972 from lightpanda-io/fix-issue-1970
Fix Expo Web crash by gracefully handling at-rules in CSSStyleSheet.insertRule
2026-03-24 13:52:09 +08:00
Karl Seguin
61dabdedec Support (and prefer) dash-separated command line arguments
--log_level -> --log-level

Underscored arguments are still supported for backwards compatibility.
2026-03-24 12:55:08 +08:00
Karl Seguin
dfd9f216bd Rename testing.eventually to testing.onload, to make it more clear 2026-03-24 12:21:46 +08:00
Adrià Arrufat
567cd97312 webapi.Element: centralize disabled state logic 2026-03-24 13:13:53 +09:00
Adrià Arrufat
0bfe00bbb7 css: disallow multiple rules in insertRule 2026-03-24 12:53:49 +09:00
Adrià Arrufat
260768463b Merge branch 'main' into osc/feat-mcp-detect-forms 2026-03-24 09:25:47 +09:00
Adrià Arrufat
fd96cd6eb9 chore(css): log unimplemented at-rules in insertRule 2026-03-24 09:20:21 +09:00
Adrià Arrufat
25a7b5b778 Merge pull request #1977 from lightpanda-io/check_dirty_once
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Only check StyleSheet dirty flag at the start (once) of operation
2026-03-24 09:12:10 +09:00
Karl Seguin
d4bcfa974f Only check StyleSheet dirty flag at the start (once) of operation 2026-03-24 07:55:11 +08:00
Karl Seguin
c91eac17d0 Merge pull request #1975 from lightpanda-io/percent-encode-path
fix: percent-encode pathname in URL.setPathname per URL spec
2026-03-24 07:41:27 +08:00
Karl Seguin
5c79961bb7 Merge pull request #1969 from lightpanda-io/fix_append_child_crash
Handle `appendAllChildren` mutating the list of children
2026-03-24 07:29:16 +08:00
Karl Seguin
a0c200bc49 Merge pull request #1968 from lightpanda-io/document_write_deleted_parent
Handle nested document.write where parent gets deleted
2026-03-24 07:29:08 +08:00
Karl Seguin
9ea39e1c34 Merge pull request #1967 from lightpanda-io/css_anchor_normalization
Anchor(...) css property normalization
2026-03-24 07:28:59 +08:00
Karl Seguin
f7125d2bf3 Merge pull request #1964 from lightpanda-io/currentSrc
Add Image.currentSrc and Media.currentSrc
2026-03-24 07:28:51 +08:00
Karl Seguin
b163d9709b Merge pull request #1959 from lightpanda-io/form_iterator
Expose form.iterator()
2026-03-24 07:28:31 +08:00
Karl Seguin
5453630955 Merge pull request #1958 from lightpanda-io/runner
Extract Session.wait into a Runner
2026-03-24 07:28:18 +08:00
Pierre Tachoire
8ada67637f fix: precent-encode hash and search 2026-03-23 17:22:50 +01:00
Adrià Arrufat
5972630e95 Update CSS parser to track skipped at-rules and refine insertRule logic 2026-03-24 00:54:20 +09:00
Pierre Tachoire
58c18114a5 fix: percent-encode pathname in URL.setPathname per URL spec
URL.setPathname() inserted the value verbatim without percent-encoding,
so `url.pathname = "c d"` produced `http://a/c d` instead of
`http://a/c%20d`. This caused sites using URL polyfills (e.g. Angular's
polyfills bundle) to detect broken native URL support and fall back to a
polyfill that relies on HTMLInputElement.checkValidity(), which is not
implemented — crashing the entire app bootstrap.
2026-03-23 16:52:39 +01:00
Pierre Tachoire
a94b0bec93 Merge pull request #1946 from lightpanda-io/cdp-response-body
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Encode non-utf8 Network.getResponseBody in base64
2026-03-23 16:46:12 +01:00
Adrià Arrufat
ff0fbb6b41 Fix Expo Web crash by gracefully handling at-rules in CSSStyleSheet.insertRule 2026-03-23 23:45:11 +09:00
Pierre Tachoire
797cae2ef8 encode captured response body during CDP call 2026-03-23 14:26:27 +01:00
Karl Seguin
433c03c709 Handle appendAllChildren mutating the list of children
`appendAllChildren` iterates through the children, but when a child is appended
it can mutate the DOM (only via a custom element connected callback AFAIK) which
can render the iterator invalid. Constantly get parent.firstChild() as the
target.
2026-03-23 21:16:11 +08:00
Karl Seguin
4d3e9feaf4 Handle nested document.write where parent gets deleted
Handles a real life case where a nested document.write mutates the DOM in a way
where there outer document.write loses its parent.
2026-03-23 21:00:02 +08:00
Karl Seguin
5700e214bf Merge pull request #1966 from lightpanda-io/mcp_tools_test
Improve MCP tools test
2026-03-23 20:47:42 +08:00
Karl Seguin
88d40a7dcd Anchor(...) css property normalization
Expands on https://github.com/lightpanda-io/browser/pull/1754 to do for
anchor(...) what we did for anchor-size(...)

fixes a number of WPT tests in:
/css/css-anchor-position/anchor-parse-valid.html
2026-03-23 20:32:03 +08:00
Karl Seguin
ff209f5adf Merge pull request #1955 from lightpanda-io/advertise_host
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Add --advertise_host option to serve command
2026-03-23 20:00:42 +08:00
Pierre Tachoire
8ad092a960 Merge pull request #1965 from lightpanda-io/katie-lpd-patch-1
Update README.md
2026-03-23 12:20:16 +01:00
Karl Seguin
0fcdc1d194 Improve MCP tools test
Add helper to navigate to page, to reduce the boilerplate in each test.

Reduce waitForSelector time from 200ms to 20ms to speed up tests.
2026-03-23 19:15:50 +08:00
Karl Seguin
60c2359fdd Merge pull request #1797 from lightpanda-io/css-improvements
Implement CSSOM and Enhanced Visibility Filtering
2026-03-23 19:11:35 +08:00
katie-lpd
08c8ba72f5 Update README.md
Update benchmark images and text to real benchmark over the network
2026-03-23 12:05:44 +01:00
Karl Seguin
cfa4201532 Add Image.currentSrc and Media.currentSrc 2026-03-23 18:20:15 +08:00
Karl Seguin
cb02eb000e Merge pull request #1961 from lightpanda-io/test_runner_fail_summary
Print summary of failed tests name at end of test runner
2026-03-23 18:13:37 +08:00
Karl Seguin
23334edc05 Merge pull request #1963 from lightpanda-io/nested_navigation
Use double-queue to better support recursive navigation
2026-03-23 18:13:18 +08:00
Karl Seguin
8dbe22a01a Use double-queue to better support recursive navigation
Enqueuing while processing the navigation queue is rare, but apparently can
happen. The most likely culprit is the microqueue task being processed which
enqueues a new navigation (e.g. when a promise resolves).

This was never well handled, with the possibility of a use-after-free or of
skipping the new navigation. This commit introduces a double queue, which is
swapped at the start of processing, so that we always have 1 list for queueing
new navigation requests, and one list that we're currently processing.
2026-03-23 18:00:04 +08:00
Adrià Arrufat
80235e2ddd test: fix scoping bug in frames test causing spurious failures 2026-03-23 16:04:21 +09:00
Karl Seguin
2abed9fe75 Print summary of failed tests name at end of test runner
Helps to see, at a glance, which test failed without having to scroll up through
the list.
2026-03-23 15:00:51 +08:00
Matt Van Horn
35551ac84e fix: add disabled flag, external form fields, and param ordering
Address review feedback from @karlseguin:

1. Use Form.getElements() instead of manual TreeWalker for field
   collection. This reuses NodeLive(.form) which handles fields
   outside the <form> via the form="id" attribute per spec.

2. Add disabled detection: checks both the element's disabled
   attribute and ancestor <fieldset disabled> (with first-legend
   exemption per spec). Fields are flagged rather than excluded -
   agents need visibility into disabled state.

3. allocator is now the first parameter in collectForms/helpers.

4. handleDetectForms returns InvalidParams on bad input instead
   of silently swallowing parse errors.

5. Added tests for disabled fields, disabled fieldsets, and
   external form fields via form="id".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 23:31:59 -07:00
Adrià Arrufat
c3a2318eca fix: pass allocator as first parameter in forms.zig 2026-03-23 15:27:49 +09:00
Adrià Arrufat
a6e801be59 forms: casting
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-23 15:12:16 +09:00
Adrià Arrufat
0bbe25ab5e forms: casting
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-23 15:12:02 +09:00
Adrià Arrufat
c37286f845 forms: casting
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-23 15:11:47 +09:00
Karl Seguin
34079913a3 Expose form.iterator()
Meant to help things like https://github.com/lightpanda-io/browser/pull/1951

Small optimization to form node_live iterator

Disable iframes test (not related, but they are super-flaky, and I'm tired of
CI's failing because of it. I'll look at them later today).
2026-03-23 13:12:22 +08:00
Adrià Arrufat
4f1b499d0f zig fmt 2026-03-23 13:52:28 +09:00
Karl Seguin
c9bc370d6a Extract Session.wait into a Runner
This is done for a couple reasons. The first is just to have things a little
more self-contained for eventually supporting more advanced "wait" logic, e.g.
waiting for a selector.

The other is to provide callers with more fine-grained controlled. Specifically
the ability to manually "tick", so that they can [presumably] do something
after every tick. This is needed by the test runner to support more advanced
cases (cases that need to test beyond 'load') and it also improves (and fixes
potential use-after-free, the lp.waitForSelector)
2026-03-23 12:30:41 +08:00
Adrià Arrufat
4b29823a5b refactor: simplify form extraction and remove const casts 2026-03-23 13:24:21 +09:00
Karl Seguin
a69a22ccd7 Merge pull request #1948 from lightpanda-io/cdp-waitforselector
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
CDP: add waitForSelector to lp.actions
2026-03-23 10:09:09 +08:00
Adrià Arrufat
a6d2ec7610 refactor: share form node ID serialization between MCP and CDP 2026-03-23 10:18:24 +09:00
Adrià Arrufat
ad83c6e70b test: fix forms unit test method casing to match normalization 2026-03-22 21:14:26 +09:00
Adrià Arrufat
c2a0d4c0b2 Merge pull request #1950 from mvanhorn/osc/feat-mcp-action-feedback
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
mcp: return page state from click/fill/scroll tools
2026-03-22 20:55:52 +09:00
Adrià Arrufat
9e7f0b4776 test: verify feedback message content in click/fill/scroll MCP tools 2026-03-22 20:39:20 +09:00
Karl Seguin
e3085cb0f1 fix test 2026-03-22 12:47:33 +08:00
Karl Seguin
4e2e895cd9 Add --advertise_host option to serve command
Allows overwriting the --host for the json/version payload. When --host is set
to 0.0.0.0, we want to provide a mechanism to specify the specific address to
connect to in /json/version (or anywhere else that we "advertise" the address).

Inspired by https://github.com/lightpanda-io/browser/pull/1923 but rather than
defaulting to 127.0.0.1 (which seems just as unsafe), adds the explicit config
option.
2026-03-22 12:40:17 +08:00
Karl Seguin
c1fc2b1301 Merge pull request #1949 from lightpanda-io/1800-fix-startup-frame-id
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Fix Page.getFrameId on STARTUP when a browser context and a target exist
2026-03-22 07:14:33 +08:00
Karl Seguin
324e5eb152 Merge pull request #1945 from lightpanda-io/replace_children
Add validation to replaceChildren
2026-03-22 06:44:53 +08:00
Karl Seguin
df4df64066 Merge pull request #1944 from lightpanda-io/about_blank_location
new URL('about:blank');
2026-03-22 06:44:37 +08:00
Karl Seguin
c557a0fd87 Merge pull request #1942 from lightpanda-io/about_blank_resolve
Search for base page when resolving from about:blank
2026-03-22 06:44:19 +08:00
Karl Seguin
a869f92e9a Merge pull request #1939 from lightpanda-io/timer_cleanup
More aggressive timer cleanup
2026-03-22 06:44:00 +08:00
Matt Van Horn
4d28265839 fix: use raw action attribute instead of resolved URL in forms
Form.getAction() resolves relative URLs against the page base, which
causes test failures when the page URL is a test server address. Use
the raw action attribute value instead, which matches what agents need
to understand the form's target path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 08:54:33 -07:00
Matt Van Horn
78c6def2b1 mcp: add detectForms tool for structured form discovery
Add a detectForms MCP tool and lp.detectForms CDP command that return
structured form metadata from the current page. Each form includes its
action URL, HTTP method, and fields with names, types, required status,
values, select options, and backendNodeIds for use with the fill tool.

This lets AI agents discover and fill forms in a single step instead of
calling interactiveElements, filtering for form fields, and guessing
which fields belong to which form.

New files:
- src/browser/forms.zig: FormInfo/FormField structs, collectForms()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 08:40:50 -07:00
Matt Van Horn
87a0690776 mcp: return page state from click/fill/scroll tools
After click, fill, and scroll actions, return the current page URL
and title instead of static success messages. This gives AI agents
immediate feedback about the page state after an action, matching
the pattern already used by waitForSelector.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 08:32:32 -07:00
Pierre Tachoire
fbc71d6ff7 cdp: handle STARTUP session into Page.getFrameTree gracefully 2026-03-21 16:29:58 +01:00
Adrià Arrufat
e10ccd846d CDP: add waitForSelector to lp.actions
It refactors the implementation from MCP to be reused.
2026-03-22 00:09:02 +09:00
Pierre Tachoire
384b2f7614 cdp: call Page.getFrameTree on startup when possible 2026-03-21 16:07:48 +01:00
Adrià Arrufat
fdc79af55c Merge pull request #1941 from mvanhorn/osc/feat-mcp-waitforselector
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Add waitForSelector MCP tool
2026-03-21 23:59:14 +09:00
Matt Van Horn
e9bed18cd8 test: add waitForSelector MCP tool tests
Add three test cases covering:
- Immediate match on an already-present element
- Polling match on an element added after a 200ms setTimeout delay
- Timeout error on a non-existent element with a short timeout

Add mcp_wait_for_selector.html test fixture that injects a #delayed
element after 200ms via setTimeout for the polling test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 06:40:43 -07:00
Pierre Tachoire
30f387d361 encode captured response depending of the content type 2026-03-21 14:11:06 +01:00
Karl Seguin
e7d272eaf6 Merge pull request #1940 from lightpanda-io/fix-mcp-crash
mcp: initialize server in mcpThread to avoid V8 isolate crashes
2026-03-21 20:35:31 +08:00
Pierre Tachoire
00d06dbe8c encode all captured responses body in base64 2026-03-21 13:29:58 +01:00
Adrià Arrufat
7b104789aa build: simplify dev version resolution 2026-03-21 21:13:50 +09:00
Pierre Tachoire
2107ade3a5 use a CapturedResponse struct for captured responses 2026-03-21 13:11:18 +01:00
Karl Seguin
e60424a402 Add validation to replaceChildren
Extract Document.replaceChildren, Element.replaceChildren and
DocumentFragment.replaceChildren into a common helper, Node.replaceChildren.

Fixes an infinite loop in WPT test:
/dom/nodes/ParentNode-replaceChildren.html
2026-03-21 19:39:49 +08:00
Karl Seguin
107da49f81 new URL('about:blank');
Add correct handling for new URL('about:blank');

When a frame is navigated to about:blank (which happens often, since it happens
as soon as a dynamic iframe is created), we make sure to give window._location
a unique value. This prevents 2 frames from referencing the same
window._location object.

Fixes a WPT crash in: 0/html/browsers/browsing-the-web/navigating-across-documents/initial-empty-document/iframe-nosrc.html
2026-03-21 18:41:58 +08:00
Karl Seguin
3e309da69f Search for base page when resolving from about:blank
When the base page (*cough* frame *cough*) is about:blank, then we need to go
up the parents to find the actual base url to resolve any new navigation URLs.
2026-03-21 16:03:39 +08:00
Adrià Arrufat
370ae2b85c main: zig fmt 2026-03-21 14:06:08 +09:00
Matt Van Horn
6008187c78 Add waitForSelector MCP tool
Adds a waitForSelector tool to the MCP server that polls for a CSS
selector match with a configurable timeout (default 5000ms). Returns the
backendNodeId of the matched element for use with click/fill tools.

The tool runs the session event loop between selector checks, so
dynamically-created elements are found as they appear from JS execution
or network responses.
2026-03-20 21:38:11 -07:00
Adrià Arrufat
598fa254cf mcp: initialize server in mcpThread to avoid V8 isolate crashes
When running mcp server, it initialized lp.mcp.Server in the main thread
which also implicitly created the V8 isolate in the main thread.
When processing requests (like calling the goto tool) inside mcpThread,
V8 would assert that the isolate doesn't match the current thread.

Fixes #1938
2026-03-21 13:33:54 +09:00
Karl Seguin
8526770e9f More aggressive timer cleanup
When a timer is cleared, e.g. clearInterval, we flag the task are deleted and
maintain the entry in window._timers. When run, the task is ignored and deleted
from _timers.

This can result in prematurely rejecting timers due to `TooManyTimeout`. One
pattern I've seen is a RAF associated with an element where the RAF is cleared
(cancelAnimationFrame) if already registered. This can quickly result in
TooManyTimers.

This commit removes the timer from _timers as soon as it's canceled. It doesn't
fully eliminate the chance of TooManyTimeout, but it does reduce it.
2026-03-21 11:38:16 +08:00
Adrià Arrufat
21325ca9be Merge branch 'main' into semantic-versioning 2026-03-21 09:46:05 +09:00
gilangjavier
b5b012bd5d refactor(cdp): always return base64-encoded Network.getResponseBody 2026-03-21 07:06:09 +07:00
Karl Seguin
b4b7a7d58a Merge pull request #1901 from lightpanda-io/goodbye_origin
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Remove Origins
2026-03-21 07:19:47 +08:00
Karl Seguin
a5378feb1d Merge pull request #1927 from lightpanda-io/feat/fetch-wait-options
Feat/fetch wait options
2026-03-21 07:18:59 +08:00
Adrià Arrufat
b5d3d37f16 Merge pull request #1931 from lightpanda-io/fix/mcp-jsonrpc-response
Fix MCP error responses missing jsonrpc field
2026-03-21 06:23:34 +09:00
Pierre Tachoire
9b02e4963b Merge pull request #1929 from mvanhorn/osc/1819-fix-detach-session-null
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Send Target.detachedFromTarget event on detach
2026-03-20 20:06:19 +01:00
Halil Durak
a865b86fa5 Merge pull request #1925 from lightpanda-io/nikneym/promise-error
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Return correct errors in promise rejections
2026-03-20 14:05:21 +03:00
Halil Durak
de28d14aff give up on switch (comptime kind), prefer union(enum) 2026-03-20 13:35:12 +03:00
Karl Seguin
2d91acbd14 Merge pull request #1933 from lightpanda-io/css-improvements-perf3
Optimize CSS visibility engine with lazy parsing and cache-friendly evaluation
2026-03-20 17:07:56 +08:00
Karl Seguin
88681b1fdb Fix Context's call_arena
The Context's call_arena should be based on the source, e.g. the IsolateWorld
or the Page, not always the page. There's no rule that says all Contexts have
to be a subset of the Page, and thus some might live longer and by doing so
outlive the page_arena.

Also, on context cleanup, isolate worlds now cleanup their identity.
2026-03-20 16:50:03 +08:00
Adrià Arrufat
1feb121ba7 CSSStyleSheet: use explicit CSSError 2026-03-20 16:50:00 +09:00
Adrià Arrufat
35cdc3c348 StyleManager: simplify rule evaluation by removing SIMD complexity 2026-03-20 12:38:15 +09:00
Adrià Arrufat
1353f76bf1 StyleManager: defer JS CSS rule allocation by lazy parsing 2026-03-20 12:30:07 +09:00
Adrià Arrufat
3e2be5b317 StyleManager: vectorize rule specificity checks with SIMD 2026-03-20 12:13:52 +09:00
Adrià Arrufat
448eca0c32 StyleManager: optimize rule evaluation using SoA and early rejection 2026-03-20 12:02:48 +09:00
Adrià Arrufat
5404ca723c SemanticTree: move NodeData initialization closer to usage 2026-03-20 10:18:16 +09:00
Adrià Arrufat
e56ffe4b60 SemanticTree): use WalkContext for walk function 2026-03-20 10:12:57 +09:00
Adrià Arrufat
02d05ae464 Fix MCP error responses missing jsonrpc field
Closes #1928
2026-03-20 09:55:54 +09:00
Adrià Arrufat
a74e97854d Merge branch 'main' into css-improvements 2026-03-20 09:46:31 +09:00
Matt Van Horn
6925fc3f70 fix(cdp): return real frame ID in STARTUP getFrameTree when page exists
dispatchStartupCommand hard-codes "TID-STARTUP" as the frame ID in
Page.getFrameTree. When a driver connects via connectOverCDP after a
real page already exists, subsequent lifecycle events (frameNavigated)
use the actual page frame ID. The driver's frame tracking was
initialized with "TID-STARTUP", causing a mismatch that hangs
navigation.

Check for an existing browser context with a target_id in
dispatchStartupCommand. If present, return the real frame ID and URL.
Fall back to "TID-STARTUP" only when no page exists yet.

Fixes #1800

This contribution was developed with AI assistance (Claude Code + Codex).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-19 16:45:17 -07:00
Matt Van Horn
84557cb4e6 fix(cdp): send Target.detachedFromTarget event on detach
detachFromTarget and setAutoAttach(false) both null bc.session_id
without notifying the client. Per the CDP spec, detaching a session
must fire a Target.detachedFromTarget event so the driver stops
sending messages on the stale session ID.

Capture the session_id before nulling it and fire the event in both
code paths. Add tests covering the event emission and the no-session
edge case.

Fixes #1819

This contribution was developed with AI assistance (Claude Code + Codex).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-19 16:42:32 -07:00
Karl Seguin
4cdc24326a Merge pull request #1918 from lightpanda-io/shadowroot_adoptedstyle
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Add `adoptedStyleSheets` property to ShadowRoot, just like Document
2026-03-20 07:11:49 +08:00
Karl Seguin
cf46f0097a Merge pull request #1915 from lightpanda-io/unhandled_rejection_improvements
Improve unhandled rejection
2026-03-20 07:11:35 +08:00
Adrià Arrufat
f1293b7346 Merge branch 'main' into semantic-versioning 2026-03-20 07:04:05 +09:00
Pierre Tachoire
d94fd2a43b Merge pull request #1793 from lightpanda-io/wpt-selfhost
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
Move WPT runs on a dedicated host
2026-03-19 17:35:21 +01:00
Pierre Tachoire
8c5e737669 ci: use mem-limit with wptrunner 2026-03-19 15:40:18 +01:00
Pierre Tachoire
fb29a1c5bf ci: adjust wpt serve wait time 2026-03-19 15:40:18 +01:00
Halil Durak
94190f93af return correct errors from promises 2026-03-19 16:30:09 +03:00
Halil Durak
93e239f682 bind more ECMAScript errors 2026-03-19 16:27:51 +03:00
Karl Seguin
a4cb5031d1 Tweak wait_until option
Small tweaks to https://github.com/lightpanda-io/browser/pull/1896

Improve the wait ergonomics with an Option with default parameter. Revert
page pointer logic to original (don't think that change was necessary).
2026-03-19 20:29:20 +08:00
Karl Seguin
a2e59af44c Merge pull request #1911 from lightpanda-io/fix/turnstile-300030-missing-navigator-apis
Fix/turnstile 300030 missing navigator apis
2026-03-19 20:26:27 +08:00
Karl Seguin
00c962bdd8 Merge pull request #1914 from lightpanda-io/semantic-tree-depth
SemanticTree: add progressive discoverability
2026-03-19 20:12:02 +08:00
Karl Seguin
1fa87442b8 log not_implemented on navigator.getBattery 2026-03-19 20:11:03 +08:00
Karl Seguin
ac5400696a Merge pull request #1916 from lightpanda-io/request_abort
Add Request.signal
2026-03-19 20:07:12 +08:00
Adrià Arrufat
5062273b7a SemanticTree: use CDPNode.Id for NodeData id 2026-03-19 20:29:54 +09:00
Adrià Arrufat
9c2393351d SemanticTree: simplify max_depth logic 2026-03-19 20:25:20 +09:00
Adrià Arrufat
f0cfe3ffc8 SemanticTree: use logger better
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-19 20:15:56 +09:00
Karl Seguin
f70865e174 Take 2.
History: We started with 1 context and thus only had 1 identity map. Frames
were added, and we tried to stick with 1 identity map per context. That didn't
work - it breaks cross-frame scripting. We introduced "Origin" so that all
frames on the same origin share the same objects. That almost worked, by
the v8::Inspector isn't bound by a Context's SecurityToken. So we tried 1 global
identity map. But that doesn't work. CDP IsolateWorlds do, in fact, need some
isolation. They need new v8::Objects created in their context, even if the
object already exists in the main context.

In the end, you end up with something like this: A page (and all its frames)
needs 1 view of the data. And each IsolateWorld needs it own view. This commit
introduces a js.Identity which is referenced by the context. The Session has a
js.Identity (used by all pages), and each IsolateWorld has its own js.Identity.

As a bonus, the arena pool memory-leak detection has been moved out of the
session and into the ArenaPool. This means _all_ arena pool access is audited
(in debug mode). This seems superfluous, but it's actually necessary since
IsolateWorlds (which now own their own identity) can outlive the Page so there's
no clear place to "check" for leaks - except on ArenaPool deinit.
2026-03-19 18:46:35 +08:00
Pierre Tachoire
615fcffb99 Merge pull request #1924 from lightpanda-io/wba-test
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Adjust wba test
2026-03-19 10:47:23 +01:00
Karl Seguin
13b746f9e4 Merge pull request #1919 from lightpanda-io/remove-make-shell
build: remove shell target from Makefile
2026-03-19 17:39:19 +08:00
Adrià Arrufat
e90fce4c55 Merge pull request #1920 from lightpanda-io/markdown-renderer-refactor
markdown: refactor renderer into a struct to simplify argument passing
2026-03-19 18:26:57 +09:00
Pierre Tachoire
59175437b5 wpt: force a wakeup of the wbauth server before the test 2026-03-19 09:40:47 +01:00
Pierre Tachoire
e950384b9b ci: sleep 5s to wait node startup 2026-03-19 09:36:47 +01:00
Pierre Tachoire
78440350dc ci: slow down execution 2026-03-19 09:32:27 +01:00
Pierre Tachoire
f435297949 ci: adjust WPT daily start time 2026-03-19 09:32:26 +01:00
Pierre Tachoire
54d1563cf3 ci: run WPT tests on a dedicated server 2026-03-19 09:32:13 +01:00
Karl Seguin
38e9f86088 fix context-leak 2026-03-19 15:42:29 +08:00
Karl Seguin
d9c5f56500 Remove Origins
js.Origin was added to allow frames on the same origin to share our zig<->js
maps / identity. It assumes that scripts on different origins will never be
allowed (by v8) to access the same zig instances.

If two different origins DID access the same zig instance, we'd have a few
different problems. First, while the mapping would exist in Origin1's
identity_map, when the zig instance was returned to a script in Origin2, it
would not be found in Origin2's identity_map, and thus create a new v8::Object.
Thus we'd end up with 2 v8::Objects for the same Zig instance. This is
potentially not the end of the world, but not great either as any zig-native
data _would_ be shared (it's the same instance after all), but js-native data
wouldn't.

The real problem this introduces though is with Finalizers. A weak reference
that falls out of scope in Origin1 will get cleaned up, even though it's still
referenced from Origin2.

Now, under normal circumstances, this isn't an issue; v8 _does_ ensure that
cross-origin access isn't allowed (because we set a SecurityToken on the
v8::Context). But it seems like the v8::Inspector isn't bound by these
restrictions and can happily access and share objects across origin.

The simplest solution I can come up with is to move the mapping from the Origin
to the Session. This does mean that objects might live longer than they have to.
When all references to an origin go out of scope, we can do some cleanup. Not
so when the Session owns this data. But really, how often are iframes on
different origins being created and deleted within the lifetime of a page?

When Origins were first introduces, the Session got burdened with having to
manage multiple lifecycles:
1 - The page-surviving data (e.g. history)
2 - The root page lifecycle (e.g. page_arena, queuedNavigation)
3 - The origin lookup

This commit doesn't change that, but it makes the session responsible for
_a lot_ more of the root page lifecycle (#2 above).

I lied. js.Origin still exists, but it's a shell of its former self. It only
exists to store the SecurityToken name that is re-used for every context with
the same origin.

The v8 namespace leaks into Session.

MutationObserver and IntersectionObserver are now back to using weak/strong refs
which was one of the failing cases before this change.
2026-03-19 14:54:10 +08:00
Karl Seguin
6c5733bba3 Merge pull request #1910 from lightpanda-io/css-improvements-perf2
Bucket stylesheet rules
2026-03-19 14:39:55 +08:00
gilangjavier
b8f1622b52 fix(cdp): base64-encode binary Network.getResponseBody payloads 2026-03-19 13:34:44 +07:00
Adrià Arrufat
f36499b806 markdown: refactor renderer into a struct to simplify argument passing 2026-03-19 15:19:11 +09:00
Adrià Arrufat
fa1dd5237d build: remove shell target from Makefile 2026-03-19 13:24:41 +09:00
Karl Seguin
2b9d5fd4d9 Add adoptedStyleSheets property to ShadowRoot, just like Document
Used in github.
2026-03-19 12:09:10 +08:00
Adrià Arrufat
2dbd32d120 build: automate version resolution in build.zig
Removes manual git flags from CI and build scripts.
Versioning is now automatically derived from git and build.zig.zon.

With this PR, we follow https://semver.org/
Logic:

1. Read the version from build.zig.zon
2. If it doesn't have a `.pre` field (i.e. dev/alpha/beta) it will use that
3. Otherwise it will get the info from git: hash and number of commits since last `.0` version
4. Then build the version: `0.3.0-dev.1493+0896edc3`

Note that, since the latest stable version is `0.2.6`.
The convention is to use `0.3.0-dev`, as:
- `0.2.6` < `0.3.0.dev` < `0.3.0`
2026-03-19 13:03:29 +09:00
Karl Seguin
1695ea81d2 on rebuild, pre-size lookups based on previous sizes 2026-03-19 11:46:58 +08:00
Karl Seguin
b7bf86fd85 update comments to reflect preference-based bucketing 2026-03-19 11:43:31 +08:00
Karl Seguin
94d8f90a96 Bucket stylesheet rules
In the first iteration of this, we kept an ArrayList of all rules with
visibility properties. Why bother evaluating if a rule's selector matches an
element if that rule doesn't have any meanignful (i.e. visibility) properties?

This commit enhances that approach by bucketing the rules. Given the following
selectors:

.hidden {....}
.footer > .small {...}

We can store the rules based on their right-most selector. So, given an element
we can do:

if (getId(el)) |id| {
   const rules = id_lookup.get(id) orelse continue;
   // check rules
}

if (getClasses(el)) |classes| {
   for (classes) |c| {
     const rules = class_lookup(c) orelse continue;
     // chck rules
   }
}
...

On an amazon product page, the total list of visibility-related rules was ~230.
Now, scanning 230 rules for a match isn't _aweful_, but remember that this has
to be done up the ancestor tree AND, for Amazon, this is called over 20K times.

This change requires that the StyleManager becomes more matching/parsing-aware
but a typical visibility check on that same Amazon product page only has to
check 2 rules (down from 230) and often has to check 0 rules.

Also, we now filter out a few more interactive-related pseudo-elements, e.g.
:hover. These aren't supported by the browser as a whole (i.e. they can _never_
match), so they can be filtered out early, when building the rules lookup.
2026-03-19 11:43:30 +08:00
Karl Seguin
964fa0a8aa Add Request.signal
Allows aborting a fetch. Improves github integration
2026-03-19 11:40:16 +08:00
Karl Seguin
db01158d2d Improve unhandled rejection
We now pay attention to the type of event that causes the unhandled exception.
This allows us to trigger the window.rejectionhandled event when that is the
correct type. It also lets us no-op for other event types which should not
trigger rejectionhandled or unhandledrejection.

Fixes stackoverflow in github integration.
2026-03-19 11:36:39 +08:00
Adrià Arrufat
e997f8317e SemanticTree: add tests for backendDOMNodeId and maxDepth 2026-03-19 12:25:02 +09:00
Karl Seguin
b9bef22bbf Merge pull request #1912 from lightpanda-io/css-improvements-fix
StyleManager: restore dirty state on rebuild allocation failure
2026-03-19 10:25:09 +08:00
Adrià Arrufat
b2a996e5c7 StyleManager: restore dirty state on rebuild allocation failure 2026-03-19 11:13:04 +09:00
Karl Seguin
a88c21cdb5 Fix Navigator Additions
Follow up to https://github.com/lightpanda-io/browser/pull/1884

Fixes build, uses arena/finalizer for PermissionStatus. Fixes tests. A few other
small cleanups.
2026-03-19 09:41:13 +08:00
shaewe180
e2be8525c4 Config: remove js_enum_from_string constant 2026-03-19 09:40:40 +08:00
shaewe180
c15afa23ca Session: fix page pointer handling in wait loop
- Refactor `wait` and `_wait` to handle `page` as `*Page` instead of `**Page`, preventing stale references during navigations.
- Update `networkidle` wait condition to use `_notified_network_idle == .done`.
- Document `--wait_ms` and `--wait_until` options in `Config.zig` help text.
2026-03-19 09:36:42 +08:00
Adrià Arrufat
7a7c4b9f49 SemanticTree): add backendNodeId and maxDepth support 2026-03-19 10:18:08 +09:00
Karl Seguin
f594b033bf Merge pull request #1897 from lightpanda-io/css-improvements-perf
Introduce StyleManager
2026-03-19 07:10:35 +08:00
Karl Seguin
10e379e4fb fix clamping 2026-03-19 07:00:26 +08:00
Karl Seguin
c1bb27c450 better encapsulate arena reset 2026-03-19 06:53:08 +08:00
Karl Seguin
dda5e2c542 Apply suggestions from code review
Co-authored-by: Adrià Arrufat <1671644+arrufat@users.noreply.github.com>
2026-03-19 06:47:40 +08:00
Karl Seguin
edd0c5c83f Merge pull request #1900 from lightpanda-io/input-event
Some checks failed
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
distapch InputEvent on input/TextArea changes
2026-03-19 06:39:44 +08:00
Francis Bouvier
c6861829c3 Merge pull request #1907 from lightpanda-io/README-remove-js-runtime
Some checks failed
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
REAMDE: again references to js-runtime
2026-03-18 16:48:16 +01:00
Francis Bouvier
e14c8b3025 REAMDE: again references to js-runtime 2026-03-18 16:45:44 +01:00
Francis Bouvier
5bc00c595c Merge pull request #1906 from lightpanda-io/README-remove-js-runtime
README: remove again references to js-runtime
2026-03-18 16:44:53 +01:00
Francis Bouvier
db5fb40de0 README: remove again references to js-runtime 2026-03-18 16:42:31 +01:00
Pierre Tachoire
4e6a357e6e use initTrusted for InputEvent 2026-03-18 16:41:28 +01:00
Francis Bouvier
6cf515151d Merge pull request #1905 from lightpanda-io/README-remove-js-runtime
README: remove reference to zig-js-runtime
2026-03-18 16:41:08 +01:00
Pierre Tachoire
bf6e4cf3a6 disaptch InputEvent on input/TextArea changes 2026-03-18 16:40:21 +01:00
Francis Bouvier
60936baa96 README: remove reference to zig-js-runtime 2026-03-18 16:39:26 +01:00
Pierre Tachoire
c29f72a7e8 Merge pull request #1898 from lightpanda-io/keyboard-event-bubble
Keyboard events are bubbling, cancelable and composed
2026-03-18 16:26:15 +01:00
Adrià Arrufat
d4427e4370 Merge pull request #1894 from lightpanda-io/semantic-tree-interactive
SemanticTree: implement interactiveOnly filter and optimize token usage
2026-03-18 22:33:45 +09:00
Karl Seguin
b85ec04175 Merge pull request #1902 from lightpanda-io/fix/emulation-set-user-agent-override
Fix/emulation set user agent override
2026-03-18 20:05:26 +08:00
Karl Seguin
da05ba0eb7 log on ignored setUserAgentOverride 2026-03-18 19:46:37 +08:00
Karl Seguin
414a68abeb Merge pull request #1899 from lightpanda-io/idle_task_fix
only run idle tasks from the root page
2026-03-18 19:41:58 +08:00
Karl Seguin
52455b732b Merge pull request #1885 from lightpanda-io/danling_context_fallback
Fallback to the Incumbent Context when the Current Context is dangling
2026-03-18 19:41:38 +08:00
Pierre Tachoire
ba71268eb3 Keyboard events are bubbling, cancelable and composed
According to the specs: https://w3c.github.io/uievents/#event-type-keyup
2026-03-18 12:36:00 +01:00
Adrià Arrufat
694aac5ce8 browser.interactive: optimize role checks with StaticStringMap 2026-03-18 20:10:15 +09:00
Adrià Arrufat
cbab0b712a SemanticTree: simplify TextVisitor printing logic 2026-03-18 20:07:11 +09:00
Karl Seguin
1aee3db521 only run idle tasks from the root page 2026-03-18 19:03:38 +08:00
Karl Seguin
e29778d72b Introduce StyleManager
A Page now has a StyleManager. The StyleManager currently answers two questions:
1 - Is an element hidden
2 - Does an element have pointer-events == none

This is used in calls such as element.checkVisibility which, on some pages, can
be called tens of thousands of times (often through other methods, like
element.getBoundingClientRect). This _can_ be a bottleneck.

The StyleManager keeps a list of rules. The rules include the selector,
specificity, and properties that we care about. Rules in a stylesheet that
contain no properties of interest are ignored. This is the first and likely
most significant optimization. Presumably, most CSS rules don't have a
display/visibility/opacity or pointer-events property.

The list is rules is cached until stylesheets are modified or delete. When this
happens, the StyleManager is flagged as "dirty" and rebuilt on-demand in the
next query.  This is our second major optimization.

For now, to check if an element is visible, we still need to scan all rules.
But having a pre-build subset of all the rules is a first step.

The next step might be to optimize the matching, or possibly optimizing common
cases (e.g. id and/or simple class selector)
2026-03-18 17:52:57 +08:00
Pierre Tachoire
f634c9843d Merge pull request #1893 from lightpanda-io/link_onload_rel
Some checks failed
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Expand rel's that trigger a link's onload
2026-03-18 09:41:10 +01:00
Pierre Tachoire
e1e45d1c5d Merge pull request #1796 from lightpanda-io/wp/mrdimidium/telemetry-common-network
Use common network runtime for telemetry messages
2026-03-18 09:34:19 +01:00
shaewe180
09327c3897 feat: fetch add wait_until parameter for page loads options
Add `--wait_until` and `--wait_ms` CLI arguments to configure session wait behavior. Updates `Session.wait` to evaluate specific page load states (`load`, `domcontentloaded`, `networkidle`, `fixed`) before completing the wait loop.
2026-03-18 15:08:51 +08:00
Adrià Arrufat
ff288c8aa2 browser.interactive: use for-else expression in role checks 2026-03-18 12:04:53 +09:00
Adrià Arrufat
e1b14a6833 SemanticTree: enable prune by default 2026-03-18 11:25:38 +09:00
Adrià Arrufat
015edc3848 SemanticTree: implement interactiveOnly filter and optimize token usage 2026-03-18 10:56:56 +09:00
Karl Seguin
bd2406f803 Merge pull request #1891 from lightpanda-io/form-requestSubmit
Some checks failed
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Implement Form.requestSubmit
2026-03-18 08:55:04 +08:00
Karl Seguin
3c29e7dbd4 Expand rel's that trigger a link's onload
Was only "stylesheet", not also includes "preload" and "modulepreload"
2026-03-18 08:53:05 +08:00
Nikolay Govorov
586413357e Close all cdp clients on shutdown 2026-03-17 23:30:36 +00:00
Nikolay Govorov
9a055a61a6 Limit telemetry body size 2026-03-17 23:23:10 +00:00
Nikolay Govorov
5fb561dc9c Used ring buffer for telemetry events buffer 2026-03-17 23:23:08 +00:00
Nikolay Govorov
b14ae02548 Move comments and bound checks 2026-03-17 23:23:05 +00:00
Nikolay Govorov
51fb08e6aa Create multi interface in Runtime on demand 2026-03-17 23:23:01 +00:00
Nikolay Govorov
a6d699ad5d Use common network runtime for telemetry messages 2026-03-17 23:21:57 +00:00
Karl Seguin
8372b45cc5 Merge pull request #1877 from lightpanda-io/xhr_and_fetch_blob_urls
Support blob urls in XHR and Fetch
2026-03-18 07:02:56 +08:00
Pierre Tachoire
1739ae6b9a check submit element and form into Form.requestSubmit 2026-03-17 21:34:48 +01:00
Pierre Tachoire
ba62150f7a add Form.requestSubmit(submitter) 2026-03-17 17:05:30 +01:00
Nikolay Govorov
8143a61955 Merge pull request #1888 from lightpanda-io/wp/mrdimidium/clenup-ci
Some checks failed
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
Cleanup CI a little bit
2026-03-17 15:47:18 +00:00
Nikolay Govorov
e16c479781 Merge pull request #1886 from lightpanda-io/wp/mrdimidium/enable-git-version-in-ci
Use `git_version` option for version command
2026-03-17 14:29:47 +00:00
Nikolay Govorov
c0c4e26d63 removes artifacts of the past from CI 2026-03-17 14:24:22 +00:00
Nikolay Govorov
b252aa71d0 Use git_version option for version command 2026-03-17 13:25:15 +00:00
Pierre Tachoire
9ef8d9c189 Merge pull request #1887 from lightpanda-io/disable_observer_weak_ref
disable observer weak ref
2026-03-17 14:09:43 +01:00
Karl Seguin
9f27416603 zig fmt 2026-03-17 20:03:31 +08:00
Karl Seguin
0729f4a03a Merge pull request #1872 from lightpanda-io/wp/mrdimidium/fix-cdp-close
Gracefull close ws socket
2026-03-17 19:58:48 +08:00
Karl Seguin
21f7b95db9 disable observer weak ref
https://github.com/lightpanda-io/browser/pull/1870 doesn't work. I think there
are ways for the inspector to move objects into a context that skips our
reference count (those remote objects?). This disables weak references for
MutationObserver and IntersectionObserver. The issue is probably more widespread
but these are two types CDP drivers us _a lot_ via inspector, so this should
fix a number of immediate crashes.

I believe the correct fix is to remove Origin and store things at the Session-
level.
2026-03-17 19:54:21 +08:00
Nikolay Govorov
4125a5aa1e Merge pull request #1874 from JasonOA888/fix/add-git-version-option
feat: add `git_version` build option for release version detection
2026-03-17 11:27:27 +00:00
Nikolay Govorov
6d0dc6cb1e Gracefull close ws socket 2026-03-17 11:15:12 +00:00
Nikolay Govorov
0675c23217 Merge pull request #1883 from Tenith01/fix/port-already-in-use
fix: show actionable error when server port is already in use
2026-03-17 10:53:36 +00:00
Karl Seguin
d0e6a1f5bb Merge pull request #1882 from Tenith01/fix/window-onerror-special-case
fix: special-case Window#onerror per WHATWG spec (5-arg signature)
2026-03-17 18:36:11 +08:00
Karl Seguin
91afe08235 Merge pull request #1878 from mvanhorn/osc/1770-window-event
Implement window.event property
2026-03-17 18:35:30 +08:00
Karl Seguin
041d9d41fb Fallback to the Incumbent Context when the Current Context is dangling
This specifically fixes a WPT crash running:
/html/browsers/browsing-the-web/history-traversal/001.html

(And probably a few others).

Isolate::GetCurrentContext can return a 'detached' context. And, for us, that's
a problem, because 'detached' v8::Context references a js.Context that we've
deinit'd. This seems to only happen when frames pass values around to other
frames and then those frames are removed. It might also require some async'ing,
I'm not sure.

To solve this, when we destroy a js.Context, we store null in the v8::Context's
embedder data, removing the link to our (dead) js.Context. When we load a
js.Context from a v8.Context, we check for null. If it is null, we return the
Incumbent context instead. This should never be null, as it's always the context
currently executing code.

I'm not sure if falling back to the Incumbent context is always correct, but
it does solve the crash.
2026-03-17 18:04:44 +08:00
Karl Seguin
7009fb5899 Merge pull request #1880 from lightpanda-io/logfilter-init-slice
Some checks failed
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
LogFilter: init with slice and silence tests
2026-03-17 17:42:23 +08:00
Tenith01
d2003c7c9a fix: stub navigator.permissions, storage, deviceMemory to unblock Turnstile 2026-03-17 14:12:13 +05:30
Tenith01
ce002b999c fix: special-case Window#onerror per WHATWG spec (5-arg signature) 2026-03-17 13:49:59 +05:30
Adrià Arrufat
5b1056862a Merge pull request #1879 from lightpanda-io/fix-leak-add-from-element
ScriptManager: fix memory leak and resource handover
2026-03-17 16:33:49 +09:00
Tenith01
cc4ac99b4a fix: show actionable error when server port is already in use 2026-03-17 13:02:55 +05:30
Adrià Arrufat
46df341506 ScriptManager: defer resource handover until request success 2026-03-17 15:45:11 +09:00
Adrià Arrufat
b698e2d078 LogFilter: init with slice and silence tests 2026-03-17 13:42:35 +09:00
Karl Seguin
5cc5e513dd Merge pull request #1876 from lightpanda-io/more-mcp-tools
Add click, fill, and scroll DOM interaction tools to MCP and CDP
2026-03-17 12:39:35 +08:00
Adrià Arrufat
e048b0372f ScriptManager: fix memory leak and resource handover
Release the arena when an inline script is empty and ensure the
handover flag is set correctly for all script execution modes.
2026-03-17 13:32:29 +09:00
Adrià Arrufat
d7aaa1c870 Merge branch 'main' into more-mcp-tools 2026-03-17 13:26:44 +09:00
Adrià Arrufat
463aac9b59 browser.actions: refactor click to use trusted MouseEvent 2026-03-17 13:22:55 +09:00
Karl Seguin
d9cdd78138 Merge pull request #1875 from lightpanda-io/history_test_stability
Try to improve stability of history test
2026-03-17 12:21:29 +08:00
Adrià Arrufat
44a83c0e1c browser.actions: use .wrap directly
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-17 12:55:10 +09:00
Matt Van Horn
96f24a2662 Implement window.event property
Add the deprecated-but-widely-used window.event property that returns
the Event currently being handled. Returns undefined when no event is
being dispatched.

Implementation saves and restores window._current_event around handler
invocation in both dispatchDirect and dispatchNode, supporting nested
event dispatch correctly.

Fixes #1770

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 20:42:45 -07:00
Karl Seguin
5d2801c652 Support blob urls in XHR and Fetch
Used quite a bit in WPT. Not sure how common this is in real world though.
2026-03-17 10:31:32 +08:00
Karl Seguin
deb08b7880 Try to improve stability of history test
Tests cannot navigate away from the page page. If they do, the testRunner will
crash, as it tries to access `assertOk` on a page that no longer exists. This
commit hacks the history test, using an iframe, to try to test the history API
without navigating off the main page.
2026-03-17 08:15:49 +08:00
JasonOA888
96e5054ffc feat: add git_version build option for release version detection
- Add git_version option to build.zig (similar to git_commit)
- Update version command to output git_version when available
- Falls back to git_commit when not on a tagged release
- CI can pass -Dgit_version=$(git describe --tags --exact-match) for releases

Fixes #1867
2026-03-17 07:41:11 +08:00
Karl Seguin
c9753a690d Merge pull request #1863 from jnMetaCode/fix/cdp-missing-disable-methods
Some checks failed
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
fix(cdp): add missing disable methods to Performance, Inspector, Security domains
2026-03-17 07:21:14 +08:00
Karl Seguin
27aaf46630 Merge pull request #1873 from lightpanda-io/fix/domexception-default-messages
Fix/domexception default messages
2026-03-17 07:20:15 +08:00
Karl Seguin
84190e1e06 fix test for new messages 2026-03-17 07:07:16 +08:00
Karl Seguin
b0b1f755ea Merge pull request #1870 from lightpanda-io/mutation_observer_rc
Switch to reference counting for Mutation Observer and Intersection O…
2026-03-17 06:43:45 +08:00
Karl Seguin
fcf1d30c77 Merge pull request #1864 from lightpanda-io/trusted_cdp_clicks
click event dispastched from CDP should be trusted
2026-03-17 06:43:32 +08:00
Karl Seguin
3c532e5aef Merge pull request #1846 from lightpanda-io/origin_cdp_fix
Fix use-after-free with certain CDP scripts
2026-03-17 06:43:07 +08:00
Karl Seguin
3efcb2705d Merge pull request #1840 from lightpanda-io/script_manager_arena_pool
Move ScriptManager to ArenaPool.
2026-03-17 06:42:48 +08:00
Karl Seguin
c25f389e91 Merge pull request #1817 from lightpanda-io/frames_postMessage
window.postMessage across frames
2026-03-17 06:42:32 +08:00
Karl Seguin
533f4075a3 Merge pull request #1868 from lightpanda-io/bom_charset
Some checks failed
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
Set charset based on BOM
2026-03-16 23:36:44 +08:00
Adrià Arrufat
f508d37426 lp: validate params in node actions and rename variables 2026-03-16 23:50:15 +09:00
Adrià Arrufat
548c6eeb7a browser.actions: remove redundant result ignores 2026-03-16 23:45:07 +09:00
Adrià Arrufat
c8265f4807 browser.actions: improve error handling 2026-03-16 23:41:22 +09:00
Adrià Arrufat
a74e46debf actions: make scroll coordinates optional
Updates the scroll action to accept optional x and y coordinates. This
allows scrolling on a single axis without resetting the other to zero.
2026-03-16 22:44:37 +09:00
Karl Seguin
1ceaabe69f Switch to reference counting for Mutation Observer and Intersection Observer
This may be a stopgap.

Our identity model assumes that v8 won't allow cross-origin access. It turns out
that with CDP and Inspector, this isn't true. Inspectors can break / violate
cross-origin restrictions. The result is that 2 origins can see the same zig
instance, which causes 2 v8::Objects to reference the same Zig instance.

This likely causes some consistency issue. Like, if you take mo in 1 context,
and write an arbitrary property, mo.hack = true, you won't observe that in the
2nd context (because it's a different v8::Object). But, it _is_ the same Zig
instance, so if you set a known/real property, it will be updated.

That's probably a pretty minor issue. The bigger issue is that it can result in
a use-after-free when using explicit strong/weak ref:

1 - Mutation observer is created in Origin1
2 - It's automatically set to weak
3 - Something is observed, the reference is made strong
4 - The MO is accessed from Origin2
5 - Creates a new v8::Object
6 - Sets it to weak
7 - Object goes out of scope in Origin2
8 - Finalizer is called  <- free
9 - MO is manipulated in Origin 1 <- use after free

Maybe the right option is to have a single shared identity map. I need to think
about it. As a stopgap, switching to reference counting (which we already
support) shold prevent the use-after free. While we'll still create 2
v8::Objects, they'll each acquireRef (_rc = 2) and thus it won't be freed until
they both release i
Maybe the right option is to have a single shared identity map. I need to think
about it. As a stopgap, switching to reference counting (which we already
support) shold prevent the use-after free. While we'll still create 2
v8::Objects, they'll each acquireRef (_rc = 2) and thus it won't be freed until
they both release it.
2026-03-16 20:56:18 +08:00
Pierre Tachoire
91a2441ed8 Merge pull request #1829 from salmanmkc/upgrade-github-actions-node24
Upgrade GitHub Actions for Node 24 compatibility
2026-03-16 12:19:22 +01:00
Pierre Tachoire
2ecbc833a9 Merge pull request #1858 from lightpanda-io/flaky-wbatest
ci: fix wba flaky test
2026-03-16 11:13:43 +01:00
Pierre Tachoire
dac456d98c ci: fix wba flaky test
Sometimes the GHA secret isn't dump in file correctly.
So this commit inject the value directly to the command line
2026-03-16 10:57:40 +01:00
Karl Seguin
422320d9ac Set charset based on BOM
Small follow up to https://github.com/lightpanda-io/browser/pull/1837 If we
sniff the content type from the byte order mark (BOM), then we should set the
charset. This has higher precedence than sniffing the content type from the
content of the document (e.g. meta tags)
2026-03-16 17:54:01 +08:00
Karl Seguin
18b635936c Merge pull request #1837 from mvanhorn/osc/531-charset-prescan
Some checks failed
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
Implement charset detection from first 1024 bytes of HTML
2026-03-16 17:39:58 +08:00
Karl Seguin
7b2895ef08 click event dispastched from CDP should be trusted 2026-03-16 17:33:12 +08:00
jnMetaCode
b09e9f7398 fix(cdp): add missing disable method to Security
Signed-off-by: JiangNan <1394485448@qq.com>
2026-03-16 17:21:20 +08:00
jnMetaCode
ac651328c3 fix(cdp): add missing disable method to Inspector
Signed-off-by: JiangNan <1394485448@qq.com>
2026-03-16 17:21:18 +08:00
jnMetaCode
0380df1cb4 fix(cdp): add missing disable method to Performance
Signed-off-by: JiangNan <1394485448@qq.com>
2026-03-16 17:21:14 +08:00
jnMetaCode
21421d5b53 fix(dom): add default messages for all DOMException error codes
The getMessage() fallback returned raw tag names like
"wrong_document_error" instead of human-readable messages.
Fill in all 18 error codes with messages based on the
WebIDL spec error descriptions.

Closes #82

Signed-off-by: JiangNan <1394485448@qq.com>
2026-03-16 17:20:29 +08:00
jnMetaCode
80c309aa69 fix(cdp): add noop Emulation.setUserAgentOverride to prevent Playwright crash
Playwright calls Emulation.setUserAgentOverride when creating a
browser context with a custom user agent. Without this handler,
Lightpanda returns UnknownMethod which crashes the Playwright
driver.

Add a noop handler matching the existing pattern for other
Emulation methods (setDeviceMetricsOverride, setEmulatedMedia, etc.)
so the CDP handshake can proceed.

Fixes #1436

Signed-off-by: JiangNan <1394485448@qq.com>
2026-03-16 17:07:56 +08:00
Adrià Arrufat
f5bc7310b1 actions: refactor node type checks for idiomatic flattening 2026-03-16 16:38:21 +09:00
Adrià Arrufat
21e9967a8a actions: simplify function names 2026-03-16 16:31:33 +09:00
Adrià Arrufat
32f450f803 browser: centralize node interaction logic
Extracts click, fill, and scroll logic from CDP and MCP domains into a
new dedicated actions module to reduce code duplication.
2026-03-16 14:22:15 +09:00
Adrià Arrufat
1972142703 mcp: add tests for click, fill, and scroll actions 2026-03-16 14:16:20 +09:00
Adrià Arrufat
b10d866e4b Add click, fill, and scroll interaction tools
Adds click, fill, and scroll functionality to both CDP and MCP
to support programmatic browser interactions.
2026-03-16 13:55:37 +09:00
Matt Van Horn
b373fb4a42 Address review feedback: fix endless loop, use stdlib, add charset flag
- Use std.ascii.eqlIgnoreCase instead of custom asciiEqlIgnoreCase
- Fix infinite loop in findAttrValue when attribute has no '=' sign
  (e.g. self-closing <meta foo="bar"/>)
- Add is_default_charset flag to Mime struct so prescan only overrides
  charset when Content-Type header didn't set one explicitly
- Add regression test for the self-closing meta loop case

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 21:20:45 -07:00
Adrià Arrufat
43a70272c5 Merge branch 'main' into css-improvements 2026-03-16 10:25:35 +09:00
Adrià Arrufat
ddd34dc57b Merge pull request #1836 from mvanhorn/osc/1822-fix-axvalue-integer-string
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
fix: serialize AXValue integer as string per CDP spec
2026-03-16 09:55:54 +09:00
Karl Seguin
265c5aba2e Merge pull request #1850 from navidemad/fix/cdp-websocket-timeout-during-navigation
Fix CDP WebSocket connection dying during complex page navigation
2026-03-16 08:41:36 +08:00
Adrià Arrufat
21fc6d1cf6 cdp: explain buffer size for int serialization 2026-03-16 09:41:28 +09:00
Karl Seguin
1a7fe6129c Merge pull request #1847 from lightpanda-io/blob_fixes
Fix issues with blobs
2026-03-16 08:34:38 +08:00
Karl Seguin
37462a16c5 Merge pull request #1853 from lightpanda-io/fix-ignore-partition-key
Fix ignore partition key
2026-03-16 08:19:09 +08:00
Karl Seguin
323ec0046c zig fmt 2026-03-16 07:36:14 +08:00
Karl Seguin
dc7c6984fb Merge pull request #1852 from lightpanda-io/fix-domparser-error-doc
Fix domparser error doc
2026-03-16 07:35:50 +08:00
Karl Seguin
92f7248a16 Merge pull request #1851 from lightpanda-io/fix-fetch-error-reject
Fix fetch error reject
2026-03-16 07:35:38 +08:00
Karl Seguin
1ec3e156fb Fix partitionKey ignore PR
Fixes https://github.com/lightpanda-io/browser/pull/1821 so that it compiles
2026-03-16 07:28:14 +08:00
Karl Seguin
1121bed49b remove test that I guess isn't reliable (CI?) 2026-03-16 07:20:57 +08:00
Karl Seguin
0eb43fb530 Fix test
Fixes test associated with https://github.com/lightpanda-io/browser/pull/1827
2026-03-16 07:16:27 +08:00
Karl Seguin
1f50dc38c3 Merge pull request #1845 from navidemad/fix-cdp-unknown-domain-disconnect
fix(cdp): don't kill WebSocket on unknown domain/method errors
2026-03-16 07:14:18 +08:00
Karl Seguin
a9d044ec10 revert domparser test change that belongs to a different PR 2026-03-16 07:11:06 +08:00
Navid EMAD
1bdf464ef2 Fix CDP WebSocket connection dying during complex page navigation
The CDP timeout handler in httpLoop had two compounding bugs:

1. Unit mismatch: timestamp(.monotonic) returns seconds, but
   ms_remaining is in milliseconds. The comparison and subtraction
   mixed units.

2. Double-counting: In the .done branch, elapsed was computed as
   absolute time since last_message, but last_message was never
   updated in this branch. Each iteration subtracted the growing
   total elapsed seconds from an already-decremented ms_remaining.

During complex page loads, Session._wait() returns .done rapidly
(due to JS macrotask execution, background tasks, or errors). Each
rapid .done return subtracted the growing elapsed (seconds) from
ms_remaining (milliseconds), draining it to zero in ~2 seconds
instead of the configured 10-second timeout.

Fix: use milliTimestamp() for consistent units, update last_message
in the .done branch for incremental elapsed tracking, and use >= for
correct boundary comparison.

Fixes #1849

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 23:07:23 +01:00
katie-lpd
a70da0d176 Update README.md 2026-03-15 16:57:09 +01:00
katie-lpd
8c52b8357c Update README.md 2026-03-15 16:33:53 +01:00
Karl Seguin
0243c6b450 Fix issues with blobs
https://github.com/lightpanda-io/browser/pull/1775 made blobs finalizable and
https://github.com/lightpanda-io/browser/pull/1795 made it possible to navigate
from blobs (important for WPT tests). This fixes a number of issues related to
both.

First, weak/strong ref'ing a value now uses the resolved value. When registering
a finalizer, we use the resolved value (the most specific type in the prototype
chain). For this reason, when toggling a weak/strong ref, we have to use the
same resolved value. This solves a segfault where a File is created, but
extended as a Blob (e.g. in createObjectURL).

Next, two issues were fixed when navigating to an invalid blob. First, the frame
is properly removed from the parent list on frame navigation error. Second,
on frame navigation error, we don't stop _all_ other navigations, we just log
the error and move on to the next frame.
2026-03-15 21:03:55 +08:00
Adrià Arrufat
f7071447cb Merge pull request #1834 from evalstate/mcp-ping
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
feat(mcp): add ping request handling
2026-03-15 18:15:51 +09:00
Halil Durak
c038bfafa1 Merge pull request #1772 from lightpanda-io/nikneym/failing-body-onload-tests
Add failing `body.onload` tests
2026-03-15 10:58:39 +03:00
sjhddh
4d60f56e66 test: add test case for fetch throwing TypeError on network errors 2026-03-15 07:26:18 +00:00
sjhddh
56d3cf51e8 test: update empty xml parse error case in domparser.html 2026-03-15 07:25:47 +00:00
sjhddh
3013e3a9e6 fix(net): fetch() should reject with a TypeError on network errors 2026-03-15 07:25:47 +00:00
Navid EMAD
fe9b2e672b fix(test): update tests to match new CDP error handling behavior
processMessage no longer returns Zig errors when dispatchCommand fails —
it sends a CDP error response and continues. Update all expectError calls
to use processMessage + expectSentError instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 05:52:20 +01:00
Karl Seguin
3e9fa4ca47 Fix use-after-free with certain CDP scripts
Origins were introduced to group memory/data that can be owned by multiple
frames (on the same origin). There's a general idea that the initial "opaque"
origin is very transient and should get replaced before any actual JavaScript
is executed (because the real origin is setup as soon as we get the header from
the response, long before we execute any script).

But...with CDP, this guarantee doesn't hold There's nothing stop a CDP script
from executing javascript at any point, including while the main page is still
being loaded. This can result on allocations made on the opaque origin which
is promptly discarded.

To solve this, this commit introduced origin takeover. Rather than just
transferring any data from one origin (the opaque) to the new one and then
deinit' the opaque one (which is what results in user-after-free), the new
origin simply maintains a list of opaque origins it has "taken-over"and is
responsible for freeing it (in its own deinit). This ensures that any allocation
made in the opaque origin remain valid.
2026-03-15 12:00:42 +08:00
Navid EMAD
a2e66f85a1 fix(cdp): don't kill WebSocket on unknown domain/method errors
When a CDP command with an unrecognized domain (e.g. `NonExistent.method`)
was sent, the error response was correctly returned but the connection
died immediately after. This happened because dispatch() re-returned the
error after sending the error response, which propagated up through
processMessage() → handleMessage() where `catch return false` closed
the WebSocket connection.

Now the error is only propagated if sendError itself fails (e.g. broken
pipe). Otherwise dispatch() returns normally and the read loop continues.

Fixes #1843

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 04:36:47 +01:00
Karl Seguin
a9b9cf14c3 Merge pull request #1841 from lightpanda-io/reject_error
Improve ergonomics around rejecting a promise with a proper JS error
2026-03-15 10:19:24 +08:00
Karl Seguin
d4b941cf30 zig fmt 2026-03-15 10:06:20 +08:00
Karl Seguin
4b6bf29b83 Improve ergonomics around rejecting a promise with a proper JS error 2026-03-15 09:55:13 +08:00
Karl Seguin
a8b147dfc0 update v8 2026-03-15 09:24:42 +08:00
Karl Seguin
65627c1296 Move ScriptManager to ArenaPool.
This removes the BufferPool. The BufferPool was per-ScriptManager and only
usable for the response. The ArenaPool is shared across pages and threads, so
can provide much better re-use. Furthermore, the ArenaPool provides an
Allocator, so that a Script's URL or inline content can be owned by the arena/
script itself, rather than the page arena.
2026-03-15 09:18:13 +08:00
Matt Van Horn
3dcdaa0a9b Implement charset detection from first 1024 bytes of HTML
Per the HTML spec, browsers should detect charset from <meta> tags
in the first 1024 bytes of a document when the HTTP Content-Type
header doesn't specify one.

Adds Mime.prescanCharset() which scans for:
- <meta charset="X">
- <meta http-equiv="Content-Type" content="...;charset=X">

Integrates into the page loading flow to set the detected charset
on the Mime when no explicit HTTP charset was provided.

Fixes #531
2026-03-14 14:15:40 -07:00
Matt Van Horn
5bc00045c7 fix: serialize AXValue integer as string per CDP spec
The CDP Accessibility spec defines AXValue.value as always being a
string, but integer values were serialized as JSON numbers. This
breaks CDP clients with strict deserialization (e.g., Rust serde).

Fixes #1822
2026-03-14 14:09:49 -07:00
evalstate
93ea95af24 feat(mcp): add ping request handling 2026-03-14 17:48:29 +00:00
Karl Seguin
f754773bf6 window.postMessage across frames
Depends on https://github.com/lightpanda-io/zig-v8-fork/pull/160

Improves postMessage support, specifically for use across frames. This commit
also addresses a few other issues (identified while implementing this).

1 - Since macrotasks can schedule more macrotasks, we need to check the time-to-
next microtask after all microtasks have completed.

2 - frame's onload callback is triggered from the frame's context, but has to
    execute on the parents contet.
2026-03-14 21:04:50 +08:00
Adrià Arrufat
f0c9c262ca Merge branch 'main' into css-improvements 2026-03-14 20:36:50 +09:00
Karl Seguin
42bb2f3c58 Merge pull request #1823 from lightpanda-io/remove_double_free
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Remove frame double-free on navigate error
2026-03-14 19:36:27 +08:00
Adrià Arrufat
3fde349b9f webapi): reorder css function params and merge pointer events 2026-03-14 20:31:00 +09:00
Adrià Arrufat
55a9976d46 css: CSSStyleSheet.replace() should resolve to the stylesheet 2026-03-14 20:30:00 +09:00
Adrià Arrufat
66a86541d1 css: handle top-level semicolons in parser 2026-03-14 20:30:00 +09:00
Adrià Arrufat
bc19079dad css: add unit tests for RulesIterator 2026-03-14 20:30:00 +09:00
Adrià Arrufat
351e44343d css: make CSSStyleSheet.insertRule index optional 2026-03-14 20:30:00 +09:00
Adrià Arrufat
e362a9cbc3 webapi.Element: use dot notation
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-14 20:29:54 +09:00
Adrià Arrufat
e2563e57f2 webapi.Element: make getCssProperties private
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-14 20:18:51 +09:00
Adrià Arrufat
df5e978247 tests: remove warning
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-14 19:54:43 +09:00
hobostay
68337a6989 Fix compilation errors: add missing log import and remove duplicate
- Add missing `const log = @import("../../log.zig");` in network.zig
- Remove duplicate `log` declaration inside setCdpCookie in storage.zig
  (already declared at file scope)

Fixes compilation errors:
- src/cdp/domains/network.zig:124:9: error: use of undeclared identifier 'log'
- src/cdp/domains/storage.zig:135:15: error: local constant shadows declaration of 'log'

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 17:32:48 +08:00
Salman Muin Kayser Chishti
bf6dbedbe4 Upgrade GitHub Actions for Node 24 compatibility
Signed-off-by: Salman Muin Kayser Chishti <13schishti@gmail.com>
2026-03-14 09:11:46 +00:00
sjhddh
a204f40968 fix(dom): return parsererror document on XML parse failure 2026-03-14 08:36:06 +00:00
Karl Seguin
fe3faa0a5a Merge pull request #1825 from sjhddh/fix-tracking-allocator-resize
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
fix: only increment TrackingAllocator reallocation_count on successful resizes
2026-03-14 15:48:49 +08:00
Karl Seguin
39d5a25258 Merge pull request #1820 from hobostay/fix-tracking-allocator-stats
Fix TrackingAllocator reallocation_count being incremented on failed operations
2026-03-14 15:48:13 +08:00
Karl Seguin
f4044230fd Merge pull request #1824 from sjhddh/fix-option-gettext-leak
fix: resolve memory leak in Option.getText() by using page arena
2026-03-14 15:45:53 +08:00
sjhddh
4d6d8d9a83 fix(test): properly count successful reallocations in TrackingAllocator 2026-03-14 06:57:04 +00:00
sjhddh
c4176a282f fix: resolve memory leak in Option.getText() by using page arena 2026-03-14 06:50:26 +00:00
Karl Seguin
1352839472 Remove frame double-free on navigate error
The explicit deinit isn't needed as here's already an errdefer in play.
2026-03-14 14:02:58 +08:00
Karl Seguin
535128da71 Merge pull request #1814 from lightpanda-io/nikneym/window-onload-alias
Make `body.onload` getter/setter alias to `window.onload`
2026-03-14 13:30:10 +08:00
hobostay
099550dddc Ignore partitionKey in cookie operations to support Puppeteer page.setCookie()
Puppeteer's page.setCookie() internally calls Network.deleteCookies twice
before setting a cookie. The second call includes a partitionKey field for
CHIPS (partitioned cookies), which caused Lightpanda to return NotImplemented.

Since Lightpanda doesn't support partitioned cookies, we now silently ignore
the partitionKey parameter and proceed with the cookie operation based on
name/domain/path matching.

This change affects:
- Network.deleteCookies: no longer rejects requests with partitionKey
- Network.setCookie (via setCdpCookie): no longer rejects cookies with partitionKey

Fixes #1818

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 13:18:42 +08:00
hobostay
7fe26bc966 Fix TrackingAllocator reallocation_count being incremented on failed operations
The reallocation_count counter was being incremented regardless of whether
the resize/remap operations succeeded. This led to inaccurate memory
allocation statistics.

- resize: Only increment when rawResize returns true (success)
- remap: Only increment when rawRemap returns non-null (success)

This fixes the TODO comments that were present in the code.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 13:10:11 +08:00
Halil Durak
cc6587d6e5 make body.onload getter/setter alias to window.onload 2026-03-13 18:49:26 +03:00
Halil Durak
8b310ce993 add failing body.onload tests 2026-03-13 17:23:26 +03:00
Karl Seguin
be8ba53263 Merge pull request #1811 from lightpanda-io/script_handling
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Better script handling.
2026-03-13 21:40:19 +08:00
Pierre Tachoire
043d48d1c7 Merge pull request #1812 from lightpanda-io/longer-sleep
ci: add a longer sleep to wait for node start on wba test
2026-03-13 13:59:03 +01:00
Karl Seguin
e8fe80189b Merge pull request #1808 from lightpanda-io/cdp_startup_frames
Tweak CDP startup messages.
2026-03-13 19:24:14 +08:00
Pierre Tachoire
0e48f317cb ci: add a longer sleep to wait for node start on wba test 2026-03-13 12:22:48 +01:00
Karl Seguin
867745c71d Tweak CDP startup messages.
1 - When Target.setAutoAttach is called, send the `Target.attachedToTarget`
    event before sending the response. This matches Chrome's behavior and
    it stops playwright from thinking there's no target and making extra calls,
    e.g. to Target.attachedToTarget.

2 - Use the same dummy frameId for all startup messages. I'm not sure why we
    have STARTUP-P and STARTUP-B. Using the same frame (a) makes more sense to
    me (b) doesn't break any existing integration tests, and (c) improves this
    scenario: https://github.com/lightpanda-io/browser/issues/1800
2026-03-13 19:07:47 +08:00
Karl Seguin
a1a7919f74 Better script handling.
Dynamic scripts have script.async == true by default (we handled this correctly
in the ScriptManager, but we didn't return the right value when .async was
accessed).

Inline scripts only consider direct children, not the entire tree.

Empty inline scripts are executed at a later time if text is inserted into them
2026-03-13 19:05:23 +08:00
Pierre Tachoire
c3de47de90 Merge pull request #1810 from lightpanda-io/fix_cookie_loading
Ensure valid cookie isn't interpreted as null
2026-03-13 11:26:24 +01:00
Pierre Tachoire
dd35bdfeb4 Merge pull request #1809 from lightpanda-io/fix_flaky_test
Fix a flaky frame test
2026-03-13 10:17:01 +01:00
Karl Seguin
07c3aec34f Ensure valid cookie isn't interpreted as null
Use an explicit type when @ptrCast() is assigned to an optional to ensure the
value isn't interpreted as null.
2026-03-13 17:00:59 +08:00
Karl Seguin
bce3e8f7c6 Fix a flaky frame test
Loading `sub 1.html` has a side effect - it increments window.top..sub1_count).
So it should be used careful. It was being used in `about_blank_renavigate` as
a placeholder which _should_ not get navigated, but there's no strict guarantee
about when it gets canceled.
2026-03-13 15:39:34 +08:00
Pierre Tachoire
ba9777e754 Merge pull request #1609 from lightpanda-io/web-bot-auth
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Web Bot Auth
2026-03-13 08:31:25 +01:00
Pierre Tachoire
7040801dfa Merge pull request #1790 from lightpanda-io/structuredClone_serializer
Add window.structuredClone
2026-03-13 08:29:49 +01:00
Adrià Arrufat
f37862a25d perf: cache css properties for visibility and interactivity
Introduces `CssCache` to store computed CSS properties, avoiding
redundant stylesheet lookups during DOM traversals.
2026-03-13 14:00:07 +09:00
Adrià Arrufat
84d76cf90d browser: improve visibility and interactivity CSS checks
Adds support for `pointer-events: none` in interactivity classification
and expands `checkVisibility` to include `visibility` and `opacity`.
Refactors CSS property lookup into a shared helper.
2026-03-13 13:33:33 +09:00
Adrià Arrufat
e12f28fb70 Merge branch 'main' into css-improvements 2026-03-13 10:07:06 +09:00
Karl Seguin
4f8a6b62b8 Add window.structuredClone
Depends on https://github.com/lightpanda-io/zig-v8-fork/pull/156

Uses V8::Serializer and V8::Deserializer which handles built-in types, e.g.
regex. But it doesn't handle Zig types by default. This is something we need
to hook in, using the delegate callbacks. Which we can do after.

Meant to replace https://github.com/lightpanda-io/browser/pull/1785
2026-03-13 07:28:33 +08:00
Karl Seguin
d3dad772cf Merge pull request #1806 from lightpanda-io/update_zig_v8_action
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
update action.yml to latest zig-v8
2026-03-13 07:26:18 +08:00
Karl Seguin
944b672fea Merge pull request #1792 from lightpanda-io/Canvas_getImageData
Add dummy getImageData to canvas
2026-03-13 07:23:05 +08:00
Karl Seguin
b1c54aa92d Merge pull request #1795 from lightpanda-io/navigate_blob_url
Allow navigation from a blob URL.
2026-03-13 07:22:50 +08:00
Karl Seguin
4ca6f43aeb Merge pull request #1803 from lightpanda-io/fix-redirection-cookies
parse cookies on redirection during header callback
2026-03-13 07:17:51 +08:00
Karl Seguin
f09e66e1cc update action.yml to latest zig-v8 2026-03-13 07:15:23 +08:00
Karl Seguin
8b7a4ceaaa Merge pull request #1794 from lightpanda-io/update-docker-zig
update zig-v8 in dockerfile
2026-03-13 07:14:35 +08:00
Pierre Tachoire
51e90f5971 parse cookies on redirection during header callback
THe change to handle bot `\n` and `\r\n` for end HTTP headers skip the
cookie parsing in case of redirection.
2026-03-12 18:42:51 +01:00
Muki Kiboigo
8db64772b7 add URL getHost test 2026-03-12 09:04:13 -07:00
Muki Kiboigo
bf0be60b89 use new validator for e2e test 2026-03-12 09:04:13 -07:00
Muki Kiboigo
172481dd72 add e2e tests w/ web bot auth 2026-03-12 09:04:13 -07:00
Muki Kiboigo
c6c0492c33 fix request authentication with web bot auth 2026-03-12 09:04:13 -07:00
Muki Kiboigo
fca29a8be2 add WebBotAuth unit tests 2026-03-12 09:04:13 -07:00
Muki Kiboigo
d365240f91 fix cli argument for WebBotAuth domain 2026-03-12 09:04:12 -07:00
Muki Kiboigo
1ed61d4783 simplify parsePemPrivateKey 2026-03-12 09:04:12 -07:00
Muki Kiboigo
a1fb11ae33 make pem private key buffers smaller with comments 2026-03-12 09:04:12 -07:00
Muki Kiboigo
9971816711 use transfer arena to sign webbotauth request 2026-03-12 09:04:12 -07:00
Muki Kiboigo
c38d9a3098 auth challenge only on use_proxy 2026-03-12 09:04:12 -07:00
Muki Kiboigo
02198de455 add support for WebBotAuth in Client 2026-03-12 09:04:10 -07:00
Muki Kiboigo
6cd8202310 add WebBotAuth and support for ed25119 to crypto 2026-03-12 09:03:15 -07:00
Muki Kiboigo
4d7b7d1d42 add web bot auth args 2026-03-12 09:03:15 -07:00
Adrià Arrufat
dfe04960c0 css: remove cssText setter from CSSRule and CSSStyleRule 2026-03-12 22:47:41 +09:00
Adrià Arrufat
de2b1cc6fe css: throw IndexSizeError in deleteRule and insertRule 2026-03-12 22:40:01 +09:00
Adrià Arrufat
2aef4ab677 webapi.Element: optimize checkVisibility and refactor loops 2026-03-12 22:32:06 +09:00
Adrià Arrufat
798f68d0ce css: remove curly block helper functions 2026-03-12 22:29:51 +09:00
Adrià Arrufat
e0343a3f6d Replace ArrayListUnmanaged with ArrayList 2026-03-12 22:23:59 +09:00
Adrià Arrufat
d918ec694b css: add log filter to CSSStyleSheet test 2026-03-12 22:21:01 +09:00
Adrià Arrufat
b2b609a309 dom: remove verbose logging and simplify css logic 2026-03-12 22:07:58 +09:00
Adrià Arrufat
48dd80867b dom: support css display: none in checkVisibility
Updates `Element.checkVisibility` to iterate through document
stylesheets and check for matching rules with `display: none`.
Also ensures `<style>` elements register their sheets and
initializes them immediately upon addition to the DOM.
2026-03-12 20:55:44 +09:00
Adrià Arrufat
f58f6e8d65 css: improve CSSOM rule handling and serialization
Refactors `CSSRule` to a union type for better type safety and updates
`CSSStyleRule` to use `CSSStyleProperties`. Adds comprehensive tests for
`insertRule`, `deleteRule`, and `replaceSync`.
2026-03-12 20:23:59 +09:00
Karl Seguin
e4e21f52b5 Allow navigation from a blob URL.
These are used a lot in WPT test.
2026-03-12 18:58:10 +08:00
Pierre Tachoire
84e1cd08b6 update zig-v8 in dockerfile 2026-03-12 11:54:06 +01:00
Pierre Tachoire
7796753e7a Merge pull request #1791 from lightpanda-io/wp/mrdimidium/update-v8
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Update zig-v8
2026-03-12 11:48:27 +01:00
Karl Seguin
880205e874 Add dummy getImageData to canvas
Probably doesn't solve many (if any) WPT tests, but it moves them further along.
2026-03-12 17:53:00 +08:00
Nikolay Govorov
1b96087b08 Update zig-v8 2026-03-12 08:50:33 +00:00
Karl Seguin
aa246c9e9f Merge pull request #1788 from lightpanda-io/range_cleanup
Add cleanup to Range
2026-03-12 16:45:05 +08:00
Adrià Arrufat
ee034943b6 feat(css): implement stylesheet rule management
Adds a CSS rule parser and implements `insertRule`, `deleteRule`, and
`replaceSync` in `CSSStyleSheet`. Also updates `CSSRuleList` to use
dynamic storage and populates sheets from `<style>` elements.
2026-03-12 16:27:25 +09:00
Karl Seguin
f1d311d232 Merge pull request #1781 from lightpanda-io/wp/mrdimidium/telemetry-network
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Use global connections poll
2026-03-12 13:46:51 +08:00
Karl Seguin
e4f7fca10d Merge pull request #1789 from lightpanda-io/fix-test-warnings
testing: add LogFilter utility for scoped log suppression
2026-03-12 13:40:13 +08:00
Adrià Arrufat
3d6d669a50 testing: add LogFilter utility for scoped log suppression 2026-03-12 13:56:53 +09:00
Nikolay Govorov
c4097e2b7e remove dead-code 2026-03-12 03:55:48 +00:00
Karl Seguin
619d27c773 Add cleanup to Range
In https://github.com/lightpanda-io/browser/pull/1774 we started to track Ranges
in the page in order to correctly make them "live". But, without correct
lifetime, they would continue to be "live" even if out of scope in JS.

This commit adds finalizers to Range via reference counting similar to Events.
It _is_ possible for a Range to outlive its page, so we can't just remove the
range from the Page's _live_range list - the page might not be valid. This
commit gives every page an unique id and the ability to try and get the page
by id from the session. By capturing the page_id at creation-time, a Range
can defensively remove itself from the page's list. If the page is already
gone, then there's no need to do anything.
2026-03-12 10:38:07 +08:00
Karl Seguin
1522c90294 Merge pull request #1787 from lightpanda-io/dummy-filelist
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Add FileList Web API stub
2026-03-12 06:37:00 +08:00
Karl Seguin
794e15ce21 Merge pull request #1786 from lightpanda-io/fontfaceset-load
FontFaceSet is now an EventTarget
2026-03-12 06:36:22 +08:00
Karl Seguin
34771b835e Merge pull request #1783 from lightpanda-io/custom_element_dynamic_markup_handling
Throw on dynamic markup in custom element callbacks during parsing
2026-03-12 06:27:22 +08:00
Karl Seguin
8df51b232a Merge pull request #1784 from lightpanda-io/origin_arena
Use origin.arena for values that are tied to the origin
2026-03-12 06:26:20 +08:00
Karl Seguin
13b8ce18b2 Merge pull request #1780 from lightpanda-io/anchor_and_form_target
Add support for target attribute on anchors and forms
2026-03-12 06:26:08 +08:00
Pierre Tachoire
448386e52b Add FileList Web API stub
Next.js hydration references FileList as a global for feature detection.
Register a minimal stub (length=0, item()→null) so the type exists in
the global scope and the reference check doesn't throw.
2026-03-11 22:31:12 +01:00
Pierre Tachoire
bf07659dd5 FontFaceSet is now an EventTarget
Dispatch loading and loaddone events on load() call
2026-03-11 22:18:42 +01:00
Karl Seguin
16dfad0895 Use origin.arena for values that are tied to the origin
Of note, the TAO and identity map entry has to use the origin arena, not
the context arena, as those can outlive the context.
2026-03-11 21:55:58 +08:00
Adrià Arrufat
f61449c31c Merge pull request #1776 from lightpanda-io/semantic-tree
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Add native Semantic Tree extraction engine for AI agents
2026-03-11 21:01:04 +09:00
Adrià Arrufat
60699229ca Merge branch 'main' into semantic-tree 2026-03-11 20:52:39 +09:00
Karl Seguin
e1dd26b307 Throw on dynamic markup in custom element callbacks during parsing
Custom element callbacks aren't allowed to call document.open/close/write while
parsing.

Fixes WPT crash:
/custom-elements/throw-on-dynamic-markup-insertion-counter-reactions.html
2026-03-11 18:41:06 +08:00
Pierre Tachoire
7d835ef99d Merge pull request #1778 from lightpanda-io/wp/mrdimidium/libcurl-malloc
Use zig allocator for libcurl
2026-03-11 10:13:13 +01:00
Karl Seguin
0971df4dfc Merge pull request #1782 from lightpanda-io/silence_shutdown_error_on_non_linux
Don't log SocketNotConnected when shutting down listener on non-Linux
2026-03-11 16:39:15 +08:00
Halil Durak
9fb57fbac0 Merge pull request #1771 from lightpanda-io/nikneym/compile-function
Prefer `ScriptCompiler::CompileFunction` to compile attribute listeners
2026-03-11 11:38:16 +03:00
Karl Seguin
48ead90850 Don't log SocketNotConnected when shutting down listener on non-Linux
On BSD, a listening socket isn't considered connected, so this error is
expected. Maybe we shouldn't call shutdown at all, but I guess it's safer this
way.
2026-03-11 16:29:44 +08:00
Pierre Tachoire
cc88bb7feb Merge pull request #1777 from lightpanda-io/mcp-missing-lp-commands
mcp: add interactiveElements and structuredData tools
2026-03-11 09:11:48 +01:00
Karl Seguin
a725e2aa6a Merge pull request #1774 from egrs/range-chardata-mutations
update live ranges after CharacterData and DOM mutations
2026-03-11 16:04:41 +08:00
Karl Seguin
ee637c3662 Add support for target attribute on anchors and forms 2026-03-11 15:49:30 +08:00
Adrià Arrufat
65d7a39554 SemanticTree: use payload captures for CData.Text checks
Improves conciseness and idiomatic Zig style by replacing .is(CData.Text) != null and .as() with direct payload captures in if statements.
2026-03-11 16:39:59 +09:00
Adrià Arrufat
37735b1caa SemanticTree: use StaticStringMap for structural role check
Improves performance and readability of isStructuralRole. Also includes minor syntax cleanup in AXNode.
2026-03-11 16:37:24 +09:00
Pierre Tachoire
c8f8d79f45 Merge pull request #1775 from lightpanda-io/arena_blob
Use arena from ArenaPool for Blob (and File)
2026-03-11 08:35:27 +01:00
Adrià Arrufat
1866e7141e SemanticTree: cast with as
Co-authored-by: Karl Seguin <karlseguin@users.noreply.github.com>
2026-03-11 16:33:39 +09:00
Adrià Arrufat
feccc9f5ce AXNode: remove unused mock JSON lifecycle methods
Simplifies TextCaptureWriter by removing unused methods, ensuring future changes to writeName will fail at build time if new methods are required.
2026-03-11 16:25:34 +09:00
Adrià Arrufat
af803da5c8 cdp.lp: use enum for getSemanticTree format param
Leverages std.json.parse to automatically validate the format param into a type-safe enum.
2026-03-11 16:21:43 +09:00
egrs
25c89c9940 Revert "remove ranges from live list on GC finalization"
This reverts commit 625d424199.
2026-03-11 08:04:53 +01:00
egrs
697a2834c2 Revert "fix CI: store list pointer on AbstractRange to avoid Page type mismatch"
This reverts commit 056b8bb536.
2026-03-11 08:04:51 +01:00
egrs
056b8bb536 fix CI: store list pointer on AbstractRange to avoid Page type mismatch
The bridge.finalizer resolves Page through its own module graph, which
can differ from Range.zig's import in release builds. Store a pointer
to the live_ranges list directly on AbstractRange so deinit can remove
without accessing Page fields.
2026-03-11 07:58:31 +01:00
egrs
625d424199 remove ranges from live list on GC finalization
Add a weak finalizer to Range that removes its linked list node from
Page._live_ranges when V8 garbage-collects the JS Range object. This
prevents the list from growing unboundedly and avoids iterating over
stale entries during mutation updates.
2026-03-11 07:27:39 +01:00
Adrià Arrufat
5329d05005 interactive: optimize getTextContent single-chunk path
Avoids an unnecessary double allocation and maintains a zero-copy fast path for single-chunk text extraction.
2026-03-11 15:27:12 +09:00
egrs
d2c55da6c9 address review: move per-range logic to AbstractRange, simplify collapsed check
Move the per-range update logic from Page into AbstractRange methods
(updateForCharacterDataReplace, updateForSplitText, updateForNodeInsertion,
updateForNodeRemoval). Page now just iterates the list and delegates.

Remove redundant start_container == end_container check in insertNode —
collapsed already implies same container.
2026-03-11 07:26:20 +01:00
Adrià Arrufat
2e6dd3edfe browser.EventManager: remove unused hasListener function 2026-03-11 15:18:14 +09:00
Nikolay Govorov
a95b4ea7b9 Use global connections poll 2026-03-11 05:44:59 +00:00
Nikolay Govorov
c891eff664 Use zig allocator for libcurl 2026-03-11 03:34:27 +00:00
Adrià Arrufat
68564ca714 mcp: add interactiveElements and structuredData tools 2026-03-11 11:09:19 +09:00
Adrià Arrufat
ca931a11be AXNode: add spacing between concatenated text nodes
When calculating accessible names for elements without explicit labels, multiple descendant text nodes were previously concatenated directly together. This adds a space between distinct text node contents to prevent words from sticking together.
2026-03-11 10:45:07 +09:00
Adrià Arrufat
6c7272061c cli: enable pruning for semantic_tree_text dump mode
Previously, semantic_tree_text hardcoded prune = false, which bypassed the structural node filters and allowed empty none nodes to pollute the root of the text dump.
2026-03-11 10:38:12 +09:00
Adrià Arrufat
4f262e5bed SemanticTree: filter computed names for generic containers
This prevents token bloat in JSON/text dumps and ensures that StaticText leaf nodes are not incorrectly pruned when structural containers (like none, table) hoist their text.
2026-03-11 10:22:40 +09:00
Karl Seguin
ff26b0d5a4 Use arena from ArenaPool for Blob (and File) 2026-03-11 09:21:54 +08:00
Adrià Arrufat
a6ccc72d15 interactive: properly concatenate text content for accessible names
This fixes a bug where only the first text node was being returned, causing fragmented text nodes (e.g. <span>Sub</span><span>mit</span>) to be missing their trailing text.
2026-03-11 09:57:08 +09:00
Karl Seguin
487ee18358 Merge pull request #1742 from lightpanda-io/context_origins
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Context origins
2026-03-11 08:54:53 +08:00
Karl Seguin
dc3d2e9790 Remove root context check from Env
This was only added [very briefly] when Env managed Origins, which it no longer
does.
2026-03-11 08:44:52 +08:00
Karl Seguin
f6d0e484b0 transfer finalizers on origin change 2026-03-11 08:44:52 +08:00
Karl Seguin
4cea9aba3c update v8 dep 2026-03-11 08:44:51 +08:00
Karl Seguin
7348a68c84 merge main 2026-03-11 08:44:51 +08:00
Karl Seguin
7d90c3f582 Move origin lookup to Session
With the last commit, this becomes the more logical place to hold this as it
ties into the Session's awareness of the root page's lifetime.
2026-03-11 08:44:51 +08:00
Karl Seguin
2a103fc94a Use Session as a container for cross-frame resources
The introduction of frames means that data is no longer tied to a specific Page
or Context. 255b9a91cc introduced Origins for
v8 values shared across frames of the same origin. The commit highlighted the
lifetime mismatched that we now have with data that can outlive 1 frame. A
specific issue with that commit was the finalizers were still Context-owned.
But like any other piece of data, that isn't right; aside from modules, nothing
should be context-owned.

This commit continues where the last left off and moves finalizers from Context
to Origin. This is done in a separate commit because it introduces significant
changes. Currently, finalizers take a *Page, but that's no longer correct. A
value created in one Page, can outlive the Page. We need another container. I
original thought to use Origin, but that isn't know to CDP/MCP. Instead, I
decide to enhance the Session.

Session is now the owner of the page.arena, the page.factory and the
page.arena_pool. Finalizers are given a *Session which they can use to release
their arena.
2026-03-11 08:44:49 +08:00
Karl Seguin
753391b7e2 Add origins safety cleanup when destroying the context for the root page 2026-03-11 08:43:41 +08:00
Karl Seguin
94ce5edd20 Frames on the same origin share v8 data
Depends on: https://github.com/lightpanda-io/zig-v8-fork/pull/153

In some ways this is an extension of
https://github.com/lightpanda-io/browser/pull/1635 but it has more implications
with respect to correctness.

A js.Context wraps a v8::Context. One of the important thing it adds is the
identity_map so that, given a Zig instance we always return the same v8::Object.

But imagine code running in a frame. This frame has its own Context, and thus
its own identity_map. What happens when that frame does:

```js
window.top.frame_loaded = true;
```

From Zig's point of view, `Window.getTop` will return the correct Zig instance.
It will return the *Window references by the "root" page. When that instance is
passed to the bridge, we'll look for the v8::Object in the Context's
`identity_map` but wont' find it. The mapping exists in the root context
`identity_map`, but not within this frame. So we create a new v8::Object and now
our 1 zig instance has N v8::Objects for every page/frame that tries to access
it.

This breaks cross-frame scripting which should work, at least to some degree,
even when frames are on the same origin.

This commit adds a `js.Origin` which contains the `identity_map`, along with our
other `v8::Global` storage. The `Env` now contains a `*js.Origin` lookup,
mapping an origin string (e.g. lightpanda.io:443) to an *Origin. When a Page's
URL is changed, we call `self.js.setOrigin(new_url)` which will then either get
or create an origin from the Env's origin lookup map.

js.Origin is reference counted so that it remains valid so long as at least 1
frame references them.

There's some special handling for null-origins (i.e. about:blank). At the root,
null origins get a distinct/isolated Origin. For a frame, the parent's origin
is used.

Above, we talked about `identity_map`, but a `js.Context` has 8 other fields
to track v8 values, e.g. `global_objects`, `global_functions`,
`global_values_temp`, etc. These all must be shared by frames on the same
origin. So all of these have also been moved to js.Origin. They've also been
merged so that we now have 3 fields: `identity_map`, `globals` and `temps`.

Finally, when the origin of a context is changed, we set the v8::Context's
SecurityToken (to that origin). This is a key part of how v8 allows cross-
context access.
2026-03-11 08:43:40 +08:00
Nikolay Govorov
3626f70d3e Merge pull request #1759 from lightpanda-io/wp/mrdimidum/net-poll-runtime
Network poll runtime
2026-03-10 23:38:07 +00:00
Nikolay Govorov
24cc24ed50 Fix Robots deinit 2026-03-10 23:28:40 +00:00
Karl Seguin
dd29ba4664 Merge pull request #1767 from egrs/css-value-normalization-gaps
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
extend CSS value normalization to cover more properties
2026-03-11 06:28:34 +08:00
egrs
7927ad8fcf route appendData through replaceData for spec compliance
Per DOM spec, appendData(data) is defined as replaceData(length, 0, data).
While the range update would be a no-op (offset=length, count=0), routing
through replaceData ensures consistent code path and spec compliance.
2026-03-10 20:27:05 +01:00
egrs
d23453ce45 update live ranges after CharacterData and DOM mutations
Per DOM spec, all live ranges must have their boundary offsets updated
when CharacterData content changes (insertData, deleteData, replaceData,
splitText) or when nodes are inserted/removed from the tree.

Track live ranges via an intrusive linked list on Page. After each
mutation, iterate and adjust start/end offsets per the spec algorithms.

Also fix Range.deleteContents loop that read _end_offset on each
iteration (now decremented by the range update), and Range.insertNode
that double-incremented _end_offset for non-collapsed ranges.

Route textContent, nodeValue, and data setters through replaceData
so range updates fire consistently.

Fixes 9 WPT test files (all now 100%): Range-mutations-insertData,
deleteData, replaceData, splitText, appendChild, insertBefore,
removeChild, appendData, dataChange (~1330 new passing subtests).
2026-03-10 19:59:04 +01:00
Halil Durak
a22040efa9 update body.onload test 2026-03-10 19:16:35 +03:00
Halil Durak
ba3da32ce6 spread new stringToPersistedFunction 2026-03-10 19:16:20 +03:00
Halil Durak
9d2ba52160 adapt stringToPersistedFunction to compileFunction
This is just a thin wrapper around it now.
2026-03-10 19:15:53 +03:00
Halil Durak
e610506df4 Local: initial compileFunction 2026-03-10 18:14:35 +03:00
Pierre Tachoire
dd91d28bfa Merge pull request #1761 from lightpanda-io/wp/mrdimidium/c-tsan
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Enable tsan for c libs
2026-03-10 15:33:33 +01:00
Pierre Tachoire
1ebf7460fe Merge pull request #1768 from lightpanda-io/inspector_cleanup
Call `resetContextGroup` on page removal
2026-03-10 15:32:47 +01:00
Pierre Tachoire
8c930e5c33 Merge pull request #1769 from lightpanda-io/form_action
Add Form.action getter/setter
2026-03-10 15:31:34 +01:00
egrs
4fb2f7474c remove incorrect entries from normalization maps
- Remove scale, contain-intrinsic-size, animation-range, text-box-edge
  from isTwoValueShorthand: these have asymmetric or 3-value semantics
  that make "X X" → "X" collapse incorrect.
- Remove line-height from isLengthProperty: bare 0 is the unitless
  number multiplier, not a length (Chrome serializes as "0" not "0px").
- Fix test: background-size "cover cover" is invalid CSS, use "auto auto".
2026-03-10 14:08:28 +01:00
Karl Seguin
5301f79989 Add Form.action getter/setter 2026-03-10 20:58:31 +08:00
egrs
6a7f7fdf15 extend CSS value normalization to cover more properties
Add missing properties to isLengthProperty (0→0px) and
isTwoValueShorthand (duplicate value collapse) maps based
on WPT test failures in css/css-sizing, css/css-align,
css/css-scroll-snap, css/css-logical, and others.

New length properties: scroll-margin/padding-*, column-width,
column-rule-width, grid-column/row-gap, outline, shape-margin,
offset-distance, translate, animation-range-*, block-step-size,
text-decoration-inset, and *-rule-*-inset (CSS Gaps).

New two-value shorthands: scroll-padding-block/inline,
scroll-snap-align, background-size, border-image-repeat,
mask-repeat/size, contain-intrinsic-size, scale, text-box-edge,
animation-range, grid-gap.
2026-03-10 13:53:27 +01:00
Karl Seguin
11fb5f990e Call resetContextGroup on page removal
Calling it here ensures that the inspector gets reset on internal page
navigation. We were seeing intermittent segfaults on a problematic WPT tests
(/encoding/legacy-mb-japanese/euc-jp/) which I believe this solves.

(The tests are still broken. Because we don't support form targets, they cause
the root page to reload in a tight cycle, causing a lot of context creation /
destruction, which I thin was the issue. This commit doesn't fix the broken test
but it hopefully fixes the crash).

Also, clear out the Inspector's default_context when the default context is
destroyed. (This was the first thing I did to try to fix the crash, it didn't
work, but I believe it's correct).
2026-03-10 20:50:58 +08:00
Adrià Arrufat
d1ee0442ea Merge branch 'main' into semantic-tree 2026-03-10 21:48:49 +09:00
Adrià Arrufat
62f31ea24a Merge pull request #1765 from egrs/lp-get-structured-data
add LP.getStructuredData CDP command
2026-03-10 21:48:18 +09:00
egrs
f4ca5313e6 use std.mem.startsWith, group duplicate property keys into arrays
Address review feedback:
- replace custom startsWith helper with std.mem.startsWith
- writeProperties now groups repeated keys (e.g. multiple og:image)
  into JSON arrays; single-occurrence keys remain strings
- add test for duplicate key serialization
2026-03-10 13:18:25 +01:00
Adrià Arrufat
064e7b404b SemanticTree: unify interactivity detection logic 2026-03-10 19:02:55 +09:00
Karl Seguin
dfd90bd564 Merge pull request #1754 from lightpanda-io/css_value_normalization
Apply some normalization to CSS values
2026-03-10 17:36:27 +08:00
Pierre Tachoire
55508eb418 Merge pull request #1763 from lightpanda-io/has_direct_listener
Add a hasDirectListeners to EventManager
2026-03-10 10:28:39 +01:00
Pierre Tachoire
2a4fa4ed6f Merge pull request #1762 from lightpanda-io/xml_get_elements_by_tag_name
Node matching using tag name string comparison on non-HTML nodes
2026-03-10 10:27:47 +01:00
Pierre Tachoire
cf7c9f6372 Merge pull request #1760 from lightpanda-io/response_blob
Add new Response and Request methods
2026-03-10 10:26:16 +01:00
Pierre Tachoire
ec68c3207d Merge pull request #1764 from lightpanda-io/js_val_args
Better support for variadic js.Value parameter (e.g. console.log)
2026-03-10 10:16:27 +01:00
Pierre Tachoire
ecf140f3d6 Merge pull request #1766 from lightpanda-io/screenshot-size
cdp: reszie the screenshot to 1920x1080
2026-03-10 10:15:46 +01:00
Pierre Tachoire
13f73b7b87 Merge pull request #1750 from lightpanda-io/url_set_username_password
Add setters to URL.username and URL.password
2026-03-10 10:15:10 +01:00
Pierre Tachoire
12c5bcd24f cdp: reszie the screenshot to 1920x1080
To be consistent w/ layout size returned
2026-03-10 10:09:53 +01:00
Adrià Arrufat
56f47ee574 Merge branch 'main' into semantic-tree 2026-03-10 17:26:34 +09:00
egrs
74f0436ac7 merge main, resolve conflicts with getInteractiveElements 2026-03-10 09:25:12 +01:00
egrs
22d31b1527 add LP.getStructuredData CDP command 2026-03-10 09:19:51 +01:00
Karl Seguin
9f3bca771a Merge pull request #1755 from lightpanda-io/cdp-page-layout-metrics
cdp: add a dummy Page.getLayoutMetrics
2026-03-10 16:16:17 +08:00
Adrià Arrufat
4e16d90a81 Merge pull request #1757 from egrs/lp-get-interactive-elements
add LP.getInteractiveElements CDP command
2026-03-10 17:15:18 +09:00
Pierre Tachoire
d669d5c153 cdp: add a dummy Page.getLayoutMetrics 2026-03-10 08:54:48 +01:00
Karl Seguin
343d985e96 Better support for variadic js.Value parameter (e.g. console.log)
The bridge will prefer to map a Zig array to a JS Array, but in the case of
a []js.Value, it should be willing to map anything into it.
2026-03-10 15:40:18 +08:00
egrs
dc3958356d address review feedback
- TreeWalker.Full instead of FullExcludeSelf so querying a specific
  nodeId evaluates the root element itself
- resolve href to absolute URL via URL.resolve
- isDisabled checks ancestor <fieldset disabled> with legend exemption
- parameter order: allocator before *Page per convention
2026-03-10 08:13:01 +01:00
Karl Seguin
c4e85c3277 Add a hasDirectListeners to EventManager
Allows checking if a direct listener exists, if it doesn't, event creation can
be skipped.

I looked at a couple sites, the benefits of this is small.
Most sites don't seem to trigger that many direct dispatches and when they do,
they seem to have a listener 50-75% of the time.
2026-03-10 14:57:40 +08:00
Karl Seguin
89e46376dc Merge pull request #1752 from lightpanda-io/build-zig-fmt-check
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
build: add code formatting check
2026-03-10 14:04:28 +08:00
Karl Seguin
8eeb34dba8 Node matching using tag name string comparison on non-HTML nodes
NodeLive (used by, e.g. getElementsByTagName) needs to revert to the non-
optimized string-comparison for non-HTML tags.

This should help fix https://github.com/lightpanda-io/browser/issues/1214
2026-03-10 13:42:54 +08:00
Nikolay Govorov
7171305972 Enable tsan for c libs 2026-03-10 03:16:50 +00:00
Nikolay Govorov
2b0c223425 Some code-review fixes 2026-03-10 03:00:55 +00:00
Nikolay Govorov
8f960ab0f7 Uses posix pipe for shutdown network runtime 2026-03-10 03:00:53 +00:00
Nikolay Govorov
60350efa10 Only one listener in network.Runtime 2026-03-10 03:00:52 +00:00
Nikolay Govorov
687f577562 Move accept loop to common runtime 2026-03-10 03:00:50 +00:00
Nikolay Govorov
8e59ce9e9f Prepare global NetworkRuntime module 2026-03-10 03:00:47 +00:00
Karl Seguin
33d75354a2 Add new Response and Request methods
-Response.blob
-Response.clone
-Request.blob
-Request.text
-Request.json
-Request.arrayBuffer
-Request.bytes
-Request.clone
2026-03-10 09:05:06 +08:00
Adrià Arrufat
a318c6263d SemanticTree: improve visibility, AX roles and xpath generation
- Use `checkVisibility` for more accurate element visibility detection.
- Add support for color, date, file, and month AX roles.
- Optimize XPath generation by tracking sibling indices during the walk.
- Refine interactivity detection for form elements.
2026-03-10 09:23:06 +09:00
Karl Seguin
0e4a65efb7 Merge pull request #1758 from lightpanda-io/http-auth-challenge
http: handle auth challenge for non-proxy auth
2026-03-10 06:39:14 +08:00
Karl Seguin
b88134cf04 Merge pull request #1756 from lightpanda-io/cdp-screenshot
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
cdp: add dummy page.captureScreenshot
2026-03-10 06:37:33 +08:00
Karl Seguin
2aaa212dbc Merge pull request #1753 from lightpanda-io/document_applets
document.applets should always return an empty collection
2026-03-10 06:35:23 +08:00
Karl Seguin
1e37990938 Merge pull request #1741 from lightpanda-io/DOMParser_invalid_xml
Throw exception, as expected, on empty input to DOMParser.parseFromSt…
2026-03-10 06:32:48 +08:00
egrs
a417c73bf7 add LP.getInteractiveElements CDP command
Returns a structured list of all interactive elements on a page:
buttons, links, inputs, ARIA widgets, contenteditable regions, and
elements with event listeners. Includes accessible names, roles,
listener types, and key attributes.

Event listener introspection (both addEventListener and inline
handlers) is unique to LP — no other browser exposes this to
automation code.
2026-03-09 19:46:12 +01:00
Pierre Tachoire
37c34351ee http: handle auth challenge for non-proxy auth 2026-03-09 19:23:36 +01:00
Pierre Tachoire
8672232ee2 cdp: add dummy page.captureScreenshot 2026-03-09 17:38:57 +01:00
Adrià Arrufat
83ba974f94 SemanticTree: optimize tree walking and xpath generation
- Use a reusable buffer for XPaths to reduce allocations.
- Improve `display: none` detection with proper CSS parsing.
- Pass parent name to children to avoid redundant AXNode lookups.
- Use `getElementById` for faster datalist lookups.
2026-03-09 22:53:39 +09:00
Adrià Arrufat
85ebbe8759 SemanticTree: improve accessibility tree and name calculation
- Add more structural roles (banner, navigation, main, list, etc.).
- Implement fallback for accessible names (SVG titles, image alt text).
- Skip children for leaf-like semantic nodes to reduce redundancy.
- Disable pruning in the default semantic tree view.
2026-03-09 21:04:47 +09:00
Adrià Arrufat
61cba3f6eb Merge branch 'main' into semantic-tree 2026-03-09 20:13:47 +09:00
Karl Seguin
3ad10ff8d0 Add support for normalization anchor-size css value
vibed this. Seems esoteric, but it helps over 1000 WPT cases pass in
/css/css-anchor-position/anchor-size-parse-valid.html
2026-03-09 18:25:01 +08:00
Karl Seguin
183643547b document.applets should always return an empty collection
Add a new .empty mode to HTMLCollection.

Fixes WPT /shadow-dom/leaktests/html-collection.html
2026-03-09 18:06:22 +08:00
Adrià Arrufat
5568340b9a build: add code formatting check 2026-03-09 18:48:38 +09:00
Karl Seguin
1399bd3065 Apply some normalization to CSS values
"10px 10px" should present as "10px".  A length of "0" should present as "0px"

Fixes a handful of WPT tests.
2026-03-09 17:47:59 +08:00
Karl Seguin
9172e16e80 Merge pull request #1751 from lightpanda-io/zig-fmt-face
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
zig fmt
2026-03-09 17:34:17 +08:00
Adrià Arrufat
3e5f602396 zig fmt 2026-03-09 18:25:09 +09:00
Adrià Arrufat
3c97332fd8 feat(dump): add semantic_tree and semantic_tree_text formats
Adds support for dumping the semantic tree in JSON or text format
via the --dump option. Updates the Config enum and usage help.
2026-03-09 18:23:52 +09:00
Karl Seguin
379a3f27b8 Merge pull request #1744 from egrs/add-range-client-rect
add Range.getBoundingClientRect and getClientRects
2026-03-09 17:17:17 +08:00
Karl Seguin
ecec932a47 Add setters to URL.username and URL.password
Also, preserve port when setting host.
2026-03-09 17:13:12 +08:00
egrs
e239f69f69 delegate Range rect methods to container element
Instead of always returning zeros, delegate getBoundingClientRect and
getClientRects to the common ancestor container element. Return zeros
only when the range is collapsed or has no element ancestor.
2026-03-09 10:09:11 +01:00
Adrià Arrufat
c77cb317c4 Merge branch 'main' into semantic-tree 2026-03-09 18:08:10 +09:00
Karl Seguin
034b089433 Merge pull request #1749 from lightpanda-io/empty_is_and_where_pseudoselector
Empty :is() and :where() pseudoselectors are valid (and return nothing)
2026-03-09 16:55:43 +08:00
Karl Seguin
c0db96482c Merge pull request #1748 from lightpanda-io/font_face_optimization
Optimize FontFace
2026-03-09 16:55:28 +08:00
Karl Seguin
ffa8fa0a6f Merge pull request #1745 from lightpanda-io/renavigate_memory_leak
Fix leak introduced in inner navigation refactoring
2026-03-09 16:55:12 +08:00
Karl Seguin
7e1d459a2d Merge pull request #1746 from egrs/fix-module-relative-imports
fix module re-import when previous compilation failed
2026-03-09 16:44:43 +08:00
Karl Seguin
71c4fce87f Empty :is() and :where() pseudoselectors are valid (and return nothing) 2026-03-09 16:39:44 +08:00
Karl Seguin
e91da78ebb Optimize FontFace
Follow up to https://github.com/lightpanda-io/browser/pull/1743

Allow eager cleanup with finalizer. User properties for (what are currently)
constants.
2026-03-09 16:08:17 +08:00
egrs
8adad6fa61 fix module re-import when previous compilation failed
When a module's compilation fails after its imported_modules entry has
been consumed by waitForImport, sibling modules that import the same
dependency would get UnknownModule errors. Fix by re-preloading modules
whose cache entry exists but has no compiled module.
2026-03-09 08:58:07 +01:00
Karl Seguin
b47004bb7c Merge pull request #1743 from egrs/add-fontface-constructor
add FontFace constructor and FontFaceSet.add()
2026-03-09 15:57:59 +08:00
Karl Seguin
08a7fb4de0 Fix leak introduced in inner navigation refactoring
A inner-navigate event can override an existing pending queued navigation. When
it does, the previously queued navigation has to be cleaned up. We were doing
this, but it must have been stripped out when navigation was refactored to work
with frames.
2026-03-09 15:51:26 +08:00
Karl Seguin
c17a9b11cc Merge pull request #1740 from egrs/fix-dynamic-inline-scripts
execute dynamically inserted inline script elements
2026-03-09 15:43:28 +08:00
egrs
245a92a644 use node.firstChild() directly per review feedback
node is already available in scope — no need to traverse back through
script.asConstElement().asConstNode().
2026-03-09 08:31:54 +01:00
Pierre Tachoire
6b313946fe Merge pull request #1739 from lightpanda-io/wpt-procs
wpt: use a pool of browser to run tests
2026-03-09 08:29:16 +01:00
egrs
4586fb1d13 add Range.getBoundingClientRect and getClientRects
headless stubs returning zero-valued DOMRect / empty list per CSSOM
View spec. fixes "getBoundingClientRect is not a function" errors on
sites where layout code calls this on Range objects (e.g. airbnb).
2026-03-09 08:23:19 +01:00
egrs
aa051434cb add FontFace constructor and FontFaceSet.add()
headless stub for the FontFace API — constructor stores family/source,
status is always "loaded", load() resolves immediately. enables sites
that use new FontFace() for programmatic font loading (e.g. boursorama).
2026-03-09 08:14:41 +01:00
Adrià Arrufat
c3a53752e7 CDP: simplify AXNode name extraction logic 2026-03-09 15:34:59 +09:00
Karl Seguin
f3e1204fa1 Throw exception, as expected, on empty input to DOMParser.parseFromString
https://github.com/lightpanda-io/browser/issues/1738
2026-03-09 13:46:36 +08:00
Adrià Arrufat
0a5eb93565 SemanticTree: Implement compound component metadata 2026-03-09 13:42:53 +09:00
Adrià Arrufat
b8a3135835 SemanticTree: add pruning support and move logic to walk 2026-03-09 13:02:03 +09:00
Adrià Arrufat
330dfccb89 webapi/Element: add missing block tags and reorganize checks 2026-03-09 11:23:52 +09:00
Adrià Arrufat
d80e926015 SemanticTree: unify tree traversal using visitor pattern 2026-03-09 11:09:27 +09:00
Adrià Arrufat
2a2b067633 mcp: fix wrong merge 2026-03-09 10:37:21 +09:00
Adrià Arrufat
be73c14395 SemanticTree: rename dump to dumpJson and update log tags 2026-03-09 10:29:32 +09:00
Adrià Arrufat
9cd5afe5b6 Merge branch 'main' into semantic-tree 2026-03-09 10:18:54 +09:00
Pierre Tachoire
1cb5d26344 wpt: use a pool of browser to run tests 2026-03-08 20:55:15 +01:00
egrs
ec9a2d8155 execute dynamically inserted inline script elements
Scripts created via createElement('script') with inline content (textContent,
text, or innerHTML) and inserted into the DOM now execute per the HTML spec.
Previously all dynamically inserted scripts without a src attribute were
skipped, breaking most JS framework hydration patterns.
2026-03-08 16:17:52 +01:00
Adrià Arrufat
4ba40f2295 CDP: implement intelligent pruning for textified semantic tree output 2026-03-08 22:48:22 +09:00
Adrià Arrufat
b674c2e448 CDP/MCP: add highly compressed text format for semantic tree 2026-03-08 22:42:00 +09:00
Karl Seguin
0227afffc8 Merge pull request #1735 from egrs/fix-missing-dom-exception-flags
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
add missing dom_exception flags to bridge declarations
2026-03-08 16:36:56 +08:00
Adrià Arrufat
b8139a6e83 CDP/MCP: improve Stagehand compatibility for semantic tree 2026-03-08 15:48:44 +09:00
Adrià Arrufat
bde5fc9264 Merge branch 'main' into semantic-tree 2026-03-08 08:18:08 +09:00
Karl Seguin
6a421a1d96 Merge pull request #1734 from lightpanda-io/mcp_safer_navigate
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Fix page re-navigate
2026-03-08 07:17:39 +08:00
egrs
4f55a0f1d0 add missing dom_exception flags to bridge declarations
atob, Performance.measure, and Navigation methods (back, forward,
navigate, traverseTo, updateCurrentEntry) return DOMException errors
but were missing the dom_exception flag, causing them to throw generic
Error objects instead of proper DOMException instances in JavaScript.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-07 14:34:28 +01:00
Karl Seguin
3de55899fa fix test 2026-03-07 11:04:22 +08:00
Karl Seguin
ae4ad713ec Fix page re-navigate
It isn't safe/correct to call `navigate` on the same page multiple times. A page
is meant to have 1 navigate call. The caller should either remove the page
and create a new one, or call Session.replacePage.

This commit removes the *Page from the MCP Server and instead interacts with
the session to create or remove+create the page as needed, and lets the Session
own the *Page.

It also adds a bit of defensiveness around parameter parsing, e.g. calling
{"method": "tools/call"} (without an id) now errors instead of crashing.
2026-03-07 10:19:37 +08:00
Karl Seguin
21313adf9c Merge pull request #1728 from lightpanda-io/about_blank
Some checks failed
e2e-test / zig build release (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / zig build release (push) Has been cancelled
wpt / build wpt runner (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
Optimize about:blank loading in general and for frames specifically
2026-03-06 23:38:11 +08:00
Karl Seguin
9c1293ca45 Merge pull request #1729 from lightpanda-io/target_navigation
Add target-aware(ish) navigation
2026-03-06 23:38:01 +08:00
Karl Seguin
1cb1e6b680 Merge pull request #1720 from lightpanda-io/frame_scheduled_navigation
Improve frame sub-navigation
2026-03-06 23:37:49 +08:00
Karl Seguin
ed6ddeaa4c Merge pull request #1732 from lightpanda-io/custom_element_clone_take_2
Fix cloning custom element with constructor which attaches the element
2026-03-06 23:37:29 +08:00
Karl Seguin
de08a89e6b Merge pull request #1726 from lightpanda-io/fix_keyboard_event_leak
Release KeyboardEvent if it isn't used
2026-03-06 23:37:15 +08:00
Karl Seguin
dd42ef1920 Merge pull request #1727 from lightpanda-io/halt_tests_on_arena_leak
Halt tests (@panic) on ArenaLeak or double-free
2026-03-06 23:35:33 +08:00
Pierre Tachoire
dd192be689 Merge pull request #1730 from lightpanda-io/wpt-concurrency
wpt: increase concurrency
2026-03-06 16:26:30 +01:00
Pierre Tachoire
52250ed10e wpt: increase concurrency 2026-03-06 15:59:28 +01:00
Karl Seguin
4a26cd8d68 Halt tests (@panic) on ArenaLeak or double-free
These are too hard to see during a full test run.
2026-03-06 20:41:57 +08:00
Karl Seguin
2ca972c3c8 Merge pull request #1731 from lightpanda-io/revert-rs-arena
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Revert pool arena usage w/ ReadableStream
2026-03-06 19:28:44 +08:00
Karl Seguin
74c0d55a6c Fix cloning custom element with constructor which attaches the element
This is a follow up to ca0f77bdee that applies
the same fix to all places where cloneNode is used and then the cloned element
is inserted. A helper was added more of a means of documentation than to DRY
up the code.
2026-03-06 17:38:16 +08:00
Pierre Tachoire
3271e1464e Revert pool arena usage w/ ReadableStream
Revert "update ref counting for new ReadableStream usages"
This reverts commit c64500dd85.

Revert "add reference counting for ReadableStream"
This reverts commit 812ad3f49e.

Revert "use a pool arena with ReadableStream"
This reverts commit 8e8a1a7541.
2026-03-06 10:21:36 +01:00
Karl Seguin
cabd62b48f Optimize about:blank loading in general and for frames specifically
Instead of going through the parser, just create / append the 3 elements.

iframe without a src automatically loads about:blank. This is important, because
the following is valid:

```js
const iframe = document.createElement('iframe');
document.documentElement.appendChild(iframe);

// documentElement should exist and should be the HTML of the blank page.
iframe.contentDocument.documentElement.appendChild(...);
```

Builds on top of https://github.com/lightpanda-io/browser/pull/1720
2026-03-06 17:15:43 +08:00
Karl Seguin
58c2355c8b Merge pull request #1725 from egrs/fix-mcp-test-hang-aarch64
initialize all App fields after allocator.create
2026-03-06 17:11:40 +08:00
Karl Seguin
bfe2065b9f Add target-aware(ish) navigation
All inner navigations have an originator and a target. Consider this:

```js
aframe.contentDocument.querySelector('#link').click();
```

The originator is the context in which this JavaScript is called, the target is
`aframe. Importantly, relative URLs are resolved based on the originator. This
commit adds that.

This is only a first step, there are other aspect to this relationship that
isn't addressed yet, like differences in behavior if the originator and target
are on different origins, and specific target targetting via the things like
the "target" attribute. What this commit does though is address the normal /
common case.

It builds on top of https://github.com/lightpanda-io/browser/pull/1720
2026-03-06 16:57:28 +08:00
egrs
9332b1355e initialize all App fields after allocator.create
Same pattern as 3dea554e (mcp/Server.zig): allocator.create returns
undefined memory, and struct field defaults (shutdown: bool = false)
are not applied when fields are set individually. Use self.* = .{...}
to ensure all fields including defaults are initialized.
2026-03-06 09:37:55 +01:00
Adrià Arrufat
45705a3e29 webapi: move tag category logic to Tag enum 2026-03-06 16:34:23 +09:00
Adrià Arrufat
e0f0b9f210 SemanticTree: use AXRole enum for interactive role check 2026-03-06 16:26:08 +09:00
Adrià Arrufat
f2832447d4 SemanticTree: optimize tag and role filtering
* Refactored tag ignoring logic to use the el.getTag() enum switch
  instead of string comparisons, improving performance and safety.
* Replaced string comparisons for interactive roles with
  std.StaticStringMap.
* Renamed internal dumpNode method to dump for brevity.
2026-03-06 16:12:57 +09:00
Adrià Arrufat
471ba5baf6 String: refactor isAllWhitespace into String 2026-03-06 15:52:53 +09:00
Adrià Arrufat
248851701f Refactor: move SemanticTree to core and expose via MCP tools 2026-03-06 15:44:03 +09:00
Adrià Arrufat
0f46277b1f CDP: implement LP.getSemanticTree for native semantic DOM extraction 2026-03-06 15:29:32 +09:00
Karl Seguin
679e703754 Release KeyboardEvent if it isn't used 2026-03-06 09:12:58 +08:00
Karl Seguin
768c3a533b Simplify navigation logic.
Must of the complexity in the previous commit had to do with the fact that
about:blank is processed synchronously, meaning that we could process a
scheduled navigation -> page.navigate -> scheduled navigation:

```
let iframe = document.createElement('iframe');
iframe.addEventListner('load', () => {
  iframe.src = "about:blank";
});
```

This is an infinite loop which is going to be a problem no mater what, but there
are different degrees of problems this can cause, e.g. looping forever vs use-
after-free or other undefined behavior.

The new approach does 2 passes through scheduled navigations, first processing
"asynchronous" navigation (anything not "about:blank"), then processing
synchronous navigation ("about:blank"). The main advantage is that if the
synchronous navigation causes more synchronous navigation, it won't be
processed until the next tick. PLUS, we can detect about:blank that loads
about:blank and stop it (which might not be to spec, but seems right to do
nonetheless). This 2-pass approach removes the need for a couple of checks and
makes everything else simpler.
2026-03-05 17:06:23 +08:00
Karl Seguin
9c7ecf221e Improve frame sub-navigation
This makes frame sub-navigation "work" for all page navigations (click, form
submit, location.top...) as well as setting the iframe.src.

Fixes at least 2 WPT crashes.

BUT, the implementation still isn't 100% correct, with two known issues:

1. Navigation currently happens in the context where it's called, not the
   context of the frame. So if Page1 accesses Frame1 and causes it to navigate,
   e.g. f1.contentDocument.querySelector('#link').click(), it's Page1 that will
   be navigated, since the JS is being executed in the Page1 context.
   This should be relatively easy to fix.

2. There are particularly complicated cases in WPT where a frame is navigated
   inside of its own load, creating an endless loop. There's some partial
   support for this as-is, but it doesn't work correctly and it currently is
   defensive and likely will not continue to navigate. This is particularly true
   when sub-navigation is done to about:blank within the frame's on load event.
   (Which is probably not a real concern, but an issue for some WPT tests)

Although it shares a lot with the original navigation code, there are many more
edge cases here, possibly due to being developed along side WPT tests. The
source of most of the complexity is the synchronous handling of "about:blank"
in page.navigate, which can result in a scheduled navigation synchronously
causing more scheduled navigation. (Specifically because
`self.documentIsComplete();` is called from page.navigate in that case). It
might be worth seeing if something can be done about that, to simplify this new
code (removing the double queue, removing the flag, simplifying pre-existing
schedule checks ,...)
2026-03-05 15:09:39 +08:00
303 changed files with 18493 additions and 5590 deletions

View File

@@ -13,7 +13,7 @@ inputs:
zig-v8:
description: 'zig v8 version to install'
required: false
default: 'v0.3.1'
default: 'v0.3.7'
v8:
description: 'v8 version to install'
required: false
@@ -46,7 +46,7 @@ runs:
- name: Cache v8
id: cache-v8
uses: actions/cache@v4
uses: actions/cache@v5
env:
cache-name: cache-v8
with:

View File

@@ -20,19 +20,17 @@ jobs:
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
- name: zig build release
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: lightpanda-build-release
path: |
@@ -47,7 +45,7 @@ jobs:
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
repository: 'lightpanda-io/demo'
fetch-depth: 0
@@ -55,7 +53,7 @@ jobs:
- run: npm install
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
@@ -63,6 +61,6 @@ jobs:
- name: run end to end integration tests
run: |
./lightpanda serve --log_level error & echo $! > LPD.pid
./lightpanda serve --log-level error & echo $! > LPD.pid
go run integration/main.go
kill `cat LPD.pid`

View File

@@ -9,15 +9,13 @@ env:
on:
push:
branches:
- main
branches: [main]
paths:
- "build.zig"
- "src/**/*.zig"
- "src/*.zig"
- "vendor/zig-js-runtime"
- ".github/**"
- "vendor/**"
- "src/**"
- "build.zig"
- "build.zig.zon"
pull_request:
# By default GH trigger on types opened, synchronize and reopened.
@@ -29,12 +27,10 @@ on:
paths:
- ".github/**"
- "src/**"
- "build.zig"
- "src/**/*.zig"
- "src/*.zig"
- "vendor/**"
- ".github/**"
- "vendor/**"
- "build.zig.zon"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
@@ -52,16 +48,14 @@ jobs:
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
- name: zig build release
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: lightpanda-build-release
path: |
@@ -76,7 +70,7 @@ jobs:
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
repository: 'lightpanda-io/demo'
fetch-depth: 0
@@ -84,7 +78,7 @@ jobs:
- run: npm install
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
@@ -104,7 +98,7 @@ jobs:
- name: run end to end tests through proxy
run: |
./proxy/proxy & echo $! > PROXY.id
./lightpanda serve --http_proxy 'http://127.0.0.1:3000' & echo $! > LPD.pid
./lightpanda serve --http-proxy 'http://127.0.0.1:3000' & echo $! > LPD.pid
go run runner/main.go
kill `cat LPD.pid` `cat PROXY.id`
@@ -117,6 +111,110 @@ jobs:
BASE_URL=https://demo-browser.lightpanda.io/ node playwright/proxy_auth.js
kill `cat LPD.pid` `cat PROXY.id`
# e2e tests w/ web-bot-auth configuration on.
wba-demo-scripts:
name: wba-demo-scripts
needs: zig-build-release
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v6
with:
repository: 'lightpanda-io/demo'
fetch-depth: 0
- run: npm install
- name: download artifact
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
- run: chmod a+x ./lightpanda
- run: echo "${{ secrets.WBA_PRIVATE_KEY_PEM }}" > private_key.pem
- name: run end to end tests
run: |
./lightpanda serve \
--web-bot-auth-key-file private_key.pem \
--web-bot-auth-keyid ${{ vars.WBA_KEY_ID }} \
--web-bot-auth-domain ${{ vars.WBA_DOMAIN }} \
& echo $! > LPD.pid
go run runner/main.go
kill `cat LPD.pid`
- name: build proxy
run: |
cd proxy
go build
- name: run end to end tests through proxy
run: |
./proxy/proxy & echo $! > PROXY.id
./lightpanda serve \
--web-bot-auth-key-file private_key.pem \
--web-bot-auth-keyid ${{ vars.WBA_KEY_ID }} \
--web-bot-auth-domain ${{ vars.WBA_DOMAIN }} \
--http-proxy 'http://127.0.0.1:3000' \
& echo $! > LPD.pid
go run runner/main.go
kill `cat LPD.pid` `cat PROXY.id`
- name: run request interception through proxy
run: |
export PROXY_USERNAME=username PROXY_PASSWORD=password
./proxy/proxy & echo $! > PROXY.id
./lightpanda serve & echo $! > LPD.pid
URL=https://demo-browser.lightpanda.io/campfire-commerce/ node puppeteer/proxy_auth.js
BASE_URL=https://demo-browser.lightpanda.io/ node playwright/proxy_auth.js
kill `cat LPD.pid` `cat PROXY.id`
wba-test:
name: wba-test
needs: zig-build-release
runs-on: ubuntu-latest
timeout-minutes: 5
# Don't execute on PR
if: github.event_name != 'pull_request'
steps:
- uses: actions/checkout@v6
with:
repository: 'lightpanda-io/demo'
fetch-depth: 0
- name: download artifact
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
- run: chmod a+x ./lightpanda
# force a wakup of the auth server before requesting it w/ the test itself
- run: curl https://${{ vars.WBA_DOMAIN }}
- name: run wba test
shell: bash
run: |
node webbotauth/validator.js &
VALIDATOR_PID=$!
sleep 5
exec 3<<< "${{ secrets.WBA_PRIVATE_KEY_PEM }}"
./lightpanda fetch --dump http://127.0.0.1:8989/ \
--web-bot-auth-key-file /proc/self/fd/3 \
--web-bot-auth-keyid ${{ vars.WBA_KEY_ID }} \
--web-bot-auth-domain ${{ vars.WBA_DOMAIN }}
wait $VALIDATOR_PID
exec 3>&-
cdp-and-hyperfine-bench:
name: cdp-and-hyperfine-bench
needs: zig-build-release
@@ -125,7 +223,6 @@ jobs:
MAX_VmHWM: 28000 # 28MB (KB)
MAX_CG_PEAK: 8000 # 8MB (KB)
MAX_AVG_DURATION: 17
LIGHTPANDA_DISABLE_TELEMETRY: true
# How to give cgroups access to the user actions-runner on the host:
# $ sudo apt install cgroup-tools
@@ -140,7 +237,7 @@ jobs:
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
repository: 'lightpanda-io/demo'
fetch-depth: 0
@@ -148,7 +245,7 @@ jobs:
- run: npm install
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
@@ -234,7 +331,7 @@ jobs:
echo "${{github.sha}}" > commit.txt
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: bench-results
path: |
@@ -262,7 +359,7 @@ jobs:
steps:
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: bench-results
@@ -280,7 +377,7 @@ jobs:
steps:
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release

View File

@@ -5,7 +5,9 @@ env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.NIGHTLY_BUILD_AWS_SECRET_ACCESS_KEY }}
AWS_BUCKET: ${{ vars.NIGHTLY_BUILD_AWS_BUCKET }}
AWS_REGION: ${{ vars.NIGHTLY_BUILD_AWS_REGION }}
RELEASE: ${{ github.ref_type == 'tag' && github.ref_name || 'nightly' }}
VERSION_FLAG: ${{ github.ref_type == 'tag' && format('-Dversion_string={0}', github.ref_name) || format('-Dpre_version={0}', 'nightly') }}
on:
push:
@@ -33,8 +35,6 @@ jobs:
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
@@ -45,7 +45,7 @@ jobs:
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin
- name: zig build
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 ${{ env.VERSION_FLAG }}
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}
@@ -72,11 +72,9 @@ jobs:
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
@@ -87,7 +85,7 @@ jobs:
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin
- name: zig build
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=generic -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=generic ${{ env.VERSION_FLAG }}
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}
@@ -116,11 +114,9 @@ jobs:
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
@@ -131,7 +127,7 @@ jobs:
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin
- name: zig build
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast ${{ env.VERSION_FLAG }}
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}
@@ -158,11 +154,9 @@ jobs:
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
@@ -173,7 +167,7 @@ jobs:
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin
- name: zig build
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast ${{ env.VERSION_FLAG }}
- name: Rename binary
run: mv zig-out/bin/lightpanda lightpanda-${{ env.ARCH }}-${{ env.OS }}

View File

@@ -5,11 +5,12 @@ env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.LPD_PERF_AWS_SECRET_ACCESS_KEY }}
AWS_BUCKET: ${{ vars.LPD_PERF_AWS_BUCKET }}
AWS_REGION: ${{ vars.LPD_PERF_AWS_REGION }}
AWS_CF_DISTRIBUTION: ${{ vars.AWS_CF_DISTRIBUTION }}
LIGHTPANDA_DISABLE_TELEMETRY: true
on:
schedule:
- cron: "23 2 * * *"
- cron: "21 2 * * *"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
@@ -18,23 +19,31 @@ jobs:
wpt-build-release:
name: zig build release
runs-on: ubuntu-latest
timeout-minutes: 15
env:
ARCH: aarch64
OS: linux
runs-on: ubuntu-24.04-arm
timeout-minutes: 20
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
os: ${{env.OS}}
arch: ${{env.ARCH}}
- name: v8 snapshot
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast snapshot_creator -- src/snapshot.bin
- name: zig build release
run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64 -Dgit_commit=$(git rev-parse --short ${{ github.sha }})
run: zig build -Dsnapshot_path=../../snapshot.bin -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=generic
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: lightpanda-build-release
path: |
@@ -44,7 +53,7 @@ jobs:
wpt-build-runner:
name: build wpt runner
runs-on: ubuntu-latest
runs-on: ubuntu-24.04-arm
timeout-minutes: 15
steps:
@@ -58,7 +67,7 @@ jobs:
CGO_ENABLED=0 go build
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: wptrunner
path: |
@@ -72,8 +81,8 @@ jobs:
- wpt-build-runner
# use a self host runner.
runs-on: lpd-bench-hetzner
timeout-minutes: 120
runs-on: lpd-wpt-aws
timeout-minutes: 600
steps:
- uses: actions/checkout@v6
@@ -90,14 +99,14 @@ jobs:
run: ./wpt manifest
- name: download lightpanda release
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: lightpanda-build-release
- run: chmod a+x ./lightpanda
- name: download wptrunner
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: wptrunner
@@ -106,8 +115,8 @@ jobs:
- name: run test with json output
run: |
./wpt serve 2> /dev/null & echo $! > WPT.pid
sleep 10s
./wptrunner -lpd-path ./lightpanda -json -concurrency 3 > wpt.json
sleep 20s
./wptrunner -lpd-path ./lightpanda -json -concurrency 5 -pool 5 --mem-limit 400 > wpt.json
kill `cat WPT.pid`
- name: write commit
@@ -115,7 +124,7 @@ jobs:
echo "${{github.sha}}" > commit.txt
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: wpt-results
path: |
@@ -138,7 +147,7 @@ jobs:
steps:
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: wpt-results

View File

@@ -1,60 +0,0 @@
name: zig-fmt
on:
pull_request:
# By default GH trigger on types opened, synchronize and reopened.
# see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# Since we skip the job when the PR is in draft state, we want to force CI
# running when the PR is marked ready_for_review w/o other change.
# see https://github.com/orgs/community/discussions/25722#discussioncomment-3248917
types: [opened, synchronize, reopened, ready_for_review]
paths:
- ".github/**"
- "build.zig"
- "src/**/*.zig"
- "src/*.zig"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
zig-fmt:
name: zig fmt
# Don't run the CI with draft PR.
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
# Zig version used from the `minimum_zig_version` field in build.zig.zon
- uses: mlugg/setup-zig@v2
- name: Run zig fmt
id: fmt
run: |
zig fmt --check ./*.zig ./**/*.zig 2> zig-fmt.err > zig-fmt.err2 || echo "Failed"
delimiter="$(openssl rand -hex 8)"
echo "zig_fmt_errs<<${delimiter}" >> "${GITHUB_OUTPUT}"
if [ -s zig-fmt.err ]; then
echo "// The following errors occurred:" >> "${GITHUB_OUTPUT}"
cat zig-fmt.err >> "${GITHUB_OUTPUT}"
fi
if [ -s zig-fmt.err2 ]; then
echo "// The following files were not formatted:" >> "${GITHUB_OUTPUT}"
cat zig-fmt.err2 >> "${GITHUB_OUTPUT}"
fi
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
- name: Fail the job
if: steps.fmt.outputs.zig_fmt_errs != ''
run: exit 1

View File

@@ -5,19 +5,18 @@ env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.LPD_PERF_AWS_SECRET_ACCESS_KEY }}
AWS_BUCKET: ${{ vars.LPD_PERF_AWS_BUCKET }}
AWS_REGION: ${{ vars.LPD_PERF_AWS_REGION }}
LIGHTPANDA_DISABLE_TELEMETRY: true
on:
push:
branches:
- main
branches: [main]
paths:
- "build.zig"
- "src/**"
- "vendor/zig-js-runtime"
- ".github/**"
- "vendor/**"
pull_request:
- "src/**"
- "build.zig"
- "build.zig.zon"
pull_request:
# By default GH trigger on types opened, synchronize and reopened.
# see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# Since we skip the job when the PR is in draft state, we want to force CI
@@ -27,28 +26,63 @@ on:
paths:
- ".github/**"
- "src/**"
- "build.zig"
- "src/**/*.zig"
- "src/*.zig"
- "vendor/**"
- ".github/**"
- "vendor/**"
- "build.zig.zon"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
zig-test-debug:
name: zig test using v8 in debug mode
timeout-minutes: 15
zig-fmt:
name: zig fmt
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
# Zig version used from the `minimum_zig_version` field in build.zig.zon
- uses: mlugg/setup-zig@v2
- name: Run zig fmt
id: fmt
run: |
zig fmt --check ./*.zig ./**/*.zig 2> zig-fmt.err > zig-fmt.err2 || echo "Failed"
delimiter="$(openssl rand -hex 8)"
echo "zig_fmt_errs<<${delimiter}" >> "${GITHUB_OUTPUT}"
if [ -s zig-fmt.err ]; then
echo "// The following errors occurred:" >> "${GITHUB_OUTPUT}"
cat zig-fmt.err >> "${GITHUB_OUTPUT}"
fi
if [ -s zig-fmt.err2 ]; then
echo "// The following files were not formatted:" >> "${GITHUB_OUTPUT}"
cat zig-fmt.err2 >> "${GITHUB_OUTPUT}"
fi
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
- name: Fail the job
if: steps.fmt.outputs.zig_fmt_errs != ''
run: exit 1
zig-test-debug:
name: zig test using v8 in debug mode
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
with:
@@ -57,21 +91,18 @@ jobs:
- name: zig build test
run: zig build -Dprebuilt_v8_path=v8/libc_v8_debug.a -Dtsan=true test
zig-test:
zig-test-release:
name: zig test
timeout-minutes: 15
# Don't run the CI with draft PR.
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
# fetch submodules recusively, to get zig-js-runtime submodules also.
submodules: recursive
- uses: ./.github/actions/install
@@ -83,7 +114,7 @@ jobs:
echo "${{github.sha}}" > commit.txt
- name: upload artifact
uses: actions/upload-artifact@v4
uses: actions/upload-artifact@v7
with:
name: bench-results
path: |
@@ -93,14 +124,13 @@ jobs:
bench-fmt:
name: perf-fmt
needs: zig-test
# Don't execute on PR
if: github.event_name != 'pull_request'
needs: zig-test-release
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event_name != 'pull_request'
container:
image: ghcr.io/lightpanda-io/perf-fmt:latest
credentials:
@@ -109,7 +139,7 @@ jobs:
steps:
- name: download artifact
uses: actions/download-artifact@v4
uses: actions/download-artifact@v8
with:
name: bench-results

View File

@@ -3,7 +3,7 @@ FROM debian:stable-slim
ARG MINISIG=0.12
ARG ZIG_MINISIG=RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U
ARG V8=14.0.365.4
ARG ZIG_V8=v0.3.1
ARG ZIG_V8=v0.3.7
ARG TARGETPLATFORM
RUN apt-get update -yq && \
@@ -53,8 +53,7 @@ RUN zig build -Doptimize=ReleaseFast \
# build release
RUN zig build -Doptimize=ReleaseFast \
-Dsnapshot_path=../../snapshot.bin \
-Dprebuilt_v8_path=v8/libc_v8.a \
-Dgit_commit=$(git rev-parse --short HEAD)
-Dprebuilt_v8_path=v8/libc_v8.a
FROM debian:stable-slim
@@ -75,4 +74,4 @@ EXPOSE 9222/tcp
# Using "tini" as PID1 ensures that signals work as expected, so e.g. "docker stop" will not hang.
# (See https://github.com/krallin/tini#why-tini).
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/bin/lightpanda", "serve", "--host", "0.0.0.0", "--port", "9222", "--log_level", "info"]
CMD ["/bin/lightpanda", "serve", "--host", "0.0.0.0", "--port", "9222", "--log-level", "info"]

View File

@@ -4,11 +4,3 @@ License names used in this document are as per [SPDX License
List](https://spdx.org/licenses/).
The default license for this project is [AGPL-3.0-only](LICENSE).
The following directories and their subdirectories are licensed under their
original upstream licenses:
```
vendor/
tests/wpt/
```

View File

@@ -47,7 +47,7 @@ help:
# $(ZIG) commands
# ------------
.PHONY: build build-v8-snapshot build-dev run run-release shell test bench data end2end
.PHONY: build build-v8-snapshot build-dev run run-release test bench data end2end
## Build v8 snapshot
build-v8-snapshot:
@@ -58,13 +58,13 @@ build-v8-snapshot:
## Build in release-fast mode
build: build-v8-snapshot
@printf "\033[36mBuilding (release fast)...\033[0m\n"
@$(ZIG) build -Doptimize=ReleaseFast -Dsnapshot_path=../../snapshot.bin -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\033[33mBuild ERROR\033[0m\n"; exit 1;)
@$(ZIG) build -Doptimize=ReleaseFast -Dsnapshot_path=../../snapshot.bin || (printf "\033[33mBuild ERROR\033[0m\n"; exit 1;)
@printf "\033[33mBuild OK\033[0m\n"
## Build in debug mode
build-dev:
@printf "\033[36mBuilding (debug)...\033[0m\n"
@$(ZIG) build -Dgit_commit=$$(git rev-parse --short HEAD) || (printf "\033[33mBuild ERROR\033[0m\n"; exit 1;)
@$(ZIG) build || (printf "\033[33mBuild ERROR\033[0m\n"; exit 1;)
@printf "\033[33mBuild OK\033[0m\n"
## Run the server in release mode
@@ -77,11 +77,6 @@ run-debug: build-dev
@printf "\033[36mRunning...\033[0m\n"
@./zig-out/bin/lightpanda || (printf "\033[33mRun ERROR\033[0m\n"; exit 1;)
## Run a JS shell in debug mode
shell:
@printf "\033[36mBuilding shell...\033[0m\n"
@$(ZIG) build shell || (printf "\033[33mBuild ERROR\033[0m\n"; exit 1;)
## Test - `grep` is used to filter out the huge compile command on build
ifeq ($(OS), macos)
test:
@@ -106,4 +101,3 @@ install: build
data:
cd src/data && go run public_suffix_list_gen.go > public_suffix_list.zig

View File

@@ -1,18 +1,32 @@
<p align="center">
<a href="https://lightpanda.io"><img src="https://cdn.lightpanda.io/assets/images/logo/lpd-logo.png" alt="Logo" height=170></a>
</p>
<h1 align="center">Lightpanda Browser</h1>
<p align="center">
<strong>The headless browser built from scratch for AI agents and automation.</strong><br>
Not a Chromium fork. Not a WebKit patch. A new browser, written in Zig.
</p>
<p align="center"><a href="https://lightpanda.io/">lightpanda.io</a></p>
</div>
<div align="center">
[![License](https://img.shields.io/github/license/lightpanda-io/browser)](https://github.com/lightpanda-io/browser/blob/main/LICENSE)
[![Twitter Follow](https://img.shields.io/twitter/follow/lightpanda_io)](https://twitter.com/lightpanda_io)
[![GitHub stars](https://img.shields.io/github/stars/lightpanda-io/browser)](https://github.com/lightpanda-io/browser)
[![Discord](https://img.shields.io/discord/1391984864894521354?style=flat-square&label=discord)](https://discord.gg/K63XeymfB5)
</div>
<div align="center">
[<img width="350px" src="https://cdn.lightpanda.io/assets/images/github/execution-time-v2.svg">
](https://github.com/lightpanda-io/demo)
&emsp;
[<img width="350px" src="https://cdn.lightpanda.io/assets/images/github/memory-frame-v2.svg">
](https://github.com/lightpanda-io/demo)
</div>
_chromedp requesting 933 real web pages over the network on a AWS EC2 m5.large instance.
See [benchmark details](https://github.com/lightpanda-io/demo/blob/main/BENCHMARKS.md#crawler-benchmark)._
Lightpanda is the open-source browser made for headless usage:
@@ -26,16 +40,6 @@ Fast web automation for AI agents, LLM training, scraping and testing:
- Exceptionally fast execution (11x faster than Chrome)
- Instant startup
[<img width="350px" src="https://cdn.lightpanda.io/assets/images/github/execution-time.svg">
](https://github.com/lightpanda-io/demo)
&emsp;
[<img width="350px" src="https://cdn.lightpanda.io/assets/images/github/memory-frame.svg">
](https://github.com/lightpanda-io/demo)
</div>
_Puppeteer requesting 100 pages from a local website on a AWS EC2 m5.large instance.
See [benchmark details](https://github.com/lightpanda-io/demo)._
[^1]: **Playwright support disclaimer:**
Due to the nature of Playwright, a script that works with the current version of the browser may not function correctly with a future version. Playwright uses an intermediate JavaScript layer that selects an execution strategy based on the browser's available features. If Lightpanda adds a new [Web API](https://developer.mozilla.org/en-US/docs/Web/API), Playwright may choose to execute different code for the same script. This new code path could attempt to use features that are not yet implemented. Lightpanda makes an effort to add compatibility tests, but we can't cover all scenarios. If you encounter an issue, please create a [GitHub issue](https://github.com/lightpanda-io/browser/issues) and include the last known working version of the script.
@@ -78,7 +82,7 @@ docker run -d --name lightpanda -p 9222:9222 lightpanda/browser:nightly
### Dump a URL
```console
./lightpanda fetch --obey_robots --log_format pretty --log_level info https://demo-browser.lightpanda.io/campfire-commerce/
./lightpanda fetch --obey-robots --log-format pretty --log-level info https://demo-browser.lightpanda.io/campfire-commerce/
```
```console
INFO telemetry : telemetry status . . . . . . . . . . . . . [+0ms]
@@ -113,7 +117,7 @@ INFO http : request complete . . . . . . . . . . . . . . . . [+141ms]
### Start a CDP server
```console
./lightpanda serve --obey_robots --log_format pretty --log_level info --host 127.0.0.1 --port 9222
./lightpanda serve --obey-robots --log-format pretty --log-level info --host 127.0.0.1 --port 9222
```
```console
INFO telemetry : telemetry status . . . . . . . . . . . . . [+0ms]
@@ -182,12 +186,10 @@ Here are the key features we have implemented:
- [x] Custom HTTP headers
- [x] Proxy support
- [x] Network interception
- [x] Respect `robots.txt` with option `--obey_robots`
- [x] Respect `robots.txt` with option `--obey-robots`
NOTE: There are hundreds of Web APIs. Developing a browser (even just for headless mode) is a huge task. Coverage will increase over time.
You can also follow the progress of our Javascript support in our dedicated [zig-js-runtime](https://github.com/lightpanda-io/zig-js-runtime#development) project.
## Build from sources
### Prerequisites
@@ -196,10 +198,10 @@ Lightpanda is written with [Zig](https://ziglang.org/) `0.15.2`. You have to
install it with the right version in order to build the project.
Lightpanda also depends on
[zig-js-runtime](https://github.com/lightpanda-io/zig-js-runtime/) (with v8),
[v8](https://chromium.googlesource.com/v8/v8.git),
[Libcurl](https://curl.se/libcurl/) and [html5ever](https://github.com/servo/html5ever).
To be able to build the v8 engine for zig-js-runtime, you have to install some libs:
To be able to build the v8 engine, you have to install some libs:
For **Debian/Ubuntu based Linux**:
@@ -315,7 +317,7 @@ First start the WPT's HTTP server from your `wpt/` clone dir.
Run a Lightpanda browser
```
zig build run -- --insecure_disable_tls_host_verification
zig build run -- --insecure-disable-tls-host-verification
```
Then you can start the wptrunner from the Demo's clone dir:

131
build.zig
View File

@@ -17,22 +17,37 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const builtin = @import("builtin");
const Build = std.Build;
const lightpanda_version = std.SemanticVersion.parse(@import("build.zig.zon").version) catch unreachable;
const min_zig_version = std.SemanticVersion.parse(@import("build.zig.zon").minimum_zig_version) catch unreachable;
const Build = blk: {
if (builtin.zig_version.order(min_zig_version) == .lt) {
const message = std.fmt.comptimePrint(
\\Zig version is too old:
\\ current Zig version: {f}
\\ minimum Zig version: {f}
, .{ builtin.zig_version, min_zig_version });
@compileError(message);
} else {
break :blk std.Build;
}
};
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const manifest = Manifest.init(b);
const git_commit = b.option([]const u8, "git_commit", "Current git commit");
const prebuilt_v8_path = b.option([]const u8, "prebuilt_v8_path", "Path to prebuilt libc_v8.a");
const snapshot_path = b.option([]const u8, "snapshot_path", "Path to v8 snapshot");
const version = resolveVersion(b);
var stdout = std.fs.File.stdout().writer(&.{});
try stdout.interface.print("Lightpanda {f}\n", .{version});
var opts = b.addOptions();
opts.addOption([]const u8, "version", manifest.version);
opts.addOption([]const u8, "git_commit", git_commit orelse "dev");
opts.addOption([]const u8, "version", b.fmt("{f}", .{version}));
opts.addOption(?[]const u8, "snapshot_path", snapshot_path);
const enable_tsan = b.option(bool, "tsan", "Enable Thread Sanitizer") orelse false;
@@ -52,8 +67,19 @@ pub fn build(b: *Build) !void {
mod.addImport("lightpanda", mod); // allow circular "lightpanda" import
mod.addImport("build_config", opts.createModule());
// Format check
const fmt_step = b.step("fmt", "Check code formatting");
const fmt = b.addFmt(.{
.paths = &.{ "src", "build.zig", "build.zig.zon" },
.check = true,
});
fmt_step.dependOn(&fmt.step);
// Set default behavior
b.default_step.dependOn(fmt_step);
try linkV8(b, mod, enable_asan, enable_tsan, prebuilt_v8_path);
try linkCurl(b, mod);
try linkCurl(b, mod, enable_tsan);
try linkHtml5Ever(b, mod);
break :blk mod;
@@ -83,6 +109,11 @@ pub fn build(b: *Build) !void {
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const version_info_step = b.step("version", "Print the resolved version information");
const version_info_run = b.addRunArtifact(exe);
version_info_run.addArg("version");
version_info_step.dependOn(&version_info_run.step);
}
{
@@ -189,19 +220,19 @@ fn linkHtml5Ever(b: *Build, mod: *Build.Module) !void {
mod.addObjectFile(obj);
}
fn linkCurl(b: *Build, mod: *Build.Module) !void {
fn linkCurl(b: *Build, mod: *Build.Module, is_tsan: bool) !void {
const target = mod.resolved_target.?;
const curl = buildCurl(b, target, mod.optimize.?);
const curl = buildCurl(b, target, mod.optimize.?, is_tsan);
mod.linkLibrary(curl);
const zlib = buildZlib(b, target, mod.optimize.?);
const zlib = buildZlib(b, target, mod.optimize.?, is_tsan);
curl.root_module.linkLibrary(zlib);
const brotli = buildBrotli(b, target, mod.optimize.?);
const brotli = buildBrotli(b, target, mod.optimize.?, is_tsan);
for (brotli) |lib| curl.root_module.linkLibrary(lib);
const nghttp2 = buildNghttp2(b, target, mod.optimize.?);
const nghttp2 = buildNghttp2(b, target, mod.optimize.?, is_tsan);
curl.root_module.linkLibrary(nghttp2);
const boringssl = buildBoringSsl(b, target, mod.optimize.?);
@@ -218,13 +249,14 @@ fn linkCurl(b: *Build, mod: *Build.Module) !void {
}
}
fn buildZlib(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Step.Compile {
fn buildZlib(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, is_tsan: bool) *Build.Step.Compile {
const dep = b.dependency("zlib", .{});
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.sanitize_thread = is_tsan,
});
const lib = b.addLibrary(.{ .name = "z", .root_module = mod });
@@ -249,13 +281,14 @@ fn buildZlib(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Opti
return lib;
}
fn buildBrotli(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) [3]*Build.Step.Compile {
fn buildBrotli(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, is_tsan: bool) [3]*Build.Step.Compile {
const dep = b.dependency("brotli", .{});
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.sanitize_thread = is_tsan,
});
mod.addIncludePath(dep.path("c/include"));
@@ -311,13 +344,14 @@ fn buildBoringSsl(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin
return .{ ssl, crypto };
}
fn buildNghttp2(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Step.Compile {
fn buildNghttp2(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, is_tsan: bool) *Build.Step.Compile {
const dep = b.dependency("nghttp2", .{});
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.sanitize_thread = is_tsan,
});
mod.addIncludePath(dep.path("lib/includes"));
@@ -362,6 +396,7 @@ fn buildCurl(
b: *Build,
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
is_tsan: bool,
) *Build.Step.Compile {
const dep = b.dependency("curl", .{});
@@ -369,6 +404,7 @@ fn buildCurl(
.target = target,
.optimize = optimize,
.link_libc = true,
.sanitize_thread = is_tsan,
});
mod.addIncludePath(dep.path("lib"));
mod.addIncludePath(dep.path("include"));
@@ -683,27 +719,50 @@ fn buildCurl(
return lib;
}
const Manifest = struct {
version: []const u8,
minimum_zig_version: []const u8,
fn init(b: *std.Build) Manifest {
const input = @embedFile("build.zig.zon");
var diagnostics: std.zon.parse.Diagnostics = .{};
defer diagnostics.deinit(b.allocator);
return std.zon.parse.fromSlice(Manifest, b.allocator, input, &diagnostics, .{
.free_on_error = true,
.ignore_unknown_fields = true,
}) catch |err| {
switch (err) {
error.OutOfMemory => @panic("OOM"),
error.ParseZon => {
std.debug.print("Parse diagnostics:\n{f}\n", .{diagnostics});
std.process.exit(1);
},
}
/// Returns `MAJOR.MINOR.PATCH-dev` when `git describe` fails.
fn resolveVersion(b: *std.Build) std.SemanticVersion {
const version_string = b.option([]const u8, "version_string", "Override the version of this build");
if (version_string) |semver_string| {
return std.SemanticVersion.parse(semver_string) catch |err| {
std.debug.panic("Expected -Dversion-string={s} to be a semantic version: {}", .{ semver_string, err });
};
}
const pre_version = b.option([]const u8, "pre_version", "Override the pre version of this build");
const pre = blk: {
if (pre_version) |pre| {
break :blk pre;
}
break :blk lightpanda_version.pre;
};
// If it's a stable release (no pre or build metadata in build.zig.zon), use it as is
if (pre == null and lightpanda_version.build == null) return lightpanda_version;
// For dev/nightly versions, calculate the commit count and hash
const git_hash_raw = runGit(b, &.{ "rev-parse", "--short", "HEAD" }) catch return lightpanda_version;
const commit_hash = std.mem.trim(u8, git_hash_raw, " \n\r");
const git_count_raw = runGit(b, &.{ "rev-list", "--count", "HEAD" }) catch return lightpanda_version;
const commit_count = std.mem.trim(u8, git_count_raw, " \n\r");
return .{
.major = lightpanda_version.major,
.minor = lightpanda_version.minor,
.patch = lightpanda_version.patch,
.pre = b.fmt("{s}.{s}", .{ pre.?, commit_count }),
.build = commit_hash,
};
}
/// Helper function to run git commands and return stdout
fn runGit(b: *std.Build, args: []const []const u8) ![]const u8 {
var code: u8 = undefined;
const dir = b.pathFromRoot(".");
var command: std.ArrayList([]const u8) = .empty;
defer command.deinit(b.allocator);
try command.appendSlice(b.allocator, &.{ "git", "-C", dir });
try command.appendSlice(b.allocator, args);
return b.runAllowFail(command.items, &code, .Ignore);
}

View File

@@ -1,13 +1,12 @@
.{
.name = .browser,
.version = "0.0.0",
.version = "1.0.0-dev",
.fingerprint = 0xda130f3af836cea0, // Changing this has security and trust implications.
.minimum_zig_version = "0.15.2",
.dependencies = .{
.v8 = .{
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/refs/tags/v0.3.1.tar.gz",
.hash = "v8-0.0.0-xddH64J7BAC81mkf6G9RbEJxS-W3TIRl5iFnShwbqCqy",
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/refs/tags/v0.3.7.tar.gz",
.hash = "v8-0.0.0-xddH67uBBAD95hWsPQz3Ni1PlZjdywtPXrGUAp8rSKco",
},
// .v8 = .{ .path = "../zig-v8-fork" },
.brotli = .{

View File

@@ -25,35 +25,38 @@ const Config = @import("Config.zig");
const Snapshot = @import("browser/js/Snapshot.zig");
const Platform = @import("browser/js/Platform.zig");
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const RobotStore = @import("browser/Robots.zig").RobotStore;
pub const Http = @import("http/Http.zig");
const Network = @import("network/Runtime.zig");
pub const ArenaPool = @import("ArenaPool.zig");
const App = @This();
http: Http,
network: Network,
config: *const Config,
platform: Platform,
snapshot: Snapshot,
telemetry: Telemetry,
allocator: Allocator,
arena_pool: ArenaPool,
robots: RobotStore,
app_dir_path: ?[]const u8,
shutdown: bool = false,
pub fn init(allocator: Allocator, config: *const Config) !*App {
const app = try allocator.create(App);
errdefer allocator.destroy(app);
app.config = config;
app.allocator = allocator;
app.* = .{
.config = config,
.allocator = allocator,
.network = undefined,
.platform = undefined,
.snapshot = undefined,
.app_dir_path = undefined,
.telemetry = undefined,
.arena_pool = undefined,
};
app.robots = RobotStore.init(allocator);
app.http = try Http.init(allocator, &app.robots, config);
errdefer app.http.deinit();
app.network = try Network.init(allocator, config);
errdefer app.network.deinit();
app.platform = try Platform.init();
errdefer app.platform.deinit();
@@ -64,7 +67,7 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
app.app_dir_path = getAndMakeAppDir(allocator);
app.telemetry = try Telemetry.init(app, config.mode);
errdefer app.telemetry.deinit();
errdefer app.telemetry.deinit(allocator);
app.arena_pool = ArenaPool.init(allocator, 512, 1024 * 16);
errdefer app.arena_pool.deinit();
@@ -72,19 +75,18 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
return app;
}
pub fn deinit(self: *App) void {
if (@atomicRmw(bool, &self.shutdown, .Xchg, true, .monotonic)) {
return;
pub fn shutdown(self: *const App) bool {
return self.network.shutdown.load(.acquire);
}
pub fn deinit(self: *App) void {
const allocator = self.allocator;
if (self.app_dir_path) |app_dir_path| {
allocator.free(app_dir_path);
self.app_dir_path = null;
}
self.telemetry.deinit();
self.robots.deinit();
self.http.deinit();
self.telemetry.deinit(allocator);
self.network.deinit();
self.snapshot.deinit();
self.platform.deinit();
self.arena_pool.deinit();

View File

@@ -17,12 +17,16 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const builtin = @import("builtin");
const log = @import("log.zig");
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const ArenaPool = @This();
const IS_DEBUG = builtin.mode == .Debug;
allocator: Allocator,
retain_bytes: usize,
free_list_len: u16 = 0,
@@ -30,10 +34,17 @@ free_list: ?*Entry = null,
free_list_max: u16,
entry_pool: std.heap.MemoryPool(Entry),
mutex: std.Thread.Mutex = .{},
// Debug mode: track acquire/release counts per debug name to detect leaks and double-frees
_leak_track: if (IS_DEBUG) std.StringHashMapUnmanaged(isize) else void = if (IS_DEBUG) .empty else {},
const Entry = struct {
next: ?*Entry,
arena: ArenaAllocator,
debug: if (IS_DEBUG) []const u8 else void = if (IS_DEBUG) "" else {},
};
pub const DebugInfo = struct {
debug: []const u8 = "",
};
pub fn init(allocator: Allocator, free_list_max: u16, retain_bytes: usize) ArenaPool {
@@ -42,10 +53,26 @@ pub fn init(allocator: Allocator, free_list_max: u16, retain_bytes: usize) Arena
.free_list_max = free_list_max,
.retain_bytes = retain_bytes,
.entry_pool = .init(allocator),
._leak_track = if (IS_DEBUG) .empty else {},
};
}
pub fn deinit(self: *ArenaPool) void {
if (IS_DEBUG) {
var has_leaks = false;
var it = self._leak_track.iterator();
while (it.next()) |kv| {
if (kv.value_ptr.* != 0) {
log.err(.bug, "ArenaPool leak", .{ .name = kv.key_ptr.*, .count = kv.value_ptr.* });
has_leaks = true;
}
}
if (has_leaks) {
@panic("ArenaPool: leaked arenas detected");
}
self._leak_track.deinit(self.allocator);
}
var entry = self.free_list;
while (entry) |e| {
entry = e.next;
@@ -54,13 +81,21 @@ pub fn deinit(self: *ArenaPool) void {
self.entry_pool.deinit();
}
pub fn acquire(self: *ArenaPool) !Allocator {
pub fn acquire(self: *ArenaPool, dbg: DebugInfo) !Allocator {
self.mutex.lock();
defer self.mutex.unlock();
if (self.free_list) |entry| {
self.free_list = entry.next;
self.free_list_len -= 1;
if (IS_DEBUG) {
entry.debug = dbg.debug;
const gop = try self._leak_track.getOrPut(self.allocator, dbg.debug);
if (!gop.found_existing) {
gop.value_ptr.* = 0;
}
gop.value_ptr.* += 1;
}
return entry.arena.allocator();
}
@@ -68,8 +103,16 @@ pub fn acquire(self: *ArenaPool) !Allocator {
entry.* = .{
.next = null,
.arena = ArenaAllocator.init(self.allocator),
.debug = if (IS_DEBUG) dbg.debug else {},
};
if (IS_DEBUG) {
const gop = try self._leak_track.getOrPut(self.allocator, dbg.debug);
if (!gop.found_existing) {
gop.value_ptr.* = 0;
}
gop.value_ptr.* += 1;
}
return entry.arena.allocator();
}
@@ -83,6 +126,19 @@ pub fn release(self: *ArenaPool, allocator: Allocator) void {
self.mutex.lock();
defer self.mutex.unlock();
if (IS_DEBUG) {
if (self._leak_track.getPtr(entry.debug)) |count| {
count.* -= 1;
if (count.* < 0) {
log.err(.bug, "ArenaPool double-free", .{ .name = entry.debug });
@panic("ArenaPool: double-free detected");
}
} else {
log.err(.bug, "ArenaPool release unknown", .{ .name = entry.debug });
@panic("ArenaPool: release of untracked arena");
}
}
const free_list_len = self.free_list_len;
if (free_list_len == self.free_list_max) {
arena.deinit();
@@ -100,13 +156,18 @@ pub fn reset(_: *const ArenaPool, allocator: Allocator, retain: usize) void {
_ = arena.reset(.{ .retain_with_limit = retain });
}
pub fn resetRetain(_: *const ArenaPool, allocator: Allocator) void {
const arena: *std.heap.ArenaAllocator = @ptrCast(@alignCast(allocator.ptr));
_ = arena.reset(.retain_capacity);
}
const testing = std.testing;
test "arena pool - basic acquire and use" {
var pool = ArenaPool.init(testing.allocator, 512, 1024 * 16);
defer pool.deinit();
const alloc = try pool.acquire();
const alloc = try pool.acquire(.{ .debug = "test" });
const buf = try alloc.alloc(u8, 64);
@memset(buf, 0xAB);
try testing.expectEqual(@as(u8, 0xAB), buf[0]);
@@ -118,14 +179,14 @@ test "arena pool - reuse entry after release" {
var pool = ArenaPool.init(testing.allocator, 512, 1024 * 16);
defer pool.deinit();
const alloc1 = try pool.acquire();
const alloc1 = try pool.acquire(.{ .debug = "test" });
try testing.expectEqual(@as(u16, 0), pool.free_list_len);
pool.release(alloc1);
try testing.expectEqual(@as(u16, 1), pool.free_list_len);
// The same entry should be returned from the free list.
const alloc2 = try pool.acquire();
const alloc2 = try pool.acquire(.{ .debug = "test" });
try testing.expectEqual(@as(u16, 0), pool.free_list_len);
try testing.expectEqual(alloc1.ptr, alloc2.ptr);
@@ -136,9 +197,9 @@ test "arena pool - multiple concurrent arenas" {
var pool = ArenaPool.init(testing.allocator, 512, 1024 * 16);
defer pool.deinit();
const a1 = try pool.acquire();
const a2 = try pool.acquire();
const a3 = try pool.acquire();
const a1 = try pool.acquire(.{ .debug = "test1" });
const a2 = try pool.acquire(.{ .debug = "test2" });
const a3 = try pool.acquire(.{ .debug = "test3" });
// All three must be distinct arenas.
try testing.expect(a1.ptr != a2.ptr);
@@ -161,8 +222,8 @@ test "arena pool - free list respects max limit" {
var pool = ArenaPool.init(testing.allocator, 1, 1024 * 16);
defer pool.deinit();
const a1 = try pool.acquire();
const a2 = try pool.acquire();
const a1 = try pool.acquire(.{ .debug = "test1" });
const a2 = try pool.acquire(.{ .debug = "test2" });
pool.release(a1);
try testing.expectEqual(@as(u16, 1), pool.free_list_len);
@@ -176,7 +237,7 @@ test "arena pool - reset clears memory without releasing" {
var pool = ArenaPool.init(testing.allocator, 512, 1024 * 16);
defer pool.deinit();
const alloc = try pool.acquire();
const alloc = try pool.acquire(.{ .debug = "test" });
const buf = try alloc.alloc(u8, 128);
@memset(buf, 0xFF);
@@ -200,8 +261,8 @@ test "arena pool - deinit with entries in free list" {
// detected by the test allocator).
var pool = ArenaPool.init(testing.allocator, 512, 1024 * 16);
const a1 = try pool.acquire();
const a2 = try pool.acquire();
const a1 = try pool.acquire(.{ .debug = "test1" });
const a2 = try pool.acquire(.{ .debug = "test2" });
_ = try a1.alloc(u8, 256);
_ = try a2.alloc(u8, 512);
pool.release(a1);

View File

@@ -23,6 +23,8 @@ const Allocator = std.mem.Allocator;
const log = @import("log.zig");
const dump = @import("browser/dump.zig");
const WebBotAuthConfig = @import("network/WebBotAuth.zig").Config;
pub const RunMode = enum {
help,
fetch,
@@ -31,6 +33,7 @@ pub const RunMode = enum {
mcp,
};
pub const MAX_LISTENERS = 16;
pub const CDP_MAX_HTTP_REQUEST_SIZE = 4096;
// max message size
@@ -153,6 +156,38 @@ pub fn userAgentSuffix(self: *const Config) ?[]const u8 {
};
}
pub fn cdpTimeout(self: *const Config) usize {
return switch (self.mode) {
.serve => |opts| if (opts.timeout > 604_800) 604_800_000 else @as(usize, opts.timeout) * 1000,
else => unreachable,
};
}
pub fn port(self: *const Config) u16 {
return switch (self.mode) {
.serve => |opts| opts.port,
else => unreachable,
};
}
pub fn advertiseHost(self: *const Config) []const u8 {
return switch (self.mode) {
.serve => |opts| opts.advertise_host orelse opts.host,
else => unreachable,
};
}
pub fn webBotAuth(self: *const Config) ?WebBotAuthConfig {
return switch (self.mode) {
inline .serve, .fetch, .mcp => |opts| WebBotAuthConfig{
.key_file = opts.common.web_bot_auth_key_file orelse return null,
.keyid = opts.common.web_bot_auth_keyid orelse return null,
.domain = opts.common.web_bot_auth_domain orelse return null,
},
.help, .version => null,
};
}
pub fn maxConnections(self: *const Config) u16 {
return switch (self.mode) {
.serve => |opts| opts.cdp_max_connections,
@@ -178,6 +213,7 @@ pub const Mode = union(RunMode) {
pub const Serve = struct {
host: []const u8 = "127.0.0.1",
port: u16 = 9222,
advertise_host: ?[]const u8 = null,
timeout: u31 = 10,
cdp_max_connections: u16 = 16,
cdp_max_pending_connections: u16 = 128,
@@ -192,6 +228,15 @@ pub const DumpFormat = enum {
html,
markdown,
wpt,
semantic_tree,
semantic_tree_text,
};
pub const WaitUntil = enum {
load,
domcontentloaded,
networkidle,
done,
};
pub const Fetch = struct {
@@ -201,6 +246,8 @@ pub const Fetch = struct {
with_base: bool = false,
with_frames: bool = false,
strip: dump.Opts.Strip = .{},
wait_ms: u32 = 5000,
wait_until: WaitUntil = .done,
};
pub const Common = struct {
@@ -217,6 +264,10 @@ pub const Common = struct {
log_format: ?log.Format = null,
log_filter_scopes: ?[]log.Scope = null,
user_agent_suffix: ?[]const u8 = null,
web_bot_auth_key_file: ?[]const u8 = null,
web_bot_auth_keyid: ?[]const u8 = null,
web_bot_auth_domain: ?[]const u8 = null,
};
/// Pre-formatted HTTP headers for reuse across Http and Client.
@@ -266,64 +317,72 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
// MAX_HELP_LEN|
const common_options =
\\
\\--insecure_disable_tls_host_verification
\\--insecure-disable-tls-host-verification
\\ Disables host verification on all HTTP requests. This is an
\\ advanced option which should only be set if you understand
\\ and accept the risk of disabling host verification.
\\
\\--obey_robots
\\--obey-robots
\\ Fetches and obeys the robots.txt (if available) of the web pages
\\ we make requests towards.
\\ Defaults to false.
\\
\\--http_proxy The HTTP proxy to use for all HTTP requests.
\\--http-proxy The HTTP proxy to use for all HTTP requests.
\\ A username:password can be included for basic authentication.
\\ Defaults to none.
\\
\\--proxy_bearer_token
\\--proxy-bearer-token
\\ The <token> to send for bearer authentication with the proxy
\\ Proxy-Authorization: Bearer <token>
\\
\\--http_max_concurrent
\\--http-max-concurrent
\\ The maximum number of concurrent HTTP requests.
\\ Defaults to 10.
\\
\\--http_max_host_open
\\--http-max-host-open
\\ The maximum number of open connection to a given host:port.
\\ Defaults to 4.
\\
\\--http_connect_timeout
\\--http-connect-timeout
\\ The time, in milliseconds, for establishing an HTTP connection
\\ before timing out. 0 means it never times out.
\\ Defaults to 0.
\\
\\--http_timeout
\\--http-timeout
\\ The maximum time, in milliseconds, the transfer is allowed
\\ to complete. 0 means it never times out.
\\ Defaults to 10000.
\\
\\--http_max_response_size
\\--http-max-response-size
\\ Limits the acceptable response size for any request
\\ (e.g. XHR, fetch, script loading, ...).
\\ Defaults to no limit.
\\
\\--log_level The log level: debug, info, warn, error or fatal.
\\--log-level The log level: debug, info, warn, error or fatal.
\\ Defaults to
++ (if (builtin.mode == .Debug) " info." else "warn.") ++
\\
\\
\\--log_format The log format: pretty or logfmt.
\\--log-format The log format: pretty or logfmt.
\\ Defaults to
++ (if (builtin.mode == .Debug) " pretty." else " logfmt.") ++
\\
\\
\\--log_filter_scopes
\\--log-filter-scopes
\\ Filter out too verbose logs per scope:
\\ http, unknown_prop, event, ...
\\
\\--user_agent_suffix
\\--user-agent-suffix
\\ Suffix to append to the Lightpanda/X.Y User-Agent
\\
\\--web-bot-auth-key-file
\\ Path to the Ed25519 private key PEM file.
\\
\\--web-bot-auth-keyid
\\ The JWK thumbprint of your public key.
\\
\\--web-bot-auth-domain
\\ Your domain e.g. yourdomain.com
;
// MAX_HELP_LEN|
@@ -338,19 +397,26 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\
\\Options:
\\--dump Dumps document to stdout.
\\ Argument must be 'html' or 'markdown'.
\\ Argument must be 'html', 'markdown', 'semantic_tree', or 'semantic_tree_text'.
\\ Defaults to no dump.
\\
\\--strip_mode Comma separated list of tag groups to remove from dump
\\ the dump. e.g. --strip_mode js,css
\\--strip-mode Comma separated list of tag groups to remove from dump
\\ the dump. e.g. --strip-mode js,css
\\ - "js" script and link[as=script, rel=preload]
\\ - "ui" includes img, picture, video, css and svg
\\ - "css" includes style and link[rel=stylesheet]
\\ - "full" includes js, ui and css
\\
\\--with_base Add a <base> tag in dump. Defaults to false.
\\--with-base Add a <base> tag in dump. Defaults to false.
\\
\\--with_frames Includes the contents of iframes. Defaults to false.
\\--with-frames Includes the contents of iframes. Defaults to false.
\\
\\--wait-ms Wait time in milliseconds.
\\ Defaults to 5000.
\\
\\--wait-until Wait until the specified event.
\\ Supported events: load, domcontentloaded, networkidle, done.
\\ Defaults to 'done'.
\\
++ common_options ++
\\
@@ -365,14 +431,19 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
\\--port Port of the CDP server
\\ Defaults to 9222
\\
\\--advertise-host
\\ The host to advertise, e.g. in the /json/version response.
\\ Useful, for example, when --host is 0.0.0.0.
\\ Defaults to --host value
\\
\\--timeout Inactivity timeout in seconds before disconnecting clients
\\ Defaults to 10 (seconds). Limited to 604800 (1 week).
\\
\\--cdp_max_connections
\\--cdp-max-connections
\\ Maximum number of simultaneous CDP connections.
\\ Defaults to 16.
\\
\\--cdp_max_pending_connections
\\--cdp-max-pending-connections
\\ Maximum pending connections in the accept queue.
\\ Defaults to 128.
\\
@@ -450,15 +521,15 @@ fn inferMode(opt: []const u8) ?RunMode {
return .fetch;
}
if (std.mem.eql(u8, opt, "--strip_mode")) {
if (std.mem.eql(u8, opt, "--strip-mode") or std.mem.eql(u8, opt, "--strip_mode")) {
return .fetch;
}
if (std.mem.eql(u8, opt, "--with_base")) {
if (std.mem.eql(u8, opt, "--with-base") or std.mem.eql(u8, opt, "--with_base")) {
return .fetch;
}
if (std.mem.eql(u8, opt, "--with_frames")) {
if (std.mem.eql(u8, opt, "--with-frames") or std.mem.eql(u8, opt, "--with_frames")) {
return .fetch;
}
@@ -506,6 +577,15 @@ fn parseServeArgs(
continue;
}
if (std.mem.eql(u8, "--advertise-host", opt) or std.mem.eql(u8, "--advertise_host", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
serve.advertise_host = try allocator.dupe(u8, str);
continue;
}
if (std.mem.eql(u8, "--timeout", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--timeout" });
@@ -519,27 +599,27 @@ fn parseServeArgs(
continue;
}
if (std.mem.eql(u8, "--cdp_max_connections", opt)) {
if (std.mem.eql(u8, "--cdp-max-connections", opt) or std.mem.eql(u8, "--cdp_max_connections", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--cdp_max_connections" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
serve.cdp_max_connections = std.fmt.parseInt(u16, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--cdp_max_connections", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
continue;
}
if (std.mem.eql(u8, "--cdp_max_pending_connections", opt)) {
if (std.mem.eql(u8, "--cdp-max-pending-connections", opt) or std.mem.eql(u8, "--cdp_max_pending_connections", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--cdp_max_pending_connections" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
serve.cdp_max_pending_connections = std.fmt.parseInt(u16, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--cdp_max_pending_connections", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
continue;
@@ -584,8 +664,34 @@ fn parseFetchArgs(
var url: ?[:0]const u8 = null;
var common: Common = .{};
var strip: dump.Opts.Strip = .{};
var wait_ms: u32 = 5000;
var wait_until: WaitUntil = .done;
while (args.next()) |opt| {
if (std.mem.eql(u8, "--wait-ms", opt) or std.mem.eql(u8, "--wait_ms", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
wait_ms = std.fmt.parseInt(u32, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
continue;
}
if (std.mem.eql(u8, "--wait-until", opt) or std.mem.eql(u8, "--wait_until", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
wait_until = std.meta.stringToEnum(WaitUntil, str) orelse {
log.fatal(.app, "invalid argument value", .{ .arg = opt, .val = str });
return error.InvalidArgument;
};
continue;
}
if (std.mem.eql(u8, "--dump", opt)) {
var peek_args = args.*;
if (peek_args.next()) |next_arg| {
@@ -604,25 +710,25 @@ fn parseFetchArgs(
if (std.mem.eql(u8, "--noscript", opt)) {
log.warn(.app, "deprecation warning", .{
.feature = "--noscript argument",
.hint = "use '--strip_mode js' instead",
.hint = "use '--strip-mode js' instead",
});
strip.js = true;
continue;
}
if (std.mem.eql(u8, "--with_base", opt)) {
if (std.mem.eql(u8, "--with-base", opt) or std.mem.eql(u8, "--with_base", opt)) {
with_base = true;
continue;
}
if (std.mem.eql(u8, "--with_frames", opt)) {
if (std.mem.eql(u8, "--with-frames", opt) or std.mem.eql(u8, "--with_frames", opt)) {
with_frames = true;
continue;
}
if (std.mem.eql(u8, "--strip_mode", opt)) {
if (std.mem.eql(u8, "--strip-mode", opt) or std.mem.eql(u8, "--strip_mode", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--strip_mode" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
@@ -640,7 +746,7 @@ fn parseFetchArgs(
strip.ui = true;
strip.css = true;
} else {
log.fatal(.app, "invalid option choice", .{ .arg = "--strip_mode", .value = trimmed });
log.fatal(.app, "invalid option choice", .{ .arg = opt, .value = trimmed });
}
}
continue;
@@ -674,6 +780,8 @@ fn parseFetchArgs(
.common = common,
.with_base = with_base,
.with_frames = with_frames,
.wait_ms = wait_ms,
.wait_until = wait_until,
};
}
@@ -683,102 +791,102 @@ fn parseCommonArg(
args: *std.process.ArgIterator,
common: *Common,
) !bool {
if (std.mem.eql(u8, "--insecure_disable_tls_host_verification", opt)) {
if (std.mem.eql(u8, "--insecure-disable-tls-host-verification", opt) or std.mem.eql(u8, "--insecure_disable_tls_host_verification", opt)) {
common.tls_verify_host = false;
return true;
}
if (std.mem.eql(u8, "--obey_robots", opt)) {
if (std.mem.eql(u8, "--obey-robots", opt) or std.mem.eql(u8, "--obey_robots", opt)) {
common.obey_robots = true;
return true;
}
if (std.mem.eql(u8, "--http_proxy", opt)) {
if (std.mem.eql(u8, "--http-proxy", opt) or std.mem.eql(u8, "--http_proxy", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_proxy" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_proxy = try allocator.dupeZ(u8, str);
return true;
}
if (std.mem.eql(u8, "--proxy_bearer_token", opt)) {
if (std.mem.eql(u8, "--proxy-bearer-token", opt) or std.mem.eql(u8, "--proxy_bearer_token", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--proxy_bearer_token" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.proxy_bearer_token = try allocator.dupeZ(u8, str);
return true;
}
if (std.mem.eql(u8, "--http_max_concurrent", opt)) {
if (std.mem.eql(u8, "--http-max-concurrent", opt) or std.mem.eql(u8, "--http_max_concurrent", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_max_concurrent" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_max_concurrent = std.fmt.parseInt(u8, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--http_max_concurrent", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--http_max_host_open", opt)) {
if (std.mem.eql(u8, "--http-max-host-open", opt) or std.mem.eql(u8, "--http_max_host_open", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_max_host_open" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_max_host_open = std.fmt.parseInt(u8, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--http_max_host_open", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--http_connect_timeout", opt)) {
if (std.mem.eql(u8, "--http-connect-timeout", opt) or std.mem.eql(u8, "--http_connect_timeout", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_connect_timeout" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_connect_timeout = std.fmt.parseInt(u31, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--http_connect_timeout", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--http_timeout", opt)) {
if (std.mem.eql(u8, "--http-timeout", opt) or std.mem.eql(u8, "--http_timeout", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_timeout" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_timeout = std.fmt.parseInt(u31, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--http_timeout", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--http_max_response_size", opt)) {
if (std.mem.eql(u8, "--http-max-response-size", opt) or std.mem.eql(u8, "--http_max_response_size", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--http_max_response_size" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.http_max_response_size = std.fmt.parseInt(usize, str, 10) catch |err| {
log.fatal(.app, "invalid argument value", .{ .arg = "--http_max_response_size", .err = err });
log.fatal(.app, "invalid argument value", .{ .arg = opt, .err = err });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--log_level", opt)) {
if (std.mem.eql(u8, "--log-level", opt) or std.mem.eql(u8, "--log_level", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--log_level" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
@@ -786,26 +894,26 @@ fn parseCommonArg(
if (std.mem.eql(u8, str, "error")) {
break :blk .err;
}
log.fatal(.app, "invalid option choice", .{ .arg = "--log_level", .value = str });
log.fatal(.app, "invalid option choice", .{ .arg = opt, .value = str });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--log_format", opt)) {
if (std.mem.eql(u8, "--log-format", opt) or std.mem.eql(u8, "--log_format", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--log_format" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.log_format = std.meta.stringToEnum(log.Format, str) orelse {
log.fatal(.app, "invalid option choice", .{ .arg = "--log_format", .value = str });
log.fatal(.app, "invalid option choice", .{ .arg = opt, .value = str });
return error.InvalidArgument;
};
return true;
}
if (std.mem.eql(u8, "--log_filter_scopes", opt)) {
if (std.mem.eql(u8, "--log-filter-scopes", opt) or std.mem.eql(u8, "--log_filter_scopes", opt)) {
if (builtin.mode != .Debug) {
log.fatal(.app, "experimental", .{ .help = "log scope filtering is only available in debug builds" });
return false;
@@ -822,7 +930,7 @@ fn parseCommonArg(
var it = std.mem.splitScalar(u8, str, ',');
while (it.next()) |part| {
try arr.append(allocator, std.meta.stringToEnum(log.Scope, part) orelse {
log.fatal(.app, "invalid option choice", .{ .arg = "--log_filter_scopes", .value = part });
log.fatal(.app, "invalid option choice", .{ .arg = opt, .value = part });
return false;
});
}
@@ -830,14 +938,14 @@ fn parseCommonArg(
return true;
}
if (std.mem.eql(u8, "--user_agent_suffix", opt)) {
if (std.mem.eql(u8, "--user-agent-suffix", opt) or std.mem.eql(u8, "--user_agent_suffix", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = "--user_agent_suffix" });
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
for (str) |c| {
if (!std.ascii.isPrint(c)) {
log.fatal(.app, "not printable character", .{ .arg = "--user_agent_suffix" });
log.fatal(.app, "not printable character", .{ .arg = opt });
return error.InvalidArgument;
}
}
@@ -845,5 +953,32 @@ fn parseCommonArg(
return true;
}
if (std.mem.eql(u8, "--web-bot-auth-key-file", opt) or std.mem.eql(u8, "--web_bot_auth_key_file", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.web_bot_auth_key_file = try allocator.dupe(u8, str);
return true;
}
if (std.mem.eql(u8, "--web-bot-auth-keyid", opt) or std.mem.eql(u8, "--web_bot_auth_keyid", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.web_bot_auth_keyid = try allocator.dupe(u8, str);
return true;
}
if (std.mem.eql(u8, "--web-bot-auth-domain", opt) or std.mem.eql(u8, "--web_bot_auth_domain", opt)) {
const str = args.next() orelse {
log.fatal(.app, "missing argument value", .{ .arg = opt });
return error.InvalidArgument;
};
common.web_bot_auth_domain = try allocator.dupe(u8, str);
return true;
}
return false;
}

View File

@@ -21,7 +21,7 @@ const lp = @import("lightpanda");
const log = @import("log.zig");
const Page = @import("browser/Page.zig");
const Transfer = @import("http/Client.zig").Transfer;
const Transfer = @import("browser/HttpClient.zig").Transfer;
const Allocator = std.mem.Allocator;

563
src/SemanticTree.zig Normal file
View File

@@ -0,0 +1,563 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. See <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const log = @import("log.zig");
const isAllWhitespace = @import("string.zig").isAllWhitespace;
const Page = lp.Page;
const interactive = @import("browser/interactive.zig");
const CData = @import("browser/webapi/CData.zig");
const Element = @import("browser/webapi/Element.zig");
const Node = @import("browser/webapi/Node.zig");
const AXNode = @import("cdp/AXNode.zig");
const CDPNode = @import("cdp/Node.zig");
const Self = @This();
dom_node: *Node,
registry: *CDPNode.Registry,
page: *Page,
arena: std.mem.Allocator,
prune: bool = true,
interactive_only: bool = false,
max_depth: u32 = std.math.maxInt(u32) - 1,
pub fn jsonStringify(self: @This(), jw: *std.json.Stringify) error{WriteFailed}!void {
var visitor = JsonVisitor{ .jw = jw, .tree = self };
var xpath_buffer: std.ArrayList(u8) = .{};
const listener_targets = interactive.buildListenerTargetMap(self.page, self.arena) catch |err| {
log.err(.app, "listener map failed", .{ .err = err });
return error.WriteFailed;
};
var visibility_cache: Element.VisibilityCache = .empty;
var pointer_events_cache: Element.PointerEventsCache = .empty;
var ctx: WalkContext = .{
.xpath_buffer = &xpath_buffer,
.listener_targets = listener_targets,
.visibility_cache = &visibility_cache,
.pointer_events_cache = &pointer_events_cache,
};
self.walk(&ctx, self.dom_node, null, &visitor, 1, 0) catch |err| {
log.err(.app, "semantic tree json dump failed", .{ .err = err });
return error.WriteFailed;
};
}
pub fn textStringify(self: @This(), writer: *std.Io.Writer) error{WriteFailed}!void {
var visitor = TextVisitor{ .writer = writer, .tree = self, .depth = 0 };
var xpath_buffer: std.ArrayList(u8) = .empty;
const listener_targets = interactive.buildListenerTargetMap(self.page, self.arena) catch |err| {
log.err(.app, "listener map failed", .{ .err = err });
return error.WriteFailed;
};
var visibility_cache: Element.VisibilityCache = .empty;
var pointer_events_cache: Element.PointerEventsCache = .empty;
var ctx: WalkContext = .{
.xpath_buffer = &xpath_buffer,
.listener_targets = listener_targets,
.visibility_cache = &visibility_cache,
.pointer_events_cache = &pointer_events_cache,
};
self.walk(&ctx, self.dom_node, null, &visitor, 1, 0) catch |err| {
log.err(.app, "semantic tree text dump failed", .{ .err = err });
return error.WriteFailed;
};
}
const OptionData = struct {
value: []const u8,
text: []const u8,
selected: bool,
};
const NodeData = struct {
id: CDPNode.Id,
axn: AXNode,
role: []const u8,
name: ?[]const u8,
value: ?[]const u8,
options: ?[]OptionData = null,
xpath: []const u8,
is_interactive: bool,
node_name: []const u8,
};
const WalkContext = struct {
xpath_buffer: *std.ArrayList(u8),
listener_targets: interactive.ListenerTargetMap,
visibility_cache: *Element.VisibilityCache,
pointer_events_cache: *Element.PointerEventsCache,
};
fn walk(
self: @This(),
ctx: *WalkContext,
node: *Node,
parent_name: ?[]const u8,
visitor: anytype,
index: usize,
current_depth: u32,
) !void {
if (current_depth > self.max_depth) return;
// 1. Skip non-content nodes
if (node.is(Element)) |el| {
const tag = el.getTag();
if (tag.isMetadata() or tag == .svg) return;
// We handle options/optgroups natively inside their parents, skip them in the general walk
if (tag == .datalist or tag == .option or tag == .optgroup) return;
// Check visibility using the engine's checkVisibility which handles CSS display: none
if (!el.checkVisibilityCached(ctx.visibility_cache, self.page)) {
return;
}
if (el.is(Element.Html)) |html_el| {
if (html_el.getHidden()) return;
}
} else if (node.is(CData.Text)) |text_node| {
const text = text_node.getWholeText();
if (isAllWhitespace(text)) {
return;
}
} else if (node._type != .document and node._type != .document_fragment) {
return;
}
const cdp_node = try self.registry.register(node);
const axn = AXNode.fromNode(node);
const role = try axn.getRole();
var is_interactive = false;
var value: ?[]const u8 = null;
var options: ?[]OptionData = null;
var node_name: []const u8 = "text";
if (node.is(Element)) |el| {
node_name = el.getTagNameLower();
if (el.is(Element.Html.Input)) |input| {
value = input.getValue();
if (el.getAttributeSafe(comptime lp.String.wrap("list"))) |list_id| {
options = try extractDataListOptions(list_id, self.page, self.arena);
}
} else if (el.is(Element.Html.TextArea)) |textarea| {
value = textarea.getValue();
} else if (el.is(Element.Html.Select)) |select| {
value = select.getValue(self.page);
options = try extractSelectOptions(el.asNode(), self.page, self.arena);
}
if (el.is(Element.Html)) |html_el| {
if (interactive.classifyInteractivity(self.page, el, html_el, ctx.listener_targets, ctx.pointer_events_cache) != null) {
is_interactive = true;
}
}
} else if (node._type == .document or node._type == .document_fragment) {
node_name = "root";
}
const initial_xpath_len = ctx.xpath_buffer.items.len;
try appendXPathSegment(node, ctx.xpath_buffer.writer(self.arena), index);
const xpath = ctx.xpath_buffer.items;
var name = try axn.getName(self.page, self.arena);
const has_explicit_label = if (node.is(Element)) |el|
el.getAttributeSafe(.wrap("aria-label")) != null or el.getAttributeSafe(.wrap("title")) != null
else
false;
const structural = isStructuralRole(role);
// Filter out computed concatenated names for generic containers without explicit labels.
// This prevents token bloat and ensures their StaticText children aren't incorrectly pruned.
// We ignore interactivity because a generic wrapper with an event listener still shouldn't hoist all text.
if (name != null and structural and !has_explicit_label) {
name = null;
}
var should_visit = true;
if (self.interactive_only) {
var keep = false;
if (interactive.isInteractiveRole(role)) {
keep = true;
} else if (interactive.isContentRole(role)) {
if (name != null and name.?.len > 0) {
keep = true;
}
} else if (std.mem.eql(u8, role, "RootWebArea")) {
keep = true;
} else if (is_interactive) {
keep = true;
}
if (!keep) {
should_visit = false;
}
} else if (self.prune) {
if (structural and !is_interactive and !has_explicit_label) {
should_visit = false;
}
if (std.mem.eql(u8, role, "StaticText") and node._parent != null) {
if (parent_name != null and name != null and std.mem.indexOf(u8, parent_name.?, name.?) != null) {
should_visit = false;
}
}
}
var did_visit = false;
var should_walk_children = true;
var data: NodeData = .{
.id = cdp_node.id,
.axn = axn,
.role = role,
.name = name,
.value = value,
.options = options,
.xpath = xpath,
.is_interactive = is_interactive,
.node_name = node_name,
};
if (should_visit) {
should_walk_children = try visitor.visit(node, &data);
did_visit = true; // Always true if should_visit was true, because visit() executed and opened structures
} else {
// If we skip the node, we must NOT tell the visitor to close it later
did_visit = false;
}
if (should_walk_children) {
// If we are printing this node normally OR skipping it and unrolling its children,
// we walk the children iterator.
var it = node.childrenIterator();
var tag_counts = std.StringArrayHashMap(usize).init(self.arena);
while (it.next()) |child| {
var tag: []const u8 = "text()";
if (child.is(Element)) |el| {
tag = el.getTagNameLower();
}
const gop = try tag_counts.getOrPut(tag);
if (!gop.found_existing) {
gop.value_ptr.* = 0;
}
gop.value_ptr.* += 1;
try self.walk(ctx, child, name, visitor, gop.value_ptr.*, current_depth + 1);
}
}
if (did_visit) {
try visitor.leave();
}
ctx.xpath_buffer.shrinkRetainingCapacity(initial_xpath_len);
}
fn extractSelectOptions(node: *Node, page: *Page, arena: std.mem.Allocator) ![]OptionData {
var options: std.ArrayList(OptionData) = .empty;
var it = node.childrenIterator();
while (it.next()) |child| {
if (child.is(Element)) |el| {
if (el.getTag() == .option) {
if (el.is(Element.Html.Option)) |opt| {
const text = opt.getText(page);
const value = opt.getValue(page);
const selected = opt.getSelected();
try options.append(arena, .{ .text = text, .value = value, .selected = selected });
}
} else if (el.getTag() == .optgroup) {
var group_it = child.childrenIterator();
while (group_it.next()) |group_child| {
if (group_child.is(Element.Html.Option)) |opt| {
const text = opt.getText(page);
const value = opt.getValue(page);
const selected = opt.getSelected();
try options.append(arena, .{ .text = text, .value = value, .selected = selected });
}
}
}
}
}
return options.toOwnedSlice(arena);
}
fn extractDataListOptions(list_id: []const u8, page: *Page, arena: std.mem.Allocator) !?[]OptionData {
if (page.document.getElementById(list_id, page)) |referenced_el| {
if (referenced_el.getTag() == .datalist) {
return try extractSelectOptions(referenced_el.asNode(), page, arena);
}
}
return null;
}
fn appendXPathSegment(node: *Node, writer: anytype, index: usize) !void {
if (node.is(Element)) |el| {
const tag = el.getTagNameLower();
try std.fmt.format(writer, "/{s}[{d}]", .{ tag, index });
} else if (node.is(CData.Text)) |_| {
try std.fmt.format(writer, "/text()[{d}]", .{index});
}
}
const JsonVisitor = struct {
jw: *std.json.Stringify,
tree: Self,
pub fn visit(self: *JsonVisitor, node: *Node, data: *NodeData) !bool {
try self.jw.beginObject();
try self.jw.objectField("nodeId");
try self.jw.write(try std.fmt.allocPrint(self.tree.arena, "{d}", .{data.id}));
try self.jw.objectField("backendDOMNodeId");
try self.jw.write(data.id);
try self.jw.objectField("nodeName");
try self.jw.write(data.node_name);
try self.jw.objectField("xpath");
try self.jw.write(data.xpath);
if (node.is(Element)) |el| {
try self.jw.objectField("nodeType");
try self.jw.write(1);
try self.jw.objectField("isInteractive");
try self.jw.write(data.is_interactive);
try self.jw.objectField("role");
try self.jw.write(data.role);
if (data.name) |name| {
if (name.len > 0) {
try self.jw.objectField("name");
try self.jw.write(name);
}
}
if (data.value) |value| {
try self.jw.objectField("value");
try self.jw.write(value);
}
if (el._attributes) |attrs| {
try self.jw.objectField("attributes");
try self.jw.beginObject();
var iter = attrs.iterator();
while (iter.next()) |attr| {
try self.jw.objectField(attr._name.str());
try self.jw.write(attr._value.str());
}
try self.jw.endObject();
}
if (data.options) |options| {
try self.jw.objectField("options");
try self.jw.beginArray();
for (options) |opt| {
try self.jw.beginObject();
try self.jw.objectField("value");
try self.jw.write(opt.value);
try self.jw.objectField("text");
try self.jw.write(opt.text);
try self.jw.objectField("selected");
try self.jw.write(opt.selected);
try self.jw.endObject();
}
try self.jw.endArray();
}
} else if (node.is(CData.Text)) |text_node| {
try self.jw.objectField("nodeType");
try self.jw.write(3);
try self.jw.objectField("nodeValue");
try self.jw.write(text_node.getWholeText());
} else {
try self.jw.objectField("nodeType");
try self.jw.write(9);
}
try self.jw.objectField("children");
try self.jw.beginArray();
if (data.options != null) {
// Signal to not walk children, as we handled them natively
return false;
}
return true;
}
pub fn leave(self: *JsonVisitor) !void {
try self.jw.endArray();
try self.jw.endObject();
}
};
fn isStructuralRole(role: []const u8) bool {
const structural_roles = std.StaticStringMap(void).initComptime(.{
.{ "none", {} },
.{ "generic", {} },
.{ "InlineTextBox", {} },
.{ "banner", {} },
.{ "navigation", {} },
.{ "main", {} },
.{ "list", {} },
.{ "listitem", {} },
.{ "table", {} },
.{ "rowgroup", {} },
.{ "row", {} },
.{ "cell", {} },
.{ "region", {} },
});
return structural_roles.has(role);
}
const TextVisitor = struct {
writer: *std.Io.Writer,
tree: Self,
depth: usize,
pub fn visit(self: *TextVisitor, node: *Node, data: *NodeData) !bool {
for (0..self.depth) |_| {
try self.writer.writeByte(' ');
}
var name_to_print: ?[]const u8 = null;
if (data.name) |n| {
if (n.len > 0) {
name_to_print = n;
}
} else if (node.is(CData.Text)) |text_node| {
const trimmed = std.mem.trim(u8, text_node.getWholeText(), " \t\r\n");
if (trimmed.len > 0) {
name_to_print = trimmed;
}
}
const is_text_only = std.mem.eql(u8, data.role, "StaticText") or std.mem.eql(u8, data.role, "none") or std.mem.eql(u8, data.role, "generic");
try self.writer.print("{d}", .{data.id});
if (!is_text_only) {
try self.writer.print(" {s}", .{data.role});
}
if (name_to_print) |n| {
try self.writer.print(" '{s}'", .{n});
}
if (data.value) |v| {
if (v.len > 0) {
try self.writer.print(" value='{s}'", .{v});
}
}
if (data.options) |options| {
try self.writer.writeAll(" options=[");
for (options, 0..) |opt, i| {
if (i > 0) try self.writer.writeAll(",");
try self.writer.print("'{s}'", .{opt.value});
if (opt.selected) {
try self.writer.writeAll("*");
}
}
try self.writer.writeAll("]\n");
self.depth += 1;
return false; // Native handling complete, do not walk children
}
try self.writer.writeByte('\n');
self.depth += 1;
// If this is a leaf-like semantic node and we already have a name,
// skip children to avoid redundant StaticText or noise.
const is_leaf_semantic = std.mem.eql(u8, data.role, "link") or
std.mem.eql(u8, data.role, "button") or
std.mem.eql(u8, data.role, "heading") or
std.mem.eql(u8, data.role, "code");
if (is_leaf_semantic and data.name != null and data.name.?.len > 0) {
return false;
}
return true;
}
pub fn leave(self: *TextVisitor) !void {
if (self.depth > 0) {
self.depth -= 1;
}
}
};
const testing = @import("testing.zig");
test "SemanticTree backendDOMNodeId" {
var registry: CDPNode.Registry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html");
defer testing.reset();
defer page._session.removePage();
const st: Self = .{
.dom_node = page.window._document.asNode(),
.registry = &registry,
.page = page,
.arena = testing.arena_allocator,
.prune = false,
.interactive_only = false,
.max_depth = std.math.maxInt(u32) - 1,
};
const json_str = try std.json.Stringify.valueAlloc(testing.allocator, st, .{});
defer testing.allocator.free(json_str);
try testing.expect(std.mem.indexOf(u8, json_str, "\"backendDOMNodeId\":") != null);
}
test "SemanticTree max_depth" {
var registry: CDPNode.Registry = .init(testing.allocator);
defer registry.deinit();
var page = try testing.pageTest("cdp/registry1.html");
defer testing.reset();
defer page._session.removePage();
const st: Self = .{
.dom_node = page.window._document.asNode(),
.registry = &registry,
.page = page,
.arena = testing.arena_allocator,
.prune = false,
.interactive_only = false,
.max_depth = 1,
};
var aw: std.Io.Writer.Allocating = .init(testing.allocator);
defer aw.deinit();
try st.textStringify(&aw.writer);
const text_str = aw.written();
try testing.expect(std.mem.indexOf(u8, text_str, "other") == null);
}

View File

@@ -18,28 +18,22 @@
const std = @import("std");
const lp = @import("lightpanda");
const builtin = @import("builtin");
const net = std.net;
const posix = std.posix;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const log = @import("log.zig");
const App = @import("App.zig");
const Config = @import("Config.zig");
const CDP = @import("cdp/cdp.zig").CDP;
const Net = @import("Net.zig");
const Http = @import("http/Http.zig");
const HttpClient = @import("http/Client.zig");
const CDP = @import("cdp/CDP.zig");
const Net = @import("network/websocket.zig");
const HttpClient = @import("browser/HttpClient.zig");
const Server = @This();
app: *App,
shutdown: std.atomic.Value(bool) = .init(false),
allocator: Allocator,
listener: ?posix.socket_t,
json_version_response: []const u8,
// Thread management
@@ -48,104 +42,53 @@ clients: std.ArrayList(*Client) = .{},
client_mutex: std.Thread.Mutex = .{},
clients_pool: std.heap.MemoryPool(Client),
pub fn init(app: *App, address: net.Address) !Server {
pub fn init(app: *App, address: net.Address) !*Server {
const allocator = app.allocator;
const json_version_response = try buildJSONVersionResponse(allocator, address);
const json_version_response = try buildJSONVersionResponse(app);
errdefer allocator.free(json_version_response);
return .{
const self = try allocator.create(Server);
errdefer allocator.destroy(self);
self.* = .{
.app = app,
.listener = null,
.allocator = allocator,
.json_version_response = json_version_response,
.clients_pool = std.heap.MemoryPool(Client).init(app.allocator),
.clients_pool = std.heap.MemoryPool(Client).init(allocator),
};
try self.app.network.bind(address, self, onAccept);
log.info(.app, "server running", .{ .address = address });
return self;
}
/// Interrupts the server so that main can complete normally and call all defer handlers.
pub fn stop(self: *Server) void {
if (self.shutdown.swap(true, .release)) {
return;
}
// Shutdown all active clients
{
pub fn shutdown(self: *Server) void {
self.client_mutex.lock();
defer self.client_mutex.unlock();
for (self.clients.items) |client| {
client.stop();
}
}
// Linux and BSD/macOS handle canceling a socket blocked on accept differently.
// For Linux, we use std.shutdown, which will cause accept to return error.SocketNotListening (EINVAL).
// For BSD, shutdown will return an error. Instead we call posix.close, which will result with error.ConnectionAborted (BADF).
if (self.listener) |listener| switch (builtin.target.os.tag) {
.linux => posix.shutdown(listener, .recv) catch |err| {
log.warn(.app, "listener shutdown", .{ .err = err });
},
.macos, .freebsd, .netbsd, .openbsd => {
self.listener = null;
posix.close(listener);
},
else => unreachable,
};
}
pub fn deinit(self: *Server) void {
if (!self.shutdown.load(.acquire)) {
self.stop();
}
self.shutdown();
self.joinThreads();
if (self.listener) |listener| {
posix.close(listener);
self.listener = null;
}
self.clients.deinit(self.allocator);
self.clients_pool.deinit();
self.allocator.free(self.json_version_response);
self.allocator.destroy(self);
}
pub fn run(self: *Server, address: net.Address, timeout_ms: u32) !void {
const flags = posix.SOCK.STREAM | posix.SOCK.CLOEXEC | posix.SOCK.NONBLOCK;
const listener = try posix.socket(address.any.family, flags, posix.IPPROTO.TCP);
self.listener = listener;
try posix.setsockopt(listener, posix.SOL.SOCKET, posix.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));
if (@hasDecl(posix.TCP, "NODELAY")) {
try posix.setsockopt(listener, posix.IPPROTO.TCP, posix.TCP.NODELAY, &std.mem.toBytes(@as(c_int, 1)));
}
try posix.bind(listener, &address.any, address.getOsSockLen());
try posix.listen(listener, self.app.config.maxPendingConnections());
log.info(.app, "server running", .{ .address = address });
while (!self.shutdown.load(.acquire)) {
const socket = posix.accept(listener, null, null, posix.SOCK.NONBLOCK) catch |err| {
switch (err) {
error.SocketNotListening, error.ConnectionAborted => {
log.info(.app, "server stopped", .{});
break;
},
error.WouldBlock => {
std.Thread.sleep(10 * std.time.ns_per_ms);
continue;
},
else => {
log.err(.app, "CDP accept", .{ .err = err });
std.Thread.sleep(std.time.ns_per_s);
continue;
},
}
};
fn onAccept(ctx: *anyopaque, socket: posix.socket_t) void {
const self: *Server = @ptrCast(@alignCast(ctx));
const timeout_ms: u32 = @intCast(self.app.config.cdpTimeout());
self.spawnWorker(socket, timeout_ms) catch |err| {
log.err(.app, "CDP spawn", .{ .err = err });
posix.close(socket);
};
}
}
fn handleConnection(self: *Server, socket: posix.socket_t, timeout_ms: u32) void {
defer posix.close(socket);
@@ -173,10 +116,10 @@ fn handleConnection(self: *Server, socket: posix.socket_t, timeout_ms: u32) void
self.registerClient(client);
defer self.unregisterClient(client);
// Check shutdown after registering to avoid missing stop() signal.
// If stop() already iterated over clients, this client won't receive stop()
// Check shutdown after registering to avoid missing the stop signal.
// If deinit() already iterated over clients, this client won't receive stop()
// and would block joinThreads() indefinitely.
if (self.shutdown.load(.acquire)) {
if (self.app.shutdown()) {
return;
}
@@ -213,7 +156,7 @@ fn unregisterClient(self: *Server, client: *Client) void {
}
fn spawnWorker(self: *Server, socket: posix.socket_t, timeout_ms: u32) !void {
if (self.shutdown.load(.acquire)) {
if (self.app.shutdown()) {
return error.ShuttingDown;
}
@@ -268,7 +211,7 @@ pub const Client = struct {
http: *HttpClient,
ws: Net.WsConnection,
fn init(
pub fn init(
socket: posix.socket_t,
allocator: Allocator,
app: *App,
@@ -283,7 +226,7 @@ pub const Client = struct {
log.info(.app, "client connected", .{ .ip = client_address });
}
const http = try app.http.createClient(allocator);
const http = try HttpClient.init(allocator, &app.network);
errdefer http.deinit();
return .{
@@ -298,12 +241,15 @@ pub const Client = struct {
fn stop(self: *Client) void {
switch (self.mode) {
.http => {},
.cdp => |*cdp| cdp.browser.env.terminate(),
.cdp => |*cdp| {
cdp.browser.env.terminate();
self.ws.sendClose();
},
}
self.ws.shutdown();
}
fn deinit(self: *Client) void {
pub fn deinit(self: *Client) void {
switch (self.mode) {
.cdp => |*cdp| cdp.deinit(),
.http => {},
@@ -351,19 +297,12 @@ pub const Client = struct {
}
var cdp = &self.mode.cdp;
var last_message = timestamp(.monotonic);
var last_message = milliTimestamp(.monotonic);
var ms_remaining = self.ws.timeout_ms;
while (true) {
switch (cdp.pageWait(ms_remaining)) {
.cdp_socket => {
if (self.readSocket() == false) {
return;
}
last_message = timestamp(.monotonic);
ms_remaining = self.ws.timeout_ms;
},
.no_page => {
const result = cdp.pageWait(ms_remaining) catch |wait_err| switch (wait_err) {
error.NoPage => {
const status = http.tick(ms_remaining) catch |err| {
log.err(.app, "http tick", .{ .err = err });
return;
@@ -375,16 +314,30 @@ pub const Client = struct {
if (self.readSocket() == false) {
return;
}
last_message = timestamp(.monotonic);
last_message = milliTimestamp(.monotonic);
ms_remaining = self.ws.timeout_ms;
continue;
},
else => return wait_err,
};
switch (result) {
.cdp_socket => {
if (self.readSocket() == false) {
return;
}
last_message = milliTimestamp(.monotonic);
ms_remaining = self.ws.timeout_ms;
},
.done => {
const elapsed = timestamp(.monotonic) - last_message;
if (elapsed > ms_remaining) {
const now = milliTimestamp(.monotonic);
const elapsed = now - last_message;
if (elapsed >= ms_remaining) {
log.info(.app, "CDP timeout", .{});
return;
}
ms_remaining -= @intCast(elapsed);
last_message = now;
},
}
}
@@ -507,7 +460,7 @@ pub const Client = struct {
fn upgradeConnection(self: *Client, request: []u8) !void {
try self.ws.upgrade(request);
self.mode = .{ .cdp = try CDP.init(self.app, self.http, self) };
self.mode = .{ .cdp = try CDP.init(self) };
}
fn writeHTTPErrorResponse(self: *Client, comptime status: u16, comptime body: []const u8) void {
@@ -535,11 +488,17 @@ pub const Client = struct {
// --------
fn buildJSONVersionResponse(
allocator: Allocator,
address: net.Address,
app: *const App,
) ![]const u8 {
const body_format = "{{\"webSocketDebuggerUrl\": \"ws://{f}/\"}}";
const body_len = std.fmt.count(body_format, .{address});
const port = app.config.port();
const host = app.config.advertiseHost();
if (std.mem.eql(u8, host, "0.0.0.0")) {
log.info(.cdp, "unreachable advertised host", .{
.message = "when --host is set to 0.0.0.0 consider setting --advertise-host to a reachable address",
});
}
const body_format = "{{\"webSocketDebuggerUrl\": \"ws://{s}:{d}/\"}}";
const body_len = std.fmt.count(body_format, .{ host, port });
// We send a Connection: Close (and actually close the connection)
// because chromedp (Go driver) sends a request to /json/version and then
@@ -553,22 +512,22 @@ fn buildJSONVersionResponse(
"Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
body_format;
return try std.fmt.allocPrint(allocator, response_format, .{ body_len, address });
return try std.fmt.allocPrint(app.allocator, response_format, .{ body_len, host, port });
}
pub const timestamp = @import("datetime.zig").timestamp;
pub const milliTimestamp = @import("datetime.zig").milliTimestamp;
const testing = std.testing;
const testing = @import("testing.zig");
test "server: buildJSONVersionResponse" {
const address = try net.Address.parseIp4("127.0.0.1", 9001);
const res = try buildJSONVersionResponse(testing.allocator, address);
defer testing.allocator.free(res);
const res = try buildJSONVersionResponse(testing.test_app);
defer testing.test_app.allocator.free(res);
try testing.expectEqualStrings("HTTP/1.1 200 OK\r\n" ++
try testing.expectEqual("HTTP/1.1 200 OK\r\n" ++
"Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9001/\"}", res);
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}", res);
}
test "Client: http invalid request" {
@@ -576,7 +535,7 @@ test "Client: http invalid request" {
defer c.deinit();
const res = try c.httpRequest("GET /over/9000 HTTP/1.1\r\n" ++ "Header: " ++ ("a" ** 4100) ++ "\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 413 \r\n" ++
try testing.expectEqual("HTTP/1.1 413 \r\n" ++
"Connection: Close\r\n" ++
"Content-Length: 17\r\n\r\n" ++
"Request too large", res);
@@ -645,7 +604,7 @@ test "Client: http valid handshake" {
"Custom: Header-Value\r\n\r\n";
const res = try c.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++
try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);
@@ -773,7 +732,7 @@ test "server: 404" {
defer c.deinit();
const res = try c.httpRequest("GET /unknown HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings("HTTP/1.1 404 \r\n" ++
try testing.expectEqual("HTTP/1.1 404 \r\n" ++
"Connection: Close\r\n" ++
"Content-Length: 9\r\n\r\n" ++
"Not found", res);
@@ -785,7 +744,7 @@ test "server: get /json/version" {
"Content-Length: 48\r\n" ++
"Connection: Close\r\n" ++
"Content-Type: application/json; charset=UTF-8\r\n\r\n" ++
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9583/\"}";
"{\"webSocketDebuggerUrl\": \"ws://127.0.0.1:9222/\"}";
{
// twice on the same connection
@@ -793,7 +752,7 @@ test "server: get /json/version" {
defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1);
try testing.expectEqual(expected_response, res1);
}
{
@@ -802,7 +761,7 @@ test "server: get /json/version" {
defer c.deinit();
const res1 = try c.httpRequest("GET /json/version HTTP/1.1\r\n\r\n");
try testing.expectEqualStrings(expected_response, res1);
try testing.expectEqual(expected_response, res1);
}
}
@@ -820,7 +779,7 @@ fn assertHTTPError(
.{ expected_status, expected_body.len, expected_body },
);
try testing.expectEqualStrings(expected_response, res);
try testing.expectEqual(expected_response, res);
}
fn assertWebSocketError(close_code: u16, input: []const u8) !void {
@@ -964,7 +923,7 @@ const TestClient = struct {
"Custom: Header-Value\r\n\r\n";
const res = try self.httpRequest(request);
try testing.expectEqualStrings("HTTP/1.1 101 Switching Protocols\r\n" ++
try testing.expectEqual("HTTP/1.1 101 Switching Protocols\r\n" ++
"Upgrade: websocket\r\n" ++
"Connection: upgrade\r\n" ++
"Sec-Websocket-Accept: flzHu2DevQ2dSCSVqKSii5e9C2o=\r\n\r\n", res);

View File

@@ -19,17 +19,13 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const js = @import("js/js.zig");
const log = @import("../log.zig");
const App = @import("../App.zig");
const HttpClient = @import("../http/Client.zig");
const HttpClient = @import("HttpClient.zig");
const ArenaPool = App.ArenaPool;
const IS_DEBUG = @import("builtin").mode == .Debug;
const Session = @import("Session.zig");
const Notification = @import("../Notification.zig");
@@ -91,25 +87,32 @@ pub fn runMicrotasks(self: *Browser) void {
self.env.runMicrotasks();
}
pub fn runMacrotasks(self: *Browser) !?u64 {
pub fn runMacrotasks(self: *Browser) !void {
const env = &self.env;
const time_to_next = try self.env.runMacrotasks();
try self.env.runMacrotasks();
env.pumpMessageLoop();
// either of the above could have queued more microtasks
env.runMicrotasks();
return time_to_next;
}
pub fn hasBackgroundTasks(self: *Browser) bool {
return self.env.hasBackgroundTasks();
}
pub fn waitForBackgroundTasks(self: *Browser) void {
self.env.waitForBackgroundTasks();
}
pub fn msToNextMacrotask(self: *Browser) ?u64 {
return self.env.msToNextMacrotask();
}
pub fn msTo(self: *Browser) bool {
return self.env.hasBackgroundTasks();
}
pub fn runIdleTasks(self: *const Browser) void {
self.env.runIdleTasks();
}

View File

@@ -205,7 +205,7 @@ pub fn dispatch(self: *EventManager, target: *EventTarget, event: *Event) Dispat
pub fn dispatchOpts(self: *EventManager, target: *EventTarget, event: *Event, comptime opts: DispatchOpts) DispatchError!void {
event.acquireRef();
defer event.deinit(false, self.page);
defer _ = event.releaseRef(self.page._session);
if (comptime IS_DEBUG) {
log.debug(.event, "eventManager.dispatch", .{ .type = event._type_string.str(), .bubbles = event._bubbles });
@@ -233,8 +233,14 @@ const DispatchDirectOptions = struct {
pub fn dispatchDirect(self: *EventManager, target: *EventTarget, event: *Event, handler: anytype, comptime opts: DispatchDirectOptions) !void {
const page = self.page;
// Set window.event to the currently dispatching event (WHATWG spec)
const window = page.window;
const prev_event = window._current_event;
window._current_event = event;
defer window._current_event = prev_event;
event.acquireRef();
defer event.deinit(false, page);
defer _ = event.releaseRef(page._session);
if (comptime IS_DEBUG) {
log.debug(.event, "dispatchDirect", .{ .type = event._type_string, .context = opts.context });
@@ -365,6 +371,29 @@ fn getFunction(handler: anytype, local: *const js.Local) ?js.Function {
};
}
/// Check if there are any listeners for a direct dispatch (non-DOM target).
/// Use this to avoid creating an event when there are no listeners.
pub fn hasDirectListeners(self: *EventManager, target: *EventTarget, typ: []const u8, handler: anytype) bool {
if (hasHandler(handler)) {
return true;
}
return self.lookup.get(.{
.event_target = @intFromPtr(target),
.type_string = .wrap(typ),
}) != null;
}
fn hasHandler(handler: anytype) bool {
const ti = @typeInfo(@TypeOf(handler));
if (ti == .null) {
return false;
}
if (ti == .optional) {
return handler != null;
}
return true;
}
fn dispatchNode(self: *EventManager, target: *Node, event: *Event, comptime opts: DispatchOpts) !void {
const ShadowRoot = @import("webapi/ShadowRoot.zig");
@@ -375,6 +404,13 @@ fn dispatchNode(self: *EventManager, target: *Node, event: *Event, comptime opts
}
const page = self.page;
// Set window.event to the currently dispatching event (WHATWG spec)
const window = page.window;
const prev_event = window._current_event;
window._current_event = event;
defer window._current_event = prev_event;
var was_handled = false;
// Create a single scope for all event handlers in this dispatch.
@@ -389,7 +425,7 @@ fn dispatchNode(self: *EventManager, target: *Node, event: *Event, comptime opts
ls.deinit();
}
const activation_state = ActivationState.create(event, target, page);
const activation_state = try ActivationState.create(event, target, page);
// Defer runs even on early return - ensures event phase is reset
// and default actions execute (unless prevented)
@@ -784,7 +820,7 @@ const ActivationState = struct {
const Input = Element.Html.Input;
fn create(event: *const Event, target: *Node, page: *Page) ?ActivationState {
fn create(event: *const Event, target: *Node, page: *Page) !?ActivationState {
if (event._type_string.eql(comptime .wrap("click")) == false) {
return null;
}

View File

@@ -48,13 +48,11 @@ const Factory = @This();
_arena: Allocator,
_slab: SlabAllocator,
pub fn init(arena: Allocator) !*Factory {
const self = try arena.create(Factory);
self.* = .{
pub fn init(arena: Allocator) Factory {
return .{
._arena = arena,
._slab = SlabAllocator.init(arena, 128),
};
return self;
}
// this is a root object
@@ -241,7 +239,7 @@ fn eventInit(arena: Allocator, typ: String, value: anytype) !Event {
const time_stamp = (raw_timestamp / 2) * 2;
return .{
._rc = 0,
._rc = .{},
._arena = arena,
._type = unionInit(Event.Type, value),
._type_string = typ,
@@ -249,16 +247,16 @@ fn eventInit(arena: Allocator, typ: String, value: anytype) !Event {
};
}
pub fn blob(self: *Factory, child: anytype) !*@TypeOf(child) {
const allocator = self._slab.allocator();
pub fn blob(_: *const Factory, arena: Allocator, child: anytype) !*@TypeOf(child) {
// Special case: Blob has slice and mime fields, so we need manual setup
const chain = try PrototypeChain(
&.{ Blob, @TypeOf(child) },
).allocate(allocator);
).allocate(arena);
const blob_ptr = chain.get(0);
blob_ptr.* = .{
._rc = .{},
._arena = arena,
._type = unionInit(Blob.Type, chain.get(1)),
._slice = "",
._mime = "",
@@ -268,19 +266,23 @@ pub fn blob(self: *Factory, child: anytype) !*@TypeOf(child) {
return chain.get(1);
}
pub fn abstractRange(self: *Factory, child: anytype, page: *Page) !*@TypeOf(child) {
const allocator = self._slab.allocator();
const chain = try PrototypeChain(&.{ AbstractRange, @TypeOf(child) }).allocate(allocator);
pub fn abstractRange(_: *const Factory, arena: Allocator, child: anytype, page: *Page) !*@TypeOf(child) {
const chain = try PrototypeChain(&.{ AbstractRange, @TypeOf(child) }).allocate(arena);
const doc = page.document.asNode();
chain.set(0, AbstractRange{
const abstract_range = chain.get(0);
abstract_range.* = AbstractRange{
._rc = .{},
._arena = arena,
._page_id = page.id,
._type = unionInit(AbstractRange.Type, chain.get(1)),
._end_offset = 0,
._start_offset = 0,
._end_container = doc,
._start_container = doc,
});
};
chain.setLeaf(1, child);
page._live_ranges.append(&abstract_range._range_link);
return chain.get(1);
}

View File

@@ -17,28 +17,27 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const log = @import("../log.zig");
const builtin = @import("builtin");
const posix = std.posix;
const Net = @import("../Net.zig");
const Config = @import("../Config.zig");
const lp = @import("lightpanda");
const log = @import("../log.zig");
const Net = @import("../network/http.zig");
const Network = @import("../network/Runtime.zig");
const URL = @import("../browser/URL.zig");
const Notification = @import("../Notification.zig");
const CookieJar = @import("../browser/webapi/storage/Cookie.zig").Jar;
const Robots = @import("../browser/Robots.zig");
const RobotStore = Robots.RobotStore;
const posix = std.posix;
const Robots = @import("../network/Robots.zig");
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const IS_DEBUG = builtin.mode == .Debug;
const Method = Net.Method;
const ResponseHead = Net.ResponseHead;
const HeaderIterator = Net.HeaderIterator;
pub const Method = Net.Method;
pub const Headers = Net.Headers;
pub const ResponseHead = Net.ResponseHead;
pub const HeaderIterator = Net.HeaderIterator;
// This is loosely tied to a browser Page. Loading all the <scripts>, doing
// XHR requests, and loading imports all happens through here. Sine the app
@@ -65,9 +64,18 @@ active: usize,
// 'networkAlmostIdle' Page.lifecycleEvent in CDP).
intercepted: usize,
// Our easy handles, managed by a curl multi.
// Our curl multi handle.
handles: Net.Handles,
// Connections currently in this client's curl_multi.
in_use: std.DoublyLinkedList = .{},
// Connections that failed to be removed from curl_multi during perform.
dirty: std.DoublyLinkedList = .{},
// Whether we're currently inside a curl_multi_perform call.
performing: bool = false,
// Use to generate the next request ID
next_request_id: u32 = 0,
@@ -77,8 +85,7 @@ queue: TransferQueue,
// The main app allocator
allocator: Allocator,
// Reference to the App-owned Robot Store.
robot_store: *RobotStore,
network: *Network,
// Queue of requests that depend on a robots.txt.
// Allows us to fetch the robots.txt just once.
pending_robots_queue: std.StringHashMapUnmanaged(std.ArrayList(Request)) = .empty,
@@ -88,8 +95,8 @@ pending_robots_queue: std.StringHashMapUnmanaged(std.ArrayList(Request)) = .empt
// request. These wil come and go with each request.
transfer_pool: std.heap.MemoryPool(Transfer),
// only needed for CDP which can change the proxy and then restore it. When
// restoring, this originally-configured value is what it goes to.
// The current proxy. CDP can change it, restoreOriginalProxy restores
// from config.
http_proxy: ?[:0]const u8 = null,
// track if the client use a proxy for connections.
@@ -97,7 +104,10 @@ http_proxy: ?[:0]const u8 = null,
// CDP.
use_proxy: bool,
config: *const Config,
// Current TLS verification state, applied per-connection in makeRequest.
tls_verify: bool = true,
obey_robots: bool,
cdp_client: ?CDPClient = null,
@@ -121,22 +131,17 @@ pub const CDPClient = struct {
const TransferQueue = std.DoublyLinkedList;
pub fn init(allocator: Allocator, ca_blob: ?Net.Blob, robot_store: *RobotStore, config: *const Config) !*Client {
pub fn init(allocator: Allocator, network: *Network) !*Client {
var transfer_pool = std.heap.MemoryPool(Transfer).init(allocator);
errdefer transfer_pool.deinit();
const client = try allocator.create(Client);
errdefer allocator.destroy(client);
var handles = try Net.Handles.init(allocator, ca_blob, config);
errdefer handles.deinit(allocator);
var handles = try Net.Handles.init(network.config);
errdefer handles.deinit();
// Set transfer callbacks on each connection.
for (handles.connections) |*conn| {
try conn.setCallbacks(Transfer.headerCallback, Transfer.dataCallback);
}
const http_proxy = config.httpProxy();
const http_proxy = network.config.httpProxy();
client.* = .{
.queue = .{},
@@ -144,10 +149,11 @@ pub fn init(allocator: Allocator, ca_blob: ?Net.Blob, robot_store: *RobotStore,
.intercepted = 0,
.handles = handles,
.allocator = allocator,
.robot_store = robot_store,
.network = network,
.http_proxy = http_proxy,
.use_proxy = http_proxy != null,
.config = config,
.tls_verify = network.config.tlsVerifyHost(),
.obey_robots = network.config.obeyRobots(),
.transfer_pool = transfer_pool,
};
@@ -156,7 +162,7 @@ pub fn init(allocator: Allocator, ca_blob: ?Net.Blob, robot_store: *RobotStore,
pub fn deinit(self: *Client) void {
self.abort();
self.handles.deinit(self.allocator);
self.handles.deinit();
self.transfer_pool.deinit();
@@ -170,7 +176,7 @@ pub fn deinit(self: *Client) void {
}
pub fn newHeaders(self: *const Client) !Net.Headers {
return Net.Headers.init(self.config.http_headers.user_agent_header);
return Net.Headers.init(self.network.config.http_headers.user_agent_header);
}
pub fn abort(self: *Client) void {
@@ -185,14 +191,14 @@ pub fn abortFrame(self: *Client, frame_id: u32) void {
// but abort can avoid the frame_id check at comptime.
fn _abort(self: *Client, comptime abort_all: bool, frame_id: u32) void {
{
var q = &self.handles.in_use;
var q = &self.in_use;
var n = q.first;
while (n) |node| {
n = node.next;
const conn: *Net.Connection = @fieldParentPtr("node", node);
var transfer = Transfer.fromConnection(conn) catch |err| {
// Let's cleanup what we can
self.handles.remove(conn);
self.removeConn(conn);
log.err(.http, "get private info", .{ .err = err, .source = "abort" });
continue;
};
@@ -229,8 +235,7 @@ fn _abort(self: *Client, comptime abort_all: bool, frame_id: u32) void {
}
if (comptime IS_DEBUG and abort_all) {
std.debug.assert(self.handles.in_use.first == null);
std.debug.assert(self.handles.available.len() == self.handles.connections.len);
std.debug.assert(self.in_use.first == null);
const running = self.handles.perform() catch |err| {
lp.assert(false, "multi perform in abort", .{ .err = err });
@@ -240,27 +245,27 @@ fn _abort(self: *Client, comptime abort_all: bool, frame_id: u32) void {
}
pub fn tick(self: *Client, timeout_ms: u32) !PerformStatus {
while (true) {
if (self.handles.hasAvailable() == false) {
while (self.queue.popFirst()) |queue_node| {
const conn = self.network.getConnection() orelse {
self.queue.prepend(queue_node);
break;
}
const queue_node = self.queue.popFirst() orelse break;
};
const transfer: *Transfer = @fieldParentPtr("_node", queue_node);
// we know this exists, because we checked hasAvailable() above
const conn = self.handles.get().?;
try self.makeRequest(conn, transfer);
}
return self.perform(@intCast(timeout_ms));
}
pub fn request(self: *Client, req: Request) !void {
if (self.config.obeyRobots()) {
if (self.obey_robots == false) {
return self.processRequest(req);
}
const robots_url = try URL.getRobotsUrl(self.allocator, req.url);
errdefer self.allocator.free(robots_url);
// If we have this robots cached, we can take a fast path.
if (self.robot_store.get(robots_url)) |robot_entry| {
if (self.network.robot_store.get(robots_url)) |robot_entry| {
defer self.allocator.free(robots_url);
switch (robot_entry) {
@@ -278,13 +283,9 @@ pub fn request(self: *Client, req: Request) !void {
return self.processRequest(req);
}
return self.fetchRobotsThenProcessRequest(robots_url, req);
}
return self.processRequest(req);
}
fn processRequest(self: *Client, req: Request) !void {
const transfer = try self.makeTransfer(req);
@@ -401,18 +402,18 @@ fn robotsDoneCallback(ctx_ptr: *anyopaque) !void {
switch (ctx.status) {
200 => {
if (ctx.buffer.items.len > 0) {
const robots: ?Robots = ctx.client.robot_store.robotsFromBytes(
ctx.client.config.http_headers.user_agent,
const robots: ?Robots = ctx.client.network.robot_store.robotsFromBytes(
ctx.client.network.config.http_headers.user_agent,
ctx.buffer.items,
) catch blk: {
log.warn(.browser, "failed to parse robots", .{ .robots_url = ctx.robots_url });
// If we fail to parse, we just insert it as absent and ignore.
try ctx.client.robot_store.putAbsent(ctx.robots_url);
try ctx.client.network.robot_store.putAbsent(ctx.robots_url);
break :blk null;
};
if (robots) |r| {
try ctx.client.robot_store.put(ctx.robots_url, r);
try ctx.client.network.robot_store.put(ctx.robots_url, r);
const path = URL.getPathname(ctx.req.url);
allowed = r.isAllowed(path);
}
@@ -421,12 +422,12 @@ fn robotsDoneCallback(ctx_ptr: *anyopaque) !void {
404 => {
log.debug(.http, "robots not found", .{ .url = ctx.robots_url });
// If we get a 404, we just insert it as absent.
try ctx.client.robot_store.putAbsent(ctx.robots_url);
try ctx.client.network.robot_store.putAbsent(ctx.robots_url);
},
else => {
log.debug(.http, "unexpected status on robots", .{ .url = ctx.robots_url, .status = ctx.status });
// If we get an unexpected status, we just insert as absent.
try ctx.client.robot_store.putAbsent(ctx.robots_url);
try ctx.client.network.robot_store.putAbsent(ctx.robots_url);
},
}
@@ -532,8 +533,8 @@ fn waitForInterceptedResponse(self: *Client, transfer: *Transfer) !bool {
fn process(self: *Client, transfer: *Transfer) !void {
// libcurl doesn't allow recursive calls, if we're in a `perform()` operation
// then we _have_ to queue this.
if (self.handles.performing == false) {
if (self.handles.get()) |conn| {
if (self.performing == false) {
if (self.network.getConnection()) |conn| {
return self.makeRequest(conn, transfer);
}
}
@@ -609,7 +610,7 @@ fn makeTransfer(self: *Client, req: Request) !*Transfer {
.req = req,
.ctx = req.ctx,
.client = self,
.max_response_size = self.config.httpMaxResponseSize(),
.max_response_size = self.network.config.httpMaxResponseSize(),
};
return transfer;
}
@@ -647,10 +648,7 @@ fn requestFailed(transfer: *Transfer, err: anyerror, comptime execute_callback:
// can be changed at any point in the easy's lifecycle.
pub fn changeProxy(self: *Client, proxy: [:0]const u8) !void {
try self.ensureNoActiveConnection();
for (self.handles.connections) |*conn| {
try conn.setProxy(proxy.ptr);
}
self.http_proxy = proxy;
self.use_proxy = true;
}
@@ -659,31 +657,21 @@ pub fn changeProxy(self: *Client, proxy: [:0]const u8) !void {
pub fn restoreOriginalProxy(self: *Client) !void {
try self.ensureNoActiveConnection();
const proxy = if (self.http_proxy) |p| p.ptr else null;
for (self.handles.connections) |*conn| {
try conn.setProxy(proxy);
}
self.use_proxy = proxy != null;
self.http_proxy = self.network.config.httpProxy();
self.use_proxy = self.http_proxy != null;
}
// Enable TLS verification on all connections.
pub fn enableTlsVerify(self: *Client) !void {
pub fn setTlsVerify(self: *Client, verify: bool) !void {
// Remove inflight connections check on enable TLS b/c chromiumoxide calls
// the command during navigate and Curl seems to accept it...
for (self.handles.connections) |*conn| {
try conn.setTlsVerify(true, self.use_proxy);
}
}
// Disable TLS verification on all connections.
pub fn disableTlsVerify(self: *Client) !void {
// Remove inflight connections check on disable TLS b/c chromiumoxide calls
// the command during navigate and Curl seems to accept it...
for (self.handles.connections) |*conn| {
try conn.setTlsVerify(false, self.use_proxy);
var it = self.in_use.first;
while (it) |node| : (it = node.next) {
const conn: *Net.Connection = @fieldParentPtr("node", node);
try conn.setTlsVerify(verify, self.use_proxy);
}
self.tls_verify = verify;
}
fn makeRequest(self: *Client, conn: *Net.Connection, transfer: *Transfer) anyerror!void {
@@ -694,9 +682,14 @@ fn makeRequest(self: *Client, conn: *Net.Connection, transfer: *Transfer) anyerr
errdefer {
transfer._conn = null;
transfer.deinit();
self.handles.isAvailable(conn);
self.releaseConn(conn);
}
// Set callbacks and per-client settings on the pooled connection.
try conn.setCallbacks(Transfer.headerCallback, Transfer.dataCallback);
try conn.setProxy(self.http_proxy);
try conn.setTlsVerify(self.tls_verify, self.use_proxy);
try conn.setURL(req.url);
try conn.setMethod(req.method);
if (req.body) |b| {
@@ -706,9 +699,15 @@ fn makeRequest(self: *Client, conn: *Net.Connection, transfer: *Transfer) anyerr
}
var header_list = req.headers;
try conn.secretHeaders(&header_list, &self.config.http_headers); // Add headers that must be hidden from intercepts
try conn.secretHeaders(&header_list, &self.network.config.http_headers); // Add headers that must be hidden from intercepts
try conn.setHeaders(&header_list);
// If we have WebBotAuth, sign our request.
if (self.network.web_bot_auth) |*wba| {
const authority = URL.getHost(req.url);
try wba.signRequest(transfer.arena.allocator(), &header_list, authority);
}
// Add cookies.
if (header_list.cookies) |cookies| {
try conn.setCookies(cookies);
@@ -718,7 +717,11 @@ fn makeRequest(self: *Client, conn: *Net.Connection, transfer: *Transfer) anyerr
// add credentials
if (req.credentials) |creds| {
if (transfer._auth_challenge != null and transfer._auth_challenge.?.source == .proxy) {
try conn.setProxyCredentials(creds);
} else {
try conn.setCredentials(creds);
}
}
}
@@ -727,10 +730,12 @@ fn makeRequest(self: *Client, conn: *Net.Connection, transfer: *Transfer) anyerr
// fails BEFORE `curl_multi_add_handle` succeeds, the we still need to do
// cleanup. But if things fail after `curl_multi_add_handle`, we expect
// perfom to pickup the failure and cleanup.
self.in_use.append(&conn.node);
self.handles.add(conn) catch |err| {
transfer._conn = null;
transfer.deinit();
self.handles.isAvailable(conn);
self.in_use.remove(&conn.node);
self.releaseConn(conn);
return err;
};
@@ -751,7 +756,22 @@ pub const PerformStatus = enum {
};
fn perform(self: *Client, timeout_ms: c_int) !PerformStatus {
const running = try self.handles.perform();
const running = blk: {
self.performing = true;
defer self.performing = false;
break :blk try self.handles.perform();
};
// Process dirty connections return them to Runtime pool.
while (self.dirty.popFirst()) |node| {
const conn: *Net.Connection = @fieldParentPtr("node", node);
self.handles.remove(conn) catch |err| {
log.fatal(.http, "multi remove handle", .{ .err = err, .src = "perform" });
@panic("multi_remove_handle");
};
self.releaseConn(conn);
}
// We're potentially going to block for a while until we get data. Process
// whatever messages we have waiting ahead of time.
@@ -825,14 +845,28 @@ fn processMessages(self: *Client) !bool {
}
}
// When the server sends "Connection: close" and closes the TLS
// connection without a close_notify alert, BoringSSL reports
// RecvError. If we already received valid HTTP headers, this is
// a normal end-of-body (the connection closure signals the end
// of the response per HTTP/1.1 when there is no Content-Length).
// We must check this before endTransfer, which may reset the
// easy handle.
const is_conn_close_recv = blk: {
const err = msg.err orelse break :blk false;
if (err != error.RecvError) break :blk false;
const hdr = msg.conn.getResponseHeader("connection", 0) orelse break :blk false;
break :blk std.ascii.eqlIgnoreCase(hdr.value, "close");
};
// release it ASAP so that it's available; some done_callbacks
// will load more resources.
self.endTransfer(transfer);
defer transfer.deinit();
if (msg.err) |err| {
requestFailed(transfer, err, true);
if (msg.err != null and !is_conn_close_recv) {
requestFailed(transfer, msg.err.?, true);
} else blk: {
// make sure the transfer can't be immediately aborted from a callback
// since we still need it here.
@@ -870,11 +904,26 @@ fn processMessages(self: *Client) !bool {
fn endTransfer(self: *Client, transfer: *Transfer) void {
const conn = transfer._conn.?;
self.handles.remove(conn);
self.removeConn(conn);
transfer._conn = null;
self.active -= 1;
}
fn removeConn(self: *Client, conn: *Net.Connection) void {
self.in_use.remove(&conn.node);
if (self.handles.remove(conn)) {
self.releaseConn(conn);
} else |_| {
// Can happen if we're in a perform() call, so we'll queue this
// for cleanup later.
self.dirty.append(&conn.node);
}
}
fn releaseConn(self: *Client, conn: *Net.Connection) void {
self.network.releaseConnection(conn);
}
fn ensureNoActiveConnection(self: *const Client) !void {
if (self.active > 0) {
return error.InflightConnection;
@@ -897,7 +946,7 @@ pub const RequestCookie = struct {
if (arr.items.len > 0) {
try arr.append(temp, 0); //null terminate
headers.cookies = @ptrCast(arr.items.ptr);
headers.cookies = @as([*c]const u8, @ptrCast(arr.items.ptr));
}
}
};
@@ -1022,7 +1071,7 @@ pub const Transfer = struct {
fn deinit(self: *Transfer) void {
self.req.headers.deinit();
if (self._conn) |conn| {
self.client.handles.remove(conn);
self.client.removeConn(conn);
}
self.arena.deinit();
self.client.transfer_pool.destroy(self);
@@ -1092,7 +1141,7 @@ pub const Transfer = struct {
requestFailed(self, err, true);
const client = self.client;
if (self._performing or client.handles.performing) {
if (self._performing or client.performing) {
// We're currently in a curl_multi_perform. We cannot call endTransfer
// as that calls curl_multi_remove_handle, and you can't do that
// from a curl callback. Instead, we flag this transfer and all of
@@ -1257,6 +1306,16 @@ pub const Transfer = struct {
if (buf_len < 3) {
// could be \r\n or \n.
// We get the last header line.
if (transfer._redirecting) {
// parse and set cookies for the redirection.
redirectionCookies(transfer, &conn) catch |err| {
if (comptime IS_DEBUG) {
log.debug(.http, "redirection cookies", .{ .err = err });
}
return 0;
};
}
return buf_len;
}
@@ -1323,7 +1382,6 @@ pub const Transfer = struct {
transfer.bytes_received += buf_len;
}
if (buf_len > 2) {
if (transfer._auth_challenge != null) {
// try to parse auth challenge.
if (std.ascii.startsWithIgnoreCase(header, "WWW-Authenticate") or
@@ -1341,21 +1399,6 @@ pub const Transfer = struct {
transfer._auth_challenge = ac;
}
}
return buf_len;
}
// Starting here, we get the last header line.
if (transfer._redirecting) {
// parse and set cookies for the redirection.
redirectionCookies(transfer, &conn) catch |err| {
if (comptime IS_DEBUG) {
log.debug(.http, "redirection cookies", .{ .err = err });
}
return 0;
};
return buf_len;
}
return buf_len;
}

View File

@@ -25,6 +25,7 @@ params: []const u8 = "",
// We keep 41 for null-termination since HTML parser expects in this format.
charset: [41]u8 = default_charset,
charset_len: usize = default_charset_len,
is_default_charset: bool = true,
/// String "UTF-8" continued by null characters.
const default_charset = .{ 'U', 'T', 'F', '-', '8' } ++ .{0} ** 36;
@@ -130,6 +131,7 @@ pub fn parse(input: []u8) !Mime {
var charset: [41]u8 = default_charset;
var charset_len: usize = default_charset_len;
var has_explicit_charset = false;
var it = std.mem.splitScalar(u8, params, ';');
while (it.next()) |attr| {
@@ -156,6 +158,7 @@ pub fn parse(input: []u8) !Mime {
// Null-terminate right after attribute value.
charset[attribute_value.len] = 0;
charset_len = attribute_value.len;
has_explicit_charset = true;
},
}
}
@@ -165,9 +168,137 @@ pub fn parse(input: []u8) !Mime {
.charset = charset,
.charset_len = charset_len,
.content_type = content_type,
.is_default_charset = !has_explicit_charset,
};
}
/// Prescan the first 1024 bytes of an HTML document for a charset declaration.
/// Looks for `<meta charset="X">` and `<meta http-equiv="Content-Type" content="...;charset=X">`.
/// Returns the charset value or null if none found.
/// See: https://www.w3.org/International/questions/qa-html-encoding-declarations
pub fn prescanCharset(html: []const u8) ?[]const u8 {
const limit = @min(html.len, 1024);
const data = html[0..limit];
// Scan for <meta tags
var pos: usize = 0;
while (pos < data.len) {
// Find next '<'
pos = std.mem.indexOfScalarPos(u8, data, pos, '<') orelse return null;
pos += 1;
if (pos >= data.len) return null;
// Check for "meta" (case-insensitive)
if (pos + 4 >= data.len) return null;
var tag_buf: [4]u8 = undefined;
_ = std.ascii.lowerString(&tag_buf, data[pos..][0..4]);
if (!std.mem.eql(u8, &tag_buf, "meta")) {
continue;
}
pos += 4;
// Must be followed by whitespace or end of tag
if (pos >= data.len) return null;
if (data[pos] != ' ' and data[pos] != '\t' and data[pos] != '\n' and
data[pos] != '\r' and data[pos] != '/')
{
continue;
}
// Scan attributes within this meta tag
const tag_end = std.mem.indexOfScalarPos(u8, data, pos, '>') orelse return null;
const attrs = data[pos..tag_end];
// Look for charset= attribute directly
if (findAttrValue(attrs, "charset")) |charset| {
if (charset.len > 0 and charset.len <= 40) return charset;
}
// Look for http-equiv="content-type" with content="...;charset=X"
if (findAttrValue(attrs, "http-equiv")) |he| {
if (std.ascii.eqlIgnoreCase(he, "content-type")) {
if (findAttrValue(attrs, "content")) |content| {
if (extractCharsetFromContentType(content)) |charset| {
return charset;
}
}
}
}
pos = tag_end + 1;
}
return null;
}
fn findAttrValue(attrs: []const u8, name: []const u8) ?[]const u8 {
var pos: usize = 0;
while (pos < attrs.len) {
// Skip whitespace
while (pos < attrs.len and (attrs[pos] == ' ' or attrs[pos] == '\t' or
attrs[pos] == '\n' or attrs[pos] == '\r'))
{
pos += 1;
}
if (pos >= attrs.len) return null;
// Read attribute name
const attr_start = pos;
while (pos < attrs.len and attrs[pos] != '=' and attrs[pos] != ' ' and
attrs[pos] != '\t' and attrs[pos] != '>' and attrs[pos] != '/')
{
pos += 1;
}
const attr_name = attrs[attr_start..pos];
// Skip whitespace around =
while (pos < attrs.len and (attrs[pos] == ' ' or attrs[pos] == '\t')) pos += 1;
if (pos >= attrs.len or attrs[pos] != '=') {
// No '=' found - skip this token. Advance at least one byte to avoid infinite loop.
if (pos == attr_start) pos += 1;
continue;
}
pos += 1; // skip '='
while (pos < attrs.len and (attrs[pos] == ' ' or attrs[pos] == '\t')) pos += 1;
if (pos >= attrs.len) return null;
// Read attribute value
const value = blk: {
if (attrs[pos] == '"' or attrs[pos] == '\'') {
const quote = attrs[pos];
pos += 1;
const val_start = pos;
while (pos < attrs.len and attrs[pos] != quote) pos += 1;
const val = attrs[val_start..pos];
if (pos < attrs.len) pos += 1; // skip closing quote
break :blk val;
} else {
const val_start = pos;
while (pos < attrs.len and attrs[pos] != ' ' and attrs[pos] != '\t' and
attrs[pos] != '>' and attrs[pos] != '/')
{
pos += 1;
}
break :blk attrs[val_start..pos];
}
};
if (std.ascii.eqlIgnoreCase(attr_name, name)) return value;
}
return null;
}
fn extractCharsetFromContentType(content: []const u8) ?[]const u8 {
var it = std.mem.splitScalar(u8, content, ';');
while (it.next()) |part| {
const trimmed = std.mem.trimLeft(u8, part, &.{ ' ', '\t' });
if (trimmed.len > 8 and std.ascii.eqlIgnoreCase(trimmed[0..8], "charset=")) {
const val = std.mem.trim(u8, trimmed[8..], &.{ ' ', '\t', '"', '\'' });
if (val.len > 0 and val.len <= 40) return val;
}
}
return null;
}
pub fn sniff(body: []const u8) ?Mime {
// 0x0C is form feed
const content = std.mem.trimLeft(u8, body, &.{ ' ', '\t', '\n', '\r', 0x0C });
@@ -178,15 +309,30 @@ pub fn sniff(body: []const u8) ?Mime {
if (content[0] != '<') {
if (std.mem.startsWith(u8, content, &.{ 0xEF, 0xBB, 0xBF })) {
// UTF-8 BOM
return .{ .content_type = .{ .text_plain = {} } };
return .{
.content_type = .{ .text_plain = {} },
.charset = default_charset,
.charset_len = default_charset_len,
.is_default_charset = false,
};
}
if (std.mem.startsWith(u8, content, &.{ 0xFE, 0xFF })) {
// UTF-16 big-endian BOM
return .{ .content_type = .{ .text_plain = {} } };
return .{
.content_type = .{ .text_plain = {} },
.charset = .{ 'U', 'T', 'F', '-', '1', '6', 'B', 'E' } ++ .{0} ** 33,
.charset_len = 8,
.is_default_charset = false,
};
}
if (std.mem.startsWith(u8, content, &.{ 0xFF, 0xFE })) {
// UTF-16 little-endian BOM
return .{ .content_type = .{ .text_plain = {} } };
return .{
.content_type = .{ .text_plain = {} },
.charset = .{ 'U', 'T', 'F', '-', '1', '6', 'L', 'E' } ++ .{0} ** 33,
.charset_len = 8,
.is_default_charset = false,
};
}
return null;
}
@@ -240,6 +386,14 @@ pub fn isHTML(self: *const Mime) bool {
return self.content_type == .text_html;
}
pub fn isText(mime: *const Mime) bool {
return switch (mime.content_type) {
.text_xml, .text_html, .text_javascript, .text_plain, .text_css => true,
.application_json => true,
else => false,
};
}
// we expect value to be lowercase
fn parseContentType(value: []const u8) !struct { ContentType, usize } {
const end = std.mem.indexOfScalarPos(u8, value, 0, ';') orelse value.len;
@@ -540,6 +694,24 @@ test "Mime: sniff" {
try expectHTML("<!-->");
try expectHTML(" \n\t <!-->");
{
const mime = Mime.sniff(&.{ 0xEF, 0xBB, 0xBF }).?;
try testing.expectEqual(.text_plain, std.meta.activeTag(mime.content_type));
try testing.expectEqual("UTF-8", mime.charsetString());
}
{
const mime = Mime.sniff(&.{ 0xFE, 0xFF }).?;
try testing.expectEqual(.text_plain, std.meta.activeTag(mime.content_type));
try testing.expectEqual("UTF-16BE", mime.charsetString());
}
{
const mime = Mime.sniff(&.{ 0xFF, 0xFE }).?;
try testing.expectEqual(.text_plain, std.meta.activeTag(mime.content_type));
try testing.expectEqual("UTF-16LE", mime.charsetString());
}
}
const Expectation = struct {
@@ -576,3 +748,35 @@ fn expect(expected: Expectation, input: []const u8) !void {
try testing.expectEqual(m.charsetStringZ(), actual.charsetStringZ());
}
}
test "Mime: prescanCharset" {
// <meta charset="X">
try testing.expectEqual("utf-8", Mime.prescanCharset("<html><head><meta charset=\"utf-8\">").?);
try testing.expectEqual("iso-8859-1", Mime.prescanCharset("<html><head><meta charset=\"iso-8859-1\">").?);
try testing.expectEqual("shift_jis", Mime.prescanCharset("<meta charset='shift_jis'>").?);
// Case-insensitive tag matching
try testing.expectEqual("utf-8", Mime.prescanCharset("<META charset=\"utf-8\">").?);
try testing.expectEqual("utf-8", Mime.prescanCharset("<Meta charset=\"utf-8\">").?);
// <meta http-equiv="Content-Type" content="text/html; charset=X">
try testing.expectEqual(
"iso-8859-1",
Mime.prescanCharset("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">").?,
);
// No charset found
try testing.expectEqual(null, Mime.prescanCharset("<html><head><title>Test</title>"));
try testing.expectEqual(null, Mime.prescanCharset(""));
try testing.expectEqual(null, Mime.prescanCharset("no html here"));
// Self-closing meta without charset must not loop forever
try testing.expectEqual(null, Mime.prescanCharset("<meta foo=\"bar\"/>"));
// Charset after 1024 bytes should not be found
var long_html: [1100]u8 = undefined;
@memset(&long_html, ' ');
const suffix = "<meta charset=\"windows-1252\">";
@memcpy(long_html[1050 .. 1050 + suffix.len], suffix);
try testing.expectEqual(null, Mime.prescanCharset(&long_html));
}

File diff suppressed because it is too large Load Diff

238
src/browser/Runner.zig Normal file
View File

@@ -0,0 +1,238 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const builtin = @import("builtin");
const log = @import("../log.zig");
const Page = @import("Page.zig");
const Session = @import("Session.zig");
const HttpClient = @import("HttpClient.zig");
const IS_DEBUG = builtin.mode == .Debug;
const Runner = @This();
page: *Page,
session: *Session,
http_client: *HttpClient,
pub const Opts = struct {};
pub fn init(session: *Session, _: Opts) !Runner {
const page = &(session.page orelse return error.NoPage);
return .{
.page = page,
.session = session,
.http_client = session.browser.http_client,
};
}
pub const WaitOpts = struct {
ms: u32,
until: lp.Config.WaitUntil = .done,
};
pub fn wait(self: *Runner, opts: WaitOpts) !void {
_ = try self._wait(false, opts);
}
pub const CDPWaitResult = enum {
done,
cdp_socket,
};
pub fn waitCDP(self: *Runner, opts: WaitOpts) !CDPWaitResult {
return self._wait(true, opts);
}
fn _wait(self: *Runner, comptime is_cdp: bool, opts: WaitOpts) !CDPWaitResult {
var timer = try std.time.Timer.start();
var ms_remaining = opts.ms;
const tick_opts = TickOpts{
.ms = 200,
.until = opts.until,
};
while (true) {
const tick_result = self._tick(is_cdp, tick_opts) catch |err| {
switch (err) {
error.JsError => {}, // already logged (with hopefully more context)
else => log.err(.browser, "session wait", .{
.err = err,
.url = self.page.url,
}),
}
return err;
};
const next_ms = switch (tick_result) {
.ok => |next_ms| next_ms,
.done => return .done,
.cdp_socket => if (comptime is_cdp) return .cdp_socket else unreachable,
};
const ms_elapsed = timer.lap() / 1_000_000;
if (ms_elapsed >= ms_remaining) {
return .done;
}
ms_remaining -= @intCast(ms_elapsed);
if (next_ms > 0) {
std.Thread.sleep(std.time.ns_per_ms * next_ms);
}
}
}
pub const TickOpts = struct {
ms: u32,
until: lp.Config.WaitUntil = .done,
};
pub const TickResult = union(enum) {
done,
ok: u32,
};
pub fn tick(self: *Runner, opts: TickOpts) !TickResult {
return switch (try self._tick(false, opts)) {
.ok => |ms| .{ .ok = ms },
.done => .done,
.cdp_socket => unreachable,
};
}
pub const CDPTickResult = union(enum) {
done,
cdp_socket,
ok: u32,
};
pub fn tickCDP(self: *Runner, opts: TickOpts) !CDPTickResult {
return self._tick(true, opts);
}
fn _tick(self: *Runner, comptime is_cdp: bool, opts: TickOpts) !CDPTickResult {
const page = self.page;
const http_client = self.http_client;
switch (page._parse_state) {
.pre, .raw, .text, .image => {
// The main page hasn't started/finished navigating.
// There's no JS to run, and no reason to run the scheduler.
if (http_client.active == 0 and (comptime is_cdp) == false) {
// haven't started navigating, I guess.
return .done;
}
// Either we have active http connections, or we're in CDP
// mode with an extra socket. Either way, we're waiting
// for http traffic
const http_result = try http_client.tick(@intCast(opts.ms));
if ((comptime is_cdp) and http_result == .cdp_socket) {
return .cdp_socket;
}
return .{ .ok = 0 };
},
.html, .complete => {
const session = self.session;
if (session.queued_navigation.items.len != 0) {
try session.processQueuedNavigation();
self.page = &session.page.?; // might have changed
return .{ .ok = 0 };
}
const browser = session.browser;
// The HTML page was parsed. We now either have JS scripts to
// download, or scheduled tasks to execute, or both.
// scheduler.run could trigger new http transfers, so do not
// store http_client.active BEFORE this call and then use
// it AFTER.
try browser.runMacrotasks();
// Each call to this runs scheduled load events.
try page.dispatchLoad();
const http_active = http_client.active;
const total_network_activity = http_active + http_client.intercepted;
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
page.notifyNetworkAlmostIdle();
}
if (page._notified_network_idle.check(total_network_activity == 0)) {
page.notifyNetworkIdle();
}
if (http_active == 0 and (comptime is_cdp == false)) {
// we don't need to consider http_client.intercepted here
// because is_cdp is true, and that can only be
// the case when interception isn't possible.
if (comptime IS_DEBUG) {
std.debug.assert(http_client.intercepted == 0);
}
if (browser.hasBackgroundTasks()) {
// _we_ have nothing to run, but v8 is working on
// background tasks. We'll wait for them.
browser.waitForBackgroundTasks();
}
switch (opts.until) {
.done => {},
.domcontentloaded => if (page._load_state == .load or page._load_state == .complete) {
return .done;
},
.load => if (page._load_state == .complete) {
return .done;
},
.networkidle => if (page._notified_network_idle == .done) {
return .done;
},
}
// We never advertise a wait time of more than 20, there can
// always be new background tasks to run.
if (browser.msToNextMacrotask()) |ms_to_next_task| {
return .{ .ok = @min(ms_to_next_task, 20) };
}
return .done;
}
// We're here because we either have active HTTP
// connections, or is_cdp == false (aka, there's
// an cdp_socket registered with the http client).
// We should continue to run tasks, so we minimize how long
// we'll poll for network I/O.
var ms_to_wait = @min(opts.ms, browser.msToNextMacrotask() orelse 200);
if (ms_to_wait > 10 and browser.hasBackgroundTasks()) {
// if we have background tasks, we don't want to wait too
// long for a message from the client. We want to go back
// to the top of the loop and run macrotasks.
ms_to_wait = 10;
}
const http_result = try http_client.tick(@intCast(@min(opts.ms, ms_to_wait)));
if ((comptime is_cdp) and http_result == .cdp_socket) {
return .cdp_socket;
}
return .{ .ok = 0 };
},
.err => |err| {
page._parse_state = .{ .raw_done = @errorName(err) };
return err;
},
.raw_done => return .done,
}
}

View File

@@ -21,18 +21,17 @@ const lp = @import("lightpanda");
const builtin = @import("builtin");
const log = @import("../log.zig");
const Http = @import("../http/Http.zig");
const HttpClient = @import("HttpClient.zig");
const net_http = @import("../network/http.zig");
const String = @import("../string.zig").String;
const js = @import("js/js.zig");
const URL = @import("URL.zig");
const Page = @import("Page.zig");
const Browser = @import("Browser.zig");
const Element = @import("webapi/Element.zig");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const IS_DEBUG = builtin.mode == .Debug;
@@ -60,11 +59,8 @@ ready_scripts: std.DoublyLinkedList,
shutdown: bool = false,
client: *Http.Client,
client: *HttpClient,
allocator: Allocator,
buffer_pool: BufferPool,
script_pool: std.heap.MemoryPool(Script),
// We can download multiple sync modules in parallel, but we want to process
// them in order. We can't use an std.DoublyLinkedList, like the other script types,
@@ -88,7 +84,7 @@ importmap: std.StringHashMapUnmanaged([:0]const u8),
// event).
page_notified_of_completion: bool,
pub fn init(allocator: Allocator, http_client: *Http.Client, page: *Page) ScriptManager {
pub fn init(allocator: Allocator, http_client: *HttpClient, page: *Page) ScriptManager {
return .{
.page = page,
.async_scripts = .{},
@@ -100,18 +96,14 @@ pub fn init(allocator: Allocator, http_client: *Http.Client, page: *Page) Script
.imported_modules = .empty,
.client = http_client,
.static_scripts_done = false,
.buffer_pool = BufferPool.init(allocator, 5),
.page_notified_of_completion = false,
.script_pool = std.heap.MemoryPool(Script).init(allocator),
};
}
pub fn deinit(self: *ScriptManager) void {
// necessary to free any buffers scripts may be referencing
// necessary to free any arenas scripts may be referencing
self.reset();
self.buffer_pool.deinit();
self.script_pool.deinit();
self.imported_modules.deinit(self.allocator);
// we don't deinit self.importmap b/c we use the page's arena for its
// allocations.
@@ -120,7 +112,10 @@ pub fn deinit(self: *ScriptManager) void {
pub fn reset(self: *ScriptManager) void {
var it = self.imported_modules.valueIterator();
while (it.next()) |value_ptr| {
self.buffer_pool.release(value_ptr.buffer);
switch (value_ptr.state) {
.done => |script| script.deinit(),
else => {},
}
}
self.imported_modules.clearRetainingCapacity();
@@ -137,13 +132,13 @@ pub fn reset(self: *ScriptManager) void {
fn clearList(list: *std.DoublyLinkedList) void {
while (list.popFirst()) |n| {
const script: *Script = @fieldParentPtr("node", n);
script.deinit(true);
script.deinit();
}
}
pub fn getHeaders(self: *ScriptManager, url: [:0]const u8) !Http.Headers {
fn getHeaders(self: *ScriptManager, arena: Allocator, url: [:0]const u8) !net_http.Headers {
var headers = try self.client.newHeaders();
try self.page.headersForRequest(self.page.arena, url, &headers);
try self.page.headersForRequest(arena, url, &headers);
return headers;
}
@@ -158,7 +153,6 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
// <script> has already been processed.
return;
}
script_element._executed = true;
const element = script_element.asElement();
if (element.getAttributeSafe(comptime .wrap("nomodule")) != null) {
@@ -191,30 +185,48 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
return;
};
var handover = false;
const page = self.page;
const arena = try page.getArena(.{ .debug = "addFromElement" });
errdefer if (!handover) {
page.releaseArena(arena);
};
var source: Script.Source = undefined;
var remote_url: ?[:0]const u8 = null;
const base_url = page.base();
if (element.getAttributeSafe(comptime .wrap("src"))) |src| {
if (try parseDataURI(page.arena, src)) |data_uri| {
if (try parseDataURI(arena, src)) |data_uri| {
source = .{ .@"inline" = data_uri };
} else {
remote_url = try URL.resolve(page.arena, base_url, src, .{});
remote_url = try URL.resolve(arena, base_url, src, .{});
source = .{ .remote = .{} };
}
} else {
const inline_source = try element.asNode().getTextContentAlloc(page.arena);
var buf = std.Io.Writer.Allocating.init(arena);
try element.asNode().getChildTextContent(&buf.writer);
try buf.writer.writeByte(0);
const data = buf.written();
const inline_source: [:0]const u8 = data[0 .. data.len - 1 :0];
if (inline_source.len == 0) {
// we haven't set script_element._executed = true yet, which is good.
// If content is appended to the script, we will execute it then.
page.releaseArena(arena);
return;
}
source = .{ .@"inline" = inline_source };
}
const script = try self.script_pool.create();
errdefer self.script_pool.destroy(script);
// Only set _executed (already-started) when we actually have content to execute
script_element._executed = true;
const is_inline = source == .@"inline";
const script = try arena.create(Script);
script.* = .{
.kind = kind,
.node = .{},
.arena = arena,
.manager = self,
.source = source,
.script_element = script_element,
@@ -258,7 +270,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
if (is_blocking == false) {
self.scriptList(script).remove(&script.node);
}
script.deinit(true);
// Let the outer errdefer handle releasing the arena if client.request fails
}
try self.client.request(.{
@@ -266,7 +278,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
.ctx = script,
.method = .GET,
.frame_id = page._frame_id,
.headers = try self.getHeaders(url),
.headers = try self.getHeaders(arena, url),
.blocking = is_blocking,
.cookie_jar = &page._session.cookie_jar,
.resource_type = .script,
@@ -277,6 +289,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
.done_callback = Script.doneCallback,
.error_callback = Script.errorCallback,
});
handover = true;
if (comptime IS_DEBUG) {
var ls: js.Local.Scope = undefined;
@@ -306,7 +319,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
}
if (script.status == 0) {
// an error (that we already logged)
script.deinit(true);
script.deinit();
return;
}
@@ -315,7 +328,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
self.is_evaluating = true;
defer {
self.is_evaluating = was_evaluating;
script.deinit(true);
script.deinit();
}
return script.eval(page);
}
@@ -347,11 +360,14 @@ pub fn preloadImport(self: *ScriptManager, url: [:0]const u8, referrer: []const
}
errdefer _ = self.imported_modules.remove(url);
const script = try self.script_pool.create();
errdefer self.script_pool.destroy(script);
const page = self.page;
const arena = try page.getArena(.{ .debug = "preloadImport" });
errdefer page.releaseArena(arena);
const script = try arena.create(Script);
script.* = .{
.kind = .module,
.arena = arena,
.url = url,
.node = .{},
.manager = self,
@@ -361,11 +377,7 @@ pub fn preloadImport(self: *ScriptManager, url: [:0]const u8, referrer: []const
.mode = .import,
};
gop.value_ptr.* = ImportedModule{
.manager = self,
};
const page = self.page;
gop.value_ptr.* = ImportedModule{};
if (comptime IS_DEBUG) {
var ls: js.Local.Scope = undefined;
@@ -380,12 +392,18 @@ pub fn preloadImport(self: *ScriptManager, url: [:0]const u8, referrer: []const
});
}
try self.client.request(.{
// This seems wrong since we're not dealing with an async import (unlike
// getAsyncModule below), but all we're trying to do here is pre-load the
// script for execution at some point in the future (when waitForImport is
// called).
self.async_scripts.append(&script.node);
self.client.request(.{
.url = url,
.ctx = script,
.method = .GET,
.frame_id = page._frame_id,
.headers = try self.getHeaders(url),
.headers = try self.getHeaders(arena, url),
.cookie_jar = &page._session.cookie_jar,
.resource_type = .script,
.notification = page._session.notification,
@@ -394,13 +412,10 @@ pub fn preloadImport(self: *ScriptManager, url: [:0]const u8, referrer: []const
.data_callback = Script.dataCallback,
.done_callback = Script.doneCallback,
.error_callback = Script.errorCallback,
});
// This seems wrong since we're not dealing with an async import (unlike
// getAsyncModule below), but all we're trying to do here is pre-load the
// script for execution at some point in the future (when waitForImport is
// called).
self.async_scripts.append(&script.node);
}) catch |err| {
self.async_scripts.remove(&script.node);
return err;
};
}
pub fn waitForImport(self: *ScriptManager, url: [:0]const u8) !ModuleSource {
@@ -421,12 +436,12 @@ pub fn waitForImport(self: *ScriptManager, url: [:0]const u8) !ModuleSource {
_ = try client.tick(200);
continue;
},
.done => {
.done => |script| {
var shared = false;
const buffer = entry.value_ptr.buffer;
const waiters = entry.value_ptr.waiters;
if (waiters == 0) {
if (waiters == 1) {
self.imported_modules.removeByPtr(entry.key_ptr);
} else {
shared = true;
@@ -435,7 +450,7 @@ pub fn waitForImport(self: *ScriptManager, url: [:0]const u8) !ModuleSource {
return .{
.buffer = buffer,
.shared = shared,
.buffer_pool = &self.buffer_pool,
.script = script,
};
},
.err => return error.Failed,
@@ -444,11 +459,14 @@ pub fn waitForImport(self: *ScriptManager, url: [:0]const u8) !ModuleSource {
}
pub fn getAsyncImport(self: *ScriptManager, url: [:0]const u8, cb: ImportAsync.Callback, cb_data: *anyopaque, referrer: []const u8) !void {
const script = try self.script_pool.create();
errdefer self.script_pool.destroy(script);
const page = self.page;
const arena = try page.getArena(.{ .debug = "getAsyncImport" });
errdefer page.releaseArena(arena);
const script = try arena.create(Script);
script.* = .{
.kind = .module,
.arena = arena,
.url = url,
.node = .{},
.manager = self,
@@ -461,7 +479,6 @@ pub fn getAsyncImport(self: *ScriptManager, url: [:0]const u8, cb: ImportAsync.C
} },
};
const page = self.page;
if (comptime IS_DEBUG) {
var ls: js.Local.Scope = undefined;
page.js.localScope(&ls);
@@ -484,11 +501,12 @@ pub fn getAsyncImport(self: *ScriptManager, url: [:0]const u8, cb: ImportAsync.C
self.is_evaluating = true;
defer self.is_evaluating = was_evaluating;
try self.client.request(.{
self.async_scripts.append(&script.node);
self.client.request(.{
.url = url,
.method = .GET,
.frame_id = page._frame_id,
.headers = try self.getHeaders(url),
.headers = try self.getHeaders(arena, url),
.ctx = script,
.resource_type = .script,
.cookie_jar = &page._session.cookie_jar,
@@ -498,9 +516,10 @@ pub fn getAsyncImport(self: *ScriptManager, url: [:0]const u8, cb: ImportAsync.C
.data_callback = Script.dataCallback,
.done_callback = Script.doneCallback,
.error_callback = Script.errorCallback,
});
self.async_scripts.append(&script.node);
}) catch |err| {
self.async_scripts.remove(&script.node);
return err;
};
}
// Called from the Page to let us know it's done parsing the HTML. Necessary that
@@ -525,18 +544,18 @@ fn evaluate(self: *ScriptManager) void {
var script: *Script = @fieldParentPtr("node", n);
switch (script.mode) {
.async => {
defer script.deinit(true);
defer script.deinit();
script.eval(page);
},
.import_async => |ia| {
defer script.deinit(false);
if (script.status < 200 or script.status > 299) {
script.deinit();
ia.callback(ia.data, error.FailedToLoad);
} else {
ia.callback(ia.data, .{
.shared = false,
.script = script,
.buffer = script.source.remote,
.buffer_pool = &self.buffer_pool,
});
}
},
@@ -562,7 +581,7 @@ fn evaluate(self: *ScriptManager) void {
}
defer {
_ = self.defer_scripts.popFirst();
script.deinit(true);
script.deinit();
}
script.eval(page);
}
@@ -613,11 +632,12 @@ fn parseImportmap(self: *ScriptManager, script: *const Script) !void {
}
pub const Script = struct {
complete: bool,
kind: Kind,
complete: bool,
status: u16 = 0,
source: Source,
url: []const u8,
arena: Allocator,
mode: ExecutionMode,
node: std.DoublyLinkedList.Node,
script_element: ?*Element.Html.Script,
@@ -668,18 +688,15 @@ pub const Script = struct {
import_async: ImportAsync,
};
fn deinit(self: *Script, comptime release_buffer: bool) void {
if ((comptime release_buffer) and self.source == .remote) {
self.manager.buffer_pool.release(self.source.remote);
}
self.manager.script_pool.destroy(self);
fn deinit(self: *Script) void {
self.manager.page.releaseArena(self.arena);
}
fn startCallback(transfer: *Http.Transfer) !void {
fn startCallback(transfer: *HttpClient.Transfer) !void {
log.debug(.http, "script fetch start", .{ .req = transfer });
}
fn headerCallback(transfer: *Http.Transfer) !bool {
fn headerCallback(transfer: *HttpClient.Transfer) !bool {
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
const header = &transfer.response_header.?;
self.status = header.status;
@@ -738,23 +755,23 @@ pub const Script = struct {
}
lp.assert(self.source.remote.capacity == 0, "ScriptManager.Header buffer", .{ .capacity = self.source.remote.capacity });
var buffer = self.manager.buffer_pool.get();
var buffer: std.ArrayList(u8) = .empty;
if (transfer.getContentLength()) |cl| {
try buffer.ensureTotalCapacity(self.manager.allocator, cl);
try buffer.ensureTotalCapacity(self.arena, cl);
}
self.source = .{ .remote = buffer };
return true;
}
fn dataCallback(transfer: *Http.Transfer, data: []const u8) !void {
fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
const self: *Script = @ptrCast(@alignCast(transfer.ctx));
self._dataCallback(transfer, data) catch |err| {
log.err(.http, "SM.dataCallback", .{ .err = err, .transfer = transfer, .len = data.len });
return err;
};
}
fn _dataCallback(self: *Script, _: *Http.Transfer, data: []const u8) !void {
try self.source.remote.appendSlice(self.manager.allocator, data);
fn _dataCallback(self: *Script, _: *HttpClient.Transfer, data: []const u8) !void {
try self.source.remote.appendSlice(self.arena, data);
}
fn doneCallback(ctx: *anyopaque) !void {
@@ -771,9 +788,8 @@ pub const Script = struct {
} else if (self.mode == .import) {
manager.async_scripts.remove(&self.node);
const entry = manager.imported_modules.getPtr(self.url).?;
entry.state = .done;
entry.state = .{ .done = self };
entry.buffer = self.source.remote;
self.deinit(false);
}
manager.evaluate();
}
@@ -799,7 +815,7 @@ pub const Script = struct {
const manager = self.manager;
manager.scriptList(self).remove(&self.node);
if (manager.shutdown) {
self.deinit(true);
self.deinit();
return;
}
@@ -811,7 +827,7 @@ pub const Script = struct {
},
else => {},
}
self.deinit(true);
self.deinit();
manager.evaluate();
}
@@ -939,76 +955,6 @@ pub const Script = struct {
}
};
const BufferPool = struct {
count: usize,
available: List = .{},
allocator: Allocator,
max_concurrent_transfers: u8,
mem_pool: std.heap.MemoryPool(Container),
const List = std.SinglyLinkedList;
const Container = struct {
node: List.Node,
buf: std.ArrayList(u8),
};
fn init(allocator: Allocator, max_concurrent_transfers: u8) BufferPool {
return .{
.available = .{},
.count = 0,
.allocator = allocator,
.max_concurrent_transfers = max_concurrent_transfers,
.mem_pool = std.heap.MemoryPool(Container).init(allocator),
};
}
fn deinit(self: *BufferPool) void {
const allocator = self.allocator;
var node = self.available.first;
while (node) |n| {
const container: *Container = @fieldParentPtr("node", n);
container.buf.deinit(allocator);
node = n.next;
}
self.mem_pool.deinit();
}
fn get(self: *BufferPool) std.ArrayList(u8) {
const node = self.available.popFirst() orelse {
// return a new buffer
return .{};
};
self.count -= 1;
const container: *Container = @fieldParentPtr("node", node);
defer self.mem_pool.destroy(container);
return container.buf;
}
fn release(self: *BufferPool, buffer: ArrayList(u8)) void {
// create mutable copy
var b = buffer;
if (self.count == self.max_concurrent_transfers) {
b.deinit(self.allocator);
return;
}
const container = self.mem_pool.create() catch |err| {
b.deinit(self.allocator);
log.err(.http, "SM BufferPool release", .{ .err = err });
return;
};
b.clearRetainingCapacity();
container.* = .{ .buf = b, .node = .{} };
self.count += 1;
self.available.prepend(&container.node);
}
};
const ImportAsync = struct {
data: *anyopaque,
callback: ImportAsync.Callback,
@@ -1018,12 +964,12 @@ const ImportAsync = struct {
pub const ModuleSource = struct {
shared: bool,
buffer_pool: *BufferPool,
script: *Script,
buffer: std.ArrayList(u8),
pub fn deinit(self: *ModuleSource) void {
if (self.shared == false) {
self.buffer_pool.release(self.buffer);
self.script.deinit();
}
}
@@ -1033,15 +979,14 @@ pub const ModuleSource = struct {
};
const ImportedModule = struct {
manager: *ScriptManager,
waiters: u16 = 1,
state: State = .loading,
buffer: std.ArrayList(u8) = .{},
waiters: u16 = 1,
const State = enum {
const State = union(enum) {
err,
done,
loading,
done: *Script,
};
};

View File

@@ -21,69 +21,131 @@ const lp = @import("lightpanda");
const builtin = @import("builtin");
const log = @import("../log.zig");
const App = @import("../App.zig");
const js = @import("js/js.zig");
const v8 = js.v8;
const storage = @import("webapi/storage/storage.zig");
const Navigation = @import("webapi/navigation/Navigation.zig");
const History = @import("webapi/History.zig");
const Page = @import("Page.zig");
pub const Runner = @import("Runner.zig");
const Browser = @import("Browser.zig");
const Factory = @import("Factory.zig");
const Notification = @import("../Notification.zig");
const QueuedNavigation = Page.QueuedNavigation;
const Allocator = std.mem.Allocator;
const ArenaPool = App.ArenaPool;
const IS_DEBUG = builtin.mode == .Debug;
// Session is like a browser's tab.
// It owns the js env and the loader for all the pages of the session.
// You can create successively multiple pages for a session, but you must
// deinit a page before running another one.
// deinit a page before running another one. It manages two distinct lifetimes.
//
// The first is the lifetime of the Session itself, where pages are created and
// removed, but share the same cookie jar and navigation history (etc...)
//
// The second is as a container the data needed by the full page hierarchy, i.e. \
// the root page and all of its frames (and all of their frames.)
const Session = @This();
// These are the fields that remain intact for the duration of the Session
browser: *Browser,
notification: *Notification,
// Used to create our Inspector and in the BrowserContext.
arena: Allocator,
cookie_jar: storage.Cookie.Jar,
storage_shed: storage.Shed,
history: History,
navigation: Navigation,
storage_shed: storage.Shed,
notification: *Notification,
cookie_jar: storage.Cookie.Jar,
// These are the fields that get reset whenever the Session's page (the root) is reset.
factory: Factory,
page_arena: Allocator,
// Origin map for same-origin context sharing. Scoped to the root page lifetime.
origins: std.StringHashMapUnmanaged(*js.Origin) = .empty,
// Identity tracking for the main world. All main world contexts share this,
// ensuring object identity works across same-origin frames.
identity: js.Identity = .{},
// Shared finalizer callbacks across all Identities. Keyed by Zig instance ptr.
// This ensures objects are only freed when ALL v8 wrappers are gone.
finalizer_callbacks: std.AutoHashMapUnmanaged(usize, *FinalizerCallback) = .empty,
// Tracked global v8 objects that need to be released on cleanup.
// Lives at Session level so objects can outlive individual Identities.
globals: std.ArrayList(v8.Global) = .empty,
// Temporary v8 globals that can be released early. Key is global.data_ptr.
// Lives at Session level so objects holding Temps can outlive individual Identities.
temps: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
// Shared resources for all pages in this session.
// These live for the duration of the page tree (root + frames).
arena_pool: *ArenaPool,
page: ?Page,
frame_id_gen: u32,
// Double buffer so that, as we process one list of queued navigations, new entries
// are added to the separate buffer. This ensures that we don't end up with
// endless navigation loops AND that we don't invalidate the list while iterating
// if a new entry gets appended
queued_navigation_1: std.ArrayList(*Page),
queued_navigation_2: std.ArrayList(*Page),
// pointer to either queued_navigation_1 or queued_navigation_2
queued_navigation: *std.ArrayList(*Page),
// Temporary buffer for about:blank navigations during processing.
// We process async navigations first (safe from re-entrance), then sync
// about:blank navigations (which may add to queued_navigation).
queued_queued_navigation: std.ArrayList(*Page),
page_id_gen: u32 = 0,
frame_id_gen: u32 = 0,
pub fn init(self: *Session, browser: *Browser, notification: *Notification) !void {
const allocator = browser.app.allocator;
const arena = try browser.arena_pool.acquire();
errdefer browser.arena_pool.release(arena);
const arena_pool = browser.arena_pool;
const arena = try arena_pool.acquire(.{ .debug = "Session" });
errdefer arena_pool.release(arena);
const page_arena = try arena_pool.acquire(.{ .debug = "Session.page_arena" });
errdefer arena_pool.release(page_arena);
self.* = .{
.page = null,
.arena = arena,
.arena_pool = arena_pool,
.page_arena = page_arena,
.factory = Factory.init(page_arena),
.history = .{},
.frame_id_gen = 0,
// The prototype (EventTarget) for Navigation is created when a Page is created.
.navigation = .{ ._proto = undefined },
.storage_shed = .{},
.browser = browser,
.queued_navigation = undefined,
.queued_navigation_1 = .{},
.queued_navigation_2 = .{},
.queued_queued_navigation = .{},
.notification = notification,
.cookie_jar = storage.Cookie.Jar.init(allocator),
};
self.queued_navigation = &self.queued_navigation_1;
}
pub fn deinit(self: *Session) void {
if (self.page != null) {
self.removePage();
}
const browser = self.browser;
self.cookie_jar.deinit();
self.storage_shed.deinit(browser.app.allocator);
browser.arena_pool.release(self.arena);
self.storage_shed.deinit(self.browser.app.allocator);
self.arena_pool.release(self.page_arena);
self.arena_pool.release(self.arena);
}
// NOTE: the caller is not the owner of the returned value,
@@ -113,33 +175,130 @@ pub fn removePage(self: *Session) void {
self.notification.dispatch(.page_remove, .{});
lp.assert(self.page != null, "Session.removePage - page is null", .{});
self.page.?.deinit();
self.page.?.deinit(false);
self.page = null;
self.navigation.onRemovePage();
self.resetPageResources();
if (comptime IS_DEBUG) {
log.debug(.browser, "remove page", .{});
}
}
pub const GetArenaOpts = struct {
debug: []const u8,
};
pub fn getArena(self: *Session, opts: GetArenaOpts) !Allocator {
return self.arena_pool.acquire(.{ .debug = opts.debug });
}
pub fn releaseArena(self: *Session, allocator: Allocator) void {
self.arena_pool.release(allocator);
}
pub fn getOrCreateOrigin(self: *Session, key_: ?[]const u8) !*js.Origin {
const key = key_ orelse {
var opaque_origin: [36]u8 = undefined;
@import("../id.zig").uuidv4(&opaque_origin);
// Origin.init will dupe opaque_origin. It's fine that this doesn't
// get added to self.origins. In fact, it further isolates it. When the
// context is freed, it'll call session.releaseOrigin which will free it.
return js.Origin.init(self.browser.app, self.browser.env.isolate, &opaque_origin);
};
const gop = try self.origins.getOrPut(self.arena, key);
if (gop.found_existing) {
const origin = gop.value_ptr.*;
origin.rc += 1;
return origin;
}
errdefer _ = self.origins.remove(key);
const origin = try js.Origin.init(self.browser.app, self.browser.env.isolate, key);
gop.key_ptr.* = origin.key;
gop.value_ptr.* = origin;
return origin;
}
pub fn releaseOrigin(self: *Session, origin: *js.Origin) void {
const rc = origin.rc;
if (rc == 1) {
_ = self.origins.remove(origin.key);
origin.deinit(self.browser.app);
} else {
origin.rc = rc - 1;
}
}
/// Reset page_arena and factory for a clean slate.
/// Called when root page is removed.
fn resetPageResources(self: *Session) void {
// Force cleanup all remaining finalized objects
{
var it = self.finalizer_callbacks.valueIterator();
while (it.next()) |fc| {
fc.*.deinit(self);
}
self.finalizer_callbacks = .empty;
}
{
for (self.globals.items) |*global| {
v8.v8__Global__Reset(global);
}
self.globals = .empty;
}
{
var it = self.temps.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
self.temps = .empty;
}
self.identity.deinit();
self.identity = .{};
if (comptime IS_DEBUG) {
std.debug.assert(self.origins.count() == 0);
}
// Defensive cleanup in case origins leaked
{
const app = self.browser.app;
var it = self.origins.valueIterator();
while (it.next()) |value| {
value.*.deinit(app);
}
self.origins = .empty;
}
self.frame_id_gen = 0;
self.arena_pool.reset(self.page_arena, 64 * 1024);
self.factory = Factory.init(self.page_arena);
}
pub fn replacePage(self: *Session) !*Page {
if (comptime IS_DEBUG) {
log.debug(.browser, "replace page", .{});
}
lp.assert(self.page != null, "Session.replacePage null page", .{});
lp.assert(self.page.?.parent == null, "Session.replacePage with parent", .{});
var current = self.page.?;
const frame_id = current._frame_id;
const parent = current.parent;
current.deinit();
current.deinit(true);
self.resetPageResources();
self.browser.env.memoryPressureNotification(.moderate);
self.page = @as(Page, undefined);
const page = &self.page.?;
try Page.init(page, frame_id, self, parent);
try Page.init(page, frame_id, self, null);
return page;
}
@@ -147,240 +306,179 @@ pub fn currentPage(self: *Session) ?*Page {
return &(self.page orelse return null);
}
pub const WaitResult = enum {
done,
no_page,
cdp_socket,
};
pub fn findPage(self: *Session, frame_id: u32) ?*Page {
pub fn findPageByFrameId(self: *Session, frame_id: u32) ?*Page {
const page = self.currentPage() orelse return null;
return if (page._frame_id == frame_id) page else null;
return findPageBy(page, "_frame_id", frame_id);
}
pub fn wait(self: *Session, wait_ms: u32) WaitResult {
var page = &(self.page orelse return .no_page);
while (true) {
const wait_result = self._wait(page, wait_ms) catch |err| {
switch (err) {
error.JsError => {}, // already logged (with hopefully more context)
else => log.err(.browser, "session wait", .{
.err = err,
.url = page.url,
}),
pub fn findPageById(self: *Session, id: u32) ?*Page {
const page = self.currentPage() orelse return null;
return findPageBy(page, "id", id);
}
return .done;
};
switch (wait_result) {
.done => {
if (page._queued_navigation == null) {
return .done;
fn findPageBy(page: *Page, comptime field: []const u8, id: u32) ?*Page {
if (@field(page, field) == id) return page;
for (page.frames.items) |f| {
if (findPageBy(f, field, id)) |found| {
return found;
}
page = self.processScheduledNavigation(page) catch return .done;
},
else => |result| return result,
}
return null;
}
pub fn runner(self: *Session, opts: Runner.Opts) !Runner {
return Runner.init(self, opts);
}
pub fn scheduleNavigation(self: *Session, page: *Page) !void {
const list = self.queued_navigation;
// Check if page is already queued
for (list.items) |existing| {
if (existing == page) {
// Already queued
return;
}
}
fn _wait(self: *Session, page: *Page, wait_ms: u32) !WaitResult {
var timer = try std.time.Timer.start();
var ms_remaining = wait_ms;
const browser = self.browser;
var http_client = browser.http_client;
// I'd like the page to know NOTHING about cdp_socket / CDP, but the
// fact is that the behavior of wait changes depending on whether or
// not we're using CDP.
// If we aren't using CDP, as soon as we think there's nothing left
// to do, we can exit - we'de done.
// But if we are using CDP, we should wait for the whole `wait_ms`
// because the http_click.tick() also monitors the CDP socket. And while
// we could let CDP poll http (like it does for HTTP requests), the fact
// is that we know more about the timing of stuff (e.g. how long to
// poll/sleep) in the page.
const exit_when_done = http_client.cdp_client == null;
while (true) {
switch (page._parse_state) {
.pre, .raw, .text, .image => {
// The main page hasn't started/finished navigating.
// There's no JS to run, and no reason to run the scheduler.
if (http_client.active == 0 and exit_when_done) {
// haven't started navigating, I guess.
return .done;
}
// Either we have active http connections, or we're in CDP
// mode with an extra socket. Either way, we're waiting
// for http traffic
if (try http_client.tick(@intCast(ms_remaining)) == .cdp_socket) {
// exit_when_done is explicitly set when there isn't
// an extra socket, so it should not be possibl to
// get an cdp_socket message when exit_when_done
// is true.
if (IS_DEBUG) {
std.debug.assert(exit_when_done == false);
return list.append(self.arena, page);
}
// data on a socket we aren't handling, return to caller
return .cdp_socket;
}
},
.html, .complete => {
if (page._queued_navigation != null) {
return .done;
}
// The HTML page was parsed. We now either have JS scripts to
// download, or scheduled tasks to execute, or both.
// scheduler.run could trigger new http transfers, so do not
// store http_client.active BEFORE this call and then use
// it AFTER.
const ms_to_next_task = try browser.runMacrotasks();
// Each call to this runs scheduled load events.
try page.dispatchLoad();
const http_active = http_client.active;
const total_network_activity = http_active + http_client.intercepted;
if (page._notified_network_almost_idle.check(total_network_activity <= 2)) {
page.notifyNetworkAlmostIdle();
}
if (page._notified_network_idle.check(total_network_activity == 0)) {
page.notifyNetworkIdle();
}
if (http_active == 0 and exit_when_done) {
// we don't need to consider http_client.intercepted here
// because exit_when_done is true, and that can only be
// the case when interception isn't possible.
if (comptime IS_DEBUG) {
std.debug.assert(http_client.intercepted == 0);
}
var ms: u64 = ms_to_next_task orelse blk: {
if (wait_ms - ms_remaining < 100) {
if (comptime builtin.is_test) {
return .done;
}
// Look, we want to exit ASAP, but we don't want
// to exit so fast that we've run none of the
// background jobs.
break :blk 50;
}
if (browser.hasBackgroundTasks()) {
// _we_ have nothing to run, but v8 is working on
// background tasks. We'll wait for them.
browser.waitForBackgroundTasks();
break :blk 20;
}
// No http transfers, no cdp extra socket, no
// scheduled tasks, we're done.
return .done;
};
if (ms > ms_remaining) {
// Same as above, except we have a scheduled task,
// it just happens to be too far into the future
// compared to how long we were told to wait.
if (!browser.hasBackgroundTasks()) {
return .done;
}
// _we_ have nothing to run, but v8 is working on
// background tasks. We'll wait for them.
browser.waitForBackgroundTasks();
ms = 20;
}
// We have a task to run in the not-so-distant future.
// You might think we can just sleep until that task is
// ready, but we should continue to run lowPriority tasks
// in the meantime, and that could unblock things. So
// we'll just sleep for a bit, and then restart our wait
// loop to see if anything new can be processed.
std.Thread.sleep(std.time.ns_per_ms * @as(u64, @intCast(@min(ms, 20))));
pub fn processQueuedNavigation(self: *Session) !void {
const navigations = self.queued_navigation;
if (self.queued_navigation == &self.queued_navigation_1) {
self.queued_navigation = &self.queued_navigation_2;
} else {
// We're here because we either have active HTTP
// connections, or exit_when_done == false (aka, there's
// an cdp_socket registered with the http client).
// We should continue to run lowPriority tasks, so we
// minimize how long we'll poll for network I/O.
var ms_to_wait = @min(200, ms_to_next_task orelse 200);
if (ms_to_wait > 10 and browser.hasBackgroundTasks()) {
// if we have background tasks, we don't want to wait too
// long for a message from the client. We want to go back
// to the top of the loop and run macrotasks.
ms_to_wait = 10;
}
if (try http_client.tick(@min(ms_remaining, ms_to_wait)) == .cdp_socket) {
// data on a socket we aren't handling, return to caller
return .cdp_socket;
}
}
},
.err => |err| {
page._parse_state = .{ .raw_done = @errorName(err) };
return err;
},
.raw_done => {
if (exit_when_done) {
return .done;
}
// we _could_ http_client.tick(ms_to_wait), but this has
// the same result, and I feel is more correct.
return .no_page;
},
self.queued_navigation = &self.queued_navigation_1;
}
const ms_elapsed = timer.lap() / 1_000_000;
if (ms_elapsed >= ms_remaining) {
return .done;
if (self.page.?._queued_navigation != null) {
// This is both an optimization and a simplification of sorts. If the
// root page is navigating, then we don't need to process any other
// navigation. Also, the navigation for the root page and for a frame
// is different enough that have two distinct code blocks is, imo,
// better. Yes, there will be duplication.
navigations.clearRetainingCapacity();
return self.processRootQueuedNavigation();
}
ms_remaining -= @intCast(ms_elapsed);
const about_blank_queue = &self.queued_queued_navigation;
defer about_blank_queue.clearRetainingCapacity();
// First pass: process async navigations (non-about:blank)
for (navigations.items) |page| {
const qn = page._queued_navigation.?;
if (qn.is_about_blank) {
// Defer about:blank to second pass
try about_blank_queue.append(self.arena, page);
continue;
}
self.processFrameNavigation(page, qn) catch |err| {
log.warn(.page, "frame navigation", .{ .url = qn.url, .err = err });
};
}
navigations.clearRetainingCapacity();
// Second pass: process synchronous navigations (about:blank)
// These may trigger new navigations which go into queued_navigation
for (about_blank_queue.items) |page| {
const qn = page._queued_navigation.?;
try self.processFrameNavigation(page, qn);
}
// Safety: Remove any about:blank navigations that were queued during
// processing to prevent infinite loops. New navigations have been queued
// in the other buffer.
const new_navigations = self.queued_navigation;
var i: usize = 0;
while (i < new_navigations.items.len) {
const page = new_navigations.items[i];
if (page._queued_navigation) |qn| {
if (qn.is_about_blank) {
log.warn(.page, "recursive about blank", .{});
_ = self.queued_navigation.swapRemove(i);
continue;
}
}
i += 1;
}
}
fn processScheduledNavigation(self: *Session, current_page: *Page) !*Page {
const browser = self.browser;
fn processFrameNavigation(self: *Session, page: *Page, qn: *QueuedNavigation) !void {
lp.assert(page.parent != null, "root queued navigation", .{});
const qn = current_page._queued_navigation.?;
// take ownership of the page's queued navigation
current_page._queued_navigation = null;
defer browser.arena_pool.release(qn.arena);
const iframe = page.iframe.?;
const parent = page.parent.?;
page._queued_navigation = null;
defer self.releaseArena(qn.arena);
errdefer iframe._window = null;
const parent_notified = page._parent_notified;
if (parent_notified) {
// we already notified the parent that we had loaded
parent._pending_loads += 1;
}
const frame_id, const parent = blk: {
const page = &self.page.?;
const frame_id = page._frame_id;
const parent = page.parent;
page.deinit(true);
page.* = undefined;
try Page.init(page, frame_id, self, parent);
errdefer {
for (parent.frames.items, 0..) |frame, i| {
if (frame == page) {
parent.frames_sorted = false;
_ = parent.frames.swapRemove(i);
break;
}
}
if (parent_notified) {
parent._pending_loads -= 1;
}
page.deinit(true);
}
page.iframe = iframe;
iframe._window = page.window;
page.navigate(qn.url, qn.opts) catch |err| {
log.err(.browser, "queued frame navigation error", .{ .err = err });
return err;
};
}
fn processRootQueuedNavigation(self: *Session) !void {
const current_page = &self.page.?;
const frame_id = current_page._frame_id;
// create a copy before the page is cleared
const qn = current_page._queued_navigation.?;
current_page._queued_navigation = null;
defer self.arena_pool.release(qn.arena);
browser.http_client.abort();
self.removePage();
break :blk .{ frame_id, parent };
};
self.page = @as(Page, undefined);
const page = &self.page.?;
try Page.init(page, frame_id, self, parent);
const new_page = &self.page.?;
try Page.init(new_page, frame_id, self, null);
// Creates a new NavigationEventTarget for this page.
try self.navigation.onNewPage(page);
try self.navigation.onNewPage(new_page);
// start JS env
// Inform CDP the main page has been created such that additional context for other Worlds can be created as well
self.notification.dispatch(.page_created, page);
self.notification.dispatch(.page_created, new_page);
page.navigate(qn.url, qn.opts) catch |err| {
log.err(.browser, "queued navigation error", .{ .err = err, .url = qn.url });
new_page.navigate(qn.url, qn.opts) catch |err| {
log.err(.browser, "queued navigation error", .{ .err = err });
return err;
};
return page;
}
pub fn nextFrameId(self: *Session) u32 {
@@ -388,3 +486,32 @@ pub fn nextFrameId(self: *Session) u32 {
self.frame_id_gen = id;
return id;
}
pub fn nextPageId(self: *Session) u32 {
const id = self.page_id_gen +% 1;
self.page_id_gen = id;
return id;
}
// Every finalizable instance of Zig gets 1 FinalizerCallback registered in the
// session. This is to ensure that, if v8 doesn't finalize the value, we can
// release on page reset.
pub const FinalizerCallback = struct {
arena: Allocator,
session: *Session,
resolved_ptr_id: usize,
finalizer_ptr_id: usize,
_deinit: *const fn (ptr_id: usize, session: *Session) void,
// For every FinalizerCallback we'll have 1+ FinalizerCallback.Identity: one
// for every identity that gets the instance. In most cases, that'l be 1.
pub const Identity = struct {
identity: *js.Identity,
fc: *Session.FinalizerCallback,
};
fn deinit(self: *FinalizerCallback, session: *Session) void {
self._deinit(self.finalizer_ptr_id, session);
session.releaseArena(self.arena);
}
};

View File

@@ -0,0 +1,855 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const log = @import("../log.zig");
const String = @import("../string.zig").String;
const Page = @import("Page.zig");
const CssParser = @import("css/Parser.zig");
const Element = @import("webapi/Element.zig");
const Selector = @import("webapi/selector/Selector.zig");
const SelectorParser = @import("webapi/selector/Parser.zig");
const SelectorList = @import("webapi/selector/List.zig");
const CSSStyleRule = @import("webapi/css/CSSStyleRule.zig");
const CSSStyleSheet = @import("webapi/css/CSSStyleSheet.zig");
const CSSStyleProperties = @import("webapi/css/CSSStyleProperties.zig");
const CSSStyleProperty = @import("webapi/css/CSSStyleDeclaration.zig").Property;
const Allocator = std.mem.Allocator;
pub const VisibilityCache = std.AutoHashMapUnmanaged(*Element, bool);
pub const PointerEventsCache = std.AutoHashMapUnmanaged(*Element, bool);
// Tracks visibility-relevant CSS rules from <style> elements.
// Rules are bucketed by their rightmost selector part for fast lookup.
const StyleManager = @This();
const Tag = Element.Tag;
const RuleList = std.MultiArrayList(VisibilityRule);
page: *Page,
arena: Allocator,
// Bucketed rules for fast lookup - keyed by rightmost selector part
id_rules: std.StringHashMapUnmanaged(RuleList) = .empty,
class_rules: std.StringHashMapUnmanaged(RuleList) = .empty,
tag_rules: std.AutoHashMapUnmanaged(Tag, RuleList) = .empty,
other_rules: RuleList = .empty, // universal, attribute, pseudo-class endings
// Document order counter for tie-breaking equal specificity
next_doc_order: u32 = 0,
// When true, rules need to be rebuilt
dirty: bool = false,
pub fn init(page: *Page) !StyleManager {
return .{
.page = page,
.arena = try page.getArena(.{ .debug = "StyleManager" }),
};
}
pub fn deinit(self: *StyleManager) void {
self.page.releaseArena(self.arena);
}
fn parseSheet(self: *StyleManager, sheet: *CSSStyleSheet) !void {
if (sheet._css_rules) |css_rules| {
for (css_rules._rules.items) |rule| {
const style_rule = rule.is(CSSStyleRule) orelse continue;
try self.addRule(style_rule);
}
return;
}
const owner_node = sheet.getOwnerNode() orelse return;
if (owner_node.is(Element.Html.Style)) |style| {
const text = try style.asNode().getTextContentAlloc(self.arena);
var it = CssParser.parseStylesheet(text);
while (it.next()) |parsed_rule| {
try self.addRawRule(parsed_rule.selector, parsed_rule.block);
}
}
}
fn addRawRule(self: *StyleManager, selector_text: []const u8, block_text: []const u8) !void {
if (selector_text.len == 0) return;
var props = VisibilityProperties{};
var it = CssParser.parseDeclarationsList(block_text);
while (it.next()) |decl| {
const name = decl.name;
const val = decl.value;
if (std.ascii.eqlIgnoreCase(name, "display")) {
props.display_none = std.ascii.eqlIgnoreCase(val, "none");
} else if (std.ascii.eqlIgnoreCase(name, "visibility")) {
props.visibility_hidden = std.ascii.eqlIgnoreCase(val, "hidden") or std.ascii.eqlIgnoreCase(val, "collapse");
} else if (std.ascii.eqlIgnoreCase(name, "opacity")) {
props.opacity_zero = std.ascii.eqlIgnoreCase(val, "0");
} else if (std.ascii.eqlIgnoreCase(name, "pointer-events")) {
props.pointer_events_none = std.ascii.eqlIgnoreCase(val, "none");
}
}
if (!props.isRelevant()) return;
const selectors = SelectorParser.parseList(self.arena, selector_text, self.page) catch return;
for (selectors) |selector| {
const rightmost = if (selector.segments.len > 0) selector.segments[selector.segments.len - 1].compound else selector.first;
const bucket_key = getBucketKey(rightmost) orelse continue;
const rule = VisibilityRule{
.props = props,
.selector = selector,
.priority = (@as(u64, computeSpecificity(selector)) << 32) | @as(u64, self.next_doc_order),
};
self.next_doc_order += 1;
switch (bucket_key) {
.id => |id| {
const gop = try self.id_rules.getOrPut(self.arena, id);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.class => |class| {
const gop = try self.class_rules.getOrPut(self.arena, class);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.tag => |tag| {
const gop = try self.tag_rules.getOrPut(self.arena, tag);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.other => {
try self.other_rules.append(self.arena, rule);
},
}
}
}
pub fn sheetRemoved(self: *StyleManager) void {
self.dirty = true;
}
pub fn sheetModified(self: *StyleManager) void {
self.dirty = true;
}
/// Rebuilds the rule list from all document stylesheets.
/// Called lazily when dirty flag is set and rules are needed.
fn rebuildIfDirty(self: *StyleManager) !void {
if (!self.dirty) {
return;
}
self.dirty = false;
errdefer self.dirty = true;
const id_rules_count = self.id_rules.count();
const class_rules_count = self.class_rules.count();
const tag_rules_count = self.tag_rules.count();
const other_rules_count = self.other_rules.len;
self.page._session.arena_pool.resetRetain(self.arena);
self.next_doc_order = 0;
self.id_rules = .empty;
try self.id_rules.ensureTotalCapacity(self.arena, id_rules_count);
self.class_rules = .empty;
try self.class_rules.ensureTotalCapacity(self.arena, class_rules_count);
self.tag_rules = .empty;
try self.tag_rules.ensureTotalCapacity(self.arena, tag_rules_count);
self.other_rules = .{};
try self.other_rules.ensureTotalCapacity(self.arena, other_rules_count);
const sheets = self.page.document._style_sheets orelse return;
for (sheets._sheets.items) |sheet| {
self.parseSheet(sheet) catch |err| {
log.err(.browser, "StyleManager parseSheet", .{ .err = err });
return err;
};
}
}
// Check if an element is hidden based on options.
// By default only checks display:none.
// Walks up the tree to check ancestors.
pub fn isHidden(self: *StyleManager, el: *Element, cache: ?*VisibilityCache, options: CheckVisibilityOptions) bool {
self.rebuildIfDirty() catch return false;
var current: ?*Element = el;
while (current) |elem| {
// Check cache first (only when checking all properties for caching consistency)
if (cache) |c| {
if (c.get(elem)) |hidden| {
if (hidden) {
return true;
}
current = elem.parentElement();
continue;
}
}
const hidden = self.isElementHidden(elem, options);
// Store in cache
if (cache) |c| {
c.put(self.page.call_arena, elem, hidden) catch {};
}
if (hidden) {
return true;
}
current = elem.parentElement();
}
return false;
}
/// Check if a single element (not ancestors) is hidden.
fn isElementHidden(self: *StyleManager, el: *Element, options: CheckVisibilityOptions) bool {
// Track best match per property (value + priority)
// Initialize priority to INLINE_PRIORITY for properties we don't care about - this makes
// the loop naturally skip them since no stylesheet rule can have priority >= INLINE_PRIORITY
var display_none: ?bool = null;
var display_priority: u64 = 0;
var visibility_hidden: ?bool = null;
var visibility_priority: u64 = 0;
var opacity_zero: ?bool = null;
var opacity_priority: u64 = 0;
// Check inline styles FIRST - they use INLINE_PRIORITY so no stylesheet can beat them
if (getInlineStyleProperty(el, comptime .wrap("display"), self.page)) |property| {
if (property._value.eql(comptime .wrap("none"))) {
return true; // Early exit for hiding value
}
display_none = false;
display_priority = INLINE_PRIORITY;
}
if (options.check_visibility) {
if (getInlineStyleProperty(el, comptime .wrap("visibility"), self.page)) |property| {
if (property._value.eql(comptime .wrap("hidden")) or property._value.eql(comptime .wrap("collapse"))) {
return true;
}
visibility_hidden = false;
visibility_priority = INLINE_PRIORITY;
}
} else {
// This can't be beat. Setting this means that, when checking rules
// we no longer have to check if options.check_visibility is enabled.
// We can just compare the priority.
visibility_priority = INLINE_PRIORITY;
}
if (options.check_opacity) {
if (getInlineStyleProperty(el, comptime .wrap("opacity"), self.page)) |property| {
if (property._value.eql(comptime .wrap("0"))) {
return true;
}
opacity_zero = false;
opacity_priority = INLINE_PRIORITY;
}
} else {
opacity_priority = INLINE_PRIORITY;
}
if (display_priority == INLINE_PRIORITY and visibility_priority == INLINE_PRIORITY and opacity_priority == INLINE_PRIORITY) {
return false;
}
// Helper to check a single rule
const Ctx = struct {
display_none: *?bool,
display_priority: *u64,
visibility_hidden: *?bool,
visibility_priority: *u64,
opacity_zero: *?bool,
opacity_priority: *u64,
el: *Element,
page: *Page,
fn checkRules(ctx: @This(), rules: *const RuleList) void {
if (ctx.display_priority.* == INLINE_PRIORITY and
ctx.visibility_priority.* == INLINE_PRIORITY and
ctx.opacity_priority.* == INLINE_PRIORITY)
{
return;
}
const priorities = rules.items(.priority);
const props_list = rules.items(.props);
const selectors = rules.items(.selector);
for (priorities, props_list, selectors) |p, props, selector| {
// Fast skip using packed u64 priority
if (p <= ctx.display_priority.* and p <= ctx.visibility_priority.* and p <= ctx.opacity_priority.*) {
continue;
}
// Logic for property dominance
const dominated = (props.display_none == null or p <= ctx.display_priority.*) and
(props.visibility_hidden == null or p <= ctx.visibility_priority.*) and
(props.opacity_zero == null or p <= ctx.opacity_priority.*);
if (dominated) continue;
if (matchesSelector(ctx.el, selector, ctx.page)) {
// Update best priorities
if (props.display_none != null and p > ctx.display_priority.*) {
ctx.display_none.* = props.display_none;
ctx.display_priority.* = p;
}
if (props.visibility_hidden != null and p > ctx.visibility_priority.*) {
ctx.visibility_hidden.* = props.visibility_hidden;
ctx.visibility_priority.* = p;
}
if (props.opacity_zero != null and p > ctx.opacity_priority.*) {
ctx.opacity_zero.* = props.opacity_zero;
ctx.opacity_priority.* = p;
}
}
}
}
};
const ctx = Ctx{
.display_none = &display_none,
.display_priority = &display_priority,
.visibility_hidden = &visibility_hidden,
.visibility_priority = &visibility_priority,
.opacity_zero = &opacity_zero,
.opacity_priority = &opacity_priority,
.el = el,
.page = self.page,
};
if (el.getAttributeSafe(comptime .wrap("id"))) |id| {
if (self.id_rules.get(id)) |rules| {
ctx.checkRules(&rules);
}
}
if (el.getAttributeSafe(comptime .wrap("class"))) |class_attr| {
var it = std.mem.tokenizeAny(u8, class_attr, &std.ascii.whitespace);
while (it.next()) |class| {
if (self.class_rules.get(class)) |rules| {
ctx.checkRules(&rules);
}
}
}
if (self.tag_rules.get(el.getTag())) |rules| {
ctx.checkRules(&rules);
}
ctx.checkRules(&self.other_rules);
return (display_none orelse false) or (visibility_hidden orelse false) or (opacity_zero orelse false);
}
/// Check if an element has pointer-events:none.
/// Checks inline style first - if set, skips stylesheet lookup.
/// Walks up the tree to check ancestors.
pub fn hasPointerEventsNone(self: *StyleManager, el: *Element, cache: ?*PointerEventsCache) bool {
self.rebuildIfDirty() catch return false;
var current: ?*Element = el;
while (current) |elem| {
// Check cache first
if (cache) |c| {
if (c.get(elem)) |pe_none| {
if (pe_none) return true;
current = elem.parentElement();
continue;
}
}
const pe_none = self.elementHasPointerEventsNone(elem);
if (cache) |c| {
c.put(self.page.call_arena, elem, pe_none) catch {};
}
if (pe_none) {
return true;
}
current = elem.parentElement();
}
return false;
}
/// Check if a single element (not ancestors) has pointer-events:none.
fn elementHasPointerEventsNone(self: *StyleManager, el: *Element) bool {
const page = self.page;
// Check inline style first
if (getInlineStyleProperty(el, .wrap("pointer-events"), page)) |property| {
if (property._value.eql(comptime .wrap("none"))) {
return true;
}
return false;
}
var result: ?bool = null;
var best_priority: u64 = 0;
// Helper to check a single rule
const checkRules = struct {
fn check(rules: *const RuleList, res: *?bool, current_priority: *u64, elem: *Element, p: *Page) void {
if (current_priority.* == INLINE_PRIORITY) return;
const priorities = rules.items(.priority);
const props_list = rules.items(.props);
const selectors = rules.items(.selector);
for (priorities, props_list, selectors) |priority, props, selector| {
if (priority <= current_priority.*) continue;
if (props.pointer_events_none == null) continue;
if (matchesSelector(elem, selector, p)) {
res.* = props.pointer_events_none;
current_priority.* = priority;
}
}
}
}.check;
if (el.getAttributeSafe(comptime .wrap("id"))) |id| {
if (self.id_rules.get(id)) |rules| {
checkRules(&rules, &result, &best_priority, el, page);
}
}
if (el.getAttributeSafe(comptime .wrap("class"))) |class_attr| {
var it = std.mem.tokenizeAny(u8, class_attr, &std.ascii.whitespace);
while (it.next()) |class| {
if (self.class_rules.get(class)) |rules| {
checkRules(&rules, &result, &best_priority, el, page);
}
}
}
if (self.tag_rules.get(el.getTag())) |rules| {
checkRules(&rules, &result, &best_priority, el, page);
}
checkRules(&self.other_rules, &result, &best_priority, el, page);
return result orelse false;
}
// Extracts visibility-relevant rules from a CSS rule.
// Creates one VisibilityRule per selector (not per selector list) so each has correct specificity.
// Buckets rules by their rightmost selector part for fast lookup.
fn addRule(self: *StyleManager, style_rule: *CSSStyleRule) !void {
const selector_text = style_rule._selector_text;
if (selector_text.len == 0) {
return;
}
// Check if the rule has visibility-relevant properties
const style = style_rule._style orelse return;
const props = extractVisibilityProperties(style);
if (!props.isRelevant()) {
return;
}
// Parse the selector list
const selectors = SelectorParser.parseList(self.arena, selector_text, self.page) catch return;
if (selectors.len == 0) {
return;
}
// Create one rule per selector - each has its own specificity
// e.g., "#id, .class { display: none }" becomes two rules with different specificities
for (selectors) |selector| {
// Get the rightmost compound (last segment, or first if no segments)
const rightmost = if (selector.segments.len > 0)
selector.segments[selector.segments.len - 1].compound
else
selector.first;
// Find the bucketing key from rightmost compound
const bucket_key = getBucketKey(rightmost) orelse continue; // skip if dynamic pseudo-class
const rule = VisibilityRule{
.props = props,
.selector = selector,
.priority = (@as(u64, computeSpecificity(selector)) << 32) | @as(u64, self.next_doc_order),
};
self.next_doc_order += 1;
// Add to appropriate bucket
switch (bucket_key) {
.id => |id| {
const gop = try self.id_rules.getOrPut(self.arena, id);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.class => |class| {
const gop = try self.class_rules.getOrPut(self.arena, class);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.tag => |tag| {
const gop = try self.tag_rules.getOrPut(self.arena, tag);
if (!gop.found_existing) gop.value_ptr.* = .{};
try gop.value_ptr.append(self.arena, rule);
},
.other => {
try self.other_rules.append(self.arena, rule);
},
}
}
}
const BucketKey = union(enum) {
id: []const u8,
class: []const u8,
tag: Tag,
other,
};
/// Returns the best bucket key for a compound selector, or null if it contains
/// a dynamic pseudo-class we should skip (hover, active, focus, etc.)
/// Priority: id > class > tag > other
fn getBucketKey(compound: Selector.Compound) ?BucketKey {
var best_key: BucketKey = .other;
for (compound.parts) |part| {
switch (part) {
.id => |id| {
best_key = .{ .id = id };
},
.class => |class| {
if (best_key != .id) {
best_key = .{ .class = class };
}
},
.tag => |tag| {
if (best_key == .other) {
best_key = .{ .tag = tag };
}
},
.tag_name => {
// Custom tag - put in other bucket (can't efficiently look up)
// Keep current best_key if we have something better
},
.pseudo_class => |pc| {
// Skip dynamic pseudo-classes - they depend on interaction state
switch (pc) {
.hover, .active, .focus, .focus_within, .focus_visible, .visited, .target => {
return null; // Skip this selector entirely
},
else => {},
}
},
.universal, .attribute => {},
}
}
return best_key;
}
/// Extracts visibility-relevant properties from a style declaration.
fn extractVisibilityProperties(style: *CSSStyleProperties) VisibilityProperties {
var props = VisibilityProperties{};
const decl = style.asCSSStyleDeclaration();
if (decl.findProperty(comptime .wrap("display"))) |property| {
props.display_none = property._value.eql(comptime .wrap("none"));
}
if (decl.findProperty(comptime .wrap("visibility"))) |property| {
props.visibility_hidden = property._value.eql(comptime .wrap("hidden")) or property._value.eql(comptime .wrap("collapse"));
}
if (decl.findProperty(comptime .wrap("opacity"))) |property| {
props.opacity_zero = property._value.eql(comptime .wrap("0"));
}
if (decl.findProperty(.wrap("pointer-events"))) |property| {
props.pointer_events_none = property._value.eql(comptime .wrap("none"));
}
return props;
}
// Computes CSS specificity for a selector.
// Returns packed value: (id_count << 20) | (class_count << 10) | element_count
pub fn computeSpecificity(selector: Selector.Selector) u32 {
var ids: u32 = 0;
var classes: u32 = 0; // includes classes, attributes, pseudo-classes
var elements: u32 = 0; // includes elements, pseudo-elements
// Count specificity for first compound
countCompoundSpecificity(selector.first, &ids, &classes, &elements);
// Count specificity for subsequent segments
for (selector.segments) |segment| {
countCompoundSpecificity(segment.compound, &ids, &classes, &elements);
}
// Pack into single u32: (ids << 20) | (classes << 10) | elements
// This gives us 10 bits each, supporting up to 1023 of each type
return (@as(u32, @min(ids, 1023)) << 20) | (@as(u32, @min(classes, 1023)) << 10) | @min(elements, 1023);
}
fn countCompoundSpecificity(compound: Selector.Compound, ids: *u32, classes: *u32, elements: *u32) void {
for (compound.parts) |part| {
switch (part) {
.id => ids.* += 1,
.class => classes.* += 1,
.tag, .tag_name => elements.* += 1,
.universal => {}, // zero specificity
.attribute => classes.* += 1,
.pseudo_class => |pc| {
switch (pc) {
// :where() has zero specificity
.where => {},
// :not(), :is(), :has() take specificity of their most specific argument
.not, .is, .has => |nested| {
var max_nested: u32 = 0;
for (nested) |nested_sel| {
const spec = computeSpecificity(nested_sel);
if (spec > max_nested) max_nested = spec;
}
// Unpack and add to our counts
ids.* += (max_nested >> 20) & 0x3FF;
classes.* += (max_nested >> 10) & 0x3FF;
elements.* += max_nested & 0x3FF;
},
// All other pseudo-classes count as class-level specificity
else => classes.* += 1,
}
},
}
}
}
fn matchesSelector(el: *Element, selector: Selector.Selector, page: *Page) bool {
const node = el.asNode();
return SelectorList.matches(node, selector, node, page);
}
const VisibilityProperties = struct {
display_none: ?bool = null,
visibility_hidden: ?bool = null,
opacity_zero: ?bool = null,
pointer_events_none: ?bool = null,
// returne true if any field in VisibilityProperties is not null
fn isRelevant(self: VisibilityProperties) bool {
return self.display_none != null or
self.visibility_hidden != null or
self.opacity_zero != null or
self.pointer_events_none != null;
}
};
const VisibilityRule = struct {
selector: Selector.Selector, // Single selector, not a list
props: VisibilityProperties,
// Packed priority: (specificity << 32) | doc_order
priority: u64,
};
const CheckVisibilityOptions = struct {
check_opacity: bool = false,
check_visibility: bool = false,
};
// Inline styles always win over stylesheets - use max u64 as sentinel
const INLINE_PRIORITY: u64 = std.math.maxInt(u64);
fn getInlineStyleProperty(el: *Element, property_name: String, page: *Page) ?*CSSStyleProperty {
const style = el.getOrCreateStyle(page) catch |err| {
log.err(.browser, "StyleManager getOrCreateStyle", .{ .err = err });
return null;
};
return style.asCSSStyleDeclaration().findProperty(property_name);
}
const testing = @import("../testing.zig");
test "StyleManager: computeSpecificity: element selector" {
// div -> (0, 0, 1)
const selector = Selector.Selector{
.first = .{ .parts = &.{.{ .tag = .div }} },
.segments = &.{},
};
try testing.expectEqual(1, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: class selector" {
// .foo -> (0, 1, 0)
const selector = Selector.Selector{
.first = .{ .parts = &.{.{ .class = "foo" }} },
.segments = &.{},
};
try testing.expectEqual(1 << 10, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: id selector" {
// #bar -> (1, 0, 0)
const selector = Selector.Selector{
.first = .{ .parts = &.{.{ .id = "bar" }} },
.segments = &.{},
};
try testing.expectEqual(1 << 20, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: combined selector" {
// div.foo#bar -> (1, 1, 1)
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .tag = .div },
.{ .class = "foo" },
.{ .id = "bar" },
} },
.segments = &.{},
};
try testing.expectEqual((1 << 20) | (1 << 10) | 1, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: universal selector" {
// * -> (0, 0, 0)
const selector = Selector.Selector{
.first = .{ .parts = &.{.universal} },
.segments = &.{},
};
try testing.expectEqual(0, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: multiple classes" {
// .a.b.c -> (0, 3, 0)
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .class = "a" },
.{ .class = "b" },
.{ .class = "c" },
} },
.segments = &.{},
};
try testing.expectEqual(3 << 10, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: descendant combinator" {
// div span -> (0, 0, 2)
const selector = Selector.Selector{
.first = .{ .parts = &.{.{ .tag = .div }} },
.segments = &.{
.{ .combinator = .descendant, .compound = .{ .parts = &.{.{ .tag = .span }} } },
},
};
try testing.expectEqual(2, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: :where() has zero specificity" {
// :where(.foo) -> (0, 0, 0) regardless of what's inside
const inner_selector = Selector.Selector{
.first = .{ .parts = &.{.{ .class = "foo" }} },
.segments = &.{},
};
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .pseudo_class = .{ .where = &.{inner_selector} } },
} },
.segments = &.{},
};
try testing.expectEqual(0, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: :not() takes inner specificity" {
// :not(.foo) -> (0, 1, 0) - takes specificity of .foo
const inner_selector = Selector.Selector{
.first = .{ .parts = &.{.{ .class = "foo" }} },
.segments = &.{},
};
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .pseudo_class = .{ .not = &.{inner_selector} } },
} },
.segments = &.{},
};
try testing.expectEqual(1 << 10, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: :is() takes most specific inner" {
// :is(.foo, #bar) -> (1, 0, 0) - takes the most specific (#bar)
const class_selector = Selector.Selector{
.first = .{ .parts = &.{.{ .class = "foo" }} },
.segments = &.{},
};
const id_selector = Selector.Selector{
.first = .{ .parts = &.{.{ .id = "bar" }} },
.segments = &.{},
};
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .pseudo_class = .{ .is = &.{ class_selector, id_selector } } },
} },
.segments = &.{},
};
try testing.expectEqual(1 << 20, computeSpecificity(selector));
}
test "StyleManager: computeSpecificity: pseudo-class (general)" {
// :hover -> (0, 1, 0) - pseudo-classes count as class-level
const selector = Selector.Selector{
.first = .{ .parts = &.{
.{ .pseudo_class = .hover },
} },
.segments = &.{},
};
try testing.expectEqual(1 << 10, computeSpecificity(selector));
}
test "StyleManager: document order tie-breaking" {
// When specificity is equal, higher doc_order (later in document) wins
const beats = struct {
fn f(spec: u32, doc_order: u32, best_spec: u32, best_doc_order: u32) bool {
return spec > best_spec or (spec == best_spec and doc_order > best_doc_order);
}
}.f;
// Higher specificity always wins regardless of doc_order
try testing.expect(beats(2, 0, 1, 10));
try testing.expect(!beats(1, 10, 2, 0));
// Equal specificity: higher doc_order wins
try testing.expect(beats(1, 5, 1, 3)); // doc_order 5 > 3
try testing.expect(!beats(1, 3, 1, 5)); // doc_order 3 < 5
// Equal specificity and doc_order: no win
try testing.expect(!beats(1, 5, 1, 5));
}

View File

@@ -167,17 +167,17 @@ pub fn ensureEncoded(allocator: Allocator, url: [:0]const u8) ![:0]const u8 {
const query_end = if (query_start) |_| (fragment_start orelse url.len) else path_end;
const path_to_encode = url[path_start..path_end];
const encoded_path = try percentEncodeSegment(allocator, path_to_encode, true);
const encoded_path = try percentEncodeSegment(allocator, path_to_encode, .path);
const encoded_query = if (query_start) |qs| blk: {
const query_to_encode = url[qs + 1 .. query_end];
const encoded = try percentEncodeSegment(allocator, query_to_encode, false);
const encoded = try percentEncodeSegment(allocator, query_to_encode, .query);
break :blk encoded;
} else null;
const encoded_fragment = if (fragment_start) |fs| blk: {
const fragment_to_encode = url[fs + 1 ..];
const encoded = try percentEncodeSegment(allocator, fragment_to_encode, false);
const encoded = try percentEncodeSegment(allocator, fragment_to_encode, .query);
break :blk encoded;
} else null;
@@ -204,11 +204,13 @@ pub fn ensureEncoded(allocator: Allocator, url: [:0]const u8) ![:0]const u8 {
return buf.items[0 .. buf.items.len - 1 :0];
}
fn percentEncodeSegment(allocator: Allocator, segment: []const u8, comptime is_path: bool) ![]const u8 {
const EncodeSet = enum { path, query, userinfo, fragment };
fn percentEncodeSegment(allocator: Allocator, segment: []const u8, comptime encode_set: EncodeSet) ![]const u8 {
// Check if encoding is needed
var needs_encoding = false;
for (segment) |c| {
if (shouldPercentEncode(c, is_path)) {
if (shouldPercentEncode(c, encode_set)) {
needs_encoding = true;
break;
}
@@ -235,7 +237,7 @@ fn percentEncodeSegment(allocator: Allocator, segment: []const u8, comptime is_p
}
}
if (shouldPercentEncode(c, is_path)) {
if (shouldPercentEncode(c, encode_set)) {
try buf.writer(allocator).print("%{X:0>2}", .{c});
} else {
try buf.append(allocator, c);
@@ -245,16 +247,19 @@ fn percentEncodeSegment(allocator: Allocator, segment: []const u8, comptime is_p
return buf.items;
}
fn shouldPercentEncode(c: u8, comptime is_path: bool) bool {
fn shouldPercentEncode(c: u8, comptime encode_set: EncodeSet) bool {
return switch (c) {
// Unreserved characters (RFC 3986)
'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_', '~' => false,
// sub-delims allowed in both path and query
'!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=' => false,
// Separators allowed in both path and query
'/', ':', '@' => false,
// Query-specific: '?' is allowed in queries but not in paths
'?' => comptime is_path,
// sub-delims allowed in path/query but some must be encoded in userinfo
'!', '$', '&', '\'', '(', ')', '*', '+', ',' => false,
';', '=' => encode_set == .userinfo,
// Separators: userinfo must encode these
'/', ':', '@' => encode_set == .userinfo,
// '?' is allowed in queries only
'?' => encode_set != .query,
// '#' is allowed in fragments only
'#' => encode_set != .fragment,
// Everything else needs encoding (including space)
else => true,
};
@@ -274,6 +279,11 @@ pub fn isCompleteHTTPUrl(url: []const u8) bool {
return false;
}
// blob: and data: URLs are complete but don't follow scheme:// pattern
if (std.mem.startsWith(u8, url, "blob:") or std.mem.startsWith(u8, url, "data:")) {
return true;
}
// Check if there's a scheme (protocol) ending with ://
const colon_pos = std.mem.indexOfScalar(u8, url, ':') orelse return false;
@@ -315,14 +325,22 @@ pub fn getPassword(raw: [:0]const u8) []const u8 {
}
pub fn getPathname(raw: [:0]const u8) []const u8 {
const protocol_end = std.mem.indexOf(u8, raw, "://") orelse 0;
const path_start = std.mem.indexOfScalarPos(u8, raw, if (protocol_end > 0) protocol_end + 3 else 0, '/') orelse raw.len;
const protocol_end = std.mem.indexOf(u8, raw, "://");
// Handle scheme:path URLs like about:blank (no "://")
if (protocol_end == null) {
const colon_pos = std.mem.indexOfScalar(u8, raw, ':') orelse return "";
const path = raw[colon_pos + 1 ..];
const query_or_hash = std.mem.indexOfAny(u8, path, "?#") orelse path.len;
return path[0..query_or_hash];
}
const path_start = std.mem.indexOfScalarPos(u8, raw, protocol_end.? + 3, '/') orelse raw.len;
const query_or_hash_start = std.mem.indexOfAnyPos(u8, raw, path_start, "?#") orelse raw.len;
if (path_start >= query_or_hash_start) {
if (std.mem.indexOf(u8, raw, "://") != null) return "/";
return "";
return "/";
}
return raw[path_start..query_or_hash_start];
@@ -339,25 +357,38 @@ pub fn isHTTPS(raw: [:0]const u8) bool {
pub fn getHostname(raw: [:0]const u8) []const u8 {
const host = getHost(raw);
const pos = std.mem.lastIndexOfScalar(u8, host, ':') orelse return host;
return host[0..pos];
const port_sep = findPortSeparator(host) orelse return host;
return host[0..port_sep];
}
pub fn getPort(raw: [:0]const u8) []const u8 {
const host = getHost(raw);
const pos = std.mem.lastIndexOfScalar(u8, host, ':') orelse return "";
if (pos + 1 >= host.len) {
return "";
const port_sep = findPortSeparator(host) orelse return "";
return host[port_sep + 1 ..];
}
// Finds the colon separating host from port, handling IPv6 bracket notation.
// For IPv6 like "[::1]:8080", returns position of ":" after "]".
// For IPv6 like "[::1]" (no port), returns null.
// For regular hosts, returns position of last ":" if followed by digits.
fn findPortSeparator(host: []const u8) ?usize {
if (host.len > 0 and host[0] == '[') {
// IPv6: find closing bracket, port separator must be after it
const bracket_end = std.mem.indexOfScalar(u8, host, ']') orelse return null;
if (bracket_end + 1 < host.len and host[bracket_end + 1] == ':') {
return bracket_end + 1;
}
return null;
}
// Regular host: find last colon and verify it's followed by digits
const pos = std.mem.lastIndexOfScalar(u8, host, ':') orelse return null;
if (pos + 1 >= host.len) return null;
for (host[pos + 1 ..]) |c| {
if (c < '0' or c > '9') {
return "";
if (c < '0' or c > '9') return null;
}
}
return host[pos + 1 ..];
return pos;
}
pub fn getSearch(raw: [:0]const u8) []const u8 {
@@ -385,21 +416,12 @@ pub fn getOrigin(allocator: Allocator, raw: [:0]const u8) !?[]const u8 {
return null;
}
var authority_start = scheme_end + 3;
const has_user_info = if (std.mem.indexOf(u8, raw[authority_start..], "@")) |pos| blk: {
authority_start += pos + 1;
break :blk true;
} else false;
// Find end of authority (start of path/query/fragment or end of string)
const authority_end_relative = std.mem.indexOfAny(u8, raw[authority_start..], "/?#");
const authority_end = if (authority_end_relative) |end|
authority_start + end
else
raw.len;
const auth = parseAuthority(raw) orelse return null;
const has_user_info = auth.has_user_info;
const authority_end = auth.host_end;
// Check for port in the host:port section
const host_part = raw[authority_start..authority_end];
const host_part = auth.getHost(raw);
if (std.mem.lastIndexOfScalar(u8, host_part, ':')) |colon_pos_in_host| {
const port = host_part[colon_pos_in_host + 1 ..];
@@ -440,31 +462,18 @@ pub fn getOrigin(allocator: Allocator, raw: [:0]const u8) !?[]const u8 {
}
fn getUserInfo(raw: [:0]const u8) ?[]const u8 {
const scheme_end = std.mem.indexOf(u8, raw, "://") orelse return null;
const auth = parseAuthority(raw) orelse return null;
if (!auth.has_user_info) return null;
// User info is from authority_start to host_start - 1 (excluding the @)
const scheme_end = std.mem.indexOf(u8, raw, "://").?;
const authority_start = scheme_end + 3;
const pos = std.mem.indexOfScalar(u8, raw[authority_start..], '@') orelse return null;
const path_start = std.mem.indexOfScalarPos(u8, raw, authority_start, '/') orelse raw.len;
const full_pos = authority_start + pos;
if (full_pos < path_start) {
return raw[authority_start..full_pos];
}
return null;
return raw[authority_start .. auth.host_start - 1];
}
pub fn getHost(raw: [:0]const u8) []const u8 {
const scheme_end = std.mem.indexOf(u8, raw, "://") orelse return "";
var authority_start = scheme_end + 3;
if (std.mem.indexOf(u8, raw[authority_start..], "@")) |pos| {
authority_start += pos + 1;
}
const authority = raw[authority_start..];
const path_start = std.mem.indexOfAny(u8, authority, "/?#") orelse return authority;
return authority[0..path_start];
const auth = parseAuthority(raw) orelse return "";
return auth.getHost(raw);
}
// Returns true if these two URLs point to the same document.
@@ -514,7 +523,7 @@ pub fn setHost(current: [:0]const u8, value: []const u8, allocator: Allocator) !
const search = getSearch(current);
const hash = getHash(current);
// Check if the host includes a port
// Check if the new value includes a port
const colon_pos = std.mem.lastIndexOfScalar(u8, value, ':');
const clean_host = if (colon_pos) |pos| blk: {
const port_str = value[pos + 1 ..];
@@ -526,7 +535,14 @@ pub fn setHost(current: [:0]const u8, value: []const u8, allocator: Allocator) !
break :blk value[0..pos];
}
break :blk value;
} else value;
} else blk: {
// No port in new value - preserve existing port
const current_port = getPort(current);
if (current_port.len > 0) {
break :blk try std.fmt.allocPrint(allocator, "{s}:{s}", .{ value, current_port });
}
break :blk value;
};
return buildUrl(allocator, protocol, clean_host, pathname, search, hash);
}
@@ -544,6 +560,9 @@ pub fn setHostname(current: [:0]const u8, value: []const u8, allocator: Allocato
pub fn setPort(current: [:0]const u8, value: ?[]const u8, allocator: Allocator) ![:0]const u8 {
const hostname = getHostname(current);
const protocol = getProtocol(current);
const pathname = getPathname(current);
const search = getSearch(current);
const hash = getHash(current);
// Handle null or default ports
const new_host = if (value) |port_str| blk: {
@@ -560,7 +579,7 @@ pub fn setPort(current: [:0]const u8, value: ?[]const u8, allocator: Allocator)
break :blk try std.fmt.allocPrint(allocator, "{s}:{s}", .{ hostname, port_str });
} else hostname;
return setHost(current, new_host, allocator);
return buildUrl(allocator, protocol, new_host, pathname, search, hash);
}
pub fn setPathname(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {
@@ -569,11 +588,13 @@ pub fn setPathname(current: [:0]const u8, value: []const u8, allocator: Allocato
const search = getSearch(current);
const hash = getHash(current);
const encoded = try percentEncodeSegment(allocator, value, .path);
// Add / prefix if not present and value is not empty
const pathname = if (value.len > 0 and value[0] != '/')
try std.fmt.allocPrint(allocator, "/{s}", .{value})
const pathname = if (encoded.len > 0 and encoded[0] != '/')
try std.fmt.allocPrint(allocator, "/{s}", .{encoded})
else
value;
encoded;
return buildUrl(allocator, protocol, host, pathname, search, hash);
}
@@ -584,11 +605,13 @@ pub fn setSearch(current: [:0]const u8, value: []const u8, allocator: Allocator)
const pathname = getPathname(current);
const hash = getHash(current);
const encoded = try percentEncodeSegment(allocator, value, .query);
// Add ? prefix if not present and value is not empty
const search = if (value.len > 0 and value[0] != '?')
try std.fmt.allocPrint(allocator, "?{s}", .{value})
const search = if (encoded.len > 0 and value[0] != '?')
try std.fmt.allocPrint(allocator, "?{s}", .{encoded})
else
value;
encoded;
return buildUrl(allocator, protocol, host, pathname, search, hash);
}
@@ -599,15 +622,75 @@ pub fn setHash(current: [:0]const u8, value: []const u8, allocator: Allocator) !
const pathname = getPathname(current);
const search = getSearch(current);
const encoded = try percentEncodeSegment(allocator, value, .fragment);
// Add # prefix if not present and value is not empty
const hash = if (value.len > 0 and value[0] != '#')
try std.fmt.allocPrint(allocator, "#{s}", .{value})
const hash = if (encoded.len > 0 and encoded[0] != '#')
try std.fmt.allocPrint(allocator, "#{s}", .{encoded})
else
value;
encoded;
return buildUrl(allocator, protocol, host, pathname, search, hash);
}
pub fn setUsername(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {
const protocol = getProtocol(current);
const host = getHost(current);
const pathname = getPathname(current);
const search = getSearch(current);
const hash = getHash(current);
const password = getPassword(current);
const encoded_username = try percentEncodeSegment(allocator, value, .userinfo);
return buildUrlWithUserInfo(allocator, protocol, encoded_username, password, host, pathname, search, hash);
}
pub fn setPassword(current: [:0]const u8, value: []const u8, allocator: Allocator) ![:0]const u8 {
const protocol = getProtocol(current);
const host = getHost(current);
const pathname = getPathname(current);
const search = getSearch(current);
const hash = getHash(current);
const username = getUsername(current);
const encoded_password = try percentEncodeSegment(allocator, value, .userinfo);
return buildUrlWithUserInfo(allocator, protocol, username, encoded_password, host, pathname, search, hash);
}
fn buildUrlWithUserInfo(
allocator: Allocator,
protocol: []const u8,
username: []const u8,
password: []const u8,
host: []const u8,
pathname: []const u8,
search: []const u8,
hash: []const u8,
) ![:0]const u8 {
if (username.len == 0 and password.len == 0) {
return buildUrl(allocator, protocol, host, pathname, search, hash);
} else if (password.len == 0) {
return std.fmt.allocPrintSentinel(allocator, "{s}//{s}@{s}{s}{s}{s}", .{
protocol,
username,
host,
pathname,
search,
hash,
}, 0);
} else {
return std.fmt.allocPrintSentinel(allocator, "{s}//{s}:{s}@{s}{s}{s}{s}", .{
protocol,
username,
password,
host,
pathname,
search,
hash,
}, 0);
}
}
pub fn concatQueryString(arena: Allocator, url: []const u8, query_string: []const u8) ![:0]const u8 {
if (query_string.len == 0) {
return arena.dupeZ(u8, url);
@@ -669,6 +752,47 @@ pub fn unescape(arena: Allocator, input: []const u8) ![]const u8 {
return result.items;
}
const AuthorityInfo = struct {
host_start: usize,
host_end: usize,
has_user_info: bool,
fn getHost(self: AuthorityInfo, raw: []const u8) []const u8 {
return raw[self.host_start..self.host_end];
}
};
// Parses the authority component of a URL, correctly handling userinfo.
// Returns null if the URL doesn't have a valid scheme (no "://").
// SECURITY: Only looks for @ within the authority portion (before /?#)
// to prevent path-based @ injection attacks.
fn parseAuthority(raw: []const u8) ?AuthorityInfo {
const scheme_end = std.mem.indexOf(u8, raw, "://") orelse return null;
const authority_start = scheme_end + 3;
// Find end of authority FIRST (start of path/query/fragment or end of string)
const authority_end = if (std.mem.indexOfAny(u8, raw[authority_start..], "/?#")) |end|
authority_start + end
else
raw.len;
// Only look for @ within the authority portion, not in path/query/fragment
const authority_portion = raw[authority_start..authority_end];
if (std.mem.indexOf(u8, authority_portion, "@")) |pos| {
return .{
.host_start = authority_start + pos + 1,
.host_end = authority_end,
.has_user_info = true,
};
}
return .{
.host_start = authority_start,
.host_end = authority_end,
.has_user_info = false,
};
}
const testing = @import("../testing.zig");
test "URL: isCompleteHTTPUrl" {
try testing.expectEqual(true, isCompleteHTTPUrl("http://example.com/about"));
@@ -1329,3 +1453,120 @@ test "URL: unescape" {
try testing.expectEqual("hello%2", result);
}
}
test "URL: getHost" {
try testing.expectEqualSlices(u8, "example.com:8080", getHost("https://example.com:8080/path"));
try testing.expectEqualSlices(u8, "example.com", getHost("https://example.com/path"));
try testing.expectEqualSlices(u8, "example.com:443", getHost("https://example.com:443/"));
try testing.expectEqualSlices(u8, "example.com", getHost("https://user:pass@example.com/page"));
try testing.expectEqualSlices(u8, "example.com:8080", getHost("https://user:pass@example.com:8080/page"));
try testing.expectEqualSlices(u8, "", getHost("not-a-url"));
// SECURITY: @ in path must NOT be treated as userinfo separator
try testing.expectEqualSlices(u8, "evil.example.com", getHost("http://evil.example.com/@victim.example.com/"));
try testing.expectEqualSlices(u8, "evil.example.com", getHost("https://evil.example.com/path/@victim.example.com"));
// IPv6 addresses
try testing.expectEqualSlices(u8, "[::1]:8080", getHost("http://[::1]:8080/path"));
try testing.expectEqualSlices(u8, "[::1]", getHost("http://[::1]/path"));
try testing.expectEqualSlices(u8, "[2001:db8::1]", getHost("https://[2001:db8::1]/"));
}
test "URL: getHostname" {
// Regular hosts
try testing.expectEqualSlices(u8, "example.com", getHostname("https://example.com:8080/path"));
try testing.expectEqualSlices(u8, "example.com", getHostname("https://example.com/path"));
// IPv6 with port
try testing.expectEqualSlices(u8, "[::1]", getHostname("http://[::1]:8080/path"));
// IPv6 without port - must return full bracket notation
try testing.expectEqualSlices(u8, "[::1]", getHostname("http://[::1]/path"));
try testing.expectEqualSlices(u8, "[2001:db8::1]", getHostname("https://[2001:db8::1]/"));
}
test "URL: getPort" {
// Regular hosts
try testing.expectEqualSlices(u8, "8080", getPort("https://example.com:8080/path"));
try testing.expectEqualSlices(u8, "", getPort("https://example.com/path"));
// IPv6 with port
try testing.expectEqualSlices(u8, "8080", getPort("http://[::1]:8080/path"));
try testing.expectEqualSlices(u8, "3000", getPort("http://[2001:db8::1]:3000/"));
// IPv6 without port - colons inside brackets must not be treated as port separator
try testing.expectEqualSlices(u8, "", getPort("http://[::1]/path"));
try testing.expectEqualSlices(u8, "", getPort("https://[2001:db8::1]/"));
}
test "URL: setPathname percent-encodes" {
// Use arena allocator to match production usage (setPathname makes intermediate allocations)
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Spaces must be encoded as %20
const result1 = try setPathname("http://a/", "c d", allocator);
try testing.expectEqualSlices(u8, "http://a/c%20d", result1);
// Already-encoded sequences must not be double-encoded
const result2 = try setPathname("https://example.com/path", "/already%20encoded", allocator);
try testing.expectEqualSlices(u8, "https://example.com/already%20encoded", result2);
// Query and hash must be preserved
const result3 = try setPathname("https://example.com/path?a=b#hash", "/new path", allocator);
try testing.expectEqualSlices(u8, "https://example.com/new%20path?a=b#hash", result3);
}
test "URL: getOrigin" {
defer testing.reset();
const Case = struct {
url: [:0]const u8,
expected: ?[]const u8,
};
const cases = [_]Case{
// Basic HTTP/HTTPS origins
.{ .url = "http://example.com/path", .expected = "http://example.com" },
.{ .url = "https://example.com/path", .expected = "https://example.com" },
.{ .url = "https://example.com:8080/path", .expected = "https://example.com:8080" },
// Default ports should be stripped
.{ .url = "http://example.com:80/path", .expected = "http://example.com" },
.{ .url = "https://example.com:443/path", .expected = "https://example.com" },
// User info should be stripped from origin
.{ .url = "http://user:pass@example.com/path", .expected = "http://example.com" },
.{ .url = "https://user@example.com:8080/path", .expected = "https://example.com:8080" },
// Non-HTTP schemes return null
.{ .url = "ftp://example.com/path", .expected = null },
.{ .url = "file:///path/to/file", .expected = null },
.{ .url = "about:blank", .expected = null },
// Query and fragment should not affect origin
.{ .url = "https://example.com?query=1", .expected = "https://example.com" },
.{ .url = "https://example.com#fragment", .expected = "https://example.com" },
.{ .url = "https://example.com/path?q=1#frag", .expected = "https://example.com" },
// SECURITY: @ in path must NOT be treated as userinfo separator
// This would be a Same-Origin Policy bypass if mishandled
.{ .url = "http://evil.example.com/@victim.example.com/", .expected = "http://evil.example.com" },
.{ .url = "https://evil.example.com/path/@victim.example.com/steal", .expected = "https://evil.example.com" },
.{ .url = "http://evil.example.com/@victim.example.com:443/", .expected = "http://evil.example.com" },
// @ in query/fragment must also not affect origin
.{ .url = "https://example.com/path?user=foo@bar.com", .expected = "https://example.com" },
.{ .url = "https://example.com/path#user@host", .expected = "https://example.com" },
};
for (cases) |case| {
const result = try getOrigin(testing.arena_allocator, case.url);
if (case.expected) |expected| {
try testing.expectString(expected, result.?);
} else {
try testing.expectEqual(null, result);
}
}
}

137
src/browser/actions.zig Normal file
View File

@@ -0,0 +1,137 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("../lightpanda.zig");
const DOMNode = @import("webapi/Node.zig");
const Element = @import("webapi/Element.zig");
const Event = @import("webapi/Event.zig");
const MouseEvent = @import("webapi/event/MouseEvent.zig");
const Page = @import("Page.zig");
const Session = @import("Session.zig");
const Selector = @import("webapi/selector/Selector.zig");
pub fn click(node: *DOMNode, page: *Page) !void {
const el = node.is(Element) orelse return error.InvalidNodeType;
const mouse_event: *MouseEvent = try .initTrusted(comptime .wrap("click"), .{
.bubbles = true,
.cancelable = true,
.composed = true,
.clientX = 0,
.clientY = 0,
}, page);
page._event_manager.dispatch(el.asEventTarget(), mouse_event.asEvent()) catch |err| {
lp.log.err(.app, "click failed", .{ .err = err });
return error.ActionFailed;
};
}
pub fn fill(node: *DOMNode, text: []const u8, page: *Page) !void {
const el = node.is(Element) orelse return error.InvalidNodeType;
if (el.is(Element.Html.Input)) |input| {
input.setValue(text, page) catch |err| {
lp.log.err(.app, "fill input failed", .{ .err = err });
return error.ActionFailed;
};
} else if (el.is(Element.Html.TextArea)) |textarea| {
textarea.setValue(text, page) catch |err| {
lp.log.err(.app, "fill textarea failed", .{ .err = err });
return error.ActionFailed;
};
} else if (el.is(Element.Html.Select)) |select| {
select.setValue(text, page) catch |err| {
lp.log.err(.app, "fill select failed", .{ .err = err });
return error.ActionFailed;
};
} else {
return error.InvalidNodeType;
}
const input_evt: *Event = try .initTrusted(comptime .wrap("input"), .{ .bubbles = true }, page);
page._event_manager.dispatch(el.asEventTarget(), input_evt) catch |err| {
lp.log.err(.app, "dispatch input event failed", .{ .err = err });
};
const change_evt: *Event = try .initTrusted(comptime .wrap("change"), .{ .bubbles = true }, page);
page._event_manager.dispatch(el.asEventTarget(), change_evt) catch |err| {
lp.log.err(.app, "dispatch change event failed", .{ .err = err });
};
}
pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, page: *Page) !void {
if (node) |n| {
const el = n.is(Element) orelse return error.InvalidNodeType;
if (x) |val| {
el.setScrollLeft(val, page) catch |err| {
lp.log.err(.app, "setScrollLeft failed", .{ .err = err });
return error.ActionFailed;
};
}
if (y) |val| {
el.setScrollTop(val, page) catch |err| {
lp.log.err(.app, "setScrollTop failed", .{ .err = err });
return error.ActionFailed;
};
}
const scroll_evt: *Event = try .initTrusted(comptime .wrap("scroll"), .{ .bubbles = true }, page);
page._event_manager.dispatch(el.asEventTarget(), scroll_evt) catch |err| {
lp.log.err(.app, "dispatch scroll event failed", .{ .err = err });
};
} else {
page.window.scrollTo(.{ .x = x orelse 0 }, y, page) catch |err| {
lp.log.err(.app, "scroll failed", .{ .err = err });
return error.ActionFailed;
};
}
}
pub fn waitForSelector(selector: [:0]const u8, timeout_ms: u32, session: *Session) !*DOMNode {
var timer = try std.time.Timer.start();
var runner = try session.runner(.{});
try runner.wait(.{ .ms = timeout_ms, .until = .load });
while (true) {
const page = runner.page;
const element = Selector.querySelector(page.document.asNode(), selector, page) catch {
return error.InvalidSelector;
};
if (element) |el| {
return el.asNode();
}
const elapsed: u32 = @intCast(timer.read() / std.time.ns_per_ms);
if (elapsed >= timeout_ms) {
return error.Timeout;
}
switch (try runner.tick(.{ .ms = timeout_ms - elapsed })) {
.done => return error.Timeout,
.ok => |recommended_sleep_ms| {
if (recommended_sleep_ms > 0) {
// guanrateed to be <= 20ms
std.Thread.sleep(std.time.ns_per_ms * recommended_sleep_ms);
}
},
}
}
}

View File

@@ -293,3 +293,191 @@ fn isBang(token: Tokenizer.Token) bool {
else => false,
};
}
pub const Rule = struct {
selector: []const u8,
block: []const u8,
};
pub fn parseStylesheet(input: []const u8) RulesIterator {
return RulesIterator.init(input);
}
pub const RulesIterator = struct {
input: []const u8,
stream: TokenStream,
has_skipped_at_rule: bool = false,
pub fn init(input: []const u8) RulesIterator {
return .{
.input = input,
.stream = TokenStream.init(input),
};
}
pub fn next(self: *RulesIterator) ?Rule {
var selector_start: ?usize = null;
var selector_end: ?usize = null;
while (true) {
const peeked = self.stream.peek() orelse return null;
if (peeked.token == .curly_bracket_block) {
if (selector_start == null) {
self.skipBlock();
continue;
}
const open_brace = self.stream.next() orelse return null;
const block_start = open_brace.end;
var block_end = block_start;
var depth: usize = 1;
while (true) {
const span = self.stream.next() orelse {
block_end = self.input.len;
break;
};
if (span.token == .curly_bracket_block) {
depth += 1;
} else if (span.token == .close_curly_bracket) {
depth -= 1;
if (depth == 0) {
block_end = span.start;
break;
}
}
}
var selector = self.input[selector_start.?..selector_end.?];
selector = std.mem.trim(u8, selector, &std.ascii.whitespace);
return .{
.selector = selector,
.block = self.input[block_start..block_end],
};
}
if (peeked.token == .at_keyword) {
self.has_skipped_at_rule = true;
self.skipAtRule();
selector_start = null;
selector_end = null;
continue;
}
if (selector_start == null and (isWhitespaceOrComment(peeked.token) or isSemicolon(peeked.token))) {
_ = self.stream.next();
continue;
}
const span = self.stream.next() orelse return null;
if (!isWhitespaceOrComment(span.token)) {
if (selector_start == null) selector_start = span.start;
selector_end = span.end;
}
}
}
fn skipBlock(self: *RulesIterator) void {
const span = self.stream.next() orelse return;
if (span.token != .curly_bracket_block) return;
var depth: usize = 1;
while (true) {
const next_span = self.stream.next() orelse return;
if (next_span.token == .curly_bracket_block) {
depth += 1;
} else if (next_span.token == .close_curly_bracket) {
depth -= 1;
if (depth == 0) return;
}
}
}
fn skipAtRule(self: *RulesIterator) void {
_ = self.stream.next(); // consume @keyword
var depth: usize = 0;
var saw_block = false;
while (true) {
const peeked = self.stream.peek() orelse return;
if (!saw_block and isSemicolon(peeked.token) and depth == 0) {
_ = self.stream.next();
return;
}
const span = self.stream.next() orelse return;
if (isWhitespaceOrComment(span.token)) continue;
if (span.token == .curly_bracket_block) {
depth += 1;
saw_block = true;
} else if (span.token == .close_curly_bracket) {
if (depth > 0) depth -= 1;
if (saw_block and depth == 0) return;
}
}
}
};
const testing = std.testing;
test "RulesIterator: single rule" {
var it = RulesIterator.init(".test { color: red; }");
const rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings(".test", rule.selector);
try testing.expectEqualStrings(" color: red; ", rule.block);
try testing.expectEqual(@as(?Rule, null), it.next());
}
test "RulesIterator: multiple rules" {
var it = RulesIterator.init("h1 { margin: 0; } p { padding: 10px; }");
var rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings("h1", rule.selector);
try testing.expectEqualStrings(" margin: 0; ", rule.block);
rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings("p", rule.selector);
try testing.expectEqualStrings(" padding: 10px; ", rule.block);
try testing.expectEqual(@as(?Rule, null), it.next());
}
test "RulesIterator: skips at-rules without block" {
var it = RulesIterator.init("@import url('style.css'); .test { color: red; }");
const rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings(".test", rule.selector);
try testing.expectEqualStrings(" color: red; ", rule.block);
try testing.expectEqual(@as(?Rule, null), it.next());
}
test "RulesIterator: skips at-rules with block" {
var it = RulesIterator.init("@media screen { .test { color: blue; } } .test2 { color: green; }");
const rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings(".test2", rule.selector);
try testing.expectEqualStrings(" color: green; ", rule.block);
try testing.expectEqual(@as(?Rule, null), it.next());
}
test "RulesIterator: comments and whitespace" {
var it = RulesIterator.init(" /* comment */ .test /* comment */ { /* comment */ color: red; } \n\t");
const rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings(".test", rule.selector);
try testing.expectEqualStrings(" /* comment */ color: red; ", rule.block);
try testing.expectEqual(@as(?Rule, null), it.next());
}
test "RulesIterator: top-level semicolons" {
var it = RulesIterator.init("*{}; ; p{}");
var rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings("*", rule.selector);
rule = it.next() orelse return error.MissingRule;
try testing.expectEqualStrings("p", rule.selector);
try testing.expectEqual(@as(?Rule, null), it.next());
}

460
src/browser/forms.zig Normal file
View File

@@ -0,0 +1,460 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("Page.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const Allocator = std.mem.Allocator;
pub const SelectOption = struct {
value: []const u8,
text: []const u8,
pub fn jsonStringify(self: *const SelectOption, jw: anytype) !void {
try jw.beginObject();
try jw.objectField("value");
try jw.write(self.value);
try jw.objectField("text");
try jw.write(self.text);
try jw.endObject();
}
};
pub const FormField = struct {
backendNodeId: ?u32 = null,
node: *Node,
tag_name: []const u8,
name: ?[]const u8,
input_type: ?[]const u8,
required: bool,
disabled: bool,
value: ?[]const u8,
placeholder: ?[]const u8,
options: []SelectOption,
pub fn jsonStringify(self: *const FormField, jw: anytype) !void {
try jw.beginObject();
if (self.backendNodeId) |id| {
try jw.objectField("backendNodeId");
try jw.write(id);
}
try jw.objectField("tagName");
try jw.write(self.tag_name);
if (self.name) |v| {
try jw.objectField("name");
try jw.write(v);
}
if (self.input_type) |v| {
try jw.objectField("inputType");
try jw.write(v);
}
try jw.objectField("required");
try jw.write(self.required);
try jw.objectField("disabled");
try jw.write(self.disabled);
if (self.value) |v| {
try jw.objectField("value");
try jw.write(v);
}
if (self.placeholder) |v| {
try jw.objectField("placeholder");
try jw.write(v);
}
if (self.options.len > 0) {
try jw.objectField("options");
try jw.beginArray();
for (self.options) |opt| {
try opt.jsonStringify(jw);
}
try jw.endArray();
}
try jw.endObject();
}
};
pub const FormInfo = struct {
backendNodeId: ?u32 = null,
node: *Node,
action: ?[]const u8,
method: ?[]const u8,
fields: []FormField,
pub fn jsonStringify(self: *const FormInfo, jw: anytype) !void {
try jw.beginObject();
if (self.backendNodeId) |id| {
try jw.objectField("backendNodeId");
try jw.write(id);
}
if (self.action) |v| {
try jw.objectField("action");
try jw.write(v);
}
if (self.method) |v| {
try jw.objectField("method");
try jw.write(v);
}
try jw.objectField("fields");
try jw.beginArray();
for (self.fields) |field| {
try field.jsonStringify(jw);
}
try jw.endArray();
try jw.endObject();
}
};
/// Populate backendNodeId on each form and its fields by registering
/// their nodes in the given registry. Works with both CDP and MCP registries.
pub fn registerNodes(forms_data: []FormInfo, registry: anytype) !void {
for (forms_data) |*form| {
const form_registered = try registry.register(form.node);
form.backendNodeId = form_registered.id;
for (form.fields) |*field| {
const field_registered = try registry.register(field.node);
field.backendNodeId = field_registered.id;
}
}
}
/// Collect all forms and their fields under `root`.
/// Uses Form.getElements() to include fields outside the <form> that
/// reference it via the form="id" attribute, matching browser behavior.
/// `arena` must be an arena allocator — returned slices borrow its memory.
pub fn collectForms(
arena: Allocator,
root: *Node,
page: *Page,
) ![]FormInfo {
var forms: std.ArrayList(FormInfo) = .empty;
var tw = TreeWalker.Full.init(root, .{});
while (tw.next()) |node| {
const form = node.is(Element.Html.Form) orelse continue;
const el = form.asElement();
const fields = try collectFormFields(arena, form, page);
if (fields.len == 0) continue;
const action_attr = el.getAttributeSafe(comptime .wrap("action"));
const method_str = form.getMethod();
try forms.append(arena, .{
.node = node,
.action = if (action_attr) |a| if (a.len > 0) a else null else null,
.method = method_str,
.fields = fields,
});
}
return forms.items;
}
fn collectFormFields(
arena: Allocator,
form: *Element.Html.Form,
page: *Page,
) ![]FormField {
var fields: std.ArrayList(FormField) = .empty;
var elements = try form.getElements(page);
var it = try elements.iterator();
while (it.next()) |el| {
const node = el.asNode();
const is_disabled = el.isDisabled();
if (el.is(Element.Html.Input)) |input| {
if (input._input_type == .hidden) continue;
if (input._input_type == .submit or input._input_type == .button or input._input_type == .image) continue;
try fields.append(arena, .{
.node = node,
.tag_name = "input",
.name = el.getAttributeSafe(comptime .wrap("name")),
.input_type = input._input_type.toString(),
.required = el.getAttributeSafe(comptime .wrap("required")) != null,
.disabled = is_disabled,
.value = input.getValue(),
.placeholder = el.getAttributeSafe(comptime .wrap("placeholder")),
.options = &.{},
});
continue;
}
if (el.is(Element.Html.TextArea)) |textarea| {
try fields.append(arena, .{
.node = node,
.tag_name = "textarea",
.name = el.getAttributeSafe(comptime .wrap("name")),
.input_type = null,
.required = el.getAttributeSafe(comptime .wrap("required")) != null,
.disabled = is_disabled,
.value = textarea.getValue(),
.placeholder = el.getAttributeSafe(comptime .wrap("placeholder")),
.options = &.{},
});
continue;
}
if (el.is(Element.Html.Select)) |select| {
const options = try collectSelectOptions(arena, node, page);
try fields.append(arena, .{
.node = node,
.tag_name = "select",
.name = el.getAttributeSafe(comptime .wrap("name")),
.input_type = null,
.required = el.getAttributeSafe(comptime .wrap("required")) != null,
.disabled = is_disabled,
.value = select.getValue(page),
.placeholder = null,
.options = options,
});
continue;
}
// Button elements from getElements() - skip (not fillable)
}
return fields.items;
}
fn collectSelectOptions(
arena: Allocator,
select_node: *Node,
page: *Page,
) ![]SelectOption {
var options: std.ArrayList(SelectOption) = .empty;
const Option = Element.Html.Option;
var tw = TreeWalker.Full.init(select_node, .{});
while (tw.next()) |node| {
const el = node.is(Element) orelse continue;
const option = el.is(Option) orelse continue;
try options.append(arena, .{
.value = option.getValue(page),
.text = option.getText(page),
});
}
return options.items;
}
const testing = @import("../testing.zig");
fn testForms(html: []const u8) ![]FormInfo {
const page = try testing.test_session.createPage();
const doc = page.window._document;
const div = try doc.createElement("div", null, page);
try page.parseHtmlAsChildren(div.asNode(), html);
return collectForms(page.call_arena, div.asNode(), page);
}
test "browser.forms: login form" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form action="/login" method="POST">
\\ <input type="email" name="email" required placeholder="Email">
\\ <input type="password" name="password" required>
\\ <input type="submit" value="Log In">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual("/login", forms[0].action.?);
try testing.expectEqual("post", forms[0].method.?);
try testing.expectEqual(2, forms[0].fields.len);
try testing.expectEqual("email", forms[0].fields[0].name.?);
try testing.expectEqual("email", forms[0].fields[0].input_type.?);
try testing.expect(forms[0].fields[0].required);
try testing.expect(!forms[0].fields[0].disabled);
try testing.expectEqual("password", forms[0].fields[1].name.?);
}
test "browser.forms: form with select" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <select name="color">
\\ <option value="red">Red</option>
\\ <option value="blue">Blue</option>
\\ </select>
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(1, forms[0].fields.len);
try testing.expectEqual("select", forms[0].fields[0].tag_name);
try testing.expectEqual(2, forms[0].fields[0].options.len);
try testing.expectEqual("red", forms[0].fields[0].options[0].value);
try testing.expectEqual("Red", forms[0].fields[0].options[0].text);
}
test "browser.forms: form with textarea" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form method="POST">
\\ <textarea name="message" placeholder="Your message"></textarea>
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(1, forms[0].fields.len);
try testing.expectEqual("textarea", forms[0].fields[0].tag_name);
try testing.expectEqual("Your message", forms[0].fields[0].placeholder.?);
}
test "browser.forms: empty form skipped" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form action="/empty">
\\ <p>No fields here</p>
\\</form>
);
try testing.expectEqual(0, forms.len);
}
test "browser.forms: hidden inputs excluded" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <input type="hidden" name="csrf" value="token123">
\\ <input type="text" name="username">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(1, forms[0].fields.len);
try testing.expectEqual("username", forms[0].fields[0].name.?);
}
test "browser.forms: multiple forms" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form action="/search" method="GET">
\\ <input type="text" name="q" placeholder="Search">
\\</form>
\\<form action="/login" method="POST">
\\ <input type="email" name="email">
\\ <input type="password" name="pass">
\\</form>
);
try testing.expectEqual(2, forms.len);
try testing.expectEqual(1, forms[0].fields.len);
try testing.expectEqual(2, forms[1].fields.len);
}
test "browser.forms: disabled fields flagged" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <input type="text" name="enabled_field">
\\ <input type="text" name="disabled_field" disabled>
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(2, forms[0].fields.len);
try testing.expect(!forms[0].fields[0].disabled);
try testing.expect(forms[0].fields[1].disabled);
}
test "browser.forms: disabled fieldset" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <fieldset disabled>
\\ <input type="text" name="in_disabled_fieldset">
\\ </fieldset>
\\ <input type="text" name="outside_fieldset">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(2, forms[0].fields.len);
try testing.expect(forms[0].fields[0].disabled);
try testing.expect(!forms[0].fields[1].disabled);
}
test "browser.forms: external field via form attribute" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<input type="text" name="external" form="myform">
\\<form id="myform" action="/submit">
\\ <input type="text" name="internal">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(2, forms[0].fields.len);
}
test "browser.forms: checkbox and radio return value attribute" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <input type="checkbox" name="agree" value="yes" checked>
\\ <input type="radio" name="color" value="red">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(2, forms[0].fields.len);
try testing.expectEqual("checkbox", forms[0].fields[0].input_type.?);
try testing.expectEqual("yes", forms[0].fields[0].value.?);
try testing.expectEqual("radio", forms[0].fields[1].input_type.?);
try testing.expectEqual("red", forms[0].fields[1].value.?);
}
test "browser.forms: form without action or method" {
defer testing.reset();
defer testing.test_session.removePage();
const forms = try testForms(
\\<form>
\\ <input type="text" name="q">
\\</form>
);
try testing.expectEqual(1, forms.len);
try testing.expectEqual(null, forms[0].action);
try testing.expectEqual("get", forms[0].method.?);
try testing.expectEqual(1, forms[0].fields.len);
}

577
src/browser/interactive.zig Normal file
View File

@@ -0,0 +1,577 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("Page.zig");
const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const EventTarget = @import("webapi/EventTarget.zig");
const Allocator = std.mem.Allocator;
pub const InteractivityType = enum {
native,
aria,
contenteditable,
listener,
focusable,
};
pub const InteractiveElement = struct {
backendNodeId: ?u32 = null,
node: *Node,
tag_name: []const u8,
role: ?[]const u8,
name: ?[]const u8,
interactivity_type: InteractivityType,
listener_types: []const []const u8,
disabled: bool,
tab_index: i32,
id: ?[]const u8,
class: ?[]const u8,
href: ?[]const u8,
input_type: ?[]const u8,
value: ?[]const u8,
element_name: ?[]const u8,
placeholder: ?[]const u8,
pub fn jsonStringify(self: *const InteractiveElement, jw: anytype) !void {
try jw.beginObject();
if (self.backendNodeId) |id| {
try jw.objectField("backendNodeId");
try jw.write(id);
}
try jw.objectField("tagName");
try jw.write(self.tag_name);
try jw.objectField("role");
try jw.write(self.role);
try jw.objectField("name");
try jw.write(self.name);
try jw.objectField("type");
try jw.write(@tagName(self.interactivity_type));
if (self.listener_types.len > 0) {
try jw.objectField("listeners");
try jw.beginArray();
for (self.listener_types) |lt| {
try jw.write(lt);
}
try jw.endArray();
}
if (self.disabled) {
try jw.objectField("disabled");
try jw.write(true);
}
try jw.objectField("tabIndex");
try jw.write(self.tab_index);
if (self.id) |v| {
try jw.objectField("id");
try jw.write(v);
}
if (self.class) |v| {
try jw.objectField("class");
try jw.write(v);
}
if (self.href) |v| {
try jw.objectField("href");
try jw.write(v);
}
if (self.input_type) |v| {
try jw.objectField("inputType");
try jw.write(v);
}
if (self.value) |v| {
try jw.objectField("value");
try jw.write(v);
}
if (self.element_name) |v| {
try jw.objectField("elementName");
try jw.write(v);
}
if (self.placeholder) |v| {
try jw.objectField("placeholder");
try jw.write(v);
}
try jw.endObject();
}
};
/// Populate backendNodeId on each interactive element by registering
/// their nodes in the given registry. Works with both CDP and MCP registries.
pub fn registerNodes(elements: []InteractiveElement, registry: anytype) !void {
for (elements) |*el| {
const registered = try registry.register(el.node);
el.backendNodeId = registered.id;
}
}
/// Collect all interactive elements under `root`.
pub fn collectInteractiveElements(
root: *Node,
arena: Allocator,
page: *Page,
) ![]InteractiveElement {
// Pre-build a map of event_target pointer → event type names,
// so classify and getListenerTypes are both O(1) per element.
const listener_targets = try buildListenerTargetMap(page, arena);
var css_cache: Element.PointerEventsCache = .empty;
var results: std.ArrayList(InteractiveElement) = .empty;
var tw = TreeWalker.Full.init(root, .{});
while (tw.next()) |node| {
const el = node.is(Element) orelse continue;
const html_el = el.is(Element.Html) orelse continue;
// Skip non-visual elements that are never user-interactive.
switch (el.getTag()) {
.script, .style, .link, .meta, .head, .noscript, .template => continue,
else => {},
}
const itype = classifyInteractivity(page, el, html_el, listener_targets, &css_cache) orelse continue;
const listener_types = getListenerTypes(
el.asEventTarget(),
listener_targets,
);
try results.append(arena, .{
.node = node,
.tag_name = el.getTagNameLower(),
.role = getRole(el),
.name = try getAccessibleName(el, arena),
.interactivity_type = itype,
.listener_types = listener_types,
.disabled = el.isDisabled(),
.tab_index = html_el.getTabIndex(),
.id = el.getAttributeSafe(comptime .wrap("id")),
.class = el.getAttributeSafe(comptime .wrap("class")),
.href = if (el.getAttributeSafe(comptime .wrap("href"))) |href|
URL.resolve(arena, page.base(), href, .{ .encode = true }) catch href
else
null,
.input_type = getInputType(el),
.value = getInputValue(el),
.element_name = el.getAttributeSafe(comptime .wrap("name")),
.placeholder = el.getAttributeSafe(comptime .wrap("placeholder")),
});
}
return results.items;
}
pub const ListenerTargetMap = std.AutoHashMapUnmanaged(usize, std.ArrayList([]const u8));
/// Pre-build a map from event_target pointer → list of event type names.
/// This lets both classifyInteractivity (O(1) "has any?") and
/// getListenerTypes (O(1) "which ones?") avoid re-iterating per element.
pub fn buildListenerTargetMap(page: *Page, arena: Allocator) !ListenerTargetMap {
var map = ListenerTargetMap{};
// addEventListener registrations
var it = page._event_manager.lookup.iterator();
while (it.next()) |entry| {
const list = entry.value_ptr.*;
if (list.first != null) {
const gop = try map.getOrPut(arena, entry.key_ptr.event_target);
if (!gop.found_existing) gop.value_ptr.* = .empty;
try gop.value_ptr.append(arena, entry.key_ptr.type_string.str());
}
}
// Inline handlers (onclick, onmousedown, etc.)
var attr_it = page._event_target_attr_listeners.iterator();
while (attr_it.next()) |entry| {
const gop = try map.getOrPut(arena, @intFromPtr(entry.key_ptr.target));
if (!gop.found_existing) gop.value_ptr.* = .empty;
// Strip "on" prefix to get the event type name.
try gop.value_ptr.append(arena, @tagName(entry.key_ptr.handler)[2..]);
}
return map;
}
pub fn classifyInteractivity(
page: *Page,
el: *Element,
html_el: *Element.Html,
listener_targets: ListenerTargetMap,
cache: ?*Element.PointerEventsCache,
) ?InteractivityType {
if (el.hasPointerEventsNone(cache, page)) return null;
// 1. Native interactive by tag
switch (el.getTag()) {
.button, .summary, .details, .select, .textarea => return .native,
.anchor, .area => {
if (el.getAttributeSafe(comptime .wrap("href")) != null) return .native;
},
.input => {
if (el.is(Element.Html.Input)) |input| {
if (input._input_type != .hidden) return .native;
}
},
else => {},
}
// 2. ARIA interactive role
if (el.getAttributeSafe(comptime .wrap("role"))) |role| {
if (isInteractiveRole(role)) return .aria;
}
// 3. contenteditable (15 bytes, exceeds SSO limit for comptime)
if (el.getAttributeSafe(.wrap("contenteditable"))) |ce| {
if (ce.len == 0 or std.ascii.eqlIgnoreCase(ce, "true")) return .contenteditable;
}
// 4. Event listeners (addEventListener or inline handlers)
const et_ptr = @intFromPtr(html_el.asEventTarget());
if (listener_targets.get(et_ptr) != null) return .listener;
// 5. Explicitly focusable via tabindex.
// Only count elements with an EXPLICIT tabindex attribute,
// since getTabIndex() returns 0 for all interactive tags by default
// (including anchors without href and hidden inputs).
if (el.getAttributeSafe(comptime .wrap("tabindex"))) |_| {
if (html_el.getTabIndex() >= 0) return .focusable;
}
return null;
}
pub fn isInteractiveRole(role: []const u8) bool {
const MAX_LEN = "menuitemcheckbox".len;
if (role.len > MAX_LEN) return false;
var buf: [MAX_LEN]u8 = undefined;
const lowered = std.ascii.lowerString(&buf, role);
const interactive_roles = std.StaticStringMap(void).initComptime(.{
.{ "button", {} },
.{ "checkbox", {} },
.{ "combobox", {} },
.{ "iframe", {} },
.{ "link", {} },
.{ "listbox", {} },
.{ "menuitem", {} },
.{ "menuitemcheckbox", {} },
.{ "menuitemradio", {} },
.{ "option", {} },
.{ "radio", {} },
.{ "searchbox", {} },
.{ "slider", {} },
.{ "spinbutton", {} },
.{ "switch", {} },
.{ "tab", {} },
.{ "textbox", {} },
.{ "treeitem", {} },
});
return interactive_roles.has(lowered);
}
pub fn isContentRole(role: []const u8) bool {
const MAX_LEN = "columnheader".len;
if (role.len > MAX_LEN) return false;
var buf: [MAX_LEN]u8 = undefined;
const lowered = std.ascii.lowerString(&buf, role);
const content_roles = std.StaticStringMap(void).initComptime(.{
.{ "article", {} },
.{ "cell", {} },
.{ "columnheader", {} },
.{ "gridcell", {} },
.{ "heading", {} },
.{ "listitem", {} },
.{ "main", {} },
.{ "navigation", {} },
.{ "region", {} },
.{ "rowheader", {} },
});
return content_roles.has(lowered);
}
fn getRole(el: *Element) ?[]const u8 {
// Explicit role attribute takes precedence
if (el.getAttributeSafe(comptime .wrap("role"))) |role| return role;
// Implicit role from tag
return switch (el.getTag()) {
.button, .summary => "button",
.anchor, .area => if (el.getAttributeSafe(comptime .wrap("href")) != null) "link" else null,
.input => blk: {
if (el.is(Element.Html.Input)) |input| {
break :blk switch (input._input_type) {
.text, .tel, .url, .email => "textbox",
.checkbox => "checkbox",
.radio => "radio",
.button, .submit, .reset, .image => "button",
.range => "slider",
.number => "spinbutton",
.search => "searchbox",
else => null,
};
}
break :blk null;
},
.select => "combobox",
.textarea => "textbox",
.details => "group",
else => null,
};
}
fn getAccessibleName(el: *Element, arena: Allocator) !?[]const u8 {
// aria-label
if (el.getAttributeSafe(comptime .wrap("aria-label"))) |v| {
if (v.len > 0) return v;
}
// alt (for img, input[type=image])
if (el.getAttributeSafe(comptime .wrap("alt"))) |v| {
if (v.len > 0) return v;
}
// title
if (el.getAttributeSafe(comptime .wrap("title"))) |v| {
if (v.len > 0) return v;
}
// placeholder
if (el.getAttributeSafe(comptime .wrap("placeholder"))) |v| {
if (v.len > 0) return v;
}
// value (for buttons)
if (el.getTag() == .input) {
if (el.getAttributeSafe(comptime .wrap("value"))) |v| {
if (v.len > 0) return v;
}
}
// Text content (first non-empty text node, trimmed)
return try getTextContent(el.asNode(), arena);
}
fn getTextContent(node: *Node, arena: Allocator) !?[]const u8 {
var tw: TreeWalker.FullExcludeSelf = .init(node, .{});
var arr: std.ArrayList(u8) = .empty;
var single_chunk: ?[]const u8 = null;
while (tw.next()) |child| {
// Skip text inside script/style elements.
if (child.is(Element)) |el| {
switch (el.getTag()) {
.script, .style => {
tw.skipChildren();
continue;
},
else => {},
}
}
if (child.is(Node.CData)) |cdata| {
if (cdata.is(Node.CData.Text)) |text| {
const content = std.mem.trim(u8, text.getWholeText(), &std.ascii.whitespace);
if (content.len > 0) {
if (single_chunk == null and arr.items.len == 0) {
single_chunk = content;
} else {
if (single_chunk) |sc| {
try arr.appendSlice(arena, sc);
try arr.append(arena, ' ');
single_chunk = null;
}
try arr.appendSlice(arena, content);
try arr.append(arena, ' ');
}
}
}
}
}
if (single_chunk) |sc| return sc;
if (arr.items.len == 0) return null;
// strip out trailing space
return arr.items[0 .. arr.items.len - 1];
}
fn getInputType(el: *Element) ?[]const u8 {
if (el.is(Element.Html.Input)) |input| {
return input._input_type.toString();
}
return null;
}
fn getInputValue(el: *Element) ?[]const u8 {
if (el.is(Element.Html.Input)) |input| {
return input.getValue();
}
return null;
}
/// Get all event listener types registered on this target.
fn getListenerTypes(target: *EventTarget, listener_targets: ListenerTargetMap) []const []const u8 {
if (listener_targets.get(@intFromPtr(target))) |types| return types.items;
return &.{};
}
const testing = @import("../testing.zig");
fn testInteractive(html: []const u8) ![]InteractiveElement {
const page = try testing.test_session.createPage();
defer testing.test_session.removePage();
const doc = page.window._document;
const div = try doc.createElement("div", null, page);
try page.parseHtmlAsChildren(div.asNode(), html);
return collectInteractiveElements(div.asNode(), page.call_arena, page);
}
test "browser.interactive: button" {
const elements = try testInteractive("<button>Click me</button>");
try testing.expectEqual(1, elements.len);
try testing.expectEqual("button", elements[0].tag_name);
try testing.expectEqual("button", elements[0].role.?);
try testing.expectEqual("Click me", elements[0].name.?);
try testing.expectEqual(InteractivityType.native, elements[0].interactivity_type);
}
test "browser.interactive: anchor with href" {
const elements = try testInteractive("<a href=\"/page\">Link</a>");
try testing.expectEqual(1, elements.len);
try testing.expectEqual("a", elements[0].tag_name);
try testing.expectEqual("link", elements[0].role.?);
try testing.expectEqual("Link", elements[0].name.?);
}
test "browser.interactive: anchor without href" {
const elements = try testInteractive("<a>Not a link</a>");
try testing.expectEqual(0, elements.len);
}
test "browser.interactive: input types" {
const elements = try testInteractive(
\\<input type="text" placeholder="Search">
\\<input type="hidden" name="csrf">
);
try testing.expectEqual(1, elements.len);
try testing.expectEqual("input", elements[0].tag_name);
try testing.expectEqual("text", elements[0].input_type.?);
try testing.expectEqual("Search", elements[0].placeholder.?);
}
test "browser.interactive: select and textarea" {
const elements = try testInteractive(
\\<select name="color"><option>Red</option></select>
\\<textarea name="msg"></textarea>
);
try testing.expectEqual(2, elements.len);
try testing.expectEqual("select", elements[0].tag_name);
try testing.expectEqual("textarea", elements[1].tag_name);
}
test "browser.interactive: aria role" {
const elements = try testInteractive("<div role=\"button\">Custom</div>");
try testing.expectEqual(1, elements.len);
try testing.expectEqual("div", elements[0].tag_name);
try testing.expectEqual("button", elements[0].role.?);
try testing.expectEqual(InteractivityType.aria, elements[0].interactivity_type);
}
test "browser.interactive: contenteditable" {
const elements = try testInteractive("<div contenteditable=\"true\">Edit me</div>");
try testing.expectEqual(1, elements.len);
try testing.expectEqual(InteractivityType.contenteditable, elements[0].interactivity_type);
}
test "browser.interactive: tabindex" {
const elements = try testInteractive("<div tabindex=\"0\">Focusable</div>");
try testing.expectEqual(1, elements.len);
try testing.expectEqual(InteractivityType.focusable, elements[0].interactivity_type);
try testing.expectEqual(@as(i32, 0), elements[0].tab_index);
}
test "browser.interactive: disabled" {
const elements = try testInteractive("<button disabled>Off</button>");
try testing.expectEqual(1, elements.len);
try testing.expect(elements[0].disabled);
}
test "browser.interactive: disabled by fieldset" {
const elements = try testInteractive(
\\<fieldset disabled>
\\ <button>Disabled</button>
\\ <legend><button>In legend</button></legend>
\\</fieldset>
);
try testing.expectEqual(2, elements.len);
// Button outside legend is disabled by fieldset
try testing.expect(elements[0].disabled);
// Button inside first legend is NOT disabled
try testing.expect(!elements[1].disabled);
}
test "browser.interactive: pointer-events none" {
const elements = try testInteractive("<button style=\"pointer-events: none;\">Click me</button>");
try testing.expectEqual(0, elements.len);
}
test "browser.interactive: non-interactive div" {
const elements = try testInteractive("<div>Just text</div>");
try testing.expectEqual(0, elements.len);
}
test "browser.interactive: details and summary" {
const elements = try testInteractive("<details><summary>More</summary><p>Content</p></details>");
try testing.expectEqual(2, elements.len);
try testing.expectEqual("details", elements[0].tag_name);
try testing.expectEqual("summary", elements[1].tag_name);
}
test "browser.interactive: mixed elements" {
const elements = try testInteractive(
\\<div>
\\ <a href="/home">Home</a>
\\ <p>Some text</p>
\\ <button id="btn1">Submit</button>
\\ <input type="email" placeholder="Email">
\\ <div>Not interactive</div>
\\ <div role="tab">Tab</div>
\\</div>
);
try testing.expectEqual(4, elements.len);
}

View File

@@ -40,8 +40,8 @@ prev_context: *Context,
// Takes the raw v8 isolate and extracts the context from it.
pub fn init(self: *Caller, v8_isolate: *v8.Isolate) void {
const v8_context = v8.v8__Isolate__GetCurrentContext(v8_isolate).?;
initWithContext(self, Context.fromC(v8_context), v8_context);
const ctx, const v8_context = Context.fromIsolate(.{ .handle = v8_isolate });
initWithContext(self, ctx, v8_context);
}
fn initWithContext(self: *Caller, ctx: *Context, v8_context: *const v8.Context) void {
@@ -128,7 +128,7 @@ fn _constructor(self: *Caller, func: anytype, info: FunctionCallbackInfo) !void
const new_this_handle = info.getThis();
var this = js.Object{ .local = local, .handle = new_this_handle };
if (@typeInfo(ReturnType) == .error_union) {
const non_error_res = res catch |err| return err;
const non_error_res = try res;
this = try local.mapZigInstanceToJs(new_this_handle, non_error_res);
} else {
this = try local.mapZigInstanceToJs(new_this_handle, res);
@@ -505,6 +505,7 @@ pub const Function = struct {
pub const Opts = struct {
noop: bool = false,
static: bool = false,
deletable: bool = true,
dom_exception: bool = false,
as_typed_array: bool = false,
null_as_undefined: bool = false,
@@ -537,9 +538,7 @@ pub const Function = struct {
pub fn call(comptime T: type, info_handle: *const v8.FunctionCallbackInfo, func: anytype, comptime opts: Opts) void {
const v8_isolate = v8.v8__FunctionCallbackInfo__GetIsolate(info_handle).?;
const v8_context = v8.v8__Isolate__GetCurrentContext(v8_isolate).?;
const ctx = Context.fromC(v8_context);
const ctx, const v8_context = Context.fromIsolate(.{ .handle = v8_isolate });
const info = FunctionCallbackInfo{ .handle = info_handle };
var hs: js.HandleScope = undefined;
@@ -734,7 +733,7 @@ fn getArgs(comptime F: type, comptime offset: usize, local: *const Local, info:
if (last_parameter_type_info == .pointer and last_parameter_type_info.pointer.size == .slice) {
const slice_type = last_parameter_type_info.pointer.child;
const corresponding_js_value = info.getArg(@intCast(last_js_parameter), local);
if (corresponding_js_value.isArray() == false and corresponding_js_value.isTypedArray() == false and slice_type != u8) {
if (slice_type == js.Value or (corresponding_js_value.isArray() == false and corresponding_js_value.isTypedArray() == false and slice_type != u8)) {
is_variadic = true;
if (js_parameter_count == 0) {
@field(args, tupleFieldName(params_to_map.len + offset - 1)) = &.{};

View File

@@ -21,11 +21,13 @@ const lp = @import("lightpanda");
const log = @import("../../log.zig");
const js = @import("js.zig");
const Env = @import("Env.zig");
const bridge = @import("bridge.zig");
const Env = @import("Env.zig");
const Origin = @import("Origin.zig");
const Scheduler = @import("Scheduler.zig");
const Page = @import("../Page.zig");
const Session = @import("../Session.zig");
const ScriptManager = @import("../ScriptManager.zig");
const v8 = js.v8;
@@ -41,6 +43,7 @@ const Context = @This();
id: usize,
env: *Env,
page: *Page,
session: *Session,
isolate: js.Isolate,
// Per-context microtask queue for isolation between contexts
@@ -60,7 +63,9 @@ templates: []*const v8.FunctionTemplate,
// Arena for the lifetime of the context
arena: Allocator,
// The page.call_arena
// The call_arena for this context. For main world contexts this is
// page.call_arena. For isolated world contexts this is a separate arena
// owned by the IsolatedWorld.
call_arena: Allocator,
// Because calls can be nested (i.e.a function calling a callback),
@@ -74,39 +79,21 @@ call_depth: usize = 0,
// context.localScope
local: ?*const js.Local = null,
// Serves two purposes. Like `global_objects`, this is used to free
// every Global(Object) we've created during the lifetime of the context.
// More importantly, it serves as an identity map - for a given Zig
// instance, we map it to the same Global(Object).
// The key is the @intFromPtr of the Zig value
identity_map: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
origin: *Origin,
// Any type that is stored in the identity_map which has a finalizer declared
// will have its finalizer stored here. This is only used when shutting down
// if v8 hasn't called the finalizer directly itself.
finalizer_callbacks: std.AutoHashMapUnmanaged(usize, *FinalizerCallback) = .empty,
finalizer_callback_pool: std.heap.MemoryPool(FinalizerCallback),
// Identity tracking for this context. For main world contexts, this points to
// Session's Identity. For isolated world contexts (CDP inspector), this points
// to IsolatedWorld's Identity. This ensures same-origin frames share object
// identity while isolated worlds have separate identity tracking.
identity: *js.Identity,
// Some web APIs have to manage opaque values. Ideally, they use an
// js.Object, but the js.Object has no lifetime guarantee beyond the
// current call. They can call .persist() on their js.Object to get
// a `Global(Object)`. We need to track these to free them.
// This used to be a map and acted like identity_map; the key was
// the @intFromPtr(js_obj.handle). But v8 can re-use address. Without
// a reliable way to know if an object has already been persisted,
// we now simply persist every time persist() is called.
global_values: std.ArrayList(v8.Global) = .empty,
global_objects: std.ArrayList(v8.Global) = .empty,
// Allocator to use for identity map operations. For main world contexts this is
// session.page_arena, for isolated worlds it's the isolated world's arena.
identity_arena: Allocator,
// Unlike other v8 types, like functions or objects, modules are not shared
// across origins.
global_modules: std.ArrayList(v8.Global) = .empty,
global_promises: std.ArrayList(v8.Global) = .empty,
global_functions: std.ArrayList(v8.Global) = .empty,
global_promise_resolvers: std.ArrayList(v8.Global) = .empty,
// Temp variants stored in HashMaps for O(1) early cleanup.
// Key is global.data_ptr.
global_values_temp: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
global_promises_temp: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
global_functions_temp: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
// Our module cache: normalized module specifier => module.
module_cache: std.StringHashMapUnmanaged(ModuleEntry) = .empty,
@@ -144,16 +131,26 @@ const ModuleEntry = struct {
resolver_promise: ?js.Promise.Global = null,
};
pub fn fromC(c_context: *const v8.Context) *Context {
pub fn fromC(c_context: *const v8.Context) ?*Context {
return @ptrCast(@alignCast(v8.v8__Context__GetAlignedPointerFromEmbedderData(c_context, 1)));
}
pub fn fromIsolate(isolate: js.Isolate) *Context {
return fromC(v8.v8__Isolate__GetCurrentContext(isolate.handle).?);
/// Returns the Context and v8::Context for the given isolate.
/// If the current context is from a destroyed Context (e.g., navigated-away iframe),
/// falls back to the incumbent context (the calling context).
pub fn fromIsolate(isolate: js.Isolate) struct { *Context, *const v8.Context } {
const v8_context = v8.v8__Isolate__GetCurrentContext(isolate.handle).?;
if (fromC(v8_context)) |ctx| {
return .{ ctx, v8_context };
}
// The current context's Context struct has been freed (e.g., iframe navigated away).
// Fall back to the incumbent context (the calling context).
const v8_incumbent = v8.v8__Isolate__GetIncumbentContext(isolate.handle).?;
return .{ fromC(v8_incumbent).?, v8_incumbent };
}
pub fn deinit(self: *Context) void {
if (comptime IS_DEBUG) {
if (comptime IS_DEBUG and @import("builtin").is_test == false) {
var it = self.unknown_properties.iterator();
while (it.next()) |kv| {
log.debug(.unknown_prop, "unknown property", .{
@@ -174,64 +171,16 @@ pub fn deinit(self: *Context) void {
// this can release objects
self.scheduler.deinit();
{
var it = self.identity_map.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
}
{
var it = self.finalizer_callbacks.valueIterator();
while (it.next()) |finalizer| {
finalizer.*.deinit();
}
self.finalizer_callback_pool.deinit();
}
for (self.global_values.items) |*global| {
v8.v8__Global__Reset(global);
}
for (self.global_objects.items) |*global| {
v8.v8__Global__Reset(global);
}
for (self.global_modules.items) |*global| {
v8.v8__Global__Reset(global);
}
for (self.global_functions.items) |*global| {
v8.v8__Global__Reset(global);
}
self.session.releaseOrigin(self.origin);
for (self.global_promises.items) |*global| {
v8.v8__Global__Reset(global);
}
for (self.global_promise_resolvers.items) |*global| {
v8.v8__Global__Reset(global);
}
{
var it = self.global_values_temp.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
}
{
var it = self.global_promises_temp.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
}
{
var it = self.global_functions_temp.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
}
// Clear the embedder data so that if V8 keeps this context alive
// (because objects created in it are still referenced), we don't
// have a dangling pointer to our freed Context struct.
v8.v8__Context__SetAlignedPointerInEmbedderData(entered.handle, 1, null);
v8.v8__Global__Reset(&self.handle);
env.isolate.notifyContextDisposed();
@@ -241,77 +190,48 @@ pub fn deinit(self: *Context) void {
v8.v8__MicrotaskQueue__DELETE(self.microtask_queue);
}
pub fn weakRef(self: *Context, obj: anytype) void {
const fc = self.finalizer_callbacks.get(@intFromPtr(obj)) orelse {
if (comptime IS_DEBUG) {
// should not be possible
std.debug.assert(false);
pub fn setOrigin(self: *Context, key: ?[]const u8) !void {
const env = self.env;
const isolate = env.isolate;
lp.assert(self.origin.rc == 1, "Ref opaque origin", .{ .rc = self.origin.rc });
const origin = try self.session.getOrCreateOrigin(key);
self.session.releaseOrigin(self.origin);
self.origin = origin;
{
var ls: js.Local.Scope = undefined;
self.localScope(&ls);
defer ls.deinit();
// Set the V8::Context SecurityToken, which is a big part of what allows
// one context to access another.
const token_local = v8.v8__Global__Get(&origin.security_token, isolate.handle);
v8.v8__Context__SetSecurityToken(ls.local.handle, token_local);
}
return;
};
v8.v8__Global__SetWeakFinalizer(&fc.global, fc, bridge.Struct(@TypeOf(obj)).JsApi.Meta.finalizer.from_v8, v8.kParameter);
}
pub fn safeWeakRef(self: *Context, obj: anytype) void {
const fc = self.finalizer_callbacks.get(@intFromPtr(obj)) orelse {
if (comptime IS_DEBUG) {
// should not be possible
std.debug.assert(false);
}
return;
};
v8.v8__Global__ClearWeak(&fc.global);
v8.v8__Global__SetWeakFinalizer(&fc.global, fc, bridge.Struct(@TypeOf(obj)).JsApi.Meta.finalizer.from_v8, v8.kParameter);
pub fn trackGlobal(self: *Context, global: v8.Global) !void {
return self.session.globals.append(self.session.page_arena, global);
}
pub fn strongRef(self: *Context, obj: anytype) void {
const fc = self.finalizer_callbacks.get(@intFromPtr(obj)) orelse {
if (comptime IS_DEBUG) {
// should not be possible
std.debug.assert(false);
}
return;
};
v8.v8__Global__ClearWeak(&fc.global);
pub fn trackTemp(self: *Context, global: v8.Global) !void {
return self.session.temps.put(self.session.page_arena, global.data_ptr, global);
}
pub fn release(self: *Context, item: anytype) void {
if (@TypeOf(item) == *anyopaque) {
// Existing *anyopaque path for identity_map. Called internally from
// finalizers
var global = self.identity_map.fetchRemove(@intFromPtr(item)) orelse {
if (comptime IS_DEBUG) {
// should not be possible
std.debug.assert(false);
}
return;
};
v8.v8__Global__Reset(&global.value);
// The item has been fianalized, remove it for the finalizer callback so that
// we don't try to call it again on shutdown.
const fc = self.finalizer_callbacks.fetchRemove(@intFromPtr(item)) orelse {
if (comptime IS_DEBUG) {
// should not be possible
std.debug.assert(false);
}
return;
};
self.finalizer_callback_pool.destroy(fc.value);
return;
}
var map = switch (@TypeOf(item)) {
js.Value.Temp => &self.global_values_temp,
js.Promise.Temp => &self.global_promises_temp,
js.Function.Temp => &self.global_functions_temp,
else => |T| @compileError("Context.release cannot be called with a " ++ @typeName(T)),
pub const IdentityResult = struct {
value_ptr: *v8.Global,
found_existing: bool,
};
if (map.fetchRemove(item.handle.data_ptr)) |kv| {
var global = kv.value;
v8.v8__Global__Reset(&global);
}
pub fn addIdentity(self: *Context, ptr: usize) !IdentityResult {
const gop = try self.identity.identity_map.getOrPut(self.identity_arena, ptr);
return .{
.value_ptr = gop.value_ptr,
.found_existing = gop.found_existing,
};
}
// Any operation on the context have to be made from a local.
@@ -336,28 +256,22 @@ pub fn toLocal(self: *Context, global: anytype) js.Local.ToLocalReturnType(@Type
return l.toLocal(global);
}
// This isn't expected to be called often. It's for converting attributes into
// function calls, e.g. <body onload="doSomething"> will turn that "doSomething"
// string into a js.Function which looks like: function(e) { doSomething(e) }
// There might be more efficient ways to do this, but doing it this way means
// our code only has to worry about js.Funtion, not some union of a js.Function
// or a string.
pub fn stringToPersistedFunction(self: *Context, str: []const u8) !js.Function.Global {
pub fn getIncumbent(self: *Context) *Page {
return fromC(v8.v8__Isolate__GetIncumbentContext(self.env.isolate.handle).?).?.page;
}
pub fn stringToPersistedFunction(
self: *Context,
function_body: []const u8,
comptime parameter_names: []const []const u8,
extensions: []const v8.Object,
) !js.Function.Global {
var ls: js.Local.Scope = undefined;
self.localScope(&ls);
defer ls.deinit();
var extra: []const u8 = "";
const normalized = std.mem.trim(u8, str, &std.ascii.whitespace);
if (normalized.len > 0 and normalized[normalized.len - 1] != ')') {
extra = "(e)";
}
const full = try std.fmt.allocPrintSentinel(self.call_arena, "(function(e) {{ {s}{s} }})", .{ normalized, extra }, 0);
const js_val = try ls.local.compileAndRun(full, null);
if (!js_val.isFunction()) {
return error.StringFunctionError;
}
return try (js.Function{ .local = &ls.local, .handle = @ptrCast(js_val.handle) }).persist();
const js_function = try ls.local.compileFunction(function_body, parameter_names, extensions);
return js_function.persist();
}
pub fn module(self: *Context, comptime want_result: bool, local: *const js.Local, src: []const u8, url: []const u8, cacheable: bool) !(if (want_result) ModuleEntry else void) {
@@ -397,15 +311,15 @@ pub fn module(self: *Context, comptime want_result: bool, local: *const js.Local
}
const owned_url = try arena.dupeZ(u8, url);
if (cacheable and !gop.found_existing) {
gop.key_ptr.* = owned_url;
}
const m = try compileModule(local, src, owned_url);
if (cacheable) {
// compileModule is synchronous - nothing can modify the cache during compilation
lp.assert(gop.value_ptr.module == null, "Context.module has module", .{});
gop.value_ptr.module = try m.persist();
if (!gop.found_existing) {
gop.key_ptr.* = owned_url;
}
}
break :blk .{ m, owned_url };
@@ -535,6 +449,14 @@ fn postCompileModule(self: *Context, mod: js.Module, url: [:0]const u8, local: *
nested_gop.key_ptr.* = owned_specifier;
nested_gop.value_ptr.* = .{};
try script_manager.preloadImport(owned_specifier, url);
} else if (nested_gop.value_ptr.module == null) {
// Entry exists but module failed to compile previously.
// The imported_modules entry may have been consumed, so
// re-preload to ensure waitForImport can find it.
// Key was stored via dupeZ so it has a sentinel in memory.
const key = nested_gop.key_ptr.*;
const key_z: [:0]const u8 = key.ptr[0..key.len :0];
try script_manager.preloadImport(key_z, url);
}
}
}
@@ -559,7 +481,7 @@ fn resolveModuleCallback(
) callconv(.c) ?*const v8.Module {
_ = import_attributes;
const self = fromC(c_context.?);
const self = fromC(c_context.?).?;
const local = js.Local{
.ctx = self,
.handle = c_context.?,
@@ -592,7 +514,7 @@ pub fn dynamicModuleCallback(
_ = host_defined_options;
_ = import_attrs;
const self = fromC(c_context.?);
const self = fromC(c_context.?).?;
const local = js.Local{
.ctx = self,
.handle = c_context.?,
@@ -610,13 +532,13 @@ pub fn dynamicModuleCallback(
break :blk js.String.toSliceZ(.{ .local = &local, .handle = resource_name.? }) catch |err| {
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback1" });
return @constCast((local.rejectPromise("Out of memory") catch return null).handle);
return @constCast(local.rejectPromise(.{ .generic_error = "Out of memory" }).handle);
};
};
const specifier = js.String.toSliceZ(.{ .local = &local, .handle = v8_specifier.? }) catch |err| {
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback2" });
return @constCast((local.rejectPromise("Out of memory") catch return null).handle);
return @constCast(local.rejectPromise(.{ .generic_error = "Out of memory" }).handle);
};
const normalized_specifier = self.script_manager.?.resolveSpecifier(
@@ -625,21 +547,21 @@ pub fn dynamicModuleCallback(
specifier,
) catch |err| {
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback3" });
return @constCast((local.rejectPromise("Out of memory") catch return null).handle);
return @constCast(local.rejectPromise(.{ .generic_error = "Out of memory" }).handle);
};
const promise = self._dynamicModuleCallback(normalized_specifier, resource, &local) catch |err| blk: {
log.err(.js, "dynamic module callback", .{
.err = err,
});
break :blk local.rejectPromise("Failed to load module") catch return null;
break :blk local.rejectPromise(.{ .generic_error = "Out of memory" });
};
return @constCast(promise.handle);
}
pub fn metaObjectCallback(c_context: ?*v8.Context, c_module: ?*v8.Module, c_meta: ?*v8.Value) callconv(.c) void {
// @HandleScope implement this without a fat context/local..
const self = fromC(c_context.?);
const self = fromC(c_context.?).?;
var local = js.Local{
.ctx = self,
.handle = c_context.?,
@@ -683,7 +605,15 @@ fn _resolveModuleCallback(self: *Context, referrer: js.Module, specifier: [:0]co
return local.toLocal(m).handle;
}
var source = try self.script_manager.?.waitForImport(normalized_specifier);
var source = self.script_manager.?.waitForImport(normalized_specifier) catch |err| switch (err) {
error.UnknownModule => blk: {
// Module is in cache but was consumed from imported_modules
// (e.g., by a previous failed resolution). Re-preload and retry.
try self.script_manager.?.preloadImport(normalized_specifier, referrer_path);
break :blk try self.script_manager.?.waitForImport(normalized_specifier);
},
else => return err,
};
defer source.deinit();
var try_catch: js.TryCatch = undefined;
@@ -1023,34 +953,6 @@ pub fn queueMicrotaskFunc(self: *Context, cb: js.Function) void {
v8.v8__MicrotaskQueue__EnqueueMicrotaskFunc(self.microtask_queue, self.isolate.handle, cb.handle);
}
pub fn createFinalizerCallback(self: *Context, global: v8.Global, ptr: *anyopaque, finalizerFn: *const fn (ptr: *anyopaque, page: *Page) void) !*FinalizerCallback {
const fc = try self.finalizer_callback_pool.create();
fc.* = .{
.ctx = self,
.ptr = ptr,
.global = global,
.finalizerFn = finalizerFn,
};
return fc;
}
// == Misc ==
// A type that has a finalizer can have its finalizer called one of two ways.
// The first is from V8 via the WeakCallback we give to weakRef. But that isn't
// guaranteed to fire, so we track this in ctx._finalizers and call them on
// context shutdown.
pub const FinalizerCallback = struct {
ctx: *Context,
ptr: *anyopaque,
global: v8.Global,
finalizerFn: *const fn (ptr: *anyopaque, page: *Page) void,
pub fn deinit(self: *FinalizerCallback) void {
self.finalizerFn(self.ptr, self.ctx.page);
self.ctx.finalizer_callback_pool.destroy(self);
}
};
// == Profiler ==
pub fn startCpuProfiler(self: *Context) void {
if (comptime !IS_DEBUG) {

View File

@@ -57,6 +57,8 @@ const Env = @This();
app: *App,
allocator: Allocator,
platform: *const Platform,
// the global isolate
@@ -70,6 +72,11 @@ isolate_params: *v8.CreateParams,
context_id: usize,
// Maps origin -> shared Origin contains, for v8 values shared across
// same-origin Contexts. There's a mismatch here between our JS model and our
// Browser model. Origins only live as long as the root page of a session exists.
// It would be wrong/dangerous to re-use an Origin across root page navigations.
// Global handles that need to be freed on deinit
eternal_function_templates: []v8.Eternal,
@@ -206,6 +213,7 @@ pub fn init(app: *App, opts: InitOpts) !Env {
return .{
.app = app,
.context_id = 0,
.allocator = allocator,
.contexts = undefined,
.context_count = 0,
.isolate = isolate,
@@ -228,7 +236,9 @@ pub fn deinit(self: *Env) void {
ctx.deinit();
}
const allocator = self.app.allocator;
const app = self.app;
const allocator = app.allocator;
if (self.inspector) |i| {
i.deinit(allocator);
}
@@ -243,8 +253,15 @@ pub fn deinit(self: *Env) void {
allocator.destroy(self.isolate_params);
}
pub fn createContext(self: *Env, page: *Page) !*Context {
const context_arena = try self.app.arena_pool.acquire();
pub const ContextParams = struct {
identity: *js.Identity,
identity_arena: Allocator,
call_arena: Allocator,
debug_name: []const u8 = "Context",
};
pub fn createContext(self: *Env, page: *Page, params: ContextParams) !*Context {
const context_arena = try self.app.arena_pool.acquire(.{ .debug = params.debug_name });
errdefer self.app.arena_pool.release(context_arena);
const isolate = self.isolate;
@@ -272,6 +289,7 @@ pub fn createContext(self: *Env, page: *Page) !*Context {
// get the global object for the context, this maps to our Window
const global_obj = v8.v8__Context__Global(v8_context).?;
{
// Store our TAO inside the internal field of the global object. This
// maps the v8::Object -> Zig instance. Almost all objects have this, and
@@ -287,29 +305,44 @@ pub fn createContext(self: *Env, page: *Page) !*Context {
};
v8.v8__Object__SetAlignedPointerInInternalField(global_obj, 0, tao);
}
// our window wrapped in a v8::Global
var global_global: v8.Global = undefined;
v8.v8__Global__New(isolate.handle, global_obj, &global_global);
const context_id = self.context_id;
self.context_id = context_id + 1;
const session = page._session;
const origin = try session.getOrCreateOrigin(null);
errdefer session.releaseOrigin(origin);
const context = try context_arena.create(Context);
context.* = .{
.env = self,
.page = page,
.origin = origin,
.id = context_id,
.session = session,
.isolate = isolate,
.arena = context_arena,
.handle = context_global,
.templates = self.templates,
.call_arena = page.call_arena,
.call_arena = params.call_arena,
.microtask_queue = microtask_queue,
.script_manager = &page._script_manager,
.scheduler = .init(context_arena),
.finalizer_callback_pool = std.heap.MemoryPool(Context.FinalizerCallback).init(self.app.allocator),
.identity = params.identity,
.identity_arena = params.identity_arena,
};
try context.identity_map.putNoClobber(context_arena, @intFromPtr(page.window), global_global);
{
// Multiple contexts can be created for the same Window (via CDP). We only
// need to register the first one.
const gop = try params.identity.identity_map.getOrPut(params.identity_arena, @intFromPtr(page.window));
if (gop.found_existing == false) {
// our window wrapped in a v8::Global
var global_global: v8.Global = undefined;
v8.v8__Global__New(isolate.handle, global_obj, &global_global);
gop.value_ptr.* = global_global;
}
}
// Store a pointer to our context inside the v8 context so that, given
// a v8 context, we can get our context out
@@ -365,8 +398,7 @@ pub fn runMicrotasks(self: *Env) void {
}
}
pub fn runMacrotasks(self: *Env) !?u64 {
var ms_to_next_task: ?u64 = null;
pub fn runMacrotasks(self: *Env) !void {
for (self.contexts[0..self.context_count]) |ctx| {
if (comptime builtin.is_test == false) {
// I hate this comptime check as much as you do. But we have tests
@@ -381,13 +413,17 @@ pub fn runMacrotasks(self: *Env) !?u64 {
var hs: js.HandleScope = undefined;
const entered = ctx.enter(&hs);
defer entered.exit();
try ctx.scheduler.run();
}
}
const ms = (try ctx.scheduler.run()) orelse continue;
if (ms_to_next_task == null or ms < ms_to_next_task.?) {
ms_to_next_task = ms;
pub fn msToNextMacrotask(self: *Env) ?u64 {
var next_task: u64 = std.math.maxInt(u64);
for (self.contexts[0..self.context_count]) |ctx| {
const candidate = ctx.scheduler.msToNextHigh() orelse continue;
next_task = @min(candidate, next_task);
}
}
return ms_to_next_task;
return if (next_task == std.math.maxInt(u64)) null else next_task;
}
pub fn pumpMessageLoop(self: *const Env) void {
@@ -475,20 +511,25 @@ pub fn terminate(self: *const Env) void {
}
fn promiseRejectCallback(message_handle: v8.PromiseRejectMessage) callconv(.c) void {
const promise_event = v8.v8__PromiseRejectMessage__GetEvent(&message_handle);
if (promise_event != v8.kPromiseRejectWithNoHandler and promise_event != v8.kPromiseHandlerAddedAfterReject) {
return;
}
const promise_handle = v8.v8__PromiseRejectMessage__GetPromise(&message_handle).?;
const v8_isolate = v8.v8__Object__GetIsolate(@ptrCast(promise_handle)).?;
const js_isolate = js.Isolate{ .handle = v8_isolate };
const ctx = Context.fromIsolate(js_isolate);
const isolate = js.Isolate{ .handle = v8_isolate };
const ctx, const v8_context = Context.fromIsolate(isolate);
const local = js.Local{
.ctx = ctx,
.isolate = js_isolate,
.handle = v8.v8__Isolate__GetCurrentContext(v8_isolate).?,
.isolate = isolate,
.handle = v8_context,
.call_arena = ctx.call_arena,
};
const page = ctx.page;
page.window.unhandledPromiseRejection(.{
page.window.unhandledPromiseRejection(promise_event == v8.kPromiseRejectWithNoHandler, .{
.local = &local,
.handle = &message_handle,
}, page) catch |err| {

View File

@@ -160,8 +160,8 @@ fn _tryCallWithThis(self: *const Function, comptime T: type, this: anytype, args
try_catch.rethrow();
return error.TryCatchRethrow;
}
caught.* = try_catch.caughtOrError(local.call_arena, error.JSExecCallback);
return error.JSExecCallback;
caught.* = try_catch.caughtOrError(local.call_arena, error.JsException);
return error.JsException;
};
if (@typeInfo(T) == .void) {
@@ -209,11 +209,11 @@ fn _persist(self: *const Function, comptime is_global: bool) !(if (is_global) Gl
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
if (comptime is_global) {
try ctx.global_functions.append(ctx.arena, global);
} else {
try ctx.global_functions_temp.put(ctx.arena, global.data_ptr, global);
try ctx.trackGlobal(global);
return .{ .handle = global, .temps = {} };
}
return .{ .handle = global };
try ctx.trackTemp(global);
return .{ .handle = global, .temps = &ctx.session.temps };
}
pub fn tempWithThis(self: *const Function, value: anytype) !Temp {
@@ -226,15 +226,18 @@ pub fn persistWithThis(self: *const Function, value: anytype) !Global {
return with_this.persist();
}
pub const Temp = G(0);
pub const Global = G(1);
pub const Temp = G(.temp);
pub const Global = G(.global);
fn G(comptime discriminator: u8) type {
const GlobalType = enum(u8) {
temp,
global,
};
fn G(comptime global_type: GlobalType) type {
return struct {
handle: v8.Global,
// makes the types different (G(0) != G(1)), without taking up space
comptime _: u8 = discriminator,
temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,
const Self = @This();
@@ -252,5 +255,12 @@ fn G(comptime discriminator: u8) type {
pub fn isEqual(self: *const Self, other: Function) bool {
return v8.v8__Global__IsEqual(&self.handle, other.handle);
}
pub fn release(self: *const Self) void {
if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {
var g = kv.value;
v8.v8__Global__Reset(&g);
}
}
};
}

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Identity manages the mapping between Zig instances and their v8::Object wrappers.
// This provides object identity semantics - the same Zig instance always maps to
// the same JS object within a given Identity scope.
//
// Main world contexts share a single Identity (on Session), ensuring that
// `window.top.document === top's document` works across same-origin frames.
//
// Isolated worlds (CDP inspector) have their own Identity, ensuring their
// v8::Global wrappers don't leak into the main world.
const std = @import("std");
const js = @import("js.zig");
const Session = @import("../Session.zig");
const v8 = js.v8;
const Identity = @This();
// Maps Zig instance pointers to their v8::Global(Object) wrappers.
identity_map: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,
pub fn deinit(self: *Identity) void {
var it = self.identity_map.valueIterator();
while (it.next()) |global| {
v8.v8__Global__Reset(global);
}
}

View File

@@ -130,6 +130,12 @@ pub fn contextCreated(
pub fn contextDestroyed(self: *Inspector, context: *const v8.Context) void {
v8.v8_inspector__Inspector__ContextDestroyed(self.handle, context);
if (self.default_context) |*dc| {
if (v8.v8__Global__IsEqual(dc, context)) {
self.default_context = null;
}
}
}
pub fn resetContextGroup(self: *const Inspector) void {

View File

@@ -78,6 +78,21 @@ pub fn createError(self: Isolate, msg: []const u8) *const v8.Value {
return v8.v8__Exception__Error(message).?;
}
pub fn createRangeError(self: Isolate, msg: []const u8) *const v8.Value {
const message = self.initStringHandle(msg);
return v8.v8__Exception__RangeError(message).?;
}
pub fn createReferenceError(self: Isolate, msg: []const u8) *const v8.Value {
const message = self.initStringHandle(msg);
return v8.v8__Exception__ReferenceError(message).?;
}
pub fn createSyntaxError(self: Isolate, msg: []const u8) *const v8.Value {
const message = self.initStringHandle(msg);
return v8.v8__Exception__SyntaxError(message).?;
}
pub fn createTypeError(self: Isolate, msg: []const u8) *const v8.Value {
const message = self.initStringHandle(msg);
return v8.v8__Exception__TypeError(message).?;

View File

@@ -17,10 +17,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("../Page.zig");
const log = @import("../../log.zig");
const string = @import("../../string.zig");
const Session = @import("../Session.zig");
const js = @import("js.zig");
const bridge = @import("bridge.zig");
const Caller = @import("Caller.zig");
@@ -32,7 +33,6 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
const v8 = js.v8;
const CallOpts = Caller.CallOpts;
const Allocator = std.mem.Allocator;
// Where js.Context has a lifetime tied to the page, and holds the
// v8::Global<v8::Context>, this has a much shorter lifetime and holds a
@@ -115,6 +115,49 @@ pub fn exec(self: *const Local, src: []const u8, name: ?[]const u8) !js.Value {
return self.compileAndRun(src, name);
}
/// Compiles a function body as function.
///
/// https://v8.github.io/api/head/classv8_1_1ScriptCompiler.html#a3a15bb5a7dfc3f998e6ac789e6b4646a
pub fn compileFunction(
self: *const Local,
function_body: []const u8,
/// We tend to know how many params we'll pass; can remove the comptime if necessary.
comptime parameter_names: []const []const u8,
extensions: []const v8.Object,
) !js.Function {
// TODO: Make configurable.
const script_name = self.isolate.initStringHandle("anonymous");
const script_source = self.isolate.initStringHandle(function_body);
var parameter_list: [parameter_names.len]*const v8.String = undefined;
inline for (0..parameter_names.len) |i| {
parameter_list[i] = self.isolate.initStringHandle(parameter_names[i]);
}
// Create `ScriptOrigin`.
var origin: v8.ScriptOrigin = undefined;
v8.v8__ScriptOrigin__CONSTRUCT(&origin, script_name);
// Create `ScriptCompilerSource`.
var script_compiler_source: v8.ScriptCompilerSource = undefined;
v8.v8__ScriptCompiler__Source__CONSTRUCT2(script_source, &origin, null, &script_compiler_source);
defer v8.v8__ScriptCompiler__Source__DESTRUCT(&script_compiler_source);
// Compile the function.
const result = v8.v8__ScriptCompiler__CompileFunction(
self.handle,
&script_compiler_source,
parameter_list.len,
&parameter_list,
extensions.len,
@ptrCast(&extensions),
v8.kNoCompileOptions,
v8.kNoCacheNoReason,
) orelse return error.CompilationError;
return .{ .local = self, .handle = result };
}
pub fn compileAndRun(self: *const Local, src: []const u8, name: ?[]const u8) !js.Value {
const script_name = self.isolate.initStringHandle(name orelse "anonymous");
const script_source = self.isolate.initStringHandle(src);
@@ -137,7 +180,7 @@ pub fn compileAndRun(self: *const Local, src: []const u8, name: ?[]const u8) !js
) orelse return error.CompilationError;
// Run the script
const result = v8.v8__Script__Run(v8_script, self.handle) orelse return error.ExecutionError;
const result = v8.v8__Script__Run(v8_script, self.handle) orelse return error.JsException;
return .{ .local = self, .handle = result };
}
@@ -158,20 +201,21 @@ pub fn compileAndRun(self: *const Local, src: []const u8, name: ?[]const u8) !js
// we can just grab it from the identity_map)
pub fn mapZigInstanceToJs(self: *const Local, js_obj_handle: ?*const v8.Object, value: anytype) !js.Object {
const ctx = self.ctx;
const arena = ctx.arena;
const context_arena = ctx.arena;
const T = @TypeOf(value);
switch (@typeInfo(T)) {
.@"struct" => {
// Struct, has to be placed on the heap
const heap = try arena.create(T);
const heap = try context_arena.create(T);
heap.* = value;
return self.mapZigInstanceToJs(js_obj_handle, heap);
},
.pointer => |ptr| {
const resolved = resolveValue(value);
const gop = try ctx.identity_map.getOrPut(arena, @intFromPtr(resolved.ptr));
const resolved_ptr_id = @intFromPtr(resolved.ptr);
const gop = try ctx.addIdentity(resolved_ptr_id);
if (gop.found_existing) {
// we've seen this instance before, return the same object
return (js.Object.Global{ .handle = gop.value_ptr.* }).local(self);
@@ -200,7 +244,7 @@ pub fn mapZigInstanceToJs(self: *const Local, js_obj_handle: ?*const v8.Object,
// The TAO contains the pointer to our Zig instance as
// well as any meta data we'll need to use it later.
// See the TaggedOpaque struct for more details.
const tao = try arena.create(TaggedOpaque);
const tao = try context_arena.create(TaggedOpaque);
tao.* = .{
.value = resolved.ptr,
.prototype_chain = resolved.prototype_chain.ptr,
@@ -220,30 +264,27 @@ pub fn mapZigInstanceToJs(self: *const Local, js_obj_handle: ?*const v8.Object,
// dont' use js_obj.persist(), because we don't want to track this in
// context.global_objects, we want to track it in context.identity_map.
v8.v8__Global__New(isolate.handle, js_obj.handle, gop.value_ptr);
if (@hasDecl(JsApi.Meta, "finalizer")) {
// It would be great if resolved knew the resolved type, but I
// can't figure out how to make that work, since it depends on
// the [runtime] `value`.
// We need the resolved finalizer, which we have in resolved.
// The above if statement would be more clear as:
// if (resolved.finalizer_from_v8) |finalizer| {
// But that's a runtime check.
// Instead, we check if the base has finalizer. The assumption
// here is that if a resolve type has a finalizer, then the base
// should have a finalizer too.
const fc = try ctx.createFinalizerCallback(gop.value_ptr.*, resolved.ptr, resolved.finalizer_from_zig.?);
{
errdefer fc.deinit();
try ctx.finalizer_callbacks.put(ctx.arena, @intFromPtr(resolved.ptr), fc);
}
if (resolved.finalizer) |finalizer| {
const finalizer_ptr_id = finalizer.ptr_id;
finalizer.acquireRef(finalizer_ptr_id);
conditionallyReference(value);
if (@hasDecl(JsApi.Meta, "weak")) {
if (comptime IS_DEBUG) {
std.debug.assert(JsApi.Meta.weak == true);
}
v8.v8__Global__SetWeakFinalizer(gop.value_ptr, fc, resolved.finalizer_from_v8, v8.kParameter);
const session = ctx.session;
const finalizer_gop = try session.finalizer_callbacks.getOrPut(session.page_arena, finalizer_ptr_id);
if (finalizer_gop.found_existing == false) {
// This is the first context (and very likely only one) to
// see this Zig instance. We need to create the FinalizerCallback
// so that we can cleanup on page reset if v8 doesn't finalize.
errdefer _ = session.finalizer_callbacks.remove(finalizer_ptr_id);
finalizer_gop.value_ptr.* = try self.createFinalizerCallback(resolved_ptr_id, finalizer_ptr_id, finalizer.deinit);
}
const fc = finalizer_gop.value_ptr.*;
const identity_finalizer = try fc.arena.create(Session.FinalizerCallback.Identity);
identity_finalizer.* = .{
.fc = fc,
.identity = ctx.identity,
};
v8.v8__Global__SetWeakFinalizer(gop.value_ptr, identity_finalizer, finalizer.release, v8.kParameter);
}
return js_obj;
},
@@ -1078,12 +1119,19 @@ fn jsUnsignedIntToZig(comptime T: type, max: comptime_int, maybe: u32) !T {
// This function recursively walks the _type union field (if there is one) to
// get the most specific class_id possible.
const Resolved = struct {
weak: bool,
ptr: *anyopaque,
class_id: u16,
prototype_chain: []const @import("TaggedOpaque.zig").PrototypeChainEntry,
finalizer_from_v8: ?*const fn (handle: ?*const v8.WeakCallbackInfo) callconv(.c) void = null,
finalizer_from_zig: ?*const fn (ptr: *anyopaque, page: *Page) void = null,
finalizer: ?Finalizer,
const Finalizer = struct {
// Resolved.ptr is the most specific value in a chain (e.g. IFrame, not EventTarget, Node, ...)
// Finalizer.ptr_id is the most specific value in a chain that defines an acquireRef
ptr_id: usize,
deinit: *const fn (ptr_id: usize, session: *Session) void,
acquireRef: *const fn (ptr_id: usize) void,
release: *const fn (handle: ?*const v8.WeakCallbackInfo) callconv(.c) void,
};
};
pub fn resolveValue(value: anytype) Resolved {
const T = bridge.Struct(@TypeOf(value));
@@ -1110,27 +1158,85 @@ pub fn resolveValue(value: anytype) Resolved {
unreachable;
}
fn resolveT(comptime T: type, value: *anyopaque) Resolved {
fn resolveT(comptime T: type, value: *T) Resolved {
const Meta = T.JsApi.Meta;
return .{
.ptr = value,
.class_id = Meta.class_id,
.prototype_chain = &Meta.prototype_chain,
.weak = if (@hasDecl(Meta, "weak")) Meta.weak else false,
.finalizer_from_v8 = if (@hasDecl(Meta, "finalizer")) Meta.finalizer.from_v8 else null,
.finalizer_from_zig = if (@hasDecl(Meta, "finalizer")) Meta.finalizer.from_zig else null,
.finalizer = blk: {
const FT = (comptime findFinalizerType(T)) orelse break :blk null;
const getFinalizerPtr = comptime finalizerPtrGetter(T, FT);
const finalizer_ptr = getFinalizerPtr(value);
const Wrap = struct {
fn deinit(ptr_id: usize, session: *Session) void {
FT.deinit(@ptrFromInt(ptr_id), session);
}
fn acquireRef(ptr_id: usize) void {
FT.acquireRef(@ptrFromInt(ptr_id));
}
fn release(handle: ?*const v8.WeakCallbackInfo) callconv(.c) void {
const ptr = v8.v8__WeakCallbackInfo__GetParameter(handle.?).?;
const identity_finalizer: *Session.FinalizerCallback.Identity = @ptrCast(@alignCast(ptr));
const fc = identity_finalizer.fc;
if (identity_finalizer.identity.identity_map.fetchRemove(fc.resolved_ptr_id)) |kv| {
var global = kv.value;
v8.v8__Global__Reset(&global);
}
FT.releaseRef(@ptrFromInt(fc.finalizer_ptr_id), fc.session);
}
};
break :blk .{
.ptr_id = @intFromPtr(finalizer_ptr),
.deinit = Wrap.deinit,
.acquireRef = Wrap.acquireRef,
.release = Wrap.release,
};
},
};
}
fn conditionallyReference(value: anytype) void {
const T = bridge.Struct(@TypeOf(value));
if (@hasDecl(T, "acquireRef")) {
value.acquireRef();
return;
// Start at the "resolved" type (the most specific) and work our way up the
// prototype chain looking for the type that defines acquireRef
fn findFinalizerType(comptime T: type) ?type {
const S = bridge.Struct(T);
if (@hasDecl(S, "acquireRef")) {
return S;
}
if (@hasField(T, "_proto")) {
conditionallyReference(value._proto);
if (@hasField(S, "_proto")) {
const ProtoPtr = std.meta.fieldInfo(S, ._proto).type;
const ProtoChild = @typeInfo(ProtoPtr).pointer.child;
return findFinalizerType(ProtoChild);
}
return null;
}
// Generate a function that follows the _proto pointer chain to get to the finalizer type
fn finalizerPtrGetter(comptime T: type, comptime FT: type) *const fn (*T) *FT {
const S = bridge.Struct(T);
if (S == FT) {
return struct {
fn get(v: *T) *FT {
return v;
}
}.get;
}
if (@hasField(S, "_proto")) {
const ProtoPtr = std.meta.fieldInfo(S, ._proto).type;
const ProtoChild = @typeInfo(ProtoPtr).pointer.child;
const childGetter = comptime finalizerPtrGetter(ProtoChild, FT);
return struct {
fn get(v: *T) *FT {
return childGetter(v._proto);
}
}.get;
}
@compileError("Cannot find path from " ++ @typeName(T) ++ " to " ++ @typeName(FT));
}
pub fn stackTrace(self: *const Local) !?[]const u8 {
@@ -1161,9 +1267,15 @@ pub fn stackTrace(self: *const Local) !?[]const u8 {
}
// == Promise Helpers ==
pub fn rejectPromise(self: *const Local, value: anytype) !js.Promise {
pub fn rejectPromise(self: *const Local, err: js.PromiseResolver.RejectError) js.Promise {
var resolver = js.PromiseResolver.init(self);
resolver.reject("Local.rejectPromise", value);
resolver.rejectError("Local.rejectPromise", err);
return resolver.promise();
}
pub fn rejectErrorPromise(self: *const Local, value: js.PromiseResolver.RejectError) !js.Promise {
var resolver = js.PromiseResolver.init(self);
resolver.rejectError("Local.rejectPromise", value);
return resolver.promise();
}
@@ -1332,6 +1444,34 @@ pub fn debugContextId(self: *const Local) i32 {
return v8.v8__Context__DebugContextId(self.handle);
}
fn createFinalizerCallback(
self: *const Local,
// Key in identity map
// The most specific value (KeyboardEvent, not Event)
resolved_ptr_id: usize,
// The most specific value where finalizers are defined
// What actually gets acquired / released / deinit
finalizer_ptr_id: usize,
deinit: *const fn (ptr_id: usize, session: *Session) void,
) !*Session.FinalizerCallback {
const session = self.ctx.session;
const arena = try session.getArena(.{ .debug = "FinalizerCallback" });
errdefer session.releaseArena(arena);
const fc = try arena.create(Session.FinalizerCallback);
fc.* = .{
.arena = arena,
.session = session,
._deinit = deinit,
.resolved_ptr_id = resolved_ptr_id,
.finalizer_ptr_id = finalizer_ptr_id,
};
return fc;
}
// Encapsulates a Local and a HandleScope. When we're going from V8->Zig
// we easily get both a Local and a HandleScope via Caller.init.
// But when we're going from Zig -> V8, things are more complicated.

View File

@@ -97,7 +97,7 @@ pub fn persist(self: Object) !Global {
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
try ctx.global_objects.append(ctx.arena, global);
try ctx.trackGlobal(global);
return .{ .handle = global };
}

73
src/browser/js/Origin.zig Normal file
View File

@@ -0,0 +1,73 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Origin represents the security token for contexts within the same origin.
// Multiple contexts (frames) from the same origin share a single Origin,
// which provides the V8 SecurityToken that allows cross-context access.
//
// Note: Identity tracking (mapping Zig instances to v8::Objects) is managed
// separately via js.Identity - Session has the main world Identity, and
// IsolatedWorlds have their own Identity instances.
const std = @import("std");
const js = @import("js.zig");
const App = @import("../../App.zig");
const v8 = js.v8;
const Allocator = std.mem.Allocator;
const Origin = @This();
rc: usize = 1,
arena: Allocator,
// The key, e.g. lightpanda.io:443
key: []const u8,
// Security token - all contexts in this origin must use the same v8::Value instance
// as their security token for V8 to allow cross-context access
security_token: v8.Global,
pub fn init(app: *App, isolate: js.Isolate, key: []const u8) !*Origin {
const arena = try app.arena_pool.acquire(.{ .debug = "Origin" });
errdefer app.arena_pool.release(arena);
var hs: js.HandleScope = undefined;
hs.init(isolate);
defer hs.deinit();
const owned_key = try arena.dupe(u8, key);
const token_local = isolate.initStringHandle(owned_key);
var token_global: v8.Global = undefined;
v8.v8__Global__New(isolate.handle, token_local, &token_global);
const self = try arena.create(Origin);
self.* = .{
.rc = 1,
.arena = arena,
.key = owned_key,
.security_token = token_global,
};
return self;
}
pub fn deinit(self: *Origin, app: *App) void {
v8.v8__Global__Reset(&self.security_token);
app.arena_pool.release(self.arena);
}

View File

@@ -16,6 +16,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const js = @import("js.zig");
const v8 = js.v8;
@@ -62,22 +63,25 @@ fn _persist(self: *const Promise, comptime is_global: bool) !(if (is_global) Glo
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
if (comptime is_global) {
try ctx.global_promises.append(ctx.arena, global);
} else {
try ctx.global_promises_temp.put(ctx.arena, global.data_ptr, global);
try ctx.trackGlobal(global);
return .{ .handle = global, .temps = {} };
}
return .{ .handle = global };
try ctx.trackTemp(global);
return .{ .handle = global, .temps = &ctx.session.temps };
}
pub const Temp = G(0);
pub const Global = G(1);
pub const Temp = G(.temp);
pub const Global = G(.global);
fn G(comptime discriminator: u8) type {
const GlobalType = enum(u8) {
temp,
global,
};
fn G(comptime global_type: GlobalType) type {
return struct {
handle: v8.Global,
// makes the types different (G(0) != G(1)), without taking up space
comptime _: u8 = discriminator,
temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,
const Self = @This();
@@ -91,5 +95,12 @@ fn G(comptime discriminator: u8) type {
.handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),
};
}
pub fn release(self: *const Self) void {
if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {
var g = kv.value;
v8.v8__Global__Reset(&g);
}
}
};
}

View File

@@ -18,8 +18,11 @@
const js = @import("js.zig");
const v8 = js.v8;
const log = @import("../../log.zig");
const DOMException = @import("../webapi/DOMException.zig");
const PromiseResolver = @This();
local: *const js.Local,
@@ -63,6 +66,43 @@ pub fn reject(self: PromiseResolver, comptime source: []const u8, value: anytype
};
}
pub const RejectError = union(enum) {
/// Not to be confused with `DOMException`; this is bare `Error`.
generic_error: []const u8,
range_error: []const u8,
reference_error: []const u8,
syntax_error: []const u8,
type_error: []const u8,
/// DOM exceptions are unknown to V8, belongs to web standards.
dom_exception: struct { err: anyerror },
};
/// Rejects the promise w/ an error object.
pub fn rejectError(
self: PromiseResolver,
comptime source: []const u8,
err: RejectError,
) void {
const handle = switch (err) {
.generic_error => |msg| self.local.isolate.createError(msg),
.range_error => |msg| self.local.isolate.createRangeError(msg),
.reference_error => |msg| self.local.isolate.createReferenceError(msg),
.syntax_error => |msg| self.local.isolate.createSyntaxError(msg),
.type_error => |msg| self.local.isolate.createTypeError(msg),
// "Exceptional".
.dom_exception => |exception| {
self._reject(DOMException.fromError(exception.err) orelse unreachable) catch |reject_err| {
log.err(.bug, "rejectDomException", .{ .source = source, .err = reject_err, .persistent = false });
};
return;
},
};
self._reject(js.Value{ .handle = handle, .local = self.local }) catch |reject_err| {
log.err(.bug, "rejectError", .{ .source = source, .err = reject_err, .persistent = false });
};
}
fn _reject(self: PromiseResolver, value: anytype) !void {
const local = self.local;
const js_val = try local.zigValueToJs(value, .{});
@@ -79,7 +119,7 @@ pub fn persist(self: PromiseResolver) !Global {
var ctx = self.local.ctx;
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
try ctx.global_promise_resolvers.append(ctx.arena, global);
try ctx.trackGlobal(global);
return .{ .handle = global };
}

View File

@@ -74,9 +74,10 @@ pub fn add(self: *Scheduler, ctx: *anyopaque, cb: Callback, run_in_ms: u32, opts
});
}
pub fn run(self: *Scheduler) !?u64 {
_ = try self.runQueue(&self.low_priority);
return self.runQueue(&self.high_priority);
pub fn run(self: *Scheduler) !void {
const now = milliTimestamp(.monotonic);
try self.runQueue(&self.low_priority, now);
try self.runQueue(&self.high_priority, now);
}
pub fn hasReadyTasks(self: *Scheduler) bool {
@@ -84,16 +85,23 @@ pub fn hasReadyTasks(self: *Scheduler) bool {
return queueuHasReadyTask(&self.low_priority, now) or queueuHasReadyTask(&self.high_priority, now);
}
fn runQueue(self: *Scheduler, queue: *Queue) !?u64 {
if (queue.count() == 0) {
return null;
pub fn msToNextHigh(self: *Scheduler) ?u64 {
const task = self.high_priority.peek() orelse return null;
const now = milliTimestamp(.monotonic);
if (task.run_at <= now) {
return 0;
}
return @intCast(task.run_at - now);
}
const now = milliTimestamp(.monotonic);
fn runQueue(self: *Scheduler, queue: *Queue, now: u64) !void {
if (queue.count() == 0) {
return;
}
while (queue.peek()) |*task_| {
if (task_.run_at > now) {
return @intCast(task_.run_at - now);
return;
}
var task = queue.remove();
if (comptime IS_DEBUG) {
@@ -114,7 +122,7 @@ fn runQueue(self: *Scheduler, queue: *Queue) !?u64 {
try self.low_priority.add(task);
}
}
return null;
return;
}
fn queueuHasReadyTask(queue: *Queue, now: u64) bool {

View File

@@ -25,7 +25,6 @@ const IS_DEBUG = @import("builtin").mode == .Debug;
const v8 = js.v8;
const JsApis = bridge.JsApis;
const Allocator = std.mem.Allocator;
const Snapshot = @This();
@@ -137,7 +136,7 @@ pub fn create() !Snapshot {
defer v8.v8__HandleScope__DESTRUCT(&handle_scope);
// Create templates (constructors only) FIRST
var templates: [JsApis.len]*v8.FunctionTemplate = undefined;
var templates: [JsApis.len]*const v8.FunctionTemplate = undefined;
inline for (JsApis, 0..) |JsApi, i| {
@setEvalBranchQuota(10_000);
templates[i] = generateConstructor(JsApi, isolate);
@@ -419,7 +418,7 @@ fn collectExternalReferences() [countExternalReferences()]isize {
// via `new ClassName()` - but they could, for example, be created in
// Zig and returned from a function call, which is why we need the
// FunctionTemplate.
fn generateConstructor(comptime JsApi: type, isolate: *v8.Isolate) *v8.FunctionTemplate {
fn generateConstructor(comptime JsApi: type, isolate: *v8.Isolate) *const v8.FunctionTemplate {
const callback = blk: {
if (@hasDecl(JsApi, "constructor")) {
break :blk JsApi.constructor.func;
@@ -429,7 +428,7 @@ fn generateConstructor(comptime JsApi: type, isolate: *v8.Isolate) *v8.FunctionT
break :blk illegalConstructorCallback;
};
const template = @constCast(v8.v8__FunctionTemplate__New__DEFAULT2(isolate, callback).?);
const template = v8.v8__FunctionTemplate__New__DEFAULT2(isolate, callback).?;
{
const internal_field_count = comptime countInternalFields(JsApi);
if (internal_field_count > 0) {
@@ -482,10 +481,15 @@ pub fn countInternalFields(comptime JsApi: type) u8 {
}
// Attaches JsApi members to the prototype template (normal case)
fn attachClass(comptime JsApi: type, isolate: *v8.Isolate, template: *v8.FunctionTemplate) void {
fn attachClass(comptime JsApi: type, isolate: *v8.Isolate, template: *const v8.FunctionTemplate) void {
const instance = v8.v8__FunctionTemplate__InstanceTemplate(template);
const prototype = v8.v8__FunctionTemplate__PrototypeTemplate(template);
// Create a signature that validates the receiver is an instance of this template.
// This prevents crashes when JavaScript extracts a getter/method and calls it
// with the wrong `this` (e.g., documentGetter.call(null)).
const signature = v8.v8__Signature__New(isolate, template);
const declarations = @typeInfo(JsApi).@"struct".decls;
var has_named_index_getter = false;
@@ -497,23 +501,47 @@ fn attachClass(comptime JsApi: type, isolate: *v8.Isolate, template: *v8.Functio
switch (definition) {
bridge.Accessor => {
const js_name = v8.v8__String__NewFromUtf8(isolate, name.ptr, v8.kNormal, @intCast(name.len));
const getter_callback = @constCast(v8.v8__FunctionTemplate__New__Config(isolate, &.{ .callback = value.getter }).?);
const getter_signature = if (value.static) null else signature;
const getter_callback = v8.v8__FunctionTemplate__New__Config(isolate, &.{
.callback = value.getter,
.signature = getter_signature,
}).?;
const setter_callback = if (value.setter) |setter|
v8.v8__FunctionTemplate__New__Config(isolate, &.{
.callback = setter,
.signature = getter_signature,
}).?
else
null;
var attribute: v8.PropertyAttribute = 0;
if (value.setter == null) {
attribute |= v8.ReadOnly;
}
if (value.deletable == false) {
attribute |= v8.DontDelete;
}
if (value.static) {
v8.v8__Template__SetAccessorProperty__DEFAULT(@ptrCast(template), js_name, getter_callback);
// Static accessors: use Template's SetAccessorProperty
v8.v8__Template__SetAccessorProperty(@ptrCast(template), js_name, getter_callback, setter_callback, attribute);
} else {
v8.v8__ObjectTemplate__SetAccessorProperty__DEFAULT(prototype, js_name, getter_callback);
}
} else {
if (comptime IS_DEBUG) {
std.debug.assert(value.static == false);
}
const setter_callback = @constCast(v8.v8__FunctionTemplate__New__Config(isolate, &.{ .callback = value.setter.? }).?);
v8.v8__ObjectTemplate__SetAccessorProperty__DEFAULT2(prototype, js_name, getter_callback, setter_callback);
v8.v8__ObjectTemplate__SetAccessorProperty__Config(prototype, &.{
.key = js_name,
.getter = getter_callback,
.setter = setter_callback,
.attribute = attribute,
});
}
},
bridge.Function => {
const function_template = @constCast(v8.v8__FunctionTemplate__New__Config(isolate, &.{ .callback = value.func, .length = value.arity }).?);
// For non-static functions, use the signature to validate the receiver
const func_signature = if (value.static) null else signature;
const function_template = v8.v8__FunctionTemplate__New__Config(isolate, &.{
.callback = value.func,
.length = value.arity,
.signature = func_signature,
}).?;
const js_name = v8.v8__String__NewFromUtf8(isolate, name.ptr, v8.kNormal, @intCast(name.len));
if (value.static) {
v8.v8__Template__Set(@ptrCast(template), js_name, @ptrCast(function_template), v8.None);
@@ -551,7 +579,7 @@ fn attachClass(comptime JsApi: type, isolate: *v8.Isolate, template: *v8.Functio
has_named_index_getter = true;
},
bridge.Iterator => {
const function_template = @constCast(v8.v8__FunctionTemplate__New__Config(isolate, &.{ .callback = value.func }).?);
const function_template = v8.v8__FunctionTemplate__New__Config(isolate, &.{ .callback = value.func }).?;
const js_name = if (value.async)
v8.v8__Symbol__GetAsyncIterator(isolate)
else

View File

@@ -56,7 +56,7 @@ fn _toSlice(self: String, comptime null_terminate: bool, allocator: Allocator) !
pub fn toSSO(self: String, comptime global: bool) !(if (global) SSO.Global else SSO) {
if (comptime global) {
return .{ .str = try self.toSSOWithAlloc(self.local.ctx.arena) };
return .{ .str = try self.toSSOWithAlloc(self.local.ctx.session.page_arena) };
}
return self.toSSOWithAlloc(self.local.call_arena);
}

View File

@@ -245,6 +245,46 @@ pub fn toJson(self: Value, allocator: Allocator) ![]u8 {
return js.String.toSliceWithAlloc(.{ .local = local, .handle = str_handle }, allocator);
}
// Currently does not support host objects (Blob, File, etc.) or transferables
// which require delegate callbacks to be implemented.
pub fn structuredClone(self: Value) !Value {
const local = self.local;
const v8_context = local.handle;
const v8_isolate = local.isolate.handle;
const size, const data = blk: {
const serializer = v8.v8__ValueSerializer__New(v8_isolate, null) orelse return error.JsException;
defer v8.v8__ValueSerializer__DELETE(serializer);
var write_result: v8.MaybeBool = undefined;
v8.v8__ValueSerializer__WriteHeader(serializer);
v8.v8__ValueSerializer__WriteValue(serializer, v8_context, self.handle, &write_result);
if (!write_result.has_value or !write_result.value) {
return error.JsException;
}
var size: usize = undefined;
const data = v8.v8__ValueSerializer__Release(serializer, &size) orelse return error.JsException;
break :blk .{ size, data };
};
defer v8.v8__ValueSerializer__FreeBuffer(data);
const cloned_handle = blk: {
const deserializer = v8.v8__ValueDeserializer__New(v8_isolate, data, size, null) orelse return error.JsException;
defer v8.v8__ValueDeserializer__DELETE(deserializer);
var read_header_result: v8.MaybeBool = undefined;
v8.v8__ValueDeserializer__ReadHeader(deserializer, v8_context, &read_header_result);
if (!read_header_result.has_value or !read_header_result.value) {
return error.JsException;
}
break :blk v8.v8__ValueDeserializer__ReadValue(deserializer, v8_context) orelse return error.JsException;
};
return .{ .local = local, .handle = cloned_handle };
}
pub fn persist(self: Value) !Global {
return self._persist(true);
}
@@ -259,11 +299,11 @@ fn _persist(self: *const Value, comptime is_global: bool) !(if (is_global) Globa
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
if (comptime is_global) {
try ctx.global_values.append(ctx.arena, global);
} else {
try ctx.global_values_temp.put(ctx.arena, global.data_ptr, global);
try ctx.trackGlobal(global);
return .{ .handle = global, .temps = {} };
}
return .{ .handle = global };
try ctx.trackTemp(global);
return .{ .handle = global, .temps = &ctx.session.temps };
}
pub fn toZig(self: Value, comptime T: type) !T {
@@ -310,15 +350,18 @@ pub fn format(self: Value, writer: *std.Io.Writer) !void {
return js_str.format(writer);
}
pub const Temp = G(0);
pub const Global = G(1);
pub const Temp = G(.temp);
pub const Global = G(.global);
fn G(comptime discriminator: u8) type {
const GlobalType = enum(u8) {
temp,
global,
};
fn G(comptime global_type: GlobalType) type {
return struct {
handle: v8.Global,
// makes the types different (G(0) != G(1)), without taking up space
comptime _: u8 = discriminator,
temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,
const Self = @This();
@@ -336,5 +379,12 @@ fn G(comptime discriminator: u8) type {
pub fn isEqual(self: *const Self, other: Value) bool {
return v8.v8__Global__IsEqual(&self.handle, other.handle);
}
pub fn release(self: *const Self) void {
if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {
var g = kv.value;
v8.v8__Global__Reset(&g);
}
}
};
}

View File

@@ -18,14 +18,12 @@
const std = @import("std");
const js = @import("js.zig");
const lp = @import("lightpanda");
const log = @import("../../log.zig");
const Page = @import("../Page.zig");
const Session = @import("../Session.zig");
const v8 = js.v8;
const Caller = @import("Caller.zig");
const Context = @import("Context.zig");
const IS_DEBUG = @import("builtin").mode == .Debug;
@@ -103,35 +101,19 @@ pub fn Builder(comptime T: type) type {
}
return entries;
}
pub fn finalizer(comptime func: *const fn (self: *T, shutdown: bool, page: *Page) void) Finalizer {
return .{
.from_zig = struct {
fn wrap(ptr: *anyopaque, page: *Page) void {
func(@ptrCast(@alignCast(ptr)), true, page);
}
}.wrap,
.from_v8 = struct {
fn wrap(handle: ?*const v8.WeakCallbackInfo) callconv(.c) void {
const ptr = v8.v8__WeakCallbackInfo__GetParameter(handle.?).?;
const fc: *Context.FinalizerCallback = @ptrCast(@alignCast(ptr));
const ctx = fc.ctx;
const value_ptr = fc.ptr;
if (ctx.finalizer_callbacks.contains(@intFromPtr(value_ptr))) {
func(@ptrCast(@alignCast(value_ptr)), false, ctx.page);
ctx.release(value_ptr);
} else {
// A bit weird, but v8 _requires_ that we release it
// If we don't. We'll 100% crash.
v8.v8__Global__Reset(&fc.global);
}
}
}.wrap,
};
}
};
fn releaseRef(comptime T: type, ptr_id: usize, session: *Session) void {
if (@hasDecl(T, "releaseRef")) {
T.releaseRef(@ptrFromInt(ptr_id), session);
return;
}
if (@hasField(T, "_proto")) {
releaseRef(Struct(std.meta.fieldInfo(T, ._proto).type), ptr_id, session);
return;
}
@compileError(@typeName(T) ++ " marked with finalizer without an acquireRef in its prototype chain");
}
pub const Constructor = struct {
@@ -198,6 +180,7 @@ pub const Function = struct {
pub const Accessor = struct {
static: bool = false,
deletable: bool = true,
cache: ?Caller.Function.Opts.Caching = null,
getter: ?*const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void = null,
setter: ?*const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void = null,
@@ -206,6 +189,7 @@ pub const Accessor = struct {
var accessor = Accessor{
.cache = opts.cache,
.static = opts.static,
.deletable = opts.deletable,
};
if (@typeInfo(@TypeOf(getter)) != .null) {
@@ -412,17 +396,6 @@ pub const Property = struct {
}
};
const Finalizer = struct {
// The finalizer wrapper when called fro Zig. This is only called on
// Context.deinit
from_zig: *const fn (ctx: *anyopaque, page: *Page) void,
// The finalizer wrapper when called from V8. This may never be called
// (hence why we fallback to calling in Context.denit). If it is called,
// it is only ever called after we SetWeak on the Global.
from_v8: *const fn (?*const v8.WeakCallbackInfo) callconv(.c) void,
};
pub fn unknownWindowPropertyCallback(c_name: ?*const v8.Name, handle: ?*const v8.PropertyCallbackInfo) callconv(.c) u8 {
const v8_isolate = v8.v8__PropertyCallbackInfo__GetIsolate(handle).?;
var caller: Caller = undefined;
@@ -723,6 +696,8 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/collections.zig"),
@import("../webapi/Console.zig"),
@import("../webapi/Crypto.zig"),
@import("../webapi/Permissions.zig"),
@import("../webapi/StorageManager.zig"),
@import("../webapi/CSS.zig"),
@import("../webapi/css/CSSRule.zig"),
@import("../webapi/css/CSSRuleList.zig"),
@@ -730,6 +705,7 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/css/CSSStyleRule.zig"),
@import("../webapi/css/CSSStyleSheet.zig"),
@import("../webapi/css/CSSStyleProperties.zig"),
@import("../webapi/css/FontFace.zig"),
@import("../webapi/css/FontFaceSet.zig"),
@import("../webapi/css/MediaQueryList.zig"),
@import("../webapi/css/StyleSheetList.zig"),
@@ -845,7 +821,10 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/event/FocusEvent.zig"),
@import("../webapi/event/WheelEvent.zig"),
@import("../webapi/event/TextEvent.zig"),
@import("../webapi/event/InputEvent.zig"),
@import("../webapi/event/PromiseRejectionEvent.zig"),
@import("../webapi/event/SubmitEvent.zig"),
@import("../webapi/event/FormDataEvent.zig"),
@import("../webapi/MessageChannel.zig"),
@import("../webapi/MessagePort.zig"),
@import("../webapi/media/MediaError.zig"),
@@ -882,6 +861,7 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/IdleDeadline.zig"),
@import("../webapi/Blob.zig"),
@import("../webapi/File.zig"),
@import("../webapi/FileList.zig"),
@import("../webapi/FileReader.zig"),
@import("../webapi/Screen.zig"),
@import("../webapi/VisualViewport.zig"),
@@ -894,6 +874,7 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/canvas/OffscreenCanvas.zig"),
@import("../webapi/canvas/OffscreenCanvasRenderingContext2D.zig"),
@import("../webapi/SubtleCrypto.zig"),
@import("../webapi/CryptoKey.zig"),
@import("../webapi/Selection.zig"),
@import("../webapi/ImageData.zig"),
});

View File

@@ -24,6 +24,8 @@ const string = @import("../../string.zig");
pub const Env = @import("Env.zig");
pub const bridge = @import("bridge.zig");
pub const Caller = @import("Caller.zig");
pub const Origin = @import("Origin.zig");
pub const Identity = @import("Identity.zig");
pub const Context = @import("Context.zig");
pub const Local = @import("Local.zig");
pub const Inspector = @import("Inspector.zig");
@@ -161,7 +163,7 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {
var ctx = self.local.ctx;
var global: v8.Global = undefined;
v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);
try ctx.global_values.append(ctx.arena, global);
try ctx.trackGlobal(global);
return .{ .handle = global };
}

54
src/browser/links.zig Normal file
View File

@@ -0,0 +1,54 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const Page = @import("Page.zig");
const Selector = @import("webapi/selector/Selector.zig");
const Allocator = std.mem.Allocator;
/// Collect all links (href attributes from anchor tags) under `root`.
/// Returns a slice of strings allocated with `arena`.
pub fn collectLinks(arena: Allocator, root: *Node, page: *Page) ![]const []const u8 {
var links: std.ArrayList([]const u8) = .empty;
if (Selector.querySelectorAll(root, "a[href]", page)) |list| {
defer list.deinit(page._session);
for (list._nodes) |node| {
if (node.is(Element.Html.Anchor)) |anchor| {
const href = anchor.getHref(page) catch |err| {
@import("../lightpanda.zig").log.err(.app, "resolve href failed", .{ .err = err });
continue;
};
if (href.len > 0) {
try links.append(arena, href);
}
}
}
} else |err| {
@import("../lightpanda.zig").log.err(.app, "query links failed", .{ .err = err });
return err;
}
return links.items;
}

View File

@@ -21,9 +21,9 @@ const std = @import("std");
const Page = @import("Page.zig");
const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const CData = @import("webapi/CData.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const isAllWhitespace = @import("../string.zig").isAllWhitespace;
pub const Opts = struct {
// Options for future customization (e.g., dialect)
@@ -46,13 +46,6 @@ const State = struct {
last_char_was_newline: bool = true,
};
fn isBlock(tag: Element.Tag) bool {
return switch (tag) {
.p, .div, .section, .article, .main, .header, .footer, .nav, .aside, .h1, .h2, .h3, .h4, .h5, .h6, .ul, .ol, .blockquote, .pre, .table, .hr => true,
else => false,
};
}
fn shouldAddSpacing(tag: Element.Tag) bool {
return switch (tag) {
.p, .h1, .h2, .h3, .h4, .h5, .h6, .blockquote, .pre, .table => true,
@@ -99,26 +92,18 @@ fn isSignificantText(node: *Node) bool {
}
fn isVisibleElement(el: *Element) bool {
return switch (el.getTag()) {
.script, .style, .noscript, .template, .head, .meta, .link, .title, .svg => false,
else => true,
};
const tag = el.getTag();
return !tag.isMetadata() and tag != .svg;
}
fn getAnchorLabel(el: *Element) ?[]const u8 {
return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));
}
fn isAllWhitespace(text: []const u8) bool {
return for (text) |c| {
if (!std.ascii.isWhitespace(c)) break false;
} else true;
}
fn hasBlockDescendant(root: *Node) bool {
var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{});
while (tw.next()) |el| {
if (isBlock(el.getTag())) return true;
if (el.getTag().isBlock()) return true;
}
return false;
}
@@ -138,53 +123,49 @@ fn hasVisibleContent(root: *Node) bool {
return false;
}
fn ensureNewline(state: *State, writer: *std.Io.Writer) !void {
if (!state.last_char_was_newline) {
try writer.writeByte('\n');
state.last_char_was_newline = true;
const Context = struct {
state: State,
writer: *std.Io.Writer,
page: *Page,
fn ensureNewline(self: *Context) !void {
if (!self.state.last_char_was_newline) {
try self.writer.writeByte('\n');
self.state.last_char_was_newline = true;
}
}
pub fn dump(node: *Node, opts: Opts, writer: *std.Io.Writer, page: *Page) !void {
_ = opts;
var state = State{};
try render(node, &state, writer, page);
if (!state.last_char_was_newline) {
try writer.writeByte('\n');
}
}
fn render(node: *Node, state: *State, writer: *std.Io.Writer, page: *Page) error{WriteFailed}!void {
fn render(self: *Context, node: *Node) error{WriteFailed}!void {
switch (node._type) {
.document, .document_fragment => {
try renderChildren(node, state, writer, page);
try self.renderChildren(node);
},
.element => |el| {
try renderElement(el, state, writer, page);
try self.renderElement(el);
},
.cdata => |cd| {
if (node.is(Node.CData.Text)) |_| {
var text = cd.getData().str();
if (state.pre_node) |pre| {
if (self.state.pre_node) |pre| {
if (node.parentNode() == pre and node.nextSibling() == null) {
text = std.mem.trimRight(u8, text, " \t\r\n");
}
}
try renderText(text, state, writer);
try self.renderText(text);
}
},
else => {},
}
}
fn renderChildren(parent: *Node, state: *State, writer: *std.Io.Writer, page: *Page) !void {
fn renderChildren(self: *Context, parent: *Node) !void {
var it = parent.childrenIterator();
while (it.next()) |child| {
try render(child, state, writer, page);
try self.render(child);
}
}
fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Page) !void {
fn renderElement(self: *Context, el: *Element) !void {
const tag = el.getTag();
if (!isVisibleElement(el)) return;
@@ -192,116 +173,116 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
// --- Opening Tag Logic ---
// Ensure block elements start on a new line (double newline for paragraphs etc)
if (isBlock(tag) and !state.in_table) {
try ensureNewline(state, writer);
if (tag.isBlock() and !self.state.in_table) {
try self.ensureNewline();
if (shouldAddSpacing(tag)) {
try writer.writeByte('\n');
try self.writer.writeByte('\n');
}
} else if (tag == .li or tag == .tr) {
try ensureNewline(state, writer);
try self.ensureNewline();
}
// Prefixes
switch (tag) {
.h1 => try writer.writeAll("# "),
.h2 => try writer.writeAll("## "),
.h3 => try writer.writeAll("### "),
.h4 => try writer.writeAll("#### "),
.h5 => try writer.writeAll("##### "),
.h6 => try writer.writeAll("###### "),
.h1 => try self.writer.writeAll("# "),
.h2 => try self.writer.writeAll("## "),
.h3 => try self.writer.writeAll("### "),
.h4 => try self.writer.writeAll("#### "),
.h5 => try self.writer.writeAll("##### "),
.h6 => try self.writer.writeAll("###### "),
.ul => {
if (state.list_depth < state.list_stack.len) {
state.list_stack[state.list_depth] = .{ .type = .unordered, .index = 0 };
state.list_depth += 1;
if (self.state.list_depth < self.state.list_stack.len) {
self.state.list_stack[self.state.list_depth] = .{ .type = .unordered, .index = 0 };
self.state.list_depth += 1;
}
},
.ol => {
if (state.list_depth < state.list_stack.len) {
state.list_stack[state.list_depth] = .{ .type = .ordered, .index = 1 };
state.list_depth += 1;
if (self.state.list_depth < self.state.list_stack.len) {
self.state.list_stack[self.state.list_depth] = .{ .type = .ordered, .index = 1 };
self.state.list_depth += 1;
}
},
.li => {
const indent = if (state.list_depth > 0) state.list_depth - 1 else 0;
for (0..indent) |_| try writer.writeAll(" ");
const indent = if (self.state.list_depth > 0) self.state.list_depth - 1 else 0;
for (0..indent) |_| try self.writer.writeAll(" ");
if (state.list_depth > 0 and state.list_stack[state.list_depth - 1].type == .ordered) {
const current_list = &state.list_stack[state.list_depth - 1];
try writer.print("{d}. ", .{current_list.index});
if (self.state.list_depth > 0 and self.state.list_stack[self.state.list_depth - 1].type == .ordered) {
const current_list = &self.state.list_stack[self.state.list_depth - 1];
try self.writer.print("{d}. ", .{current_list.index});
current_list.index += 1;
} else {
try writer.writeAll("- ");
try self.writer.writeAll("- ");
}
state.last_char_was_newline = false;
self.state.last_char_was_newline = false;
},
.table => {
state.in_table = true;
state.table_row_index = 0;
state.table_col_count = 0;
self.state.in_table = true;
self.state.table_row_index = 0;
self.state.table_col_count = 0;
},
.tr => {
state.table_col_count = 0;
try writer.writeByte('|');
self.state.table_col_count = 0;
try self.writer.writeByte('|');
},
.td, .th => {
// Note: leading pipe handled by previous cell closing or tr opening
state.last_char_was_newline = false;
try writer.writeByte(' ');
self.state.last_char_was_newline = false;
try self.writer.writeByte(' ');
},
.blockquote => {
try writer.writeAll("> ");
state.last_char_was_newline = false;
try self.writer.writeAll("> ");
self.state.last_char_was_newline = false;
},
.pre => {
try writer.writeAll("```\n");
state.pre_node = el.asNode();
state.last_char_was_newline = true;
try self.writer.writeAll("```\n");
self.state.pre_node = el.asNode();
self.state.last_char_was_newline = true;
},
.code => {
if (state.pre_node == null) {
try writer.writeByte('`');
state.in_code = true;
state.last_char_was_newline = false;
if (self.state.pre_node == null) {
try self.writer.writeByte('`');
self.state.in_code = true;
self.state.last_char_was_newline = false;
}
},
.b, .strong => {
try writer.writeAll("**");
state.last_char_was_newline = false;
try self.writer.writeAll("**");
self.state.last_char_was_newline = false;
},
.i, .em => {
try writer.writeAll("*");
state.last_char_was_newline = false;
try self.writer.writeAll("*");
self.state.last_char_was_newline = false;
},
.s, .del => {
try writer.writeAll("~~");
state.last_char_was_newline = false;
try self.writer.writeAll("~~");
self.state.last_char_was_newline = false;
},
.hr => {
try writer.writeAll("---\n");
state.last_char_was_newline = true;
try self.writer.writeAll("---\n");
self.state.last_char_was_newline = true;
return;
},
.br => {
if (state.in_table) {
try writer.writeByte(' ');
if (self.state.in_table) {
try self.writer.writeByte(' ');
} else {
try writer.writeByte('\n');
state.last_char_was_newline = true;
try self.writer.writeByte('\n');
self.state.last_char_was_newline = true;
}
return;
},
.img => {
try writer.writeAll("![");
try self.writer.writeAll("![");
if (el.getAttributeSafe(comptime .wrap("alt"))) |alt| {
try escapeMarkdown(writer, alt);
try self.escape(alt);
}
try writer.writeAll("](");
try self.writer.writeAll("](");
if (el.getAttributeSafe(comptime .wrap("src"))) |src| {
const absolute_src = URL.resolve(page.call_arena, page.base(), src, .{ .encode = true }) catch src;
try writer.writeAll(absolute_src);
const absolute_src = URL.resolve(self.page.call_arena, self.page.base(), src, .{ .encode = true }) catch src;
try self.writer.writeAll(absolute_src);
}
try writer.writeAll(")");
state.last_char_was_newline = false;
try self.writer.writeAll(")");
self.state.last_char_was_newline = false;
return;
},
.anchor => {
@@ -312,57 +293,57 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
if (!has_content and label == null and href_raw == null) return;
const has_block = hasBlockDescendant(el.asNode());
const href = if (href_raw) |h| URL.resolve(page.call_arena, page.base(), h, .{ .encode = true }) catch h else null;
const href = if (href_raw) |h| URL.resolve(self.page.call_arena, self.page.base(), h, .{ .encode = true }) catch h else null;
if (has_block) {
try renderChildren(el.asNode(), state, writer, page);
try self.renderChildren(el.asNode());
if (href) |h| {
if (!state.last_char_was_newline) try writer.writeByte('\n');
try writer.writeAll("([](");
try writer.writeAll(h);
try writer.writeAll("))\n");
state.last_char_was_newline = true;
if (!self.state.last_char_was_newline) try self.writer.writeByte('\n');
try self.writer.writeAll("([](");
try self.writer.writeAll(h);
try self.writer.writeAll("))\n");
self.state.last_char_was_newline = true;
}
return;
}
if (isStandaloneAnchor(el)) {
if (!state.last_char_was_newline) try writer.writeByte('\n');
try writer.writeByte('[');
if (!self.state.last_char_was_newline) try self.writer.writeByte('\n');
try self.writer.writeByte('[');
if (has_content) {
try renderChildren(el.asNode(), state, writer, page);
try self.renderChildren(el.asNode());
} else {
try writer.writeAll(label orelse "");
try self.writer.writeAll(label orelse "");
}
try writer.writeAll("](");
try self.writer.writeAll("](");
if (href) |h| {
try writer.writeAll(h);
try self.writer.writeAll(h);
}
try writer.writeAll(")\n");
state.last_char_was_newline = true;
try self.writer.writeAll(")\n");
self.state.last_char_was_newline = true;
return;
}
try writer.writeByte('[');
try self.writer.writeByte('[');
if (has_content) {
try renderChildren(el.asNode(), state, writer, page);
try self.renderChildren(el.asNode());
} else {
try writer.writeAll(label orelse "");
try self.writer.writeAll(label orelse "");
}
try writer.writeAll("](");
try self.writer.writeAll("](");
if (href) |h| {
try writer.writeAll(h);
try self.writer.writeAll(h);
}
try writer.writeByte(')');
state.last_char_was_newline = false;
try self.writer.writeByte(')');
self.state.last_char_was_newline = false;
return;
},
.input => {
const type_attr = el.getAttributeSafe(comptime .wrap("type")) orelse return;
if (std.ascii.eqlIgnoreCase(type_attr, "checkbox")) {
const checked = el.getAttributeSafe(comptime .wrap("checked")) != null;
try writer.writeAll(if (checked) "[x] " else "[ ] ");
state.last_char_was_newline = false;
try self.writer.writeAll(if (checked) "[x] " else "[ ] ");
self.state.last_char_was_newline = false;
}
return;
},
@@ -370,85 +351,85 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
}
// --- Render Children ---
try renderChildren(el.asNode(), state, writer, page);
try self.renderChildren(el.asNode());
// --- Closing Tag Logic ---
// Suffixes
switch (tag) {
.pre => {
if (!state.last_char_was_newline) {
try writer.writeByte('\n');
if (!self.state.last_char_was_newline) {
try self.writer.writeByte('\n');
}
try writer.writeAll("```\n");
state.pre_node = null;
state.last_char_was_newline = true;
try self.writer.writeAll("```\n");
self.state.pre_node = null;
self.state.last_char_was_newline = true;
},
.code => {
if (state.pre_node == null) {
try writer.writeByte('`');
state.in_code = false;
state.last_char_was_newline = false;
if (self.state.pre_node == null) {
try self.writer.writeByte('`');
self.state.in_code = false;
self.state.last_char_was_newline = false;
}
},
.b, .strong => {
try writer.writeAll("**");
state.last_char_was_newline = false;
try self.writer.writeAll("**");
self.state.last_char_was_newline = false;
},
.i, .em => {
try writer.writeAll("*");
state.last_char_was_newline = false;
try self.writer.writeAll("*");
self.state.last_char_was_newline = false;
},
.s, .del => {
try writer.writeAll("~~");
state.last_char_was_newline = false;
try self.writer.writeAll("~~");
self.state.last_char_was_newline = false;
},
.blockquote => {},
.ul, .ol => {
if (state.list_depth > 0) state.list_depth -= 1;
if (self.state.list_depth > 0) self.state.list_depth -= 1;
},
.table => {
state.in_table = false;
self.state.in_table = false;
},
.tr => {
try writer.writeByte('\n');
if (state.table_row_index == 0) {
try writer.writeByte('|');
for (0..state.table_col_count) |_| {
try writer.writeAll("---|");
try self.writer.writeByte('\n');
if (self.state.table_row_index == 0) {
try self.writer.writeByte('|');
for (0..self.state.table_col_count) |_| {
try self.writer.writeAll("---|");
}
try writer.writeByte('\n');
try self.writer.writeByte('\n');
}
state.table_row_index += 1;
state.last_char_was_newline = true;
self.state.table_row_index += 1;
self.state.last_char_was_newline = true;
},
.td, .th => {
try writer.writeAll(" |");
state.table_col_count += 1;
state.last_char_was_newline = false;
try self.writer.writeAll(" |");
self.state.table_col_count += 1;
self.state.last_char_was_newline = false;
},
else => {},
}
// Post-block newlines
if (isBlock(tag) and !state.in_table) {
try ensureNewline(state, writer);
if (tag.isBlock() and !self.state.in_table) {
try self.ensureNewline();
}
}
fn renderText(text: []const u8, state: *State, writer: *std.Io.Writer) !void {
fn renderText(self: *Context, text: []const u8) !void {
if (text.len == 0) return;
if (state.pre_node) |_| {
try writer.writeAll(text);
state.last_char_was_newline = text[text.len - 1] == '\n';
if (self.state.pre_node) |_| {
try self.writer.writeAll(text);
self.state.last_char_was_newline = text[text.len - 1] == '\n';
return;
}
// Check for pure whitespace
if (isAllWhitespace(text)) {
if (!state.last_char_was_newline) {
try writer.writeByte(' ');
if (!self.state.last_char_was_newline) {
try self.writer.writeByte(' ');
}
return;
}
@@ -457,32 +438,46 @@ fn renderText(text: []const u8, state: *State, writer: *std.Io.Writer) !void {
var it = std.mem.tokenizeAny(u8, text, " \t\n\r");
var first = true;
while (it.next()) |word| {
if (!first or (!state.last_char_was_newline and std.ascii.isWhitespace(text[0]))) {
try writer.writeByte(' ');
if (!first or (!self.state.last_char_was_newline and std.ascii.isWhitespace(text[0]))) {
try self.writer.writeByte(' ');
}
try escapeMarkdown(writer, word);
state.last_char_was_newline = false;
try self.escape(word);
self.state.last_char_was_newline = false;
first = false;
}
// Handle trailing whitespace from the original text
if (!first and !state.last_char_was_newline and std.ascii.isWhitespace(text[text.len - 1])) {
try writer.writeByte(' ');
if (!first and !self.state.last_char_was_newline and std.ascii.isWhitespace(text[text.len - 1])) {
try self.writer.writeByte(' ');
}
}
fn escapeMarkdown(writer: *std.Io.Writer, text: []const u8) !void {
fn escape(self: *Context, text: []const u8) !void {
for (text) |c| {
switch (c) {
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '#', '+', '-', '!', '|' => {
try writer.writeByte('\\');
try writer.writeByte(c);
try self.writer.writeByte('\\');
try self.writer.writeByte(c);
},
else => try writer.writeByte(c),
else => try self.writer.writeByte(c),
}
}
}
};
pub fn dump(node: *Node, opts: Opts, writer: *std.Io.Writer, page: *Page) !void {
_ = opts;
var ctx: Context = .{
.state = .{},
.writer = writer,
.page = page,
};
try ctx.render(node);
if (!ctx.state.last_char_was_newline) {
try writer.writeByte('\n');
}
}
fn testMarkdownHTML(html: []const u8, expected: []const u8) !void {
const testing = @import("../testing.zig");

View File

@@ -23,6 +23,9 @@ const h5e = @import("html5ever.zig");
const Page = @import("../Page.zig");
const Node = @import("../webapi/Node.zig");
const Element = @import("../webapi/Element.zig");
pub const AttributeIterator = h5e.AttributeIterator;
const Allocator = std.mem.Allocator;
const IS_DEBUG = @import("builtin").mode == .Debug;

View File

@@ -0,0 +1,489 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("Page.zig");
const URL = @import("URL.zig");
const TreeWalker = @import("webapi/TreeWalker.zig");
const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig");
const Allocator = std.mem.Allocator;
/// Key-value pair for structured data properties.
pub const Property = struct {
key: []const u8,
value: []const u8,
};
pub const AlternateLink = struct {
href: []const u8,
hreflang: ?[]const u8,
type: ?[]const u8,
title: ?[]const u8,
};
pub const StructuredData = struct {
json_ld: []const []const u8,
open_graph: []const Property,
twitter_card: []const Property,
meta: []const Property,
links: []const Property,
alternate: []const AlternateLink,
pub fn jsonStringify(self: *const StructuredData, jw: anytype) !void {
try jw.beginObject();
try jw.objectField("jsonLd");
try jw.write(self.json_ld);
try jw.objectField("openGraph");
try writeProperties(jw, self.open_graph);
try jw.objectField("twitterCard");
try writeProperties(jw, self.twitter_card);
try jw.objectField("meta");
try writeProperties(jw, self.meta);
try jw.objectField("links");
try writeProperties(jw, self.links);
if (self.alternate.len > 0) {
try jw.objectField("alternate");
try jw.beginArray();
for (self.alternate) |alt| {
try jw.beginObject();
try jw.objectField("href");
try jw.write(alt.href);
if (alt.hreflang) |v| {
try jw.objectField("hreflang");
try jw.write(v);
}
if (alt.type) |v| {
try jw.objectField("type");
try jw.write(v);
}
if (alt.title) |v| {
try jw.objectField("title");
try jw.write(v);
}
try jw.endObject();
}
try jw.endArray();
}
try jw.endObject();
}
};
/// Serializes properties as a JSON object. When a key appears multiple times
/// (e.g. multiple og:image tags), values are grouped into an array.
/// Alternatives considered: always-array values (verbose), or an array of
/// {key, value} pairs (preserves order but less ergonomic for consumers).
fn writeProperties(jw: anytype, properties: []const Property) !void {
try jw.beginObject();
for (properties, 0..) |prop, i| {
// Skip keys already written by an earlier occurrence.
var already_written = false;
for (properties[0..i]) |prev| {
if (std.mem.eql(u8, prev.key, prop.key)) {
already_written = true;
break;
}
}
if (already_written) continue;
// Count total occurrences to decide string vs array.
var count: usize = 0;
for (properties) |p| {
if (std.mem.eql(u8, p.key, prop.key)) count += 1;
}
try jw.objectField(prop.key);
if (count == 1) {
try jw.write(prop.value);
} else {
try jw.beginArray();
for (properties) |p| {
if (std.mem.eql(u8, p.key, prop.key)) {
try jw.write(p.value);
}
}
try jw.endArray();
}
}
try jw.endObject();
}
/// Extract all structured data from the page.
pub fn collectStructuredData(
root: *Node,
arena: Allocator,
page: *Page,
) !StructuredData {
var json_ld: std.ArrayList([]const u8) = .empty;
var open_graph: std.ArrayList(Property) = .empty;
var twitter_card: std.ArrayList(Property) = .empty;
var meta: std.ArrayList(Property) = .empty;
var links: std.ArrayList(Property) = .empty;
var alternate: std.ArrayList(AlternateLink) = .empty;
// Extract language from the root <html> element.
if (root.is(Element)) |root_el| {
if (root_el.getAttributeSafe(comptime .wrap("lang"))) |lang| {
try meta.append(arena, .{ .key = "language", .value = lang });
}
} else {
// Root is document — check documentElement.
var children = root.childrenIterator();
while (children.next()) |child| {
const el = child.is(Element) orelse continue;
if (el.getTag() == .html) {
if (el.getAttributeSafe(comptime .wrap("lang"))) |lang| {
try meta.append(arena, .{ .key = "language", .value = lang });
}
break;
}
}
}
var tw = TreeWalker.Full.init(root, .{});
while (tw.next()) |node| {
const el = node.is(Element) orelse continue;
switch (el.getTag()) {
.script => {
try collectJsonLd(el, arena, &json_ld);
tw.skipChildren();
},
.meta => collectMeta(el, &open_graph, &twitter_card, &meta, arena) catch {},
.title => try collectTitle(node, arena, &meta),
.link => try collectLink(el, arena, page, &links, &alternate),
// Skip body subtree for non-JSON-LD — all other metadata is in <head>.
// JSON-LD can appear in <body> so we don't skip the whole body.
else => {},
}
}
return .{
.json_ld = json_ld.items,
.open_graph = open_graph.items,
.twitter_card = twitter_card.items,
.meta = meta.items,
.links = links.items,
.alternate = alternate.items,
};
}
fn collectJsonLd(
el: *Element,
arena: Allocator,
json_ld: *std.ArrayList([]const u8),
) !void {
const type_attr = el.getAttributeSafe(comptime .wrap("type")) orelse return;
if (!std.ascii.eqlIgnoreCase(type_attr, "application/ld+json")) return;
var buf: std.Io.Writer.Allocating = .init(arena);
try el.asNode().getTextContent(&buf.writer);
const text = buf.written();
if (text.len > 0) {
try json_ld.append(arena, std.mem.trim(u8, text, &std.ascii.whitespace));
}
}
fn collectMeta(
el: *Element,
open_graph: *std.ArrayList(Property),
twitter_card: *std.ArrayList(Property),
meta: *std.ArrayList(Property),
arena: Allocator,
) !void {
// charset: <meta charset="..."> (no content attribute needed).
if (el.getAttributeSafe(comptime .wrap("charset"))) |charset| {
try meta.append(arena, .{ .key = "charset", .value = charset });
}
const content = el.getAttributeSafe(comptime .wrap("content")) orelse return;
// Open Graph: <meta property="og:...">
if (el.getAttributeSafe(comptime .wrap("property"))) |property| {
if (std.mem.startsWith(u8, property, "og:")) {
try open_graph.append(arena, .{ .key = property[3..], .value = content });
return;
}
// Article, profile, etc. are OG sub-namespaces.
if (std.mem.startsWith(u8, property, "article:") or
std.mem.startsWith(u8, property, "profile:") or
std.mem.startsWith(u8, property, "book:") or
std.mem.startsWith(u8, property, "music:") or
std.mem.startsWith(u8, property, "video:"))
{
try open_graph.append(arena, .{ .key = property, .value = content });
return;
}
}
// Twitter Cards: <meta name="twitter:...">
if (el.getAttributeSafe(comptime .wrap("name"))) |name| {
if (std.mem.startsWith(u8, name, "twitter:")) {
try twitter_card.append(arena, .{ .key = name[8..], .value = content });
return;
}
// Standard meta tags by name.
const known_names = [_][]const u8{
"description", "author", "keywords", "robots",
"viewport", "generator", "theme-color",
};
for (known_names) |known| {
if (std.ascii.eqlIgnoreCase(name, known)) {
try meta.append(arena, .{ .key = known, .value = content });
return;
}
}
}
// http-equiv (e.g. Content-Type, refresh)
if (el.getAttributeSafe(comptime .wrap("http-equiv"))) |http_equiv| {
try meta.append(arena, .{ .key = http_equiv, .value = content });
}
}
fn collectTitle(
node: *Node,
arena: Allocator,
meta: *std.ArrayList(Property),
) !void {
var buf: std.Io.Writer.Allocating = .init(arena);
try node.getTextContent(&buf.writer);
const text = std.mem.trim(u8, buf.written(), &std.ascii.whitespace);
if (text.len > 0) {
try meta.append(arena, .{ .key = "title", .value = text });
}
}
fn collectLink(
el: *Element,
arena: Allocator,
page: *Page,
links: *std.ArrayList(Property),
alternate: *std.ArrayList(AlternateLink),
) !void {
const rel = el.getAttributeSafe(comptime .wrap("rel")) orelse return;
const raw_href = el.getAttributeSafe(comptime .wrap("href")) orelse return;
const href = URL.resolve(arena, page.base(), raw_href, .{ .encode = true }) catch raw_href;
if (std.ascii.eqlIgnoreCase(rel, "alternate")) {
try alternate.append(arena, .{
.href = href,
.hreflang = el.getAttributeSafe(comptime .wrap("hreflang")),
.type = el.getAttributeSafe(comptime .wrap("type")),
.title = el.getAttributeSafe(comptime .wrap("title")),
});
return;
}
const relevant_rels = [_][]const u8{
"canonical", "icon", "manifest", "shortcut icon",
"apple-touch-icon", "search", "author", "license",
"dns-prefetch", "preconnect",
};
for (relevant_rels) |known| {
if (std.ascii.eqlIgnoreCase(rel, known)) {
try links.append(arena, .{ .key = known, .value = href });
return;
}
}
}
// --- Tests ---
const testing = @import("../testing.zig");
fn testStructuredData(html: []const u8) !StructuredData {
const page = try testing.test_session.createPage();
defer testing.test_session.removePage();
const doc = page.window._document;
const div = try doc.createElement("div", null, page);
try page.parseHtmlAsChildren(div.asNode(), html);
return collectStructuredData(div.asNode(), page.call_arena, page);
}
fn findProperty(props: []const Property, key: []const u8) ?[]const u8 {
for (props) |p| {
if (std.mem.eql(u8, p.key, key)) return p.value;
}
return null;
}
test "structured_data: json-ld" {
const data = try testStructuredData(
\\<script type="application/ld+json">
\\{"@context":"https://schema.org","@type":"Article","headline":"Test"}
\\</script>
);
try testing.expectEqual(1, data.json_ld.len);
try testing.expect(std.mem.indexOf(u8, data.json_ld[0], "Article") != null);
}
test "structured_data: multiple json-ld" {
const data = try testStructuredData(
\\<script type="application/ld+json">{"@type":"Organization"}</script>
\\<script type="application/ld+json">{"@type":"BreadcrumbList"}</script>
\\<script type="text/javascript">var x = 1;</script>
);
try testing.expectEqual(2, data.json_ld.len);
}
test "structured_data: open graph" {
const data = try testStructuredData(
\\<meta property="og:title" content="My Page">
\\<meta property="og:description" content="A description">
\\<meta property="og:image" content="https://example.com/img.jpg">
\\<meta property="og:url" content="https://example.com">
\\<meta property="og:type" content="article">
\\<meta property="article:published_time" content="2026-03-10">
);
try testing.expectEqual(6, data.open_graph.len);
try testing.expectEqual("My Page", findProperty(data.open_graph, "title").?);
try testing.expectEqual("article", findProperty(data.open_graph, "type").?);
try testing.expectEqual("2026-03-10", findProperty(data.open_graph, "article:published_time").?);
}
test "structured_data: open graph duplicate keys" {
const data = try testStructuredData(
\\<meta property="og:title" content="My Page">
\\<meta property="og:image" content="https://example.com/img1.jpg">
\\<meta property="og:image" content="https://example.com/img2.jpg">
\\<meta property="og:image" content="https://example.com/img3.jpg">
);
// Duplicate keys are preserved as separate Property entries.
try testing.expectEqual(4, data.open_graph.len);
// Verify serialization groups duplicates into arrays.
const json = try std.json.Stringify.valueAlloc(testing.allocator, data, .{});
defer testing.allocator.free(json);
const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{});
defer parsed.deinit();
const og = parsed.value.object.get("openGraph").?.object;
// "title" appears once → string.
switch (og.get("title").?) {
.string => {},
else => return error.TestUnexpectedResult,
}
// "image" appears 3 times → array.
switch (og.get("image").?) {
.array => |arr| try testing.expectEqual(3, arr.items.len),
else => return error.TestUnexpectedResult,
}
}
test "structured_data: twitter card" {
const data = try testStructuredData(
\\<meta name="twitter:card" content="summary_large_image">
\\<meta name="twitter:site" content="@example">
\\<meta name="twitter:title" content="My Page">
);
try testing.expectEqual(3, data.twitter_card.len);
try testing.expectEqual("summary_large_image", findProperty(data.twitter_card, "card").?);
try testing.expectEqual("@example", findProperty(data.twitter_card, "site").?);
}
test "structured_data: meta tags" {
const data = try testStructuredData(
\\<title>Page Title</title>
\\<meta name="description" content="A test page">
\\<meta name="author" content="Test Author">
\\<meta name="keywords" content="test, example">
\\<meta name="robots" content="index, follow">
);
try testing.expectEqual("Page Title", findProperty(data.meta, "title").?);
try testing.expectEqual("A test page", findProperty(data.meta, "description").?);
try testing.expectEqual("Test Author", findProperty(data.meta, "author").?);
try testing.expectEqual("test, example", findProperty(data.meta, "keywords").?);
try testing.expectEqual("index, follow", findProperty(data.meta, "robots").?);
}
test "structured_data: link elements" {
const data = try testStructuredData(
\\<link rel="canonical" href="https://example.com/page">
\\<link rel="icon" href="/favicon.ico">
\\<link rel="manifest" href="/manifest.json">
\\<link rel="stylesheet" href="/style.css">
);
try testing.expectEqual(3, data.links.len);
try testing.expectEqual("https://example.com/page", findProperty(data.links, "canonical").?);
// stylesheet should be filtered out
try testing.expectEqual(null, findProperty(data.links, "stylesheet"));
}
test "structured_data: alternate links" {
const data = try testStructuredData(
\\<link rel="alternate" href="https://example.com/fr" hreflang="fr" title="French">
\\<link rel="alternate" href="https://example.com/de" hreflang="de">
);
try testing.expectEqual(2, data.alternate.len);
try testing.expectEqual("fr", data.alternate[0].hreflang.?);
try testing.expectEqual("French", data.alternate[0].title.?);
try testing.expectEqual("de", data.alternate[1].hreflang.?);
try testing.expectEqual(null, data.alternate[1].title);
}
test "structured_data: non-metadata elements ignored" {
const data = try testStructuredData(
\\<div>Just text</div>
\\<p>More text</p>
\\<a href="/link">Link</a>
);
try testing.expectEqual(0, data.json_ld.len);
try testing.expectEqual(0, data.open_graph.len);
try testing.expectEqual(0, data.twitter_card.len);
try testing.expectEqual(0, data.meta.len);
try testing.expectEqual(0, data.links.len);
}
test "structured_data: charset and http-equiv" {
const data = try testStructuredData(
\\<meta charset="utf-8">
\\<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
);
try testing.expectEqual("utf-8", findProperty(data.meta, "charset").?);
try testing.expectEqual("text/html; charset=utf-8", findProperty(data.meta, "Content-Type").?);
}
test "structured_data: mixed content" {
const data = try testStructuredData(
\\<title>My Site</title>
\\<meta property="og:title" content="OG Title">
\\<meta name="twitter:card" content="summary">
\\<meta name="description" content="A page">
\\<link rel="canonical" href="https://example.com">
\\<script type="application/ld+json">{"@type":"WebSite"}</script>
);
try testing.expectEqual(1, data.json_ld.len);
try testing.expectEqual(1, data.open_graph.len);
try testing.expectEqual(1, data.twitter_card.len);
try testing.expectEqual("My Site", findProperty(data.meta, "title").?);
try testing.expectEqual("A page", findProperty(data.meta, "description").?);
try testing.expectEqual(1, data.links.len);
}

View File

@@ -15,10 +15,10 @@
a1.play();
cb.push(a1.playState);
});
testing.eventually(() => testing.expectEqual(['idle', 'running', 'finished', true], cb));
testing.onload(() => testing.expectEqual(['idle', 'running', 'finished', true], cb));
</script>
<script id=startTime>
<!-- <script id=startTime>
let a2 = document.createElement('div').animate(null, null);
// startTime defaults to null
testing.expectEqual(null, a2.startTime);
@@ -39,7 +39,7 @@
// onfinish callback should be scheduled and called asynchronously
a3.onfinish = function() { calls.push('finish'); };
a3.play();
testing.eventually(() => testing.expectEqual(['finish'], calls));
testing.onload(() => testing.expectEqual(['finish'], calls));
</script>
<script id=pause>
@@ -52,7 +52,7 @@
a4.pause();
cb4.push(a4.playState)
});
testing.eventually(() => testing.expectEqual(['running', 'paused'], cb4));
testing.onload(() => testing.expectEqual(['running', 'paused'], cb4));
</script>
<script id=finish>
@@ -65,5 +65,6 @@
cb5.push(a5.playState);
a5.play();
});
testing.eventually(() => testing.expectEqual(['idle', 'finished'], cb5));
testing.onload(() => testing.expectEqual(['idle', 'finished'], cb5));
</script>
-->

View File

@@ -98,6 +98,64 @@
}
</script>
<script id=mime_parsing>
// MIME types are lowercased
{
const blob = new Blob([], { type: "TEXT/HTML" });
testing.expectEqual("text/html", blob.type);
}
{
const blob = new Blob([], { type: "Application/JSON" });
testing.expectEqual("application/json", blob.type);
}
// MIME with parameters - lowercased
{
const blob = new Blob([], { type: "text/html; charset=UTF-8" });
testing.expectEqual("text/html; charset=utf-8", blob.type);
}
// Any ASCII string is accepted and lowercased (no MIME structure validation)
{
const blob = new Blob([], { type: "invalid" });
testing.expectEqual("invalid", blob.type);
}
{
const blob = new Blob([], { type: "/" });
testing.expectEqual("/", blob.type);
}
// Non-ASCII characters cause empty string (chars outside U+0020-U+007E)
{
const blob = new Blob([], { type: "ý/x" });
testing.expectEqual("", blob.type);
}
{
const blob = new Blob([], { type: "text/plàin" });
testing.expectEqual("", blob.type);
}
// Control characters cause empty string
{
const blob = new Blob([], { type: "text/html\x00" });
testing.expectEqual("", blob.type);
}
// Empty type stays empty
{
const blob = new Blob([]);
testing.expectEqual("", blob.type);
}
{
const blob = new Blob([], { type: "" });
testing.expectEqual("", blob.type);
}
</script>
<script id=slice>
{
const parts = ["la", "symphonie", "des", "éclairs"];

View File

@@ -89,6 +89,54 @@
}
</script>
<script id="CanvasRenderingContext2D#getImageData">
{
const element = document.createElement("canvas");
element.width = 100;
element.height = 50;
const ctx = element.getContext("2d");
const imageData = ctx.getImageData(0, 0, 10, 20);
testing.expectEqual(true, imageData instanceof ImageData);
testing.expectEqual(imageData.width, 10);
testing.expectEqual(imageData.height, 20);
testing.expectEqual(imageData.data.length, 10 * 20 * 4);
testing.expectEqual(true, imageData.data instanceof Uint8ClampedArray);
// Undrawn canvas should return transparent black pixels.
testing.expectEqual(imageData.data[0], 0);
testing.expectEqual(imageData.data[1], 0);
testing.expectEqual(imageData.data[2], 0);
testing.expectEqual(imageData.data[3], 0);
}
</script>
<script id="CanvasRenderingContext2D#getImageData invalid">
{
const element = document.createElement("canvas");
const ctx = element.getContext("2d");
// Zero or negative width/height should throw IndexSizeError.
testing.expectError('Index or size', () => ctx.getImageData(0, 0, 0, 10));
testing.expectError('Index or size', () => ctx.getImageData(0, 0, 10, 0));
testing.expectError('Index or size', () => ctx.getImageData(0, 0, -5, 10));
testing.expectError('Index or size', () => ctx.getImageData(0, 0, 10, -5));
}
</script>
<script id="CanvasRenderingContext2D#canvas">
{
const element = document.createElement("canvas");
const ctx = element.getContext("2d");
testing.expectEqual(ctx.canvas, element);
// Setting dimensions via ctx.canvas should update the element.
ctx.canvas.width = 40;
ctx.canvas.height = 25;
testing.expectEqual(element.width, 40);
testing.expectEqual(element.height, 25);
}
</script>
<script id="getter">
{

View File

@@ -62,3 +62,26 @@
testing.expectEqual(offscreen.height, 96);
}
</script>
<script id=OffscreenCanvasRenderingContext2D#getImageData>
{
const canvas = new OffscreenCanvas(100, 50);
const ctx = canvas.getContext("2d");
const imageData = ctx.getImageData(0, 0, 10, 20);
testing.expectEqual(true, imageData instanceof ImageData);
testing.expectEqual(imageData.width, 10);
testing.expectEqual(imageData.height, 20);
testing.expectEqual(imageData.data.length, 10 * 20 * 4);
// Undrawn canvas should return transparent black pixels.
testing.expectEqual(imageData.data[0], 0);
testing.expectEqual(imageData.data[1], 0);
testing.expectEqual(imageData.data[2], 0);
testing.expectEqual(imageData.data[3], 0);
// Zero or negative dimensions should throw.
testing.expectError('Index or size', () => ctx.getImageData(0, 0, 0, 10));
testing.expectError('Index or size', () => ctx.getImageData(0, 0, 10, -5));
}
</script>

View File

@@ -0,0 +1,63 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<script id="constructor_basic">
{
const face = new FontFace("TestFont", "url(test.woff)");
testing.expectTrue(face instanceof FontFace);
}
</script>
<script id="constructor_name">
{
testing.expectEqual('FontFace', FontFace.name);
}
</script>
<script id="family_property">
{
const face = new FontFace("MyFont", "url(font.woff2)");
testing.expectEqual("MyFont", face.family);
}
</script>
<script id="status_is_loaded">
{
const face = new FontFace("F", "url(f.woff)");
testing.expectEqual("loaded", face.status);
}
</script>
<script id="loaded_is_promise">
{
const face = new FontFace("F", "url(f.woff)");
testing.expectTrue(face.loaded instanceof Promise);
}
</script>
<script id="load_returns_promise">
{
const face = new FontFace("F", "url(f.woff)");
testing.expectTrue(face.load() instanceof Promise);
}
</script>
<script id="default_descriptors">
{
const face = new FontFace("F", "url(f.woff)");
testing.expectEqual("normal", face.style);
testing.expectEqual("normal", face.weight);
testing.expectEqual("normal", face.stretch);
testing.expectEqual("normal", face.variant);
testing.expectEqual("normal", face.featureSettings);
testing.expectEqual("auto", face.display);
}
</script>
<script id="document_fonts_add">
{
const face = new FontFace("AddedFont", "url(added.woff)");
const result = document.fonts.add(face);
testing.expectTrue(result === document.fonts);
}
</script>

View File

@@ -56,3 +56,25 @@
testing.expectEqual('FontFaceSet', document.fonts.constructor.name);
}
</script>
<script id="document_fonts_addEventListener">
{
let loading = false;
document.fonts.addEventListener('loading', function() {
loading = true;
});
let loadingdone = false;
document.fonts.addEventListener('loadingdone', function() {
loadingdone = true;
});
document.fonts.load("italic bold 16px Roboto");
testing.onload(() => {
testing.expectEqual(true, loading);
testing.expectEqual(true, loadingdone);
});
testing.expectEqual(true, true);
}
</script>

View File

@@ -275,3 +275,261 @@
testing.expectEqual('red', div.style.getPropertyValue('color'));
}
</script>
<script id="CSSStyleDeclaration_normalize_zero_to_0px">
{
// Per CSSOM spec, unitless zero in length properties should serialize as "0px"
const div = document.createElement('div');
div.style.width = '0';
testing.expectEqual('0px', div.style.width);
div.style.margin = '0';
testing.expectEqual('0px', div.style.margin);
div.style.padding = '0';
testing.expectEqual('0px', div.style.padding);
div.style.top = '0';
testing.expectEqual('0px', div.style.top);
// Scroll properties
div.style.scrollMarginTop = '0';
testing.expectEqual('0px', div.style.scrollMarginTop);
div.style.scrollPaddingBottom = '0';
testing.expectEqual('0px', div.style.scrollPaddingBottom);
// Multi-column
div.style.columnWidth = '0';
testing.expectEqual('0px', div.style.columnWidth);
div.style.columnRuleWidth = '0';
testing.expectEqual('0px', div.style.columnRuleWidth);
// Outline shorthand
div.style.outline = '0';
testing.expectEqual('0px', div.style.outline);
// Shapes
div.style.shapeMargin = '0';
testing.expectEqual('0px', div.style.shapeMargin);
// Non-length properties should not be affected
div.style.opacity = '0';
testing.expectEqual('0', div.style.opacity);
div.style.zIndex = '0';
testing.expectEqual('0', div.style.zIndex);
}
</script>
<script id="CSSStyleDeclaration_normalize_first_baseline">
{
// "first baseline" should serialize canonically as "baseline"
const div = document.createElement('div');
div.style.alignItems = 'first baseline';
testing.expectEqual('baseline', div.style.alignItems);
div.style.alignContent = 'first baseline';
testing.expectEqual('baseline', div.style.alignContent);
div.style.alignSelf = 'first baseline';
testing.expectEqual('baseline', div.style.alignSelf);
div.style.justifySelf = 'first baseline';
testing.expectEqual('baseline', div.style.justifySelf);
// "last baseline" should remain unchanged
div.style.alignItems = 'last baseline';
testing.expectEqual('last baseline', div.style.alignItems);
}
</script>
<script id="CSSStyleDeclaration_normalize_duplicate_values">
{
// For 2-value shorthand properties, "X X" should collapse to "X"
const div = document.createElement('div');
div.style.placeContent = 'center center';
testing.expectEqual('center', div.style.placeContent);
div.style.placeContent = 'start start';
testing.expectEqual('start', div.style.placeContent);
div.style.gap = '10px 10px';
testing.expectEqual('10px', div.style.gap);
// Different values should not collapse
div.style.placeContent = 'center start';
testing.expectEqual('center start', div.style.placeContent);
div.style.gap = '10px 20px';
testing.expectEqual('10px 20px', div.style.gap);
// New shorthands
div.style.overflow = 'hidden hidden';
testing.expectEqual('hidden', div.style.overflow);
div.style.scrollSnapAlign = 'start start';
testing.expectEqual('start', div.style.scrollSnapAlign);
div.style.overscrollBehavior = 'auto auto';
testing.expectEqual('auto', div.style.overscrollBehavior);
}
</script>
<script id="CSSStyleDeclaration_normalize_anchor_size">
{
// anchor-size() should serialize with dashed ident (anchor name) before size keyword
const div = document.createElement('div');
// Already canonical order - should stay the same
div.style.width = 'anchor-size(--foo width)';
testing.expectEqual('anchor-size(--foo width)', div.style.width);
// Non-canonical order - should be reordered
div.style.width = 'anchor-size(width --foo)';
testing.expectEqual('anchor-size(--foo width)', div.style.width);
// With fallback value
div.style.width = 'anchor-size(height --bar, 100px)';
testing.expectEqual('anchor-size(--bar height, 100px)', div.style.width);
// Different size keywords
div.style.width = 'anchor-size(block --baz)';
testing.expectEqual('anchor-size(--baz block)', div.style.width);
div.style.width = 'anchor-size(inline --qux)';
testing.expectEqual('anchor-size(--qux inline)', div.style.width);
div.style.width = 'anchor-size(self-block --test)';
testing.expectEqual('anchor-size(--test self-block)', div.style.width);
div.style.width = 'anchor-size(self-inline --test)';
testing.expectEqual('anchor-size(--test self-inline)', div.style.width);
// Without anchor name (implicit default anchor)
div.style.width = 'anchor-size(width)';
testing.expectEqual('anchor-size(width)', div.style.width);
// Nested anchor-size in fallback
div.style.width = 'anchor-size(width --foo, anchor-size(height --bar))';
testing.expectEqual('anchor-size(--foo width, anchor-size(--bar height))', div.style.width);
}
</script>
<script id="CSSStyleSheet_insertRule_deleteRule">
{
const style = document.createElement('style');
document.head.appendChild(style);
const sheet = style.sheet;
testing.expectEqual(0, sheet.cssRules.length);
sheet.insertRule('.test { color: green; }', 0);
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.test', sheet.cssRules[0].selectorText);
testing.expectEqual('green', sheet.cssRules[0].style.color);
sheet.deleteRule(0);
testing.expectEqual(0, sheet.cssRules.length);
let caught = false;
try {
sheet.deleteRule(5);
} catch (e) {
caught = true;
testing.expectEqual('IndexSizeError', e.name);
}
testing.expectTrue(caught);
}
</script>
<script id="CSSStyleSheet_insertRule_default_index">
{
const style = document.createElement('style');
document.head.appendChild(style);
const sheet = style.sheet;
testing.expectEqual(0, sheet.cssRules.length);
// Call without index, should default to 0
sheet.insertRule('.test-default { color: blue; }');
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.test-default', sheet.cssRules[0].selectorText);
// Insert another rule without index, should default to 0 and push the first one to index 1
sheet.insertRule('.test-at-0 { color: red; }');
testing.expectEqual(2, sheet.cssRules.length);
testing.expectEqual('.test-at-0', sheet.cssRules[0].selectorText);
testing.expectEqual('.test-default', sheet.cssRules[1].selectorText);
}
</script>
<script id="CSSStyleSheet_insertRule_semicolon">
{
const style = document.createElement('style');
document.head.appendChild(style);
const sheet = style.sheet;
// Should not throw even with trailing semicolon
sheet.insertRule('*{};');
testing.expectEqual(1, sheet.cssRules.length);
}
</script>
<script id="CSSStyleSheet_insertRule_multiple_rules">
{
const style = document.createElement('style');
document.head.appendChild(style);
const sheet = style.sheet;
let caught = false;
try {
sheet.insertRule('a { color: red; } b { color: blue; }');
} catch (e) {
caught = true;
testing.expectEqual('SyntaxError', e.name);
}
testing.expectTrue(caught);
testing.expectEqual(0, sheet.cssRules.length);
}
</script>
<script id="CSSStyleSheet_replaceSync">
{
const sheet = new CSSStyleSheet();
testing.expectEqual(0, sheet.cssRules.length);
sheet.replaceSync('.test { color: blue; }');
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.test', sheet.cssRules[0].selectorText);
testing.expectEqual('blue', sheet.cssRules[0].style.color);
let replacedAsync = false;
testing.async(async () => {
const result = await sheet.replace('.async-test { margin: 10px; }');
testing.expectTrue(result === sheet);
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.async-test', sheet.cssRules[0].selectorText);
replacedAsync = true;
});
testing.onload(() => testing.expectTrue(replacedAsync));
}
</script>
<script id="CSSStyleRule_cssText">
{
const sheet = new CSSStyleSheet();
sheet.replaceSync('.test { color: red; margin: 10px; }');
// Check serialization format
const cssText = sheet.cssRules[0].cssText;
testing.expectTrue(cssText.includes('.test { '));
testing.expectTrue(cssText.includes('color: red;'));
testing.expectTrue(cssText.includes('margin: 10px;'));
testing.expectTrue(cssText.includes('}'));
}
</script>

View File

@@ -72,3 +72,59 @@
testing.expectEqual(2, calls);
}
</script>
<div id=fragment_clone_container></div>
<script id=clone_fragment>
{
let calls = 0;
class MyFragmentCloneElement extends HTMLElement {
constructor() {
super();
calls += 1;
$('#fragment_clone_container').appendChild(this);
}
}
customElements.define('my-fragment-clone-element', MyFragmentCloneElement);
// Create a DocumentFragment with a custom element
const fragment = document.createDocumentFragment();
const customEl = document.createElement('my-fragment-clone-element');
fragment.appendChild(customEl);
// Clone the fragment - this should trigger the crash
// because the constructor will attach the element during cloning
const clonedFragment = fragment.cloneNode(true);
testing.expectEqual(2, calls);
}
</script>
<div id=range_clone_container></div>
<script id=clone_range>
{
let calls = 0;
class MyRangeCloneElement extends HTMLElement {
constructor() {
super();
calls += 1;
$('#range_clone_container').appendChild(this);
}
}
customElements.define('my-range-clone-element', MyRangeCloneElement);
// Create a container with a custom element
const container = document.createElement('div');
const customEl = document.createElement('my-range-clone-element');
container.appendChild(customEl);
// Create a range that includes the custom element
const range = document.createRange();
range.selectNodeContents(container);
// Clone the range contents - this should trigger the crash
// because the constructor will attach the element during cloning
const clonedContents = range.cloneContents();
testing.expectEqual(2, calls);
}
</script>

View File

@@ -0,0 +1,66 @@
<!DOCTYPE html>
<head>
<script src="../testing.js"></script>
<script>
// Test that document.open/write/close throw InvalidStateError during custom element
// reactions when the element is parsed from HTML
window.constructorOpenException = null;
window.constructorWriteException = null;
window.constructorCloseException = null;
window.constructorCalled = false;
class ThrowTestElement extends HTMLElement {
constructor() {
super();
window.constructorCalled = true;
// Try document.open on the same document during constructor - should throw
try {
document.open();
} catch (e) {
window.constructorOpenException = e;
}
// Try document.write on the same document during constructor - should throw
try {
document.write('<b>test</b>');
} catch (e) {
window.constructorWriteException = e;
}
// Try document.close on the same document during constructor - should throw
try {
document.close();
} catch (e) {
window.constructorCloseException = e;
}
}
}
customElements.define('throw-test-element', ThrowTestElement);
</script>
</head>
<body>
<!-- This element will be parsed from HTML, triggering the constructor -->
<throw-test-element id="test-element"></throw-test-element>
<script id="verify_throws">
{
// Verify the constructor was called
testing.expectEqual(true, window.constructorCalled);
// Verify document.open threw InvalidStateError
testing.expectEqual(true, window.constructorOpenException !== null);
testing.expectEqual('InvalidStateError', window.constructorOpenException.name);
// Verify document.write threw InvalidStateError
testing.expectEqual(true, window.constructorWriteException !== null);
testing.expectEqual('InvalidStateError', window.constructorWriteException.name);
// Verify document.close threw InvalidStateError
testing.expectEqual(true, window.constructorCloseException !== null);
testing.expectEqual('InvalidStateError', window.constructorCloseException.name);
}
</script>
</body>

View File

@@ -24,11 +24,10 @@
<script id=byId name="test1">
testing.expectEqual(1, document.querySelector.length);
testing.expectError("SyntaxError: Syntax Error", () => document.querySelector(''));
testing.expectError("SyntaxError", () => document.querySelector(''));
testing.withError((err) => {
testing.expectEqual(12, err.code);
testing.expectEqual("SyntaxError", err.name);
testing.expectEqual("Syntax Error", err.message);
}, () => document.querySelector(''));
testing.expectEqual('test1', document.querySelector('#byId').getAttribute('name'));

View File

@@ -34,11 +34,10 @@
</script>
<script id=script1 name="test1">
testing.expectError("SyntaxError: Syntax Error", () => document.querySelectorAll(''));
testing.expectError("SyntaxError", () => document.querySelectorAll(''));
testing.withError((err) => {
testing.expectEqual(12, err.code);
testing.expectEqual("SyntaxError", err.name);
testing.expectEqual("Syntax Error", err.message);
}, () => document.querySelectorAll(''));
</script>

View File

@@ -342,3 +342,4 @@
testing.expectEqual('html', doc.lastChild.nodeName);
}
</script>

View File

@@ -131,7 +131,7 @@
document.open();
}, 5);
testing.eventually(() => {
testing.onload(() => {
// The element should be gone now
const afterOpen = document.getElementById('will_be_removed');
testing.expectEqual(null, afterOpen);

View File

@@ -127,7 +127,7 @@
testing.withError((err) => {
testing.expectEqual(3, err.code);
testing.expectEqual('Hierarchy Error', err.message);
testing.expectEqual('HierarchyRequestError', err.name);
testing.expectEqual(true, err instanceof DOMException);
testing.expectEqual(true, err instanceof Error);
}, () => link.appendChild(content));

View File

@@ -3,11 +3,19 @@
<body></body>
<script id=basic>
{
{
const parser = new DOMParser();
testing.expectEqual('object', typeof parser);
testing.expectEqual('function', typeof parser.parseFromString);
}
{
const parser = new DOMParser();
let d = parser.parseFromString('', 'text/xml');
testing.expectEqual('<parsererror>error</parsererror>', new XMLSerializer().serializeToString(d));
}
}
</script>
<script id=parseSimpleHTML>
@@ -389,3 +397,25 @@
}
}
</script>
<script id=getElementsByTagName-xml>
{
const parser = new DOMParser();
const doc = parser.parseFromString('<layout><row><col>A</col><col>B</col></row></layout>', 'text/xml');
// Test getElementsByTagName on document
const rows = doc.getElementsByTagName('row');
testing.expectEqual(1, rows.length);
// Test getElementsByTagName on element
const row = rows[0];
const cols = row.getElementsByTagName('col');
testing.expectEqual(2, cols.length);
testing.expectEqual('A', cols[0].textContent);
testing.expectEqual('B', cols[1].textContent);
// Test getElementsByTagName('*') on element
const allElements = row.getElementsByTagName('*');
testing.expectEqual(2, allElements.length);
}
</script>

View File

@@ -36,7 +36,6 @@
testing.withError((err) => {
testing.expectEqual(8, err.code);
testing.expectEqual("NotFoundError", err.name);
testing.expectEqual("Not Found", err.message);
}, () => el1.removeAttributeNode(script_id_node));
testing.expectEqual(an1, el1.removeAttributeNode(an1));

View File

@@ -0,0 +1,226 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<body></body>
<!-
<script id="inline_display_none">
{
const el = document.createElement("div");
document.body.appendChild(el);
testing.expectEqual(true, el.checkVisibility());
el.style.display = "none";
testing.expectEqual(false, el.checkVisibility());
el.style.display = "block";
testing.expectEqual(true, el.checkVisibility());
el.remove();
}
</script>
<script id="inline_visibility_hidden">
{
const el = document.createElement("div");
document.body.appendChild(el);
el.style.visibility = "hidden";
// Without visibilityProperty option, visibility:hidden is not checked
testing.expectEqual(true, el.checkVisibility());
// With visibilityProperty: true, visibility:hidden is detected
testing.expectEqual(false, el.checkVisibility({ visibilityProperty: true }));
el.style.visibility = "collapse";
testing.expectEqual(false, el.checkVisibility({ visibilityProperty: true }));
el.style.visibility = "visible";
testing.expectEqual(true, el.checkVisibility({ visibilityProperty: true }));
el.remove();
}
</script>
<script id="inline_opacity_zero">
{
const el = document.createElement("div");
document.body.appendChild(el);
el.style.opacity = "0";
// Without checkOpacity option, opacity:0 is not checked
testing.expectEqual(true, el.checkVisibility());
// With checkOpacity: true, opacity:0 is detected
testing.expectEqual(false, el.checkVisibility({ checkOpacity: true }));
el.style.opacity = "0.5";
testing.expectEqual(true, el.checkVisibility({ checkOpacity: true }));
el.style.opacity = "1";
testing.expectEqual(true, el.checkVisibility({ checkOpacity: true }));
el.remove();
}
</script>
<script id="parent_hidden_hides_child">
{
const parent = document.createElement("div");
const child = document.createElement("span");
parent.appendChild(child);
document.body.appendChild(parent);
testing.expectEqual(true, child.checkVisibility());
// display:none on parent hides children (no option needed)
parent.style.display = "none";
testing.expectEqual(false, child.checkVisibility());
// visibility:hidden on parent - needs visibilityProperty option
parent.style.display = "block";
parent.style.visibility = "hidden";
testing.expectEqual(true, child.checkVisibility()); // without option
testing.expectEqual(false, child.checkVisibility({ visibilityProperty: true }));
// opacity:0 on parent - needs checkOpacity option
parent.style.visibility = "visible";
parent.style.opacity = "0";
testing.expectEqual(true, child.checkVisibility()); // without option
testing.expectEqual(false, child.checkVisibility({ checkOpacity: true }));
parent.remove();
}
</script>
<style id="style-basic">
.hidden-by-class { display: none; }
.visible-by-class { display: block; }
</style>
<script id="style_tag_basic">
{
const el = document.createElement("div");
document.body.appendChild(el);
testing.expectEqual(true, el.checkVisibility());
el.className = "hidden-by-class";
testing.expectEqual(false, el.checkVisibility());
el.className = "visible-by-class";
testing.expectEqual(true, el.checkVisibility());
el.className = "";
el.remove();
}
</script>
<style id="style-specificity">
.spec-hidden { display: none; }
#spec-visible { display: block; }
</style>
<script id="specificity_id_beats_class">
{
const el = document.createElement("div");
el.id = "spec-visible";
el.className = "spec-hidden";
document.body.appendChild(el);
// ID selector (#spec-visible: display:block) should beat class selector (.spec-hidden: display:none)
testing.expectEqual(true, el.checkVisibility());
el.remove();
}
</script>
<style id="style-order-1">
.order-test { display: none; }
</style>
<style id="style-order-2">
.order-test { display: block; }
</style>
<script id="rule_order_later_wins">
{
const el = document.createElement("div");
el.className = "order-test";
document.body.appendChild(el);
// Second style block should win (display: block)
testing.expectEqual(true, el.checkVisibility());
el.remove();
}
</script>
<style id="style-override">
.should-be-hidden { display: none; }
</style>
<script id="inline_overrides_stylesheet">
{
const el = document.createElement("div");
el.className = "should-be-hidden";
document.body.appendChild(el);
testing.expectEqual(false, el.checkVisibility());
// Inline style should override
el.style.display = "block";
testing.expectEqual(true, el.checkVisibility());
el.remove();
}
</script>
<script id="dynamic_style_element">
{
const el = document.createElement("div");
el.className = "dynamic-style-test";
document.body.appendChild(el);
testing.expectEqual(true, el.checkVisibility());
// Add a style element
const style = document.createElement("style");
style.textContent = ".dynamic-style-test { display: none; }";
document.head.appendChild(style);
testing.expectEqual(false, el.checkVisibility());
// Remove the style element
style.remove();
testing.expectEqual(true, el.checkVisibility());
el.remove();
}
</script>
<script id="deep_nesting">
{
const levels = 5;
let current = document.body;
const elements = [];
for (let i = 0; i < levels; i++) {
const el = document.createElement("div");
current.appendChild(el);
elements.push(el);
current = el;
}
// All should be visible
for (let i = 0; i < levels; i++) {
testing.expectEqual(true, elements[i].checkVisibility());
}
// Hide middle element
elements[2].style.display = "none";
// Elements 0, 1 should still be visible
testing.expectEqual(true, elements[0].checkVisibility());
testing.expectEqual(true, elements[1].checkVisibility());
// Elements 2, 3, 4 should be hidden
testing.expectEqual(false, elements[2].checkVisibility());
testing.expectEqual(false, elements[3].checkVisibility());
testing.expectEqual(false, elements[4].checkVisibility());
elements[0].remove();
}
</script>

View File

@@ -12,7 +12,7 @@
testing.expectEqual('', $('#a0').href);
testing.expectEqual(testing.BASE_URL + 'element/anchor1.html', $('#a1').href);
testing.expectEqual(testing.ORIGIN + 'hello/world/anchor2.html', $('#a2').href);
testing.expectEqual(testing.ORIGIN + '/hello/world/anchor2.html', $('#a2').href);
testing.expectEqual('https://www.openmymind.net/Elixirs-With-Statement/', $('#a3').href);
testing.expectEqual(testing.BASE_URL + 'element/html/foo', $('#link').href);

View File

@@ -532,6 +532,6 @@
testing.expectEqual(true, result);
});
testing.eventually(() => testing.expectEqual(true, asyncBlockDispatched));
testing.onload(() => testing.expectEqual(true, asyncBlockDispatched));
}
</script>

View File

@@ -23,6 +23,22 @@
}
</script>
<script id="action">
{
const form = document.createElement('form')
testing.expectEqual(testing.BASE_URL + 'element/html/form.html', form.action)
form.action = 'hello';
testing.expectEqual(testing.BASE_URL + 'element/html/hello', form.action)
form.action = '/hello';
testing.expectEqual(testing.ORIGIN + '/hello', form.action)
form.action = 'https://lightpanda.io/hello';
testing.expectEqual('https://lightpanda.io/hello', form.action)
}
</script>
<!-- Test fixtures for form.method -->
<form id="form_get" method="get"></form>
<form id="form_post" method="post"></form>
@@ -327,3 +343,164 @@
testing.expectEqual('', form.elements['choice'].value)
}
</script>
<!-- Test: requestSubmit() fires the submit event (unlike submit()) -->
<form id="test_form2" action="/should-not-navigate2" method="get">
<input name="q" value="test2">
</form>
<script id="requestSubmit_fires_submit_event">
{
const form = $('#test_form2');
let submitFired = false;
form.addEventListener('submit', (e) => {
e.preventDefault();
submitFired = true;
});
form.requestSubmit();
testing.expectEqual(true, submitFired);
}
</script>
<!-- Test: requestSubmit() with preventDefault stops navigation -->
<form id="test_form3" action="/should-not-navigate3" method="get">
<input name="q" value="test3">
</form>
<script id="requestSubmit_respects_preventDefault">
{
const form = $('#test_form3');
form.addEventListener('submit', (e) => {
e.preventDefault();
});
form.requestSubmit();
// Form submission was prevented, so no navigation should be scheduled
testing.expectEqual(true, true);
}
</script>
<!-- Test: requestSubmit() with non-submit-button submitter throws TypeError -->
<form id="test_form_rs1" action="/should-not-navigate4" method="get">
<input id="rs1_text" type="text" name="q" value="test">
<input id="rs1_submit" type="submit" value="Go">
<input id="rs1_image" type="image" src="x.png">
<button id="rs1_btn_submit" type="submit">Submit</button>
<button id="rs1_btn_reset" type="reset">Reset</button>
<button id="rs1_btn_button" type="button">Button</button>
</form>
<script id="requestSubmit_rejects_non_submit_button">
{
const form = $('#test_form_rs1');
form.addEventListener('submit', (e) => e.preventDefault());
// A text input is not a submit button — should throw TypeError
testing.expectError('TypeError', () => {
form.requestSubmit($('#rs1_text'));
});
// A reset button is not a submit button — should throw TypeError
testing.expectError('TypeError', () => {
form.requestSubmit($('#rs1_btn_reset'));
});
// A <button type="button"> is not a submit button — should throw TypeError
testing.expectError('TypeError', () => {
form.requestSubmit($('#rs1_btn_button'));
});
// A <div> is not a submit button — should throw TypeError
const div = document.createElement('div');
form.appendChild(div);
testing.expectError('TypeError', () => {
form.requestSubmit(div);
});
}
</script>
<!-- Test: requestSubmit() accepts valid submit buttons -->
<script id="requestSubmit_accepts_submit_buttons">
{
const form = $('#test_form_rs1');
let submitCount = 0;
form.addEventListener('submit', (e) => { e.preventDefault(); submitCount++; });
// <input type="submit"> is a valid submitter
form.requestSubmit($('#rs1_submit'));
testing.expectEqual(1, submitCount);
// <input type="image"> is a valid submitter
form.requestSubmit($('#rs1_image'));
testing.expectEqual(2, submitCount);
// <button type="submit"> is a valid submitter
form.requestSubmit($('#rs1_btn_submit'));
testing.expectEqual(3, submitCount);
}
</script>
<!-- Test: requestSubmit() with submitter not owned by form throws NotFoundError -->
<form id="test_form_rs2" action="/should-not-navigate5" method="get">
<input type="text" name="q" value="test">
</form>
<form id="test_form_rs3">
<input id="rs3_submit" type="submit" value="Other Submit">
</form>
<script id="requestSubmit_rejects_wrong_form_submitter">
{
const form = $('#test_form_rs2');
// Submit button belongs to a different form — should throw NotFoundError
testing.expectError('NotFoundError', () => {
form.requestSubmit($('#rs3_submit'));
});
}
</script>
<!-- Test: requestSubmit(submitter) sets SubmitEvent.submitter -->
<form id="test_form_submitter" action="/should-not-navigate6" method="get">
<button id="submitter_btn" type="submit">Save</button>
</form>
<script id="requestSubmit_sets_submitter">
{
const form = $('#test_form_submitter');
const btn = $('#submitter_btn');
let capturedSubmitter = undefined;
form.addEventListener('submit', (e) => {
e.preventDefault();
capturedSubmitter = e.submitter;
});
form.requestSubmit(btn);
testing.expectEqual(btn, capturedSubmitter);
}
</script>
<!-- Test: requestSubmit() without submitter sets submitter to the form element -->
<form id="test_form_submitter2" action="/should-not-navigate7" method="get">
<input type="text" name="q" value="test">
</form>
<script id="requestSubmit_default_submitter_is_form">
{
const form = $('#test_form_submitter2');
let capturedSubmitter = undefined;
form.addEventListener('submit', (e) => {
e.preventDefault();
capturedSubmitter = e.submitter;
});
form.requestSubmit();
testing.expectEqual(form, capturedSubmitter);
}
</script>

View File

@@ -29,15 +29,17 @@
testing.expectEqual('', img.src);
testing.expectEqual('', img.alt);
testing.expectEqual('', img.currentSrc);
img.src = 'test.png';
// src property returns resolved absolute URL
testing.expectEqual(testing.BASE_URL + 'element/html/test.png', img.src);
testing.expectEqual(testing.BASE_URL + 'element/html/test.png', img.currentSrc);
// getAttribute returns the raw attribute value
testing.expectEqual('test.png', img.getAttribute('src'));
img.src = '/absolute/path.png';
testing.expectEqual(testing.ORIGIN + 'absolute/path.png', img.src);
testing.expectEqual(testing.ORIGIN + '/absolute/path.png', img.src);
testing.expectEqual('/absolute/path.png', img.getAttribute('src'));
img.src = 'https://example.com/image.png';
@@ -137,7 +139,7 @@
});
});
testing.eventually(() => testing.expectEqual(true, result));
testing.onload(() => testing.expectEqual(true, result));
}
</script>
@@ -148,7 +150,7 @@
const img = document.createElement("img");
img.addEventListener("load", () => { fired = true; });
document.body.appendChild(img);
testing.eventually(() => testing.expectEqual(false, fired));
testing.onload(() => testing.expectEqual(false, fired));
}
</script>
@@ -161,7 +163,7 @@
document.body.appendChild(img);
img.src = "https://cdn.lightpanda.io/website/assets/images/docs/hn.png";
testing.eventually(() => testing.expectEqual(true, result));
testing.onload(() => testing.expectEqual(true, result));
}
</script>

View File

@@ -210,7 +210,7 @@
});
input.setSelectionRange(1, 4);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(5, eventCount);
testing.expectEqual('selectionchange', lastEvent.type);
testing.expectEqual(input, lastEvent.target);
@@ -247,7 +247,7 @@
input.select();
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, eventCount);
testing.expectEqual('select', lastEvent.type);
testing.expectEqual(input, lastEvent.target);

View File

@@ -8,7 +8,7 @@
testing.expectEqual('https://lightpanda.io/opensource-browser/15', l2.href);
l2.href = '/over/9000';
testing.expectEqual(testing.ORIGIN + 'over/9000', l2.href);
testing.expectEqual(testing.ORIGIN + '/over/9000', l2.href);
l2.crossOrigin = 'nope';
testing.expectEqual('anonymous', l2.crossOrigin);
@@ -54,7 +54,7 @@
link.rel = 'stylesheet';
link.addEventListener('load', () => { fired = true; });
document.head.appendChild(link);
testing.eventually(() => testing.expectEqual(false, fired));
testing.onload(() => testing.expectEqual(false, fired));
}
</script>
@@ -66,7 +66,7 @@
link.href = 'https://lightpanda.io/opensource-browser/15';
link.addEventListener('load', () => { fired = true; });
document.head.appendChild(link);
testing.eventually(() => testing.expectEqual(false, fired));
testing.onload(() => testing.expectEqual(false, fired));
}
</script>
@@ -81,6 +81,27 @@
// then set href.
link.href = 'https://lightpanda.io/opensource-browser/15';
testing.eventually(() => testing.expectEqual(true, result));
testing.onload(() => testing.expectEqual(true, result));
}
</script>
<script id="refs">
{
const rels = ['stylesheet', 'preload', 'modulepreload'];
const results = rels.map(() => false);
rels.forEach((rel, i) => {
let link = document.createElement('link')
link.rel = rel;
link.href = '/nope';
link.onload = () => results[i] = true;
document.documentElement.appendChild(link);
});
testing.onload(() => {
results.forEach((r) => {
testing.expectEqual(true, r);
});
});
}
</script>

View File

@@ -236,9 +236,11 @@
{
const audio = document.createElement('audio');
testing.expectEqual('', audio.src);
testing.expectEqual('', audio.currentSrc);
audio.src = 'test.mp3';
testing.expectEqual(testing.BASE_URL + 'element/html/test.mp3', audio.src);
testing.expectEqual(testing.BASE_URL + 'element/html/test.mp3', audio.currentSrc);
}
</script>

View File

@@ -0,0 +1,61 @@
<!DOCTYPE html>
<script src="../../../testing.js"></script>
<script id=force_async>
{
// Dynamically created scripts have async=true by default
let s = document.createElement('script');
testing.expectEqual(true, s.async);
// Setting async=false clears the force async flag and removes attribute
s.async = false;
testing.expectEqual(false, s.async);
testing.expectEqual(false, s.hasAttribute('async'));
// Setting async=true adds the attribute
s.async = true;
testing.expectEqual(true, s.async);
testing.expectEqual(true, s.hasAttribute('async'));
}
</script>
<script></script>
<script id=empty>
{
// Empty parser-inserted script should have async=true (force async retained)
let scripts = document.getElementsByTagName('script');
let emptyScript = scripts[scripts.length - 2];
testing.expectEqual(true, emptyScript.async);
}
</script>
<script id=text_content>
{
let s = document.createElement('script');
s.appendChild(document.createComment('COMMENT'));
s.appendChild(document.createTextNode(' TEXT '));
s.appendChild(document.createProcessingInstruction('P', 'I'));
let a = s.appendChild(document.createElement('a'));
a.appendChild(document.createTextNode('ELEMENT'));
// script.text should return only direct Text node children
testing.expectEqual(' TEXT ', s.text);
// script.textContent should return all descendant text
testing.expectEqual(' TEXT ELEMENT', s.textContent);
}
</script>
<script id=lazy_inline>
{
// Empty script in DOM, then append text - should execute
window.lazyScriptRan = false;
let s = document.createElement('script');
document.head.appendChild(s);
// Script is in DOM but empty, so not yet executed
testing.expectEqual(false, window.lazyScriptRan);
// Append text node with code
s.appendChild(document.createTextNode('window.lazyScriptRan = true;'));
// Now it should have executed
testing.expectEqual(true, window.lazyScriptRan);
}
</script>

View File

@@ -8,14 +8,14 @@
script1.async = false;
script1.src = "dynamic1.js";
document.getElementsByTagName('head')[0].appendChild(script1);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, loaded1);
});
</script>
<script id=no_double_execute>
document.getElementsByTagName('head')[0].appendChild(script1);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, loaded1);
});
</script>
@@ -25,7 +25,7 @@
const script2a = document.createElement('script');
script2a.src = "dynamic2.js";
document.getElementsByTagName('head')[0].appendChild(script2a);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(2, loaded2);
});
</script>
@@ -38,7 +38,7 @@
</script>
<script id=src_after_append>
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(2, loaded2);
});
</script>

View File

@@ -0,0 +1,54 @@
<!DOCTYPE html>
<head></head>
<script src="../../../testing.js"></script>
<script id=textContent_inline>
window.inline_executed = false;
const s1 = document.createElement('script');
s1.textContent = 'window.inline_executed = true;';
document.head.appendChild(s1);
testing.expectTrue(window.inline_executed);
</script>
<script id=text_property_inline>
window.text_executed = false;
const s2 = document.createElement('script');
s2.text = 'window.text_executed = true;';
document.head.appendChild(s2);
testing.expectTrue(window.text_executed);
</script>
<script id=innerHTML_inline>
window.innerHTML_executed = false;
const s3 = document.createElement('script');
s3.innerHTML = 'window.innerHTML_executed = true;';
document.head.appendChild(s3);
testing.expectTrue(window.innerHTML_executed);
</script>
<script id=no_double_execute_inline>
window.inline_counter = 0;
const s4 = document.createElement('script');
s4.textContent = 'window.inline_counter++;';
document.head.appendChild(s4);
document.head.appendChild(s4);
testing.expectEqual(1, window.inline_counter);
</script>
<script id=empty_script_no_execute>
window.empty_ran = false;
const s5 = document.createElement('script');
document.head.appendChild(s5);
testing.expectFalse(window.empty_ran);
</script>
<script id=module_inline>
window.module_executed = false;
const s6 = document.createElement('script');
s6.type = 'module';
s6.textContent = 'window.module_executed = true;';
document.head.appendChild(s6);
testing.onload(() => {
testing.expectTrue(window.module_executed);
});
</script>

View File

@@ -21,7 +21,7 @@
testing.expectEqual(testing.BASE_URL + 'element/html/script/empty.js', s.src);
document.head.appendChild(s);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(true, dom_load);
testing.expectEqual(true, attribute_load);
});

View File

@@ -427,7 +427,7 @@
div.setAttribute('slot', 'content');
host.appendChild(div);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, calls);
});
}
@@ -455,7 +455,7 @@
div.setAttribute('slot', 'other');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, calls);
});
}
@@ -483,7 +483,7 @@
div.remove();
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, calls);
});
}
@@ -511,7 +511,7 @@
div.slot = 'other';
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, calls);
});
}

View File

@@ -128,6 +128,20 @@
});
});
testing.eventually(() => testing.expectEqual(true, result));
testing.onload(() => testing.expectEqual(true, result));
}
</script>
<script id="style-tag-content-parsing">
{
const style = document.createElement("style");
style.textContent = '.content-test { padding: 5px; }';
document.head.appendChild(style);
const sheet = style.sheet;
testing.expectTrue(sheet instanceof CSSStyleSheet);
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.content-test', sheet.cssRules[0].selectorText);
testing.expectEqual('5px', sheet.cssRules[0].style.padding);
}
</script>

View File

@@ -256,7 +256,7 @@
textarea.select();
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(1, eventCount);
testing.expectEqual('select', lastEvent.type);
testing.expectEqual(textarea, lastEvent.target);
@@ -295,7 +295,7 @@
});
textarea.setSelectionRange(1, 4);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(5, eventCount);
testing.expectEqual('selectionchange', lastEvent.type);
testing.expectEqual(textarea, lastEvent.target);

View File

@@ -66,11 +66,10 @@
{
const container = $('#test-container');
testing.expectError("SyntaxError: Syntax Error", () => container.matches(''));
testing.expectError("SyntaxError", () => container.matches(''));
testing.withError((err) => {
testing.expectEqual(12, err.code);
testing.expectEqual("SyntaxError", err.name);
testing.expectEqual("Syntax Error", err.message);
}, () => container.matches(''));
}
</script>

View File

@@ -81,6 +81,17 @@
}
</script>
<script id="is_empty">
{
// Empty :is() and :where() are valid per spec and match nothing
const isEmptyResult = document.querySelectorAll(':is()');
testing.expectEqual(0, isEmptyResult.length);
const whereEmptyResult = document.querySelectorAll(':where()');
testing.expectEqual(0, whereEmptyResult.length);
}
</script>
<div id=escaped class=":popover-open"></div>
<script id="escaped">
{

View File

@@ -12,11 +12,10 @@
const p1 = $('#p1');
testing.expectEqual(null, p1.querySelector('#p1'));
testing.expectError("SyntaxError: Syntax Error", () => p1.querySelector(''));
testing.expectError("SyntaxError", () => p1.querySelector(''));
testing.withError((err) => {
testing.expectEqual(12, err.code);
testing.expectEqual("SyntaxError", err.name);
testing.expectEqual("Syntax Error", err.message);
}, () => p1.querySelector(''));
testing.expectEqual($('#c2'), p1.querySelector('#c2'));

View File

@@ -24,11 +24,10 @@
<script id=errors>
{
const root = $('#root');
testing.expectError("SyntaxError: Syntax Error", () => root.querySelectorAll(''));
testing.expectError("SyntaxError", () => root.querySelectorAll(''));
testing.withError((err) => {
testing.expectEqual(12, err.code);
testing.expectEqual("SyntaxError", err.name);
testing.expectEqual("Syntax Error", err.message);
}, () => root.querySelectorAll(''));
}
</script>

View File

@@ -0,0 +1,139 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<head>
<title>element.replaceChildren Tests</title>
</head>
<body>
<div id="test">Original content</div>
</body>
<script id=error_replace_with_self>
{
// Test that element.replaceChildren(element) throws HierarchyRequestError
const doc = document.implementation.createHTMLDocument("title");
testing.expectError('HierarchyRequest', () => {
doc.body.replaceChildren(doc.body);
});
}
</script>
<script id=error_replace_with_ancestor>
{
// Test that replacing with an ancestor throws HierarchyRequestError
const doc = document.implementation.createHTMLDocument("title");
const child = doc.createElement('div');
doc.body.appendChild(child);
testing.expectError('HierarchyRequest', () => {
child.replaceChildren(doc.body);
});
}
</script>
<script id=replace_children_basic>
{
// Test basic element.replaceChildren
const doc = document.implementation.createHTMLDocument("title");
const child1 = doc.createElement('div');
const child2 = doc.createElement('span');
doc.body.appendChild(child1);
doc.body.replaceChildren(child2);
testing.expectEqual(1, doc.body.childNodes.length);
testing.expectEqual(child2, doc.body.firstChild);
testing.expectEqual(null, child1.parentNode);
}
</script>
<script id=replace_children_empty>
{
// Test element.replaceChildren with no arguments removes all children
const doc = document.implementation.createHTMLDocument("title");
doc.body.appendChild(doc.createElement('div'));
doc.body.appendChild(doc.createElement('span'));
doc.body.replaceChildren();
testing.expectEqual(0, doc.body.childNodes.length);
}
</script>
<script id=replace_children_fragment>
{
// Test element.replaceChildren with DocumentFragment
const doc = document.implementation.createHTMLDocument("title");
const frag = doc.createDocumentFragment();
frag.appendChild(doc.createElement('div'));
frag.appendChild(doc.createElement('span'));
doc.body.replaceChildren(frag);
testing.expectEqual(2, doc.body.childNodes.length);
testing.expectEqual('DIV', doc.body.firstChild.tagName);
testing.expectEqual('SPAN', doc.body.lastChild.tagName);
testing.expectEqual(0, frag.childNodes.length);
}
</script>
<script id=error_fragment_replace_with_self>
{
// Test that replacing with a fragment containing self throws
const doc = document.implementation.createHTMLDocument("title");
const frag = doc.createDocumentFragment();
const child = doc.createElement('div');
frag.appendChild(child);
testing.expectError('HierarchyRequest', () => {
child.replaceChildren(frag);
});
}
</script>
<script id=replace_children_text>
{
// Test element.replaceChildren with text
const doc = document.implementation.createHTMLDocument("title");
doc.body.appendChild(doc.createElement('div'));
doc.body.replaceChildren('Hello', 'World');
testing.expectEqual(2, doc.body.childNodes.length);
testing.expectEqual('Hello', doc.body.firstChild.textContent);
testing.expectEqual('World', doc.body.lastChild.textContent);
}
</script>
<script id=replace_children_mixed>
{
// Test element.replaceChildren with mixed nodes and text
const doc = document.implementation.createHTMLDocument("title");
const span = doc.createElement('span');
span.textContent = 'middle';
doc.body.replaceChildren('start', span, 'end');
testing.expectEqual(3, doc.body.childNodes.length);
testing.expectEqual('start', doc.body.childNodes[0].textContent);
testing.expectEqual('SPAN', doc.body.childNodes[1].tagName);
testing.expectEqual('end', doc.body.childNodes[2].textContent);
}
</script>
<script id=replace_children_reparents>
{
// Test that replaceChildren properly reparents nodes from another parent
const doc = document.implementation.createHTMLDocument("title");
const div1 = doc.createElement('div');
const div2 = doc.createElement('div');
const child = doc.createElement('span');
div1.appendChild(child);
testing.expectEqual(div1, child.parentNode);
div2.replaceChildren(child);
testing.expectEqual(div2, child.parentNode);
testing.expectEqual(0, div1.childNodes.length);
}
</script>

View File

@@ -12,8 +12,6 @@
// Empty functional pseudo-classes should error
testing.expectError("Error: InvalidPseudoClass", () => container.querySelector(':has()'));
testing.expectError("Error: InvalidPseudoClass", () => container.querySelector(':not()'));
testing.expectError("Error: InvalidPseudoClass", () => container.querySelector(':is()'));
testing.expectError("Error: InvalidPseudoClass", () => container.querySelector(':where()'));
testing.expectError("Error: InvalidPseudoClass", () => container.querySelector(':lang()'));
}
</script>
@@ -45,8 +43,8 @@
const container = $('#container');
// Empty selectors
testing.expectError("SyntaxError: Syntax Error", () => container.querySelector(''));
testing.expectError("SyntaxError: Syntax Error", () => document.querySelectorAll(''));
testing.expectError("SyntaxError", () => container.querySelector(''));
testing.expectError("SyntaxError", () => document.querySelectorAll(''));
}
</script>

View File

@@ -242,7 +242,7 @@
<script id=abortsignal_timeout>
var s3 = AbortSignal.timeout(10);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(true, s3.aborted);
testing.expectEqual('TimeoutError', s3.reason);
testing.expectError('Error: TimeoutError', () => {

View File

@@ -61,7 +61,7 @@
window.postMessage('test data', '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual('test data', receivedEvent.data);
testing.expectEqual(window, receivedEvent.source);
testing.expectEqual('message', receivedEvent.type);
@@ -81,7 +81,7 @@
const testObj = { type: 'test', value: 123, nested: { key: 'value' } };
window.postMessage(testObj, '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(testObj, receivedData);
});
}
@@ -111,7 +111,7 @@
window.postMessage(42, '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(42, received);
});
}
@@ -129,7 +129,7 @@
const arr = [1, 2, 3, 'test'];
window.postMessage(arr, '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(arr, received);
});
}
@@ -146,7 +146,7 @@
window.postMessage(null, '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(null, received);
});
}
@@ -163,7 +163,7 @@
window.postMessage('test', '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual('http://127.0.0.1:9582', receivedOrigin);
});
}

View File

@@ -12,7 +12,7 @@
window.postMessage('trigger', '*');
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual(2, count);
});
}

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<script id=onerrorFiveArguments>
let called = false;
let argCount = 0;
window.onerror = function() {
called = true;
argCount = arguments.length;
return true; // suppress default
};
try { undefinedVariable; } catch(e) { window.reportError(e); }
testing.expectEqual(true, called);
testing.expectEqual(5, argCount);
window.onerror = null;
</script>
<script id=onerrorCalledBeforeEventListener>
let callOrder = [];
window.onerror = function() { callOrder.push('onerror'); return true; };
window.addEventListener('error', function() { callOrder.push('listener'); });
try { undefinedVariable; } catch(e) { window.reportError(e); }
testing.expectEqual('onerror', callOrder[0]);
testing.expectEqual('listener', callOrder[1]);
window.onerror = null;
</script>
<script id=onerrorReturnTrueSuppresses>
let listenerCalled = false;
window.onerror = function() { return true; };
window.addEventListener('error', function(e) {
// listener still fires even when onerror returns true
listenerCalled = true;
});
try { undefinedVariable; } catch(e) { window.reportError(e); }
testing.expectEqual(true, listenerCalled);
window.onerror = null;
</script>

View File

@@ -7,54 +7,69 @@
}
</script>
<iframe id=f1 onload="frame1Onload" src="support/sub 1.html"></iframe>
<iframe id=f0></iframe>
<iframe id=f1 onload="frame1Onload()" src="support/sub 1.html"></iframe>
<iframe id=f2 src="support/sub2.html"></iframe>
<script id=empty>
{
const blank = document.createElement('iframe');
testing.expectEqual(null, blank.contentDocument);
document.documentElement.appendChild(blank);
testing.expectEqual('<html><head></head><body></body></html>', blank.contentDocument.documentElement.outerHTML);
const f0 = $('#f0')
testing.expectEqual('<html><head></head><body></body></html>', f0.contentDocument.documentElement.outerHTML);
}
</script>
<script id="basic">
// reload it
$('#f2').src = 'support/sub2.html';
testing.expectEqual(true, true);
testing.eventually(() => {
testing.expectEqual(undefined, window[10]);
testing.expectEqual(window, window[0].top);
testing.expectEqual(window, window[0].parent);
testing.expectEqual(false, window === window[0]);
testing.onload(() => {
testing.expectEqual(undefined, window[20]);
testing.expectEqual(window, window[1].top);
testing.expectEqual(window, window[1].parent);
testing.expectEqual(false, window === window[1]);
testing.expectEqual(false, window[0] === window[1]);
testing.expectEqual(window, window[2].top);
testing.expectEqual(window, window[2].parent);
testing.expectEqual(false, window === window[2]);
testing.expectEqual(false, window[1] === window[2]);
testing.expectEqual(0, $('#f1').childNodes.length);
testing.expectEqual(testing.BASE_URL + 'frames/support/sub%201.html', $('#f1').src);
testing.expectEqual(window[0], $('#f1').contentWindow);
testing.expectEqual(window[1], $('#f2').contentWindow);
testing.expectEqual(window[1], $('#f1').contentWindow);
testing.expectEqual(window[2], $('#f2').contentWindow);
testing.expectEqual(window[0].document, $('#f1').contentDocument);
testing.expectEqual(window[1].document, $('#f2').contentDocument);
testing.expectEqual(window[1].document, $('#f1').contentDocument);
testing.expectEqual(window[2].document, $('#f2').contentDocument);
// sibling frames share the same top
testing.expectEqual(window[0].top, window[1].top);
testing.expectEqual(window[1].top, window[2].top);
// child frames have no sub-frames
testing.expectEqual(0, window[0].length);
testing.expectEqual(0, window[1].length);
testing.expectEqual(0, window[2].length);
// self and window are self-referential on child frames
testing.expectEqual(window[0], window[0].self);
testing.expectEqual(window[0], window[0].window);
testing.expectEqual(window[1], window[1].self);
testing.expectEqual(window[1], window[1].window);
testing.expectEqual(window[2], window[2].self);
// child frame's top.parent is itself (root has no parent)
testing.expectEqual(window, window[0].top.parent);
// Todo: Context security tokens
// testing.expectEqual(true, window.sub1_loaded);
// testing.expectEqual(true, window.sub2_loaded);
// testing.expectEqual(1, window.sub1_count);
// testing.expectEqual(2, window.sub2_count);
// Cross-frame property access
testing.expectEqual(true, window.sub1_loaded);
testing.expectEqual(true, window.sub2_loaded);
testing.expectEqual(1, window.sub1_count);
// depends on how far the initial load got before it was cancelled.
testing.expectEqual(true, window.sub2_count == 1 || window.sub2_count == 2);
});
</script>
@@ -62,33 +77,82 @@
{
let f3_load_event = false;
let f3 = document.createElement('iframe');
f3.id = 'f3';
f3.addEventListener('load', () => {
f3_load_event = true;
});
f3.src = 'invalid'; // still fires load!
document.documentElement.appendChild(f3);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual('f1_onload_loaded', window.f1_onload);
testing.expectEqual(true, f3_load_event);
});
}
</script>
<script id=onload>
<script id=about_blank>
{
let f4 = document.createElement('iframe');
f4.id = 'f4';
f4.src = "about:blank";
document.documentElement.appendChild(f4);
testing.eventually(() => {
testing.onload(() => {
testing.expectEqual("<html><head></head><body></body></html>", f4.contentDocument.documentElement.outerHTML);
});
}
</script>
<script id=count>
testing.eventually(() => {
testing.expectEqual(4, window.length);
<script id=about_blank_renavigate>
{
let f5 = document.createElement('iframe');
f5.id = 'f5';
f5.src = "support/page.html";
document.documentElement.appendChild(f5);
f5.src = "about:blank";
testing.onload(() => {
testing.expectEqual("<html><head></head><body></body></html>", f5.contentDocument.documentElement.outerHTML);
});
}
</script>
<script id=link_click type=module>
const state = await testing.async();
let count = 0;
let f6 = document.createElement('iframe');
f6.id = 'f6';
f6.addEventListener('load', () => {
if (++count == 2) {
state.resolve();
return;
}
f6.contentDocument.querySelector('#link').click();
});
f6.src = 'support/with_link.html';
document.documentElement.appendChild(f6);
await state.done(() => {
testing.expectEqual("<html><head></head><body>It was clicked!\n</body></html>", f6.contentDocument.documentElement.outerHTML);
});
</script>
<script id=about_blank_nav>
{
let i = document.createElement('iframe');
document.documentElement.appendChild(i);
i.contentWindow.location.href = 'support/page.html';
testing.onload(() => {
testing.expectEqual('<html><head></head><body>a-page\n</body></html>', i.contentDocument.documentElement.outerHTML);
});
}
</script>
<script id=count>
testing.onload(() => {
testing.expectEqual(9, window.length);
});
</script>

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<iframe id="receiver"></iframe>
<script id="messages">
{
let reply = null;
window.addEventListener('message', (e) => {
reply = e.data;
});
const iframe = $('#receiver');
iframe.src = 'support/message_receiver.html';
iframe.addEventListener('load', () => {
iframe.contentWindow.postMessage('ping', '*');
});
testing.onload(() => {
testing.expectEqual('pong', reply.data);
testing.expectEqual(testing.ORIGIN, reply.origin);
});
}
</script>

View File

@@ -0,0 +1,2 @@
<!DOCTYPE html>
It was clicked!

Some files were not shown because too many files have changed in this diff Show More