mirror of
https://github.com/lexmount/moli.git
synced 2026-11-20 16:03:35 +00:00
fix(cdp): enforce DOM node publication across commands
Separate backend identity, session frontend bindings, and publication. Publish missing Inspector paths before returning usable node IDs, validate CDP frontend inputs, and retain explicit internal automation bindings. Preserve expanded and associated subtrees during refresh and mutation resync, including shallow template fragments. Cover cross-command behavior and message ordering with a raw CDP client mirror, Chromium comparisons, and real chromedp queries.
This commit is contained in:
@@ -438,6 +438,15 @@ jobs:
|
||||
export MOLI_BIN="$PWD/target/ci-bin/moli-release-head/moli"
|
||||
uv run --project moli-cdp-smoke --no-sync moli-cdp-smoke \
|
||||
--browser moli --strict-full-suite --repeat 2 --jobs 4 --output-dir target/smoke/cdp-ci
|
||||
- uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
|
||||
with:
|
||||
go-version-file: moli-cdp-smoke/chromedp/go.mod
|
||||
cache-dependency-path: moli-cdp-smoke/chromedp/go.sum
|
||||
- name: Run chromedp DOM search regression
|
||||
working-directory: moli-cdp-smoke/chromedp
|
||||
env:
|
||||
MOLI_BIN: ${{ github.workspace }}/target/ci-bin/moli-release-head/moli
|
||||
run: go test -mod=readonly -count=1 ./...
|
||||
- name: Require full Chromium smoke
|
||||
if: ${{ always() }}
|
||||
env:
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
package chromedp_smoke
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/chromedp/cdproto/cdp"
|
||||
"github.com/chromedp/cdproto/dom"
|
||||
"github.com/chromedp/cdproto/runtime"
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
func startMoli(t *testing.T, ctx context.Context) string {
|
||||
t.Helper()
|
||||
binary := os.Getenv("MOLI_BIN")
|
||||
if binary == "" {
|
||||
binary = "../../target/debug/moli"
|
||||
}
|
||||
binary, err := filepath.Abs(binary)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := listener.Addr().(*net.TCPAddr)
|
||||
if err := listener.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var logs bytes.Buffer
|
||||
cmd := exec.Command(binary, "serve", "--host", "127.0.0.1", "--port", fmt.Sprint(address.Port))
|
||||
cmd.Stdout, cmd.Stderr = &logs, &logs
|
||||
cmd.Env = append(os.Environ(), "HTTP_PROXY=", "HTTPS_PROXY=", "ALL_PROXY=", "http_proxy=", "https_proxy=", "all_proxy=")
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = cmd.Process.Kill()
|
||||
_ = cmd.Wait()
|
||||
if t.Failed() {
|
||||
t.Logf("Moli output:\n%s", logs.String())
|
||||
}
|
||||
})
|
||||
client := &http.Client{Transport: &http.Transport{Proxy: nil}, Timeout: time.Second}
|
||||
t.Cleanup(client.CloseIdleConnections)
|
||||
ticker := time.NewTicker(25 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+address.String()+"/json/version", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
if err == nil {
|
||||
var version struct {
|
||||
WebSocketURL string `json:"webSocketDebuggerUrl"`
|
||||
}
|
||||
err = json.NewDecoder(response.Body).Decode(&version)
|
||||
response.Body.Close()
|
||||
if err == nil && version.WebSocketURL != "" {
|
||||
return version.WebSocketURL
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Fatalf("Moli discovery: %v", ctx.Err())
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDOMSearchPublishesNodePaths(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
endpoint := startMoli(t, ctx)
|
||||
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, "<!doctype html><html><body><main><section><ul id='links'>")
|
||||
if r.URL.Path == "/static" {
|
||||
for i := range 12 {
|
||||
fmt.Fprintf(w, "<li><a href='/next/%d'>link %d</a></li>", i, i)
|
||||
}
|
||||
}
|
||||
fmt.Fprint(w, "</ul></section></main>")
|
||||
if r.URL.Path == "/trees" {
|
||||
fmt.Fprint(w, `<main><section><a href="/light">light</a></section></main>
|
||||
<div id="host"></div><iframe srcdoc="<main><a href='/frame'>frame</a></main>"></iframe>
|
||||
<script>document.getElementById('host').attachShadow({mode:'closed'}).innerHTML='<section><a href="/shadow">shadow</a></section>';</script>`)
|
||||
}
|
||||
if r.URL.Path == "/script" {
|
||||
fmt.Fprint(w, `<script>
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const li = document.createElement('li');
|
||||
const a = document.createElement('a');
|
||||
a.href = '/next/' + i;
|
||||
a.textContent = 'link ' + i;
|
||||
li.append(a);
|
||||
document.getElementById('links').append(li);
|
||||
}
|
||||
|
||||
</script>`)
|
||||
}
|
||||
fmt.Fprint(w, "</body></html>")
|
||||
}))
|
||||
defer fixture.Close()
|
||||
allocator, cancelAllocator := chromedp.NewRemoteAllocator(ctx, endpoint, chromedp.NoModifyURL)
|
||||
defer cancelAllocator()
|
||||
browser, cancelBrowser := chromedp.NewContext(allocator, chromedp.WithDebugf(t.Logf))
|
||||
defer cancelBrowser()
|
||||
var links []string
|
||||
for i := range 12 {
|
||||
links = append(links, fmt.Sprintf("/next/%d", i))
|
||||
}
|
||||
for _, fixtureCase := range []struct {
|
||||
path string
|
||||
hrefs []string
|
||||
}{
|
||||
{"/static", links},
|
||||
{"/script", links},
|
||||
{"/trees", []string{"/light", "/shadow", "/frame"}},
|
||||
} {
|
||||
t.Run(fixtureCase.path, func(t *testing.T) {
|
||||
if err := chromedp.Run(browser, chromedp.Navigate(fixture.URL+fixtureCase.path)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for range 2 {
|
||||
var nodes []*cdp.Node
|
||||
// The default BySearch / NodeReady path uses the frontend node cache.
|
||||
// Do not replace this with ByJSPath or Runtime.evaluate.
|
||||
if err := chromedp.Run(browser, chromedp.Nodes("a[href]", &nodes, chromedp.AtLeast(1))); err != nil {
|
||||
t.Fatalf("get links: %v", err)
|
||||
}
|
||||
if len(nodes) != len(fixtureCase.hrefs) {
|
||||
t.Fatalf("got %d links, want %d", len(nodes), len(fixtureCase.hrefs))
|
||||
}
|
||||
for i, node := range nodes {
|
||||
if got, want := node.AttributeValue("href"), fixtureCase.hrefs[i]; got != want {
|
||||
t.Fatalf("link %d: href=%q, want %q", i, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDOMSearchPublicationAcrossCommandsAndDynamicTrees(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
endpoint := startMoli(t, ctx)
|
||||
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, `<!doctype html><html><body>
|
||||
<main><section><a id="target" href="/next">link</a></section></main>
|
||||
<div id="host"></div><iframe id="frame" srcdoc="<p>original</p>"></iframe>
|
||||
</body></html>`)
|
||||
}))
|
||||
defer fixture.Close()
|
||||
allocator, cancelAllocator := chromedp.NewRemoteAllocator(ctx, endpoint, chromedp.NoModifyURL)
|
||||
defer cancelAllocator()
|
||||
for _, test := range []struct {
|
||||
name, expression, selector, command string
|
||||
}{
|
||||
{"describe-target", `document.getElementById('target')`, "#target", "describe"},
|
||||
{"describe-ancestor", `document.querySelector('section')`, "#target", "describe"},
|
||||
{"request-target", `document.getElementById('target')`, "#target", "request"},
|
||||
{"request-ancestor", `document.querySelector('section')`, "#target", "request"},
|
||||
{"new-shadow-root", `document.getElementById('host').attachShadow({mode:'closed'}).innerHTML='<section><a id=shadow-hit href=/shadow>shadow</a></section>'`, "#shadow-hit", "evaluate"},
|
||||
{"replacement-frame-document", `new Promise(resolve => {const f=document.getElementById('frame');f.onload=()=>resolve(true);f.srcdoc='<main><a id=frame-hit href=/frame>replacement</a></main>';})`, "#frame-hit", "evaluate"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
browser, cancelBrowser := chromedp.NewContext(allocator, chromedp.WithDebugf(t.Logf))
|
||||
defer cancelBrowser()
|
||||
query, cancelQuery := context.WithTimeout(browser, 10*time.Second)
|
||||
defer cancelQuery()
|
||||
var owners []*cdp.Node
|
||||
if err := chromedp.Run(query,
|
||||
chromedp.Navigate(fixture.URL),
|
||||
// Publish the owners before changing their associated trees.
|
||||
chromedp.Nodes("#host, #frame", &owners, chromedp.ByQueryAll, chromedp.AtLeast(2)),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
object, exception, err := runtime.Evaluate(test.expression).WithAwaitPromise(true).Do(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exception != nil {
|
||||
return fmt.Errorf("evaluate: %v", exception)
|
||||
}
|
||||
switch test.command {
|
||||
case "describe":
|
||||
_, err = dom.DescribeNode().WithObjectID(object.ObjectID).Do(ctx)
|
||||
case "request":
|
||||
_, err = dom.RequestNode(object.ObjectID).Do(ctx)
|
||||
}
|
||||
return err
|
||||
}),
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for range 2 {
|
||||
var nodes []*cdp.Node
|
||||
if err := chromedp.Run(query, chromedp.Nodes(test.selector, &nodes)); err != nil {
|
||||
t.Fatalf("BySearch / NodeReady: %v", err)
|
||||
}
|
||||
if len(nodes) != 1 || nodes[0].AttributeValue("href") == "" {
|
||||
t.Fatalf("expected one usable link in the frontend cache, got %v", nodes)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
module github.com/lexmount/moli/moli-cdp-smoke/chromedp
|
||||
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
github.com/chromedp/cdproto v0.0.0-20260427013145-5737772c319b
|
||||
github.com/chromedp/chromedp v0.15.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/chromedp/sysutil v1.1.0 // indirect
|
||||
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
github.com/chromedp/cdproto v0.0.0-20260427013145-5737772c319b h1:fpvdcCAe2z3H8OvVY00iKOp3Wapbs/Gy375Fn6l/XM4=
|
||||
github.com/chromedp/cdproto v0.0.0-20260427013145-5737772c319b/go.mod h1:cbyjALe67vDvlvdiG9369P8w5U2w6IshwtyD2f2Tvag=
|
||||
github.com/chromedp/chromedp v0.15.1 h1:EJWiPm7BNqDqjYy6U0lTSL5wNH+iNt9GjC3a4gfjNyQ=
|
||||
github.com/chromedp/chromedp v0.15.1/go.mod h1:CdTHtUqD/dqaFw/cvFWtTydoEQS44wLBuwbMR9EkOY4=
|
||||
github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM=
|
||||
github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8=
|
||||
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVwYFP/L05h5TKQxvkXoKxNvTpjxYKdF1Nlwuao=
|
||||
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
|
||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
||||
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
|
||||
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
||||
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=
|
||||
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
@@ -0,0 +1,619 @@
|
||||
"""Validate the client's DOM mirror in WebSocket order, before each reply."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import json
|
||||
import sys
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
from ..assertions import SmokeError, assert_equal, record
|
||||
from ..navigation import command, navigate, receive_response
|
||||
from ..raw_cdp import RawCdpClient, connect_raw_cdp
|
||||
|
||||
|
||||
def _descendants(node: dict[str, Any]):
|
||||
for relation in ("children", "shadowRoots", "pseudoElements"):
|
||||
for child in node.get(relation, []):
|
||||
yield relation, child
|
||||
for relation in ("contentDocument", "templateContent"):
|
||||
if isinstance(node.get(relation), dict):
|
||||
yield relation, node[relation]
|
||||
|
||||
|
||||
@dataclass
|
||||
class _DomMirror:
|
||||
nodes: dict[int, dict[str, Any]] = field(default_factory=dict)
|
||||
parents: dict[int, int] = field(default_factory=dict)
|
||||
relations: dict[int, str] = field(default_factory=dict)
|
||||
|
||||
def require(self, node_id: int, label: str, *, allow_zero: bool = False) -> None:
|
||||
if allow_zero and node_id == 0:
|
||||
return
|
||||
if not node_id or node_id not in self.nodes:
|
||||
raise SmokeError(f"{label} references unpublished nodeId={node_id}")
|
||||
|
||||
def clear(self) -> None:
|
||||
self.nodes.clear()
|
||||
self.parents.clear()
|
||||
self.relations.clear()
|
||||
|
||||
def forget(self, node_id: int) -> None:
|
||||
for child in [child for child, parent in self.parents.items() if parent == node_id]:
|
||||
self.forget(child)
|
||||
self.nodes.pop(node_id, None)
|
||||
self.parents.pop(node_id, None)
|
||||
self.relations.pop(node_id, None)
|
||||
|
||||
def remember(self, node: dict[str, Any], parent: int = 0, relation: str = "children") -> None:
|
||||
node_id = node["nodeId"]
|
||||
if not node_id:
|
||||
raise SmokeError(f"DOM tree snapshot contains nodeId 0: {node!r}")
|
||||
self.require(parent, "snapshot parent", allow_zero=True)
|
||||
self.require(node.get("parentId", 0), "snapshot parentId", allow_zero=True)
|
||||
# A replacement snapshot retires its old descendants, including frame
|
||||
# documents and shadow roots. Merely remembering IDs would miss this.
|
||||
self.forget(node_id)
|
||||
self.nodes[node_id] = node
|
||||
self.parents[node_id] = parent
|
||||
self.relations[node_id] = relation
|
||||
for child_relation, child in _descendants(node):
|
||||
self.remember(child, node_id, child_relation)
|
||||
|
||||
def describe(self, node: dict[str, Any]) -> None:
|
||||
for field_name in ("nodeId", "parentId"):
|
||||
self.require(node.get(field_name, 0), f"describeNode {field_name}", allow_zero=True)
|
||||
for _, child in _descendants(node):
|
||||
self.describe(child)
|
||||
|
||||
def observe(self, message: dict[str, Any]) -> None:
|
||||
method, params = message.get("method"), message.get("params", {})
|
||||
if method == "DOM.documentUpdated":
|
||||
self.clear()
|
||||
elif method == "DOM.setChildNodes":
|
||||
parent = params["parentId"]
|
||||
self.require(parent, method, allow_zero=True)
|
||||
if parent:
|
||||
for child in [child for child, owner in self.parents.items()
|
||||
if owner == parent and self.relations[child] == "children"]:
|
||||
self.forget(child)
|
||||
for node in params["nodes"]:
|
||||
self.remember(node, parent)
|
||||
elif method == "DOM.childNodeInserted":
|
||||
parent, previous = params["parentNodeId"], params["previousNodeId"]
|
||||
self.require(parent, method)
|
||||
self.require(previous, "previousNodeId", allow_zero=True)
|
||||
if previous:
|
||||
assert_equal(self.parents[previous], parent, "previous sibling parent")
|
||||
self.remember(params["node"], parent)
|
||||
elif method == "DOM.childNodeRemoved":
|
||||
self.require(params["parentNodeId"], method)
|
||||
self.require(params["nodeId"], method)
|
||||
assert_equal(self.parents[params["nodeId"]], params["parentNodeId"], "removed node parent")
|
||||
self.forget(params["nodeId"])
|
||||
elif method in ("DOM.attributeModified", "DOM.attributeRemoved",
|
||||
"DOM.characterDataModified", "DOM.childNodeCountUpdated"):
|
||||
self.require(params["nodeId"], method)
|
||||
elif method == "DOM.shadowRootPushed":
|
||||
self.require(params["hostId"], method)
|
||||
self.remember(params["root"], params["hostId"], "shadowRoots")
|
||||
elif method == "DOM.shadowRootPopped":
|
||||
self.require(params["hostId"], method)
|
||||
self.require(params["rootId"], method)
|
||||
self.forget(params["rootId"])
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Probe:
|
||||
client: RawCdpClient
|
||||
mirrors: dict[str, _DomMirror] = field(default_factory=dict)
|
||||
messages: list[dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
def observe(self, message: dict[str, Any]) -> None:
|
||||
self.messages.append(message)
|
||||
if mirror := self.mirrors.get(message.get("sessionId", "")):
|
||||
mirror.observe(message)
|
||||
|
||||
async def call(self, method: str, params: dict[str, Any] | None = None,
|
||||
session: str | None = None) -> dict[str, Any]:
|
||||
result, _, _ = await command(self.client, method, params, session_id=session,
|
||||
on_message=self.observe)
|
||||
if result.get("exceptionDetails"):
|
||||
raise SmokeError(f"{method} threw: {result!r}")
|
||||
if mirror := self.mirrors.get(session or ""):
|
||||
if method == "DOM.getDocument":
|
||||
mirror.clear()
|
||||
mirror.remember(result["root"])
|
||||
elif method == "DOM.describeNode":
|
||||
mirror.describe(result["node"])
|
||||
elif method in ("DOM.getSearchResults", "DOM.pushNodesByBackendIdsToFrontend"):
|
||||
for node_id in result["nodeIds"]:
|
||||
mirror.require(node_id, f"{method} response",
|
||||
allow_zero=method == "DOM.pushNodesByBackendIdsToFrontend")
|
||||
elif method == "DOM.requestNode":
|
||||
mirror.require(result["nodeId"], "requestNode response")
|
||||
elif method in ("DOM.getFrameOwner", "DOM.getNodeForLocation") and "nodeId" in result:
|
||||
mirror.require(result["nodeId"], f"{method} response")
|
||||
return result
|
||||
|
||||
async def attach(self, target: str) -> str:
|
||||
session = (await self.call("Target.attachToTarget", {
|
||||
"targetId": target, "flatten": True,
|
||||
}))["sessionId"]
|
||||
self.mirrors[session] = _DomMirror()
|
||||
return session
|
||||
|
||||
async def evaluate(self, session: str, expression: str) -> dict[str, Any]:
|
||||
return (await self.call("Runtime.evaluate", {
|
||||
"expression": expression, "awaitPromise": True,
|
||||
}, session))["result"]
|
||||
|
||||
async def document(self, session: str, depth: int = 1) -> None:
|
||||
await self.call("DOM.getDocument", {"depth": depth, "pierce": False}, session)
|
||||
|
||||
async def search(self, session: str, query: str, count: int, *, ua: bool = False) -> list[int]:
|
||||
search = await self.call("DOM.performSearch", {
|
||||
"query": query, "includeUserAgentShadowDOM": ua,
|
||||
}, session)
|
||||
assert_equal(search["resultCount"], count, f"{query} result count")
|
||||
result = await self.call("DOM.getSearchResults", {
|
||||
"searchId": search["searchId"], "fromIndex": 0, "toIndex": count,
|
||||
}, session)
|
||||
await self.call("DOM.discardSearchResults", {"searchId": search["searchId"]}, session)
|
||||
return result["nodeIds"]
|
||||
|
||||
|
||||
_HTML = """<main><section id="branch"><a id="target" class="hit light-hit">before</a>
|
||||
<a id="second" class="hit light-hit">second</a><a id="third" class="hit light-hit">third</a>
|
||||
</section></main><div id="host"><em id="retained">retained</em></div>
|
||||
<input id="control" type="search" value="needle">"""
|
||||
|
||||
|
||||
async def _install(probe: _Probe, session: str) -> None:
|
||||
await probe.evaluate(session, "(async () => {document.body.innerHTML = " + json.dumps(_HTML) + ";"
|
||||
"const frame=document.createElement('iframe');frame.id='frame';"
|
||||
"const loaded=new Promise(resolve=>frame.onload=resolve);"
|
||||
"frame.srcdoc='<main><a id=frame-hit class=hit>frame</a></main>';"
|
||||
"document.body.append(frame);await loaded;return true;})()")
|
||||
|
||||
|
||||
async def _describe_search(probe: _Probe, session: str, ancestor: bool, backend: bool) -> None:
|
||||
selector = "#branch" if ancestor else "#target"
|
||||
obj = await probe.evaluate(session, f"document.querySelector({json.dumps(selector)})")
|
||||
described = await probe.call("DOM.describeNode", {"objectId": obj["objectId"], "depth": 2}, session)
|
||||
assert_equal(described["node"]["nodeId"], 0, "cold describeNode ID")
|
||||
if backend:
|
||||
again = await probe.call("DOM.describeNode", {
|
||||
"backendNodeId": described["node"]["backendNodeId"], "depth": 2,
|
||||
}, session)
|
||||
assert_equal(again["node"]["nodeId"], 0, "cold backend describeNode ID")
|
||||
ids = await probe.search(session, "#target", 1)
|
||||
described = await probe.call("DOM.describeNode", {"nodeId": ids[0]}, session)
|
||||
assert_equal(described["node"]["nodeId"], ids[0], "published describeNode ID")
|
||||
assert_equal(await probe.search(session, "#target", 1), ids, "repeat search identity")
|
||||
|
||||
|
||||
async def _request_search(probe: _Probe, session: str, ancestor: bool) -> None:
|
||||
selector = "#branch" if ancestor else "#target"
|
||||
obj = await probe.evaluate(session, f"document.querySelector({json.dumps(selector)})")
|
||||
requested = await probe.call("DOM.requestNode", {"objectId": obj["objectId"]}, session)
|
||||
assert_equal(await probe.call("DOM.requestNode", {"objectId": obj["objectId"]}, session),
|
||||
requested, "repeat requestNode identity")
|
||||
await probe.search(session, "#target", 1)
|
||||
|
||||
|
||||
async def _ranges(probe: _Probe, session: str) -> None:
|
||||
search = await probe.call("DOM.performSearch", {"query": ".light-hit"}, session)
|
||||
assert_equal(search["resultCount"], 3, "range result count")
|
||||
params = {"searchId": search["searchId"], "fromIndex": 1, "toIndex": 2}
|
||||
result = await probe.call("DOM.getSearchResults", params, session)
|
||||
attrs = (await probe.call("DOM.getAttributes", {"nodeId": result["nodeIds"][0]}, session))["attributes"]
|
||||
assert_equal(dict(zip(attrs[::2], attrs[1::2]))["id"], "second", "requested result range")
|
||||
before = len(probe.messages)
|
||||
assert_equal(await probe.call("DOM.getSearchResults", params, session), result, "repeat range")
|
||||
assert_equal([m for m in probe.messages[before:] if m.get("method") == "DOM.setChildNodes"], [],
|
||||
"already published range needs no repeated path")
|
||||
await probe.call("DOM.getSearchResults", {**params, "fromIndex": 0, "toIndex": 3}, session)
|
||||
|
||||
|
||||
async def _mixed(probe: _Probe, session: str) -> None:
|
||||
await probe.evaluate(session, "document.getElementById('host').attachShadow({mode:'closed'})"
|
||||
".innerHTML='<section><a id=shadow-hit class=hit>shadow</a></section>'")
|
||||
await probe.search(session, ".hit", 5)
|
||||
|
||||
|
||||
async def _ua(probe: _Probe, session: str) -> None:
|
||||
hidden = await probe.call("DOM.performSearch", {"query": "#editing-view-port"}, session)
|
||||
assert_equal(hidden["resultCount"], 0, "UA nodes excluded by default")
|
||||
await probe.search(session, "#editing-view-port", 1, ua=True)
|
||||
|
||||
|
||||
async def _detached(probe: _Probe, session: str) -> None:
|
||||
search = await probe.call("DOM.performSearch", {"query": "#target"}, session)
|
||||
assert_equal(search["resultCount"], 1, "retained search result")
|
||||
await probe.evaluate(session, "window.retained=document.getElementById('target');retained.remove()")
|
||||
result = await probe.call("DOM.getSearchResults", {
|
||||
"searchId": search["searchId"], "fromIndex": 0, "toIndex": 1,
|
||||
}, session)
|
||||
assert_equal(probe.mirrors[session].nodes[result["nodeIds"][0]]["localName"], "a", "detached node")
|
||||
|
||||
|
||||
async def _associated(probe: _Probe, session: str, frame: bool, depth: int) -> None:
|
||||
await probe.document(session, depth)
|
||||
owner = "frame" if frame else "host"
|
||||
root = next(node_id for node_id, node in probe.mirrors[session].nodes.items()
|
||||
if node["nodeType"] == 9 and probe.mirrors[session].parents[node_id] == 0)
|
||||
await probe.call("DOM.querySelector", {"nodeId": root, "selector": f"#{owner}"}, session)
|
||||
if frame:
|
||||
await probe.evaluate(session, "new Promise(resolve => {const f=document.getElementById('frame');"
|
||||
"f.onload=()=>resolve(true);f.srcdoc='<section><a id=replacement>new</a></section>';})")
|
||||
query = "#replacement"
|
||||
else:
|
||||
await probe.evaluate(session, "document.getElementById('host').attachShadow({mode:'closed'})"
|
||||
".innerHTML='<section><a id=replacement>shadow</a></section>'")
|
||||
query = "#replacement"
|
||||
await probe.search(session, query, 1)
|
||||
# Previously published light children remain usable after the owner gains
|
||||
# an associated tree; searching it must repair any replacement snapshot.
|
||||
if not frame and depth == -1:
|
||||
await probe.search(session, "#retained", 1)
|
||||
|
||||
|
||||
async def _parent_refresh(probe: _Probe, session: str, whitespace: bool) -> None:
|
||||
await probe.call("DOM.enable", {"includeWhitespace": "all" if whitespace else "none"}, session)
|
||||
await probe.evaluate(session, "document.getElementById('host').attachShadow({mode:'closed'})"
|
||||
".innerHTML='<section><a id=shadow-hit>shadow</a></section>'")
|
||||
await probe.call("DOM.getDocument", {"depth": -1, "pierce": True}, session)
|
||||
await probe.search(session, "#frame-hit", 1)
|
||||
await probe.search(session, "#shadow-hit", 1)
|
||||
await probe.search(session, "#editing-view-port", 1, ua=True)
|
||||
mirror = probe.mirrors[session]
|
||||
body = next(node_id for node_id, node in mirror.nodes.items() if node["nodeName"] == "BODY")
|
||||
retained = set(mirror.nodes)
|
||||
for _ in range(2):
|
||||
# The same complete child-snapshot publisher services mutation resyncs.
|
||||
# A shallow replacement must retain every still-live published ID,
|
||||
# including frame documents and generated/author shadow descendants.
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": body, "depth": 1}, session)
|
||||
missing = retained - mirror.nodes.keys()
|
||||
if missing:
|
||||
raise SmokeError(f"parent replacement retired live published nodes: {sorted(missing)}")
|
||||
await probe.search(session, "#frame-hit", 1)
|
||||
await probe.search(session, "#shadow-hit", 1)
|
||||
await probe.search(session, "#editing-view-port", 1, ua=True)
|
||||
await probe.evaluate(session, "document.getElementById('frame').contentDocument"
|
||||
".getElementById('frame-hit').setAttribute('data-resync','retained')")
|
||||
await probe.search(session, "#frame-hit", 1)
|
||||
|
||||
|
||||
async def _template_refresh(probe: _Probe, session: str, owner_refresh: bool) -> None:
|
||||
await probe.evaluate(session, "document.body.innerHTML='<div id=template-host><template id=tpl>"
|
||||
"<article id=article><span>inside</span></article></template></div>'")
|
||||
await probe.document(session, -1)
|
||||
mirror = probe.mirrors[session]
|
||||
template = next(node for node in mirror.nodes.values() if node["nodeName"] == "TEMPLATE")
|
||||
fragment = template["templateContent"]
|
||||
assert_equal(fragment.get("children"), None, "template content stays shallow")
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": fragment["nodeId"], "depth": -1}, session)
|
||||
article = next(node for node in mirror.nodes.values() if node["nodeName"] == "ARTICLE")
|
||||
retained = set(mirror.nodes)
|
||||
body = next(node_id for node_id, node in mirror.nodes.items() if node["nodeName"] == "BODY")
|
||||
if owner_refresh:
|
||||
await probe.evaluate(session, "document.getElementById('template-host').attachShadow({mode:'closed'})"
|
||||
".innerHTML='<a id=template-shadow>shadow</a>'")
|
||||
await probe.search(session, "#template-shadow", 1)
|
||||
else:
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": body, "depth": 1}, session)
|
||||
missing = retained - mirror.nodes.keys()
|
||||
if missing:
|
||||
raise SmokeError(f"template parent request retired published nodes: {sorted(missing)}")
|
||||
result = await probe.call("DOM.pushNodesByBackendIdsToFrontend", {
|
||||
"backendNodeIds": [article["backendNodeId"]],
|
||||
}, session)
|
||||
# Chromium returns 0 for backend IDs owned by the inert template Document.
|
||||
# A nonzero result must still refer to the retained frontend node.
|
||||
if result["nodeIds"][0] not in (0, article["nodeId"]):
|
||||
raise SmokeError(f"template descendant changed identity: {result!r}")
|
||||
await probe.call("DOM.getAttributes", {"nodeId": article["nodeId"]}, session)
|
||||
|
||||
|
||||
async def _deep_owner_refresh(probe: _Probe, session: str) -> None:
|
||||
await probe.evaluate(session, "(() => {document.body.innerHTML='<main id=deep-host style=display:none></main>';"
|
||||
"let cursor=document.getElementById('deep-host');"
|
||||
"for(let i=0;i<1100;++i){const child=document.createElement('div');"
|
||||
"child.id='deep-'+i;cursor.appendChild(child);cursor=child;}})()")
|
||||
await probe.document(session, 4)
|
||||
mirror = probe.mirrors[session]
|
||||
for index in range(0, 1100, 50):
|
||||
node = next(n for n in mirror.nodes.values()
|
||||
if n.get("attributes") == ["id", f"deep-{index}"])
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": node["nodeId"], "depth": 50}, session)
|
||||
leaf = next(n for n in mirror.nodes.values() if n.get("attributes") == ["id", "deep-1099"])
|
||||
retained = set(mirror.nodes)
|
||||
await probe.evaluate(session, "document.getElementById('deep-host').attachShadow({mode:'closed'})"
|
||||
".innerHTML='<b id=deep-shadow>shadow</b>'")
|
||||
await probe.search(session, "#deep-shadow", 1)
|
||||
missing = retained - mirror.nodes.keys()
|
||||
if missing:
|
||||
raise SmokeError(f"1100-level owner refresh retired published nodes: {sorted(missing)}")
|
||||
result = await probe.call("DOM.pushNodesByBackendIdsToFrontend", {
|
||||
"backendNodeIds": [leaf["backendNodeId"]],
|
||||
}, session)
|
||||
assert_equal(result["nodeIds"], [leaf["nodeId"]], "deep descendant keeps its frontend identity")
|
||||
await probe.evaluate(session, "document.getElementById('deep-1099').setAttribute('data-state','retained')")
|
||||
|
||||
|
||||
async def _request_depth(probe: _Probe, session: str) -> None:
|
||||
await probe.document(session, 3)
|
||||
mirror = probe.mirrors[session]
|
||||
body = next(node_id for node_id, node in mirror.nodes.items() if node["nodeName"] == "BODY")
|
||||
for depth in (2, 3):
|
||||
start = len(probe.messages)
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": body, "depth": depth}, session)
|
||||
events = [m["params"] for m in probe.messages[start:] if m.get("method") == "DOM.setChildNodes"]
|
||||
if any(event["parentId"] == body for event in events):
|
||||
raise SmokeError("deeper request replaced an already expanded parent")
|
||||
start = len(probe.messages)
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": body, "depth": depth}, session)
|
||||
assert_equal([m for m in probe.messages[start:] if m.get("method") == "DOM.setChildNodes"], [],
|
||||
"repeated child expansion is idempotent")
|
||||
for selector in ("#target", "#retained"):
|
||||
obj = await probe.evaluate(session, f"document.querySelector({json.dumps(selector)})")
|
||||
node = (await probe.call("DOM.describeNode", {"objectId": obj["objectId"]}, session))["node"]
|
||||
mirror.require(node["nodeId"], "deeper request published all branches")
|
||||
|
||||
|
||||
async def _frontend_inputs(probe: _Probe, target: str) -> None:
|
||||
peer = await probe.attach(target)
|
||||
await probe.document(peer, -1)
|
||||
obj = await probe.evaluate(peer, "document.getElementById('branch')")
|
||||
peer_node = (await probe.call("DOM.describeNode", {"objectId": obj["objectId"]}, peer))["node"]
|
||||
session = await probe.attach(target)
|
||||
guessed = peer_node["nodeId"]
|
||||
|
||||
async def assert_ignored_child_request(node_id: int) -> None:
|
||||
start = len(probe.messages)
|
||||
result = await probe.call("DOM.requestChildNodes", {"nodeId": node_id, "depth": 1}, session)
|
||||
assert_equal(result, {}, "missing child request succeeds")
|
||||
assert_equal([m for m in probe.messages[start:] if m.get("method", "").startswith("DOM.")], [],
|
||||
"missing child request must not publish nodes")
|
||||
|
||||
await assert_ignored_child_request(guessed)
|
||||
await probe.document(session)
|
||||
if guessed in probe.mirrors[session].nodes:
|
||||
raise SmokeError("fixture ID must be unknown to the shallow session")
|
||||
commands = [("DOM.requestChildNodes", {"depth": 1}),
|
||||
("DOM.querySelector", {"selector": "#target"}),
|
||||
("DOM.querySelectorAll", {"selector": "#target"}),
|
||||
("DOM.getAttributes", {}), ("DOM.getOuterHTML", {}),
|
||||
("DOM.describeNode", {}), ("DOM.resolveNode", {})]
|
||||
for method, params in commands:
|
||||
start = len(probe.messages)
|
||||
rid = await probe.client.send(method, {"nodeId": guessed, **params}, session_id=session)
|
||||
reply, _ = await receive_response(probe.client, rid, on_message=probe.observe)
|
||||
if method == "DOM.requestChildNodes":
|
||||
assert_equal(reply.get("result"), {}, "unknown child request is a no-op")
|
||||
else:
|
||||
assert_equal(reply.get("error", {}).get("code"), -32000, "unknown frontend input")
|
||||
assert_equal([m for m in probe.messages[start:] if m.get("method", "").startswith("DOM.")], [],
|
||||
"unknown input must not publish nodes")
|
||||
obj = await probe.evaluate(session, "document.getElementById('branch')")
|
||||
node_id = (await probe.call("DOM.requestNode", {"objectId": obj["objectId"]}, session))["nodeId"]
|
||||
for method, params in commands:
|
||||
await probe.call(method, {"nodeId": node_id, **params}, session)
|
||||
await probe.call("DOM.disable", session=session)
|
||||
await assert_ignored_child_request(node_id)
|
||||
|
||||
|
||||
async def _id_exit(probe: _Probe, target: str, kind: str, state: int) -> None:
|
||||
session = await probe.attach(target)
|
||||
await probe.evaluate(session, "document.getElementById('target').style.cssText="
|
||||
"'position:fixed;left:20px;top:20px;width:40px;height:40px;display:block;z-index:10000'")
|
||||
if state:
|
||||
await probe.document(session, 1 if state == 1 else -1)
|
||||
selector = "#frame" if kind == "frame_owner" else "#target"
|
||||
obj = await probe.evaluate(session, f"document.querySelector({json.dumps(selector)})")
|
||||
described = (await probe.call("DOM.describeNode", {"objectId": obj["objectId"]}, session))["node"]
|
||||
backend = described["backendNodeId"]
|
||||
assert_equal(bool(described["nodeId"]), state == 2, "initial frontend publication state")
|
||||
if kind == "push":
|
||||
params = {"backendNodeIds": [backend, 0, backend]}
|
||||
if not state:
|
||||
rid = await probe.client.send("DOM.pushNodesByBackendIdsToFrontend", params, session_id=session)
|
||||
reply, _ = await receive_response(probe.client, rid, on_message=probe.observe)
|
||||
if "Document needs to be requested first" not in reply.get("error", {}).get("message", ""):
|
||||
raise SmokeError(f"cold pushNodes must require a requested document: {reply!r}")
|
||||
return
|
||||
result = await probe.call("DOM.pushNodesByBackendIdsToFrontend", params, session)
|
||||
node_id = result["nodeIds"][0]
|
||||
assert_equal(result["nodeIds"], [node_id, 0, node_id], "batch positions and duplicate identities")
|
||||
elif kind == "frame_owner":
|
||||
tree = (await probe.call("Page.getFrameTree", session=session))["frameTree"]
|
||||
result = await probe.call("DOM.getFrameOwner", {
|
||||
"frameId": tree["childFrames"][0]["frame"]["id"],
|
||||
}, session)
|
||||
assert_equal(result["backendNodeId"], backend, "frame owner backend identity")
|
||||
node_id = result.get("nodeId")
|
||||
else:
|
||||
result = await probe.call("DOM.getNodeForLocation", {"x": 25, "y": 25}, session)
|
||||
assert_equal(result["backendNodeId"], backend, "hit-test backend identity")
|
||||
node_id = result.get("nodeId")
|
||||
if not state:
|
||||
assert_equal(node_id, None, "cold document exposes backend identity only")
|
||||
assert_equal(probe.mirrors[session].nodes, {}, "cold lookup does not publish a DOM tree")
|
||||
return
|
||||
probe.mirrors[session].require(node_id, "node ID outlet")
|
||||
if state == 2:
|
||||
assert_equal(node_id, described["nodeId"], "previously published identity stays stable")
|
||||
await probe.call("DOM.getAttributes", {"nodeId": node_id}, session)
|
||||
assert_equal(await probe.search(session, selector, 1), [node_id], "subsequent search reuses publication")
|
||||
|
||||
|
||||
async def _push_inspector_trees(probe: _Probe, target: str) -> None:
|
||||
session = await probe.attach(target)
|
||||
await probe.evaluate(session, "window.pushShadow=document.getElementById('host').attachShadow({mode:'closed'});"
|
||||
"pushShadow.innerHTML='<a id=push-shadow>shadow</a>';"
|
||||
"window.pushDetached=document.createElement('a');pushDetached.textContent='detached';"
|
||||
"document.body.insertAdjacentHTML('beforeend','<li id=push-marker>marker</li>');"
|
||||
"document.getElementById('push-marker').getBoundingClientRect()")
|
||||
await probe.document(session)
|
||||
backend_ids = []
|
||||
for expression in ("document.getElementById('target')", "pushShadow.firstChild",
|
||||
"document.getElementById('frame').contentDocument.getElementById('frame-hit')",
|
||||
"pushDetached"):
|
||||
obj = await probe.evaluate(session, expression)
|
||||
node = (await probe.call("DOM.describeNode", {"objectId":obj["objectId"]}, session))["node"]
|
||||
backend_ids.append(node["backendNodeId"])
|
||||
for selector, field, wanted in (("#control", "shadowRoots", "editing-view-port"),
|
||||
("#push-marker", "pseudoElements", None)):
|
||||
obj = await probe.evaluate(session, f"document.querySelector({json.dumps(selector)})")
|
||||
node = (await probe.call("DOM.describeNode", {
|
||||
"objectId":obj["objectId"], "depth":-1, "pierce":True,
|
||||
}, session))["node"]
|
||||
pending = list(node.get(field, []))
|
||||
match = None
|
||||
while pending:
|
||||
candidate = pending.pop()
|
||||
attributes = candidate.get("attributes", [])
|
||||
if wanted is None or dict(zip(attributes[::2],attributes[1::2])).get("id") == wanted:
|
||||
match = candidate
|
||||
break
|
||||
pending.extend(child for _, child in _descendants(candidate))
|
||||
if match is None:
|
||||
raise SmokeError(f"missing generated Inspector node for {selector}: {node!r}")
|
||||
backend_ids.append(match["backendNodeId"])
|
||||
result = await probe.call("DOM.pushNodesByBackendIdsToFrontend", {"backendNodeIds":backend_ids}, session)
|
||||
for backend, node_id in zip(backend_ids, result["nodeIds"], strict=True):
|
||||
probe.mirrors[session].require(node_id, "mixed Inspector push response")
|
||||
node = (await probe.call("DOM.describeNode", {"nodeId":node_id}, session))["node"]
|
||||
assert_equal(node["backendNodeId"], backend, "published backend identity")
|
||||
# Chromium can assign a fresh frontend ID when pushing a detached root
|
||||
# again. Connected Inspector nodes retain their published identities.
|
||||
connected = [index for index in range(len(backend_ids)) if index != 3]
|
||||
repeated = await probe.call("DOM.pushNodesByBackendIdsToFrontend", {
|
||||
"backendNodeIds":[backend_ids[index] for index in connected],
|
||||
}, session)
|
||||
assert_equal(repeated["nodeIds"], [result["nodeIds"][index] for index in connected],
|
||||
"repeat connected push identity")
|
||||
|
||||
|
||||
async def _sessions(probe: _Probe, session: str, target: str, mutations: bool) -> None:
|
||||
other = await probe.attach(target)
|
||||
await probe.document(other, -1 if mutations else 1)
|
||||
if not mutations:
|
||||
await probe.search(session, "#target", 1)
|
||||
obj = await probe.evaluate(other, "document.getElementById('target')")
|
||||
node = (await probe.call("DOM.describeNode", {"objectId": obj["objectId"]}, other))["node"]
|
||||
assert_equal(node["nodeId"], 0, "another session's search does not publish here")
|
||||
await probe.search(other, "#target", 1)
|
||||
return
|
||||
expression = """(() => {const n=document.getElementById('target');
|
||||
n.setAttribute('data-state','ready');n.removeAttribute('data-state');
|
||||
n.firstChild.data='changed';n.append(document.createElement('b'));n.lastChild.remove();
|
||||
n.firstChild.data=' ';n.firstChild.data='restored';})()"""
|
||||
for published in (False, True):
|
||||
if published:
|
||||
obj = await probe.evaluate(session, "document.getElementById('branch')")
|
||||
branch = await probe.call("DOM.requestNode", {"objectId": obj["objectId"]}, session)
|
||||
await probe.call("DOM.requestChildNodes", {"nodeId": branch["nodeId"], "depth": -1}, session)
|
||||
start = len(probe.messages)
|
||||
await probe.evaluate(session, expression)
|
||||
await probe.evaluate(other, "void 0")
|
||||
events = [m for m in probe.messages[start:] if m.get("method", "").startswith("DOM.")]
|
||||
for receiver in (session, other):
|
||||
methods = {m["method"] for m in events if m.get("sessionId") == receiver}
|
||||
if receiver == session and not published:
|
||||
assert_equal(methods, set(), "hidden nodes emit no mutation events")
|
||||
else:
|
||||
expected = {"DOM.attributeModified", "DOM.attributeRemoved", "DOM.characterDataModified",
|
||||
"DOM.childNodeInserted", "DOM.childNodeRemoved"}
|
||||
if not expected <= methods:
|
||||
raise SmokeError(f"missing published mutations: {expected - methods!r}")
|
||||
|
||||
|
||||
async def _navigation(probe: _Probe, session: str, fixture: str) -> None:
|
||||
await probe.search(session, "#target", 1)
|
||||
search = await probe.call("DOM.performSearch", {"query": "#target"}, session)
|
||||
await navigate(probe.client, session, {"url": f"{fixture}/plain?dom-publication-replacement"},
|
||||
on_message=probe.observe)
|
||||
assert_equal(probe.mirrors[session].nodes, {}, "documentUpdated retires the client mirror")
|
||||
rid = await probe.client.send("DOM.getSearchResults", {
|
||||
"searchId": search["searchId"], "fromIndex": 0, "toIndex": 1,
|
||||
}, session_id=session)
|
||||
response, _ = await receive_response(probe.client, rid, on_message=probe.observe)
|
||||
if "error" not in response:
|
||||
raise SmokeError(f"retired search survived navigation: {response!r}")
|
||||
await _install(probe, session)
|
||||
await probe.document(session)
|
||||
await probe.search(session, "#target", 1)
|
||||
|
||||
|
||||
async def run_dom_publication_group(endpoint: str, fixture: str, results: list[dict[str, Any]]) -> None:
|
||||
# This isolated worker receives 1000-level snapshots; its JSON decoder,
|
||||
# DOM mirror, and persisted diagnostics must all be able to inspect them.
|
||||
sys.setrecursionlimit(max(sys.getrecursionlimit(), 8000))
|
||||
scenarios = [
|
||||
(f"describe_{reference}_{node}", lambda p, s, t, a=ancestor, b=backend: _describe_search(p, s, a, b))
|
||||
for node, ancestor in (("target", False), ("ancestor", True))
|
||||
for reference, backend in (("object", False), ("backend", True))
|
||||
]
|
||||
scenarios += [(f"request_{node}", lambda p, s, t, a=ancestor: _request_search(p, s, a))
|
||||
for node, ancestor in (("target", False), ("ancestor", True))]
|
||||
scenarios += [
|
||||
("range_and_repeat", lambda p, s, t: _ranges(p, s)),
|
||||
("mixed_light_shadow_frame", lambda p, s, t: _mixed(p, s)),
|
||||
("generated_ua", lambda p, s, t: _ua(p, s)),
|
||||
("retained_detached", lambda p, s, t: _detached(p, s)),
|
||||
("session_isolation", lambda p, s, t: _sessions(p, s, t, False)),
|
||||
("mutation_visibility", lambda p, s, t: _sessions(p, s, t, True)),
|
||||
("navigation_reset", lambda p, s, t: _navigation(p, s, fixture)),
|
||||
]
|
||||
scenarios += [
|
||||
(f"new_{kind}_{depth}", lambda p, s, t, f=frame, d=depth: _associated(p, s, f, d))
|
||||
for kind, frame in (("shadow", False), ("frame_document", True)) for depth in (1, -1)
|
||||
]
|
||||
scenarios += [
|
||||
(f"parent_refresh_whitespace_{whitespace}", lambda p, s, t, w=whitespace: _parent_refresh(p, s, w))
|
||||
for whitespace in (False, True)
|
||||
]
|
||||
scenarios += [
|
||||
(f"{kind}_publication_{state}", lambda p, s, t, k=kind, d=state: _id_exit(p, t, k, d))
|
||||
for kind in ("push", "frame_owner", "hit_test") for state in range(3)
|
||||
]
|
||||
scenarios.append(("push_inspector_trees", lambda p, s, t: _push_inspector_trees(p, t)))
|
||||
scenarios.append(("frontend_input_visibility", lambda p, s, t: _frontend_inputs(p, t)))
|
||||
scenarios += [
|
||||
("template_parent_refresh", lambda p, s, t: _template_refresh(p, s, False)),
|
||||
("template_owner_refresh", lambda p, s, t: _template_refresh(p, s, True)),
|
||||
("request_children_depth_and_repeat", lambda p, s, t: _request_depth(p, s)),
|
||||
("deep_owner_refresh", lambda p, s, t: _deep_owner_refresh(p, s)),
|
||||
]
|
||||
failures = []
|
||||
for name, scenario in scenarios:
|
||||
client = await connect_raw_cdp(endpoint)
|
||||
probe = _Probe(client)
|
||||
target = None
|
||||
try:
|
||||
target = (await probe.call("Target.createTarget", {"url": "about:blank"}))["targetId"]
|
||||
session = await probe.attach(target)
|
||||
await probe.call("Page.enable", session=session)
|
||||
await navigate(client, session, {"url": f"{fixture}/plain?dom-publication"},
|
||||
on_message=probe.observe)
|
||||
await _install(probe, session)
|
||||
await probe.document(session)
|
||||
await scenario(probe, session, target)
|
||||
record(results, f"dom_publication_{name}", {"messages": len(probe.messages)})
|
||||
except Exception as error:
|
||||
failures.append(f"{name}: {error}")
|
||||
results.append({"name": f"dom_publication_{name}", "ok": False,
|
||||
"error": str(error), "wire": probe.messages})
|
||||
finally:
|
||||
if target:
|
||||
with contextlib.suppress(Exception):
|
||||
async with asyncio.timeout(3):
|
||||
await probe.call("Target.closeTarget", {"targetId": target})
|
||||
await client.websocket.close()
|
||||
if failures:
|
||||
raise SmokeError("DOM publication contracts failed: " + "; ".join(failures))
|
||||
@@ -33,6 +33,7 @@ from .groups.core import run_core_group
|
||||
from .groups.dom_input import run_dom_input_group
|
||||
from .groups.dom_hit_test import run_dom_hit_test_group
|
||||
from .groups.dom_parser_mutations import run_dom_parser_mutations_group
|
||||
from .groups.dom_publication import run_dom_publication_group
|
||||
from .groups.dom_shadow_outer_html import run_dom_shadow_outer_html_group
|
||||
from .groups.dom_snapshot import run_dom_snapshot_group
|
||||
from .groups.dom_whitespace import run_dom_whitespace_group
|
||||
@@ -174,6 +175,12 @@ RAW_GROUPS: tuple[SmokeGroup, ...] = (
|
||||
"raw",
|
||||
run_agent_episode_group,
|
||||
),
|
||||
SmokeGroup(
|
||||
"dom-publication",
|
||||
"Raw DOM mirror publication, search/request/describe order, associated and detached trees, mutations, and session isolation.",
|
||||
"raw",
|
||||
run_dom_publication_group,
|
||||
),
|
||||
SmokeGroup(
|
||||
"dom-parser-mutations",
|
||||
"Raw Chromium-calibrated parser-tail DOM mutation and DCL binding refresh order.",
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from moli_cdp_smoke.assertions import SmokeError
|
||||
from moli_cdp_smoke.groups.dom_publication import _DomMirror
|
||||
|
||||
|
||||
def node(node_id, **fields):
|
||||
return {"nodeId": node_id, "nodeType": 1, **fields}
|
||||
|
||||
|
||||
class DomPublicationMirrorTests(unittest.TestCase):
|
||||
def test_description_does_not_publish_a_node(self):
|
||||
mirror = _DomMirror()
|
||||
mirror.describe(node(0, backendNodeId=123))
|
||||
with self.assertRaisesRegex(SmokeError, "unpublished"):
|
||||
mirror.require(123, "getSearchResults response")
|
||||
|
||||
def test_result_must_be_known_before_a_later_notification(self):
|
||||
mirror = _DomMirror()
|
||||
mirror.remember(node(1))
|
||||
with self.assertRaisesRegex(SmokeError, "unpublished"):
|
||||
mirror.require(2, "getSearchResults response")
|
||||
mirror.observe({"method": "DOM.setChildNodes", "params": {
|
||||
"parentId": 1, "nodes": [node(2, parentId=1)],
|
||||
}})
|
||||
mirror.require(2, "getSearchResults response")
|
||||
|
||||
def test_unknown_event_targets_and_parents_fail(self):
|
||||
for method, params in [
|
||||
("DOM.attributeModified", {"nodeId": 2}),
|
||||
("DOM.attributeRemoved", {"nodeId": 2}),
|
||||
("DOM.characterDataModified", {"nodeId": 2}),
|
||||
("DOM.childNodeCountUpdated", {"nodeId": 2}),
|
||||
("DOM.setChildNodes", {"parentId": 2, "nodes": []}),
|
||||
("DOM.childNodeInserted", {"parentNodeId": 1, "previousNodeId": 2, "node": node(3)}),
|
||||
]:
|
||||
with self.subTest(method=method):
|
||||
mirror = _DomMirror()
|
||||
mirror.remember(node(1))
|
||||
with self.assertRaisesRegex(SmokeError, "unpublished"):
|
||||
mirror.observe({"method": method, "params": params})
|
||||
|
||||
def test_replacement_and_removal_retire_all_associated_descendants(self):
|
||||
for update in [
|
||||
{"method": "DOM.setChildNodes", "params": {"parentId": 1, "nodes": [node(2)]}},
|
||||
{"method": "DOM.childNodeRemoved", "params": {"parentNodeId": 1, "nodeId": 2}},
|
||||
{"method": "DOM.documentUpdated", "params": {}},
|
||||
]:
|
||||
with self.subTest(update=update):
|
||||
mirror = _DomMirror()
|
||||
mirror.remember(node(1, children=[node(2, children=[node(3)],
|
||||
shadowRoots=[node(4, children=[node(5)])],
|
||||
contentDocument=node(6, children=[node(7)]),
|
||||
templateContent=node(8), pseudoElements=[node(9)])]))
|
||||
mirror.observe(update)
|
||||
for node_id in range(3, 10):
|
||||
with self.assertRaisesRegex(SmokeError, "unpublished"):
|
||||
mirror.require(node_id, "retired descendant")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -31,6 +31,7 @@ class GroupSelectionTests(unittest.TestCase):
|
||||
self.assertIn("file-chooser", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("inspector-routing", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("cdp-ordering", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("dom-publication", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("navigation-outcomes", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("font-face", DEFAULT_GROUP_NAMES)
|
||||
self.assertIn("media-error", DEFAULT_GROUP_NAMES)
|
||||
|
||||
@@ -297,6 +297,7 @@ impl Page {
|
||||
RendererPageReply::OptionalExecutionContextId(_) => {
|
||||
"an optional execution context reply"
|
||||
}
|
||||
RendererPageReply::DocumentNodeRequested { .. } => "a requested document node reply",
|
||||
RendererPageReply::OptionalDocumentNodeObjectSnapshot(_) => {
|
||||
"an optional document node object snapshot"
|
||||
}
|
||||
@@ -336,7 +337,7 @@ impl Page {
|
||||
RendererPageReply::DocumentPerformSearch(_) => {
|
||||
"a document perform search registration reply"
|
||||
}
|
||||
RendererPageReply::DocumentSearchResults(_) => "a document search results reply",
|
||||
RendererPageReply::DocumentSearchResults { .. } => "a document search results reply",
|
||||
RendererPageReply::DocumentSearchResultsDiscarded => {
|
||||
"a document search results discarded reply"
|
||||
}
|
||||
|
||||
@@ -849,6 +849,7 @@ impl Page {
|
||||
object_id: &str,
|
||||
depth: i32,
|
||||
pierce: bool,
|
||||
bind_frontend_nodes: bool,
|
||||
) -> Result<PendingPageCommand> {
|
||||
self.start_page_command(RendererPageCommand::document_node_snapshot_for_object_id(
|
||||
inspector_session_id,
|
||||
@@ -856,9 +857,37 @@ impl Page {
|
||||
object_id.to_owned(),
|
||||
depth,
|
||||
pierce,
|
||||
bind_frontend_nodes,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn start_request_document_node_for_object_id(
|
||||
&self,
|
||||
inspector_session_id: Option<String>,
|
||||
include_whitespace: bool,
|
||||
object_id: String,
|
||||
) -> Result<PendingPageCommand> {
|
||||
self.start_page_command(RendererPageCommand::request_document_node_for_object_id(
|
||||
inspector_session_id,
|
||||
include_whitespace,
|
||||
object_id,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn finish_request_document_node_for_object_id(
|
||||
&mut self,
|
||||
completion: CompletedPageCommand,
|
||||
) -> Result<(Option<u32>, Option<RendererDocumentChildNodeSnapshotEvents>)> {
|
||||
let reply = self.finish_page_command(completion);
|
||||
expect_page_reply!(
|
||||
reply,
|
||||
"request document node page command",
|
||||
"a requested document node reply",
|
||||
RendererPageReply::DocumentNodeRequested { node_id, child_node_snapshot_events } =>
|
||||
Ok((node_id, child_node_snapshot_events)),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn finish_document_node_snapshot_for_object_id(
|
||||
&mut self,
|
||||
completion: CompletedPageCommand,
|
||||
@@ -1296,13 +1325,18 @@ impl Page {
|
||||
pub fn finish_document_search_results(
|
||||
&mut self,
|
||||
completion: CompletedPageCommand,
|
||||
) -> Result<RendererDomSearchResultsResolution> {
|
||||
) -> Result<(
|
||||
RendererDomSearchResultsResolution,
|
||||
Option<RendererDocumentChildNodeSnapshotEvents>,
|
||||
)> {
|
||||
let reply = self.finish_page_command(completion);
|
||||
expect_page_reply!(
|
||||
reply,
|
||||
"document search results page command",
|
||||
"a document search results resolution",
|
||||
RendererPageReply::DocumentSearchResults(result) => Ok(result),
|
||||
RendererPageReply::DocumentSearchResults { resolution, child_node_snapshot_events } => {
|
||||
Ok((resolution, child_node_snapshot_events))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1384,8 +1418,24 @@ impl Page {
|
||||
frontend_node_id: u32,
|
||||
) -> Result<PendingPageCommand> {
|
||||
self.start_page_command(RendererPageCommand::DocumentFrontendNodeBinding {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
reference: RendererDomNodeReference::FrontendNodeId {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve an internal automation binding without treating it as a CDP input.
|
||||
pub fn start_document_internal_frontend_node_binding(
|
||||
&self,
|
||||
inspector_session_id: Option<String>,
|
||||
frontend_node_id: u32,
|
||||
) -> Result<PendingPageCommand> {
|
||||
self.start_page_command(RendererPageCommand::DocumentFrontendNodeBinding {
|
||||
reference: RendererDomNodeReference::InternalFrontendNodeBinding {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -941,6 +941,22 @@ pub enum DevToolsDomNodeReference {
|
||||
}
|
||||
|
||||
impl DevToolsDomNodeReference {
|
||||
pub(crate) fn into_renderer_reference_for_protocol(
|
||||
self,
|
||||
inspector_session_id: Option<String>,
|
||||
protocol: FrontendProtocol,
|
||||
) -> moli_core::page::RendererDomNodeReference {
|
||||
if protocol != FrontendProtocol::Cdp
|
||||
&& let Self::FrontendNodeId(frontend_node_id) = self
|
||||
{
|
||||
return moli_core::page::RendererDomNodeReference::InternalFrontendNodeBinding {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
};
|
||||
}
|
||||
self.into_renderer_reference(inspector_session_id)
|
||||
}
|
||||
|
||||
pub(crate) fn into_renderer_reference(
|
||||
self,
|
||||
inspector_session_id: Option<String>,
|
||||
|
||||
@@ -800,12 +800,15 @@ impl CdpConnection {
|
||||
self.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let pending = {
|
||||
let page = self.loaded_page_mut_for_protocol_access_for_owner(owner)?;
|
||||
// LocateNodes and script node values need internal frontend handles.
|
||||
// Allocating them does not publish a node in the CDP DOM mirror.
|
||||
page.start_document_node_snapshot_for_object_id_in_inspector_session(
|
||||
inspector_session_id,
|
||||
include_whitespace,
|
||||
object_id,
|
||||
depth,
|
||||
pierce,
|
||||
true,
|
||||
)
|
||||
.map_err(|error| format!("resolve runtime node snapshot failed: {error}"))?
|
||||
};
|
||||
|
||||
@@ -914,6 +914,11 @@ async fn child_frame_renderer_dom_node_ids_for_selector_async(
|
||||
selector: &str,
|
||||
base_id: u64,
|
||||
) -> ChildFrameRendererDomNodeIds {
|
||||
ctx.process_async(
|
||||
json!({"id":base_id+3,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = ctx.take_response_by_id(base_id + 3);
|
||||
ctx.process_async(json!({
|
||||
"id": base_id,
|
||||
"method": "Page.createIsolatedWorld",
|
||||
|
||||
@@ -1769,10 +1769,13 @@ fn cdp_result_payload_from_devtools_result(result: AutomationResult) -> Value {
|
||||
AutomationResult::DescribeNode(result) => json!({
|
||||
"node": result.node,
|
||||
}),
|
||||
AutomationResult::GetFrameOwner(result) => json!({
|
||||
"nodeId": result.node_id,
|
||||
"backendNodeId": result.backend_node_id,
|
||||
}),
|
||||
AutomationResult::GetFrameOwner(result) => {
|
||||
let mut value = json!({"backendNodeId": result.backend_node_id});
|
||||
if result.node_id != 0 {
|
||||
value["nodeId"] = json!(result.node_id);
|
||||
}
|
||||
value
|
||||
}
|
||||
AutomationResult::QuerySelector(result) => {
|
||||
if result.multiple {
|
||||
json!({ "nodeIds": result.node_ids })
|
||||
|
||||
@@ -34,11 +34,10 @@ pub(crate) use resolve::{
|
||||
|
||||
#[cfg(test)]
|
||||
use node_payload::{
|
||||
backend_node_id_for_snapshot, node_snapshot_base_payload, node_snapshot_to_cdp_with_limit,
|
||||
};
|
||||
use node_payload::{
|
||||
collect_flattened_node_snapshot, frontend_node_id_for_snapshot, node_snapshot_to_cdp,
|
||||
backend_node_id_for_snapshot, frontend_node_id_for_snapshot, node_snapshot_base_payload,
|
||||
node_snapshot_to_cdp_with_limit,
|
||||
};
|
||||
use node_payload::{collect_flattened_node_snapshot, node_snapshot_to_cdp};
|
||||
|
||||
pub(crate) enum DomCommandDispatchStep {
|
||||
Pending(Box<PendingDomCommandDispatch>),
|
||||
|
||||
@@ -15,6 +15,7 @@ use crate::automation::{
|
||||
DevToolsGetAttributesResult, DevToolsGetOuterHtmlCommand, DevToolsGetOuterHtmlResult,
|
||||
DevToolsGetPropertyResult, DevToolsGetTextResult, DevToolsQuerySelectorResult,
|
||||
DevToolsResolveNodeCommand, DevToolsResolveNodeResult, DevToolsScrollIntoViewIfNeededCommand,
|
||||
FrontendProtocol,
|
||||
};
|
||||
use crate::conn::{CdpConnection, CommandOwnerScope};
|
||||
use moli_core::page::{
|
||||
@@ -52,30 +53,48 @@ pub(super) async fn execute_devtools_dom_command(
|
||||
command.root,
|
||||
&command.selector,
|
||||
command.multiple,
|
||||
command.context.protocol,
|
||||
)
|
||||
.await?;
|
||||
Ok(AutomationResult::QuerySelector(result))
|
||||
}
|
||||
AutomationCommand::GetAttributes(command) => {
|
||||
let result = attributes_command(conn, owner, command.reference).await?;
|
||||
let result =
|
||||
attributes_command(conn, owner, command.reference, command.context.protocol)
|
||||
.await?;
|
||||
Ok(AutomationResult::GetAttributes(result))
|
||||
}
|
||||
AutomationCommand::GetText(command) => {
|
||||
let result = text_command(conn, owner, command.reference).await?;
|
||||
let result =
|
||||
text_command(conn, owner, command.reference, command.context.protocol).await?;
|
||||
Ok(AutomationResult::GetText(result))
|
||||
}
|
||||
AutomationCommand::GetProperty(command) => {
|
||||
let result = property_command(conn, owner, command.reference, &command.name).await?;
|
||||
let result = property_command(
|
||||
conn,
|
||||
owner,
|
||||
command.reference,
|
||||
&command.name,
|
||||
command.context.protocol,
|
||||
)
|
||||
.await?;
|
||||
Ok(AutomationResult::GetProperty(result))
|
||||
}
|
||||
AutomationCommand::GetOuterHtml(command) => {
|
||||
let DevToolsGetOuterHtmlCommand {
|
||||
context: _,
|
||||
context,
|
||||
reference,
|
||||
include_shadow_dom,
|
||||
} = command;
|
||||
let outer_html =
|
||||
outer_html_command(conn, owner, frame_id, reference, include_shadow_dom).await?;
|
||||
let outer_html = outer_html_command(
|
||||
conn,
|
||||
owner,
|
||||
frame_id,
|
||||
reference,
|
||||
include_shadow_dom,
|
||||
context.protocol,
|
||||
)
|
||||
.await?;
|
||||
Ok(AutomationResult::GetOuterHtml(DevToolsGetOuterHtmlResult {
|
||||
outer_html,
|
||||
}))
|
||||
@@ -107,12 +126,14 @@ async fn query_selector_command(
|
||||
root: Option<DevToolsDomNodeReference>,
|
||||
selector: &str,
|
||||
multiple: bool,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<DevToolsQuerySelectorResult, PendingDomCommandStartError> {
|
||||
let renderer_inspector_session_id =
|
||||
conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let include_whitespace = dom_agent_includes_whitespace_for_owner(conn, owner);
|
||||
let root = match root {
|
||||
Some(reference) => reference.into_renderer_reference(renderer_inspector_session_id.clone()),
|
||||
Some(reference) => reference
|
||||
.into_renderer_reference_for_protocol(renderer_inspector_session_id.clone(), protocol),
|
||||
None => moli_core::page::RendererDomNodeReference::BackendNodeId(
|
||||
child_frame_document_root_node_reference(conn, owner, frame_id)
|
||||
.await?
|
||||
@@ -145,6 +166,7 @@ async fn resolve_frontend_node_reference(
|
||||
conn: &mut CdpConnection,
|
||||
owner: &CommandOwnerScope,
|
||||
reference: DevToolsDomNodeReference,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<DevToolsDomNodeReference, PendingDomCommandStartError> {
|
||||
let DevToolsDomNodeReference::FrontendNodeId(frontend_node_id) = reference else {
|
||||
return Ok(reference);
|
||||
@@ -152,9 +174,15 @@ async fn resolve_frontend_node_reference(
|
||||
let renderer_inspector_session_id = renderer_inspector_session_id_for_owner(conn, owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_frontend_node_binding(renderer_inspector_session_id, frontend_node_id)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let pending = if protocol == FrontendProtocol::Cdp {
|
||||
page.start_document_frontend_node_binding(renderer_inspector_session_id, frontend_node_id)
|
||||
} else {
|
||||
page.start_document_internal_frontend_node_binding(
|
||||
renderer_inspector_session_id,
|
||||
frontend_node_id,
|
||||
)
|
||||
}
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let completion = pending
|
||||
.wait()
|
||||
.await
|
||||
@@ -195,12 +223,15 @@ async fn attributes_command(
|
||||
conn: &mut CdpConnection,
|
||||
owner: &CommandOwnerScope,
|
||||
reference: DevToolsDomNodeReference,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<DevToolsGetAttributesResult, PendingDomCommandStartError> {
|
||||
let session = conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_attributes(reference.into_renderer_reference(session))
|
||||
.start_document_node_attributes(
|
||||
reference.into_renderer_reference_for_protocol(session, protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let completion = pending
|
||||
.wait()
|
||||
@@ -215,12 +246,13 @@ async fn text_command(
|
||||
conn: &mut CdpConnection,
|
||||
owner: &CommandOwnerScope,
|
||||
reference: DevToolsDomNodeReference,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<DevToolsGetTextResult, PendingDomCommandStartError> {
|
||||
let session = conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_text(reference.into_renderer_reference(session))
|
||||
.start_document_node_text(reference.into_renderer_reference_for_protocol(session, protocol))
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let completion = pending
|
||||
.wait()
|
||||
@@ -236,12 +268,16 @@ async fn property_command(
|
||||
owner: &CommandOwnerScope,
|
||||
reference: DevToolsDomNodeReference,
|
||||
name: &str,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<DevToolsGetPropertyResult, PendingDomCommandStartError> {
|
||||
let session = conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_property(reference.into_renderer_reference(session), name)
|
||||
.start_document_node_property(
|
||||
reference.into_renderer_reference_for_protocol(session, protocol),
|
||||
name,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let completion = pending
|
||||
.wait()
|
||||
@@ -258,6 +294,7 @@ async fn outer_html_command(
|
||||
frame_id: &str,
|
||||
reference: Option<DevToolsDomNodeReference>,
|
||||
include_shadow_dom: bool,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<String, PendingDomCommandStartError> {
|
||||
let reference = match reference {
|
||||
Some(reference) => reference,
|
||||
@@ -272,7 +309,7 @@ async fn outer_html_command(
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_outer_html_for_node(
|
||||
reference.into_renderer_reference(session),
|
||||
reference.into_renderer_reference_for_protocol(session, protocol),
|
||||
include_shadow_dom,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
@@ -290,7 +327,9 @@ async fn resolve_node_command(
|
||||
owner: &CommandOwnerScope,
|
||||
command: DevToolsResolveNodeCommand,
|
||||
) -> Result<DevToolsResolveNodeResult, PendingDomCommandStartError> {
|
||||
let reference = resolve_frontend_node_reference(conn, owner, command.reference).await?;
|
||||
let reference =
|
||||
resolve_frontend_node_reference(conn, owner, command.reference, command.context.protocol)
|
||||
.await?;
|
||||
let object_group = command.object_group;
|
||||
let renderer_inspector_session_id = renderer_inspector_session_id_for_owner(conn, owner);
|
||||
let remote_object = {
|
||||
@@ -353,14 +392,15 @@ async fn describe_node_command(
|
||||
command: DevToolsDescribeNodeCommand,
|
||||
) -> Result<DevToolsDescribeNodeResult, PendingDomCommandStartError> {
|
||||
let DevToolsDescribeNodeCommand {
|
||||
context: _,
|
||||
context,
|
||||
reference,
|
||||
depth,
|
||||
pierce,
|
||||
} = command;
|
||||
let snapshot = match reference {
|
||||
Some(reference) => {
|
||||
node_snapshot_for_reference(conn, owner, reference, depth, pierce).await?
|
||||
node_snapshot_for_reference(conn, owner, reference, depth, pierce, context.protocol)
|
||||
.await?
|
||||
}
|
||||
None => child_frame_root_node_snapshot(conn, owner, frame_id, depth, pierce).await?,
|
||||
};
|
||||
@@ -376,12 +416,14 @@ async fn node_snapshot_for_reference(
|
||||
reference: DevToolsDomNodeReference,
|
||||
depth: i32,
|
||||
pierce: bool,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<moli_core::page::DocumentNodeSnapshot, PendingDomCommandStartError> {
|
||||
let renderer_inspector_session_id = renderer_inspector_session_id_for_owner(conn, owner);
|
||||
let include_whitespace = dom_agent_includes_whitespace_for_owner(conn, owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let reference = reference.into_renderer_reference(renderer_inspector_session_id.clone());
|
||||
let reference = reference
|
||||
.into_renderer_reference_for_protocol(renderer_inspector_session_id.clone(), protocol);
|
||||
let pending = page
|
||||
.start_document_node_snapshot_for_node_in_inspector_session(
|
||||
renderer_inspector_session_id,
|
||||
@@ -439,7 +481,9 @@ async fn dom_geometry_command(
|
||||
owner: &CommandOwnerScope,
|
||||
command: DevToolsDomGeometryCommand,
|
||||
) -> Result<DevToolsDomGeometryResult, PendingDomCommandStartError> {
|
||||
let geometry = document_geometry_for_reference(conn, owner, command.reference).await?;
|
||||
let geometry =
|
||||
document_geometry_for_reference(conn, owner, command.reference, command.context.protocol)
|
||||
.await?;
|
||||
devtools_dom_geometry_result_from_renderer(command.operation, geometry).map_err(|error| {
|
||||
PendingDomCommandStartError {
|
||||
code: -32000,
|
||||
@@ -459,7 +503,7 @@ async fn scroll_into_view_if_needed_command(
|
||||
// The lightweight headless renderer currently treats scrollIntoViewIfNeeded
|
||||
// as a geometry-validating no-op. Match the top-level path for child-frame
|
||||
// elements: prove the referenced node has geometry, then complete.
|
||||
match document_geometry_for_reference(conn, owner, reference).await? {
|
||||
match document_geometry_for_reference(conn, owner, reference, command.context.protocol).await? {
|
||||
RendererDocumentNodeGeometry::FoundElement { .. } => Ok(()),
|
||||
RendererDocumentNodeGeometry::FoundNonElement { .. }
|
||||
| RendererDocumentNodeGeometry::NoLayoutObject
|
||||
@@ -474,12 +518,15 @@ async fn document_geometry_for_reference(
|
||||
conn: &mut CdpConnection,
|
||||
owner: &CommandOwnerScope,
|
||||
reference: DevToolsDomNodeReference,
|
||||
protocol: FrontendProtocol,
|
||||
) -> Result<RendererDocumentNodeGeometry, PendingDomCommandStartError> {
|
||||
let session = conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_geometry_for_node(reference.into_renderer_reference(session))
|
||||
.start_document_geometry_for_node(
|
||||
reference.into_renderer_reference_for_protocol(session, protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let completion = pending
|
||||
.wait()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#[cfg(test)]
|
||||
pub(super) use moli_protocol_cdp::{
|
||||
backend_node_id_for_snapshot, node_snapshot_base_payload, node_snapshot_to_cdp_with_limit,
|
||||
};
|
||||
pub(super) use moli_protocol_cdp::{
|
||||
collect_flattened_node_snapshot, frontend_node_id_for_snapshot, node_snapshot_to_cdp,
|
||||
backend_node_id_for_snapshot, frontend_node_id_for_snapshot, node_snapshot_base_payload,
|
||||
node_snapshot_to_cdp_with_limit,
|
||||
};
|
||||
pub(super) use moli_protocol_cdp::{collect_flattened_node_snapshot, node_snapshot_to_cdp};
|
||||
|
||||
@@ -157,11 +157,13 @@ async fn describe_node_with_pierce_surfaces_closed_shadow_root_tree() {
|
||||
|
||||
ctx.process_async(json!({"id": 4, "method": "DOM.describeNode", "params": { "nodeId": host_id, "depth": -1 }})).await;
|
||||
let without_pierce = take_response_by_id(&mut ctx, 4);
|
||||
assert!(
|
||||
without_pierce["result"]["node"]
|
||||
.get("shadowRoots")
|
||||
.is_none()
|
||||
);
|
||||
let shallow_shadow_roots = without_pierce["result"]["node"]["shadowRoots"]
|
||||
.as_array()
|
||||
.expect("shallow shadow roots without piercing");
|
||||
assert_eq!(shallow_shadow_roots.len(), 1);
|
||||
assert_eq!(shallow_shadow_roots[0]["shadowRootType"], json!("closed"));
|
||||
assert_eq!(shallow_shadow_roots[0]["childNodeCount"], json!(1));
|
||||
assert!(shallow_shadow_roots[0].get("children").is_none());
|
||||
|
||||
ctx.process_async(json!({"id": 5, "method": "DOM.describeNode", "params": { "nodeId": host_id, "depth": -1, "pierce": true }})).await;
|
||||
let with_pierce = take_response_by_id(&mut ctx, 5);
|
||||
@@ -170,6 +172,10 @@ async fn describe_node_with_pierce_surfaces_closed_shadow_root_tree() {
|
||||
.expect("shadow roots");
|
||||
assert_eq!(shadow_roots.len(), 1);
|
||||
assert_eq!(shadow_roots[0]["shadowRootType"], json!("closed"));
|
||||
assert_eq!(
|
||||
shadow_roots[0]["backendNodeId"],
|
||||
shallow_shadow_roots[0]["backendNodeId"]
|
||||
);
|
||||
assert_eq!(
|
||||
shadow_roots[0]["children"][0]["attributes"],
|
||||
json!(["data-inside", "yes"])
|
||||
|
||||
@@ -44,6 +44,7 @@ use command_start::*;
|
||||
mod pending_command;
|
||||
use pending_command::*;
|
||||
mod node_completion;
|
||||
pub(super) use node_completion::push_set_child_nodes_event_from_snapshots;
|
||||
use node_completion::*;
|
||||
mod protocol_dispatch;
|
||||
pub(crate) use node_completion::complete_pending_dom_command_output_plan;
|
||||
@@ -225,7 +226,9 @@ pub(super) enum PendingDomCommandKind {
|
||||
GetAttributesLive,
|
||||
GetTextLive,
|
||||
GetPropertyLive,
|
||||
RequestNodeObjectReference,
|
||||
RequestNodeObjectReference {
|
||||
top_frame_id: Option<String>,
|
||||
},
|
||||
GetOuterHtmlDocument,
|
||||
GetOuterHtmlObjectReference,
|
||||
GetOuterHtmlNodeReference,
|
||||
@@ -256,7 +259,7 @@ pub(super) enum PendingDomCommandKind {
|
||||
SetChildNodesSnapshotForBackendNode {
|
||||
after: PendingSetChildNodesAfter,
|
||||
top_frame_id: Option<String>,
|
||||
missing_node_message: &'static str,
|
||||
missing_node_message: Option<&'static str>,
|
||||
},
|
||||
QuerySelectorSetChildNodesLive {
|
||||
multiple: bool,
|
||||
|
||||
@@ -490,7 +490,11 @@ pub(super) fn start_devtools_remove_node_command(
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_remove_document_node(command.reference.into_renderer_reference(session))
|
||||
.start_remove_document_node(
|
||||
command
|
||||
.reference
|
||||
.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
command_id,
|
||||
@@ -535,7 +539,11 @@ pub(super) fn start_devtools_dom_geometry_command(
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_geometry_for_node(command.reference.into_renderer_reference(session))
|
||||
.start_document_geometry_for_node(
|
||||
command
|
||||
.reference
|
||||
.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let kind = PendingDomCommandKind::RendererNodeGeometry {
|
||||
operation: command.operation,
|
||||
@@ -590,7 +598,10 @@ pub(super) fn start_devtools_describe_node_command(
|
||||
let include_whitespace = dom_agent_includes_whitespace_for_owner(conn, owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let reference = reference.into_renderer_reference(renderer_inspector_session_id.clone());
|
||||
let reference = reference.into_renderer_reference_for_protocol(
|
||||
renderer_inspector_session_id.clone(),
|
||||
command.context.protocol,
|
||||
);
|
||||
let pending = page
|
||||
.start_document_node_snapshot_for_node_in_inspector_session(
|
||||
renderer_inspector_session_id,
|
||||
@@ -659,17 +670,19 @@ pub(super) fn start_devtools_request_child_nodes_command(
|
||||
} else {
|
||||
command.depth
|
||||
};
|
||||
let missing_node_message = if matches!(
|
||||
command.reference,
|
||||
DevToolsDomNodeReference::FrontendNodeId(_)
|
||||
) {
|
||||
"Could not find node with given id"
|
||||
} else {
|
||||
"InvalidNode"
|
||||
let missing_node_message = match (&command.reference, command.context.protocol) {
|
||||
(DevToolsDomNodeReference::FrontendNodeId(_), crate::automation::FrontendProtocol::Cdp) => {
|
||||
None
|
||||
}
|
||||
(DevToolsDomNodeReference::FrontendNodeId(_), _) => {
|
||||
Some("Could not find node with given id")
|
||||
}
|
||||
(DevToolsDomNodeReference::BackendNodeId(_), _) => Some("InvalidNode"),
|
||||
};
|
||||
let reference = command
|
||||
.reference
|
||||
.into_renderer_reference(renderer_inspector_session_id.clone());
|
||||
let reference = command.reference.into_renderer_reference_for_protocol(
|
||||
renderer_inspector_session_id.clone(),
|
||||
command.context.protocol,
|
||||
);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
@@ -786,7 +799,10 @@ fn start_query_selector_command(
|
||||
|
||||
let publish_node_paths =
|
||||
publish_node_paths && matches!(reference, DevToolsDomNodeReference::FrontendNodeId(_));
|
||||
let reference = reference.into_renderer_reference(renderer_inspector_session_id.clone());
|
||||
let reference = reference.into_renderer_reference_for_protocol(
|
||||
renderer_inspector_session_id.clone(),
|
||||
command.context.protocol,
|
||||
);
|
||||
let page =
|
||||
loaded_page_mut_for_owner(conn, owner).ok_or_else(|| PendingDomCommandStartError {
|
||||
code: -32000,
|
||||
@@ -1188,6 +1204,7 @@ pub(super) fn start_devtools_resolve_node_command(
|
||||
command_id,
|
||||
owner,
|
||||
frontend_node_id,
|
||||
command.context.protocol == crate::automation::FrontendProtocol::Cdp,
|
||||
PendingDomCommandKind::ResolveFrontendNodeForResolveNode {
|
||||
frontend_node_id,
|
||||
requested_execution_context_id: command.execution_context_id,
|
||||
@@ -1357,18 +1374,18 @@ pub(super) fn start_dom_object_reference_operation(
|
||||
match operation {
|
||||
PendingDomObjectReferenceOperation::RequestNode => {
|
||||
let pending = page
|
||||
.start_document_node_snapshot_for_object_id_in_inspector_session(
|
||||
.start_request_document_node_for_object_id(
|
||||
renderer_inspector_session_id,
|
||||
include_whitespace,
|
||||
&object_id,
|
||||
0,
|
||||
false,
|
||||
object_id,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
command_id,
|
||||
owner_scope: owner.clone(),
|
||||
kind: PendingDomCommandKind::RequestNodeObjectReference,
|
||||
kind: PendingDomCommandKind::RequestNodeObjectReference {
|
||||
top_frame_id: top_frame_id_for_owner(conn, owner),
|
||||
},
|
||||
pending,
|
||||
}))
|
||||
}
|
||||
@@ -1413,6 +1430,7 @@ pub(super) fn start_dom_object_reference_operation(
|
||||
&object_id,
|
||||
depth,
|
||||
pierce,
|
||||
false,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
@@ -1515,7 +1533,7 @@ pub(super) fn start_devtools_get_outer_html_command(
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_outer_html_for_node(
|
||||
reference.into_renderer_reference(session),
|
||||
reference.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
include_shadow_dom,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
|
||||
@@ -187,10 +187,6 @@ fn prepare(
|
||||
} else {
|
||||
params.depth
|
||||
};
|
||||
let frontend = matches!(
|
||||
params.reference,
|
||||
DevToolsDomNodeReference::FrontendNodeId(_)
|
||||
);
|
||||
Ok(Operation::new(
|
||||
Command::DocumentChildNodeSnapshotEventsForNode {
|
||||
reference: params.reference.into_renderer_reference(session.clone()),
|
||||
@@ -200,19 +196,15 @@ fn prepare(
|
||||
pierce: params.pierce,
|
||||
},
|
||||
move |reply| match reply {
|
||||
Ok(Reply::OptionalDocumentChildNodeSnapshotEvents(Some(mut snapshots))) => {
|
||||
snapshots.events.truncate(1);
|
||||
Ok(Reply::OptionalDocumentChildNodeSnapshotEvents(Some(snapshots))) => {
|
||||
let mut response = Response::success(json!({}));
|
||||
response.notifications =
|
||||
child_notifications(snapshots, top_frame.as_deref());
|
||||
response
|
||||
}
|
||||
// Like Chromium, missing or unpublished targets are a no-op.
|
||||
Ok(Reply::OptionalDocumentChildNodeSnapshotEvents(None)) => {
|
||||
if frontend {
|
||||
node_not_found()
|
||||
} else {
|
||||
Response::error(-32000, "InvalidNode")
|
||||
}
|
||||
Response::success(json!({}))
|
||||
}
|
||||
Err(error) => Response::error(
|
||||
-32000,
|
||||
@@ -230,9 +222,11 @@ fn prepare(
|
||||
inspector_session_id: session,
|
||||
},
|
||||
|reply| match reply {
|
||||
Ok(Reply::OptionalDocumentNodeReference(Some(node))) => Response::success(
|
||||
json!({"backendNodeId": node.backend_node_id, "nodeId": node.node_id}),
|
||||
),
|
||||
Ok(Reply::OptionalDocumentNodeReference(Some(node))) => {
|
||||
Response::success(get_frame_owner_result_value(
|
||||
&get_frame_owner_result_from_node_reference(node),
|
||||
))
|
||||
}
|
||||
Ok(Reply::OptionalDocumentNodeReference(None)) => Response::error(
|
||||
-32000,
|
||||
"Frame with the given id does not belong to the target.",
|
||||
@@ -253,7 +247,7 @@ fn prepare(
|
||||
.object_id
|
||||
.as_deref()
|
||||
.and_then(|id| cached_dom_remote_object_node_for_owner(conn, &owner, id));
|
||||
let project = move |reply| project_node_snapshot(reply, top_frame, cached, false);
|
||||
let project = move |reply| project_node_snapshot(reply, top_frame, cached);
|
||||
if let Some(object) = params.reference.object_id {
|
||||
let object = dom_object_reference_id_for_owner(conn, &owner, &object.into());
|
||||
Ok(Operation::new(
|
||||
@@ -263,6 +257,7 @@ fn prepare(
|
||||
object,
|
||||
params.depth,
|
||||
params.pierce,
|
||||
false,
|
||||
),
|
||||
project,
|
||||
))
|
||||
@@ -291,7 +286,7 @@ fn prepare(
|
||||
.flatten()
|
||||
.ok_or_else(StartError::invalid_params)?;
|
||||
Ok(Operation::new(
|
||||
Command::document_node_snapshot_for_object_id(
|
||||
Command::request_document_node_for_object_id(
|
||||
session,
|
||||
whitespace,
|
||||
dom_object_reference_id_for_owner(
|
||||
@@ -299,10 +294,26 @@ fn prepare(
|
||||
&owner,
|
||||
¶ms.object_id.inner().to_owned().into(),
|
||||
),
|
||||
0,
|
||||
false,
|
||||
),
|
||||
move |reply| project_node_snapshot(reply, top_frame, None, true),
|
||||
move |reply| match reply {
|
||||
Ok(Reply::DocumentNodeRequested {
|
||||
node_id,
|
||||
child_node_snapshot_events,
|
||||
}) => {
|
||||
let mut response = node_id
|
||||
.map(|id| Response::success(json!({"nodeId": id})))
|
||||
.unwrap_or_else(node_not_found);
|
||||
if let Some(events) = child_node_snapshot_events {
|
||||
response.notifications =
|
||||
child_notifications(events, top_frame.as_deref());
|
||||
}
|
||||
response
|
||||
}
|
||||
Err(error) => {
|
||||
Response::error(-32000, format!("Could not request node object: {error}"))
|
||||
}
|
||||
_ => unreachable!("DOM request node reply"),
|
||||
},
|
||||
))
|
||||
}
|
||||
DomAction::GetOuterHtml => {
|
||||
@@ -442,17 +453,25 @@ fn prepare(
|
||||
from_index: params.from_index,
|
||||
to_index: params.to_index,
|
||||
},
|
||||
|reply| match reply {
|
||||
Ok(Reply::DocumentSearchResults(
|
||||
RendererDomSearchResultsResolution::Found(nodes),
|
||||
)) => Response::success(
|
||||
json!({"nodeIds": nodes.into_iter().map(|node| node.frontend_node_id).collect::<Vec<_>>()}),
|
||||
),
|
||||
Ok(Reply::DocumentSearchResults(
|
||||
RendererDomSearchResultsResolution::SearchResultNotFound,
|
||||
)) => Response::error(-32000, "No search session with given id found"),
|
||||
Ok(Reply::DocumentSearchResults(_)) => {
|
||||
Response::error(-32000, "Invalid search result range")
|
||||
move |reply| match reply {
|
||||
Ok(Reply::DocumentSearchResults {
|
||||
resolution,
|
||||
child_node_snapshot_events,
|
||||
}) => {
|
||||
let mut response = match resolution {
|
||||
RendererDomSearchResultsResolution::Found(nodes) => Response::success(
|
||||
json!({"nodeIds": nodes.into_iter().map(|node| node.frontend_node_id).collect::<Vec<_>>()}),
|
||||
),
|
||||
RendererDomSearchResultsResolution::SearchResultNotFound => {
|
||||
Response::error(-32000, "No search session with given id found")
|
||||
}
|
||||
_ => Response::error(-32000, "Invalid search result range"),
|
||||
};
|
||||
if let Some(snapshots) = child_node_snapshot_events {
|
||||
response.notifications =
|
||||
child_notifications(snapshots, top_frame.as_deref());
|
||||
}
|
||||
response
|
||||
}
|
||||
Err(error) => Response::error(
|
||||
-32000,
|
||||
@@ -493,13 +512,9 @@ fn project_node_snapshot(
|
||||
reply: anyhow::Result<Reply>,
|
||||
top_frame: Option<String>,
|
||||
cached: Option<Value>,
|
||||
request: bool,
|
||||
) -> Response {
|
||||
match reply {
|
||||
Ok(Reply::OptionalDocumentNodeObjectSnapshot(snapshot)) => match *snapshot {
|
||||
Some(snapshot) if request => frontend_node_id_for_snapshot(&snapshot.snapshot)
|
||||
.map(|id| Response::success(json!({"nodeId": id})))
|
||||
.unwrap_or_else(node_not_found),
|
||||
Some(snapshot) => node_for_describe_node_object_snapshot(
|
||||
&snapshot.snapshot,
|
||||
snapshot.frame_id.as_deref(),
|
||||
@@ -511,13 +526,7 @@ fn project_node_snapshot(
|
||||
.map(|node| Response::success(json!({"node": node})))
|
||||
.unwrap_or_else(node_not_found),
|
||||
},
|
||||
Err(error) => Response::error(
|
||||
-32000,
|
||||
format!(
|
||||
"Could not {} node object: {error}",
|
||||
if request { "request" } else { "describe" }
|
||||
),
|
||||
),
|
||||
Err(error) => Response::error(-32000, format!("Could not describe node object: {error}")),
|
||||
_ => unreachable!("DOM node snapshot reply"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,7 @@ fn resolve(
|
||||
id.clone(),
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
move |reply| match reply {
|
||||
Ok(Reply::OptionalDocumentNodeObjectSnapshot(snapshot)) => {
|
||||
|
||||
@@ -183,10 +183,13 @@ pub(super) fn complete_pending_dom_command_result(
|
||||
};
|
||||
match page.finish_document_child_node_snapshot_events(completion) {
|
||||
Ok(Some(_)) => Ok(AutomationResult::Empty),
|
||||
Ok(None) => Err(DevToolsError::from(PendingDomCommandStartError {
|
||||
code: -32000,
|
||||
message: missing_node_message.to_owned(),
|
||||
})),
|
||||
Ok(None) => match missing_node_message {
|
||||
Some(message) => Err(DevToolsError::from(PendingDomCommandStartError {
|
||||
code: -32000,
|
||||
message: message.to_owned(),
|
||||
})),
|
||||
None => Ok(AutomationResult::Empty),
|
||||
},
|
||||
Err(error) => Err(DevToolsError::new(
|
||||
DevToolsErrorKind::Internal,
|
||||
format!("Could not capture child node snapshots: {error}"),
|
||||
@@ -295,6 +298,7 @@ pub(super) fn complete_pending_dom_command_result(
|
||||
&cache_object_id,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
) {
|
||||
Ok(pending) => {
|
||||
DevToolsDomCommandTaskStep::Pending(Box::new(PendingDomCommandDispatch {
|
||||
@@ -561,10 +565,13 @@ pub(super) fn get_frame_owner_result_from_node_reference(
|
||||
}
|
||||
|
||||
pub(super) fn get_frame_owner_result_value(result: &DevToolsGetFrameOwnerResult) -> Value {
|
||||
json!({
|
||||
"nodeId": result.node_id,
|
||||
let mut value = json!({
|
||||
"backendNodeId": result.backend_node_id,
|
||||
})
|
||||
});
|
||||
if result.node_id != 0 {
|
||||
value["nodeId"] = json!(result.node_id);
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
pub(super) fn live_node_not_element_error() -> PendingDomCommandStartError {
|
||||
@@ -1139,13 +1146,17 @@ pub(super) fn complete_set_child_nodes_snapshot_for_backend_node(
|
||||
completion: CompletedPageCommand,
|
||||
after: PendingSetChildNodesAfter,
|
||||
top_frame_id: Option<String>,
|
||||
missing_node_message: &'static str,
|
||||
missing_node_message: Option<&'static str>,
|
||||
out: &mut DomCommandOutput,
|
||||
) -> DomCommandTaskStep {
|
||||
let snapshot_events = match page.finish_document_child_node_snapshot_events(completion) {
|
||||
Ok(Some(events)) => events,
|
||||
Ok(None) => {
|
||||
out.push_error(-32000, missing_node_message);
|
||||
if let Some(message) = missing_node_message {
|
||||
out.push_error(-32000, message);
|
||||
} else {
|
||||
complete_set_child_nodes_after(after, out);
|
||||
}
|
||||
return DomCommandTaskStep::Complete;
|
||||
}
|
||||
Err(error) => {
|
||||
@@ -1156,19 +1167,18 @@ pub(super) fn complete_set_child_nodes_snapshot_for_backend_node(
|
||||
return DomCommandTaskStep::Complete;
|
||||
}
|
||||
};
|
||||
let Some(event) = snapshot_events.events.into_iter().next() else {
|
||||
complete_set_child_nodes_after(after, out);
|
||||
return DomCommandTaskStep::Complete;
|
||||
};
|
||||
complete_set_child_nodes_from_snapshots(
|
||||
session_id,
|
||||
after,
|
||||
event.parent_frontend_node_id,
|
||||
event.snapshots,
|
||||
snapshot_events.top_snapshot_node_id,
|
||||
top_frame_id,
|
||||
out,
|
||||
)
|
||||
for event in snapshot_events.events {
|
||||
push_set_child_nodes_event_from_snapshots(
|
||||
session_id,
|
||||
event.parent_frontend_node_id,
|
||||
&event.snapshots,
|
||||
snapshot_events.top_snapshot_node_id,
|
||||
top_frame_id.as_deref(),
|
||||
out,
|
||||
);
|
||||
}
|
||||
complete_set_child_nodes_after(after, out);
|
||||
DomCommandTaskStep::Complete
|
||||
}
|
||||
|
||||
pub(super) fn complete_query_selector_set_child_nodes_live(
|
||||
@@ -1212,27 +1222,6 @@ pub(super) fn complete_query_selector_set_child_nodes_live(
|
||||
DomCommandTaskStep::Complete
|
||||
}
|
||||
|
||||
pub(super) fn complete_set_child_nodes_from_snapshots(
|
||||
session_id: Option<&str>,
|
||||
after: PendingSetChildNodesAfter,
|
||||
parent_frontend_node_id: u32,
|
||||
snapshots: Vec<DocumentNodeSnapshot>,
|
||||
top_snapshot_node_id: DocumentSnapshotNodeId,
|
||||
top_frame_id: Option<String>,
|
||||
out: &mut DomCommandOutput,
|
||||
) -> DomCommandTaskStep {
|
||||
push_set_child_nodes_event_from_snapshots(
|
||||
session_id,
|
||||
parent_frontend_node_id,
|
||||
&snapshots,
|
||||
top_snapshot_node_id,
|
||||
top_frame_id.as_deref(),
|
||||
out,
|
||||
);
|
||||
complete_set_child_nodes_after(after, out);
|
||||
DomCommandTaskStep::Complete
|
||||
}
|
||||
|
||||
pub(super) fn complete_set_child_nodes_after(
|
||||
after: PendingSetChildNodesAfter,
|
||||
out: &mut DomCommandOutput,
|
||||
@@ -1343,24 +1332,31 @@ pub(super) fn top_snapshot_node_id_for_live_object_snapshot(
|
||||
}
|
||||
|
||||
pub(super) fn complete_request_node_object_reference(
|
||||
_session_id: Option<&str>,
|
||||
session_id: Option<&str>,
|
||||
page: &mut Page,
|
||||
completion: CompletedPageCommand,
|
||||
top_frame_id: Option<String>,
|
||||
out: &mut DomCommandOutput,
|
||||
) -> DomCommandTaskStep {
|
||||
match page.finish_document_node_snapshot_for_object_id(completion) {
|
||||
Ok(Some(object_snapshot)) => {
|
||||
let Some(frontend_node_id) =
|
||||
super::frontend_node_id_for_snapshot(&object_snapshot.snapshot)
|
||||
else {
|
||||
out.push_error(-32000, "Could not find node with given id");
|
||||
return DomCommandTaskStep::Complete;
|
||||
};
|
||||
match page.finish_request_document_node_for_object_id(completion) {
|
||||
Ok((Some(frontend_node_id), snapshot_events)) => {
|
||||
if let Some(snapshot_events) = snapshot_events {
|
||||
for event in snapshot_events.events {
|
||||
push_set_child_nodes_event_from_snapshots(
|
||||
session_id,
|
||||
event.parent_frontend_node_id,
|
||||
&event.snapshots,
|
||||
snapshot_events.top_snapshot_node_id,
|
||||
top_frame_id.as_deref(),
|
||||
out,
|
||||
);
|
||||
}
|
||||
}
|
||||
out.push_result(json!({
|
||||
"nodeId": frontend_node_id
|
||||
}));
|
||||
}
|
||||
Ok(None) => out.push_error(-32000, "Could not find node with given id"),
|
||||
Ok((None, _)) => out.push_error(-32000, "Could not find node with given id"),
|
||||
Err(error) => {
|
||||
out.push_error(-32000, format!("Could not request node object: {error}"));
|
||||
}
|
||||
|
||||
@@ -281,8 +281,14 @@ pub(super) fn complete_pending_dom_command(
|
||||
PendingDomCommandKind::GetPropertyLive => {
|
||||
return complete_get_property_live(page, completion, out);
|
||||
}
|
||||
PendingDomCommandKind::RequestNodeObjectReference => {
|
||||
return complete_request_node_object_reference(session_id, page, completion, out);
|
||||
PendingDomCommandKind::RequestNodeObjectReference { top_frame_id } => {
|
||||
return complete_request_node_object_reference(
|
||||
session_id,
|
||||
page,
|
||||
completion,
|
||||
top_frame_id,
|
||||
out,
|
||||
);
|
||||
}
|
||||
PendingDomCommandKind::GetOuterHtmlDocument => {
|
||||
return complete_get_outer_html_document(page, completion, out);
|
||||
@@ -381,6 +387,7 @@ pub(super) fn complete_pending_dom_command(
|
||||
&cache_object_id,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
) {
|
||||
Ok(pending) => {
|
||||
DomCommandTaskStep::Pending(Box::new(PendingDomCommandDispatch {
|
||||
|
||||
@@ -31,7 +31,11 @@ pub(super) fn start_devtools_get_attributes_command(
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_attributes(command.reference.into_renderer_reference(session))
|
||||
.start_document_node_attributes(
|
||||
command
|
||||
.reference
|
||||
.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
command_id,
|
||||
@@ -55,15 +59,22 @@ pub(super) fn start_document_frontend_node_binding_command(
|
||||
command_id: Option<u64>,
|
||||
owner: &CommandOwnerScope,
|
||||
frontend_node_id: u32,
|
||||
require_published: bool,
|
||||
kind: PendingDomCommandKind,
|
||||
) -> Result<Option<PendingDomCommandDispatch>, PendingDomCommandStartError> {
|
||||
let renderer_inspector_session_id =
|
||||
conn.target_renderer_runtime_inspector_session_id_for_owner(owner);
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_frontend_node_binding(renderer_inspector_session_id, frontend_node_id)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
let pending = if require_published {
|
||||
page.start_document_frontend_node_binding(renderer_inspector_session_id, frontend_node_id)
|
||||
} else {
|
||||
page.start_document_internal_frontend_node_binding(
|
||||
renderer_inspector_session_id,
|
||||
frontend_node_id,
|
||||
)
|
||||
}
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
command_id,
|
||||
owner_scope: owner.clone(),
|
||||
@@ -82,7 +93,11 @@ pub(super) fn start_devtools_get_text_command(
|
||||
let page = loaded_page_mut_for_owner(conn, owner)
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_text(command.reference.into_renderer_reference(session))
|
||||
.start_document_node_text(
|
||||
command
|
||||
.reference
|
||||
.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
Ok(Some(PendingDomCommandDispatch {
|
||||
command_id,
|
||||
@@ -103,7 +118,9 @@ pub(super) fn start_devtools_get_property_command(
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_document_node_property(
|
||||
command.reference.into_renderer_reference(session),
|
||||
command
|
||||
.reference
|
||||
.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
&command.name,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
@@ -541,7 +558,7 @@ pub(super) fn start_devtools_scroll_into_view_if_needed_command(
|
||||
.ok_or_else(PendingDomCommandStartError::no_document_loaded)?;
|
||||
let pending = page
|
||||
.start_scroll_node_into_view_if_needed(
|
||||
reference.into_renderer_reference(session),
|
||||
reference.into_renderer_reference_for_protocol(session, command.context.protocol),
|
||||
command.rect,
|
||||
)
|
||||
.map_err(PendingDomCommandStartError::renderer_error)?;
|
||||
|
||||
@@ -1520,20 +1520,43 @@ async fn typed_child_capture_and_removal_use_scoped_node_references() {
|
||||
);
|
||||
}
|
||||
let missing = DevToolsDomNodeReference::FrontendNodeId(u32::MAX);
|
||||
for command in [
|
||||
AutomationCommand::RequestChildNodes(crate::automation::DevToolsRequestChildNodesCommand {
|
||||
context: context.clone(),
|
||||
reference: missing.clone(),
|
||||
depth: 1,
|
||||
pierce: false,
|
||||
}),
|
||||
for protocol in [
|
||||
FrontendProtocol::Cdp,
|
||||
FrontendProtocol::WebDriverClassic,
|
||||
FrontendProtocol::WebDriverBidi,
|
||||
] {
|
||||
let result = execute(
|
||||
&mut ctx.conn,
|
||||
AutomationCommand::RequestChildNodes(
|
||||
crate::automation::DevToolsRequestChildNodesCommand {
|
||||
context: AutomationContext {
|
||||
protocol,
|
||||
..context.clone()
|
||||
},
|
||||
reference: missing.clone(),
|
||||
depth: 1,
|
||||
pierce: false,
|
||||
},
|
||||
),
|
||||
)
|
||||
.await;
|
||||
if protocol == FrontendProtocol::Cdp {
|
||||
assert!(matches!(result, Ok(AutomationResult::Empty)));
|
||||
continue;
|
||||
}
|
||||
let error = result.unwrap_err();
|
||||
assert_eq!(error.kind, DevToolsErrorKind::NoSuchNode);
|
||||
assert_eq!(error.message, "Could not find node with given id");
|
||||
}
|
||||
let error = execute(
|
||||
&mut ctx.conn,
|
||||
AutomationCommand::RemoveNode(crate::automation::DevToolsRemoveNodeCommand {
|
||||
context,
|
||||
reference: missing,
|
||||
}),
|
||||
] {
|
||||
let error = execute(&mut ctx.conn, command).await.unwrap_err();
|
||||
assert_eq!(error.kind, DevToolsErrorKind::NoSuchNode);
|
||||
assert_eq!(error.message, "Could not find node with given id");
|
||||
}
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(error.kind, DevToolsErrorKind::NoSuchNode);
|
||||
assert_eq!(error.message, "Could not find node with given id");
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ pub(super) fn complete_get_search_results_live(
|
||||
completion: CompletedPageCommand,
|
||||
out: &mut DomCommandOutput,
|
||||
) -> DomCommandTaskStep {
|
||||
let resolution = {
|
||||
let (resolution, snapshot_events) = {
|
||||
let Some(page) = super::loaded_page_mut_for_owner(conn, owner) else {
|
||||
out.push_error(-32000, "NoDocumentLoaded");
|
||||
return DomCommandTaskStep::Complete;
|
||||
@@ -229,6 +229,19 @@ pub(super) fn complete_get_search_results_live(
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(snapshot_events) = snapshot_events {
|
||||
let top_frame_id = super::top_frame_id_for_owner(conn, owner);
|
||||
for event in snapshot_events.events {
|
||||
super::resolve::push_set_child_nodes_event_from_snapshots(
|
||||
owner.session_id(),
|
||||
event.parent_frontend_node_id,
|
||||
&event.snapshots,
|
||||
snapshot_events.top_snapshot_node_id,
|
||||
top_frame_id.as_deref(),
|
||||
out,
|
||||
);
|
||||
}
|
||||
}
|
||||
match resolution {
|
||||
RendererDomSearchResultsResolution::Found(nodes) => {
|
||||
let node_ids = nodes
|
||||
|
||||
@@ -2855,14 +2855,14 @@ async fn get_document_depth_one_matches_registry_writer_shape() {
|
||||
"params": { "nodeId": 1, "depth": 1 }
|
||||
}))
|
||||
.await;
|
||||
let document_children = ctx.take_first_matching("document child nodes", |message| {
|
||||
message["method"] == json!("DOM.setChildNodes") && message["params"]["parentId"] == json!(1)
|
||||
});
|
||||
assert_eq!(document_children["method"], "DOM.setChildNodes");
|
||||
assert_eq!(document_children["params"]["parentId"], json!(1));
|
||||
let root_children = document_children["params"]["nodes"]
|
||||
.as_array()
|
||||
.expect("document children");
|
||||
ctx.expect_result(29, json!({}), None);
|
||||
assert!(
|
||||
ctx.take_all()
|
||||
.iter()
|
||||
.all(|message| message["method"] != json!("DOM.setChildNodes")),
|
||||
"DOM.getDocument already published the document children"
|
||||
);
|
||||
let root_children = root["children"].as_array().expect("document children");
|
||||
assert_eq!(root_children.len(), 1);
|
||||
assert_eq!(root_children[0]["nodeId"], json!(2));
|
||||
let html_backend_node_id = root_children[0]["backendNodeId"]
|
||||
@@ -2871,11 +2871,10 @@ async fn get_document_depth_one_matches_registry_writer_shape() {
|
||||
.expect("html backend node id");
|
||||
assert!(
|
||||
moli_core::page::is_renderer_backend_node_id(html_backend_node_id),
|
||||
"DOM.requestChildNodes should assign renderer backend ids to live child payload: {document_children}"
|
||||
"DOM.getDocument should assign renderer backend ids to live child payload: {root}"
|
||||
);
|
||||
assert_eq!(root_children[0]["nodeName"], json!("HTML"));
|
||||
assert_eq!(root_children[0]["childNodeCount"], json!(2));
|
||||
ctx.expect_result(29, json!({}), None);
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 30,
|
||||
@@ -3201,12 +3200,11 @@ async fn document_query_search_and_frame_owner_paths_use_explicit_dispatch() {
|
||||
"DOM.requestChildNodes without child frame owners should not use legacy fallback",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(request_child_nodes[0]["method"], json!("DOM.setChildNodes"));
|
||||
assert_eq!(
|
||||
request_child_nodes[0]["params"]["parentId"],
|
||||
json!(html_node_id)
|
||||
request_child_nodes,
|
||||
vec![json!({ "id": 6, "result": {} })],
|
||||
"requesting already expanded children must not replace the subtree"
|
||||
);
|
||||
assert_eq!(request_child_nodes[1], json!({ "id": 6, "result": {} }));
|
||||
|
||||
let perform_search = complete_command_dispatch_without_legacy_fallback_for_test(
|
||||
&mut ctx,
|
||||
@@ -3528,6 +3526,14 @@ async fn get_frame_owner_returns_iframe_node_for_child_frame() {
|
||||
.expect("child frame id")
|
||||
.to_owned();
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 90,
|
||||
"method": "DOM.getDocument",
|
||||
"params": { "depth": 1 }
|
||||
}))
|
||||
.await;
|
||||
take_response_by_id(&mut ctx, 90);
|
||||
|
||||
let owner_raw = json!({
|
||||
"id": 3,
|
||||
"method": "DOM.getFrameOwner",
|
||||
@@ -3601,6 +3607,14 @@ async fn async_dispatch_get_document_and_get_frame_owner_surface_child_frame_met
|
||||
.expect("child frame id")
|
||||
.to_owned();
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 90,
|
||||
"method": "DOM.getDocument",
|
||||
"params": { "depth": 1 }
|
||||
}))
|
||||
.await;
|
||||
take_response_by_id(&mut ctx, 90);
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 3,
|
||||
"method": "DOM.getFrameOwner",
|
||||
@@ -3914,6 +3928,8 @@ async fn child_frame_get_outer_html_preserves_session_and_shadow_inclusion() {
|
||||
.as_str()
|
||||
.expect("attached session id")
|
||||
.to_owned();
|
||||
ctx.process_async(json!({"id":4,"sessionId":attached_session_id,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}})).await;
|
||||
let _ = take_response_by_id(&mut ctx, 4);
|
||||
let cdp_context = AutomationContext {
|
||||
protocol: FrontendProtocol::Cdp,
|
||||
session_id: Some(DevToolsSessionId::from(attached_session_id.as_str())),
|
||||
@@ -4678,15 +4694,20 @@ async fn protocol_neutral_describe_node_targets_child_frame_with_pierce() {
|
||||
let AutomationResult::DescribeNode(result) = result else {
|
||||
panic!("expected describe node result");
|
||||
};
|
||||
assert_eq!(
|
||||
result.node["nodeId"],
|
||||
json!(0),
|
||||
"an internal query allocates a handle without publishing a DOM tree"
|
||||
);
|
||||
let shadow_roots = result.node["shadowRoots"]
|
||||
.as_array()
|
||||
.unwrap_or_else(|| panic!("child frame host should expose closed shadow root: {result:?}"));
|
||||
assert_eq!(shadow_roots.len(), 1);
|
||||
assert_eq!(shadow_roots[0]["shadowRootType"], json!("closed"));
|
||||
let shadow_root_node_id = shadow_roots[0]["nodeId"]
|
||||
let shadow_root_backend_id = shadow_roots[0]["backendNodeId"]
|
||||
.as_u64()
|
||||
.and_then(|id| u32::try_from(id).ok())
|
||||
.expect("child-frame shadow root should have frontend node id");
|
||||
.expect("child-frame shadow root should have backend node id");
|
||||
let inside = node_array_element_by_attribute(&shadow_roots[0]["children"], "id", "inside");
|
||||
assert!(
|
||||
inside.is_some(),
|
||||
@@ -4698,8 +4719,8 @@ async fn protocol_neutral_describe_node_targets_child_frame_with_pierce() {
|
||||
.execute_automation_command(AutomationCommand::DescribeNode(
|
||||
DevToolsDescribeNodeCommand {
|
||||
context,
|
||||
reference: Some(DevToolsDomNodeReference::FrontendNodeId(
|
||||
shadow_root_node_id,
|
||||
reference: Some(DevToolsDomNodeReference::BackendNodeId(
|
||||
shadow_root_backend_id,
|
||||
)),
|
||||
depth: 0,
|
||||
pierce: false,
|
||||
@@ -4715,10 +4736,14 @@ async fn protocol_neutral_describe_node_targets_child_frame_with_pierce() {
|
||||
assert_eq!(
|
||||
result.node["shadowRootType"],
|
||||
json!("closed"),
|
||||
"expected shadow root frontend id to describe the shadow root node: {:?}",
|
||||
"expected shadow root backend id to describe the shadow root node: {:?}",
|
||||
result.node
|
||||
);
|
||||
assert_eq!(result.node["parentId"], json!(host_node_id));
|
||||
assert_eq!(
|
||||
result.node["parentId"],
|
||||
json!(0),
|
||||
"describeNode must not expose an unpublished parent binding"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
@@ -4834,8 +4859,9 @@ async fn get_document_surfaces_child_frame_id_for_iframe_node() {
|
||||
|
||||
assert_eq!(iframe["frameId"], json!(child_frame_id));
|
||||
assert!(
|
||||
iframe.get("contentDocument").is_none(),
|
||||
"contentDocument requires pierce=true: {iframe}"
|
||||
iframe["contentDocument"]["nodeType"] == json!(9)
|
||||
&& iframe["contentDocument"].get("children").is_none(),
|
||||
"without pierce the frame owner exposes only its shallow Document: {iframe}"
|
||||
);
|
||||
assert!(peer_div.get("frameId").is_none());
|
||||
}
|
||||
|
||||
@@ -1344,9 +1344,8 @@ async fn get_node_for_location_uses_real_layout_hit_testing() {
|
||||
json!(expected_backend_node_id)
|
||||
);
|
||||
assert!(
|
||||
response["result"]["nodeId"]
|
||||
.as_u64()
|
||||
.is_some_and(|id| id > 0)
|
||||
response["result"].get("nodeId").is_none(),
|
||||
"a hit before DOM.getDocument must expose backend identity only: {response}"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -753,7 +753,7 @@ async fn inspector_depth_boundary_includes_an_only_text_child_like_chromium() {
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn request_child_nodes_rejects_zero_depth_and_invalid_node() {
|
||||
async fn request_child_nodes_rejects_zero_depth_and_ignores_unknown_node() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(
|
||||
@@ -781,5 +781,5 @@ async fn request_child_nodes_rejects_zero_depth_and_invalid_node() {
|
||||
"params": { "nodeId": 999, "depth": 1 }
|
||||
}))
|
||||
.await;
|
||||
ctx.expect_error(3, -32000, "Could not find node with given id");
|
||||
assert_eq!(ctx.take_all(), vec![json!({"id":3,"result":{}})]);
|
||||
}
|
||||
|
||||
@@ -996,6 +996,11 @@ async fn set_node_name_allows_xml_processing_instruction_target_like_chromium()
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1, "<!doctype html><html><body></body></html>").await;
|
||||
ctx.process_async(
|
||||
json!({"id":90001,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 2,
|
||||
@@ -3640,7 +3645,7 @@ async fn query_selector_all_low_root_node_id_misses_without_frontend_binding() {
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn request_child_nodes_low_node_id_misses_without_frontend_binding() {
|
||||
async fn request_child_nodes_ignores_low_node_id_without_frontend_binding() {
|
||||
let mut ctx = TestContext::new();
|
||||
let (node_id, mutation_completion) =
|
||||
append_live_node_without_refreshing_page_snapshot(&mut ctx).await;
|
||||
@@ -3655,16 +3660,7 @@ async fn request_child_nodes_low_node_id_misses_without_frontend_binding() {
|
||||
panic!("DOM.requestChildNodes should verify frontend node binding in renderer");
|
||||
};
|
||||
let messages = complete_pending_command_task_for_test(&mut ctx, *pending).await;
|
||||
assert_eq!(
|
||||
messages,
|
||||
vec![json!({
|
||||
"id": 3,
|
||||
"error": {
|
||||
"code": -32000,
|
||||
"message": "Could not find node with given id"
|
||||
}
|
||||
})]
|
||||
);
|
||||
assert_eq!(messages, vec![json!({"id": 3, "result": {}})]);
|
||||
|
||||
let page = loaded_page_mut_for_test(&mut ctx);
|
||||
let _ = page
|
||||
@@ -4029,6 +4025,11 @@ async fn request_node_resolves_runtime_object_id() {
|
||||
"<!doctype html><html><body><div id='box'>ok</div></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(
|
||||
json!({"id":90001,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
let execution_context_id =
|
||||
enable_runtime_and_take_execution_context_id_async(&mut ctx, 10).await;
|
||||
@@ -4257,6 +4258,11 @@ async fn async_dispatch_request_node_resolves_runtime_object_id() {
|
||||
"<!doctype html><html><body><div id='box'>ok</div></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(
|
||||
json!({"id":90001,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
let _ = enable_runtime_and_take_execution_context_id_async(&mut ctx, 10).await;
|
||||
let _ = ctx.take_all();
|
||||
@@ -4307,6 +4313,11 @@ async fn object_snapshot_producers_register_renderer_frontend_bindings() {
|
||||
"<!doctype html><html><body><section id='request-target' data-source='request'></section><article id='describe-target' data-source='describe'></article></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(
|
||||
json!({"id":90001,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
let _ = enable_runtime_and_take_execution_context_id_async(&mut ctx, 10).await;
|
||||
let _ = ctx.take_all();
|
||||
@@ -4451,6 +4462,10 @@ async fn request_node_object_id_reads_live_document_after_document_open_replacem
|
||||
)
|
||||
.await;
|
||||
|
||||
ctx.process_async(json!({"id":90001,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 13,
|
||||
"method": "DOM.requestNode",
|
||||
@@ -4762,7 +4777,9 @@ async fn query_selector_accepts_renderer_backend_shadow_root_reference() {
|
||||
);
|
||||
|
||||
let context = AutomationContext {
|
||||
protocol: FrontendProtocol::Cdp,
|
||||
// Backend-root queries exercise the internal automation API,
|
||||
// without publishing a CDP DOM tree.
|
||||
protocol: FrontendProtocol::WebDriverClassic,
|
||||
session_id: None,
|
||||
target_id: Some(DevToolsTargetId::from("TID-1")),
|
||||
browser_context_id: Some(DevToolsBrowserContextId::from("BID-A")),
|
||||
@@ -4861,7 +4878,9 @@ async fn child_frame_query_selector_accepts_renderer_backend_shadow_root_referen
|
||||
let _ = take_response_by_id(&mut ctx, 5);
|
||||
|
||||
let context = AutomationContext {
|
||||
protocol: FrontendProtocol::Cdp,
|
||||
// Backend-root queries exercise the internal automation API,
|
||||
// without publishing a CDP DOM tree.
|
||||
protocol: FrontendProtocol::WebDriverClassic,
|
||||
session_id: None,
|
||||
target_id: Some(DevToolsTargetId::from(child_frame_id.as_str())),
|
||||
browser_context_id: Some(DevToolsBrowserContextId::from("BID-CHILD-SHADOW-QUERY")),
|
||||
@@ -6158,6 +6177,12 @@ async fn resolve_node_with_child_default_execution_context_uses_child_document_b
|
||||
"<!doctype html><html><body><input value='main'><iframe srcdoc=\"<body><div id='child' style='left:3px;top:4px;width:9px;height:11px'>child-body</div></body>\"></iframe></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(
|
||||
json!({"id":90001,"method":"DOM.getDocument","params":{"depth":-1,"pierce":true}}),
|
||||
)
|
||||
.await;
|
||||
let _ = take_response_by_id(&mut ctx, 90001);
|
||||
|
||||
ctx.capture_fixture_layout(None).await;
|
||||
let child_frame_id = child_frame_id_for_single_iframe_async(&mut ctx, 2).await;
|
||||
|
||||
|
||||
@@ -1,5 +1,651 @@
|
||||
use super::*;
|
||||
|
||||
fn remember_search_frontend_nodes(node: &Value, known: &mut std::collections::BTreeSet<u64>) {
|
||||
if let Some(parent) = node["parentId"].as_u64() {
|
||||
assert!(
|
||||
parent == 0 || known.contains(&parent),
|
||||
"unknown snapshot parent: {node}"
|
||||
);
|
||||
}
|
||||
known.insert(node["nodeId"].as_u64().expect("frontend node id"));
|
||||
for field in ["children", "shadowRoots", "pseudoElements"] {
|
||||
if let Some(children) = node[field].as_array() {
|
||||
for child in children {
|
||||
remember_search_frontend_nodes(child, known);
|
||||
}
|
||||
}
|
||||
}
|
||||
for field in ["contentDocument", "templateContent"] {
|
||||
if node[field].is_object() {
|
||||
remember_search_frontend_nodes(&node[field], known);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_frontend_node_publication(
|
||||
messages: &[Value],
|
||||
command_id: u64,
|
||||
known: &mut std::collections::BTreeSet<u64>,
|
||||
) -> Vec<Value> {
|
||||
let (reply, events) = messages.split_last().expect("search reply");
|
||||
assert_eq!(reply["id"], command_id);
|
||||
remember_search_frontend_events(events, known);
|
||||
let ids = reply["result"]["nodeIds"]
|
||||
.as_array()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| vec![reply["result"]["nodeId"].clone()]);
|
||||
for id in &ids {
|
||||
let id = id.as_u64().expect("node id");
|
||||
assert!(
|
||||
id != 0 && known.contains(&id),
|
||||
"unpublished result: {reply}; events={events:?}"
|
||||
);
|
||||
}
|
||||
ids
|
||||
}
|
||||
|
||||
fn remember_search_frontend_events(events: &[Value], known: &mut std::collections::BTreeSet<u64>) {
|
||||
for event in events {
|
||||
assert_eq!(event["method"], "DOM.setChildNodes");
|
||||
let parent = event["params"]["parentId"].as_u64().expect("parent id");
|
||||
assert!(
|
||||
parent == 0 || known.contains(&parent),
|
||||
"unknown parent: {event}"
|
||||
);
|
||||
for node in event["params"]["nodes"].as_array().expect("nodes") {
|
||||
remember_search_frontend_nodes(node, known);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn node_id_outlets_require_publication_despite_internal_document_bindings() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
if let Some(bc) = ctx.conn.browser_context.as_mut() {
|
||||
bc.set_active_target_id("TID-1");
|
||||
}
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><button style='position:fixed;left:20px;top:20px;width:40px;height:40px'>hit</button><iframe srcdoc='<p>child</p>'></iframe></main></body></html>",
|
||||
).await;
|
||||
ctx.capture_fixture_layout(None).await;
|
||||
ctx.process_async(
|
||||
json!({"id":2,"method":"Runtime.evaluate","params":{"expression":"document"}}),
|
||||
)
|
||||
.await;
|
||||
let object = take_response_by_id(&mut ctx, 2)["result"]["result"]["objectId"].clone();
|
||||
let owner = crate::conn::CommandOwnerScope::capture(&ctx.conn, None);
|
||||
let snapshot = ctx
|
||||
.conn
|
||||
.document_node_snapshot_for_runtime_remote_object_id_for_owner_async(
|
||||
&owner,
|
||||
object.as_str().unwrap(),
|
||||
-1,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(snapshot.snapshot.frontend_node_id.is_some_and(|id| id != 0));
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(
|
||||
json!({"id":3,"method":"DOM.pushNodesByBackendIdsToFrontend","params":{
|
||||
"backendNodeIds":[snapshot.snapshot.backend_node_id]
|
||||
}}),
|
||||
)
|
||||
.await;
|
||||
ctx.expect_error(3, -32000, "Document needs to be requested first");
|
||||
ctx.process_async(json!({"id":4,"method":"Page.getFrameTree"}))
|
||||
.await;
|
||||
let frame = take_response_by_id(&mut ctx, 4)["result"]["frameTree"]["childFrames"][0]["frame"]
|
||||
["id"]
|
||||
.clone();
|
||||
for (id, method, params) in [
|
||||
(5, "DOM.getFrameOwner", json!({"frameId":frame})),
|
||||
(6, "DOM.getNodeForLocation", json!({"x":25,"y":25})),
|
||||
] {
|
||||
ctx.process_async(json!({"id":id,"method":method,"params":params}))
|
||||
.await;
|
||||
let response = take_response_by_id(&mut ctx, id);
|
||||
assert!(
|
||||
response["result"]["backendNodeId"]
|
||||
.as_u64()
|
||||
.is_some_and(|id| id != 0),
|
||||
"{response}"
|
||||
);
|
||||
assert!(
|
||||
response["result"].get("nodeId").is_none(),
|
||||
"an internal document binding must not enable frontend IDs: {response}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn frontend_inputs_require_publication_without_discarding_internal_bindings() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><section id=target><span id=leaf>text</span></section></main></body></html>",
|
||||
).await;
|
||||
ctx.process_async(json!({"id":2,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_async(json!({"id":3,"method":"Runtime.evaluate","params":{
|
||||
"expression":"document.getElementById('target')"
|
||||
}}))
|
||||
.await;
|
||||
let object = take_response_by_id(&mut ctx, 3)["result"]["result"]["objectId"].clone();
|
||||
let owner = crate::conn::CommandOwnerScope::capture(&ctx.conn, None);
|
||||
let snapshot = ctx
|
||||
.conn
|
||||
.document_node_snapshot_for_runtime_remote_object_id_for_owner_async(
|
||||
&owner,
|
||||
object.as_str().unwrap(),
|
||||
-1,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let bound = u64::from(snapshot.snapshot.frontend_node_id.unwrap());
|
||||
assert!(bound != 0 && !known.contains(&bound));
|
||||
ctx.sent.clear();
|
||||
let read_commands = [
|
||||
("DOM.requestChildNodes", json!({"nodeId":bound,"depth":1})),
|
||||
(
|
||||
"DOM.querySelector",
|
||||
json!({"nodeId":bound,"selector":"#leaf"}),
|
||||
),
|
||||
(
|
||||
"DOM.querySelectorAll",
|
||||
json!({"nodeId":bound,"selector":"#leaf"}),
|
||||
),
|
||||
("DOM.getAttributes", json!({"nodeId":bound})),
|
||||
("DOM.getOuterHTML", json!({"nodeId":bound})),
|
||||
("DOM.describeNode", json!({"nodeId":bound})),
|
||||
("DOM.resolveNode", json!({"nodeId":bound})),
|
||||
("DOM.getNodeStackTraces", json!({"nodeId":bound})),
|
||||
];
|
||||
for (method, params) in read_commands.iter().cloned().chain([
|
||||
(
|
||||
"DOM.setAttributeValue",
|
||||
json!({"nodeId":bound,"name":"data-blocked","value":"yes"}),
|
||||
),
|
||||
(
|
||||
"DOMDebugger.setDOMBreakpoint",
|
||||
json!({"nodeId":bound,"type":"subtree-modified"}),
|
||||
),
|
||||
("DOM.setNodeName", json!({"nodeId":bound,"name":"article"})),
|
||||
("DOM.removeNode", json!({"nodeId":bound})),
|
||||
]) {
|
||||
ctx.process_async(json!({"id":4,"method":method,"params":params}))
|
||||
.await;
|
||||
let messages = ctx.take_all();
|
||||
assert_eq!(
|
||||
messages.len(),
|
||||
1,
|
||||
"hidden input must not emit notifications: {method}: {messages:?}"
|
||||
);
|
||||
if method == "DOM.requestChildNodes" {
|
||||
assert_eq!(messages[0], json!({"id":4,"result":{}}));
|
||||
continue;
|
||||
}
|
||||
assert_eq!(
|
||||
messages[0]["error"]["code"], -32000,
|
||||
"{method}: {messages:?}"
|
||||
);
|
||||
assert!(
|
||||
messages[0]["error"]["message"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_ascii_lowercase()
|
||||
.contains("node"),
|
||||
"{method}: {messages:?}"
|
||||
);
|
||||
}
|
||||
let internal = ctx
|
||||
.conn
|
||||
.document_node_snapshot_for_runtime_remote_object_id_for_owner_async(
|
||||
&owner,
|
||||
object.as_str().unwrap(),
|
||||
-1,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
internal.snapshot.frontend_node_id, snapshot.snapshot.frontend_node_id,
|
||||
"rejecting frontend input must preserve the internal binding"
|
||||
);
|
||||
ctx.process_async(json!({"id":5,"method":"DOM.requestNode","params":{"objectId":object}}))
|
||||
.await;
|
||||
assert_eq!(
|
||||
assert_frontend_node_publication(&ctx.take_all(), 5, &mut known),
|
||||
vec![json!(bound)]
|
||||
);
|
||||
for (method, params) in read_commands {
|
||||
ctx.process_async(json!({"id":6,"method":method,"params":params}))
|
||||
.await;
|
||||
let messages = ctx.take_all();
|
||||
let (reply, events) = messages.split_last().unwrap();
|
||||
remember_search_frontend_events(events, &mut known);
|
||||
assert_eq!(reply["id"], 6);
|
||||
assert!(
|
||||
reply.get("error").is_none(),
|
||||
"published input failed: {method}: {reply}"
|
||||
);
|
||||
}
|
||||
ctx.process_async(json!({"id":7,"method":"Runtime.evaluate","params":{
|
||||
"expression":"document.getElementById('target').tagName + '|' + document.getElementById('target').hasAttribute('data-blocked')",
|
||||
"returnByValue":true
|
||||
}})).await;
|
||||
assert_eq!(
|
||||
take_response_by_id(&mut ctx, 7)["result"]["result"]["value"],
|
||||
"SECTION|false"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn describing_a_document_does_not_enable_frontend_node_requests() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(
|
||||
&mut ctx,
|
||||
1,
|
||||
"<!doctype html><html><body><a id='target'>link</a></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(
|
||||
json!({"id":2,"method":"Runtime.evaluate","params":{"expression":"document"}}),
|
||||
)
|
||||
.await;
|
||||
let object = take_response_by_id(&mut ctx, 2)["result"]["result"]["objectId"].clone();
|
||||
ctx.process_async(json!({"id":3,"method":"DOM.describeNode","params":{"objectId":object,"depth":-1,"pierce":true}})).await;
|
||||
assert_eq!(
|
||||
take_response_by_id(&mut ctx, 3)["result"]["node"]["nodeId"],
|
||||
0
|
||||
);
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(json!({"id":4,"method":"DOM.requestNode","params":{"objectId":object}}))
|
||||
.await;
|
||||
assert_eq!(ctx.take_all(), vec![json!({"id":4,"result":{"nodeId":0}})]);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn describe_and_request_node_preserve_search_publication() {
|
||||
for command in ["DOM.describeNode", "DOM.requestNode", "internal snapshot"] {
|
||||
for expression in [
|
||||
"document.getElementById('target')",
|
||||
"document.querySelector('section')",
|
||||
] {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><section><a id='target' href='/next'>link</a></section></main></body></html>",
|
||||
).await;
|
||||
ctx.process_async(json!({"id":2,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_async(
|
||||
json!({"id":3,"method":"Runtime.evaluate","params":{"expression":expression}}),
|
||||
)
|
||||
.await;
|
||||
let object = take_response_by_id(&mut ctx, 3)["result"]["result"]["objectId"].clone();
|
||||
ctx.sent.clear();
|
||||
if command == "internal snapshot" {
|
||||
let owner = crate::conn::CommandOwnerScope::capture(&ctx.conn, None);
|
||||
let snapshot = ctx
|
||||
.conn
|
||||
.document_node_snapshot_for_runtime_remote_object_id_for_owner_async(
|
||||
&owner,
|
||||
object.as_str().expect("object ID"),
|
||||
0,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.expect("internal snapshot")
|
||||
.expect("live node");
|
||||
let bound = u64::from(snapshot.snapshot.frontend_node_id.expect("bound ID"));
|
||||
assert!(
|
||||
bound != 0 && !known.contains(&bound),
|
||||
"an internal binding must not imply frontend publication"
|
||||
);
|
||||
assert!(
|
||||
ctx.take_all().is_empty(),
|
||||
"internal snapshots must not publish DOM paths"
|
||||
);
|
||||
for params in [
|
||||
json!({"objectId":object,"depth":-1}),
|
||||
json!({"backendNodeId":snapshot.snapshot.backend_node_id,"depth":-1}),
|
||||
] {
|
||||
ctx.process_async(json!({"id":4,"method":"DOM.describeNode","params":params}))
|
||||
.await;
|
||||
let messages = ctx.take_all();
|
||||
assert_eq!(messages.len(), 1, "describeNode must not publish a path");
|
||||
let mut described_ids = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(
|
||||
&messages[0]["result"]["node"],
|
||||
&mut described_ids,
|
||||
);
|
||||
assert_eq!(
|
||||
described_ids,
|
||||
std::collections::BTreeSet::from([0]),
|
||||
"describeNode must hide internal bindings: {messages:?}"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
ctx.process_async(json!({"id":4,"method":command,"params":{"objectId":object}}))
|
||||
.await;
|
||||
let messages = ctx.take_all();
|
||||
if command == "DOM.describeNode" {
|
||||
assert_eq!(messages.len(), 1, "describeNode must not publish a path");
|
||||
let node = &messages[0]["result"]["node"];
|
||||
assert_eq!(
|
||||
node["nodeId"], 0,
|
||||
"describing an unknown node must not bind it"
|
||||
);
|
||||
ctx.process_async(json!({"id":5,"method":"DOM.describeNode","params":{"backendNodeId":node["backendNodeId"],"depth":2}})).await;
|
||||
let described = take_response_by_id(&mut ctx, 5);
|
||||
assert_eq!(described["result"]["node"]["nodeId"], 0);
|
||||
} else {
|
||||
assert_frontend_node_publication(&messages, 4, &mut known);
|
||||
}
|
||||
}
|
||||
ctx.process_async(
|
||||
json!({"id":6,"method":"DOM.performSearch","params":{"query":"#target"}}),
|
||||
)
|
||||
.await;
|
||||
let search = take_response_by_id(&mut ctx, 6);
|
||||
assert_eq!(search["result"]["resultCount"], 1);
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(json!({"id":7,"method":"DOM.getSearchResults","params":{"searchId":search["result"]["searchId"],"fromIndex":0,"toIndex":1}})).await;
|
||||
let ids = assert_frontend_node_publication(&ctx.take_all(), 7, &mut known);
|
||||
ctx.process_async(
|
||||
json!({"id":8,"method":"DOM.describeNode","params":{"nodeId":ids[0]}}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
take_response_by_id(&mut ctx, 8)["result"]["node"]["nodeId"],
|
||||
ids[0],
|
||||
"describeNode must reuse an already published ID"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn internally_bound_nodes_emit_mutations_only_after_publication() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><section id='parent'><a id='target'>before</a><i></i></section></main></body></html>",
|
||||
).await;
|
||||
ctx.process_async(json!({"id":2,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_async(json!({"id":3,"method":"Runtime.evaluate","params":{"expression":"document.getElementById('parent')"}})).await;
|
||||
let object = take_response_by_id(&mut ctx, 3)["result"]["result"]["objectId"].clone();
|
||||
let owner = crate::conn::CommandOwnerScope::capture(&ctx.conn, None);
|
||||
let snapshot = ctx
|
||||
.conn
|
||||
.document_node_snapshot_for_runtime_remote_object_id_for_owner_async(
|
||||
&owner,
|
||||
object.as_str().expect("object ID"),
|
||||
-1,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.expect("internal snapshot")
|
||||
.expect("live node");
|
||||
let bound = u64::from(snapshot.snapshot.frontend_node_id.expect("bound ID"));
|
||||
assert!(bound != 0 && !known.contains(&bound));
|
||||
ctx.sent.clear();
|
||||
|
||||
for published in [false, true] {
|
||||
if published {
|
||||
ctx.process_async(
|
||||
json!({"id":4,"method":"DOM.requestNode","params":{"objectId":object}}),
|
||||
)
|
||||
.await;
|
||||
let ids = assert_frontend_node_publication(&ctx.take_all(), 4, &mut known);
|
||||
assert_eq!(
|
||||
ids,
|
||||
vec![json!(bound)],
|
||||
"publication must reuse the internal binding"
|
||||
);
|
||||
ctx.process_async(json!({"id":6,"method":"DOM.requestChildNodes","params":{"nodeId":bound,"depth":-1}})).await;
|
||||
let reply = take_response_by_id(&mut ctx, 6);
|
||||
assert_eq!(reply["result"], json!({}));
|
||||
remember_search_frontend_events(&ctx.take_all(), &mut known);
|
||||
}
|
||||
ctx.process_async(json!({"id":5,"method":"Runtime.evaluate","params":{
|
||||
"expression":"(() => { const target = document.getElementById('target'); target.setAttribute('data-x', 'changed'); target.removeAttribute('data-x'); target.firstChild.data = 'changed'; target.appendChild(document.createElement('b')); target.lastChild.remove(); target.firstChild.data = ' '; target.firstChild.data = 'restored'; })()",
|
||||
"returnByValue":true
|
||||
}})).await;
|
||||
let messages = ctx.take_all();
|
||||
let events = messages
|
||||
.iter()
|
||||
.filter(|message| {
|
||||
message["method"]
|
||||
.as_str()
|
||||
.is_some_and(|method| method.starts_with("DOM."))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if !published {
|
||||
assert!(
|
||||
events.is_empty(),
|
||||
"internal bindings must not emit DOM notifications: {messages:?}"
|
||||
);
|
||||
} else {
|
||||
for method in [
|
||||
"DOM.attributeModified",
|
||||
"DOM.attributeRemoved",
|
||||
"DOM.characterDataModified",
|
||||
"DOM.childNodeInserted",
|
||||
"DOM.childNodeRemoved",
|
||||
] {
|
||||
assert!(
|
||||
events.iter().any(|event| event["method"] == method),
|
||||
"missing {method}: {messages:?}"
|
||||
);
|
||||
}
|
||||
for event in events {
|
||||
for field in ["nodeId", "parentNodeId", "previousNodeId"] {
|
||||
if let Some(id) = event["params"][field].as_u64() {
|
||||
assert!(
|
||||
id == 0 || known.contains(&id),
|
||||
"unpublished mutation target: {event}"
|
||||
);
|
||||
}
|
||||
}
|
||||
if event["params"]["node"].is_object() {
|
||||
remember_search_frontend_nodes(&event["params"]["node"], &mut known);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
messages
|
||||
.iter()
|
||||
.any(|message| message["id"] == 5 && message["result"].is_object()),
|
||||
"Runtime command must complete: {messages:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn search_publishes_new_associated_roots_of_already_known_owners() {
|
||||
for (expression, query) in [
|
||||
(
|
||||
"document.getElementById('host').attachShadow({mode:'closed'}).innerHTML='<section><a id=shadow-hit>shadow</a></section>'",
|
||||
"#shadow-hit",
|
||||
),
|
||||
(
|
||||
"new Promise(resolve => {const frame=document.getElementById('frame');frame.onload=resolve;frame.srcdoc='<main><a id=frame-hit>replacement</a></main>';})",
|
||||
"#frame-hit",
|
||||
),
|
||||
] {
|
||||
for depth in [1, -1] {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><div id='host'><em id='retained'>retained</em></div><iframe id='frame' srcdoc='<p>original</p>'></iframe></body></html>",
|
||||
).await;
|
||||
ctx.process_async(
|
||||
json!({"id":2,"method":"DOM.getDocument","params":{"depth":depth,"pierce":false}}),
|
||||
)
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_and_wait_for_response_async(json!({"id":3,"method":"Runtime.evaluate","params":{"expression":expression,"awaitPromise":true}})).await;
|
||||
let changed = take_response_by_id(&mut ctx, 3);
|
||||
assert!(changed["result"]["exceptionDetails"].is_null(), "{changed}");
|
||||
ctx.process_async(
|
||||
json!({"id":4,"method":"DOM.performSearch","params":{"query":query}}),
|
||||
)
|
||||
.await;
|
||||
let search = take_response_by_id(&mut ctx, 4);
|
||||
assert_eq!(search["result"]["resultCount"], 1);
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(json!({"id":5,"method":"DOM.getSearchResults","params":{"searchId":search["result"]["searchId"],"fromIndex":0,"toIndex":1}})).await;
|
||||
let messages = ctx.take_all();
|
||||
let ids = assert_frontend_node_publication(&messages, 5, &mut known);
|
||||
if query == "#shadow-hit" && depth == -1 {
|
||||
let host = messages
|
||||
.iter()
|
||||
.filter(|event| event["method"] == "DOM.setChildNodes")
|
||||
.flat_map(|event| event["params"]["nodes"].as_array().expect("nodes"))
|
||||
.find(|node| {
|
||||
node["attributes"].as_array().is_some_and(|attrs| {
|
||||
attrs
|
||||
.windows(2)
|
||||
.any(|pair| pair == [json!("id"), json!("host")])
|
||||
})
|
||||
})
|
||||
.expect("refreshed shadow host");
|
||||
assert_eq!(
|
||||
host["children"][0]["localName"], "em",
|
||||
"refreshing the host must retain its previously published light children"
|
||||
);
|
||||
}
|
||||
ctx.process_async(json!({"id":6,"method":"DOM.getSearchResults","params":{"searchId":search["result"]["searchId"],"fromIndex":0,"toIndex":1}})).await;
|
||||
assert_eq!(
|
||||
ctx.take_all(),
|
||||
vec![json!({"id":6,"result":{"nodeIds":ids}})]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn search_publishes_mixed_light_shadow_frame_and_generated_paths_from_shallow_documents() {
|
||||
for depth in [1, 4, -1] {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><p class='hit'>light</p><div id='host'></div><iframe srcdoc=\"<main><p class='hit'>frame</p></main>\"></iframe><input type='search' value='needle'></main><script>document.getElementById('host').attachShadow({mode:'closed'}).innerHTML='<section><p class=hit>shadow</p></section>'</script></body></html>",
|
||||
).await;
|
||||
ctx.process_async(
|
||||
json!({"id":2,"method":"DOM.getDocument","params":{"depth":depth,"pierce":false}}),
|
||||
)
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.sent.clear();
|
||||
for (id, query, ua, count) in [(3, ".hit", false, 3), (6, "#editing-view-port", true, 1)] {
|
||||
ctx.process_async(json!({"id":id,"method":"DOM.performSearch","params":{"query":query,"includeUserAgentShadowDOM":ua}})).await;
|
||||
let search = ctx.take_all();
|
||||
assert_eq!(search.len(), 1, "performSearch must not publish nodes");
|
||||
assert_eq!(search[0]["result"]["resultCount"], count);
|
||||
let search_id = &search[0]["result"]["searchId"];
|
||||
ctx.process_async(json!({"id":id+1,"method":"DOM.getSearchResults","params":{"searchId":search_id,"fromIndex":0,"toIndex":count}})).await;
|
||||
let ids = assert_frontend_node_publication(&ctx.take_all(), id + 1, &mut known);
|
||||
assert_eq!(ids.len(), count as usize);
|
||||
ctx.process_async(json!({"id":id+2,"method":"DOM.getSearchResults","params":{"searchId":search_id,"fromIndex":0,"toIndex":count}})).await;
|
||||
assert_eq!(
|
||||
ctx.take_all(),
|
||||
vec![json!({"id":id+2,"result":{"nodeIds":ids}})],
|
||||
"published Inspector paths must be reused"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn search_only_publishes_paths_for_the_requested_range() {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx, 1,
|
||||
"<!doctype html><html><body><main><section><div><a class='hit' id='left'>left</a></div></section><section><div><a class='hit' id='right'>right</a></div></section></main></body></html>",
|
||||
).await;
|
||||
ctx.process_async(json!({"id":2,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_async(json!({"id":3,"method":"DOM.performSearch","params":{"query":".hit"}}))
|
||||
.await;
|
||||
let search = take_response_by_id(&mut ctx, 3);
|
||||
assert_eq!(search["result"]["resultCount"], 2);
|
||||
for (id, from, to, expected, absent) in [(4, 1, 2, "right", "left"), (5, 0, 1, "left", "right")]
|
||||
{
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(json!({"id":id,"method":"DOM.getSearchResults","params":{"searchId":search["result"]["searchId"],"fromIndex":from,"toIndex":to}})).await;
|
||||
let messages = ctx.take_all();
|
||||
assert_frontend_node_publication(&messages, id, &mut known);
|
||||
let events = &messages[..messages.len() - 1];
|
||||
let publishes = |id: &str| {
|
||||
events
|
||||
.iter()
|
||||
.flat_map(|event| event["params"]["nodes"].as_array().expect("nodes"))
|
||||
.any(|node| find_dom_node_by_attribute(node, "id", id).is_some())
|
||||
};
|
||||
assert!(
|
||||
publishes(expected),
|
||||
"the requested branch must be published: {messages:?}"
|
||||
);
|
||||
assert!(
|
||||
!publishes(absent),
|
||||
"the other branch must not be traversed: {messages:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn search_publishes_retained_removed_nodes_under_a_detached_root() {
|
||||
for removed in ["target", "container"] {
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
navigate_to_data_html_async(&mut ctx,1,"<!doctype html><html><body><div id='container'><section><b id='target'>retained</b></section></div></body></html>").await;
|
||||
ctx.process_async(json!({"id":2,"method":"DOM.getDocument","params":{"depth":1}}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 2);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_search_frontend_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.process_async(
|
||||
json!({"id":3,"method":"DOM.performSearch","params":{"query":"#target"}}),
|
||||
)
|
||||
.await;
|
||||
let search = take_response_by_id(&mut ctx, 3);
|
||||
assert_eq!(search["result"]["resultCount"], 1);
|
||||
ctx.process_async(json!({"id":4,"method":"Runtime.evaluate","params":{"expression":format!("window.held=document.getElementById('{removed}');held.remove()")}})).await;
|
||||
let _ = take_response_by_id(&mut ctx, 4);
|
||||
ctx.sent.clear();
|
||||
ctx.process_async(json!({"id":5,"method":"DOM.getSearchResults","params":{"searchId":search["result"]["searchId"],"fromIndex":0,"toIndex":1}})).await;
|
||||
let messages = ctx.take_all();
|
||||
assert_eq!(messages[0]["params"]["parentId"], 0);
|
||||
assert_frontend_node_publication(&messages, 5, &mut known);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn get_search_results_no_bc_error() {
|
||||
let mut ctx = TestContext::new();
|
||||
@@ -123,6 +769,101 @@ async fn search_flow_with_real_dom() {
|
||||
ctx.expect_error(6, -32000, "No search session with given id found");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn search_results_publish_node_paths_before_reply_for_each_session() {
|
||||
fn remember_nodes(node: &Value, known: &mut std::collections::BTreeSet<u64>) {
|
||||
known.insert(node["nodeId"].as_u64().expect("frontend node id"));
|
||||
if let Some(children) = node["children"].as_array() {
|
||||
for child in children {
|
||||
remember_nodes(child, known);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut ctx = TestContext::new();
|
||||
load_bc(&mut ctx, "BID-A");
|
||||
ctx.conn
|
||||
.browser_context
|
||||
.as_mut()
|
||||
.expect("browser context")
|
||||
.attach_active_session("SID-search");
|
||||
navigate_to_data_html_async(
|
||||
&mut ctx,
|
||||
1,
|
||||
"<!doctype html><html><body><main><section><ul><li><a href='/one'>one</a></li><li><a href='/two'>two</a></li></ul></section><aside><b>unrelated</b></aside></main></body></html>",
|
||||
)
|
||||
.await;
|
||||
ctx.process_async(json!({
|
||||
"id": 2,
|
||||
"method": "Target.attachToTarget",
|
||||
"params": { "targetId": "TID-1" }
|
||||
}))
|
||||
.await;
|
||||
let second_session = take_response_by_id(&mut ctx, 2)["result"]["sessionId"]
|
||||
.as_str()
|
||||
.expect("second session")
|
||||
.to_owned();
|
||||
ctx.sent.clear();
|
||||
|
||||
for session_id in ["SID-search", second_session.as_str()] {
|
||||
ctx.process_async(json!({
|
||||
"id": 3, "sessionId": session_id, "method": "DOM.getDocument",
|
||||
"params": { "depth": 1 }
|
||||
}))
|
||||
.await;
|
||||
let document = take_response_by_id(&mut ctx, 3);
|
||||
let mut known = std::collections::BTreeSet::new();
|
||||
remember_nodes(&document["result"]["root"], &mut known);
|
||||
ctx.sent.clear();
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 4, "sessionId": session_id, "method": "DOM.performSearch",
|
||||
"params": { "query": "a[href]" }
|
||||
}))
|
||||
.await;
|
||||
let search = ctx.take_all();
|
||||
assert_eq!(search.len(), 1, "performSearch must not publish nodes");
|
||||
assert_eq!(search[0]["result"]["resultCount"], 2);
|
||||
let search_id = &search[0]["result"]["searchId"];
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 5, "sessionId": session_id, "method": "DOM.getSearchResults",
|
||||
"params": { "searchId": search_id, "fromIndex": 0, "toIndex": 2 }
|
||||
}))
|
||||
.await;
|
||||
let messages = ctx.take_all();
|
||||
assert!(
|
||||
messages.len() > 1,
|
||||
"the unrequested paths must be published"
|
||||
);
|
||||
let (reply, events) = messages.split_last().expect("search results reply");
|
||||
assert_eq!(reply["id"], 5);
|
||||
for event in events {
|
||||
assert_eq!(event["sessionId"], session_id);
|
||||
assert_eq!(event["method"], "DOM.setChildNodes");
|
||||
let parent = event["params"]["parentId"].as_u64().expect("parent id");
|
||||
assert!(known.contains(&parent), "ancestors must precede children");
|
||||
for node in event["params"]["nodes"].as_array().expect("child nodes") {
|
||||
assert_ne!(node["nodeName"], "B", "do not expand unrelated subtrees");
|
||||
remember_nodes(node, &mut known);
|
||||
}
|
||||
}
|
||||
let node_ids = reply["result"]["nodeIds"].as_array().expect("search IDs");
|
||||
assert_eq!(node_ids.len(), 2);
|
||||
for node_id in node_ids {
|
||||
assert!(known.contains(&node_id.as_u64().expect("result node id")));
|
||||
}
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 6, "sessionId": session_id, "method": "DOM.getSearchResults",
|
||||
"params": { "searchId": search_id, "fromIndex": 1, "toIndex": 2 }
|
||||
}))
|
||||
.await;
|
||||
ctx.expect_result(6, json!({ "nodeIds": [node_ids[1]] }), Some(session_id));
|
||||
assert!(ctx.take_all().is_empty(), "do not republish known paths");
|
||||
}
|
||||
}
|
||||
|
||||
async fn perform_search_and_get_all_results(
|
||||
ctx: &mut TestContext,
|
||||
command_id: u64,
|
||||
@@ -1385,8 +2126,11 @@ async fn query_selector_nodes_found_emits_set_child_nodes_once_like_chromium() {
|
||||
"params": { "searchId": "0", "fromIndex": 0, "toIndex": 1 }
|
||||
}))
|
||||
.await;
|
||||
let div_node_id = ctx.take_one()["result"]["nodeIds"][0].as_u64().unwrap_or(0);
|
||||
let div_node_id = take_response_by_id(&mut ctx, 3)["result"]["nodeIds"][0]
|
||||
.as_u64()
|
||||
.unwrap_or(0);
|
||||
assert!(div_node_id > 0);
|
||||
ctx.sent.clear();
|
||||
|
||||
ctx.process_async(json!({
|
||||
"id": 4,
|
||||
|
||||
@@ -29,8 +29,10 @@ pub(crate) fn with_backend_node(
|
||||
DevToolsDomNodeReference::BackendNodeId(id) => next(id).require_owner_turn(),
|
||||
DevToolsDomNodeReference::FrontendNodeId(frontend_node_id) => {
|
||||
let command = Command::DocumentFrontendNodeBinding {
|
||||
inspector_session_id: session,
|
||||
frontend_node_id,
|
||||
reference: moli_core::page::RendererDomNodeReference::FrontendNodeId {
|
||||
inspector_session_id: session,
|
||||
frontend_node_id,
|
||||
},
|
||||
};
|
||||
let continue_binding = move |reply| match reply {
|
||||
Ok(Reply::DocumentFrontendNodeBinding(
|
||||
|
||||
@@ -636,6 +636,9 @@ impl RendererInspectorCommandEnvelope {
|
||||
RendererInspectorPageCommand::DocumentNodeSnapshotForObjectId { .. } => {
|
||||
Some("DocumentNodeSnapshotForObjectId")
|
||||
}
|
||||
RendererInspectorPageCommand::RequestDocumentNodeForObjectId { .. } => {
|
||||
Some("RequestDocumentNodeForObjectId")
|
||||
}
|
||||
RendererInspectorPageCommand::AccessibilityTreePayloadsForObjectId { .. } => {
|
||||
Some("AccessibilityTreePayloadsForObjectId")
|
||||
}
|
||||
|
||||
@@ -660,6 +660,7 @@ impl JsContextHost {
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let mut payloads = Vec::with_capacity(children.len());
|
||||
let mut published_backend_ids = Vec::with_capacity(children.len());
|
||||
for child in children.iter().copied() {
|
||||
let mut snapshot = live_inspector_document_node_snapshot(
|
||||
self.dom_host(),
|
||||
@@ -683,6 +684,14 @@ impl JsContextHost {
|
||||
payloads.push(moli_protocol_cdp::node_snapshot_to_cdp(
|
||||
&snapshot, None, None,
|
||||
)?);
|
||||
published_backend_ids.push(backend_node_id);
|
||||
}
|
||||
for backend_node_id in published_backend_ids {
|
||||
self.dom_agent_state.mark_node_published(
|
||||
inspector_session_id,
|
||||
Some(document_id),
|
||||
backend_node_id,
|
||||
);
|
||||
}
|
||||
self.dom_agent_state.mark_children_requested(
|
||||
inspector_session_id,
|
||||
@@ -710,7 +719,7 @@ impl JsContextHost {
|
||||
.backend_node_id_for_node(document_id, target);
|
||||
if let Some(frontend_node_id) = self
|
||||
.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
Some(document_id),
|
||||
target_backend_node_id,
|
||||
@@ -727,7 +736,7 @@ impl JsContextHost {
|
||||
.backend_node_id_for_node(document_id, current);
|
||||
if let Some(frontend_node_id) = self
|
||||
.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
Some(document_id),
|
||||
backend_node_id,
|
||||
@@ -754,7 +763,7 @@ impl JsContextHost {
|
||||
.backend_node_id_for_node(document_id, parent);
|
||||
parent_frontend_node_id = self
|
||||
.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
Some(document_id),
|
||||
next_backend_node_id,
|
||||
|
||||
@@ -647,14 +647,19 @@ impl PageVm {
|
||||
search_id,
|
||||
from_index,
|
||||
to_index,
|
||||
} => Ok(RendererPageReply::DocumentSearchResults(
|
||||
self.document_search_results(
|
||||
} => {
|
||||
let (resolution, child_node_snapshot_events) = self
|
||||
.document_search_results_with_child_node_snapshot_events(
|
||||
inspector_session_id.as_deref(),
|
||||
&search_id,
|
||||
from_index,
|
||||
to_index,
|
||||
),
|
||||
)),
|
||||
);
|
||||
Ok(RendererPageReply::DocumentSearchResults {
|
||||
resolution,
|
||||
child_node_snapshot_events,
|
||||
})
|
||||
}
|
||||
RendererPageCommand::DocumentDiscardSearchResults {
|
||||
inspector_session_id,
|
||||
search_id,
|
||||
@@ -681,15 +686,15 @@ impl PageVm {
|
||||
frontend_node_id,
|
||||
),
|
||||
)),
|
||||
RendererPageCommand::DocumentFrontendNodeBinding {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
} => Ok(RendererPageReply::DocumentFrontendNodeBinding(
|
||||
self.document_frontend_node_binding(
|
||||
inspector_session_id.as_deref(),
|
||||
frontend_node_id,
|
||||
),
|
||||
)),
|
||||
RendererPageCommand::DocumentFrontendNodeBinding { reference } => {
|
||||
let binding = self.backend_node_id_for_document_node_reference(reference);
|
||||
Ok(RendererPageReply::DocumentFrontendNodeBinding(
|
||||
binding.map_or(
|
||||
crate::RendererDomFrontendNodeBindingResolution::NotFound,
|
||||
crate::RendererDomFrontendNodeBindingResolution::BackendNodeId,
|
||||
),
|
||||
))
|
||||
}
|
||||
RendererPageCommand::RegisterDocumentBidiNodeBinding {
|
||||
inspector_session_id,
|
||||
shared_id,
|
||||
@@ -1326,6 +1331,7 @@ impl PageVm {
|
||||
object_id,
|
||||
depth,
|
||||
pierce,
|
||||
bind_frontend_nodes,
|
||||
} => {
|
||||
self.configure_document_dom_agent_session(
|
||||
inspector_session_id,
|
||||
@@ -1337,9 +1343,22 @@ impl PageVm {
|
||||
&object_id,
|
||||
depth,
|
||||
pierce,
|
||||
bind_frontend_nodes,
|
||||
)?),
|
||||
))
|
||||
}
|
||||
RendererInspectorPageCommand::RequestDocumentNodeForObjectId {
|
||||
include_whitespace,
|
||||
object_id,
|
||||
} => {
|
||||
self.configure_document_dom_agent_session(inspector_session_id, include_whitespace);
|
||||
let (node_id, child_node_snapshot_events) =
|
||||
self.request_document_node_for_object_id(inspector_session_id, &object_id)?;
|
||||
Ok(RendererPageReply::DocumentNodeRequested {
|
||||
node_id,
|
||||
child_node_snapshot_events,
|
||||
})
|
||||
}
|
||||
RendererInspectorPageCommand::AccessibilityTreePayloadsForObjectId { object_id } => {
|
||||
Ok(RendererPageReply::OptionalAccessibilityPayloadsForObjectId(
|
||||
self.accessibility_tree_payloads_for_object_id(
|
||||
|
||||
@@ -396,19 +396,19 @@ fn live_document_node_snapshot_with_budget(
|
||||
|
||||
let shadow_root_type = dom_host.shadow_root_mode(node_id);
|
||||
let parent_id = parent_id.or_else(|| live_shadow_root_host_for_handle(dom_host, node_id));
|
||||
let shadow_roots = if pierce && depth != 0 {
|
||||
let shadow_roots = if let Some(next_tree_depth) = next_tree_depth {
|
||||
dom_host
|
||||
.shadow_root_handle(node_id)
|
||||
.and_then(|shadow_root_id| {
|
||||
live_document_node_snapshot_with_budget(
|
||||
dom_host,
|
||||
shadow_root_id,
|
||||
next_depth,
|
||||
if pierce { next_depth } else { 0 },
|
||||
Some(node_id),
|
||||
pierce,
|
||||
include_whitespace,
|
||||
force_single_text_child_at_depth_boundary,
|
||||
next_tree_depth?,
|
||||
next_tree_depth,
|
||||
)
|
||||
})
|
||||
.into_iter()
|
||||
@@ -1946,6 +1946,7 @@ impl PageVm {
|
||||
object_id: &str,
|
||||
depth: i32,
|
||||
pierce: bool,
|
||||
bind_frontend_nodes: bool,
|
||||
) -> Result<Option<DocumentNodeObjectSnapshot>> {
|
||||
let Some(handle) = self
|
||||
.vm_mut()
|
||||
@@ -1953,7 +1954,7 @@ impl PageVm {
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let snapshot = self.document_node_snapshot_for_live_handle_for_session(
|
||||
let mut snapshot = self.document_node_snapshot_for_live_handle_for_session(
|
||||
inspector_session_id,
|
||||
handle,
|
||||
depth,
|
||||
@@ -1961,9 +1962,39 @@ impl PageVm {
|
||||
true,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
);
|
||||
if bind_frontend_nodes && let Some(snapshot) = snapshot.as_mut() {
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot.snapshot,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
);
|
||||
}
|
||||
Ok(snapshot)
|
||||
}
|
||||
|
||||
pub(crate) fn request_document_node_for_object_id(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
object_id: &str,
|
||||
) -> Result<(Option<u32>, Option<RendererDocumentChildNodeSnapshotEvents>)> {
|
||||
let Some(handle) = self
|
||||
.vm_mut()
|
||||
.live_node_handle_for_runtime_object_id(inspector_session_id, object_id)?
|
||||
else {
|
||||
return Ok((None, None));
|
||||
};
|
||||
let Some(backend_node_id) = self.renderer_backend_node_id_for_live_handle(handle) else {
|
||||
return Ok((None, None));
|
||||
};
|
||||
let (resolution, events) =
|
||||
self.document_node_ids_with_publication(inspector_session_id, &[backend_node_id]);
|
||||
let node_id = match resolution {
|
||||
RendererDocumentFrontendNodeIdsResolution::Found(ids) => ids[0].unwrap_or(0),
|
||||
RendererDocumentFrontendNodeIdsResolution::DocumentNotBound => 0,
|
||||
};
|
||||
Ok((Some(node_id), events))
|
||||
}
|
||||
|
||||
pub(crate) fn document_node_exists_for_backend_node_id(
|
||||
&mut self,
|
||||
backend_node_id: u32,
|
||||
@@ -1990,7 +2021,7 @@ impl PageVm {
|
||||
let Some(key) = self.current_renderer_backend_node_key_for_id(backend_node_id) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let snapshot = match key.inspector_identity {
|
||||
let mut snapshot = match key.inspector_identity {
|
||||
None => self.document_node_snapshot_for_live_handle(key.handle, depth, pierce),
|
||||
Some(DocumentNodeInspectorIdentity::MarkerPseudoElement) => {
|
||||
self.document_marker_pseudo_element_snapshot_for_live_host(None, key.handle)
|
||||
@@ -2000,6 +2031,9 @@ impl PageVm {
|
||||
None, key.handle, identity, depth, true,
|
||||
),
|
||||
};
|
||||
if let Some(snapshot) = snapshot.as_mut() {
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(None, &mut snapshot.snapshot, true);
|
||||
}
|
||||
Ok(snapshot)
|
||||
}
|
||||
|
||||
@@ -2075,7 +2109,7 @@ impl PageVm {
|
||||
.dom_host()
|
||||
.dom()
|
||||
.document_node_id();
|
||||
let snapshot = self.document_node_snapshot_for_live_handle_for_session(
|
||||
let mut snapshot = self.document_node_snapshot_for_live_handle_for_session(
|
||||
inspector_session_id,
|
||||
handle,
|
||||
depth,
|
||||
@@ -2083,11 +2117,12 @@ impl PageVm {
|
||||
true,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
)?;
|
||||
self.mark_document_snapshot_children_requested(
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&snapshot.snapshot,
|
||||
depth,
|
||||
&mut snapshot.snapshot,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
);
|
||||
self.mark_document_snapshot_published(inspector_session_id, &snapshot.snapshot, depth);
|
||||
Some(snapshot)
|
||||
}
|
||||
|
||||
@@ -2164,17 +2199,15 @@ impl PageVm {
|
||||
parent_backend_node_id,
|
||||
child_count,
|
||||
);
|
||||
for snapshot in &snapshots {
|
||||
self.mark_document_snapshot_children_requested(
|
||||
inspector_session_id,
|
||||
snapshot,
|
||||
depth,
|
||||
);
|
||||
}
|
||||
events.push(RendererDocumentChildNodeSnapshotEvent {
|
||||
parent_frontend_node_id,
|
||||
snapshots,
|
||||
});
|
||||
self.append_document_child_snapshot_event(
|
||||
inspector_session_id,
|
||||
RendererDocumentChildNodeSnapshotEvent {
|
||||
parent_frontend_node_id,
|
||||
snapshots,
|
||||
},
|
||||
depth,
|
||||
&mut events,
|
||||
);
|
||||
}
|
||||
Some(RendererDocumentChildNodeSnapshotEvents {
|
||||
top_snapshot_node_id,
|
||||
@@ -2193,64 +2226,236 @@ impl PageVm {
|
||||
let backend_node_id = self.backend_node_id_for_document_node_reference(reference)?;
|
||||
self.configure_document_dom_agent_session(inspector_session_id, include_whitespace);
|
||||
let handle = self.live_handle_for_backend_node_id(backend_node_id)?;
|
||||
self.document_child_node_snapshot_events_for_live_handles(
|
||||
inspector_session_id,
|
||||
&[handle],
|
||||
depth,
|
||||
pierce,
|
||||
)
|
||||
let mut events = Vec::new();
|
||||
let mut pending = vec![(handle, depth)];
|
||||
while let Some((handle, depth)) = pending.pop() {
|
||||
let dom_host = self.vm().document_runtime.dom_host();
|
||||
if !matches!(
|
||||
dom_host.node(handle)?.kind(),
|
||||
NodeData::Element(_) | NodeData::Document(_) | NodeData::DocumentFragment(_)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
let backend = self.renderer_backend_node_id_for_live_handle(handle)?;
|
||||
if self.document_node_children_requested(inspector_session_id, backend) {
|
||||
if depth != 0 {
|
||||
let next = if depth > 0 { depth - 1 } else { depth };
|
||||
let dom_host = self.vm().document_runtime.dom_host();
|
||||
let children = dom_host
|
||||
.child_handles(handle)
|
||||
.filter(|child| {
|
||||
include_whitespace || !inspector_whitespace_text_node(dom_host, *child)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
pending.extend(children.into_iter().rev().map(|child| (child, next)));
|
||||
}
|
||||
} else {
|
||||
let mut snapshots = self.document_child_node_snapshot_events_for_live_handles(
|
||||
inspector_session_id,
|
||||
&[handle],
|
||||
depth,
|
||||
pierce,
|
||||
)?;
|
||||
events.append(&mut snapshots.events);
|
||||
}
|
||||
}
|
||||
Some(RendererDocumentChildNodeSnapshotEvents {
|
||||
top_snapshot_node_id: self
|
||||
.vm()
|
||||
.document_runtime
|
||||
.dom_host()
|
||||
.dom()
|
||||
.document_node_id(),
|
||||
events,
|
||||
})
|
||||
}
|
||||
|
||||
fn document_query_selector_node_path_snapshot_events(
|
||||
fn inspector_parent_backend_node_id(
|
||||
&mut self,
|
||||
backend_node_id: u32,
|
||||
child_owners: &HashMap<DomHandle, DomHandle>,
|
||||
) -> Option<(u32, bool)> {
|
||||
let key = self.current_renderer_backend_node_key_for_id(backend_node_id)?;
|
||||
if key.inspector_identity == Some(DocumentNodeInspectorIdentity::MarkerPseudoElement) {
|
||||
return self
|
||||
.renderer_backend_node_id_for_live_handle(key.handle)
|
||||
.map(|id| (id, true));
|
||||
}
|
||||
if let Some(identity) = key.inspector_identity {
|
||||
let host = live_document_node_snapshot(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
key.handle,
|
||||
0,
|
||||
None,
|
||||
false,
|
||||
)?;
|
||||
let snapshot = user_agent_shadow_node_snapshot(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
&host,
|
||||
identity,
|
||||
0,
|
||||
true,
|
||||
)?;
|
||||
return match snapshot.inspector_parent_identity {
|
||||
Some(parent) => self
|
||||
.renderer_backend_node_id_for_inspector_node(key.handle, parent)
|
||||
.map(|id| (id, false)),
|
||||
None => self
|
||||
.renderer_backend_node_id_for_live_handle(key.handle)
|
||||
.map(|id| (id, true)),
|
||||
};
|
||||
}
|
||||
let dom_host = self.vm().document_runtime.dom_host();
|
||||
let parent = dom_host
|
||||
.dom()
|
||||
.parent_node(key.handle)
|
||||
.or_else(|| dom_host.shadow_root_host(key.handle))
|
||||
.or_else(|| child_owners.get(&key.handle).copied())?;
|
||||
let associated = dom_host.dom().parent_node(key.handle).is_none();
|
||||
self.renderer_backend_node_id_for_live_handle(parent)
|
||||
.map(|id| (id, associated))
|
||||
}
|
||||
|
||||
fn document_node_path_snapshot_events(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
root: DomHandle,
|
||||
result_handles: &[DomHandle],
|
||||
backend_node_ids: impl IntoIterator<Item = u32>,
|
||||
) -> Option<RendererDocumentChildNodeSnapshotEvents> {
|
||||
let mut backend_node_ids = backend_node_ids.into_iter().peekable();
|
||||
backend_node_ids.peek()?;
|
||||
let top_snapshot_node_id = self
|
||||
.vm()
|
||||
.document_runtime
|
||||
.dom_host()
|
||||
.dom()
|
||||
.document_node_id();
|
||||
let root_backend = self.renderer_backend_node_id_for_live_handle(root)?;
|
||||
let child_owners = self
|
||||
.inspector_child_document_handles()
|
||||
.into_iter()
|
||||
.map(|(owner, document)| (document, owner))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let mut events = Vec::new();
|
||||
|
||||
for &result_handle in result_handles {
|
||||
let path = {
|
||||
let dom_host = self.vm().document_runtime.dom_host();
|
||||
let mut current = result_handle;
|
||||
let mut path = Vec::new();
|
||||
while current != root {
|
||||
let parent = dom_host.dom().parent_node(current)?;
|
||||
path.push(parent);
|
||||
current = parent;
|
||||
// A later stale result must not discard paths already prepared for
|
||||
// earlier results in the same batch.
|
||||
'nodes: for backend_node_id in backend_node_ids {
|
||||
if self.document_node_is_published(inspector_session_id, backend_node_id) {
|
||||
continue;
|
||||
}
|
||||
let mut current = backend_node_id;
|
||||
let mut path = Vec::new();
|
||||
let mut refresh_owner = false;
|
||||
while current != root_backend {
|
||||
let Some((parent, associated)) =
|
||||
self.inspector_parent_backend_node_id(current, &child_owners)
|
||||
else {
|
||||
// A retained search result can leave the Document between
|
||||
// performSearch and getSearchResults. Publish its detached
|
||||
// Inspector root before expanding the remaining path.
|
||||
if refresh_owner
|
||||
|| !self.document_node_is_published(inspector_session_id, current)
|
||||
{
|
||||
let Some(mut snapshot) = self
|
||||
.document_node_snapshot_for_backend_node_id_in_inspector_session(
|
||||
inspector_session_id,
|
||||
current,
|
||||
0,
|
||||
false,
|
||||
)
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|node| node.snapshot)
|
||||
else {
|
||||
continue 'nodes;
|
||||
};
|
||||
snapshot.parent_id = None;
|
||||
snapshot.parent_frontend_node_id = None;
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
);
|
||||
self.append_document_child_snapshot_event(
|
||||
inspector_session_id,
|
||||
RendererDocumentChildNodeSnapshotEvent {
|
||||
parent_frontend_node_id: 0,
|
||||
snapshots: vec![snapshot],
|
||||
},
|
||||
0,
|
||||
&mut events,
|
||||
);
|
||||
}
|
||||
break;
|
||||
};
|
||||
path.push((parent, current, refresh_owner));
|
||||
// Associated roots are sent in the owner's snapshot, not in
|
||||
// its ordinary children. Re-publish that owner through its
|
||||
// parent if a ShadowRoot or child Document appeared later.
|
||||
if !associated && self.document_node_is_published(inspector_session_id, parent) {
|
||||
break;
|
||||
}
|
||||
path.reverse();
|
||||
path
|
||||
};
|
||||
|
||||
// Chromium's PushNodePathToFrontend exposes each previously
|
||||
// unrequested ancestor in root-to-leaf order. That guarantees a
|
||||
// query result is present in the frontend node map before the
|
||||
// command response returns, while shared paths are emitted once.
|
||||
for parent in path {
|
||||
let parent_backend_node_id =
|
||||
self.renderer_backend_node_id_for_live_handle(parent)?;
|
||||
if self
|
||||
.document_node_children_requested(inspector_session_id, parent_backend_node_id)
|
||||
{
|
||||
refresh_owner = associated;
|
||||
current = parent;
|
||||
}
|
||||
for (parent, child, refresh_owner) in path.into_iter().rev() {
|
||||
if !refresh_owner && self.document_node_is_published(inspector_session_id, child) {
|
||||
continue;
|
||||
}
|
||||
let mut parent_events = self.document_child_node_snapshot_events_for_live_handles(
|
||||
inspector_session_id,
|
||||
&[parent],
|
||||
0,
|
||||
false,
|
||||
)?;
|
||||
events.append(&mut parent_events.events);
|
||||
let Some(key) = self.current_renderer_backend_node_key_for_id(parent) else {
|
||||
continue 'nodes;
|
||||
};
|
||||
if key.inspector_identity.is_none() {
|
||||
let Some(mut parent_events) = self
|
||||
.document_child_node_snapshot_events_for_live_handles(
|
||||
inspector_session_id,
|
||||
&[key.handle],
|
||||
0,
|
||||
false,
|
||||
)
|
||||
else {
|
||||
continue 'nodes;
|
||||
};
|
||||
events.append(&mut parent_events.events);
|
||||
} else {
|
||||
let Some(mut snapshot) = self
|
||||
.document_node_snapshot_for_backend_node_id_in_inspector_session(
|
||||
inspector_session_id,
|
||||
parent,
|
||||
1,
|
||||
true,
|
||||
)
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|node| node.snapshot)
|
||||
else {
|
||||
continue 'nodes;
|
||||
};
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot,
|
||||
self.document_dom_agent_includes_whitespace(inspector_session_id),
|
||||
);
|
||||
let Some(parent_frontend_node_id) = snapshot.frontend_node_id else {
|
||||
continue 'nodes;
|
||||
};
|
||||
self.mark_document_node_children_requested(
|
||||
inspector_session_id,
|
||||
parent,
|
||||
snapshot.child_count,
|
||||
);
|
||||
self.append_document_child_snapshot_event(
|
||||
inspector_session_id,
|
||||
RendererDocumentChildNodeSnapshotEvent {
|
||||
parent_frontend_node_id,
|
||||
snapshots: snapshot.children,
|
||||
},
|
||||
0,
|
||||
&mut events,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(RendererDocumentChildNodeSnapshotEvents {
|
||||
top_snapshot_node_id,
|
||||
events,
|
||||
@@ -2336,7 +2541,6 @@ impl PageVm {
|
||||
backend_node_id,
|
||||
);
|
||||
Some(RendererDocumentQuerySelectorNode {
|
||||
live_node_id: handle,
|
||||
frontend_node_id,
|
||||
backend_node_id,
|
||||
})
|
||||
@@ -2422,25 +2626,51 @@ impl PageVm {
|
||||
selector,
|
||||
multiple,
|
||||
);
|
||||
let result_handles = match &query_selector_resolution {
|
||||
let backend_node_ids = match &query_selector_resolution {
|
||||
RendererDocumentQuerySelectorResolution::Found(nodes) => nodes
|
||||
.iter()
|
||||
.map(|node| node.live_node_id)
|
||||
.map(|node| node.backend_node_id)
|
||||
.collect::<Vec<_>>(),
|
||||
RendererDocumentQuerySelectorResolution::MissingRoot
|
||||
| RendererDocumentQuerySelectorResolution::InvalidSelector(_) => Vec::new(),
|
||||
};
|
||||
let child_node_snapshot_events = self.document_query_selector_node_path_snapshot_events(
|
||||
inspector_session_id,
|
||||
root,
|
||||
&result_handles,
|
||||
);
|
||||
let child_node_snapshot_events =
|
||||
self.document_node_path_snapshot_events(inspector_session_id, root, backend_node_ids);
|
||||
RendererDocumentQuerySelectorWithChildNodeSnapshotEvents {
|
||||
child_node_snapshot_events,
|
||||
query_selector_resolution,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn document_search_results_with_child_node_snapshot_events(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
search_id: &str,
|
||||
from_index: usize,
|
||||
to_index: usize,
|
||||
) -> (
|
||||
crate::RendererDomSearchResultsResolution,
|
||||
Option<RendererDocumentChildNodeSnapshotEvents>,
|
||||
) {
|
||||
let mut resolution =
|
||||
self.document_search_results(inspector_session_id, search_id, from_index, to_index);
|
||||
let crate::RendererDomSearchResultsResolution::Found(nodes) = &mut resolution else {
|
||||
return (resolution, None);
|
||||
};
|
||||
let backend_ids = nodes
|
||||
.iter()
|
||||
.map(|node| node.backend_node_id)
|
||||
.collect::<Vec<_>>();
|
||||
let (publication, events) =
|
||||
self.document_node_ids_with_publication(inspector_session_id, &backend_ids);
|
||||
if let RendererDocumentFrontendNodeIdsResolution::Found(ids) = publication {
|
||||
for (node, id) in nodes.iter_mut().zip(ids) {
|
||||
node.frontend_node_id = id.unwrap_or(0);
|
||||
}
|
||||
}
|
||||
(resolution, events)
|
||||
}
|
||||
|
||||
pub(crate) fn document_query_selector_with_child_node_snapshot_events_for_node(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -2500,6 +2730,12 @@ impl PageVm {
|
||||
pierce,
|
||||
include_whitespace,
|
||||
) {
|
||||
self.project_child_frame_content_documents(
|
||||
&mut snapshot,
|
||||
depth,
|
||||
pierce,
|
||||
include_whitespace,
|
||||
);
|
||||
self.project_generated_user_agent_shadow_roots(
|
||||
&mut snapshot,
|
||||
depth,
|
||||
@@ -2560,10 +2796,11 @@ impl PageVm {
|
||||
);
|
||||
}
|
||||
self.project_generated_pseudo_elements(snapshot);
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
self.project_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
snapshot,
|
||||
include_whitespace,
|
||||
false,
|
||||
);
|
||||
}
|
||||
snapshot.map(|snapshot| DocumentNodeObjectSnapshot {
|
||||
@@ -2594,7 +2831,12 @@ impl PageVm {
|
||||
false,
|
||||
)?;
|
||||
let mut snapshot = marker_pseudo_element_snapshot(&originating_element);
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(inspector_session_id, &mut snapshot, true);
|
||||
self.project_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot,
|
||||
true,
|
||||
false,
|
||||
);
|
||||
Some(DocumentNodeObjectSnapshot {
|
||||
frame_id,
|
||||
owner_node_id: None,
|
||||
@@ -2626,10 +2868,11 @@ impl PageVm {
|
||||
depth,
|
||||
include_whitespace,
|
||||
)?;
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
self.project_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot,
|
||||
include_whitespace,
|
||||
false,
|
||||
);
|
||||
Some(DocumentNodeObjectSnapshot {
|
||||
frame_id,
|
||||
@@ -2639,6 +2882,26 @@ impl PageVm {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn inspector_child_document_handles(&self) -> Vec<(DomHandle, DomHandle)> {
|
||||
self.vm()
|
||||
.live_child_document_handles_in_snapshot_order()
|
||||
.into_iter()
|
||||
.filter(|(_, owner, _)| {
|
||||
let vm = self.vm();
|
||||
vm.child_browsing_context_current_url(*owner)
|
||||
.is_some_and(|url| {
|
||||
child_content_document_belongs_to_top_target(
|
||||
vm.document_runtime.document_url(),
|
||||
&url,
|
||||
vm.child_browsing_context_is_same_origin_with_top(*owner),
|
||||
vm.child_browsing_context_has_opaque_origin(*owner),
|
||||
)
|
||||
})
|
||||
})
|
||||
.map(|(_, owner, document)| (owner, document))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn project_child_frame_content_documents(
|
||||
&self,
|
||||
snapshot: &mut DocumentNodeSnapshot,
|
||||
@@ -2646,28 +2909,9 @@ impl PageVm {
|
||||
pierce: bool,
|
||||
include_whitespace: bool,
|
||||
) {
|
||||
if !pierce || depth == 0 {
|
||||
return;
|
||||
}
|
||||
let child_documents = self
|
||||
.vm()
|
||||
.live_child_document_handles_in_snapshot_order()
|
||||
.inspector_child_document_handles()
|
||||
.into_iter()
|
||||
.filter(|(_, owner_node_id, _)| {
|
||||
let vm = self.vm();
|
||||
let Some(child_document_url) =
|
||||
vm.child_browsing_context_current_url(*owner_node_id)
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
child_content_document_belongs_to_top_target(
|
||||
vm.document_runtime.document_url(),
|
||||
&child_document_url,
|
||||
vm.child_browsing_context_is_same_origin_with_top(*owner_node_id),
|
||||
vm.child_browsing_context_has_opaque_origin(*owner_node_id),
|
||||
)
|
||||
})
|
||||
.map(|(_, owner_node_id, document_handle)| (owner_node_id, document_handle))
|
||||
.collect::<HashMap<_, _>>();
|
||||
if child_documents.is_empty() {
|
||||
return;
|
||||
@@ -2677,15 +2921,14 @@ impl PageVm {
|
||||
let mut stack = vec![(snapshot, depth)];
|
||||
while let Some((snapshot, depth)) = stack.pop() {
|
||||
let next_depth = if depth > 0 { depth - 1 } else { depth };
|
||||
if depth != 0
|
||||
&& snapshot.is_element
|
||||
if snapshot.is_element
|
||||
&& snapshot.inspector_identity.is_none()
|
||||
&& let Some(document_handle) = child_documents.get(&snapshot.node_id)
|
||||
{
|
||||
snapshot.associated = live_inspector_document_node_snapshot(
|
||||
dom_host,
|
||||
*document_handle,
|
||||
next_depth,
|
||||
if pierce { next_depth } else { 0 },
|
||||
None,
|
||||
pierce,
|
||||
include_whitespace,
|
||||
@@ -2777,6 +3020,21 @@ impl PageVm {
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshot: &mut DocumentNodeSnapshot,
|
||||
include_whitespace: bool,
|
||||
) {
|
||||
self.project_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
snapshot,
|
||||
include_whitespace,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
fn project_renderer_dom_agent_ids_to_snapshot(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshot: &mut DocumentNodeSnapshot,
|
||||
include_whitespace: bool,
|
||||
bind: bool,
|
||||
) {
|
||||
let mut stack = vec![snapshot];
|
||||
while let Some(snapshot) = stack.pop() {
|
||||
@@ -2789,13 +3047,22 @@ impl PageVm {
|
||||
self.renderer_backend_node_id_for_live_handle(snapshot.node_id)
|
||||
};
|
||||
if let Some(backend_node_id) = backend_node_id {
|
||||
let frontend_node_id = self
|
||||
.document_frontend_node_id_for_backend_node_id_in_whitespace_projection(
|
||||
let frontend_node_id = if bind {
|
||||
self.document_frontend_node_id_for_backend_node_id_in_whitespace_projection(
|
||||
inspector_session_id,
|
||||
backend_node_id,
|
||||
include_whitespace,
|
||||
inspector_whitespace_text_snapshot(snapshot),
|
||||
);
|
||||
)
|
||||
} else {
|
||||
// Internal snapshots may allocate handles without sending
|
||||
// nodes to the client. describeNode only exposes published IDs.
|
||||
self.existing_document_frontend_node_id(inspector_session_id, backend_node_id)
|
||||
.filter(|_| {
|
||||
self.document_node_is_published(inspector_session_id, backend_node_id)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
};
|
||||
snapshot.backend_node_id = Some(backend_node_id);
|
||||
snapshot.frontend_node_id = Some(frontend_node_id);
|
||||
}
|
||||
@@ -2807,19 +3074,33 @@ impl PageVm {
|
||||
{
|
||||
self.renderer_backend_node_id_for_inspector_node(snapshot.node_id, parent_identity)
|
||||
.map(|parent_backend_node_id| {
|
||||
self.document_frontend_node_id_for_backend_node_id(
|
||||
self.existing_document_frontend_node_id(
|
||||
inspector_session_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
.filter(|_| {
|
||||
bind || self.document_node_is_published(
|
||||
inspector_session_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
})
|
||||
} else {
|
||||
snapshot.parent_id.and_then(|parent_id| {
|
||||
self.renderer_backend_node_id_for_live_handle(parent_id)
|
||||
.map(|parent_backend_node_id| {
|
||||
self.document_frontend_node_id_for_backend_node_id(
|
||||
self.existing_document_frontend_node_id(
|
||||
inspector_session_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
.filter(|_| {
|
||||
bind || self.document_node_is_published(
|
||||
inspector_session_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
})
|
||||
})
|
||||
};
|
||||
@@ -2844,12 +3125,22 @@ impl PageVm {
|
||||
) -> Option<u32> {
|
||||
let backend_node_id = match reference {
|
||||
RendererDomNodeReference::BackendNodeId(id) => id,
|
||||
RendererDomNodeReference::FrontendNodeId {
|
||||
RendererDomNodeReference::InternalFrontendNodeBinding {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
} => match self
|
||||
.document_frontend_node_binding(inspector_session_id.as_deref(), frontend_node_id)
|
||||
{
|
||||
RendererDomFrontendNodeBindingResolution::BackendNodeId(id) => id,
|
||||
RendererDomFrontendNodeBindingResolution::NotFound => return None,
|
||||
},
|
||||
RendererDomNodeReference::FrontendNodeId {
|
||||
inspector_session_id,
|
||||
frontend_node_id,
|
||||
} => match self.published_document_frontend_node_binding(
|
||||
inspector_session_id.as_deref(),
|
||||
frontend_node_id,
|
||||
) {
|
||||
RendererDomFrontendNodeBindingResolution::BackendNodeId(id) => id,
|
||||
RendererDomFrontendNodeBindingResolution::NotFound => {
|
||||
return None;
|
||||
@@ -3400,7 +3691,8 @@ impl PageVm {
|
||||
handle = parent;
|
||||
}
|
||||
let frame_id = self.vm().child_frame_id_for_live_node_handle(handle);
|
||||
let Some(node) = self.document_node_reference_for_live_handle(inspector_session_id, handle)
|
||||
let Some(node) =
|
||||
self.published_document_node_reference_for_live_handle(inspector_session_id, handle)
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
@@ -3528,52 +3820,131 @@ impl PageVm {
|
||||
Ok(Some(!rects.is_empty()))
|
||||
}
|
||||
|
||||
pub(crate) fn document_frontend_node_ids_for_backend_node_ids(
|
||||
fn document_node_ids_with_publication(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
backend_node_ids: &[u32],
|
||||
) -> RendererDocumentFrontendNodeIdsResolution {
|
||||
let include_whitespace = self.document_dom_agent_includes_whitespace(inspector_session_id);
|
||||
let document_handle = self
|
||||
) -> (
|
||||
RendererDocumentFrontendNodeIdsResolution,
|
||||
Option<RendererDocumentChildNodeSnapshotEvents>,
|
||||
) {
|
||||
// Freeze earlier DOM facts before new nodes become visible to this
|
||||
// session; their notifications must not reference a future publication.
|
||||
self.flush_pending_dom_mutation_event_batches();
|
||||
let root = self
|
||||
.vm()
|
||||
.document_runtime
|
||||
.dom_host()
|
||||
.dom()
|
||||
.document_node_id();
|
||||
let Some(document_backend_node_id) =
|
||||
self.renderer_backend_node_id_for_live_handle(document_handle)
|
||||
else {
|
||||
return RendererDocumentFrontendNodeIdsResolution::DocumentNotBound;
|
||||
};
|
||||
if !self.document_has_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_backend_node_id,
|
||||
) {
|
||||
return RendererDocumentFrontendNodeIdsResolution::DocumentNotBound;
|
||||
if !self
|
||||
.renderer_backend_node_id_for_live_handle(root)
|
||||
.is_some_and(|id| self.document_node_is_published(inspector_session_id, id))
|
||||
{
|
||||
return (
|
||||
RendererDocumentFrontendNodeIdsResolution::DocumentNotBound,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
RendererDocumentFrontendNodeIdsResolution::Found(
|
||||
backend_node_ids
|
||||
.iter()
|
||||
.map(|backend_node_id| {
|
||||
let handle = self.live_handle_for_backend_node_id(*backend_node_id)?;
|
||||
let backend_node_id = self.renderer_backend_node_id_for_live_handle(handle)?;
|
||||
let is_whitespace_text = inspector_whitespace_text_node(
|
||||
let include_whitespace = self.document_dom_agent_includes_whitespace(inspector_session_id);
|
||||
let mut ids = vec![None; backend_node_ids.len()];
|
||||
let mut pending = Vec::new();
|
||||
for (index, &backend_node_id) in backend_node_ids.iter().enumerate() {
|
||||
let Some(key) = self.current_renderer_backend_node_key_for_id(backend_node_id) else {
|
||||
continue;
|
||||
};
|
||||
let visible = match key.inspector_identity {
|
||||
None => {
|
||||
include_whitespace
|
||||
|| !inspector_whitespace_text_node(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
key.handle,
|
||||
)
|
||||
}
|
||||
Some(DocumentNodeInspectorIdentity::MarkerPseudoElement) => self
|
||||
.vm()
|
||||
.marker_pseudo_element_is_generated_for_document_snapshot(key.handle),
|
||||
Some(identity @ DocumentNodeInspectorIdentity::UserAgentShadowTreeNode { .. }) => {
|
||||
live_document_node_snapshot(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
handle,
|
||||
);
|
||||
Some(self
|
||||
.document_frontend_node_id_for_backend_node_id_in_whitespace_projection(
|
||||
inspector_session_id,
|
||||
backend_node_id,
|
||||
include_whitespace,
|
||||
is_whitespace_text,
|
||||
))
|
||||
})
|
||||
.collect(),
|
||||
key.handle,
|
||||
0,
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.and_then(|host| {
|
||||
user_agent_shadow_node_snapshot(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
&host,
|
||||
identity,
|
||||
0,
|
||||
true,
|
||||
)
|
||||
})
|
||||
.is_some_and(|snapshot| {
|
||||
include_whitespace || !inspector_whitespace_text_snapshot(&snapshot)
|
||||
})
|
||||
}
|
||||
};
|
||||
if !visible {
|
||||
continue;
|
||||
}
|
||||
if self.document_node_is_published(inspector_session_id, backend_node_id) {
|
||||
ids[index] =
|
||||
self.existing_document_frontend_node_id(inspector_session_id, backend_node_id);
|
||||
} else {
|
||||
pending.push((index, backend_node_id));
|
||||
}
|
||||
}
|
||||
let events = self.document_node_path_snapshot_events(
|
||||
inspector_session_id,
|
||||
root,
|
||||
pending.iter().map(|(_, id)| *id),
|
||||
);
|
||||
for (index, backend_node_id) in pending {
|
||||
if self.document_node_is_published(inspector_session_id, backend_node_id) {
|
||||
ids[index] =
|
||||
self.existing_document_frontend_node_id(inspector_session_id, backend_node_id);
|
||||
}
|
||||
}
|
||||
(
|
||||
RendererDocumentFrontendNodeIdsResolution::Found(ids),
|
||||
events,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn document_frontend_node_ids_for_backend_node_ids(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
backend_node_ids: &[u32],
|
||||
) -> RendererDocumentFrontendNodeIdsResolution {
|
||||
let (resolution, events) =
|
||||
self.document_node_ids_with_publication(inspector_session_id, backend_node_ids);
|
||||
if let Some(events) = events {
|
||||
self.queue_document_child_snapshot_events(inspector_session_id, events);
|
||||
}
|
||||
resolution
|
||||
}
|
||||
|
||||
fn published_document_node_reference_for_live_handle(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
handle: DomHandle,
|
||||
) -> Option<RendererDocumentNodeReference> {
|
||||
let backend_node_id = self.renderer_backend_node_id_for_live_handle(handle)?;
|
||||
let node_id = match self.document_frontend_node_ids_for_backend_node_ids(
|
||||
inspector_session_id,
|
||||
&[backend_node_id],
|
||||
) {
|
||||
RendererDocumentFrontendNodeIdsResolution::Found(ids) => ids[0].unwrap_or(0),
|
||||
RendererDocumentFrontendNodeIdsResolution::DocumentNotBound => 0,
|
||||
};
|
||||
Some(RendererDocumentNodeReference {
|
||||
node_id,
|
||||
backend_node_id,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn child_frame_owner_node_reference_by_frame_id(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -3582,7 +3953,7 @@ impl PageVm {
|
||||
let handle = self
|
||||
.vm()
|
||||
.child_browsing_context_owner_node_id_by_frame_id(frame_id)?;
|
||||
self.document_node_reference_for_live_handle(inspector_session_id, handle)
|
||||
self.published_document_node_reference_for_live_handle(inspector_session_id, handle)
|
||||
}
|
||||
|
||||
pub(crate) fn child_frame_document_root_node_reference_by_frame_id(
|
||||
@@ -3980,7 +4351,7 @@ impl PageVm {
|
||||
frontend_node_id: u32,
|
||||
) -> Option<DomHandle> {
|
||||
let crate::RendererDomFrontendNodeBindingResolution::BackendNodeId(backend_node_id) =
|
||||
self.document_frontend_node_binding(inspector_session_id, frontend_node_id)
|
||||
self.published_document_frontend_node_binding(inspector_session_id, frontend_node_id)
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
@@ -4339,15 +4339,22 @@ pub struct RendererRuntimeRealmInfo {
|
||||
}
|
||||
|
||||
/// A native DOM-agent reference, resolved against the current Page document.
|
||||
/// Frontend IDs belong to one Inspector session; backend IDs use the Page's
|
||||
/// live node registry. Inspector objects and WebDriver/BiDi handles require
|
||||
/// their own resolution and cannot enter this native-only query boundary.
|
||||
/// CDP frontend IDs must be published in their Inspector session. Internal
|
||||
/// automation may explicitly resolve an unpublished binding. Backend IDs use
|
||||
/// the Page's live node registry; protocol object handles require their own
|
||||
/// resolution before entering this boundary.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RendererDomNodeReference {
|
||||
/// A CDP node ID that must already be visible to this session.
|
||||
FrontendNodeId {
|
||||
inspector_session_id: Option<String>,
|
||||
frontend_node_id: u32,
|
||||
},
|
||||
/// An internal automation binding; it does not imply CDP publication.
|
||||
InternalFrontendNodeBinding {
|
||||
inspector_session_id: Option<String>,
|
||||
frontend_node_id: u32,
|
||||
},
|
||||
BackendNodeId(u32),
|
||||
}
|
||||
|
||||
@@ -4385,7 +4392,6 @@ pub enum RendererDocumentQuerySelectorResolution {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct RendererDocumentQuerySelectorNode {
|
||||
pub live_node_id: crate::dom::NodeId,
|
||||
pub frontend_node_id: u32,
|
||||
pub backend_node_id: u32,
|
||||
}
|
||||
@@ -4706,6 +4712,11 @@ pub(crate) enum RendererInspectorPageCommand {
|
||||
object_id: String,
|
||||
depth: i32,
|
||||
pierce: bool,
|
||||
bind_frontend_nodes: bool,
|
||||
},
|
||||
RequestDocumentNodeForObjectId {
|
||||
include_whitespace: bool,
|
||||
object_id: String,
|
||||
},
|
||||
AccessibilityTreePayloadsForObjectId {
|
||||
object_id: String,
|
||||
@@ -5040,8 +5051,7 @@ pub enum RendererPageCommand {
|
||||
frontend_node_id: u32,
|
||||
},
|
||||
DocumentFrontendNodeBinding {
|
||||
inspector_session_id: Option<String>,
|
||||
frontend_node_id: u32,
|
||||
reference: RendererDomNodeReference,
|
||||
},
|
||||
RegisterDocumentBidiNodeBinding {
|
||||
inspector_session_id: Option<String>,
|
||||
@@ -5517,6 +5527,7 @@ impl RendererPageCommand {
|
||||
object_id: String,
|
||||
depth: i32,
|
||||
pierce: bool,
|
||||
bind_frontend_nodes: bool,
|
||||
) -> Self {
|
||||
Self::inspector_command(
|
||||
inspector_session_id,
|
||||
@@ -5525,6 +5536,21 @@ impl RendererPageCommand {
|
||||
object_id,
|
||||
depth,
|
||||
pierce,
|
||||
bind_frontend_nodes,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn request_document_node_for_object_id(
|
||||
inspector_session_id: Option<String>,
|
||||
include_whitespace: bool,
|
||||
object_id: String,
|
||||
) -> Self {
|
||||
Self::inspector_command(
|
||||
inspector_session_id,
|
||||
RendererInspectorPageCommand::RequestDocumentNodeForObjectId {
|
||||
include_whitespace,
|
||||
object_id,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -6044,6 +6070,10 @@ pub enum RendererPageReply {
|
||||
ExecutionContextIds(Vec<i64>),
|
||||
OptionalExecutionContextId(Option<i64>),
|
||||
OptionalDocumentNodeObjectSnapshot(Box<Option<DocumentNodeObjectSnapshot>>),
|
||||
DocumentNodeRequested {
|
||||
node_id: Option<u32>,
|
||||
child_node_snapshot_events: Option<RendererDocumentChildNodeSnapshotEvents>,
|
||||
},
|
||||
OptionalDomSnapshotCapturePayload(Option<RendererDomSnapshotCapturePayload>),
|
||||
OptionalDocumentChildNodeSnapshots(Option<RendererDocumentChildNodeSnapshots>),
|
||||
OptionalDocumentChildNodeSnapshotEvents(Option<RendererDocumentChildNodeSnapshotEvents>),
|
||||
@@ -6053,7 +6083,10 @@ pub enum RendererPageReply {
|
||||
RendererDocumentQuerySelectorWithChildNodeSnapshotEvents,
|
||||
),
|
||||
DocumentPerformSearch(RendererDomSearchRegistration),
|
||||
DocumentSearchResults(RendererDomSearchResultsResolution),
|
||||
DocumentSearchResults {
|
||||
resolution: RendererDomSearchResultsResolution,
|
||||
child_node_snapshot_events: Option<RendererDocumentChildNodeSnapshotEvents>,
|
||||
},
|
||||
DocumentSearchResultsDiscarded,
|
||||
DocumentNodeStackTracesEnabled,
|
||||
DocumentNodeStackTrace(RendererDomNodeStackTraceResolution),
|
||||
|
||||
@@ -231,6 +231,28 @@ impl RendererDomAgentState {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn mark_node_published(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<DocumentId>,
|
||||
backend_node_id: u32,
|
||||
) {
|
||||
self.with_session_mut(inspector_session_id, document_id, |state| {
|
||||
state.published_nodes.insert(backend_node_id);
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) fn node_is_published(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<DocumentId>,
|
||||
backend_node_id: u32,
|
||||
) -> bool {
|
||||
self.with_session_mut(inspector_session_id, document_id, |state| {
|
||||
state.published_nodes.contains(&backend_node_id)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn frontend_node_id_for_existing_backend_node_id(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -242,6 +264,20 @@ impl RendererDomAgentState {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn published_frontend_node_id_for_backend_node_id(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<DocumentId>,
|
||||
backend_node_id: u32,
|
||||
) -> Option<u32> {
|
||||
self.with_session_mut(inspector_session_id, document_id, |state| {
|
||||
if !state.published_nodes.contains(&backend_node_id) {
|
||||
return None;
|
||||
}
|
||||
state.frontend_node_id_for_existing_backend_node_id(backend_node_id)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn remove_frontend_bindings_for_backend_node_ids(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -380,10 +416,10 @@ impl RendererDomAgentState {
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<DocumentId>,
|
||||
nodes: Vec<crate::RendererDomSearchResultNode>,
|
||||
backend_node_ids: Vec<u32>,
|
||||
) -> crate::RendererDomSearchRegistration {
|
||||
self.with_session_mut(inspector_session_id, document_id, |state| {
|
||||
state.register_search_results(nodes)
|
||||
state.register_search_results(backend_node_ids)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -468,7 +504,12 @@ pub(super) struct RendererDomAgentSessionState {
|
||||
frontend_node_bindings: RendererFrontendNodeBindings,
|
||||
bidi_node_bindings: HashMap<String, u32>,
|
||||
bidi_node_shared_ids_by_backend_node_id: HashMap<u32, String>,
|
||||
search_results: HashMap<String, Vec<crate::RendererDomSearchResultNode>>,
|
||||
// Search matches do not allocate frontend IDs. Only reading a range can
|
||||
// publish its nodes and expose their IDs to the client.
|
||||
search_results: HashMap<String, Vec<u32>>,
|
||||
// A binding may be allocated while preparing a snapshot. Only DOM tree
|
||||
// responses/notifications make it a valid path anchor for the client.
|
||||
published_nodes: HashSet<u32>,
|
||||
children_requested: HashSet<u32>,
|
||||
cached_child_counts: HashMap<u32, usize>,
|
||||
capture_node_stack_traces: bool,
|
||||
@@ -491,6 +532,7 @@ impl RendererDomAgentSessionState {
|
||||
self.bidi_node_bindings.clear();
|
||||
self.bidi_node_shared_ids_by_backend_node_id.clear();
|
||||
self.search_results.clear();
|
||||
self.published_nodes.clear();
|
||||
self.children_requested.clear();
|
||||
self.cached_child_counts.clear();
|
||||
}
|
||||
@@ -535,19 +577,11 @@ impl RendererDomAgentSessionState {
|
||||
fn remove_frontend_binding_for_backend_node_id(&mut self, backend_node_id: u32) {
|
||||
self.frontend_node_bindings
|
||||
.remove_backend_node_id(backend_node_id);
|
||||
self.published_nodes.remove(&backend_node_id);
|
||||
self.children_requested.remove(&backend_node_id);
|
||||
self.cached_child_counts.remove(&backend_node_id);
|
||||
}
|
||||
|
||||
pub(super) fn register_frontend_node_binding(
|
||||
&mut self,
|
||||
frontend_node_id: u32,
|
||||
backend_node_id: u32,
|
||||
) {
|
||||
self.frontend_node_bindings
|
||||
.register_explicit(frontend_node_id, backend_node_id);
|
||||
}
|
||||
|
||||
pub(super) fn frontend_node_id_for_backend_node_id(&mut self, backend_node_id: u32) -> u32 {
|
||||
self.frontend_node_bindings
|
||||
.id_for_backend_node_id(backend_node_id)
|
||||
@@ -598,19 +632,13 @@ impl RendererDomAgentSessionState {
|
||||
|
||||
pub(super) fn register_search_results(
|
||||
&mut self,
|
||||
nodes: Vec<crate::RendererDomSearchResultNode>,
|
||||
backend_node_ids: Vec<u32>,
|
||||
) -> crate::RendererDomSearchRegistration {
|
||||
for node in &nodes {
|
||||
// Zero is Chromium's placeholder for a search result hidden from
|
||||
// this session, not a frontend node identity.
|
||||
if node.frontend_node_id != 0 {
|
||||
self.register_frontend_node_binding(node.frontend_node_id, node.backend_node_id);
|
||||
}
|
||||
}
|
||||
let search_id = self.next_search_id.to_string();
|
||||
self.next_search_id += 1;
|
||||
let result_count = nodes.len() as u32;
|
||||
self.search_results.insert(search_id.clone(), nodes);
|
||||
let result_count = backend_node_ids.len() as u32;
|
||||
self.search_results
|
||||
.insert(search_id.clone(), backend_node_ids);
|
||||
crate::RendererDomSearchRegistration {
|
||||
search_id,
|
||||
result_count,
|
||||
@@ -639,7 +667,15 @@ impl RendererDomAgentSessionState {
|
||||
if to_index > results.len() {
|
||||
return crate::RendererDomSearchResultsResolution::BadToIndex;
|
||||
}
|
||||
crate::RendererDomSearchResultsResolution::Found(results[from_index..to_index].to_vec())
|
||||
crate::RendererDomSearchResultsResolution::Found(
|
||||
results[from_index..to_index]
|
||||
.iter()
|
||||
.map(|&backend_node_id| crate::RendererDomSearchResultNode {
|
||||
frontend_node_id: 0,
|
||||
backend_node_id,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -877,10 +913,7 @@ mod tests {
|
||||
let search = state.register_search_results(
|
||||
Some("session-1"),
|
||||
Some(document_id(1)),
|
||||
vec![crate::RendererDomSearchResultNode {
|
||||
frontend_node_id: 9,
|
||||
backend_node_id,
|
||||
}],
|
||||
vec![backend_node_id],
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
@@ -916,16 +949,13 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_zero_placeholder_does_not_create_a_frontend_binding() {
|
||||
fn search_results_do_not_allocate_frontend_bindings() {
|
||||
let state = RendererDomAgentState::default();
|
||||
let backend_node_id = state.backend_node_id_for_node(document_id(1), dom_handle(7));
|
||||
let search = state.register_search_results(
|
||||
Some("session-1"),
|
||||
Some(document_id(1)),
|
||||
vec![crate::RendererDomSearchResultNode {
|
||||
frontend_node_id: 0,
|
||||
backend_node_id,
|
||||
}],
|
||||
vec![backend_node_id],
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
@@ -941,9 +971,13 @@ mod tests {
|
||||
&& nodes[0].frontend_node_id == 0
|
||||
&& nodes[0].backend_node_id == backend_node_id
|
||||
));
|
||||
assert_eq!(
|
||||
state.frontend_node_binding(Some("session-1"), Some(document_id(1)), 0),
|
||||
crate::RendererDomFrontendNodeBindingResolution::NotFound
|
||||
assert!(!state.has_frontend_node_id_for_backend_node_id(
|
||||
Some("session-1"),
|
||||
Some(document_id(1)),
|
||||
backend_node_id,
|
||||
));
|
||||
assert!(
|
||||
!state.node_is_published(Some("session-1"), Some(document_id(1)), backend_node_id,)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,177 @@
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
|
||||
use crate::document_runtime::{DevToolsDomChildListMutationFact, DevToolsDomMutationFact};
|
||||
use crate::dom::native::NodeType;
|
||||
use crate::runtime::{
|
||||
RendererDocumentLifecycleIdentity, RendererDomMutationEvent, RendererDomMutationEventBatch,
|
||||
};
|
||||
use moli_page_types::{DevToolsSessionKey, DocumentNodeSnapshot};
|
||||
use moli_page_types::{
|
||||
DevToolsSessionKey, DocumentNodeAssociatedSnapshot, DocumentNodeSnapshot,
|
||||
MAX_DOM_OUTPUT_TREE_DEPTH,
|
||||
};
|
||||
|
||||
use super::PageVm;
|
||||
|
||||
impl PageVm {
|
||||
/// Restore expanded children in subsequent notifications when a template
|
||||
/// fragment or the wire depth limit prevents including them in this one.
|
||||
pub(crate) fn append_document_child_snapshot_event(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
event: crate::runtime::RendererDocumentChildNodeSnapshotEvent,
|
||||
depth: i32,
|
||||
events: &mut Vec<crate::runtime::RendererDocumentChildNodeSnapshotEvent>,
|
||||
) {
|
||||
let mut pending = VecDeque::from([event]);
|
||||
while let Some(mut event) = pending.pop_front() {
|
||||
for snapshot in &mut event.snapshots {
|
||||
self.preserve_requested_document_children(inspector_session_id, snapshot);
|
||||
let mut stack = vec![(&mut *snapshot, MAX_DOM_OUTPUT_TREE_DEPTH)];
|
||||
while let Some((node, remaining_depth)) = stack.pop() {
|
||||
let next_depth = remaining_depth.saturating_sub(1);
|
||||
// Leave room for an associated root. Its own child list
|
||||
// can be restored after the enclosing snapshot is sent.
|
||||
if remaining_depth <= 1 {
|
||||
if let Some(children) = self
|
||||
.take_expanded_document_child_snapshot_event(inspector_session_id, node)
|
||||
{
|
||||
pending.push_back(children);
|
||||
}
|
||||
} else {
|
||||
stack.extend(
|
||||
node.children
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth)),
|
||||
);
|
||||
}
|
||||
stack.extend(
|
||||
node.shadow_roots
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth)),
|
||||
);
|
||||
stack.extend(
|
||||
node.pseudo_elements
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth)),
|
||||
);
|
||||
match node.associated.as_deref_mut() {
|
||||
Some(DocumentNodeAssociatedSnapshot::TemplateContent(content)) => {
|
||||
if let Some(children) = self
|
||||
.take_expanded_document_child_snapshot_event(
|
||||
inspector_session_id,
|
||||
content,
|
||||
)
|
||||
{
|
||||
pending.push_back(children);
|
||||
}
|
||||
}
|
||||
Some(DocumentNodeAssociatedSnapshot::ContentDocument(document)) => {
|
||||
stack.push((document, next_depth))
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
self.mark_document_snapshot_published(inspector_session_id, snapshot, depth);
|
||||
}
|
||||
events.push(event);
|
||||
}
|
||||
}
|
||||
|
||||
fn take_expanded_document_child_snapshot_event(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshot: &mut DocumentNodeSnapshot,
|
||||
) -> Option<crate::runtime::RendererDocumentChildNodeSnapshotEvent> {
|
||||
if snapshot.children.is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.mark_document_node_children_requested(
|
||||
inspector_session_id,
|
||||
snapshot
|
||||
.backend_node_id
|
||||
.expect("child-list backend identity"),
|
||||
snapshot.children.len(),
|
||||
);
|
||||
Some(crate::runtime::RendererDocumentChildNodeSnapshotEvent {
|
||||
parent_frontend_node_id: snapshot.frontend_node_id.expect("bound child-list parent"),
|
||||
snapshots: std::mem::take(&mut snapshot.children),
|
||||
})
|
||||
}
|
||||
|
||||
/// Publish requested Inspector paths through the same before-response
|
||||
/// output journal as DOM mutation snapshots, for native and legacy commands.
|
||||
pub(crate) fn queue_document_child_snapshot_events(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshots: crate::runtime::RendererDocumentChildNodeSnapshotEvents,
|
||||
) {
|
||||
if snapshots.events.is_empty() {
|
||||
return;
|
||||
}
|
||||
let events = snapshots
|
||||
.events
|
||||
.into_iter()
|
||||
.map(|event| RendererDomMutationEvent::SetChildNodes {
|
||||
parent_node_id: event.parent_frontend_node_id,
|
||||
nodes: event.snapshots,
|
||||
})
|
||||
.collect();
|
||||
self.pending_dom_mutation_event_batches
|
||||
.push(RendererDomMutationEventBatch::new(
|
||||
self.devtools_agent_token(),
|
||||
DevToolsSessionKey::from_wire_session_id(inspector_session_id),
|
||||
events,
|
||||
));
|
||||
}
|
||||
|
||||
/// setChildNodes replaces the client's node objects. Keep the children it
|
||||
/// already expanded when a shallow snapshot refreshes an associated tree.
|
||||
pub(crate) fn preserve_requested_document_children(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshot: &mut DocumentNodeSnapshot,
|
||||
) {
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
let include_whitespace = self.document_dom_agent_includes_whitespace(inspector_session_id);
|
||||
let mut stack = vec![snapshot];
|
||||
while let Some(snapshot) = stack.pop() {
|
||||
if snapshot.children.len() < snapshot.child_count
|
||||
&& let Some(backend_node_id) = snapshot.backend_node_id
|
||||
&& self.dom_agent_state.children_requested(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_id,
|
||||
)
|
||||
&& let Some(mut expanded) = self
|
||||
.document_node_snapshot_for_backend_node_id_in_inspector_session(
|
||||
inspector_session_id,
|
||||
backend_node_id,
|
||||
1,
|
||||
false,
|
||||
)
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut expanded.snapshot,
|
||||
include_whitespace,
|
||||
);
|
||||
snapshot.children = expanded.snapshot.children;
|
||||
snapshot.child_count = expanded.snapshot.child_count;
|
||||
}
|
||||
stack.extend(snapshot.children.iter_mut().rev());
|
||||
stack.extend(snapshot.shadow_roots.iter_mut().rev());
|
||||
stack.extend(snapshot.pseudo_elements.iter_mut().rev());
|
||||
if let Some(associated) = snapshot.associated.as_deref_mut() {
|
||||
stack.push(associated.node_mut());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn sync_devtools_dom_mutation_recording_interest(&mut self) {
|
||||
let enabled = self.dom_agent_state.has_frontend_bindings();
|
||||
self.vm()
|
||||
@@ -18,25 +180,37 @@ impl PageVm {
|
||||
.set_devtools_mutation_records_enabled(enabled);
|
||||
}
|
||||
|
||||
pub(crate) fn mark_document_snapshot_children_requested(
|
||||
/// Records a snapshot committed to a DOM tree response or notification.
|
||||
/// A describeNode response does not populate the client's DOM mirror.
|
||||
pub(crate) fn mark_document_snapshot_published(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
snapshot: &DocumentNodeSnapshot,
|
||||
depth: i32,
|
||||
) {
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
let mut stack = vec![(snapshot, depth)];
|
||||
while let Some((snapshot, remaining_depth)) = stack.pop() {
|
||||
let mut stack = vec![(snapshot, depth, true)];
|
||||
while let Some((snapshot, remaining_depth, expand)) = stack.pop() {
|
||||
let forced_single_text_child = remaining_depth == 0
|
||||
&& matches!(snapshot.children.as_slice(), [child] if child.node_type == NodeType::Text as u8);
|
||||
if let Some(backend_node_id) = snapshot.backend_node_id {
|
||||
if snapshot.frontend_node_id.is_some_and(|id| id != 0) {
|
||||
self.dom_agent_state.mark_node_published(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_id,
|
||||
);
|
||||
}
|
||||
self.dom_agent_state.cache_child_count(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_id,
|
||||
snapshot.child_count,
|
||||
);
|
||||
if remaining_depth != 0 || forced_single_text_child {
|
||||
if expand
|
||||
&& snapshot.children.len() == snapshot.child_count
|
||||
&& (remaining_depth != 0 || forced_single_text_child)
|
||||
{
|
||||
self.dom_agent_state.mark_children_requested(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
@@ -45,10 +219,7 @@ impl PageVm {
|
||||
);
|
||||
}
|
||||
}
|
||||
if remaining_depth == 0 {
|
||||
if forced_single_text_child {
|
||||
stack.extend(snapshot.children.iter().rev().map(|child| (child, 0)));
|
||||
}
|
||||
if !expand {
|
||||
continue;
|
||||
}
|
||||
let next_depth = if remaining_depth > 0 {
|
||||
@@ -56,23 +227,52 @@ impl PageVm {
|
||||
} else {
|
||||
remaining_depth
|
||||
};
|
||||
if let Some(associated) = snapshot.associated.as_deref() {
|
||||
stack.push((
|
||||
associated.node(),
|
||||
next_depth,
|
||||
matches!(
|
||||
associated,
|
||||
DocumentNodeAssociatedSnapshot::ContentDocument(_)
|
||||
),
|
||||
));
|
||||
}
|
||||
stack.extend(
|
||||
snapshot
|
||||
.pseudo_elements
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth, true)),
|
||||
);
|
||||
stack.extend(
|
||||
snapshot
|
||||
.shadow_roots
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth)),
|
||||
.map(|child| (child, next_depth, true)),
|
||||
);
|
||||
stack.extend(
|
||||
snapshot
|
||||
.children
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|child| (child, next_depth)),
|
||||
.map(|child| (child, next_depth, true)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn document_node_children_requested(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
backend_node_id: u32,
|
||||
) -> bool {
|
||||
self.dom_agent_state.children_requested(
|
||||
inspector_session_id,
|
||||
self.current_dom_agent_document_id(),
|
||||
backend_node_id,
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn mark_document_node_children_requested(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -195,7 +395,7 @@ impl PageVm {
|
||||
name,
|
||||
value,
|
||||
} => {
|
||||
let Some(node_id) = self.existing_frontend_node_id_for_handle(
|
||||
let Some(node_id) = self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
*target,
|
||||
@@ -241,7 +441,7 @@ impl PageVm {
|
||||
}
|
||||
DevToolsDomMutationFact::ChildList(mutation) => {
|
||||
if resynchronized_parents.contains(&mutation.target) {
|
||||
self.remove_finally_detached_bindings_for_resynchronized_parent(
|
||||
self.remove_finally_detached_bindings(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
mutation,
|
||||
@@ -287,7 +487,7 @@ impl PageVm {
|
||||
|| !crate::runtime::page_dom::inspector_whitespace_text_value(value);
|
||||
if old_visible == new_visible {
|
||||
if new_visible
|
||||
&& let Some(node_id) = self.existing_frontend_node_id_for_handle(
|
||||
&& let Some(node_id) = self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
target,
|
||||
@@ -319,7 +519,7 @@ impl PageVm {
|
||||
};
|
||||
let Some(parent_node_id) = self
|
||||
.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
parent_backend_node_id,
|
||||
@@ -358,7 +558,7 @@ impl PageVm {
|
||||
child_count,
|
||||
);
|
||||
if children_requested {
|
||||
if let Some(node_id) = self.existing_frontend_node_id_for_handle(
|
||||
if let Some(node_id) = self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
target,
|
||||
@@ -371,8 +571,6 @@ impl PageVm {
|
||||
self.resynchronize_child_nodes_for_session(
|
||||
inspector_session_id,
|
||||
parent,
|
||||
parent_node_id,
|
||||
include_whitespace,
|
||||
events,
|
||||
"visible text node has no frontend binding",
|
||||
);
|
||||
@@ -420,7 +618,7 @@ impl PageVm {
|
||||
let previous_node_id = match previous_sibling_without_whitespace {
|
||||
None => 0,
|
||||
Some(previous_sibling) => {
|
||||
let Some(previous_node_id) = self.existing_frontend_node_id_for_handle(
|
||||
let Some(previous_node_id) = self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
previous_sibling,
|
||||
@@ -428,8 +626,6 @@ impl PageVm {
|
||||
self.resynchronize_child_nodes_for_session(
|
||||
inspector_session_id,
|
||||
parent,
|
||||
parent_node_id,
|
||||
include_whitespace,
|
||||
events,
|
||||
"previous visible sibling has no frontend binding",
|
||||
);
|
||||
@@ -448,13 +644,12 @@ impl PageVm {
|
||||
self.resynchronize_child_nodes_for_session(
|
||||
inspector_session_id,
|
||||
parent,
|
||||
parent_node_id,
|
||||
include_whitespace,
|
||||
events,
|
||||
"visible text node has no frontend binding after insertion",
|
||||
);
|
||||
return;
|
||||
}
|
||||
self.mark_document_snapshot_published(inspector_session_id, &snapshot, 0);
|
||||
events.push(RendererDomMutationEvent::ChildNodeInserted {
|
||||
parent_node_id,
|
||||
previous_node_id,
|
||||
@@ -477,12 +672,22 @@ impl PageVm {
|
||||
};
|
||||
let Some(parent_node_id) = self
|
||||
.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
else {
|
||||
if self
|
||||
.dom_agent_state
|
||||
.has_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
parent_backend_node_id,
|
||||
)
|
||||
{
|
||||
self.remove_finally_detached_bindings(inspector_session_id, document_id, mutation);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
let children_requested = self.dom_agent_state.children_requested(
|
||||
@@ -550,7 +755,7 @@ impl PageVm {
|
||||
);
|
||||
if children_requested
|
||||
&& let Some(node_id) = removed_root.and_then(|handle| {
|
||||
self.existing_frontend_node_id_for_handle(
|
||||
self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
handle,
|
||||
@@ -589,7 +794,7 @@ impl PageVm {
|
||||
match previous_sibling {
|
||||
None => 0,
|
||||
Some(previous_sibling) => {
|
||||
let Some(previous_node_id) = self.existing_frontend_node_id_for_handle(
|
||||
let Some(previous_node_id) = self.published_frontend_node_id_for_handle(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
previous_sibling,
|
||||
@@ -597,8 +802,6 @@ impl PageVm {
|
||||
return self.resynchronize_child_nodes_for_session(
|
||||
inspector_session_id,
|
||||
mutation.target,
|
||||
parent_node_id,
|
||||
include_whitespace,
|
||||
events,
|
||||
"previous sibling has no frontend binding",
|
||||
);
|
||||
@@ -633,12 +836,11 @@ impl PageVm {
|
||||
return self.resynchronize_child_nodes_for_session(
|
||||
inspector_session_id,
|
||||
mutation.target,
|
||||
parent_node_id,
|
||||
include_whitespace,
|
||||
events,
|
||||
"inserted node has no frontend binding",
|
||||
);
|
||||
};
|
||||
self.mark_document_snapshot_published(inspector_session_id, &snapshot, 0);
|
||||
events.push(RendererDomMutationEvent::ChildNodeInserted {
|
||||
parent_node_id,
|
||||
previous_node_id,
|
||||
@@ -655,7 +857,7 @@ impl PageVm {
|
||||
false
|
||||
}
|
||||
|
||||
fn remove_finally_detached_bindings_for_resynchronized_parent(
|
||||
fn remove_finally_detached_bindings(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<crate::frame_owner_model::DocumentId>,
|
||||
@@ -692,91 +894,32 @@ impl PageVm {
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
parent: crate::dom::native::NativeNodeId,
|
||||
parent_node_id: u32,
|
||||
include_whitespace: bool,
|
||||
events: &mut Vec<RendererDomMutationEvent>,
|
||||
reason: &'static str,
|
||||
) -> bool {
|
||||
let child_handles = {
|
||||
let dom_host = self.vm().document_runtime.dom_host();
|
||||
if dom_host.node(parent).is_none() {
|
||||
tracing::warn!(
|
||||
?parent,
|
||||
reason,
|
||||
"could not resynchronize DOM child bindings"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
dom_host
|
||||
.child_handles(parent)
|
||||
.filter(|&handle| {
|
||||
include_whitespace
|
||||
|| !crate::runtime::page_dom::inspector_whitespace_text_node(
|
||||
dom_host, handle,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let mut nodes = Vec::with_capacity(child_handles.len());
|
||||
for child in child_handles {
|
||||
let Some(mut snapshot) =
|
||||
crate::runtime::page_dom::live_inspector_document_node_snapshot(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
child,
|
||||
0,
|
||||
Some(parent),
|
||||
false,
|
||||
include_whitespace,
|
||||
)
|
||||
else {
|
||||
tracing::warn!(
|
||||
?parent,
|
||||
?child,
|
||||
reason,
|
||||
"could not snapshot DOM child for resynchronization"
|
||||
);
|
||||
return false;
|
||||
};
|
||||
self.assign_renderer_dom_agent_ids_to_snapshot(
|
||||
inspector_session_id,
|
||||
&mut snapshot,
|
||||
include_whitespace,
|
||||
);
|
||||
if snapshot.frontend_node_id.is_none() {
|
||||
tracing::warn!(
|
||||
?parent,
|
||||
?child,
|
||||
reason,
|
||||
"could not bind DOM child for resynchronization"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
nodes.push(snapshot);
|
||||
}
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
let Some(parent_backend_node_id) = self.renderer_backend_node_id_for_live_handle(parent)
|
||||
else {
|
||||
let Some(snapshots) = self.document_child_node_snapshot_events_for_live_handles(
|
||||
inspector_session_id,
|
||||
&[parent],
|
||||
0,
|
||||
false,
|
||||
) else {
|
||||
tracing::warn!(
|
||||
?parent,
|
||||
reason,
|
||||
"could not bind DOM parent for resynchronization"
|
||||
"could not resynchronize Inspector child snapshots"
|
||||
);
|
||||
return false;
|
||||
};
|
||||
self.dom_agent_state.cache_child_count(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
parent_backend_node_id,
|
||||
nodes.len(),
|
||||
);
|
||||
events.push(RendererDomMutationEvent::SetChildNodes {
|
||||
parent_node_id,
|
||||
nodes,
|
||||
});
|
||||
events.extend(snapshots.events.into_iter().map(|event| {
|
||||
RendererDomMutationEvent::SetChildNodes {
|
||||
parent_node_id: event.parent_frontend_node_id,
|
||||
nodes: event.snapshots,
|
||||
}
|
||||
}));
|
||||
true
|
||||
}
|
||||
|
||||
fn existing_frontend_node_id_for_handle(
|
||||
fn published_frontend_node_id_for_handle(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
document_id: Option<crate::frame_owner_model::DocumentId>,
|
||||
@@ -784,7 +927,7 @@ impl PageVm {
|
||||
) -> Option<u32> {
|
||||
let backend_node_id = self.renderer_backend_node_id_for_live_handle(handle)?;
|
||||
self.dom_agent_state
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
.published_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_id,
|
||||
|
||||
@@ -16,7 +16,6 @@ pub(super) enum DocumentSearchMatch {
|
||||
Generated {
|
||||
host: DomHandle,
|
||||
identity: DocumentNodeInspectorIdentity,
|
||||
is_whitespace_text: bool,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -35,10 +34,9 @@ impl PageVm {
|
||||
.document_node_id(),
|
||||
];
|
||||
documents.extend(
|
||||
self.vm()
|
||||
.live_child_document_handles_in_snapshot_order()
|
||||
self.inspector_child_document_handles()
|
||||
.into_iter()
|
||||
.map(|(_, _, document)| document),
|
||||
.map(|(_, document)| document),
|
||||
);
|
||||
let matches = search_document_matches(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
@@ -232,8 +230,6 @@ fn generated_search_match(
|
||||
Some(DocumentSearchMatch::Generated {
|
||||
host,
|
||||
identity: snapshot.inspector_identity?,
|
||||
is_whitespace_text: snapshot.node_type == NodeType::Text as u8
|
||||
&& snapshot.node_value.trim().is_empty(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -25,27 +25,6 @@ impl RendererFrontendNodeBindings {
|
||||
self.backend_to_frontend.clear();
|
||||
}
|
||||
|
||||
pub(super) fn register_explicit(&mut self, frontend_node_id: u32, backend_node_id: u32) {
|
||||
if let Some(old_backend_node_id) = self
|
||||
.frontend_to_backend
|
||||
.insert(frontend_node_id, backend_node_id)
|
||||
&& old_backend_node_id != backend_node_id
|
||||
{
|
||||
self.backend_to_frontend.remove(&old_backend_node_id);
|
||||
}
|
||||
if let Some(old_frontend_node_id) = self
|
||||
.backend_to_frontend
|
||||
.insert(backend_node_id, frontend_node_id)
|
||||
&& old_frontend_node_id != frontend_node_id
|
||||
{
|
||||
self.frontend_to_backend.remove(&old_frontend_node_id);
|
||||
}
|
||||
self.next_id = self
|
||||
.next_id
|
||||
.max(frontend_node_id.saturating_add(1))
|
||||
.max(FRONTEND_NODE_ID_START);
|
||||
}
|
||||
|
||||
pub(super) fn id_for_backend_node_id(&mut self, backend_node_id: u32) -> u32 {
|
||||
if let Some(frontend_node_id) = self.backend_to_frontend.get(&backend_node_id).copied() {
|
||||
return frontend_node_id;
|
||||
@@ -86,16 +65,12 @@ impl RendererFrontendNodeBindings {
|
||||
}
|
||||
|
||||
fn allocate_id(&mut self) -> u32 {
|
||||
loop {
|
||||
let frontend_node_id = self.next_id;
|
||||
self.next_id = self
|
||||
.next_id
|
||||
.checked_add(1)
|
||||
.expect("renderer frontend node id namespace exhausted");
|
||||
if frontend_node_id != 0 && !self.frontend_to_backend.contains_key(&frontend_node_id) {
|
||||
return frontend_node_id;
|
||||
}
|
||||
}
|
||||
let frontend_node_id = self.next_id;
|
||||
self.next_id = self
|
||||
.next_id
|
||||
.checked_add(1)
|
||||
.expect("renderer frontend node id namespace exhausted");
|
||||
frontend_node_id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,22 +94,26 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_registration_advances_allocator_and_clears_conflicts() {
|
||||
fn removing_a_binding_does_not_reuse_its_frontend_id() {
|
||||
let mut bindings = RendererFrontendNodeBindings::default();
|
||||
let first_backend_node_id = moli_page_types::RENDERER_BACKEND_NODE_ID_START;
|
||||
let second_backend_node_id = first_backend_node_id + 1;
|
||||
let third_backend_node_id = first_backend_node_id + 2;
|
||||
|
||||
bindings.register_explicit(7, first_backend_node_id);
|
||||
|
||||
assert_eq!(bindings.id_for_backend_node_id(second_backend_node_id), 8);
|
||||
bindings.register_explicit(7, third_backend_node_id);
|
||||
let first = bindings.id_for_backend_node_id(first_backend_node_id);
|
||||
bindings.remove_backend_node_id(first_backend_node_id);
|
||||
let second = bindings.id_for_backend_node_id(second_backend_node_id);
|
||||
let rebound = bindings.id_for_backend_node_id(first_backend_node_id);
|
||||
|
||||
assert_eq!(bindings.backend_node_id_for_frontend_node_id(first), None);
|
||||
assert!(second > first);
|
||||
assert!(rebound > second);
|
||||
assert_eq!(
|
||||
bindings.backend_node_id_for_frontend_node_id(7),
|
||||
Some(third_backend_node_id)
|
||||
bindings.backend_node_id_for_frontend_node_id(second),
|
||||
Some(second_backend_node_id)
|
||||
);
|
||||
assert_eq!(
|
||||
bindings.backend_node_id_for_frontend_node_id(rebound),
|
||||
Some(first_backend_node_id)
|
||||
);
|
||||
assert_eq!(bindings.id_for_backend_node_id(first_backend_node_id), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -4926,70 +4926,34 @@ impl PageVm {
|
||||
inspector_session_id: Option<&str>,
|
||||
matches: Vec<dom_search::DocumentSearchMatch>,
|
||||
) -> crate::RendererDomSearchRegistration {
|
||||
let include_whitespace = self.document_dom_agent_includes_whitespace(inspector_session_id);
|
||||
let document_handle = self
|
||||
.vm()
|
||||
.document_runtime
|
||||
.dom_host()
|
||||
.dom()
|
||||
.document_node_id();
|
||||
let document_is_published = self
|
||||
.renderer_backend_node_id_for_live_handle(document_handle)
|
||||
.is_some_and(|document_backend_node_id| {
|
||||
self.document_has_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_backend_node_id,
|
||||
)
|
||||
});
|
||||
let mut nodes = Vec::with_capacity(matches.len());
|
||||
let mut backend_node_ids = Vec::with_capacity(matches.len());
|
||||
for search_match in matches {
|
||||
let (backend_node_id, is_whitespace_text) = match search_match {
|
||||
let backend_node_id = match search_match {
|
||||
dom_search::DocumentSearchMatch::Live(handle) => {
|
||||
let Some(backend_node_id) =
|
||||
self.renderer_backend_node_id_for_live_handle(handle)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let is_whitespace_text = super::page_dom::inspector_whitespace_text_node(
|
||||
self.vm().document_runtime.dom_host(),
|
||||
handle,
|
||||
);
|
||||
(backend_node_id, is_whitespace_text)
|
||||
backend_node_id
|
||||
}
|
||||
dom_search::DocumentSearchMatch::Generated {
|
||||
host,
|
||||
identity,
|
||||
is_whitespace_text,
|
||||
} => {
|
||||
dom_search::DocumentSearchMatch::Generated { host, identity } => {
|
||||
let Some(backend_node_id) =
|
||||
self.renderer_backend_node_id_for_inspector_node(host, identity)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
(backend_node_id, is_whitespace_text)
|
||||
backend_node_id
|
||||
}
|
||||
};
|
||||
let frontend_node_id = if document_is_published {
|
||||
self.document_frontend_node_id_for_backend_node_id_in_whitespace_projection(
|
||||
inspector_session_id,
|
||||
backend_node_id,
|
||||
include_whitespace,
|
||||
is_whitespace_text,
|
||||
)
|
||||
} else {
|
||||
// Chromium preserves the result count before a session has
|
||||
// requested its document, but keeps every result unbound.
|
||||
// Search must not become an implicit document publication path.
|
||||
0
|
||||
};
|
||||
nodes.push(crate::RendererDomSearchResultNode {
|
||||
frontend_node_id,
|
||||
backend_node_id,
|
||||
});
|
||||
backend_node_ids.push(backend_node_id);
|
||||
}
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
self.dom_agent_state
|
||||
.register_search_results(inspector_session_id, document_id, nodes)
|
||||
self.dom_agent_state.register_search_results(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_ids,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn document_frontend_node_id_for_backend_node_id(
|
||||
@@ -5024,28 +4988,29 @@ impl PageVm {
|
||||
self.document_frontend_node_id_for_backend_node_id(inspector_session_id, backend_node_id)
|
||||
}
|
||||
|
||||
pub(super) fn document_has_frontend_node_id_for_backend_node_id(
|
||||
&mut self,
|
||||
pub(super) fn document_node_is_published(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
backend_node_id: u32,
|
||||
) -> bool {
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
self.dom_agent_state
|
||||
.has_frontend_node_id_for_backend_node_id(
|
||||
inspector_session_id,
|
||||
document_id,
|
||||
backend_node_id,
|
||||
)
|
||||
self.dom_agent_state.node_is_published(
|
||||
inspector_session_id,
|
||||
self.current_dom_agent_document_id(),
|
||||
backend_node_id,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn document_node_children_requested(
|
||||
&mut self,
|
||||
pub(super) fn existing_document_frontend_node_id(
|
||||
&self,
|
||||
inspector_session_id: Option<&str>,
|
||||
backend_node_id: u32,
|
||||
) -> bool {
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
) -> Option<u32> {
|
||||
self.dom_agent_state
|
||||
.children_requested(inspector_session_id, document_id, backend_node_id)
|
||||
.frontend_node_id_for_existing_backend_node_id(
|
||||
inspector_session_id,
|
||||
self.current_dom_agent_document_id(),
|
||||
backend_node_id,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn document_frontend_node_binding(
|
||||
@@ -5061,6 +5026,22 @@ impl PageVm {
|
||||
)
|
||||
}
|
||||
|
||||
/// Resolve an Inspector input without exposing an internal-only binding.
|
||||
pub(super) fn published_document_frontend_node_binding(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
frontend_node_id: u32,
|
||||
) -> crate::RendererDomFrontendNodeBindingResolution {
|
||||
match self.document_frontend_node_binding(inspector_session_id, frontend_node_id) {
|
||||
crate::RendererDomFrontendNodeBindingResolution::BackendNodeId(backend_node_id)
|
||||
if self.document_node_is_published(inspector_session_id, backend_node_id) =>
|
||||
{
|
||||
crate::RendererDomFrontendNodeBindingResolution::BackendNodeId(backend_node_id)
|
||||
}
|
||||
_ => crate::RendererDomFrontendNodeBindingResolution::NotFound,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn register_document_bidi_node_binding(
|
||||
&mut self,
|
||||
inspector_session_id: Option<&str>,
|
||||
@@ -5146,9 +5127,8 @@ impl PageVm {
|
||||
frontend_node_id: u32,
|
||||
) -> crate::RendererDomNodeStackTraceResolution {
|
||||
let document_id = self.current_dom_agent_document_id();
|
||||
let crate::RendererDomFrontendNodeBindingResolution::BackendNodeId(backend_node_id) = self
|
||||
.dom_agent_state
|
||||
.frontend_node_binding(inspector_session_id, document_id, frontend_node_id)
|
||||
let crate::RendererDomFrontendNodeBindingResolution::BackendNodeId(backend_node_id) =
|
||||
self.published_document_frontend_node_binding(inspector_session_id, frontend_node_id)
|
||||
else {
|
||||
return crate::RendererDomNodeStackTraceResolution::MissingNode;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
use super::*;
|
||||
use crate::{RendererDomMutationEvent, RendererDomSearchResultsResolution};
|
||||
use moli_page_types::DocumentNodeSnapshot;
|
||||
|
||||
fn inspector_descendants(snapshot: &DocumentNodeSnapshot) -> Vec<&DocumentNodeSnapshot> {
|
||||
let mut pending = vec![snapshot];
|
||||
let mut nodes = Vec::new();
|
||||
while let Some(node) = pending.pop() {
|
||||
pending.extend(&node.children);
|
||||
pending.extend(&node.shadow_roots);
|
||||
pending.extend(&node.pseudo_elements);
|
||||
if let Some(associated) = node.associated.as_deref() {
|
||||
pending.push(associated.node());
|
||||
}
|
||||
nodes.push(node);
|
||||
}
|
||||
nodes
|
||||
}
|
||||
|
||||
fn serialized_inspector_ids(snapshot: &DocumentNodeSnapshot) -> std::collections::HashSet<u32> {
|
||||
let wire = moli_protocol_cdp::node_snapshot_to_cdp(snapshot, None, None).unwrap();
|
||||
let mut pending = vec![&wire];
|
||||
let mut ids = std::collections::HashSet::new();
|
||||
while let Some(value) = pending.pop() {
|
||||
match value {
|
||||
serde_json::Value::Object(fields) => {
|
||||
if let Some(id) = fields.get("nodeId").and_then(serde_json::Value::as_u64) {
|
||||
ids.insert(u32::try_from(id).unwrap());
|
||||
}
|
||||
pending.extend(fields.values());
|
||||
}
|
||||
serde_json::Value::Array(values) => pending.extend(values),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
ids
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn child_list_resync_keeps_publication_consistent_beyond_the_wire_depth_limit() {
|
||||
run_page_vm_large_stack_async_test("deep-dom-publication", || async {
|
||||
for levels in [1000, 1001, 1002, 1100] {
|
||||
let mut page = test_page_vm();
|
||||
page.vm_mut()
|
||||
.eval(&format!(
|
||||
r#"
|
||||
document.body.innerHTML = '<main id=parent><i id=previous></i><i id=anchor></i></main>';
|
||||
let cursor = document.getElementById('parent');
|
||||
for (let i = 0; i < {levels}; ++i) {{
|
||||
const child = document.createElement('div');
|
||||
child.id = 'level-' + i;
|
||||
cursor.appendChild(child);
|
||||
cursor = child;
|
||||
if (i === 999) {{
|
||||
child.attachShadow({{mode:'closed'}}).innerHTML = '<b>boundary</b>';
|
||||
}}
|
||||
}}
|
||||
"#
|
||||
))
|
||||
.unwrap();
|
||||
let document = page
|
||||
.document_node_snapshot_for_document(Some("session"), 4, false)
|
||||
.unwrap();
|
||||
let mut visible = serialized_inspector_ids(&document.snapshot);
|
||||
// Expand in separate bounded requests, just as a client can walk past
|
||||
// the per-message wire limit without ever requesting an oversized tree.
|
||||
for index in (0..levels).step_by(200) {
|
||||
let handle = page
|
||||
.vm()
|
||||
.element_handle_by_id_for_test(&format!("level-{index}"))
|
||||
.unwrap();
|
||||
let backend = page
|
||||
.renderer_backend_node_id_for_live_handle(handle)
|
||||
.unwrap();
|
||||
let frontend = page
|
||||
.existing_document_frontend_node_id(Some("session"), backend)
|
||||
.unwrap();
|
||||
assert!(visible.contains(&frontend));
|
||||
let expanded = page
|
||||
.document_child_node_snapshot_events_for_node(
|
||||
Some("session"),
|
||||
false,
|
||||
crate::RendererDomNodeReference::FrontendNodeId {
|
||||
inspector_session_id: Some("session".to_owned()),
|
||||
frontend_node_id: frontend,
|
||||
},
|
||||
199,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
for event in expanded.events {
|
||||
assert!(visible.contains(&event.parent_frontend_node_id));
|
||||
for node in &event.snapshots {
|
||||
visible.extend(serialized_inspector_ids(node));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Put an expanded associated root at the last wire level as well:
|
||||
// reserving its shallow root must not drop its separate child list.
|
||||
let boundary_host = page
|
||||
.vm()
|
||||
.element_handle_by_id_for_test("level-999")
|
||||
.unwrap();
|
||||
let boundary_root = page
|
||||
.vm()
|
||||
.document_runtime
|
||||
.dom_host()
|
||||
.shadow_root_handle(boundary_host)
|
||||
.unwrap();
|
||||
let boundary_backend = page
|
||||
.renderer_backend_node_id_for_live_handle(boundary_root)
|
||||
.unwrap();
|
||||
let boundary_frontend = page
|
||||
.existing_document_frontend_node_id(Some("session"), boundary_backend)
|
||||
.unwrap();
|
||||
assert!(visible.contains(&boundary_frontend));
|
||||
let expanded = page
|
||||
.document_child_node_snapshot_events_for_live_handles(
|
||||
Some("session"),
|
||||
&[boundary_root],
|
||||
0,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
let mut associated_before = vec![(boundary_backend, boundary_frontend)];
|
||||
for event in expanded.events {
|
||||
assert!(visible.contains(&event.parent_frontend_node_id));
|
||||
for node in &event.snapshots {
|
||||
visible.extend(serialized_inspector_ids(node));
|
||||
associated_before.extend(inspector_descendants(node).into_iter().map(|node| {
|
||||
(
|
||||
node.backend_node_id.unwrap(),
|
||||
node.frontend_node_id.unwrap(),
|
||||
)
|
||||
}));
|
||||
}
|
||||
}
|
||||
let mut before = (0..levels)
|
||||
.map(|index| {
|
||||
let handle = page
|
||||
.vm()
|
||||
.element_handle_by_id_for_test(&format!("level-{index}"))
|
||||
.unwrap();
|
||||
let backend = page
|
||||
.renderer_backend_node_id_for_live_handle(handle)
|
||||
.unwrap();
|
||||
let frontend = page
|
||||
.existing_document_frontend_node_id(Some("session"), backend)
|
||||
.unwrap();
|
||||
assert!(visible.contains(&frontend));
|
||||
(backend, frontend)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
before.extend(associated_before);
|
||||
let parent = page.vm().element_handle_by_id_for_test("parent").unwrap();
|
||||
let parent_backend = page
|
||||
.renderer_backend_node_id_for_live_handle(parent)
|
||||
.unwrap();
|
||||
let parent_frontend = page
|
||||
.existing_document_frontend_node_id(Some("session"), parent_backend)
|
||||
.unwrap();
|
||||
let previous = page.vm().element_handle_by_id_for_test("previous").unwrap();
|
||||
let previous_backend = page
|
||||
.renderer_backend_node_id_for_live_handle(previous)
|
||||
.unwrap();
|
||||
page.take_pending_dom_mutation_event_batches();
|
||||
page.dom_agent_state
|
||||
.remove_frontend_bindings_for_backend_node_ids(
|
||||
Some("session"),
|
||||
page.current_dom_agent_document_id(),
|
||||
[previous_backend],
|
||||
);
|
||||
page.vm_mut()
|
||||
.eval("document.getElementById('previous').after(document.createElement('span'))")
|
||||
.unwrap();
|
||||
let batches = page.take_pending_dom_mutation_event_batches();
|
||||
let replacements = batches
|
||||
.iter()
|
||||
.flat_map(|batch| &batch.events)
|
||||
.filter_map(|event| match event {
|
||||
RendererDomMutationEvent::SetChildNodes {
|
||||
parent_node_id,
|
||||
nodes,
|
||||
} => Some((*parent_node_id, nodes)),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
replacements.first().map(|(id, _)| *id),
|
||||
Some(parent_frontend)
|
||||
);
|
||||
// The ancestor replacement retires its old subtree in the client.
|
||||
visible = std::collections::HashSet::from([parent_frontend]);
|
||||
for (parent, nodes) in replacements {
|
||||
assert!(
|
||||
visible.contains(&parent),
|
||||
"notification references an unknown parent"
|
||||
);
|
||||
for node in nodes {
|
||||
visible.extend(serialized_inspector_ids(node));
|
||||
}
|
||||
}
|
||||
for (backend, frontend) in before {
|
||||
assert!(page.document_node_is_published(Some("session"), backend));
|
||||
assert!(
|
||||
visible.contains(&frontend),
|
||||
"{levels}-level resync retained unpublished wire ID {frontend}"
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn child_list_resync_preserves_published_inspector_subtrees() {
|
||||
run_page_vm_async_test(async move {
|
||||
for include_whitespace in [false, true] {
|
||||
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?;
|
||||
let (mut page_vm, _resource_source, _owner_wake_rx) =
|
||||
page_vm_with_bound_task_sources_and_owner_wake(
|
||||
&loader,
|
||||
Url::parse("https://example.com/inspector-resync")?,
|
||||
);
|
||||
page_vm.vm_mut().eval(r#"
|
||||
document.body.innerHTML = `<main id="parent">
|
||||
<i id="previous"></i><i id="anchor"></i>
|
||||
<iframe id="frame"></iframe>
|
||||
<input id="control" type="search" value="needle">
|
||||
<li id="marker">marker</li>
|
||||
<div id="host"><em id="retained">light</em></div>
|
||||
<template id="template"><article><span>inside</span>
|
||||
<template><b>nested</b></template></article></template>
|
||||
</main>`;
|
||||
document.getElementById('host').attachShadow({mode:'closed'})
|
||||
.innerHTML = '<section><a id="shadow-hit">shadow</a></section>';
|
||||
"#)?;
|
||||
materialize_child_realm_through_page_turn_for_test(&mut page_vm, "frame")?;
|
||||
page_vm.vm_mut().eval(
|
||||
"document.getElementById('frame').contentDocument.body.innerHTML = '<main><a id=frame-hit>frame</a></main>'",
|
||||
)?;
|
||||
page_vm.configure_document_dom_agent_session(Some("session"), include_whitespace);
|
||||
let document = page_vm
|
||||
.document_node_snapshot_for_document(Some("session"), -1, true)
|
||||
.expect("complete Inspector document");
|
||||
let parent = page_vm.vm().element_handle_by_id_for_test("parent").unwrap();
|
||||
let previous = page_vm.vm().element_handle_by_id_for_test("previous").unwrap();
|
||||
let initial = inspector_descendants(&document.snapshot);
|
||||
let parent_snapshot = initial.iter().find(|node| node.node_id == parent).unwrap();
|
||||
let mut before = inspector_descendants(parent_snapshot).into_iter().cloned().collect::<Vec<_>>();
|
||||
assert!(before.iter().any(|node| node.associated.is_some()), "iframe document must be published");
|
||||
assert!(before.iter().any(|node| node.shadow_root_type.as_deref() == Some("user-agent")), "generated UA root must be published");
|
||||
assert!(before.iter().any(|node| !node.pseudo_elements.is_empty()), "generated marker must be published");
|
||||
let mut fragments = before.iter().filter_map(|node| node.template_content().map(|content| content.node_id)).collect::<Vec<_>>();
|
||||
while let Some(fragment) = fragments.pop() {
|
||||
let expanded = page_vm.document_child_node_snapshot_events_for_live_handles(
|
||||
Some("session"), &[fragment], -1, true,
|
||||
).expect("expand template content");
|
||||
for node in expanded.events.iter().flat_map(|event| &event.snapshots).flat_map(inspector_descendants) {
|
||||
if let Some(content) = node.template_content() {
|
||||
fragments.push(content.node_id);
|
||||
}
|
||||
before.push(node.clone());
|
||||
}
|
||||
}
|
||||
let parent_id = parent_snapshot.frontend_node_id.unwrap();
|
||||
let document_id = page_vm.current_dom_agent_document_id();
|
||||
let previous_backend = page_vm.renderer_backend_node_id_for_live_handle(previous).unwrap();
|
||||
page_vm.take_pending_dom_mutation_event_batches();
|
||||
// Exercise the fallback used when an incremental insertion cannot
|
||||
// name its previous sibling, rather than calling the helper directly.
|
||||
page_vm.dom_agent_state.remove_frontend_bindings_for_backend_node_ids(
|
||||
Some("session"), document_id, [previous_backend],
|
||||
);
|
||||
page_vm.vm_mut().eval(
|
||||
"document.getElementById('previous').after(document.createElement('span'))",
|
||||
)?;
|
||||
let batches = page_vm.take_pending_dom_mutation_event_batches();
|
||||
let replacement_events = batches.iter()
|
||||
.filter(|batch| batch.session.wire_session_id() == Some("session"))
|
||||
.flat_map(|batch| &batch.events)
|
||||
.filter_map(|event| match event {
|
||||
RendererDomMutationEvent::SetChildNodes { parent_node_id, nodes } => Some((*parent_node_id, nodes)),
|
||||
_ => None,
|
||||
}).collect::<Vec<_>>();
|
||||
assert_eq!(replacement_events.first().map(|(id, _)| *id), Some(parent_id),
|
||||
"missing previous sibling must resynchronize the parent first");
|
||||
assert!(replacement_events.len() >= 3, "expanded template fragments need separate notifications");
|
||||
let mut visible = std::collections::HashSet::from([parent_id]);
|
||||
for (parent, nodes) in replacement_events {
|
||||
assert!(visible.contains(&parent), "fragment notification preceded its owner");
|
||||
for node in nodes {
|
||||
visible.extend(serialized_inspector_ids(node));
|
||||
}
|
||||
}
|
||||
assert!(!visible.contains(&0));
|
||||
// Replacing the parent's children retires all old descendants in
|
||||
// the client mirror. Any ID the renderer still publishes must be
|
||||
// present in the replacement, including associated trees.
|
||||
for old in before.into_iter().filter(|node| node.node_id != parent) {
|
||||
let backend = old.backend_node_id.unwrap();
|
||||
let frontend = old.frontend_node_id.unwrap();
|
||||
if page_vm.document_node_is_published(Some("session"), backend)
|
||||
&& page_vm.existing_document_frontend_node_id(Some("session"), backend) == Some(frontend)
|
||||
{
|
||||
assert!(visible.contains(&frontend),
|
||||
"resync lost published {} node {frontend} (whitespace={include_whitespace})", old.node_name);
|
||||
}
|
||||
}
|
||||
for (query, ua) in [("#frame-hit", false), ("#shadow-hit", false), ("#editing-view-port", true)] {
|
||||
let search = page_vm.document_perform_search(Some("session"), query, ua);
|
||||
assert_eq!(search.result_count, 1, "{query}");
|
||||
let (resolution, _) = page_vm.document_search_results_with_child_node_snapshot_events(
|
||||
Some("session"), &search.search_id, 0, 1,
|
||||
);
|
||||
let RendererDomSearchResultsResolution::Found(results) = resolution else {
|
||||
panic!("missing retained search result for {query}");
|
||||
};
|
||||
assert!(visible.contains(&results[0].frontend_node_id), "search returned retired ID for {query}");
|
||||
}
|
||||
}
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await.expect("Inspector resync fixture should run");
|
||||
}
|
||||
@@ -121,6 +121,7 @@ mod computed_size;
|
||||
mod cssom_zoom;
|
||||
mod dedicated_worker_client_event;
|
||||
mod document_script_completion;
|
||||
mod dom_publication;
|
||||
mod element_toggle_event;
|
||||
mod fetch_xhr;
|
||||
mod file_entry_file_callback;
|
||||
|
||||
@@ -28,6 +28,14 @@ async fn typed_attributes_resolve_scoped_references_against_live_nodes() {
|
||||
"<!doctype html><html><body><input id='probe' data-state='before'></body></html>",
|
||||
)
|
||||
.await;
|
||||
page.run_async_command(RendererPageCommand::DocumentNodeSnapshotForDocument {
|
||||
inspector_session_id: Some("attributes-a".to_owned()),
|
||||
include_whitespace: false,
|
||||
depth: 1,
|
||||
pierce: false,
|
||||
})
|
||||
.await
|
||||
.expect("publish a shallow frontend tree");
|
||||
let (reply, _) = page
|
||||
.run_async_command(RendererPageCommand::DocumentQuerySelectorForDocument {
|
||||
inspector_session_id: Some("attributes-a".to_owned()),
|
||||
@@ -48,6 +56,38 @@ async fn typed_attributes_resolve_scoped_references_against_live_nodes() {
|
||||
inspector_session_id: Some("attributes-a".to_owned()),
|
||||
frontend_node_id: node.frontend_node_id,
|
||||
};
|
||||
let backend = RendererDomNodeReference::BackendNodeId(node.backend_node_id);
|
||||
assert!(matches!(
|
||||
attributes(
|
||||
&page,
|
||||
RendererDomNodeReference::InternalFrontendNodeBinding {
|
||||
inspector_session_id: Some("attributes-a".to_owned()),
|
||||
frontend_node_id: node.frontend_node_id,
|
||||
},
|
||||
)
|
||||
.await,
|
||||
Attributes::Found(_)
|
||||
));
|
||||
assert_eq!(
|
||||
attributes(&page, frontend.clone()).await,
|
||||
Attributes::MissingNode,
|
||||
"an internal query binding is not a valid frontend input"
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
attributes(&page, backend.clone()).await,
|
||||
Attributes::Found(_)
|
||||
),
|
||||
"backend references remain independent of frontend publication"
|
||||
);
|
||||
page.run_async_command(
|
||||
RendererPageCommand::DocumentFrontendNodeIdsForBackendNodeIds {
|
||||
inspector_session_id: Some("attributes-a".to_owned()),
|
||||
backend_node_ids: vec![node.backend_node_id],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("publish the frontend tree");
|
||||
assert_eq!(
|
||||
attributes(&page, frontend.clone()).await,
|
||||
Attributes::Found(vec![
|
||||
@@ -74,7 +114,6 @@ async fn typed_attributes_resolve_scoped_references_against_live_nodes() {
|
||||
})
|
||||
.await
|
||||
.expect("mutate the live node");
|
||||
let backend = RendererDomNodeReference::BackendNodeId(node.backend_node_id);
|
||||
assert_eq!(
|
||||
attributes(&page, frontend.clone()).await,
|
||||
attributes(&page, backend.clone()).await,
|
||||
|
||||
Reference in New Issue
Block a user