~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/js/tinymce/plugins/paste/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:
240
240
        };
241
241
});
242
242
 
 
243
// Included from: js/tinymce/plugins/paste/classes/SmartPaste.js
 
244
 
 
245
/**
 
246
 * SmartPaste.js
 
247
 *
 
248
 * Released under LGPL License.
 
249
 * Copyright (c) 1999-2016 Ephox Corp. All rights reserved
 
250
 *
 
251
 * License: http://www.tinymce.com/license
 
252
 * Contributing: http://www.tinymce.com/contributing
 
253
 */
 
254
 
 
255
/**
 
256
 * Tries to be smart depending on what the user pastes if it looks like an url
 
257
 * it will make a link out of the current selection. If it's an image url that looks
 
258
 * like an image it will check if it's an image and insert it as an image.
 
259
 *
 
260
 * @class tinymce.pasteplugin.SmartPaste
 
261
 * @private
 
262
 */
 
263
define("tinymce/pasteplugin/SmartPaste", [
 
264
        "tinymce/util/Tools"
 
265
], function (Tools) {
 
266
        var isAbsoluteUrl = function (url) {
 
267
                return /^https?:\/\/[\w\?\-\/+=.&%]+$/i.test(url);
 
268
        };
 
269
 
 
270
        var isImageUrl = function (url) {
 
271
                return isAbsoluteUrl(url) && /.(gif|jpe?g|png)$/.test(url);
 
272
        };
 
273
 
 
274
        var createImage = function (editor, url, pasteHtml) {
 
275
                editor.undoManager.extra(function () {
 
276
                        pasteHtml(editor, url);
 
277
                }, function () {
 
278
                        editor.insertContent('<img src="' + url + '">');
 
279
                });
 
280
 
 
281
                return true;
 
282
        };
 
283
 
 
284
        var createLink = function (editor, url, pasteHtml) {
 
285
                editor.undoManager.extra(function () {
 
286
                        pasteHtml(editor, url);
 
287
                }, function () {
 
288
                        editor.execCommand('mceInsertLink', false, url);
 
289
                });
 
290
 
 
291
                return true;
 
292
        };
 
293
 
 
294
        var linkSelection = function (editor, html, pasteHtml) {
 
295
                return editor.selection.isCollapsed() === false && isAbsoluteUrl(html) ? createLink(editor, html, pasteHtml) : false;
 
296
        };
 
297
 
 
298
        var insertImage = function (editor, html, pasteHtml) {
 
299
                return isImageUrl(html) ? createImage(editor, html, pasteHtml) : false;
 
300
        };
 
301
 
 
302
        var pasteHtml = function (editor, html) {
 
303
                editor.insertContent(html, {
 
304
                        merge: editor.settings.paste_merge_formats !== false,
 
305
                        paste: true
 
306
                });
 
307
 
 
308
                return true;
 
309
        };
 
310
 
 
311
        var smartInsertContent = function (editor, html) {
 
312
                Tools.each([
 
313
                        linkSelection,
 
314
                        insertImage,
 
315
                        pasteHtml
 
316
                ], function (action) {
 
317
                        return action(editor, html, pasteHtml) !== true;
 
318
                });
 
319
        };
 
320
 
 
321
        var insertContent = function (editor, html) {
 
322
                if (editor.settings.smart_paste === false) {
 
323
                        pasteHtml(editor, html);
 
324
                } else {
 
325
                        smartInsertContent(editor, html);
 
326
                }
 
327
        };
 
328
 
 
329
        return {
 
330
                isImageUrl: isImageUrl,
 
331
                isAbsoluteUrl: isAbsoluteUrl,
 
332
                insertContent: insertContent
 
333
        };
 
334
});
 
335
 
243
336
// Included from: js/tinymce/plugins/paste/classes/Clipboard.js
244
337
 
