~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/js/tinymce/plugins/wplink/plugin.js

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
                var doingUndoRedo;
94
94
                var doingUndoRedoTimer;
95
95
                var $ = window.jQuery;
 
96
                var emailRegex = /^(mailto:)?[a-z0-9._%+-]+@[a-z0-9][a-z0-9.-]*\.[a-z]{2,63}$/i;
 
97
                var urlRegex1 = /^https?:\/\/([^\s/?.#-][^\s\/?.#]*\.?)+(\/[^\s"]*)?$/i;
 
98
                var urlRegex2 = /^https?:\/\/[^\/]+\.[^\/]+($|\/)/i;
 
99
                var speak = ( typeof window.wp !== 'undefined' && window.wp.a11y && window.wp.a11y.speak ) ? window.wp.a11y.speak : function() {};
 
100
                var hasLinkError = false;
96
101
 
97
102
                function getSelectedLink() {
98
103
                        var href, html,
131
136
                }
132
137
 
133
138
                function removePlaceholderStrings( content, dataAttr ) {
134
 
                        if ( dataAttr ) {
135
 
                                content = content.replace( / data-wplink-edit="true"/g, '' );
136
 
                        }
137
 
 
138
 
                        return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
 
139
                        return content.replace( /(<a [^>]+>)([\s\S]*?)<\/a>/g, function( all, tag, text ) {
 
140
                                if ( tag.indexOf( ' href="_wp_link_placeholder"' ) > -1 ) {
 
141
                                        return text;
 
142
                                }
 
143
 
 
144
                                if ( dataAttr ) {
 
145
                                        tag = tag.replace( / data-wplink-edit="true"/g, '' );
 
146
                                }
 
147
 
 
148
                                tag = tag.replace( / data-wplink-url-error="true"/g, '' );
 
149
 
 
150
                                return tag + text + '</a>';
 
151
                        });
 
152
                }
 
153
 
 
154
                function checkLink( node ) {
 
155
                        var $link = editor.$( node );
 
156
                        var href = $link.attr( 'href' );
 
157
 
 
158
                        if ( ! href || typeof $ === 'undefined' ) {
 
159
                                return;
 
160
                        }
 
161
 
 
162
                        hasLinkError = false;
 
163
 
 
164
                        if ( /^http/i.test( href ) && ( ! urlRegex1.test( href ) || ! urlRegex2.test( href ) ) ) {
 
165
                                hasLinkError = true;
 
166
                                $link.attr( 'data-wplink-url-error', 'true' );
 
167
                                speak( editor.translate( 'Warning: the link has been inserted but may have errors. Please test it.' ), 'assertive' );
 
168
                        } else {
 
169
                                $link.removeAttr( 'data-wplink-url-error' );
 
170
                        }
139
171
                }
140
172
 
141
173
                editor.on( 'preinit', function() {
158
190
                                editToolbar = editor.wp._createToolbar( editButtons, true );
159
191
 
160
192
                                editToolbar.on( 'show', function() {
161
 
                                        if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
 
193
                                        if ( typeof window.wpLink === 'undefined' || ! window.wpLink.modalOpen ) {
162
194
                                                window.setTimeout( function() {
163
195
                                                        var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0],
164
196
                                                                selection = linkNode && ( linkNode.textContent || linkNode.innerText );
222
254
                                        return;
223
255
                                }
224
256
 
225
 
                                if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) ) {
 
257
                                if ( ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( href ) && ! emailRegex.test( href ) ) {
226
258
                                        href = 'http://' + href;
227
259
                                }
228
260
 
231
263
                                if ( ! tinymce.trim( linkNode.innerHTML ) ) {
232
264
                                        editor.$( linkNode ).text( text || href );
233
265
                                }
 
266
 
 
267
                                checkLink( linkNode );
234
268
                        }
235
269
 
236
270
                        inputInstance.reset();
237
271
                        editor.nodeChanged();
238
272
 
239
273
                        // Audible confirmation message when a link has been inserted in the Editor.
240
 
                        if ( typeof window.wp !== 'undefined' && window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
241
 
                                window.wp.a11y.speak( window.wpLinkL10n.linkInserted );
 
274
                        if ( typeof window.wpLinkL10n !== 'undefined' && ! hasLinkError ) {
 
275
                                speak( window.wpLinkL10n.linkInserted );
242
276
                        }
243
277
                } );
244
278
 
246
280
                        if ( ! editToolbar.tempHide ) {
247
281
                                inputInstance.reset();
248
282
                                removePlaceholders();
249
 
                                editor.focus();
250
 
                                editToolbar.tempHide = false;
251
283
                        }
252
284
                } );
253
285
 
254
 
                // WP default shortcut
 
286
                editor.addCommand( 'wp_unlink', function() {
 
287
                        editor.execCommand( 'unlink' );
 
288
                        editToolbar.tempHide = false;
 
289
                        editor.execCommand( 'wp_link_cancel' );
 
290
                } );
 
291
 
 
292
                // WP default shortcuts
255
293
                editor.addShortcut( 'access+a', '', 'WP_Link' );
 
294
                editor.addShortcut( 'access+s', '', 'wp_unlink' );
256
295
                // The "de-facto standard" shortcut, see #27305
257
296
                editor.addShortcut( 'meta+k', '', 'WP_Link' );
258
297
 
313
352
                // When doing undo and redo with keyboard shortcuts (Ctrl|Cmd+Z, Ctrl|Cmd+Shift+Z, Ctrl|Cmd+Y),
314
353
                // set a flag to not focus the inline dialog. The editor has to remain focused so the users can do consecutive undo/redo.
315
354
                editor.on( 'keydown', function( event ) {
 
355
                        if ( event.keyCode === 27 ) { // Esc
 
356
                                editor.execCommand( 'wp_link_cancel' );
 
357
                        }
 
358
 
316
359
                        if ( event.altKey || ( tinymce.Env.mac && ( ! event.metaKey || event.ctrlKey ) ) ||
317
360
                                ( ! tinymce.Env.mac && ! event.ctrlKey ) ) {
318
361
 
386
429
                                                        $input.val( ui.item.permalink );
387
430
                                                        $( element.firstChild.nextSibling ).val( ui.item.title );
388
431
 
389
 
                                                        if ( 9 === event.keyCode && typeof window.wp !== 'undefined' &&
390
 
                                                                window.wp.a11y && typeof window.wpLinkL10n !== 'undefined' ) {
 
432
                                                        if ( 9 === event.keyCode && typeof window.wpLinkL10n !== 'undefined' ) {
391
433
                                                                // Audible confirmation message when a link has been selected.
392
 
                                                                window.wp.a11y.speak( window.wpLinkL10n.linkSelected );
 
434
                                                                speak( window.wpLinkL10n.linkSelected );
393
435
                                                        }
394
436
 
395
437
                                                        return false;
475
517
                        var linkNode = editor.dom.getParent( event.element, 'a' ),
476
518
                                $linkNode, href, edit;
477
519
 
478
 
                        if ( tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
 
520
                        if ( typeof window.wpLink !== 'undefined' && window.wpLink.modalOpen ) {
479
521
                                editToolbar.tempHide = true;
480
522
                                return;
481
523
                        }
498
540
                                        previewInstance.setURL( href );
499
541
                                        event.element = linkNode;
500
542
                                        event.toolbar = toolbar;
 
543
 
 
544
                                        if ( $linkNode.attr( 'data-wplink-url-error' ) === 'true' ) {
 
545
                                                toolbar.$el.find( '.wp-link-preview a' ).addClass( 'wplink-url-error' );
 
546
                                        } else {
 
547
                                                toolbar.$el.find( '.wp-link-preview a' ).removeClass( 'wplink-url-error' );
 
548
                                                hasLinkError = false;
 
549
                                        }
501
550
                                }
 
551
                        } else if ( editToolbar.visible() ) {
 
552
                                editor.execCommand( 'wp_link_cancel' );
502
553
                        }
503
554
                } );
504
555
 
511
562
                editor.addButton( 'wp_link_remove', {
512
563
                        tooltip: 'Remove',
513
564
                        icon: 'dashicon dashicons-no',
514
 
                        cmd: 'unlink'
 
565
                        cmd: 'wp_unlink'
515
566
                } );
516
567
 
517
568
                editor.addButton( 'wp_link_advanced', {
555
606
                        close: function() {
556
607
                                editToolbar.tempHide = false;
557
608
                                editor.execCommand( 'wp_link_cancel' );
558
 
                        }
 
609
                        },
 
610
                        checkLink: checkLink
559
611
                };
560
612
        } );
561
613
} )( window.tinymce );