-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to blocks: upgrade and remove spans if possible
- Loading branch information
Showing
9 changed files
with
109 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default function anchorReducer( node ) { | ||
if ( node.nodeName === 'A' ) { | ||
// In jsdom-jscore, 'node.target' can be null. | ||
// TODO: Explore fixing this by patching jsdom-jscore. | ||
if ( node.target && node.target.toLowerCase() === '_blank' ) { | ||
node.rel = 'noreferrer noopener'; | ||
} else { | ||
node.removeAttribute( 'target' ); | ||
node.removeAttribute( 'rel' ); | ||
} | ||
|
||
// Saves anchor elements name attribute as id | ||
if ( node.name && ! node.id ) { | ||
node.id = node.name; | ||
} | ||
|
||
// Keeps id only if there is an internal link pointing to it | ||
if ( | ||
node.id && | ||
! node.ownerDocument.querySelector( `[href="#${ node.id }"]` ) | ||
) { | ||
node.removeAttribute( 'id' ); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/blocks/src/api/raw-handling/empty-span-remover.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { unwrap } from '@wordpress/dom'; | ||
|
||
export default ( node ) => { | ||
// Remove spans with no attributes. | ||
if ( node.nodeName === 'SPAN' && ! node.attributes.length ) { | ||
unwrap( node ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/blocks/src/api/raw-handling/test/anchor-reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import anchorReducer from '../anchor-reducer'; | ||
import { deepFilterHTML } from '../utils'; | ||
|
||
describe( 'anchorReducer', () => { | ||
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, [ anchorReducer ], {} ) ).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, [ anchorReducer ], {} ) ).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, [ anchorReducer ], {} ) ).toEqual( | ||
output | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters