plugin.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.0.1 (2019-02-21)
  8. */
  9. (function () {
  10. var paste = (function (domGlobals) {
  11. 'use strict';
  12. var Cell = function (initial) {
  13. var value = initial;
  14. var get = function () {
  15. return value;
  16. };
  17. var set = function (v) {
  18. value = v;
  19. };
  20. var clone = function () {
  21. return Cell(get());
  22. };
  23. return {
  24. get: get,
  25. set: set,
  26. clone: clone
  27. };
  28. };
  29. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  30. var hasProPlugin = function (editor) {
  31. if (/(^|[ ,])powerpaste([, ]|$)/.test(editor.settings.plugins) && global.get('powerpaste')) {
  32. if (typeof domGlobals.window.console !== 'undefined' && domGlobals.window.console.log) {
  33. domGlobals.window.console.log('PowerPaste is incompatible with Paste plugin! Remove \'paste\' from the \'plugins\' option.');
  34. }
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. };
  40. var DetectProPlugin = { hasProPlugin: hasProPlugin };
  41. var get = function (clipboard, quirks) {
  42. return {
  43. clipboard: clipboard,
  44. quirks: quirks
  45. };
  46. };
  47. var Api = { get: get };
  48. var firePastePreProcess = function (editor, html, internal, isWordHtml) {
  49. return editor.fire('PastePreProcess', {
  50. content: html,
  51. internal: internal,
  52. wordContent: isWordHtml
  53. });
  54. };
  55. var firePastePostProcess = function (editor, node, internal, isWordHtml) {
  56. return editor.fire('PastePostProcess', {
  57. node: node,
  58. internal: internal,
  59. wordContent: isWordHtml
  60. });
  61. };
  62. var firePastePlainTextToggle = function (editor, state) {
  63. return editor.fire('PastePlainTextToggle', { state: state });
  64. };
  65. var firePaste = function (editor, ieFake) {
  66. return editor.fire('paste', { ieFake: ieFake });
  67. };
  68. var Events = {
  69. firePastePreProcess: firePastePreProcess,
  70. firePastePostProcess: firePastePostProcess,
  71. firePastePlainTextToggle: firePastePlainTextToggle,
  72. firePaste: firePaste
  73. };
  74. var togglePlainTextPaste = function (editor, clipboard) {
  75. if (clipboard.pasteFormat.get() === 'text') {
  76. clipboard.pasteFormat.set('html');
  77. Events.firePastePlainTextToggle(editor, false);
  78. } else {
  79. clipboard.pasteFormat.set('text');
  80. Events.firePastePlainTextToggle(editor, true);
  81. }
  82. editor.focus();
  83. };
  84. var Actions = { togglePlainTextPaste: togglePlainTextPaste };
  85. var register = function (editor, clipboard) {
  86. editor.addCommand('mceTogglePlainTextPaste', function () {
  87. Actions.togglePlainTextPaste(editor, clipboard);
  88. });
  89. editor.addCommand('mceInsertClipboardContent', function (ui, value) {
  90. if (value.content) {
  91. clipboard.pasteHtml(value.content, value.internal);
  92. }
  93. if (value.text) {
  94. clipboard.pasteText(value.text);
  95. }
  96. });
  97. };
  98. var Commands = { register: register };
  99. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  100. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  101. var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  102. var global$4 = tinymce.util.Tools.resolve('tinymce.util.VK');
  103. var internalMimeType = 'x-tinymce/html';
  104. var internalMark = '<!-- ' + internalMimeType + ' -->';
  105. var mark = function (html) {
  106. return internalMark + html;
  107. };
  108. var unmark = function (html) {
  109. return html.replace(internalMark, '');
  110. };
  111. var isMarked = function (html) {
  112. return html.indexOf(internalMark) !== -1;
  113. };
  114. var InternalHtml = {
  115. mark: mark,
  116. unmark: unmark,
  117. isMarked: isMarked,
  118. internalHtmlMime: function () {
  119. return internalMimeType;
  120. }
  121. };
  122. var global$5 = tinymce.util.Tools.resolve('tinymce.html.Entities');
  123. var isPlainText = function (text) {
  124. return !/<(?:\/?(?!(?:div|p|br|span)>)\w+|(?:(?!(?:span style="white-space:\s?pre;?">)|br\s?\/>))\w+\s[^>]+)>/i.test(text);
  125. };
  126. var toBRs = function (text) {
  127. return text.replace(/\r?\n/g, '<br>');
  128. };
  129. var openContainer = function (rootTag, rootAttrs) {
  130. var key;
  131. var attrs = [];
  132. var tag = '<' + rootTag;
  133. if (typeof rootAttrs === 'object') {
  134. for (key in rootAttrs) {
  135. if (rootAttrs.hasOwnProperty(key)) {
  136. attrs.push(key + '="' + global$5.encodeAllRaw(rootAttrs[key]) + '"');
  137. }
  138. }
  139. if (attrs.length) {
  140. tag += ' ' + attrs.join(' ');
  141. }
  142. }
  143. return tag + '>';
  144. };
  145. var toBlockElements = function (text, rootTag, rootAttrs) {
  146. var blocks = text.split(/\n\n/);
  147. var tagOpen = openContainer(rootTag, rootAttrs);
  148. var tagClose = '</' + rootTag + '>';
  149. var paragraphs = global$3.map(blocks, function (p) {
  150. return p.split(/\n/).join('<br />');
  151. });
  152. var stitch = function (p) {
  153. return tagOpen + p + tagClose;
  154. };
  155. return paragraphs.length === 1 ? paragraphs[0] : global$3.map(paragraphs, stitch).join('');
  156. };
  157. var convert = function (text, rootTag, rootAttrs) {
  158. return rootTag ? toBlockElements(text, rootTag, rootAttrs) : toBRs(text);
  159. };
  160. var Newlines = {
  161. isPlainText: isPlainText,
  162. convert: convert,
  163. toBRs: toBRs,
  164. toBlockElements: toBlockElements
  165. };
  166. var global$6 = tinymce.util.Tools.resolve('tinymce.html.DomParser');
  167. var global$7 = tinymce.util.Tools.resolve('tinymce.html.Node');
  168. var global$8 = tinymce.util.Tools.resolve('tinymce.html.Schema');
  169. var global$9 = tinymce.util.Tools.resolve('tinymce.html.Serializer');
  170. var shouldBlockDrop = function (editor) {
  171. return editor.getParam('paste_block_drop', false);
  172. };
  173. var shouldPasteDataImages = function (editor) {
  174. return editor.getParam('paste_data_images', false);
  175. };
  176. var shouldFilterDrop = function (editor) {
  177. return editor.getParam('paste_filter_drop', true);
  178. };
  179. var getPreProcess = function (editor) {
  180. return editor.getParam('paste_preprocess');
  181. };
  182. var getPostProcess = function (editor) {
  183. return editor.getParam('paste_postprocess');
  184. };
  185. var getWebkitStyles = function (editor) {
  186. return editor.getParam('paste_webkit_styles');
  187. };
  188. var shouldRemoveWebKitStyles = function (editor) {
  189. return editor.getParam('paste_remove_styles_if_webkit', true);
  190. };
  191. var shouldMergeFormats = function (editor) {
  192. return editor.getParam('paste_merge_formats', true);
  193. };
  194. var isSmartPasteEnabled = function (editor) {
  195. return editor.getParam('smart_paste', true);
  196. };
  197. var isPasteAsTextEnabled = function (editor) {
  198. return editor.getParam('paste_as_text', false);
  199. };
  200. var getRetainStyleProps = function (editor) {
  201. return editor.getParam('paste_retain_style_properties');
  202. };
  203. var getWordValidElements = function (editor) {
  204. var defaultValidElements = '-strong/b,-em/i,-u,-span,-p,-ol,-ul,-li,-h1,-h2,-h3,-h4,-h5,-h6,' + '-p/div,-a[href|name],sub,sup,strike,br,del,table[width],tr,' + 'td[colspan|rowspan|width],th[colspan|rowspan|width],thead,tfoot,tbody';
  205. return editor.getParam('paste_word_valid_elements', defaultValidElements);
  206. };
  207. var shouldConvertWordFakeLists = function (editor) {
  208. return editor.getParam('paste_convert_word_fake_lists', true);
  209. };
  210. var shouldUseDefaultFilters = function (editor) {
  211. return editor.getParam('paste_enable_default_filters', true);
  212. };
  213. var Settings = {
  214. shouldBlockDrop: shouldBlockDrop,
  215. shouldPasteDataImages: shouldPasteDataImages,
  216. shouldFilterDrop: shouldFilterDrop,
  217. getPreProcess: getPreProcess,
  218. getPostProcess: getPostProcess,
  219. getWebkitStyles: getWebkitStyles,
  220. shouldRemoveWebKitStyles: shouldRemoveWebKitStyles,
  221. shouldMergeFormats: shouldMergeFormats,
  222. isSmartPasteEnabled: isSmartPasteEnabled,
  223. isPasteAsTextEnabled: isPasteAsTextEnabled,
  224. getRetainStyleProps: getRetainStyleProps,
  225. getWordValidElements: getWordValidElements,
  226. shouldConvertWordFakeLists: shouldConvertWordFakeLists,
  227. shouldUseDefaultFilters: shouldUseDefaultFilters
  228. };
  229. function filter(content, items) {
  230. global$3.each(items, function (v) {
  231. if (v.constructor === RegExp) {
  232. content = content.replace(v, '');
  233. } else {
  234. content = content.replace(v[0], v[1]);
  235. }
  236. });
  237. return content;
  238. }
  239. function innerText(html) {
  240. var schema = global$8();
  241. var domParser = global$6({}, schema);
  242. var text = '';
  243. var shortEndedElements = schema.getShortEndedElements();
  244. var ignoreElements = global$3.makeMap('script noscript style textarea video audio iframe object', ' ');
  245. var blockElements = schema.getBlockElements();
  246. function walk(node) {
  247. var name = node.name, currentNode = node;
  248. if (name === 'br') {
  249. text += '\n';
  250. return;
  251. }
  252. if (name === 'wbr') {
  253. return;
  254. }
  255. if (shortEndedElements[name]) {
  256. text += ' ';
  257. }
  258. if (ignoreElements[name]) {
  259. text += ' ';
  260. return;
  261. }
  262. if (node.type === 3) {
  263. text += node.value;
  264. }
  265. if (!node.shortEnded) {
  266. if (node = node.firstChild) {
  267. do {
  268. walk(node);
  269. } while (node = node.next);
  270. }
  271. }
  272. if (blockElements[name] && currentNode.next) {
  273. text += '\n';
  274. if (name === 'p') {
  275. text += '\n';
  276. }
  277. }
  278. }
  279. html = filter(html, [/<!\[[^\]]+\]>/g]);
  280. walk(domParser.parse(html));
  281. return text;
  282. }
  283. function trimHtml(html) {
  284. function trimSpaces(all, s1, s2) {
  285. if (!s1 && !s2) {
  286. return ' ';
  287. }
  288. return '\xA0';
  289. }
  290. html = filter(html, [
  291. /^[\s\S]*<body[^>]*>\s*|\s*<\/body[^>]*>[\s\S]*$/ig,
  292. /<!--StartFragment-->|<!--EndFragment-->/g,
  293. [
  294. /( ?)<span class="Apple-converted-space">\u00a0<\/span>( ?)/g,
  295. trimSpaces
  296. ],
  297. /<br class="Apple-interchange-newline">/g,
  298. /<br>$/i
  299. ]);
  300. return html;
  301. }
  302. function createIdGenerator(prefix) {
  303. var count = 0;
  304. return function () {
  305. return prefix + count++;
  306. };
  307. }
  308. var isMsEdge = function () {
  309. return domGlobals.navigator.userAgent.indexOf(' Edge/') !== -1;
  310. };
  311. var Utils = {
  312. filter: filter,
  313. innerText: innerText,
  314. trimHtml: trimHtml,
  315. createIdGenerator: createIdGenerator,
  316. isMsEdge: isMsEdge
  317. };
  318. function isWordContent(content) {
  319. return /<font face="Times New Roman"|class="?Mso|style="[^"]*\bmso-|style='[^'']*\bmso-|w:WordDocument/i.test(content) || /class="OutlineElement/.test(content) || /id="?docs\-internal\-guid\-/.test(content);
  320. }
  321. function isNumericList(text) {
  322. var found, patterns;
  323. patterns = [
  324. /^[IVXLMCD]{1,2}\.[ \u00a0]/,
  325. /^[ivxlmcd]{1,2}\.[ \u00a0]/,
  326. /^[a-z]{1,2}[\.\)][ \u00a0]/,
  327. /^[A-Z]{1,2}[\.\)][ \u00a0]/,
  328. /^[0-9]+\.[ \u00a0]/,
  329. /^[\u3007\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d]+\.[ \u00a0]/,
  330. /^[\u58f1\u5f10\u53c2\u56db\u4f0d\u516d\u4e03\u516b\u4e5d\u62fe]+\.[ \u00a0]/
  331. ];
  332. text = text.replace(/^[\u00a0 ]+/, '');
  333. global$3.each(patterns, function (pattern) {
  334. if (pattern.test(text)) {
  335. found = true;
  336. return false;
  337. }
  338. });
  339. return found;
  340. }
  341. function isBulletList(text) {
  342. return /^[\s\u00a0]*[\u2022\u00b7\u00a7\u25CF]\s*/.test(text);
  343. }
  344. function convertFakeListsToProperLists(node) {
  345. var currentListNode, prevListNode, lastLevel = 1;
  346. function getText(node) {
  347. var txt = '';
  348. if (node.type === 3) {
  349. return node.value;
  350. }
  351. if (node = node.firstChild) {
  352. do {
  353. txt += getText(node);
  354. } while (node = node.next);
  355. }
  356. return txt;
  357. }
  358. function trimListStart(node, regExp) {
  359. if (node.type === 3) {
  360. if (regExp.test(node.value)) {
  361. node.value = node.value.replace(regExp, '');
  362. return false;
  363. }
  364. }
  365. if (node = node.firstChild) {
  366. do {
  367. if (!trimListStart(node, regExp)) {
  368. return false;
  369. }
  370. } while (node = node.next);
  371. }
  372. return true;
  373. }
  374. function removeIgnoredNodes(node) {
  375. if (node._listIgnore) {
  376. node.remove();
  377. return;
  378. }
  379. if (node = node.firstChild) {
  380. do {
  381. removeIgnoredNodes(node);
  382. } while (node = node.next);
  383. }
  384. }
  385. function convertParagraphToLi(paragraphNode, listName, start) {
  386. var level = paragraphNode._listLevel || lastLevel;
  387. if (level !== lastLevel) {
  388. if (level < lastLevel) {
  389. if (currentListNode) {
  390. currentListNode = currentListNode.parent.parent;
  391. }
  392. } else {
  393. prevListNode = currentListNode;
  394. currentListNode = null;
  395. }
  396. }
  397. if (!currentListNode || currentListNode.name !== listName) {
  398. prevListNode = prevListNode || currentListNode;
  399. currentListNode = new global$7(listName, 1);
  400. if (start > 1) {
  401. currentListNode.attr('start', '' + start);
  402. }
  403. paragraphNode.wrap(currentListNode);
  404. } else {
  405. currentListNode.append(paragraphNode);
  406. }
  407. paragraphNode.name = 'li';
  408. if (level > lastLevel && prevListNode) {
  409. prevListNode.lastChild.append(currentListNode);
  410. }
  411. lastLevel = level;
  412. removeIgnoredNodes(paragraphNode);
  413. trimListStart(paragraphNode, /^\u00a0+/);
  414. trimListStart(paragraphNode, /^\s*([\u2022\u00b7\u00a7\u25CF]|\w+\.)/);
  415. trimListStart(paragraphNode, /^\u00a0+/);
  416. }
  417. var elements = [];
  418. var child = node.firstChild;
  419. while (typeof child !== 'undefined' && child !== null) {
  420. elements.push(child);
  421. child = child.walk();
  422. if (child !== null) {
  423. while (typeof child !== 'undefined' && child.parent !== node) {
  424. child = child.walk();
  425. }
  426. }
  427. }
  428. for (var i = 0; i < elements.length; i++) {
  429. node = elements[i];
  430. if (node.name === 'p' && node.firstChild) {
  431. var nodeText = getText(node);
  432. if (isBulletList(nodeText)) {
  433. convertParagraphToLi(node, 'ul');
  434. continue;
  435. }
  436. if (isNumericList(nodeText)) {
  437. var matches = /([0-9]+)\./.exec(nodeText);
  438. var start = 1;
  439. if (matches) {
  440. start = parseInt(matches[1], 10);
  441. }
  442. convertParagraphToLi(node, 'ol', start);
  443. continue;
  444. }
  445. if (node._listLevel) {
  446. convertParagraphToLi(node, 'ul', 1);
  447. continue;
  448. }
  449. currentListNode = null;
  450. } else {
  451. prevListNode = currentListNode;
  452. currentListNode = null;
  453. }
  454. }
  455. }
  456. function filterStyles(editor, validStyles, node, styleValue) {
  457. var outputStyles = {}, matches;
  458. var styles = editor.dom.parseStyle(styleValue);
  459. global$3.each(styles, function (value, name) {
  460. switch (name) {
  461. case 'mso-list':
  462. matches = /\w+ \w+([0-9]+)/i.exec(styleValue);
  463. if (matches) {
  464. node._listLevel = parseInt(matches[1], 10);
  465. }
  466. if (/Ignore/i.test(value) && node.firstChild) {
  467. node._listIgnore = true;
  468. node.firstChild._listIgnore = true;
  469. }
  470. break;
  471. case 'horiz-align':
  472. name = 'text-align';
  473. break;
  474. case 'vert-align':
  475. name = 'vertical-align';
  476. break;
  477. case 'font-color':
  478. case 'mso-foreground':
  479. name = 'color';
  480. break;
  481. case 'mso-background':
  482. case 'mso-highlight':
  483. name = 'background';
  484. break;
  485. case 'font-weight':
  486. case 'font-style':
  487. if (value !== 'normal') {
  488. outputStyles[name] = value;
  489. }
  490. return;
  491. case 'mso-element':
  492. if (/^(comment|comment-list)$/i.test(value)) {
  493. node.remove();
  494. return;
  495. }
  496. break;
  497. }
  498. if (name.indexOf('mso-comment') === 0) {
  499. node.remove();
  500. return;
  501. }
  502. if (name.indexOf('mso-') === 0) {
  503. return;
  504. }
  505. if (Settings.getRetainStyleProps(editor) === 'all' || validStyles && validStyles[name]) {
  506. outputStyles[name] = value;
  507. }
  508. });
  509. if (/(bold)/i.test(outputStyles['font-weight'])) {
  510. delete outputStyles['font-weight'];
  511. node.wrap(new global$7('b', 1));
  512. }
  513. if (/(italic)/i.test(outputStyles['font-style'])) {
  514. delete outputStyles['font-style'];
  515. node.wrap(new global$7('i', 1));
  516. }
  517. outputStyles = editor.dom.serializeStyle(outputStyles, node.name);
  518. if (outputStyles) {
  519. return outputStyles;
  520. }
  521. return null;
  522. }
  523. var filterWordContent = function (editor, content) {
  524. var retainStyleProperties, validStyles;
  525. retainStyleProperties = Settings.getRetainStyleProps(editor);
  526. if (retainStyleProperties) {
  527. validStyles = global$3.makeMap(retainStyleProperties.split(/[, ]/));
  528. }
  529. content = Utils.filter(content, [
  530. /<br class="?Apple-interchange-newline"?>/gi,
  531. /<b[^>]+id="?docs-internal-[^>]*>/gi,
  532. /<!--[\s\S]+?-->/gi,
  533. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|img|meta|link|style|\w:\w+)(?=[\s\/>]))[^>]*>/gi,
  534. [
  535. /<(\/?)s>/gi,
  536. '<$1strike>'
  537. ],
  538. [
  539. /&nbsp;/gi,
  540. '\xA0'
  541. ],
  542. [
  543. /<span\s+style\s*=\s*"\s*mso-spacerun\s*:\s*yes\s*;?\s*"\s*>([\s\u00a0]*)<\/span>/gi,
  544. function (str, spaces) {
  545. return spaces.length > 0 ? spaces.replace(/./, ' ').slice(Math.floor(spaces.length / 2)).split('').join('\xA0') : '';
  546. }
  547. ]
  548. ]);
  549. var validElements = Settings.getWordValidElements(editor);
  550. var schema = global$8({
  551. valid_elements: validElements,
  552. valid_children: '-li[p]'
  553. });
  554. global$3.each(schema.elements, function (rule) {
  555. if (!rule.attributes.class) {
  556. rule.attributes.class = {};
  557. rule.attributesOrder.push('class');
  558. }
  559. if (!rule.attributes.style) {
  560. rule.attributes.style = {};
  561. rule.attributesOrder.push('style');
  562. }
  563. });
  564. var domParser = global$6({}, schema);
  565. domParser.addAttributeFilter('style', function (nodes) {
  566. var i = nodes.length, node;
  567. while (i--) {
  568. node = nodes[i];
  569. node.attr('style', filterStyles(editor, validStyles, node, node.attr('style')));
  570. if (node.name === 'span' && node.parent && !node.attributes.length) {
  571. node.unwrap();
  572. }
  573. }
  574. });
  575. domParser.addAttributeFilter('class', function (nodes) {
  576. var i = nodes.length, node, className;
  577. while (i--) {
  578. node = nodes[i];
  579. className = node.attr('class');
  580. if (/^(MsoCommentReference|MsoCommentText|msoDel)$/i.test(className)) {
  581. node.remove();
  582. }
  583. node.attr('class', null);
  584. }
  585. });
  586. domParser.addNodeFilter('del', function (nodes) {
  587. var i = nodes.length;
  588. while (i--) {
  589. nodes[i].remove();
  590. }
  591. });
  592. domParser.addNodeFilter('a', function (nodes) {
  593. var i = nodes.length, node, href, name;
  594. while (i--) {
  595. node = nodes[i];
  596. href = node.attr('href');
  597. name = node.attr('name');
  598. if (href && href.indexOf('#_msocom_') !== -1) {
  599. node.remove();
  600. continue;
  601. }
  602. if (href && href.indexOf('file://') === 0) {
  603. href = href.split('#')[1];
  604. if (href) {
  605. href = '#' + href;
  606. }
  607. }
  608. if (!href && !name) {
  609. node.unwrap();
  610. } else {
  611. if (name && !/^_?(?:toc|edn|ftn)/i.test(name)) {
  612. node.unwrap();
  613. continue;
  614. }
  615. node.attr({
  616. href: href,
  617. name: name
  618. });
  619. }
  620. }
  621. });
  622. var rootNode = domParser.parse(content);
  623. if (Settings.shouldConvertWordFakeLists(editor)) {
  624. convertFakeListsToProperLists(rootNode);
  625. }
  626. content = global$9({ validate: editor.settings.validate }, schema).serialize(rootNode);
  627. return content;
  628. };
  629. var preProcess = function (editor, content) {
  630. return Settings.shouldUseDefaultFilters(editor) ? filterWordContent(editor, content) : content;
  631. };
  632. var WordFilter = {
  633. preProcess: preProcess,
  634. isWordContent: isWordContent
  635. };
  636. var processResult = function (content, cancelled) {
  637. return {
  638. content: content,
  639. cancelled: cancelled
  640. };
  641. };
  642. var postProcessFilter = function (editor, html, internal, isWordHtml) {
  643. var tempBody = editor.dom.create('div', { style: 'display:none' }, html);
  644. var postProcessArgs = Events.firePastePostProcess(editor, tempBody, internal, isWordHtml);
  645. return processResult(postProcessArgs.node.innerHTML, postProcessArgs.isDefaultPrevented());
  646. };
  647. var filterContent = function (editor, content, internal, isWordHtml) {
  648. var preProcessArgs = Events.firePastePreProcess(editor, content, internal, isWordHtml);
  649. if (editor.hasEventListeners('PastePostProcess') && !preProcessArgs.isDefaultPrevented()) {
  650. return postProcessFilter(editor, preProcessArgs.content, internal, isWordHtml);
  651. } else {
  652. return processResult(preProcessArgs.content, preProcessArgs.isDefaultPrevented());
  653. }
  654. };
  655. var process = function (editor, html, internal) {
  656. var isWordHtml = WordFilter.isWordContent(html);
  657. var content = isWordHtml ? WordFilter.preProcess(editor, html) : html;
  658. return filterContent(editor, content, internal, isWordHtml);
  659. };
  660. var ProcessFilters = { process: process };
  661. var removeMeta = function (editor, html) {
  662. var body = editor.dom.create('body', {}, html);
  663. global$3.each(body.querySelectorAll('meta'), function (elm) {
  664. return elm.parentNode.removeChild(elm);
  665. });
  666. return body.innerHTML;
  667. };
  668. var pasteHtml = function (editor, html) {
  669. editor.insertContent(removeMeta(editor, html), {
  670. merge: Settings.shouldMergeFormats(editor),
  671. paste: true
  672. });
  673. return true;
  674. };
  675. var isAbsoluteUrl = function (url) {
  676. return /^https?:\/\/[\w\?\-\/+=.&%@~#]+$/i.test(url);
  677. };
  678. var isImageUrl = function (url) {
  679. return isAbsoluteUrl(url) && /.(gif|jpe?g|png)$/.test(url);
  680. };
  681. var createImage = function (editor, url, pasteHtmlFn) {
  682. editor.undoManager.extra(function () {
  683. pasteHtmlFn(editor, url);
  684. }, function () {
  685. editor.insertContent('<img src="' + url + '">');
  686. });
  687. return true;
  688. };
  689. var createLink = function (editor, url, pasteHtmlFn) {
  690. editor.undoManager.extra(function () {
  691. pasteHtmlFn(editor, url);
  692. }, function () {
  693. editor.execCommand('mceInsertLink', false, url);
  694. });
  695. return true;
  696. };
  697. var linkSelection = function (editor, html, pasteHtmlFn) {
  698. return editor.selection.isCollapsed() === false && isAbsoluteUrl(html) ? createLink(editor, html, pasteHtmlFn) : false;
  699. };
  700. var insertImage = function (editor, html, pasteHtmlFn) {
  701. return isImageUrl(html) ? createImage(editor, html, pasteHtmlFn) : false;
  702. };
  703. var smartInsertContent = function (editor, html) {
  704. global$3.each([
  705. linkSelection,
  706. insertImage,
  707. pasteHtml
  708. ], function (action) {
  709. return action(editor, html, pasteHtml) !== true;
  710. });
  711. };
  712. var insertContent = function (editor, html) {
  713. if (Settings.isSmartPasteEnabled(editor) === false) {
  714. pasteHtml(editor, html);
  715. } else {
  716. smartInsertContent(editor, html);
  717. }
  718. };
  719. var SmartPaste = {
  720. isImageUrl: isImageUrl,
  721. isAbsoluteUrl: isAbsoluteUrl,
  722. insertContent: insertContent
  723. };
  724. var constant = function (value) {
  725. return function () {
  726. return value;
  727. };
  728. };
  729. var never = constant(false);
  730. var always = constant(true);
  731. var never$1 = never;
  732. var always$1 = always;
  733. var none = function () {
  734. return NONE;
  735. };
  736. var NONE = function () {
  737. var eq = function (o) {
  738. return o.isNone();
  739. };
  740. var call = function (thunk) {
  741. return thunk();
  742. };
  743. var id = function (n) {
  744. return n;
  745. };
  746. var noop = function () {
  747. };
  748. var nul = function () {
  749. return null;
  750. };
  751. var undef = function () {
  752. return undefined;
  753. };
  754. var me = {
  755. fold: function (n, s) {
  756. return n();
  757. },
  758. is: never$1,
  759. isSome: never$1,
  760. isNone: always$1,
  761. getOr: id,
  762. getOrThunk: call,
  763. getOrDie: function (msg) {
  764. throw new Error(msg || 'error: getOrDie called on none.');
  765. },
  766. getOrNull: nul,
  767. getOrUndefined: undef,
  768. or: id,
  769. orThunk: call,
  770. map: none,
  771. ap: none,
  772. each: noop,
  773. bind: none,
  774. flatten: none,
  775. exists: never$1,
  776. forall: always$1,
  777. filter: none,
  778. equals: eq,
  779. equals_: eq,
  780. toArray: function () {
  781. return [];
  782. },
  783. toString: constant('none()')
  784. };
  785. if (Object.freeze)
  786. Object.freeze(me);
  787. return me;
  788. }();
  789. var some = function (a) {
  790. var constant_a = function () {
  791. return a;
  792. };
  793. var self = function () {
  794. return me;
  795. };
  796. var map = function (f) {
  797. return some(f(a));
  798. };
  799. var bind = function (f) {
  800. return f(a);
  801. };
  802. var me = {
  803. fold: function (n, s) {
  804. return s(a);
  805. },
  806. is: function (v) {
  807. return a === v;
  808. },
  809. isSome: always$1,
  810. isNone: never$1,
  811. getOr: constant_a,
  812. getOrThunk: constant_a,
  813. getOrDie: constant_a,
  814. getOrNull: constant_a,
  815. getOrUndefined: constant_a,
  816. or: self,
  817. orThunk: self,
  818. map: map,
  819. ap: function (optfab) {
  820. return optfab.fold(none, function (fab) {
  821. return some(fab(a));
  822. });
  823. },
  824. each: function (f) {
  825. f(a);
  826. },
  827. bind: bind,
  828. flatten: constant_a,
  829. exists: bind,
  830. forall: bind,
  831. filter: function (f) {
  832. return f(a) ? me : NONE;
  833. },
  834. equals: function (o) {
  835. return o.is(a);
  836. },
  837. equals_: function (o, elementEq) {
  838. return o.fold(never$1, function (b) {
  839. return elementEq(a, b);
  840. });
  841. },
  842. toArray: function () {
  843. return [a];
  844. },
  845. toString: function () {
  846. return 'some(' + a + ')';
  847. }
  848. };
  849. return me;
  850. };
  851. var from = function (value) {
  852. return value === null || value === undefined ? NONE : some(value);
  853. };
  854. var Option = {
  855. some: some,
  856. none: none,
  857. from: from
  858. };
  859. var typeOf = function (x) {
  860. if (x === null)
  861. return 'null';
  862. var t = typeof x;
  863. if (t === 'object' && Array.prototype.isPrototypeOf(x))
  864. return 'array';
  865. if (t === 'object' && String.prototype.isPrototypeOf(x))
  866. return 'string';
  867. return t;
  868. };
  869. var isType = function (type) {
  870. return function (value) {
  871. return typeOf(value) === type;
  872. };
  873. };
  874. var isFunction = isType('function');
  875. var map = function (xs, f) {
  876. var len = xs.length;
  877. var r = new Array(len);
  878. for (var i = 0; i < len; i++) {
  879. var x = xs[i];
  880. r[i] = f(x, i, xs);
  881. }
  882. return r;
  883. };
  884. var each = function (xs, f) {
  885. for (var i = 0, len = xs.length; i < len; i++) {
  886. var x = xs[i];
  887. f(x, i, xs);
  888. }
  889. };
  890. var filter$1 = function (xs, pred) {
  891. var r = [];
  892. for (var i = 0, len = xs.length; i < len; i++) {
  893. var x = xs[i];
  894. if (pred(x, i, xs)) {
  895. r.push(x);
  896. }
  897. }
  898. return r;
  899. };
  900. var slice = Array.prototype.slice;
  901. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  902. return slice.call(x);
  903. };
  904. var nu = function (baseFn) {
  905. var data = Option.none();
  906. var callbacks = [];
  907. var map = function (f) {
  908. return nu(function (nCallback) {
  909. get(function (data) {
  910. nCallback(f(data));
  911. });
  912. });
  913. };
  914. var get = function (nCallback) {
  915. if (isReady())
  916. call(nCallback);
  917. else
  918. callbacks.push(nCallback);
  919. };
  920. var set = function (x) {
  921. data = Option.some(x);
  922. run(callbacks);
  923. callbacks = [];
  924. };
  925. var isReady = function () {
  926. return data.isSome();
  927. };
  928. var run = function (cbs) {
  929. each(cbs, call);
  930. };
  931. var call = function (cb) {
  932. data.each(function (x) {
  933. setTimeout(function () {
  934. cb(x);
  935. }, 0);
  936. });
  937. };
  938. baseFn(set);
  939. return {
  940. get: get,
  941. map: map,
  942. isReady: isReady
  943. };
  944. };
  945. var pure = function (a) {
  946. return nu(function (callback) {
  947. callback(a);
  948. });
  949. };
  950. var LazyValue = {
  951. nu: nu,
  952. pure: pure
  953. };
  954. var bounce = function (f) {
  955. return function () {
  956. var args = [];
  957. for (var _i = 0; _i < arguments.length; _i++) {
  958. args[_i] = arguments[_i];
  959. }
  960. var me = this;
  961. setTimeout(function () {
  962. f.apply(me, args);
  963. }, 0);
  964. };
  965. };
  966. var nu$1 = function (baseFn) {
  967. var get = function (callback) {
  968. baseFn(bounce(callback));
  969. };
  970. var map = function (fab) {
  971. return nu$1(function (callback) {
  972. get(function (a) {
  973. var value = fab(a);
  974. callback(value);
  975. });
  976. });
  977. };
  978. var bind = function (aFutureB) {
  979. return nu$1(function (callback) {
  980. get(function (a) {
  981. aFutureB(a).get(callback);
  982. });
  983. });
  984. };
  985. var anonBind = function (futureB) {
  986. return nu$1(function (callback) {
  987. get(function (a) {
  988. futureB.get(callback);
  989. });
  990. });
  991. };
  992. var toLazy = function () {
  993. return LazyValue.nu(get);
  994. };
  995. var toCached = function () {
  996. var cache = null;
  997. return nu$1(function (callback) {
  998. if (cache === null) {
  999. cache = toLazy();
  1000. }
  1001. cache.get(callback);
  1002. });
  1003. };
  1004. return {
  1005. map: map,
  1006. bind: bind,
  1007. anonBind: anonBind,
  1008. toLazy: toLazy,
  1009. toCached: toCached,
  1010. get: get
  1011. };
  1012. };
  1013. var pure$1 = function (a) {
  1014. return nu$1(function (callback) {
  1015. callback(a);
  1016. });
  1017. };
  1018. var Future = {
  1019. nu: nu$1,
  1020. pure: pure$1
  1021. };
  1022. var par = function (asyncValues, nu) {
  1023. return nu(function (callback) {
  1024. var r = [];
  1025. var count = 0;
  1026. var cb = function (i) {
  1027. return function (value) {
  1028. r[i] = value;
  1029. count++;
  1030. if (count >= asyncValues.length) {
  1031. callback(r);
  1032. }
  1033. };
  1034. };
  1035. if (asyncValues.length === 0) {
  1036. callback([]);
  1037. } else {
  1038. each(asyncValues, function (asyncValue, i) {
  1039. asyncValue.get(cb(i));
  1040. });
  1041. }
  1042. });
  1043. };
  1044. var par$1 = function (futures) {
  1045. return par(futures, Future.nu);
  1046. };
  1047. var mapM = function (array, fn) {
  1048. var futures = map(array, fn);
  1049. return par$1(futures);
  1050. };
  1051. var pasteHtml$1 = function (editor, html, internalFlag) {
  1052. var internal = internalFlag ? internalFlag : InternalHtml.isMarked(html);
  1053. var args = ProcessFilters.process(editor, InternalHtml.unmark(html), internal);
  1054. if (args.cancelled === false) {
  1055. SmartPaste.insertContent(editor, args.content);
  1056. }
  1057. };
  1058. var pasteText = function (editor, text) {
  1059. text = editor.dom.encode(text).replace(/\r\n/g, '\n');
  1060. text = Newlines.convert(text, editor.settings.forced_root_block, editor.settings.forced_root_block_attrs);
  1061. pasteHtml$1(editor, text, false);
  1062. };
  1063. var getDataTransferItems = function (dataTransfer) {
  1064. var items = {};
  1065. var mceInternalUrlPrefix = 'data:text/mce-internal,';
  1066. if (dataTransfer) {
  1067. if (dataTransfer.getData) {
  1068. var legacyText = dataTransfer.getData('Text');
  1069. if (legacyText && legacyText.length > 0) {
  1070. if (legacyText.indexOf(mceInternalUrlPrefix) === -1) {
  1071. items['text/plain'] = legacyText;
  1072. }
  1073. }
  1074. }
  1075. if (dataTransfer.types) {
  1076. for (var i = 0; i < dataTransfer.types.length; i++) {
  1077. var contentType = dataTransfer.types[i];
  1078. try {
  1079. items[contentType] = dataTransfer.getData(contentType);
  1080. } catch (ex) {
  1081. items[contentType] = '';
  1082. }
  1083. }
  1084. }
  1085. }
  1086. return items;
  1087. };
  1088. var getClipboardContent = function (editor, clipboardEvent) {
  1089. var content = getDataTransferItems(clipboardEvent.clipboardData || editor.getDoc().dataTransfer);
  1090. return Utils.isMsEdge() ? global$3.extend(content, { 'text/html': '' }) : content;
  1091. };
  1092. var hasContentType = function (clipboardContent, mimeType) {
  1093. return mimeType in clipboardContent && clipboardContent[mimeType].length > 0;
  1094. };
  1095. var hasHtmlOrText = function (content) {
  1096. return hasContentType(content, 'text/html') || hasContentType(content, 'text/plain');
  1097. };
  1098. var getBase64FromUri = function (uri) {
  1099. var idx;
  1100. idx = uri.indexOf(',');
  1101. if (idx !== -1) {
  1102. return uri.substr(idx + 1);
  1103. }
  1104. return null;
  1105. };
  1106. var isValidDataUriImage = function (settings, imgElm) {
  1107. return settings.images_dataimg_filter ? settings.images_dataimg_filter(imgElm) : true;
  1108. };
  1109. var extractFilename = function (editor, str) {
  1110. var m = str.match(/([\s\S]+?)\.(?:jpeg|jpg|png|gif)$/i);
  1111. return m ? editor.dom.encode(m[1]) : null;
  1112. };
  1113. var uniqueId = Utils.createIdGenerator('mceclip');
  1114. var pasteImage = function (editor, imageItem) {
  1115. var base64 = getBase64FromUri(imageItem.uri);
  1116. var id = uniqueId();
  1117. var name = editor.settings.images_reuse_filename && imageItem.blob.name ? extractFilename(editor, imageItem.blob.name) : id;
  1118. var img = new domGlobals.Image();
  1119. img.src = imageItem.uri;
  1120. if (isValidDataUriImage(editor.settings, img)) {
  1121. var blobCache = editor.editorUpload.blobCache;
  1122. var blobInfo = void 0, existingBlobInfo = void 0;
  1123. existingBlobInfo = blobCache.findFirst(function (cachedBlobInfo) {
  1124. return cachedBlobInfo.base64() === base64;
  1125. });
  1126. if (!existingBlobInfo) {
  1127. blobInfo = blobCache.create(id, imageItem.blob, base64, name);
  1128. blobCache.add(blobInfo);
  1129. } else {
  1130. blobInfo = existingBlobInfo;
  1131. }
  1132. pasteHtml$1(editor, '<img src="' + blobInfo.blobUri() + '">', false);
  1133. } else {
  1134. pasteHtml$1(editor, '<img src="' + imageItem.uri + '">', false);
  1135. }
  1136. };
  1137. var isClipboardEvent = function (event) {
  1138. return event.type === 'paste';
  1139. };
  1140. var readBlobsAsDataUris = function (items) {
  1141. return mapM(items, function (item) {
  1142. return Future.nu(function (resolve) {
  1143. var blob = item.getAsFile ? item.getAsFile() : item;
  1144. var reader = new window.FileReader();
  1145. reader.onload = function () {
  1146. resolve({
  1147. blob: blob,
  1148. uri: reader.result
  1149. });
  1150. };
  1151. reader.readAsDataURL(blob);
  1152. });
  1153. });
  1154. };
  1155. var getImagesFromDataTransfer = function (dataTransfer) {
  1156. var items = dataTransfer.items ? map(from$1(dataTransfer.items), function (item) {
  1157. return item.getAsFile();
  1158. }) : [];
  1159. var files = dataTransfer.files ? from$1(dataTransfer.files) : [];
  1160. var images = filter$1(items.length > 0 ? items : files, function (file) {
  1161. return /^image\/(jpeg|png|gif|bmp)$/.test(file.type);
  1162. });
  1163. return images;
  1164. };
  1165. var pasteImageData = function (editor, e, rng) {
  1166. var dataTransfer = isClipboardEvent(e) ? e.clipboardData : e.dataTransfer;
  1167. if (editor.settings.paste_data_images && dataTransfer) {
  1168. var images = getImagesFromDataTransfer(dataTransfer);
  1169. if (images.length > 0) {
  1170. e.preventDefault();
  1171. readBlobsAsDataUris(images).get(function (blobResults) {
  1172. if (rng) {
  1173. editor.selection.setRng(rng);
  1174. }
  1175. each(blobResults, function (result) {
  1176. pasteImage(editor, result);
  1177. });
  1178. });
  1179. return true;
  1180. }
  1181. }
  1182. return false;
  1183. };
  1184. var isBrokenAndroidClipboardEvent = function (e) {
  1185. var clipboardData = e.clipboardData;
  1186. return domGlobals.navigator.userAgent.indexOf('Android') !== -1 && clipboardData && clipboardData.items && clipboardData.items.length === 0;
  1187. };
  1188. var isKeyboardPasteEvent = function (e) {
  1189. return global$4.metaKeyPressed(e) && e.keyCode === 86 || e.shiftKey && e.keyCode === 45;
  1190. };
  1191. var registerEventHandlers = function (editor, pasteBin, pasteFormat) {
  1192. var keyboardPasteTimeStamp = 0;
  1193. var keyboardPastePlainTextState;
  1194. editor.on('keydown', function (e) {
  1195. function removePasteBinOnKeyUp(e) {
  1196. if (isKeyboardPasteEvent(e) && !e.isDefaultPrevented()) {
  1197. pasteBin.remove();
  1198. }
  1199. }
  1200. if (isKeyboardPasteEvent(e) && !e.isDefaultPrevented()) {
  1201. keyboardPastePlainTextState = e.shiftKey && e.keyCode === 86;
  1202. if (keyboardPastePlainTextState && global$1.webkit && domGlobals.navigator.userAgent.indexOf('Version/') !== -1) {
  1203. return;
  1204. }
  1205. e.stopImmediatePropagation();
  1206. keyboardPasteTimeStamp = new Date().getTime();
  1207. if (global$1.ie && keyboardPastePlainTextState) {
  1208. e.preventDefault();
  1209. Events.firePaste(editor, true);
  1210. return;
  1211. }
  1212. pasteBin.remove();
  1213. pasteBin.create();
  1214. editor.once('keyup', removePasteBinOnKeyUp);
  1215. editor.once('paste', function () {
  1216. editor.off('keyup', removePasteBinOnKeyUp);
  1217. });
  1218. }
  1219. });
  1220. function insertClipboardContent(clipboardContent, isKeyBoardPaste, plainTextMode, internal) {
  1221. var content, isPlainTextHtml;
  1222. if (hasContentType(clipboardContent, 'text/html')) {
  1223. content = clipboardContent['text/html'];
  1224. } else {
  1225. content = pasteBin.getHtml();
  1226. internal = internal ? internal : InternalHtml.isMarked(content);
  1227. if (pasteBin.isDefaultContent(content)) {
  1228. plainTextMode = true;
  1229. }
  1230. }
  1231. content = Utils.trimHtml(content);
  1232. pasteBin.remove();
  1233. isPlainTextHtml = internal === false && Newlines.isPlainText(content);
  1234. if (!content.length || isPlainTextHtml) {
  1235. plainTextMode = true;
  1236. }
  1237. if (plainTextMode) {
  1238. if (hasContentType(clipboardContent, 'text/plain') && isPlainTextHtml) {
  1239. content = clipboardContent['text/plain'];
  1240. } else {
  1241. content = Utils.innerText(content);
  1242. }
  1243. }
  1244. if (pasteBin.isDefaultContent(content)) {
  1245. if (!isKeyBoardPaste) {
  1246. editor.windowManager.alert('Please use Ctrl+V/Cmd+V keyboard shortcuts to paste contents.');
  1247. }
  1248. return;
  1249. }
  1250. if (plainTextMode) {
  1251. pasteText(editor, content);
  1252. } else {
  1253. pasteHtml$1(editor, content, internal);
  1254. }
  1255. }
  1256. var getLastRng = function () {
  1257. return pasteBin.getLastRng() || editor.selection.getRng();
  1258. };
  1259. editor.on('paste', function (e) {
  1260. var clipboardTimer = new Date().getTime();
  1261. var clipboardContent = getClipboardContent(editor, e);
  1262. var clipboardDelay = new Date().getTime() - clipboardTimer;
  1263. var isKeyBoardPaste = new Date().getTime() - keyboardPasteTimeStamp - clipboardDelay < 1000;
  1264. var plainTextMode = pasteFormat.get() === 'text' || keyboardPastePlainTextState;
  1265. var internal = hasContentType(clipboardContent, InternalHtml.internalHtmlMime());
  1266. keyboardPastePlainTextState = false;
  1267. if (e.isDefaultPrevented() || isBrokenAndroidClipboardEvent(e)) {
  1268. pasteBin.remove();
  1269. return;
  1270. }
  1271. if (!hasHtmlOrText(clipboardContent) && pasteImageData(editor, e, getLastRng())) {
  1272. pasteBin.remove();
  1273. return;
  1274. }
  1275. if (!isKeyBoardPaste) {
  1276. e.preventDefault();
  1277. }
  1278. if (global$1.ie && (!isKeyBoardPaste || e.ieFake) && !hasContentType(clipboardContent, 'text/html')) {
  1279. pasteBin.create();
  1280. editor.dom.bind(pasteBin.getEl(), 'paste', function (e) {
  1281. e.stopPropagation();
  1282. });
  1283. editor.getDoc().execCommand('Paste', false, null);
  1284. clipboardContent['text/html'] = pasteBin.getHtml();
  1285. }
  1286. if (hasContentType(clipboardContent, 'text/html')) {
  1287. e.preventDefault();
  1288. if (!internal) {
  1289. internal = InternalHtml.isMarked(clipboardContent['text/html']);
  1290. }
  1291. insertClipboardContent(clipboardContent, isKeyBoardPaste, plainTextMode, internal);
  1292. } else {
  1293. global$2.setEditorTimeout(editor, function () {
  1294. insertClipboardContent(clipboardContent, isKeyBoardPaste, plainTextMode, internal);
  1295. }, 0);
  1296. }
  1297. });
  1298. };
  1299. var registerEventsAndFilters = function (editor, pasteBin, pasteFormat) {
  1300. registerEventHandlers(editor, pasteBin, pasteFormat);
  1301. var src;
  1302. editor.parser.addNodeFilter('img', function (nodes, name, args) {
  1303. var isPasteInsert = function (args) {
  1304. return args.data && args.data.paste === true;
  1305. };
  1306. var remove = function (node) {
  1307. if (!node.attr('data-mce-object') && src !== global$1.transparentSrc) {
  1308. node.remove();
  1309. }
  1310. };
  1311. var isWebKitFakeUrl = function (src) {
  1312. return src.indexOf('webkit-fake-url') === 0;
  1313. };
  1314. var isDataUri = function (src) {
  1315. return src.indexOf('data:') === 0;
  1316. };
  1317. if (!editor.settings.paste_data_images && isPasteInsert(args)) {
  1318. var i = nodes.length;
  1319. while (i--) {
  1320. src = nodes[i].attributes.map.src;
  1321. if (!src) {
  1322. continue;
  1323. }
  1324. if (isWebKitFakeUrl(src)) {
  1325. remove(nodes[i]);
  1326. } else if (!editor.settings.allow_html_data_urls && isDataUri(src)) {
  1327. remove(nodes[i]);
  1328. }
  1329. }
  1330. }
  1331. });
  1332. };
  1333. var getPasteBinParent = function (editor) {
  1334. return global$1.ie && editor.inline ? domGlobals.document.body : editor.getBody();
  1335. };
  1336. var isExternalPasteBin = function (editor) {
  1337. return getPasteBinParent(editor) !== editor.getBody();
  1338. };
  1339. var delegatePasteEvents = function (editor, pasteBinElm, pasteBinDefaultContent) {
  1340. if (isExternalPasteBin(editor)) {
  1341. editor.dom.bind(pasteBinElm, 'paste keyup', function (e) {
  1342. if (!isDefault(editor, pasteBinDefaultContent)) {
  1343. editor.fire('paste');
  1344. }
  1345. });
  1346. }
  1347. };
  1348. var create = function (editor, lastRngCell, pasteBinDefaultContent) {
  1349. var dom = editor.dom, body = editor.getBody();
  1350. var pasteBinElm;
  1351. lastRngCell.set(editor.selection.getRng());
  1352. pasteBinElm = editor.dom.add(getPasteBinParent(editor), 'div', {
  1353. 'id': 'mcepastebin',
  1354. 'class': 'mce-pastebin',
  1355. 'contentEditable': true,
  1356. 'data-mce-bogus': 'all',
  1357. 'style': 'position: fixed; top: 50%; width: 10px; height: 10px; overflow: hidden; opacity: 0'
  1358. }, pasteBinDefaultContent);
  1359. if (global$1.ie || global$1.gecko) {
  1360. dom.setStyle(pasteBinElm, 'left', dom.getStyle(body, 'direction', true) === 'rtl' ? 65535 : -65535);
  1361. }
  1362. dom.bind(pasteBinElm, 'beforedeactivate focusin focusout', function (e) {
  1363. e.stopPropagation();
  1364. });
  1365. delegatePasteEvents(editor, pasteBinElm, pasteBinDefaultContent);
  1366. pasteBinElm.focus();
  1367. editor.selection.select(pasteBinElm, true);
  1368. };
  1369. var remove = function (editor, lastRngCell) {
  1370. if (getEl(editor)) {
  1371. var pasteBinClone = void 0;
  1372. var lastRng = lastRngCell.get();
  1373. while (pasteBinClone = editor.dom.get('mcepastebin')) {
  1374. editor.dom.remove(pasteBinClone);
  1375. editor.dom.unbind(pasteBinClone);
  1376. }
  1377. if (lastRng) {
  1378. editor.selection.setRng(lastRng);
  1379. }
  1380. }
  1381. lastRngCell.set(null);
  1382. };
  1383. var getEl = function (editor) {
  1384. return editor.dom.get('mcepastebin');
  1385. };
  1386. var getHtml = function (editor) {
  1387. var pasteBinElm, pasteBinClones, i, dirtyWrappers, cleanWrapper;
  1388. var copyAndRemove = function (toElm, fromElm) {
  1389. toElm.appendChild(fromElm);
  1390. editor.dom.remove(fromElm, true);
  1391. };
  1392. pasteBinClones = global$3.grep(getPasteBinParent(editor).childNodes, function (elm) {
  1393. return elm.id === 'mcepastebin';
  1394. });
  1395. pasteBinElm = pasteBinClones.shift();
  1396. global$3.each(pasteBinClones, function (pasteBinClone) {
  1397. copyAndRemove(pasteBinElm, pasteBinClone);
  1398. });
  1399. dirtyWrappers = editor.dom.select('div[id=mcepastebin]', pasteBinElm);
  1400. for (i = dirtyWrappers.length - 1; i >= 0; i--) {
  1401. cleanWrapper = editor.dom.create('div');
  1402. pasteBinElm.insertBefore(cleanWrapper, dirtyWrappers[i]);
  1403. copyAndRemove(cleanWrapper, dirtyWrappers[i]);
  1404. }
  1405. return pasteBinElm ? pasteBinElm.innerHTML : '';
  1406. };
  1407. var getLastRng = function (lastRng) {
  1408. return lastRng.get();
  1409. };
  1410. var isDefaultContent = function (pasteBinDefaultContent, content) {
  1411. return content === pasteBinDefaultContent;
  1412. };
  1413. var isPasteBin = function (elm) {
  1414. return elm && elm.id === 'mcepastebin';
  1415. };
  1416. var isDefault = function (editor, pasteBinDefaultContent) {
  1417. var pasteBinElm = getEl(editor);
  1418. return isPasteBin(pasteBinElm) && isDefaultContent(pasteBinDefaultContent, pasteBinElm.innerHTML);
  1419. };
  1420. var PasteBin = function (editor) {
  1421. var lastRng = Cell(null);
  1422. var pasteBinDefaultContent = '%MCEPASTEBIN%';
  1423. return {
  1424. create: function () {
  1425. return create(editor, lastRng, pasteBinDefaultContent);
  1426. },
  1427. remove: function () {
  1428. return remove(editor, lastRng);
  1429. },
  1430. getEl: function () {
  1431. return getEl(editor);
  1432. },
  1433. getHtml: function () {
  1434. return getHtml(editor);
  1435. },
  1436. getLastRng: function () {
  1437. return getLastRng(lastRng);
  1438. },
  1439. isDefault: function () {
  1440. return isDefault(editor, pasteBinDefaultContent);
  1441. },
  1442. isDefaultContent: function (content) {
  1443. return isDefaultContent(pasteBinDefaultContent, content);
  1444. }
  1445. };
  1446. };
  1447. var Clipboard = function (editor, pasteFormat) {
  1448. var pasteBin = PasteBin(editor);
  1449. editor.on('preInit', function () {
  1450. return registerEventsAndFilters(editor, pasteBin, pasteFormat);
  1451. });
  1452. return {
  1453. pasteFormat: pasteFormat,
  1454. pasteHtml: function (html, internalFlag) {
  1455. return pasteHtml$1(editor, html, internalFlag);
  1456. },
  1457. pasteText: function (text) {
  1458. return pasteText(editor, text);
  1459. },
  1460. pasteImageData: function (e, rng) {
  1461. return pasteImageData(editor, e, rng);
  1462. },
  1463. getDataTransferItems: getDataTransferItems,
  1464. hasHtmlOrText: hasHtmlOrText,
  1465. hasContentType: hasContentType
  1466. };
  1467. };
  1468. var noop = function () {
  1469. };
  1470. var hasWorkingClipboardApi = function (clipboardData) {
  1471. return global$1.iOS === false && clipboardData !== undefined && typeof clipboardData.setData === 'function' && Utils.isMsEdge() !== true;
  1472. };
  1473. var setHtml5Clipboard = function (clipboardData, html, text) {
  1474. if (hasWorkingClipboardApi(clipboardData)) {
  1475. try {
  1476. clipboardData.clearData();
  1477. clipboardData.setData('text/html', html);
  1478. clipboardData.setData('text/plain', text);
  1479. clipboardData.setData(InternalHtml.internalHtmlMime(), html);
  1480. return true;
  1481. } catch (e) {
  1482. return false;
  1483. }
  1484. } else {
  1485. return false;
  1486. }
  1487. };
  1488. var setClipboardData = function (evt, data, fallback, done) {
  1489. if (setHtml5Clipboard(evt.clipboardData, data.html, data.text)) {
  1490. evt.preventDefault();
  1491. done();
  1492. } else {
  1493. fallback(data.html, done);
  1494. }
  1495. };
  1496. var fallback = function (editor) {
  1497. return function (html, done) {
  1498. var markedHtml = InternalHtml.mark(html);
  1499. var outer = editor.dom.create('div', {
  1500. 'contenteditable': 'false',
  1501. 'data-mce-bogus': 'all'
  1502. });
  1503. var inner = editor.dom.create('div', { contenteditable: 'true' }, markedHtml);
  1504. editor.dom.setStyles(outer, {
  1505. position: 'fixed',
  1506. top: '0',
  1507. left: '-3000px',
  1508. width: '1000px',
  1509. overflow: 'hidden'
  1510. });
  1511. outer.appendChild(inner);
  1512. editor.dom.add(editor.getBody(), outer);
  1513. var range = editor.selection.getRng();
  1514. inner.focus();
  1515. var offscreenRange = editor.dom.createRng();
  1516. offscreenRange.selectNodeContents(inner);
  1517. editor.selection.setRng(offscreenRange);
  1518. setTimeout(function () {
  1519. editor.selection.setRng(range);
  1520. outer.parentNode.removeChild(outer);
  1521. done();
  1522. }, 0);
  1523. };
  1524. };
  1525. var getData = function (editor) {
  1526. return {
  1527. html: editor.selection.getContent({ contextual: true }),
  1528. text: editor.selection.getContent({ format: 'text' })
  1529. };
  1530. };
  1531. var isTableSelection = function (editor) {
  1532. return !!editor.dom.getParent(editor.selection.getStart(), 'td[data-mce-selected],th[data-mce-selected]', editor.getBody());
  1533. };
  1534. var hasSelectedContent = function (editor) {
  1535. return !editor.selection.isCollapsed() || isTableSelection(editor);
  1536. };
  1537. var cut = function (editor) {
  1538. return function (evt) {
  1539. if (hasSelectedContent(editor)) {
  1540. setClipboardData(evt, getData(editor), fallback(editor), function () {
  1541. setTimeout(function () {
  1542. editor.execCommand('Delete');
  1543. }, 0);
  1544. });
  1545. }
  1546. };
  1547. };
  1548. var copy = function (editor) {
  1549. return function (evt) {
  1550. if (hasSelectedContent(editor)) {
  1551. setClipboardData(evt, getData(editor), fallback(editor), noop);
  1552. }
  1553. };
  1554. };
  1555. var register$1 = function (editor) {
  1556. editor.on('cut', cut(editor));
  1557. editor.on('copy', copy(editor));
  1558. };
  1559. var CutCopy = { register: register$1 };
  1560. var global$a = tinymce.util.Tools.resolve('tinymce.dom.RangeUtils');
  1561. var getCaretRangeFromEvent = function (editor, e) {
  1562. return global$a.getCaretRangeFromPoint(e.clientX, e.clientY, editor.getDoc());
  1563. };
  1564. var isPlainTextFileUrl = function (content) {
  1565. var plainTextContent = content['text/plain'];
  1566. return plainTextContent ? plainTextContent.indexOf('file://') === 0 : false;
  1567. };
  1568. var setFocusedRange = function (editor, rng) {
  1569. editor.focus();
  1570. editor.selection.setRng(rng);
  1571. };
  1572. var setup = function (editor, clipboard, draggingInternallyState) {
  1573. if (Settings.shouldBlockDrop(editor)) {
  1574. editor.on('dragend dragover draggesture dragdrop drop drag', function (e) {
  1575. e.preventDefault();
  1576. e.stopPropagation();
  1577. });
  1578. }
  1579. if (!Settings.shouldPasteDataImages(editor)) {
  1580. editor.on('drop', function (e) {
  1581. var dataTransfer = e.dataTransfer;
  1582. if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
  1583. e.preventDefault();
  1584. }
  1585. });
  1586. }
  1587. editor.on('drop', function (e) {
  1588. var dropContent, rng;
  1589. rng = getCaretRangeFromEvent(editor, e);
  1590. if (e.isDefaultPrevented() || draggingInternallyState.get()) {
  1591. return;
  1592. }
  1593. dropContent = clipboard.getDataTransferItems(e.dataTransfer);
  1594. var internal = clipboard.hasContentType(dropContent, InternalHtml.internalHtmlMime());
  1595. if ((!clipboard.hasHtmlOrText(dropContent) || isPlainTextFileUrl(dropContent)) && clipboard.pasteImageData(e, rng)) {
  1596. return;
  1597. }
  1598. if (rng && Settings.shouldFilterDrop(editor)) {
  1599. var content_1 = dropContent['mce-internal'] || dropContent['text/html'] || dropContent['text/plain'];
  1600. if (content_1) {
  1601. e.preventDefault();
  1602. global$2.setEditorTimeout(editor, function () {
  1603. editor.undoManager.transact(function () {
  1604. if (dropContent['mce-internal']) {
  1605. editor.execCommand('Delete');
  1606. }
  1607. setFocusedRange(editor, rng);
  1608. content_1 = Utils.trimHtml(content_1);
  1609. if (!dropContent['text/html']) {
  1610. clipboard.pasteText(content_1);
  1611. } else {
  1612. clipboard.pasteHtml(content_1, internal);
  1613. }
  1614. });
  1615. });
  1616. }
  1617. }
  1618. });
  1619. editor.on('dragstart', function (e) {
  1620. draggingInternallyState.set(true);
  1621. });
  1622. editor.on('dragover dragend', function (e) {
  1623. if (Settings.shouldPasteDataImages(editor) && draggingInternallyState.get() === false) {
  1624. e.preventDefault();
  1625. setFocusedRange(editor, getCaretRangeFromEvent(editor, e));
  1626. }
  1627. if (e.type === 'dragend') {
  1628. draggingInternallyState.set(false);
  1629. }
  1630. });
  1631. };
  1632. var DragDrop = { setup: setup };
  1633. var setup$1 = function (editor) {
  1634. var plugin = editor.plugins.paste;
  1635. var preProcess = Settings.getPreProcess(editor);
  1636. if (preProcess) {
  1637. editor.on('PastePreProcess', function (e) {
  1638. preProcess.call(plugin, plugin, e);
  1639. });
  1640. }
  1641. var postProcess = Settings.getPostProcess(editor);
  1642. if (postProcess) {
  1643. editor.on('PastePostProcess', function (e) {
  1644. postProcess.call(plugin, plugin, e);
  1645. });
  1646. }
  1647. };
  1648. var PrePostProcess = { setup: setup$1 };
  1649. function addPreProcessFilter(editor, filterFunc) {
  1650. editor.on('PastePreProcess', function (e) {
  1651. e.content = filterFunc(editor, e.content, e.internal, e.wordContent);
  1652. });
  1653. }
  1654. function addPostProcessFilter(editor, filterFunc) {
  1655. editor.on('PastePostProcess', function (e) {
  1656. filterFunc(editor, e.node);
  1657. });
  1658. }
  1659. function removeExplorerBrElementsAfterBlocks(editor, html) {
  1660. if (!WordFilter.isWordContent(html)) {
  1661. return html;
  1662. }
  1663. var blockElements = [];
  1664. global$3.each(editor.schema.getBlockElements(), function (block, blockName) {
  1665. blockElements.push(blockName);
  1666. });
  1667. var explorerBlocksRegExp = new RegExp('(?:<br>&nbsp;[\\s\\r\\n]+|<br>)*(<\\/?(' + blockElements.join('|') + ')[^>]*>)(?:<br>&nbsp;[\\s\\r\\n]+|<br>)*', 'g');
  1668. html = Utils.filter(html, [[
  1669. explorerBlocksRegExp,
  1670. '$1'
  1671. ]]);
  1672. html = Utils.filter(html, [
  1673. [
  1674. /<br><br>/g,
  1675. '<BR><BR>'
  1676. ],
  1677. [
  1678. /<br>/g,
  1679. ' '
  1680. ],
  1681. [
  1682. /<BR><BR>/g,
  1683. '<br>'
  1684. ]
  1685. ]);
  1686. return html;
  1687. }
  1688. function removeWebKitStyles(editor, content, internal, isWordHtml) {
  1689. if (isWordHtml || internal) {
  1690. return content;
  1691. }
  1692. var webKitStylesSetting = Settings.getWebkitStyles(editor);
  1693. var webKitStyles;
  1694. if (Settings.shouldRemoveWebKitStyles(editor) === false || webKitStylesSetting === 'all') {
  1695. return content;
  1696. }
  1697. if (webKitStylesSetting) {
  1698. webKitStyles = webKitStylesSetting.split(/[, ]/);
  1699. }
  1700. if (webKitStyles) {
  1701. var dom_1 = editor.dom, node_1 = editor.selection.getNode();
  1702. content = content.replace(/(<[^>]+) style="([^"]*)"([^>]*>)/gi, function (all, before, value, after) {
  1703. var inputStyles = dom_1.parseStyle(dom_1.decode(value));
  1704. var outputStyles = {};
  1705. if (webKitStyles === 'none') {
  1706. return before + after;
  1707. }
  1708. for (var i = 0; i < webKitStyles.length; i++) {
  1709. var inputValue = inputStyles[webKitStyles[i]], currentValue = dom_1.getStyle(node_1, webKitStyles[i], true);
  1710. if (/color/.test(webKitStyles[i])) {
  1711. inputValue = dom_1.toHex(inputValue);
  1712. currentValue = dom_1.toHex(currentValue);
  1713. }
  1714. if (currentValue !== inputValue) {
  1715. outputStyles[webKitStyles[i]] = inputValue;
  1716. }
  1717. }
  1718. outputStyles = dom_1.serializeStyle(outputStyles, 'span');
  1719. if (outputStyles) {
  1720. return before + ' style="' + outputStyles + '"' + after;
  1721. }
  1722. return before + after;
  1723. });
  1724. } else {
  1725. content = content.replace(/(<[^>]+) style="([^"]*)"([^>]*>)/gi, '$1$3');
  1726. }
  1727. content = content.replace(/(<[^>]+) data-mce-style="([^"]+)"([^>]*>)/gi, function (all, before, value, after) {
  1728. return before + ' style="' + value + '"' + after;
  1729. });
  1730. return content;
  1731. }
  1732. function removeUnderlineAndFontInAnchor(editor, root) {
  1733. editor.$('a', root).find('font,u').each(function (i, node) {
  1734. editor.dom.remove(node, true);
  1735. });
  1736. }
  1737. var setup$2 = function (editor) {
  1738. if (global$1.webkit) {
  1739. addPreProcessFilter(editor, removeWebKitStyles);
  1740. }
  1741. if (global$1.ie) {
  1742. addPreProcessFilter(editor, removeExplorerBrElementsAfterBlocks);
  1743. addPostProcessFilter(editor, removeUnderlineAndFontInAnchor);
  1744. }
  1745. };
  1746. var Quirks = { setup: setup$2 };
  1747. var makeSetupHandler = function (editor, clipboard) {
  1748. return function (api) {
  1749. api.setActive(clipboard.pasteFormat.get() === 'text');
  1750. var pastePlainTextToggleHandler = function (e) {
  1751. return api.setActive(e.state);
  1752. };
  1753. editor.on('PastePlainTextToggle', pastePlainTextToggleHandler);
  1754. return function () {
  1755. return editor.off('PastePlainTextToggle', pastePlainTextToggleHandler);
  1756. };
  1757. };
  1758. };
  1759. var register$2 = function (editor, clipboard) {
  1760. editor.ui.registry.addToggleButton('pastetext', {
  1761. active: false,
  1762. icon: 'paste-text',
  1763. tooltip: 'Paste as text',
  1764. onAction: function () {
  1765. return editor.execCommand('mceTogglePlainTextPaste');
  1766. },
  1767. onSetup: makeSetupHandler(editor, clipboard)
  1768. });
  1769. editor.ui.registry.addToggleMenuItem('pastetext', {
  1770. text: 'Paste as text',
  1771. onAction: function () {
  1772. return editor.execCommand('mceTogglePlainTextPaste');
  1773. },
  1774. onSetup: makeSetupHandler(editor, clipboard)
  1775. });
  1776. };
  1777. var Buttons = { register: register$2 };
  1778. global.add('paste', function (editor) {
  1779. if (DetectProPlugin.hasProPlugin(editor) === false) {
  1780. var draggingInternallyState = Cell(false);
  1781. var pasteFormat = Cell(Settings.isPasteAsTextEnabled(editor) ? 'text' : 'html');
  1782. var clipboard = Clipboard(editor, pasteFormat);
  1783. var quirks = Quirks.setup(editor);
  1784. Buttons.register(editor, clipboard);
  1785. Commands.register(editor, clipboard);
  1786. PrePostProcess.setup(editor);
  1787. CutCopy.register(editor);
  1788. DragDrop.setup(editor, clipboard, draggingInternallyState);
  1789. return Api.get(clipboard, quirks);
  1790. }
  1791. });
  1792. function Plugin () {
  1793. }
  1794. return Plugin;
  1795. }(window));
  1796. })();