245
338
/**
276
369
        "tinymce/dom/RangeUtils",
277
370
        "tinymce/util/VK",
278
371
        "tinymce/pasteplugin/Utils",
 
372
        "tinymce/pasteplugin/SmartPaste",
279
373
        "tinymce/util/Delay"
280
 
], function(Env, RangeUtils, VK, Utils, Delay) {
 
374
], function(Env, RangeUtils, VK, Utils, SmartPaste, Delay) {
281
375
        return function(editor) {
282
376
                var self = this, pasteBinElm, lastRng, keyboardPasteTimeStamp = 0, draggingInternally = false;
283
377
                var pasteBinDefaultContent = '%MCEPASTEBIN%', keyboardPastePlainTextState;
311
405
                                }
312
406
 
313
407
                                if (!args.isDefaultPrevented()) {
314
 
                                        editor.insertContent(html, {merge: editor.settings.paste_merge_formats !== false, data: {paste: true}});
 
408
                                        SmartPaste.insertContent(editor, html);
315
409
                                }
316
410
                        }
317
411
                }
571
665
                        return hasContentType(content, 'text/html') || hasContentType(content, 'text/plain');
572
666
                }
573
667
 
 
668
                function getBase64FromUri(uri) {
 
669
                        var idx;
 
670
 
 
671
                        idx = uri.indexOf(',');
 
672
                        if (idx !== -1) {
 
673
                                return uri.substr(idx + 1);
 
674
                        }
 
675
 
 
676
                        return null;
 
677
                }
 
678
 
 
679
                function isValidDataUriImage(settings, imgElm) {
 
680
                        return settings.images_dataimg_filter ? settings.images_dataimg_filter(imgElm) : true;
 
681
                }
 
682
 
 
683
                function pasteImage(rng, reader, blob) {
 
684
                        if (rng) {
 
685
                                editor.selection.setRng(rng);
 
686
                                rng = null;
 
687
                        }
 
688
 
 
689
                        var dataUri = reader.result;
 
690
                        var base64 = getBase64FromUri(dataUri);
 
691
 
 
692
                        var img = new Image();
 
693
                        img.src = dataUri;
 
694
 
 
695
                        // TODO: Move the bulk of the cache logic to EditorUpload
 
696
                        if (isValidDataUriImage(editor.settings, img)) {
 
697
                                var blobCache = editor.editorUpload.blobCache;
 
698
                                var blobInfo, existingBlobInfo;
 
699
 
 
700
                                existingBlobInfo = blobCache.findFirst(function(cachedBlobInfo) {
 
701
                                        return cachedBlobInfo.base64() === base64;
 
702
                                });
 
703
 
 
704
                                if (!existingBlobInfo) {
 
705
                                        blobInfo = blobCache.create(uniqueId(), blob, base64);
 
706
                                        blobCache.add(blobInfo);
 
707
                                } else {
 
708
                                        blobInfo = existingBlobInfo;
 
709
                                }
 
710
 
 
711
                                pasteHtml('<img src="' + blobInfo.blobUri() + '">');
 
712
                        } else {
 
713
                                pasteHtml('<img src="' + dataUri + '">');
 
714
                        }
 
715
                }
 
716
 
574
717
                /**
575
718
                 * Checks if the clipboard contains image data if it does it will take that data
576
719
                 * and convert it into a data url image and paste that image at the caret location.
582
725
                function pasteImageData(e, rng) {
583
726
                        var dataTransfer = e.clipboardData || e.dataTransfer;
584
727
 
585
 
                        function getBase64FromUri(uri) {
586
 
                                var idx;
587
 
 
588
 
                                idx = uri.indexOf(',');
589
 
                                if (idx !== -1) {
590
 
                                        return uri.substr(idx + 1);
591
 
                                }
592
 
 
593
 
                                return null;
594
 
                        }
595
 
 
596
728
                        function processItems(items) {
597
729
                                var i, item, reader, hadImage = false;
598
730
 
599
 
                                function pasteImage(reader, blob) {
600
 
                                        if (rng) {
601
 
                                                editor.selection.setRng(rng);
602
 
                                                rng = null;
603
 
                                        }
604
 
 
605
 
                                        var blobCache = editor.editorUpload.blobCache;
606
 
                                        var blobInfo = blobCache.create(uniqueId(), blob, getBase64FromUri(reader.result));
607
 
                                        blobCache.add(blobInfo);
608
 
 
609
 
                                        pasteHtml('<img src="' + blobInfo.blobUri() + '">');
610
 
                                }
611
 
 
612
731
                                if (items) {
613
732
                                        for (i = 0; i < items.length; i++) {
614
733
                                                item = items[i];
617
736
                                                        var blob = item.getAsFile ? item.getAsFile() : item;
618
737
 
619
738
                                                        reader = new FileReader();
620
 
                                                        reader.onload = pasteImage.bind(null, reader, blob);
 
739
                                                        reader.onload = pasteImage.bind(null, rng, reader, blob);
621
740
                                                        reader.readAsDataURL(blob);
622
741
 
623
742
                                                        e.preventDefault();
874
993
 
875
994
                self.pasteHtml = pasteHtml;
876
995
                self.pasteText = pasteText;
 
996
                self.pasteImageData = pasteImageData;
877
997
 
878
998
                editor.on('preInit', function() {
879
999
                        registerEventHandlers();