Skip to content

Commit

Permalink
scope the selectors in the post editor
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian authored and oandregal committed Mar 29, 2023
1 parent e5ed227 commit e60c4b7
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,6 @@ const withDuotoneControls = createHigherOrderComponent(
'withDuotoneControls'
);

/**
* Function that scopes a selector with another one. This works a bit like
* SCSS nesting except the `&` operator isn't supported.
*
* @example
* ```js
* const scope = '.a, .b .c';
* const selector = '> .x, .y';
* const merged = scopeSelector( scope, selector );
* // merged is '.a > .x, .a .y, .b .c > .x, .b .c .y'
* ```
*
* @param {string} scope Selector to scope to.
* @param {string} selector Original selector.
*
* @return {string} Scoped selector.
*/
function scopeSelector( scope, selector ) {
const scopes = scope.split( ',' );
const selectors = selector.split( ',' );

const selectorsScoped = [];
scopes.forEach( ( outer ) => {
selectors.forEach( ( inner ) => {
selectorsScoped.push( `${ outer.trim() } ${ inner.trim() }` );
} );
} );

return selectorsScoped.join( ', ' );
}

function BlockDuotoneStyles( { name, duotoneStyle, id } ) {
const duotonePalette = useMultiOriginPresets( {
presetSetting: 'color.duotone',
Expand Down Expand Up @@ -282,10 +251,29 @@ function BlockDuotoneStyles( { name, duotoneStyle, id } ) {
// Extra .editor-styles-wrapper specificity is needed in the editor
// since we're not using inline styles to apply the filter. We need to
// override duotone applied by global styles and theme.json.
const selectorsGroup = scopeSelector(
`.editor-styles-wrapper .${ id }`,
duotoneSupportSelectors
);

// This is a JavaScript implementation of the PHP function in lib/class-wp-duotone-gutenberg.php
const scopes = ( '.' + id ).split( ',' );
const selectors = duotoneSupportSelectors.split( ',' );

const selectorsScoped = [];
scopes.forEach( ( outer ) => {
selectors.forEach( ( inner ) => {
outer = outer.trim();
inner = inner.trim();
if ( outer && inner ) {
// unlike scopeSelector this concatenates the selectors without a space.
selectorsScoped.push(
'.editor-styles-wrapper ' + outer + '' + inner
);
} else if ( outer ) {
selectorsScoped.push( '.editor-styles-wrapper ' + inner );
} else if ( inner ) {
selectorsScoped.push( '.editor-styles-wrapper ' + outer );
}
} );
} );
const selectorsGroup = selectorsScoped.join( ',' );

return createPortal(
<InlineDuotone
Expand Down

0 comments on commit e60c4b7

Please sign in to comment.