|
| 1 | +/** |
| 2 | + * Internal dependencies |
| 3 | + */ |
| 4 | +import { scopeSelector, getValueFromObjectPath } from './utils'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Determine the CSS selector for the block type and target provided, returning |
| 8 | + * it if available. |
| 9 | + * |
| 10 | + * @param {Object} blockType The block's type. |
| 11 | + * @param {string|string[]} target The desired selector's target e.g. `root`, delimited string, or array path. |
| 12 | + * @param {Object} options Options object. |
| 13 | + * @param {boolean} options.fallback Whether or not to fallback to broader selector. |
| 14 | + * |
| 15 | + * @return {?string} The CSS selector or `null` if no selector available. |
| 16 | + */ |
| 17 | +export function getBlockCSSSelector( |
| 18 | + blockType, |
| 19 | + target = 'root', |
| 20 | + options = {} |
| 21 | +) { |
| 22 | + if ( ! target ) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + const { fallback = false } = options; |
| 27 | + const { name, selectors, supports } = blockType; |
| 28 | + |
| 29 | + const hasSelectors = selectors && Object.keys( selectors ).length > 0; |
| 30 | + const path = Array.isArray( target ) ? target.join( '.' ) : target; |
| 31 | + |
| 32 | + // Root selector. |
| 33 | + |
| 34 | + // Calculated before returning as it can be used as a fallback for feature |
| 35 | + // selectors later on. |
| 36 | + let rootSelector = null; |
| 37 | + |
| 38 | + if ( hasSelectors && selectors.root ) { |
| 39 | + // Use the selectors API if available. |
| 40 | + rootSelector = selectors?.root; |
| 41 | + } else if ( supports?.__experimentalSelector ) { |
| 42 | + // Use the old experimental selector supports property if set. |
| 43 | + rootSelector = supports.__experimentalSelector; |
| 44 | + } else { |
| 45 | + // If no root selector found, generate default block class selector. |
| 46 | + rootSelector = |
| 47 | + '.wp-block-' + name.replace( 'core/', '' ).replace( '/', '-' ); |
| 48 | + } |
| 49 | + |
| 50 | + // Return selector if it's the root target we are looking for. |
| 51 | + if ( path === 'root' ) { |
| 52 | + return rootSelector; |
| 53 | + } |
| 54 | + |
| 55 | + // If target is not `root` or `duotone` we have a feature or subfeature |
| 56 | + // as the target. If the target is a string convert to an array. |
| 57 | + const pathArray = Array.isArray( target ) ? target : target.split( '.' ); |
| 58 | + |
| 59 | + // Feature selectors ( may fallback to root selector ); |
| 60 | + if ( pathArray.length === 1 ) { |
| 61 | + const fallbackSelector = fallback ? rootSelector : null; |
| 62 | + |
| 63 | + // Prefer the selectors API if available. |
| 64 | + if ( hasSelectors ) { |
| 65 | + // Get selector from either `feature.root` or shorthand path. |
| 66 | + const featureSelector = |
| 67 | + getValueFromObjectPath( selectors, `${ path }.root`, null ) || |
| 68 | + getValueFromObjectPath( selectors, path, null ); |
| 69 | + |
| 70 | + // Return feature selector if found or any available fallback. |
| 71 | + return featureSelector || fallbackSelector; |
| 72 | + } |
| 73 | + |
| 74 | + // Try getting old experimental supports selector value. |
| 75 | + const featureSelector = getValueFromObjectPath( |
| 76 | + supports, |
| 77 | + `${ path }.__experimentalSelector`, |
| 78 | + null |
| 79 | + ); |
| 80 | + |
| 81 | + // If nothing to work with, provide fallback selector if available. |
| 82 | + if ( ! featureSelector ) { |
| 83 | + return fallbackSelector; |
| 84 | + } |
| 85 | + |
| 86 | + // Scope the feature selector by the block's root selector. |
| 87 | + return scopeSelector( rootSelector, featureSelector ); |
| 88 | + } |
| 89 | + |
| 90 | + // Subfeature selector. |
| 91 | + // This may fallback either to parent feature or root selector. |
| 92 | + let subfeatureSelector; |
| 93 | + |
| 94 | + // Use selectors API if available. |
| 95 | + if ( hasSelectors ) { |
| 96 | + subfeatureSelector = getValueFromObjectPath( selectors, path, null ); |
| 97 | + } |
| 98 | + |
| 99 | + // Only return if we have a subfeature selector. |
| 100 | + if ( subfeatureSelector ) { |
| 101 | + return subfeatureSelector; |
| 102 | + } |
| 103 | + |
| 104 | + // To this point we don't have a subfeature selector. If a fallback has been |
| 105 | + // requested, remove subfeature from target path and return results of a |
| 106 | + // call for the parent feature's selector. |
| 107 | + if ( fallback ) { |
| 108 | + return getBlockCSSSelector( blockType, pathArray[ 0 ], options ); |
| 109 | + } |
| 110 | + |
| 111 | + // We tried. |
| 112 | + return null; |
| 113 | +} |
0 commit comments