mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 05:02:40 +00:00
* fix(browser): type CDP command replies and parse frame trees once CdpConnection.send is typed by method name. A command registered in src/browser/protocol/CdpCommands.ts has its parameters typed from the protocol table Playwright ships and its reply parsed once by its own result schema; a mismatch rejects that command with CdpMalformedReply (protocol_error, command, JSON pointer) without poisoning the connection. Playwright storage sessions admit replies through the same table. Page.getFrameTree and Page.getResourceTree are the first registered commands. Their one parser keeps per-frame facts tolerant (malformed child entries remain explicit omissions) and replaces the per-site readers: observeMainFrameUrl, mainFrameUrl, walkFrameTrees, frameTreeResources, runtimeFrameTreeSchema and the scenario storage frame schema. A reply without a frame tree used to be retried for two seconds and reported as target_not_allowed; it is now one protocol_error. A guard test counts literal CDP command names without a result schema per source file; the baseline can only shrink. * fix(browser): consume parsed CDP facts across boundaries * refactor(browser): register target, storage and acknowledgement CDP replies Register result schemas for Target.attachToTarget, Target.getTargetInfo, Storage.getStorageKey, Page.addScriptToEvaluateOnNewDocument, Debugger.setBreakpointByUrl, Debugger.evaluateOnCallFrame and fourteen commands whose reply REA reads only as success. Their callers use the parsed reply; the per-site readers are removed (attachedSessionId and the scenario storage and service-worker zod schemas, which threw raw ZodError). Playwright's Target.getTargetInfo in scenario attach now goes through the same admission. The untyped-command baseline drops from 96 to 48. * fix(browser): report malformed script source and coverage replies as protocol errors * fix(browser): type DOM listener, metrics and emulation CDP replies * fix(browser): report malformed script source and coverage replies as protocol errors (#10638)
273 lines
8.9 KiB
TypeScript
273 lines
8.9 KiB
TypeScript
import type { CdpCommandResult } from "./protocol/CdpCommands.js";
|
|
import type {
|
|
SnapshotDocumentNodes,
|
|
SnapshotAttributes,
|
|
} from "./CdpSnapshotNodes.js";
|
|
|
|
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
const XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
|
|
export const XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
|
|
|
|
type DomSnapshotNodes = Pick<
|
|
SnapshotDocumentNodes,
|
|
"nodeTypes" | "nodeNames" | "parents" | "attributes"
|
|
>;
|
|
|
|
interface XmlElementNamespace {
|
|
readonly svgElement: boolean;
|
|
readonly elementNamespace: string | undefined;
|
|
readonly localName: string | undefined;
|
|
readonly xlinkHref: string | undefined;
|
|
}
|
|
|
|
interface DocumentNamespaceAncestry {
|
|
readonly xml: boolean | undefined;
|
|
readonly xmlElements: Map<number, XmlElementNamespace>;
|
|
}
|
|
|
|
export interface DomSvgNodeQuery {
|
|
readonly backendNodeId: number;
|
|
readonly nodeName: string;
|
|
}
|
|
|
|
/** Bind the browser's SVG flag to the exact snapshotted element. */
|
|
export const capturedSvgNodeKind = (
|
|
query: DomSvgNodeQuery,
|
|
{ node }: CdpCommandResult<"DOM.describeNode">,
|
|
): boolean | undefined =>
|
|
node.backendNodeId !== query.backendNodeId ||
|
|
node.nodeName !== query.nodeName ||
|
|
node.nodeType !== 1
|
|
? undefined
|
|
: // CDP omits isSVG for non-SVG nodes.
|
|
(node.isSVG ?? false);
|
|
|
|
interface DomXmlElement {
|
|
readonly svgElement: boolean;
|
|
readonly xmlDocument: boolean | undefined;
|
|
readonly elementNamespace: string | undefined;
|
|
readonly localName: string | undefined;
|
|
readonly xlinkHref: string | undefined;
|
|
}
|
|
|
|
// DOMSnapshot has no namespace URI. HTML documents use uppercase `HTML` and
|
|
// the parser's local names. XML documents preserve prefixed names and xmlns
|
|
// declarations, including those on standalone SVG roots.
|
|
const xmlDocument = (nodes: DomSnapshotNodes): boolean | undefined => {
|
|
const documentIndex = nodes.nodeTypes.findIndex((nodeType) => nodeType === 9);
|
|
if (documentIndex < 0) return false;
|
|
if (
|
|
svgSourceNode(nodes, documentIndex) === undefined ||
|
|
nodes.parents[documentIndex] !== -1
|
|
)
|
|
return undefined;
|
|
for (let index = 0; index < nodes.nodeTypes.length; index += 1) {
|
|
if (nodes.nodeTypes[index] !== 1) continue;
|
|
if (nodes.parents[index] !== documentIndex) continue;
|
|
const source = svgSourceNode(nodes, index);
|
|
if (source === undefined) return undefined;
|
|
const name = source.nodeName;
|
|
if (name === "HTML") return false;
|
|
if (name === "html" || name.includes(":")) return true;
|
|
// Explicit namespaces also identify standalone SVG and other XML roots.
|
|
// Namespace-free synthetic snapshots retain the legacy HTML behavior.
|
|
const attributes = source.attributes;
|
|
for (const [attributeName] of attributes)
|
|
if (xmlnsPrefix(attributeName) !== undefined) return true;
|
|
return false;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
export const createDocumentNamespaceAncestry = (
|
|
nodes: DomSnapshotNodes,
|
|
): DocumentNamespaceAncestry => {
|
|
const xml = xmlDocument(nodes);
|
|
return {
|
|
xml,
|
|
xmlElements: xml === true ? resolveXmlElements(nodes) : new Map(),
|
|
};
|
|
};
|
|
|
|
export const domXmlElement = (
|
|
ancestry: DocumentNamespaceAncestry,
|
|
index: number,
|
|
): DomXmlElement => {
|
|
if (ancestry.xml === false) {
|
|
return {
|
|
svgElement: false,
|
|
xmlDocument: false,
|
|
elementNamespace: undefined,
|
|
localName: undefined,
|
|
xlinkHref: undefined,
|
|
};
|
|
}
|
|
if (ancestry.xml === undefined) {
|
|
return {
|
|
svgElement: false,
|
|
xmlDocument: undefined,
|
|
elementNamespace: undefined,
|
|
localName: undefined,
|
|
xlinkHref: undefined,
|
|
};
|
|
}
|
|
const resolved = ancestry.xmlElements.get(index);
|
|
if (resolved === undefined) {
|
|
return {
|
|
svgElement: false,
|
|
xmlDocument: undefined,
|
|
elementNamespace: undefined,
|
|
localName: undefined,
|
|
xlinkHref: undefined,
|
|
};
|
|
}
|
|
return {
|
|
svgElement: resolved.svgElement,
|
|
xmlDocument: true,
|
|
elementNamespace: resolved.elementNamespace,
|
|
localName: resolved.localName,
|
|
xlinkHref: resolved.xlinkHref,
|
|
};
|
|
};
|
|
|
|
interface NamespaceRestore {
|
|
readonly prefix: string;
|
|
readonly previous: string | undefined;
|
|
}
|
|
|
|
type XmlWalkStep =
|
|
| { readonly node: number }
|
|
| { readonly restore: readonly NamespaceRestore[] };
|
|
|
|
// Walk parent-before-child with one mutable namespace environment. Restore
|
|
// declarations on exit so siblings retain their own scope. Only declarations
|
|
// on the active path are retained, rather than cumulative maps at every node.
|
|
const resolveXmlElements = (
|
|
nodes: DomSnapshotNodes,
|
|
): Map<number, XmlElementNamespace> => {
|
|
const children = new Map<number, number[]>();
|
|
const pending: XmlWalkStep[] = [];
|
|
for (let node = 0; node < nodes.nodeNames.length; node += 1) {
|
|
const parent = nodes.parents[node];
|
|
if (parent === undefined) continue;
|
|
if (parent < 0) pending.push({ node });
|
|
else {
|
|
const siblings = children.get(parent);
|
|
if (siblings === undefined) children.set(parent, [node]);
|
|
else siblings.push(node);
|
|
}
|
|
}
|
|
const bindings = new Map<string, string>();
|
|
const resolved = new Map<number, XmlElementNamespace>();
|
|
while (pending.length > 0) {
|
|
const step = pending.pop();
|
|
if (step === undefined) break;
|
|
if ("restore" in step) {
|
|
for (const { prefix, previous } of step.restore) {
|
|
if (previous === undefined) bindings.delete(prefix);
|
|
else bindings.set(prefix, previous);
|
|
}
|
|
continue;
|
|
}
|
|
const { node } = step;
|
|
const source = svgSourceNode(nodes, node);
|
|
if (source === undefined) continue;
|
|
const attributes = source.attributes;
|
|
const restore = applyDeclarations(bindings, attributes);
|
|
const expandedName = elementExpandedName(source.nodeName, bindings);
|
|
const svgElement =
|
|
source.nodeType === 1 && expandedName?.namespace === SVG_NAMESPACE;
|
|
resolved.set(node, {
|
|
svgElement,
|
|
elementNamespace: expandedName?.namespace,
|
|
localName: expandedName?.localName,
|
|
xlinkHref: svgElement ? xlinkHrefValue(attributes, bindings) : undefined,
|
|
});
|
|
pending.push({ restore });
|
|
for (const child of children.get(node) ?? []) pending.push({ node: child });
|
|
}
|
|
// Cycles (including self-parents) and dangling chains cannot reach a root,
|
|
// so remain unresolved instead of deriving URLs from an invalid ancestry.
|
|
return resolved;
|
|
};
|
|
|
|
const applyDeclarations = (
|
|
bindings: Map<string, string>,
|
|
attributes: SnapshotAttributes,
|
|
): readonly NamespaceRestore[] => {
|
|
const restore: NamespaceRestore[] = [];
|
|
const seen = new Set<string>();
|
|
for (const [name, value] of attributes) {
|
|
const prefix = xmlnsPrefix(name);
|
|
if (prefix === undefined || seen.has(prefix)) continue;
|
|
seen.add(prefix);
|
|
restore.push({ prefix, previous: bindings.get(prefix) });
|
|
if (value.length === 0) bindings.delete(prefix);
|
|
else bindings.set(prefix, value);
|
|
}
|
|
return restore;
|
|
};
|
|
|
|
const xmlnsPrefix = (name: string | undefined): string | undefined => {
|
|
if (name === undefined) return undefined;
|
|
if (name === "xmlns") return "";
|
|
if (!name.startsWith("xmlns:")) return undefined;
|
|
const prefix = name.slice("xmlns:".length);
|
|
if (prefix.length === 0 || prefix === "xmlns" || prefix.includes(":"))
|
|
return undefined;
|
|
return prefix;
|
|
};
|
|
|
|
const elementExpandedName = (
|
|
nodeName: string | undefined,
|
|
bindings: ReadonlyMap<string, string>,
|
|
): { readonly namespace: string; readonly localName: string } | undefined => {
|
|
if (nodeName === undefined) return undefined;
|
|
const colon = nodeName.indexOf(":");
|
|
if (colon < 0)
|
|
return { namespace: bindings.get("") ?? "", localName: nodeName };
|
|
const prefix = nodeName.slice(0, colon);
|
|
const local = nodeName.slice(colon + 1);
|
|
if (
|
|
prefix.length === 0 ||
|
|
local.length === 0 ||
|
|
local.includes(":") ||
|
|
prefix === "xmlns"
|
|
)
|
|
return undefined;
|
|
const namespace = bindings.get(prefix);
|
|
return namespace === undefined ? undefined : { namespace, localName: local };
|
|
};
|
|
|
|
// First prefixed href bound to the XLink namespace. Duplicate names keep the
|
|
// earlier value. Plain href is applied by the caller and wins even when empty.
|
|
const xlinkHrefValue = (
|
|
attributes: SnapshotAttributes,
|
|
bindings: ReadonlyMap<string, string>,
|
|
): string | undefined => {
|
|
const seen = new Set<string>();
|
|
for (const [name, value] of attributes) {
|
|
if (seen.has(name)) continue;
|
|
seen.add(name);
|
|
const colon = name.indexOf(":");
|
|
if (colon <= 0) continue;
|
|
const prefix = name.slice(0, colon);
|
|
const local = name.slice(colon + 1);
|
|
if (local !== "href" || bindings.get(prefix) !== XLINK_NAMESPACE) continue;
|
|
return value;
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
// Namespace interpretation needs these parsed facts even when a node value is unknown.
|
|
const svgSourceNode = (nodes: DomSnapshotNodes, index: number) => {
|
|
const nodeType = nodes.nodeTypes[index];
|
|
const nodeName = nodes.nodeNames[index];
|
|
const attributes = nodes.attributes[index];
|
|
return nodeType === undefined ||
|
|
nodeName === undefined ||
|
|
attributes === undefined
|
|
? undefined
|
|
: { nodeType, nodeName, attributes };
|
|
};
|