]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/tinymce/plugins/wplink/plugin.js
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wplink / plugin.js
index c7836b3763cc63464ae8aa9259469be9fd2ed197..96357f2e9eb2cf728e710efa96a0b956089f4451 100644 (file)
                var doingUndoRedo;
                var doingUndoRedoTimer;
                var $ = window.jQuery;
+               var emailRegex = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
+               var urlRegex1 = /^https?:\/\/([^\s/?.#-][^\s\/?.#]*\.?)+(\/[^\s"]*)?$/i;
+               var urlRegex2 = /^https?:\/\/[^\/]+\.[^\/]+($|\/)/i;
+               var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {};
+               var hasLinkError = false;
 
                function getSelectedLink() {
                        var href, html,
                }
 
                function removePlaceholderStrings( content, dataAttr ) {
-                       if ( dataAttr ) {
-                               content = content.replace( / data-wplink-edit="true"/g, '' );
+                       return content.replace( /(<a [^>]+>)([\s\S]*?)<\/a>/g, function( all, tag, text ) {
+                               if ( tag.indexOf( ' href="_wp_link_placeholder"' ) > -1 ) {
+                                       return text;
+                               }
+
+                               if ( dataAttr ) {
+                                       tag = tag.replace( / data-wplink-edit="true"/g, '' );
+                               }
+
+                               tag = tag.replace( / data-wplink-url-error="true"/g, '' );
+
+                               return tag + text + '</a>';
+                       });
+               }
+
+               function checkLink( node ) {
+                       var $link = editor.$( node );
+                       var href = $link.attr( 'href' );
+
+                       if ( ! href || typeof $ === 'undefined' ) {
+                               return;
                        }
 
-                       return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
+                       hasLinkError = false;
+
+                       if ( /^http/i.test( href ) && ( ! urlRegex1.test( href ) || ! urlRegex2.test( href ) ) ) {
+                               hasLinkError = true;
+                               $link.attr( 'data-wplink-url-error', 'true' );
+                               speak( editor.translate( 'Warning: the link has been inserted but may have errors. Please test it.' ), 'assertive' );
+                       } else {
+                               $link.removeAttr( 'data-wplink-url-error' );
+                       }
                }
 
                editor.on( 'preinit', function() {
                                editToolbar = editor.wp._createToolbar( editButtons, true );
 
                                editToolbar.on( 'show', function() {
-                                       if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
+                                       if ( typeof window.wpLink === 'undefined' || ! window.wpLink.modalOpen ) {
                                                window.setTimeout( function() {
                                                        var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0],
                                                                selection = linkNode && ( linkNode.textContent || linkNode.innerText );
                                        return;
                                }
 
-                               if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) ) {
+                               if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) && ! emailRegex.test( href ) ) {
                                        href = 'http://' + href;
                                }
 
                                if ( ! tinymce.trim( linkNode.innerHTML ) ) {
                                        editor.$( linkNode ).text( text || href );
                                }
+
+                               checkLink( linkNode );
                        }
 
                        inputInstance.reset();
                        editor.nodeChanged();
 
                        // Audible confirmation message when a link has been inserted in the Editor.
-                       if ( typeof window.wp !== 'undefined' && window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
-                               window.wp.a11y.speak( window.wpLinkL10n.linkInserted );
+                       if ( typeof window.wpLinkL10n !== 'undefined' && ! hasLinkError ) {
+                               speak( window.wpLinkL10n.linkInserted );
                        }
                } );
 
                        if ( ! editToolbar.tempHide ) {
                                inputInstance.reset();
                                removePlaceholders();
-                               editor.focus();
-                               editToolbar.tempHide = false;
                        }
                } );
 
-               // WP default shortcut
+               editor.addCommand( 'wp_unlink', function() {
+                       editor.execCommand( 'unlink' );
+                       editToolbar.tempHide = false;
+                       editor.execCommand( 'wp_link_cancel' );
+               } );
+
+               // WP default shortcuts
                editor.addShortcut( 'access+a', '', 'WP_Link' );
+               editor.addShortcut( 'access+s', '', 'wp_unlink' );
                // The "de-facto standard" shortcut, see #27305
                editor.addShortcut( 'meta+k', '', 'WP_Link' );
 
                // When doing undo and redo with keyboard shortcuts (Ctrl|Cmd+Z, Ctrl|Cmd+Shift+Z, Ctrl|Cmd+Y),
                // set a flag to not focus the inline dialog. The editor has to remain focused so the users can do consecutive undo/redo.
                editor.on( 'keydown', function( event ) {
+                       if ( event.keyCode === 27 ) { // Esc
+                               editor.execCommand( 'wp_link_cancel' );
+                       }
+
                        if ( event.altKey || ( tinymce.Env.mac && ( ! event.metaKey || event.ctrlKey ) ) ||
                                ( ! tinymce.Env.mac && ! event.ctrlKey ) ) {
 
                                                        $input.val( ui.item.permalink );
                                                        $( element.firstChild.nextSibling ).val( ui.item.title );
 
-                                                       if ( 9 === event.keyCode && typeof window.wp !== 'undefined' &&
-                                                               window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
+                                                       if ( 9 === event.keyCode && typeof window.wpLinkL10n !== 'undefined' ) {
                                                                // Audible confirmation message when a link has been selected.
-                                                               window.wp.a11y.speak( window.wpLinkL10n.linkSelected );
+                                                               speak( window.wpLinkL10n.linkSelected );
                                                        }
 
                                                        return false;
                                                        $input.autocomplete( 'search' );
                                                }
                                        } )
+                                       // Returns a jQuery object containing the menu element.
                                        .autocomplete( 'widget' )
                                                .addClass( 'wplink-autocomplete' )
                                                .attr( 'role', 'listbox' )
-                                               .removeAttr( 'tabindex' ); // Remove the `tabindex=0` attribute added by jQuery UI.
+                                               .removeAttr( 'tabindex' ) // Remove the `tabindex=0` attribute added by jQuery UI.
+                                               /*
+                                                * Looks like Safari and VoiceOver need an `aria-selected` attribute. See ticket #33301.
+                                                * The `menufocus` and `menublur` events are the same events used to add and remove
+                                                * the `ui-state-focus` CSS class on the menu items. See jQuery UI Menu Widget.
+                                                */
+                                               .on( 'menufocus', function( event, ui ) {
+                                                       ui.item.attr( 'aria-selected', 'true' );
+                                               })
+                                               .on( 'menublur', function() {
+                                                       /*
+                                                        * The `menublur` event returns an object where the item is `null`
+                                                        * so we need to find the active item with other means.
+                                                        */
+                                                       $( this ).find( '[aria-selected="true"]' ).removeAttr( 'aria-selected' );
+                                               });
                                }
 
                                tinymce.$( input ).on( 'keydown', function( event ) {
                        var linkNode = editor.dom.getParent( event.element, 'a' ),
                                $linkNode, href, edit;
 
-                       if ( tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
+                       if ( typeof window.wpLink !== 'undefined' && window.wpLink.modalOpen ) {
                                editToolbar.tempHide = true;
                                return;
                        }
                                edit = $linkNode.attr( 'data-wplink-edit' );
 
                                if ( href === '_wp_link_placeholder' || edit ) {
-                                       if ( edit && ! inputInstance.getURL() ) {
+                                       if ( href !== '_wp_link_placeholder' && ! inputInstance.getURL() ) {
                                                inputInstance.setURL( href );
                                        }
 
                                        previewInstance.setURL( href );
                                        event.element = linkNode;
                                        event.toolbar = toolbar;
+
+                                       if ( $linkNode.attr( 'data-wplink-url-error' ) === 'true' ) {
+                                               toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' );
+                                       } else {
+                                               toolbar.$el.find( '.wp-link-preview a' ).removeClass( 'wplink-url-error' );
+                                               hasLinkError = false;
+                                       }
                                }
+                       } else if ( editToolbar.visible() ) {
+                               editor.execCommand( 'wp_link_cancel' );
                        }
                } );
 
                } );
 
                editor.addButton( 'wp_link_remove', {
-                       tooltip: 'Remove',
-                       icon: 'dashicon dashicons-no',
-                       cmd: 'unlink'
+                       tooltip: 'Remove link',
+                       icon: 'dashicon dashicons-editor-unlink',
+                       cmd: 'wp_unlink'
                } );
 
                editor.addButton( 'wp_link_advanced', {
                        close: function() {
                                editToolbar.tempHide = false;
                                editor.execCommand( 'wp_link_cancel' );
-                       }
+                       },
+                       checkLink: checkLink
                };
        } );
 } )( window.tinymce );