diff --git a/packages/block-editor/src/hooks/anchor.js b/packages/block-editor/src/hooks/anchor.js index b2ecc81f907136..3785db7954b62b 100644 --- a/packages/block-editor/src/hooks/anchor.js +++ b/packages/block-editor/src/hooks/anchor.js @@ -20,13 +20,6 @@ import { useBlockEditingMode } from '../components/block-editing-mode'; */ const ANCHOR_REGEX = /[\s#]/g; -const ANCHOR_SCHEMA = { - type: 'string', - source: 'attribute', - attribute: 'id', - selector: '*', -}; - /** * Filters registered block settings, extending attributes with anchor using ID * of the first node. @@ -44,7 +37,9 @@ export function addAttribute( settings ) { // Gracefully handle if settings.attributes is undefined. settings.attributes = { ...settings.attributes, - anchor: ANCHOR_SCHEMA, + anchor: { + type: 'string', + }, }; } @@ -90,7 +85,7 @@ function BlockEditAnchorControlPure( { anchor, setAttributes } ) { onChange={ ( nextValue ) => { nextValue = nextValue.replace( ANCHOR_REGEX, '-' ); setAttributes( { - anchor: nextValue, + anchor: nextValue !== '' ? nextValue : undefined, } ); } } autoCapitalize="none" diff --git a/packages/block-editor/src/hooks/test/__snapshots__/anchor.native.js.snap b/packages/block-editor/src/hooks/test/__snapshots__/anchor.native.js.snap index 03407a1fe55d85..99af763fc6594f 100644 --- a/packages/block-editor/src/hooks/test/__snapshots__/anchor.native.js.snap +++ b/packages/block-editor/src/hooks/test/__snapshots__/anchor.native.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`anchor should set the ID attribute on the block 1`] = ` -" +"
" `; diff --git a/packages/block-library/src/group/test/__snapshots__/transforms.native.js.snap b/packages/block-library/src/group/test/__snapshots__/transforms.native.js.snap index ca0bd5b36ee0c4..013d86f4867445 100644 --- a/packages/block-library/src/group/test/__snapshots__/transforms.native.js.snap +++ b/packages/block-library/src/group/test/__snapshots__/transforms.native.js.snap @@ -3,7 +3,7 @@ exports[`Group block transforms to Columns block 1`] = ` "One.
diff --git a/packages/block-library/src/list/utils.js b/packages/block-library/src/list/utils.js index 60f3354cea426f..25d28083d405fc 100644 --- a/packages/block-library/src/list/utils.js +++ b/packages/block-library/src/list/utils.js @@ -14,7 +14,7 @@ export function createListBlockFromDOMElement( listElement ) { const type = listElement.getAttribute( 'type' ); const listAttributes = { ordered: 'OL' === listElement.tagName, - anchor: listElement.id === '' ? undefined : listElement.id, + anchor: listElement.id ? listElement.id : undefined, start: listElement.getAttribute( 'start' ) ? parseInt( listElement.getAttribute( 'start' ), 10 ) : undefined, diff --git a/packages/block-library/src/separator/transforms.js b/packages/block-library/src/separator/transforms.js index 1f40cb5dba8b5e..2457714aeab251 100644 --- a/packages/block-library/src/separator/transforms.js +++ b/packages/block-library/src/separator/transforms.js @@ -24,7 +24,7 @@ const transforms = { blocks: [ 'core/spacer' ], // Transform to Spacer. transform: ( { anchor } ) => { return createBlock( 'core/spacer', { - anchor: anchor || '', + anchor: anchor || undefined, } ); }, }, diff --git a/packages/block-library/src/spacer/transforms.js b/packages/block-library/src/spacer/transforms.js index ddd8afb2ef98d9..e730d0368148fa 100644 --- a/packages/block-library/src/spacer/transforms.js +++ b/packages/block-library/src/spacer/transforms.js @@ -10,7 +10,7 @@ const transforms = { blocks: [ 'core/separator' ], // Transform to Separator. transform: ( { anchor } ) => { return createBlock( 'core/separator', { - anchor: anchor || '', + anchor: anchor || undefined, } ); }, }, diff --git a/packages/blocks/src/api/parser/apply-built-in-validation-fixes.js b/packages/blocks/src/api/parser/apply-built-in-validation-fixes.js index 52d6583265e6b9..240b7d164c3fba 100644 --- a/packages/blocks/src/api/parser/apply-built-in-validation-fixes.js +++ b/packages/blocks/src/api/parser/apply-built-in-validation-fixes.js @@ -2,7 +2,21 @@ * Internal dependencies */ import { fixCustomClassname } from './fix-custom-classname'; -import { fixAriaLabel } from './fix-aria-label'; +import { fixGlobalAttribute } from './fix-global-attribute'; + +const ARIA_LABEL_ATTR_SCHEMA = { + type: 'string', + source: 'attribute', + selector: '[data-aria-label] > *', + attribute: 'aria-label', +}; + +const ANCHOR_ATTR_SCHEMA = { + type: 'string', + source: 'attribute', + selector: '[data-anchor] > *', + attribute: 'id', +}; /** * Attempts to fix block invalidation by applying build-in validation fixes @@ -26,10 +40,22 @@ export function applyBuiltInValidationFixes( block, blockType ) { originalContent ); // Fix block invalidation for ariaLabel attribute. - updatedBlockAttributes = fixAriaLabel( + updatedBlockAttributes = fixGlobalAttribute( updatedBlockAttributes, blockType, - originalContent + originalContent, + 'ariaLabel', + 'data-aria-label', + ARIA_LABEL_ATTR_SCHEMA + ); + // Fix block invalidation for anchor attribute. + updatedBlockAttributes = fixGlobalAttribute( + updatedBlockAttributes, + blockType, + originalContent, + 'anchor', + 'data-anchor', + ANCHOR_ATTR_SCHEMA ); return { diff --git a/packages/blocks/src/api/parser/fix-aria-label.js b/packages/blocks/src/api/parser/fix-aria-label.js deleted file mode 100644 index 79fa30c713da20..00000000000000 --- a/packages/blocks/src/api/parser/fix-aria-label.js +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Internal dependencies - */ -import { hasBlockSupport } from '../registration'; -import { parseWithAttributeSchema } from './get-block-attributes'; - -const ARIA_LABEL_ATTR_SCHEMA = { - type: 'string', - source: 'attribute', - selector: '[data-aria-label] > *', - attribute: 'aria-label', -}; - -/** - * Given an HTML string, returns the aria-label attribute assigned to - * the root element in the markup. - * - * @param {string} innerHTML Markup string from which to extract the aria-label. - * - * @return {string} The aria-label assigned to the root element. - */ -export function getHTMLRootElementAriaLabel( innerHTML ) { - const parsed = parseWithAttributeSchema( - `test