Skip to content

Commit

Permalink
Adds aria-label to links that open in new windows (#11063)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwelcher authored and youknowriad committed Oct 30, 2018
1 parent 844f0ea commit 489fe16
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { sprintf, __ } from '@wordpress/i18n';
import { Component, createRef } from '@wordpress/element';
import {
ExternalLink,
Expand All @@ -26,7 +26,16 @@ import PositionedAtSelection from './positioned-at-selection';

const stopKeyPropagation = ( event ) => event.stopPropagation();

function createLinkFormat( { url, opensInNewWindow } ) {
/**
* Generates the format object that will be applied to the link text.
*
* @param {string} url The href of the link.
* @param {boolean} opensInNewWindow Whether this link will open in a new window.
* @param {Object} text The text that is being hyperlinked.
*
* @return {Object} The final format object.
*/
function createLinkFormat( { url, opensInNewWindow, text } ) {
const format = {
type: 'core/link',
attributes: {
Expand All @@ -35,8 +44,12 @@ function createLinkFormat( { url, opensInNewWindow } ) {
};

if ( opensInNewWindow ) {
// translators: accessibility label for external links, where the argument is the link text
const label = sprintf( __( '%s (opens in a new tab)' ), text ).trim();

format.attributes.target = '_blank';
format.attributes.rel = 'noreferrer noopener';
format.attributes[ 'aria-label' ] = label;
}

return format;
Expand Down Expand Up @@ -139,7 +152,7 @@ class InlineLinkUI extends Component {

// Apply now if URL is not being edited.
if ( ! isShowingInput( this.props, this.state ) ) {
onChange( applyFormat( value, createLinkFormat( { url, opensInNewWindow } ) ) );
onChange( applyFormat( value, createLinkFormat( { url, opensInNewWindow, text: value.text } ) ) );
}
}

Expand All @@ -152,7 +165,7 @@ class InlineLinkUI extends Component {
const { isActive, value, onChange, speak } = this.props;
const { inputValue, opensInNewWindow } = this.state;
const url = prependHTTP( inputValue );
const format = createLinkFormat( { url, opensInNewWindow } );
const format = createLinkFormat( { url, opensInNewWindow, text: value.text } );

event.preventDefault();

Expand Down

0 comments on commit 489fe16

Please sign in to comment.