Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: duplicate markup on anchor attribute #48438

Closed
wants to merge 9 commits into from
9 changes: 9 additions & 0 deletions docs/reference-guides/block-api/block-supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ supports: {
}
```

If this property is set to `true` or `html`, the id attribute of the root element of the saved markup are referenced. If this property is set to `delimiter`, the attribute is saved as a comment delimiter. This value should be used for dynamic blocks that don't save markup.

```js
// Declare support for anchor links.
supports: {
anchor: 'delimiter'
}
```

## align

- Type: `boolean` or `array`
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ Show minutes required to finish reading the post. ([Source](https://github.com/W
- **Name:** core/post-time-to-read
- **Experimental:** true
- **Category:** theme
- **Supports:** color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** anchor, color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** textAlign

## Title
Expand Down
15 changes: 12 additions & 3 deletions lib/block-supports/anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_anchor_support( $block_type ) {
$has_anchor_support = _wp_array_get( $block_type->supports, array( 'anchor' ), true );
if ( ! $has_anchor_support ) {
$anchor_support = _wp_array_get( $block_type->supports, array( 'anchor' ), true );
if ( ! $anchor_support ) {
return;
}

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
if ( true === $anchor_support || 'html' === $anchor_support ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
'source' => 'attribute',
'attribute' => 'id',
'selector' => '*',
);
}

if ( 'delimiter' === $anchor_support ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
);
Expand Down
20 changes: 15 additions & 5 deletions packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import { useBlockEditingMode } from '../components/block-editing-mode';
*/
const ANCHOR_REGEX = /[\s#]/g;

const ANCHOR_SCHEMA = {
const ANCHOR_SCHEMA_HTML = {
type: 'string',
source: 'attribute',
attribute: 'id',
selector: '*',
};

const ANCHOR_SCHEMA_DELIMITER = {
type: 'string',
};

/**
* Filters registered block settings, extending attributes with anchor using ID
* of the first node.
Expand All @@ -43,10 +47,16 @@ export function addAttribute( settings ) {
}
if ( hasBlockSupport( settings, 'anchor' ) ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
anchor: ANCHOR_SCHEMA,
};
const { anchor } = settings.supports;
if ( [ true, 'html', 'delimiter' ].includes( anchor ) ) {
settings.attributes = {
...settings.attributes,
anchor:
anchor === 'delimiter'
? ANCHOR_SCHEMA_DELIMITER
: ANCHOR_SCHEMA_HTML,
};
}
}

return settings;
Expand Down
39 changes: 36 additions & 3 deletions packages/block-editor/src/hooks/test/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,48 @@ describe( 'anchor', () => {
expect( settings.attributes ).toBe( undefined );
} );

it( 'should assign a new anchor attribute', () => {
[ false, 'incorrect' ].forEach( ( value ) => {
it( `should do nothing if value is ${ value }`, () => {
const settings = registerBlockType( {
...blockSettings,
supports: {
anchor: value,
},
} );

expect( settings.attributes ).toBe( undefined );
} );
} );

[ true, 'html' ].forEach( ( value ) => {
it( `should assign a new anchor attribute referencing html if value is ${ value }`, () => {
const settings = registerBlockType( {
...blockSettings,
supports: {
anchor: value,
},
} );

expect( settings.attributes.anchor ).toEqual( {
attribute: 'id',
selector: '*',
source: 'attribute',
type: 'string',
} );
} );
} );

it( 'should assign a new anchor attribute referencing comment delimiter if value is delimiter', () => {
const settings = registerBlockType( {
...blockSettings,
supports: {
anchor: true,
anchor: `delimiter`,
},
} );

expect( settings.attributes ).toHaveProperty( 'anchor' );
expect( settings.attributes.anchor ).toEqual( {
type: 'string',
} );
} );

it( 'should not override attributes defined in settings', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/avatar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"usesContext": [ "postType", "postId", "commentId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"align": true,
"alignWide": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/calendar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"color": {
"link": true,
"__experimentalSkipSerialization": [ "text", "background" ],
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"inserter": false,
"__experimentalBorder": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comment-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"color": {
"gradients": true,
"link": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comment-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comment-edit-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"link": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comment-reply-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"color": {
"gradients": true,
"link": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"usesContext": [ "postId" ],
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"html": false,
"reusable": false,
"spacing": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"comments/paginationArrow": "paginationArrow"
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"align": true,
"reusable": false,
"html": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/home-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"style"
],
"supports": {
"anchor": true,
"anchor": "delimiter",
"reusable": false,
"html": false,
"typography": {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/latest-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
},
"supports": {
"align": true,
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/loginout/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"className": true,
"typography": {
"fontSize": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"anchor": "delimiter",
"html": false,
"inserter": true,
"typography": {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"openSubmenusOnClick"
],
"supports": {
"anchor": true,
"anchor": "delimiter",
"reusable": false,
"html": false,
"typography": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"usesContext": [ "postType", "postId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"spacing": {
"margin": true,
"padding": true
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-author-name/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"usesContext": [ "postType", "postId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-author/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"usesContext": [ "postType", "postId", "queryId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"spacing": {
"margin": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"usesContext": [ "postId", "postType" ],
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"gradients": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-comments-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"supports": {
"anchor": true,
"anchor": "delimiter",
"html": false,
"color": {
"link": true,
Expand Down
Loading
Loading