plugin.js 59 KB

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