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:Image Block: Hide 'noreferrer' and 'noopener' in Link Rel #17398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/block-library/src/image/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export const LINK_DESTINATION_NONE = 'none';
export const LINK_DESTINATION_MEDIA = 'media';
export const LINK_DESTINATION_ATTACHMENT = 'attachment';
export const LINK_DESTINATION_CUSTOM = 'custom';
export const NEW_TAB_REL = 'noreferrer noopener';
export const NEW_TAB_REL = [ 'noreferrer', 'noopener' ];
export const ALLOWED_MEDIA_TYPES = [ 'image' ];
export const DEFAULT_SIZE_SLUG = 'large';
15 changes: 8 additions & 7 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { speak } from '@wordpress/a11y';
import { createUpgradedEmbedBlock } from '../embed/util';
import icon from './icon';
import ImageSize from './image-size';
import { getUpdatedLinkTargetSettings } from './utils';
import { getUpdatedLinkTargetSettings, removeNewTabRel } from './utils';

/**
* Module constants
Expand Down Expand Up @@ -585,6 +585,7 @@ export class ImageEdit extends Component {
sizeSlug,
} = attributes;

const cleanRel = removeNewTabRel( rel );
const isExternal = isExternalImage( id, url );
const editImageIcon = ( <SVG width={ 20 } height={ 20 } viewBox="0 0 20 20"><Rect x={ 11 } y={ 3 } width={ 7 } height={ 5 } rx={ 1 } /><Rect x={ 2 } y={ 12 } width={ 7 } height={ 5 } rx={ 1 } /><Path d="M13,12h1a3,3,0,0,1-3,3v2a5,5,0,0,0,5-5h1L15,9Z" /><Path d="M4,8H3l2,3L7,8H6A3,3,0,0,1,9,5V3A5,5,0,0,0,4,8Z" /></SVG> );
const controls = (
Expand Down Expand Up @@ -617,18 +618,18 @@ export class ImageEdit extends Component {
onChange={ this.onSetNewTab }
checked={ linkTarget === '_blank' } />
<TextControl
label={ __( 'Link CSS Class' ) }
value={ linkClass || '' }
label={ __( 'Link Rel' ) }
value={ cleanRel || '' }
onChange={ this.onSetLinkRel }
onKeyPress={ stopPropagation }
onKeyDown={ stopPropagationRelevantKeys }
onChange={ this.onSetLinkClass }
/>
<TextControl
label={ __( 'Link Rel' ) }
value={ rel || '' }
onChange={ this.onSetLinkRel }
label={ __( 'Link CSS Class' ) }
value={ linkClass || '' }
onKeyPress={ stopPropagation }
onKeyDown={ stopPropagationRelevantKeys }
onChange={ this.onSetLinkClass }
/>
</>
}
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -24,6 +25,8 @@ export default function save( { attributes } ) {
sizeSlug,
} = attributes;

const newRel = isEmpty( rel ) ? undefined : rel;

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
Expand All @@ -47,7 +50,7 @@ export default function save( { attributes } ) {
className={ linkClass }
href={ href }
target={ linkTarget }
rel={ rel }
rel={ newRel }
>
{ image }
</a>
Expand Down
40 changes: 36 additions & 4 deletions packages/block-library/src/image/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* External dependencies
*/
import {
isEmpty,
each,
} from 'lodash';

/**
* Internal dependencies
*/
Expand All @@ -12,6 +20,30 @@ export function calculatePreferedImageSize( image, container ) {
return { width, height };
}

export function removeNewTabRel( currentRel ) {
let newRel = currentRel;

if ( currentRel !== undefined && ! isEmpty( newRel ) ) {
if ( ! isEmpty( newRel ) ) {
each( NEW_TAB_REL, function( relVal ) {
const regExp = new RegExp( '\\b' + relVal + '\\b', 'gi' );
newRel = newRel.replace( regExp, '' );
} );

// Only trim if NEW_TAB_REL values was replaced.
if ( newRel !== currentRel ) {
newRel = newRel.trim();
}

if ( isEmpty( newRel ) ) {
newRel = undefined;
}
}
}

return newRel;
}

/**
* Helper to get the link target settings to be stored.
*
Expand All @@ -24,11 +56,11 @@ export function calculatePreferedImageSize( image, container ) {
export function getUpdatedLinkTargetSettings( value, { rel } ) {
const linkTarget = value ? '_blank' : undefined;

let updatedRel = rel;
if ( linkTarget && ! rel ) {
updatedRel = NEW_TAB_REL;
} else if ( ! linkTarget && rel === NEW_TAB_REL ) {
let updatedRel;
if ( ! linkTarget && ! rel ) {
updatedRel = undefined;
} else {
updatedRel = removeNewTabRel( rel );
}

return {
Expand Down