* @output wp-includes/js/wplink.js
( function( $, wpLinkL10n, wp ) {
var editor, searchTimer, River, Query, correctedURL,
emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,63}$/i,
urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,63}[^ "]*$/i,
isTouch = ( 'ontouchend' in document );
return editor.$( 'a[data-wplink-edit="true"]' );
minRiverAJAXDuration: 200,
inputs.wrap = $('#wp-link-wrap');
inputs.dialog = $( '#wp-link' );
inputs.backdrop = $( '#wp-link-backdrop' );
inputs.submit = $( '#wp-link-submit' );
inputs.close = $( '#wp-link-close' );
inputs.text = $( '#wp-link-text' );
inputs.url = $( '#wp-link-url' );
inputs.nonce = $( '#_ajax_linking_nonce' );
inputs.openInNewTab = $( '#wp-link-target' );
inputs.search = $( '#wp-link-search' );
rivers.search = new River( $( '#search-results' ) );
rivers.recent = new River( $( '#most-recent-results' ) );
rivers.elements = inputs.dialog.find( '.query-results' );
// Get search notice text.
inputs.queryNotice = $( '#query-notice-message' );
inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
inputs.dialog.on( 'keydown', wpLink.keydown );
inputs.dialog.on( 'keyup', wpLink.keyup );
inputs.submit.on( 'click', function( event ) {
inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel button' ).on( 'click', function( event ) {
rivers.elements.on( 'river-select', wpLink.updateFields );
// Display 'hint' message when search field or 'query-results' box are focused.
inputs.search.on( 'focus.wplink', function() {
inputs.queryNoticeTextDefault.hide();
inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
} ).on( 'blur.wplink', function() {
inputs.queryNoticeTextDefault.show();
inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide();
inputs.search.on( 'keyup input', function() {
window.clearTimeout( searchTimer );
searchTimer = window.setTimeout( function() {
wpLink.searchInternalLinks();
inputs.url.on( 'paste', function() {
setTimeout( wpLink.correctURL, 0 );
inputs.url.on( 'blur', wpLink.correctURL );
// If URL wasn't corrected last time and doesn't start with http:, https:, ? # or /, prepend http://.
correctURL: function () {
var url = $.trim( inputs.url.val() );
if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
inputs.url.val( 'http://' + url );
open: function( editorId, url, text ) {
$body = $( document.body );
$body.addClass( 'modal-open' );
window.wpActiveEditor = editorId;
if ( ! window.wpActiveEditor ) {
this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
if ( typeof window.tinymce !== 'undefined' ) {
// Make sure the link wrapper is the last element in the body,
// or the inline editor toolbar may show above the backdrop.
$body.append( inputs.backdrop, inputs.wrap );
ed = window.tinymce.get( window.wpActiveEditor );
if ( ed && ! ed.isHidden() ) {
if ( ! wpLink.isMCE() && document.selection ) {
this.range = document.selection.createRange();
wpLink.refresh( url, text );
$( document ).trigger( 'wplink-open', inputs.wrap );
return editor && ! editor.isHidden();
refresh: function( url, text ) {
// Refresh rivers (clear links, check visibility).
wpLink.mceRefresh( url, text );
// For the Text editor the "Link text" field is always shown.
if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
inputs.wrap.addClass( 'has-text-field' );
if ( document.selection ) {
linkText = document.selection.createRange().text || text || '';
} else if ( typeof this.textarea.selectionStart !== 'undefined' &&
( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || text || '';
wpLink.setDefaultValues();
// Close the onscreen keyboard.
inputs.url.trigger( 'focus' ).trigger( 'blur' );
* Focus the URL field and highlight its contents.
* If this is moved above the selection changes,
* IE will show a flashing cursor over the dialog.
window.setTimeout( function() {
inputs.url.trigger( 'focus' );
// Load the most recent results if this is the first time opening the panel.
if ( ! rivers.recent.ul.children().length ) {
correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
hasSelectedText: function( linkNode ) {
var node, nodes, i, html = editor.selection.getContent();
// Partial html and not a fully selected anchor element.
if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
nodes = linkNode[0].childNodes;
if ( ! nodes || ! nodes.length ) {
for ( i = nodes.length - 1; i >= 0; i-- ) {
if ( node.nodeType != 3 && ! window.tinymce.dom.BookmarkManager.isBookmarkNode( node ) ) {
mceRefresh: function( searchStr, text ) {
onlyText = this.hasSelectedText( linkNode );
linkText = linkNode.text();
href = linkNode.attr( 'href' );
if ( ! $.trim( linkText ) ) {
if ( searchStr && ( urlRegexp.test( searchStr ) || emailRegexp.test( searchStr ) ) ) {
if ( href !== '_wp_link_placeholder' ) {
inputs.openInNewTab.prop( 'checked', '_blank' === linkNode.attr( 'target' ) );
inputs.submit.val( wpLinkL10n.update );
this.setDefaultValues( linkText );
if ( searchStr && searchStr !== href ) {
// The user has typed something in the inline dialog. Trigger a search with it.
inputs.search.val( searchStr );
// Always reset the search.
window.setTimeout( function() {
wpLink.searchInternalLinks();
linkText = editor.selection.getContent({ format: 'text' }) || text || '';
this.setDefaultValues( linkText );
inputs.text.val( linkText );
inputs.wrap.addClass( 'has-text-field' );
inputs.wrap.removeClass( 'has-text-field' );
close: function( reset ) {
$( document.body ).removeClass( 'modal-open' );
wpLink.modalOpen = false;
if ( reset !== 'noReset' ) {
if ( ! wpLink.isMCE() ) {
wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
if ( editor.plugins.wplink ) {
editor.plugins.wplink.close();
$( document ).trigger( 'wplink-close', inputs.wrap );
href: $.trim( inputs.url.val() ),
target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : null
buildHtml: function(attrs) {
var html = '<a href="' + attrs.href + '"';
html += ' rel="noopener" target="' + attrs.target + '"';
var attrs, text, html, begin, end, cursor, selection,
textarea = wpLink.textarea;
attrs = wpLink.getAttrs();
text = inputs.text.val();
var parser = document.createElement( 'a' );
parser.href = attrs.href;
if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line
// If there's no href, return.
html = wpLink.buildHtml(attrs);
if ( document.selection && wpLink.range ) {
// Note: If no text is selected, IE will not place the cursor
// inside the closing tag.
wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
} else if ( typeof textarea.selectionStart !== 'undefined' ) {
begin = textarea.selectionStart;
end = textarea.selectionEnd;
selection = text || textarea.value.substring( begin, end );
html = html + selection + '</a>';
cursor = begin + html.length;
// If no text is selected, place the cursor inside the closing tag.
if ( begin === end && ! selection ) {
textarea.value.substring( 0, begin ) +
textarea.value.substring( end, textarea.value.length )
// Update cursor position.
textarea.selectionStart = textarea.selectionEnd = cursor;
$( textarea ).trigger( 'change' );
// Audible confirmation message when a link has been inserted in the Editor.
wp.a11y.speak( wpLinkL10n.linkInserted );
var attrs = wpLink.getAttrs(),
var parser = document.createElement( 'a' );
parser.href = attrs.href;
if ( 'javascript:' === parser.protocol || 'data:' === parser.protocol ) { // jshint ignore:line
editor.execCommand( 'unlink' );
editor.undoManager.transact( function() {
editor.execCommand( 'mceInsertLink', false, { href: '_wp_link_placeholder', 'data-wp-temp-link': 1 } );
$link = editor.$( 'a[data-wp-temp-link="1"]' ).removeAttr( 'data-wp-temp-link' );
hasText = $.trim( $link.text() );
editor.execCommand( 'unlink' );
if ( inputs.wrap.hasClass( 'has-text-field' ) ) {
text = inputs.text.val();
} else if ( ! hasText ) {
$link.text( attrs.href );
attrs['data-wplink-edit'] = null;
attrs['data-mce-href'] = attrs.href;
wpLink.close( 'noReset' );
editor.selection.select( $link[0] );
if ( editor.plugins.wplink ) {
editor.plugins.wplink.checkLink( $link[0] );
// Audible confirmation message when a link has been inserted in the Editor.
wp.a11y.speak( wpLinkL10n.linkInserted );
updateFields: function( e, li ) {
inputs.url.val( li.children( '.item-permalink' ).val() );
if ( inputs.wrap.hasClass( 'has-text-field' ) && ! inputs.text.val() ) {
inputs.text.val( li.children( '.item-title' ).text() );
getUrlFromSelection: function( selection ) {
selection = editor.selection.getContent({ format: 'text' });
} else if ( document.selection && wpLink.range ) {
selection = wpLink.range.text;
} else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
selection = $.trim( selection );
if ( selection && emailRegexp.test( selection ) ) {
// Selection is email address.
return 'mailto:' + selection;
} else if ( selection && urlRegexp.test( selection ) ) {
return selection.replace( /&|�?38;/gi, '&' );
setDefaultValues: function( selection ) {
inputs.url.val( this.getUrlFromSelection( selection ) );