From 39937059562f436d0cd58779694bff8d9f0e29b9 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Mon, 28 Oct 2024 12:34:44 +0100 Subject: [PATCH] Fix incorrect handling of double quotes in `stringify` function. --- src/components/utils.js | 2 +- tests/inspector/components/utils.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/utils.js b/src/components/utils.js index 6725eb2..1f0b30a 100644 --- a/src/components/utils.js +++ b/src/components/utils.js @@ -68,7 +68,7 @@ export function truncateString( string, length ) { // Retain the JSON.stringify() formatting instead of fixing 100 tests across the project :) function stringifySingleToDoubleQuotesReplacer( value, indent, stringify ) { if ( typeof value === 'string' ) { - return `"${ value.replace( '\'', '"' ) }"`; + return `"${ value.replaceAll( '\'', '\\"' ) }"`; } return stringify( value ); diff --git a/tests/inspector/components/utils.js b/tests/inspector/components/utils.js index a70165d..26e4b80 100644 --- a/tests/inspector/components/utils.js +++ b/tests/inspector/components/utils.js @@ -21,6 +21,10 @@ describe( 'Utils', () => { expect( stringify( () => 'foo' ) ).to.equal( 'function() {…}' ); } ); + it( 'serializes quotes properly', () => { + expect( stringify( '\'foo\'' ) ).to.equal( '"\\"foo\\""' ); + } ); + it( 'stringifies values (no quotes around text)', () => { expect( stringify( undefined ) ).to.equal( 'undefined' ); expect( stringify( true ) ).to.equal( 'true' );