Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/injected/src/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export function yamlEscapeKeyIfNeeded(str: string): string {
}

export function yamlEscapeValueIfNeeded(str: string): string {
if (!yamlStringNeedsQuotes(str))
return str;
return '"' + str.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => {
// Sanitize lone surrogates (valid in JS, invalid in JSON per RFC 8259 §8.2).
// Same approach as cli/driver.ts for non-JS language bindings.
const sanitized = (str as any).toWellFormed ? (str as any).toWellFormed() : str;
if (!yamlStringNeedsQuotes(sanitized))
return sanitized;
return '"' + sanitized.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => {
switch (c) {
case '\\':
return '\\\\';
Expand Down
9 changes: 6 additions & 3 deletions packages/playwright-core/src/utils/isomorphic/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export function yamlEscapeKeyIfNeeded(str: string): string {
}

export function yamlEscapeValueIfNeeded(str: string): string {
if (!yamlStringNeedsQuotes(str))
return str;
return '"' + str.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => {
// Sanitize lone surrogates (valid in JS, invalid in JSON per RFC 8259 §8.2).
// Same approach as cli/driver.ts for non-JS language bindings.
const sanitized = (str as any).toWellFormed ? (str as any).toWellFormed() : str;
if (!yamlStringNeedsQuotes(sanitized))
return sanitized;
return '"' + sanitized.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => {
switch (c) {
case '\\':
return '\\\\';
Expand Down
67 changes: 67 additions & 0 deletions tests/mcp/snapshot-unicode.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test, expect } from './fixtures';

test('should handle lone high surrogate in snapshot', async ({ client, server }) => {
server.setContent('/', `
<div id="container"></div>
<script>
const container = document.getElementById('container');
// Insert lone high surrogate (U+D800)
container.textContent = 'before' + String.fromCharCode(0xD800) + 'after';
</script>
`, 'text/html');

const response = await client.callTool({
name: 'browser_navigate',
arguments: {
url: server.PREFIX,
},
});

expect(response).toHaveResponse({
snapshot: expect.any(String),
});

// Lone surrogates should be replaced with U+FFFD (replacement character)
expect(response.content[0].text).toContain('\uFFFD');
});

test('should handle lone low surrogate in snapshot', async ({ client, server }) => {
server.setContent('/', `
<div id="container"></div>
<script>
const container = document.getElementById('container');
// Insert lone low surrogate (U+DC00)
container.textContent = 'before' + String.fromCharCode(0xDC00) + 'after';
</script>
`, 'text/html');

const response = await client.callTool({
name: 'browser_navigate',
arguments: {
url: server.PREFIX,
},
});

expect(response).toHaveResponse({
snapshot: expect.any(String),
});

// Lone surrogates should be replaced with U+FFFD
expect(response.content[0].text).toContain('\uFFFD');
});