]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/tinymce/plugins/wplink/plugin.js
Wordpress 4.6-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wplink / plugin.js
index d98c0da69499755887cf28443c8d0580e34cb4c0..1ce1c926c1abc85b69c5ea18f4b325ffff32dd62 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;
                        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;
                        }
                                        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'
+                       cmd: 'wp_unlink'
                } );
 
                editor.addButton( 'wp_link_advanced', {
                        close: function() {
                                editToolbar.tempHide = false;
                                editor.execCommand( 'wp_link_cancel' );
-                       }
+                       },
+                       checkLink: checkLink
                };
        } );
 } )( window.tinymce );