Skip to content

Commit

Permalink
Allow the target and rel attributes on <a> tags. (#8125)
Browse files Browse the repository at this point in the history
The `target` attribute is only supported when its value is `'_blank'`, in which case the `rel` attribute is set to `'noopener noreferrer'`.

For any other values, the `target` and `rel` attributes are removed, as we don't know how to handle them.

Fixes #4498.
  • Loading branch information
pento authored Jul 25, 2018
1 parent f508995 commit 8d14697
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export default function( node, doc, schema ) {
node = replaceTag( node, 'strong', doc );
} else if ( node.nodeName === 'I' ) {
node = replaceTag( node, 'em', doc );
} else if ( node.nodeName === 'A' ) {
if ( node.target.toLowerCase() === '_blank' ) {
node.rel = 'noreferrer noopener';
} else {
node.removeAttribute( 'target' );
node.removeAttribute( 'rel' );
}
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ describe( 'phrasingContentReducer', () => {
it( 'should remove invalid phrasing content', () => {
expect( deepFilterHTML( '<strong><p>test</p></strong>', [ phrasingContentReducer ], { p: {} } ) ).toEqual( '<p>test</p>' );
} );

it( 'should normalise the rel attribute', () => {
const input = '<a href="https://wordpress.org" target="_blank">WordPress</a>';
const output = '<a href="https://wordpress.org" target="_blank" rel="noreferrer noopener">WordPress</a>';
expect( deepFilterHTML( input, [ phrasingContentReducer ], {} ) ).toEqual( output );
} );

it( 'should only allow target="_blank"', () => {
const input = '<a href="https://wordpress.org" target="_self">WordPress</a>';
const output = '<a href="https://wordpress.org">WordPress</a>';
expect( deepFilterHTML( input, [ phrasingContentReducer ], {} ) ).toEqual( output );
} );

it( 'should remove the rel attribute when target is not set', () => {
const input = '<a href="https://wordpress.org" rel="noopener">WordPress</a>';
const output = '<a href="https://wordpress.org">WordPress</a>';
expect( deepFilterHTML( input, [ phrasingContentReducer ], {} ) ).toEqual( output );
} );
} );
4 changes: 2 additions & 2 deletions packages/blocks/src/api/raw-handling/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ describe( 'removeInvalidHTML', () => {
} );

it( 'should keep some attributes', () => {
const input = '<a href="#keep">test</a>';
const output = '<a href="#keep">test</a>';
const input = '<a href="#keep" target="_blank">test</a>';
const output = '<a href="#keep" target="_blank">test</a>';
expect( removeInvalidHTML( input, schema ) ).toBe( output );
} );

Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/api/raw-handling/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const phrasingContentSchema = {
em: {},
del: {},
ins: {},
a: { attributes: [ 'href' ] },
a: { attributes: [ 'href', 'target', 'rel' ] },
code: {},
abbr: { attributes: [ 'title' ] },
sub: {},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/ms-word-online-out.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/">link</a></p>
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/" target="_blank" rel="noreferrer noopener">link</a></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
Expand Down

0 comments on commit 8d14697

Please sign in to comment.