Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(driver): let the client declare lone surrogate support (#43548)

Pavel Feldman89cd2a6463ea
diff --git a/packages/playwright-core/src/cli/driver.ts b/packages/playwright-core/src/cli/driver.ts
index 619ca44c84..c95619af74 100644
--- a/packages/playwright-core/src/cli/driver.ts
+++ b/packages/playwright-core/src/cli/driver.ts
@@ -29,7 +29,12 @@ import type { LaunchServerOptions } from '../client/types';
export function runDriver() {
const dispatcherConnection = new DispatcherConnection();
- new RootDispatcher(dispatcherConnection, async (rootScope, { sdkLanguage }) => {
+ // Certain Language Binding JSON parsers (e.g. .NET) do not like strings with lone surrogates.
+ // The client tells us whether it supports them when it initializes.
+ let replacer: ((key: string, value: any) => any) | undefined;
+ new RootDispatcher(dispatcherConnection, async (rootScope, { sdkLanguage, supportsLoneSurrogates }) => {
+ if (!supportsLoneSurrogates)
+ replacer = (key, value) => typeof value === 'string' ? value.toWellFormed() : value;
const playwright = createPlaywright({ sdkLanguage, isClientCollocatedWithServer: true });
return new PlaywrightDispatcher(rootScope, playwright);
});
@@ -40,13 +45,6 @@ export function runDriver() {
// eslint-disable-next-line no-restricted-properties
process.stdout.on('error', () => {});
transport.onmessage = (message: string) => dispatcherConnection.dispatch(JSON.parse(message));
- // Certain Language Binding JSON parsers (e.g. .NET) do not like strings with lone surrogates.
- const isJavaScriptLanguageBinding = !process.env.PW_LANG_NAME || process.env.PW_LANG_NAME === 'javascript';
- const replacer = !isJavaScriptLanguageBinding && (String.prototype as any).toWellFormed ? (key: string, value: any): any => {
- if (typeof value === 'string')
- return value.toWellFormed();
- return value;
- } : undefined;
dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message, replacer));
transport.onclose = () => {
// Drop any messages during shutdown on the floor.
diff --git a/packages/playwright-core/src/client/channels.d.ts b/packages/playwright-core/src/client/channels.d.ts
index dd93e916e0..ceac209bef 100644
--- a/packages/playwright-core/src/client/channels.d.ts
+++ b/packages/playwright-core/src/client/channels.d.ts
@@ -4745,9 +4745,10 @@ export interface RootChannel extends RootEventTarget, Channel {
}
export type RootInitializeParams = {
sdkLanguage: SDKLanguage,
+ supportsLoneSurrogates?: boolean,
};
export type RootInitializeOptions = {
-
+ supportsLoneSurrogates?: boolean,
};
export type RootInitializeResult = {
playwright: PlaywrightChannel,
diff --git a/packages/playwright-core/src/client/connection.ts b/packages/playwright-core/src/client/connection.ts
index 425c87d6f6..edd7a36037 100644
--- a/packages/playwright-core/src/client/connection.ts
+++ b/packages/playwright-core/src/client/connection.ts
@@ -62,6 +62,7 @@ class Root extends ChannelOwner<channels.RootChannel> {
async initialize(): Promise<Playwright> {
return Playwright.from((await this._channel.initialize({
sdkLanguage: 'javascript',
+ supportsLoneSurrogates: true,
}, kNoTimeout)).playwright);
}
}
diff --git a/packages/playwright-core/src/server/channels.d.ts b/packages/playwright-core/src/server/channels.d.ts
index 8e61916cde..dc066c5af3 100644
--- a/packages/playwright-core/src/server/channels.d.ts
+++ b/packages/playwright-core/src/server/channels.d.ts
@@ -4746,9 +4746,10 @@ export interface RootChannel extends RootEventTarget, Channel {
}
export type RootInitializeParams = {
sdkLanguage: SDKLanguage,
+ supportsLoneSurrogates?: boolean,
};
export type RootInitializeOptions = {
-
+ supportsLoneSurrogates?: boolean,
};
export type RootInitializeResult = {
playwright: PlaywrightChannel,
diff --git a/packages/protocol/spec/playwright.yml b/packages/protocol/spec/playwright.yml
index b4c9651b2e..71141c47b9 100644
--- a/packages/protocol/spec/playwright.yml
+++ b/packages/protocol/spec/playwright.yml
@@ -21,6 +21,9 @@ Root:
internal: true
parameters:
sdkLanguage: SDKLanguage
+ # Whether the client JSON parser accepts strings with lone surrogates.
+ # When it does not, the driver replaces them with U+FFFD.
+ supportsLoneSurrogates: boolean?
returns:
playwright: Playwright
diff --git a/packages/protocol/src/validator.ts b/packages/protocol/src/validator.ts
index 4b7dbdbbf3..3af381cf78 100644
--- a/packages/protocol/src/validator.ts
+++ b/packages/protocol/src/validator.ts
@@ -2744,6 +2744,7 @@ scheme.PageWebStorageClearResult = tOptional(tObject({}));
scheme.RootInitializer = tOptional(tObject({}));
scheme.RootInitializeParams = tObject({
sdkLanguage: tType('SDKLanguage'),
+ supportsLoneSurrogates: tOptional(tBoolean),
});
scheme.RootInitializeResult = tObject({
playwright: tChannel(['Playwright']),