Skip to content

Commit

Permalink
Block Editor: Avoid double-wrapping selectors when transforming the s…
Browse files Browse the repository at this point in the history
…tyles (#54981)

* Block Editor: Avoid double-wrapping selectors when transforming the styles
* Include space in the check
  • Loading branch information
Mamaduka authored and mikachan committed Oct 4, 2023
1 parent 020570d commit 5c01d3d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ color: red;
}"
`;

exports[`CSS selector wrap should not double wrap selectors 1`] = `
".my-namespace h1,
.my-namespace .red {
color: red;
}"
`;

exports[`CSS selector wrap should replace :root selectors 1`] = `
".my-namespace {
--my-color: #ff0000;
}"
`;

exports[`CSS selector wrap should replace root tags 1`] = `
".my-namespace,
.my-namespace h1 {
Expand Down Expand Up @@ -49,9 +62,3 @@ color: red;
}
}"
`;

exports[`CSS selector wrap should replace :root selectors 1`] = `
".my-namespace {
--my-color: #ff0000;
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ describe( 'CSS selector wrap', () => {

expect( output ).toMatchSnapshot();
} );

it( 'should not double wrap selectors', () => {
const callback = wrap( '.my-namespace' );
const input = ` .my-namespace h1, .red { color: red; }`;

const output = traverse( input, callback );

expect( output ).toMatchSnapshot();
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const wrap =
return selector;
}

// Skip the update when a selector already has a namespace + space (" ").
if ( selector.trim().startsWith( `${ namespace } ` ) ) {
return selector;
}

// Anything other than a root tag is always prefixed.
{
if ( ! selector.match( IS_ROOT_TAG ) ) {
Expand Down

0 comments on commit 5c01d3d

Please sign in to comment.