plugin.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  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.1.0 (2019-10-17)
  8. */
  9. (function (domGlobals) {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var noop = function () {
  13. };
  14. var constant = function (value) {
  15. return function () {
  16. return value;
  17. };
  18. };
  19. var not = function (f) {
  20. return function () {
  21. var args = [];
  22. for (var _i = 0; _i < arguments.length; _i++) {
  23. args[_i] = arguments[_i];
  24. }
  25. return !f.apply(null, args);
  26. };
  27. };
  28. var never = constant(false);
  29. var always = constant(true);
  30. var none = function () {
  31. return NONE;
  32. };
  33. var NONE = function () {
  34. var eq = function (o) {
  35. return o.isNone();
  36. };
  37. var call = function (thunk) {
  38. return thunk();
  39. };
  40. var id = function (n) {
  41. return n;
  42. };
  43. var me = {
  44. fold: function (n, s) {
  45. return n();
  46. },
  47. is: never,
  48. isSome: never,
  49. isNone: always,
  50. getOr: id,
  51. getOrThunk: call,
  52. getOrDie: function (msg) {
  53. throw new Error(msg || 'error: getOrDie called on none.');
  54. },
  55. getOrNull: constant(null),
  56. getOrUndefined: constant(undefined),
  57. or: id,
  58. orThunk: call,
  59. map: none,
  60. each: noop,
  61. bind: none,
  62. exists: never,
  63. forall: always,
  64. filter: none,
  65. equals: eq,
  66. equals_: eq,
  67. toArray: function () {
  68. return [];
  69. },
  70. toString: constant('none()')
  71. };
  72. if (Object.freeze) {
  73. Object.freeze(me);
  74. }
  75. return me;
  76. }();
  77. var some = function (a) {
  78. var constant_a = constant(a);
  79. var self = function () {
  80. return me;
  81. };
  82. var bind = function (f) {
  83. return f(a);
  84. };
  85. var me = {
  86. fold: function (n, s) {
  87. return s(a);
  88. },
  89. is: function (v) {
  90. return a === v;
  91. },
  92. isSome: always,
  93. isNone: never,
  94. getOr: constant_a,
  95. getOrThunk: constant_a,
  96. getOrDie: constant_a,
  97. getOrNull: constant_a,
  98. getOrUndefined: constant_a,
  99. or: self,
  100. orThunk: self,
  101. map: function (f) {
  102. return some(f(a));
  103. },
  104. each: function (f) {
  105. f(a);
  106. },
  107. bind: bind,
  108. exists: bind,
  109. forall: bind,
  110. filter: function (f) {
  111. return f(a) ? me : NONE;
  112. },
  113. toArray: function () {
  114. return [a];
  115. },
  116. toString: function () {
  117. return 'some(' + a + ')';
  118. },
  119. equals: function (o) {
  120. return o.is(a);
  121. },
  122. equals_: function (o, elementEq) {
  123. return o.fold(never, function (b) {
  124. return elementEq(a, b);
  125. });
  126. }
  127. };
  128. return me;
  129. };
  130. var from = function (value) {
  131. return value === null || value === undefined ? NONE : some(value);
  132. };
  133. var Option = {
  134. some: some,
  135. none: none,
  136. from: from
  137. };
  138. var typeOf = function (x) {
  139. if (x === null) {
  140. return 'null';
  141. }
  142. var t = typeof x;
  143. if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  144. return 'array';
  145. }
  146. if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  147. return 'string';
  148. }
  149. return t;
  150. };
  151. var isType = function (type) {
  152. return function (value) {
  153. return typeOf(value) === type;
  154. };
  155. };
  156. var isString = isType('string');
  157. var isArray = isType('array');
  158. var isBoolean = isType('boolean');
  159. var isFunction = isType('function');
  160. var isNumber = isType('number');
  161. var nativeSlice = Array.prototype.slice;
  162. var nativePush = Array.prototype.push;
  163. var map = function (xs, f) {
  164. var len = xs.length;
  165. var r = new Array(len);
  166. for (var i = 0; i < len; i++) {
  167. var x = xs[i];
  168. r[i] = f(x, i);
  169. }
  170. return r;
  171. };
  172. var each = function (xs, f) {
  173. for (var i = 0, len = xs.length; i < len; i++) {
  174. var x = xs[i];
  175. f(x, i);
  176. }
  177. };
  178. var filter = function (xs, pred) {
  179. var r = [];
  180. for (var i = 0, len = xs.length; i < len; i++) {
  181. var x = xs[i];
  182. if (pred(x, i)) {
  183. r.push(x);
  184. }
  185. }
  186. return r;
  187. };
  188. var groupBy = function (xs, f) {
  189. if (xs.length === 0) {
  190. return [];
  191. } else {
  192. var wasType = f(xs[0]);
  193. var r = [];
  194. var group = [];
  195. for (var i = 0, len = xs.length; i < len; i++) {
  196. var x = xs[i];
  197. var type = f(x);
  198. if (type !== wasType) {
  199. r.push(group);
  200. group = [];
  201. }
  202. wasType = type;
  203. group.push(x);
  204. }
  205. if (group.length !== 0) {
  206. r.push(group);
  207. }
  208. return r;
  209. }
  210. };
  211. var foldl = function (xs, f, acc) {
  212. each(xs, function (x) {
  213. acc = f(acc, x);
  214. });
  215. return acc;
  216. };
  217. var find = function (xs, pred) {
  218. for (var i = 0, len = xs.length; i < len; i++) {
  219. var x = xs[i];
  220. if (pred(x, i)) {
  221. return Option.some(x);
  222. }
  223. }
  224. return Option.none();
  225. };
  226. var flatten = function (xs) {
  227. var r = [];
  228. for (var i = 0, len = xs.length; i < len; ++i) {
  229. if (!isArray(xs[i])) {
  230. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  231. }
  232. nativePush.apply(r, xs[i]);
  233. }
  234. return r;
  235. };
  236. var bind = function (xs, f) {
  237. var output = map(xs, f);
  238. return flatten(output);
  239. };
  240. var reverse = function (xs) {
  241. var r = nativeSlice.call(xs, 0);
  242. r.reverse();
  243. return r;
  244. };
  245. var head = function (xs) {
  246. return xs.length === 0 ? Option.none() : Option.some(xs[0]);
  247. };
  248. var last = function (xs) {
  249. return xs.length === 0 ? Option.none() : Option.some(xs[xs.length - 1]);
  250. };
  251. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  252. return nativeSlice.call(x);
  253. };
  254. var compareDocumentPosition = function (a, b, match) {
  255. return (a.compareDocumentPosition(b) & match) !== 0;
  256. };
  257. var documentPositionPreceding = function (a, b) {
  258. return compareDocumentPosition(a, b, domGlobals.Node.DOCUMENT_POSITION_PRECEDING);
  259. };
  260. var documentPositionContainedBy = function (a, b) {
  261. return compareDocumentPosition(a, b, domGlobals.Node.DOCUMENT_POSITION_CONTAINED_BY);
  262. };
  263. var Node = {
  264. documentPositionPreceding: documentPositionPreceding,
  265. documentPositionContainedBy: documentPositionContainedBy
  266. };
  267. var Cell = function (initial) {
  268. var value = initial;
  269. var get = function () {
  270. return value;
  271. };
  272. var set = function (v) {
  273. value = v;
  274. };
  275. var clone = function () {
  276. return Cell(get());
  277. };
  278. return {
  279. get: get,
  280. set: set,
  281. clone: clone
  282. };
  283. };
  284. var firstMatch = function (regexes, s) {
  285. for (var i = 0; i < regexes.length; i++) {
  286. var x = regexes[i];
  287. if (x.test(s)) {
  288. return x;
  289. }
  290. }
  291. return undefined;
  292. };
  293. var find$1 = function (regexes, agent) {
  294. var r = firstMatch(regexes, agent);
  295. if (!r) {
  296. return {
  297. major: 0,
  298. minor: 0
  299. };
  300. }
  301. var group = function (i) {
  302. return Number(agent.replace(r, '$' + i));
  303. };
  304. return nu(group(1), group(2));
  305. };
  306. var detect = function (versionRegexes, agent) {
  307. var cleanedAgent = String(agent).toLowerCase();
  308. if (versionRegexes.length === 0) {
  309. return unknown();
  310. }
  311. return find$1(versionRegexes, cleanedAgent);
  312. };
  313. var unknown = function () {
  314. return nu(0, 0);
  315. };
  316. var nu = function (major, minor) {
  317. return {
  318. major: major,
  319. minor: minor
  320. };
  321. };
  322. var Version = {
  323. nu: nu,
  324. detect: detect,
  325. unknown: unknown
  326. };
  327. var edge = 'Edge';
  328. var chrome = 'Chrome';
  329. var ie = 'IE';
  330. var opera = 'Opera';
  331. var firefox = 'Firefox';
  332. var safari = 'Safari';
  333. var isBrowser = function (name, current) {
  334. return function () {
  335. return current === name;
  336. };
  337. };
  338. var unknown$1 = function () {
  339. return nu$1({
  340. current: undefined,
  341. version: Version.unknown()
  342. });
  343. };
  344. var nu$1 = function (info) {
  345. var current = info.current;
  346. var version = info.version;
  347. return {
  348. current: current,
  349. version: version,
  350. isEdge: isBrowser(edge, current),
  351. isChrome: isBrowser(chrome, current),
  352. isIE: isBrowser(ie, current),
  353. isOpera: isBrowser(opera, current),
  354. isFirefox: isBrowser(firefox, current),
  355. isSafari: isBrowser(safari, current)
  356. };
  357. };
  358. var Browser = {
  359. unknown: unknown$1,
  360. nu: nu$1,
  361. edge: constant(edge),
  362. chrome: constant(chrome),
  363. ie: constant(ie),
  364. opera: constant(opera),
  365. firefox: constant(firefox),
  366. safari: constant(safari)
  367. };
  368. var windows = 'Windows';
  369. var ios = 'iOS';
  370. var android = 'Android';
  371. var linux = 'Linux';
  372. var osx = 'OSX';
  373. var solaris = 'Solaris';
  374. var freebsd = 'FreeBSD';
  375. var isOS = function (name, current) {
  376. return function () {
  377. return current === name;
  378. };
  379. };
  380. var unknown$2 = function () {
  381. return nu$2({
  382. current: undefined,
  383. version: Version.unknown()
  384. });
  385. };
  386. var nu$2 = function (info) {
  387. var current = info.current;
  388. var version = info.version;
  389. return {
  390. current: current,
  391. version: version,
  392. isWindows: isOS(windows, current),
  393. isiOS: isOS(ios, current),
  394. isAndroid: isOS(android, current),
  395. isOSX: isOS(osx, current),
  396. isLinux: isOS(linux, current),
  397. isSolaris: isOS(solaris, current),
  398. isFreeBSD: isOS(freebsd, current)
  399. };
  400. };
  401. var OperatingSystem = {
  402. unknown: unknown$2,
  403. nu: nu$2,
  404. windows: constant(windows),
  405. ios: constant(ios),
  406. android: constant(android),
  407. linux: constant(linux),
  408. osx: constant(osx),
  409. solaris: constant(solaris),
  410. freebsd: constant(freebsd)
  411. };
  412. var DeviceType = function (os, browser, userAgent, mediaMatch) {
  413. var isiPad = os.isiOS() && /ipad/i.test(userAgent) === true;
  414. var isiPhone = os.isiOS() && !isiPad;
  415. var isMobile = os.isiOS() || os.isAndroid();
  416. var isTouch = isMobile || mediaMatch('(pointer:coarse)');
  417. var isTablet = isiPad || !isiPhone && isMobile && mediaMatch('(min-device-width:768px)');
  418. var isPhone = isiPhone || isMobile && !isTablet;
  419. var iOSwebview = browser.isSafari() && os.isiOS() && /safari/i.test(userAgent) === false;
  420. var isDesktop = !isPhone && !isTablet && !iOSwebview;
  421. return {
  422. isiPad: constant(isiPad),
  423. isiPhone: constant(isiPhone),
  424. isTablet: constant(isTablet),
  425. isPhone: constant(isPhone),
  426. isTouch: constant(isTouch),
  427. isAndroid: os.isAndroid,
  428. isiOS: os.isiOS,
  429. isWebView: constant(iOSwebview),
  430. isDesktop: constant(isDesktop)
  431. };
  432. };
  433. var detect$1 = function (candidates, userAgent) {
  434. var agent = String(userAgent).toLowerCase();
  435. return find(candidates, function (candidate) {
  436. return candidate.search(agent);
  437. });
  438. };
  439. var detectBrowser = function (browsers, userAgent) {
  440. return detect$1(browsers, userAgent).map(function (browser) {
  441. var version = Version.detect(browser.versionRegexes, userAgent);
  442. return {
  443. current: browser.name,
  444. version: version
  445. };
  446. });
  447. };
  448. var detectOs = function (oses, userAgent) {
  449. return detect$1(oses, userAgent).map(function (os) {
  450. var version = Version.detect(os.versionRegexes, userAgent);
  451. return {
  452. current: os.name,
  453. version: version
  454. };
  455. });
  456. };
  457. var UaString = {
  458. detectBrowser: detectBrowser,
  459. detectOs: detectOs
  460. };
  461. var contains = function (str, substr) {
  462. return str.indexOf(substr) !== -1;
  463. };
  464. var normalVersionRegex = /.*?version\/\ ?([0-9]+)\.([0-9]+).*/;
  465. var checkContains = function (target) {
  466. return function (uastring) {
  467. return contains(uastring, target);
  468. };
  469. };
  470. var browsers = [
  471. {
  472. name: 'Edge',
  473. versionRegexes: [/.*?edge\/ ?([0-9]+)\.([0-9]+)$/],
  474. search: function (uastring) {
  475. return contains(uastring, 'edge/') && contains(uastring, 'chrome') && contains(uastring, 'safari') && contains(uastring, 'applewebkit');
  476. }
  477. },
  478. {
  479. name: 'Chrome',
  480. versionRegexes: [
  481. /.*?chrome\/([0-9]+)\.([0-9]+).*/,
  482. normalVersionRegex
  483. ],
  484. search: function (uastring) {
  485. return contains(uastring, 'chrome') && !contains(uastring, 'chromeframe');
  486. }
  487. },
  488. {
  489. name: 'IE',
  490. versionRegexes: [
  491. /.*?msie\ ?([0-9]+)\.([0-9]+).*/,
  492. /.*?rv:([0-9]+)\.([0-9]+).*/
  493. ],
  494. search: function (uastring) {
  495. return contains(uastring, 'msie') || contains(uastring, 'trident');
  496. }
  497. },
  498. {
  499. name: 'Opera',
  500. versionRegexes: [
  501. normalVersionRegex,
  502. /.*?opera\/([0-9]+)\.([0-9]+).*/
  503. ],
  504. search: checkContains('opera')
  505. },
  506. {
  507. name: 'Firefox',
  508. versionRegexes: [/.*?firefox\/\ ?([0-9]+)\.([0-9]+).*/],
  509. search: checkContains('firefox')
  510. },
  511. {
  512. name: 'Safari',
  513. versionRegexes: [
  514. normalVersionRegex,
  515. /.*?cpu os ([0-9]+)_([0-9]+).*/
  516. ],
  517. search: function (uastring) {
  518. return (contains(uastring, 'safari') || contains(uastring, 'mobile/')) && contains(uastring, 'applewebkit');
  519. }
  520. }
  521. ];
  522. var oses = [
  523. {
  524. name: 'Windows',
  525. search: checkContains('win'),
  526. versionRegexes: [/.*?windows\ nt\ ?([0-9]+)\.([0-9]+).*/]
  527. },
  528. {
  529. name: 'iOS',
  530. search: function (uastring) {
  531. return contains(uastring, 'iphone') || contains(uastring, 'ipad');
  532. },
  533. versionRegexes: [
  534. /.*?version\/\ ?([0-9]+)\.([0-9]+).*/,
  535. /.*cpu os ([0-9]+)_([0-9]+).*/,
  536. /.*cpu iphone os ([0-9]+)_([0-9]+).*/
  537. ]
  538. },
  539. {
  540. name: 'Android',
  541. search: checkContains('android'),
  542. versionRegexes: [/.*?android\ ?([0-9]+)\.([0-9]+).*/]
  543. },
  544. {
  545. name: 'OSX',
  546. search: checkContains('os x'),
  547. versionRegexes: [/.*?os\ x\ ?([0-9]+)_([0-9]+).*/]
  548. },
  549. {
  550. name: 'Linux',
  551. search: checkContains('linux'),
  552. versionRegexes: []
  553. },
  554. {
  555. name: 'Solaris',
  556. search: checkContains('sunos'),
  557. versionRegexes: []
  558. },
  559. {
  560. name: 'FreeBSD',
  561. search: checkContains('freebsd'),
  562. versionRegexes: []
  563. }
  564. ];
  565. var PlatformInfo = {
  566. browsers: constant(browsers),
  567. oses: constant(oses)
  568. };
  569. var detect$2 = function (userAgent, mediaMatch) {
  570. var browsers = PlatformInfo.browsers();
  571. var oses = PlatformInfo.oses();
  572. var browser = UaString.detectBrowser(browsers, userAgent).fold(Browser.unknown, Browser.nu);
  573. var os = UaString.detectOs(oses, userAgent).fold(OperatingSystem.unknown, OperatingSystem.nu);
  574. var deviceType = DeviceType(os, browser, userAgent, mediaMatch);
  575. return {
  576. browser: browser,
  577. os: os,
  578. deviceType: deviceType
  579. };
  580. };
  581. var PlatformDetection = { detect: detect$2 };
  582. var mediaMatch = function (query) {
  583. return domGlobals.window.matchMedia(query).matches;
  584. };
  585. var platform = Cell(PlatformDetection.detect(domGlobals.navigator.userAgent, mediaMatch));
  586. var detect$3 = function () {
  587. return platform.get();
  588. };
  589. var fromHtml = function (html, scope) {
  590. var doc = scope || domGlobals.document;
  591. var div = doc.createElement('div');
  592. div.innerHTML = html;
  593. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  594. domGlobals.console.error('HTML does not have a single root node', html);
  595. throw new Error('HTML must have a single root node');
  596. }
  597. return fromDom(div.childNodes[0]);
  598. };
  599. var fromTag = function (tag, scope) {
  600. var doc = scope || domGlobals.document;
  601. var node = doc.createElement(tag);
  602. return fromDom(node);
  603. };
  604. var fromText = function (text, scope) {
  605. var doc = scope || domGlobals.document;
  606. var node = doc.createTextNode(text);
  607. return fromDom(node);
  608. };
  609. var fromDom = function (node) {
  610. if (node === null || node === undefined) {
  611. throw new Error('Node cannot be null or undefined');
  612. }
  613. return { dom: constant(node) };
  614. };
  615. var fromPoint = function (docElm, x, y) {
  616. var doc = docElm.dom();
  617. return Option.from(doc.elementFromPoint(x, y)).map(fromDom);
  618. };
  619. var Element = {
  620. fromHtml: fromHtml,
  621. fromTag: fromTag,
  622. fromText: fromText,
  623. fromDom: fromDom,
  624. fromPoint: fromPoint
  625. };
  626. var ATTRIBUTE = domGlobals.Node.ATTRIBUTE_NODE;
  627. var CDATA_SECTION = domGlobals.Node.CDATA_SECTION_NODE;
  628. var COMMENT = domGlobals.Node.COMMENT_NODE;
  629. var DOCUMENT = domGlobals.Node.DOCUMENT_NODE;
  630. var DOCUMENT_TYPE = domGlobals.Node.DOCUMENT_TYPE_NODE;
  631. var DOCUMENT_FRAGMENT = domGlobals.Node.DOCUMENT_FRAGMENT_NODE;
  632. var ELEMENT = domGlobals.Node.ELEMENT_NODE;
  633. var TEXT = domGlobals.Node.TEXT_NODE;
  634. var PROCESSING_INSTRUCTION = domGlobals.Node.PROCESSING_INSTRUCTION_NODE;
  635. var ENTITY_REFERENCE = domGlobals.Node.ENTITY_REFERENCE_NODE;
  636. var ENTITY = domGlobals.Node.ENTITY_NODE;
  637. var NOTATION = domGlobals.Node.NOTATION_NODE;
  638. var ELEMENT$1 = ELEMENT;
  639. var is = function (element, selector) {
  640. var dom = element.dom();
  641. if (dom.nodeType !== ELEMENT$1) {
  642. return false;
  643. } else {
  644. var elem = dom;
  645. if (elem.matches !== undefined) {
  646. return elem.matches(selector);
  647. } else if (elem.msMatchesSelector !== undefined) {
  648. return elem.msMatchesSelector(selector);
  649. } else if (elem.webkitMatchesSelector !== undefined) {
  650. return elem.webkitMatchesSelector(selector);
  651. } else if (elem.mozMatchesSelector !== undefined) {
  652. return elem.mozMatchesSelector(selector);
  653. } else {
  654. throw new Error('Browser lacks native selectors');
  655. }
  656. }
  657. };
  658. var eq = function (e1, e2) {
  659. return e1.dom() === e2.dom();
  660. };
  661. var regularContains = function (e1, e2) {
  662. var d1 = e1.dom();
  663. var d2 = e2.dom();
  664. return d1 === d2 ? false : d1.contains(d2);
  665. };
  666. var ieContains = function (e1, e2) {
  667. return Node.documentPositionContainedBy(e1.dom(), e2.dom());
  668. };
  669. var browser = detect$3().browser;
  670. var contains$1 = browser.isIE() ? ieContains : regularContains;
  671. var is$1 = is;
  672. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.RangeUtils');
  673. var global$2 = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  674. var global$3 = tinymce.util.Tools.resolve('tinymce.util.VK');
  675. var lift2 = function (oa, ob, f) {
  676. return oa.isSome() && ob.isSome() ? Option.some(f(oa.getOrDie(), ob.getOrDie())) : Option.none();
  677. };
  678. var fromElements = function (elements, scope) {
  679. var doc = scope || domGlobals.document;
  680. var fragment = doc.createDocumentFragment();
  681. each(elements, function (element) {
  682. fragment.appendChild(element.dom());
  683. });
  684. return Element.fromDom(fragment);
  685. };
  686. var Immutable = function () {
  687. var fields = [];
  688. for (var _i = 0; _i < arguments.length; _i++) {
  689. fields[_i] = arguments[_i];
  690. }
  691. return function () {
  692. var values = [];
  693. for (var _i = 0; _i < arguments.length; _i++) {
  694. values[_i] = arguments[_i];
  695. }
  696. if (fields.length !== values.length) {
  697. throw new Error('Wrong number of arguments to struct. Expected "[' + fields.length + ']", got ' + values.length + ' arguments');
  698. }
  699. var struct = {};
  700. each(fields, function (name, i) {
  701. struct[name] = constant(values[i]);
  702. });
  703. return struct;
  704. };
  705. };
  706. var keys = Object.keys;
  707. var each$1 = function (obj, f) {
  708. var props = keys(obj);
  709. for (var k = 0, len = props.length; k < len; k++) {
  710. var i = props[k];
  711. var x = obj[i];
  712. f(x, i);
  713. }
  714. };
  715. var parent = function (element) {
  716. return Option.from(element.dom().parentNode).map(Element.fromDom);
  717. };
  718. var children = function (element) {
  719. return map(element.dom().childNodes, Element.fromDom);
  720. };
  721. var child = function (element, index) {
  722. var cs = element.dom().childNodes;
  723. return Option.from(cs[index]).map(Element.fromDom);
  724. };
  725. var firstChild = function (element) {
  726. return child(element, 0);
  727. };
  728. var lastChild = function (element) {
  729. return child(element, element.dom().childNodes.length - 1);
  730. };
  731. var spot = Immutable('element', 'offset');
  732. var before = function (marker, element) {
  733. var parent$1 = parent(marker);
  734. parent$1.each(function (v) {
  735. v.dom().insertBefore(element.dom(), marker.dom());
  736. });
  737. };
  738. var append = function (parent, element) {
  739. parent.dom().appendChild(element.dom());
  740. };
  741. var before$1 = function (marker, elements) {
  742. each(elements, function (x) {
  743. before(marker, x);
  744. });
  745. };
  746. var append$1 = function (parent, elements) {
  747. each(elements, function (x) {
  748. append(parent, x);
  749. });
  750. };
  751. var remove = function (element) {
  752. var dom = element.dom();
  753. if (dom.parentNode !== null) {
  754. dom.parentNode.removeChild(dom);
  755. }
  756. };
  757. var fireListEvent = function (editor, action, element) {
  758. return editor.fire('ListMutation', {
  759. action: action,
  760. element: element
  761. });
  762. };
  763. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  764. var path = function (parts, scope) {
  765. var o = scope !== undefined && scope !== null ? scope : Global;
  766. for (var i = 0; i < parts.length && o !== undefined && o !== null; ++i) {
  767. o = o[parts[i]];
  768. }
  769. return o;
  770. };
  771. var resolve = function (p, scope) {
  772. var parts = p.split('.');
  773. return path(parts, scope);
  774. };
  775. var unsafe = function (name, scope) {
  776. return resolve(name, scope);
  777. };
  778. var getOrDie = function (name, scope) {
  779. var actual = unsafe(name, scope);
  780. if (actual === undefined || actual === null) {
  781. throw new Error(name + ' not available on this browser');
  782. }
  783. return actual;
  784. };
  785. var Global$1 = { getOrDie: getOrDie };
  786. var htmlElement = function (scope) {
  787. return Global$1.getOrDie('HTMLElement', scope);
  788. };
  789. var isPrototypeOf = function (x) {
  790. var scope = resolve('ownerDocument.defaultView', x);
  791. return htmlElement(scope).prototype.isPrototypeOf(x);
  792. };
  793. var HTMLElement = { isPrototypeOf: isPrototypeOf };
  794. var global$4 = tinymce.util.Tools.resolve('tinymce.dom.DomQuery');
  795. var global$5 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  796. var isTextNode = function (node) {
  797. return node && node.nodeType === 3;
  798. };
  799. var isListNode = function (node) {
  800. return node && /^(OL|UL|DL)$/.test(node.nodeName);
  801. };
  802. var isOlUlNode = function (node) {
  803. return node && /^(OL|UL)$/.test(node.nodeName);
  804. };
  805. var isListItemNode = function (node) {
  806. return node && /^(LI|DT|DD)$/.test(node.nodeName);
  807. };
  808. var isDlItemNode = function (node) {
  809. return node && /^(DT|DD)$/.test(node.nodeName);
  810. };
  811. var isTableCellNode = function (node) {
  812. return node && /^(TH|TD)$/.test(node.nodeName);
  813. };
  814. var isBr = function (node) {
  815. return node && node.nodeName === 'BR';
  816. };
  817. var isFirstChild = function (node) {
  818. return node.parentNode.firstChild === node;
  819. };
  820. var isLastChild = function (node) {
  821. return node.parentNode.lastChild === node;
  822. };
  823. var isTextBlock = function (editor, node) {
  824. return node && !!editor.schema.getTextBlockElements()[node.nodeName];
  825. };
  826. var isBlock = function (node, blockElements) {
  827. return node && node.nodeName in blockElements;
  828. };
  829. var isBogusBr = function (dom, node) {
  830. if (!isBr(node)) {
  831. return false;
  832. }
  833. if (dom.isBlock(node.nextSibling) && !isBr(node.previousSibling)) {
  834. return true;
  835. }
  836. return false;
  837. };
  838. var isEmpty = function (dom, elm, keepBookmarks) {
  839. var empty = dom.isEmpty(elm);
  840. if (keepBookmarks && dom.select('span[data-mce-type=bookmark]', elm).length > 0) {
  841. return false;
  842. }
  843. return empty;
  844. };
  845. var isChildOfBody = function (dom, elm) {
  846. return dom.isChildOf(elm, dom.getRoot());
  847. };
  848. var NodeType = {
  849. isTextNode: isTextNode,
  850. isListNode: isListNode,
  851. isOlUlNode: isOlUlNode,
  852. isDlItemNode: isDlItemNode,
  853. isListItemNode: isListItemNode,
  854. isTableCellNode: isTableCellNode,
  855. isBr: isBr,
  856. isFirstChild: isFirstChild,
  857. isLastChild: isLastChild,
  858. isTextBlock: isTextBlock,
  859. isBlock: isBlock,
  860. isBogusBr: isBogusBr,
  861. isEmpty: isEmpty,
  862. isChildOfBody: isChildOfBody
  863. };
  864. var getParentList = function (editor) {
  865. var selectionStart = editor.selection.getStart(true);
  866. return editor.dom.getParent(selectionStart, 'OL,UL,DL', getClosestListRootElm(editor, selectionStart));
  867. };
  868. var isParentListSelected = function (parentList, selectedBlocks) {
  869. return parentList && selectedBlocks.length === 1 && selectedBlocks[0] === parentList;
  870. };
  871. var findSubLists = function (parentList) {
  872. return global$5.grep(parentList.querySelectorAll('ol,ul,dl'), function (elm) {
  873. return NodeType.isListNode(elm);
  874. });
  875. };
  876. var getSelectedSubLists = function (editor) {
  877. var parentList = getParentList(editor);
  878. var selectedBlocks = editor.selection.getSelectedBlocks();
  879. if (isParentListSelected(parentList, selectedBlocks)) {
  880. return findSubLists(parentList);
  881. } else {
  882. return global$5.grep(selectedBlocks, function (elm) {
  883. return NodeType.isListNode(elm) && parentList !== elm;
  884. });
  885. }
  886. };
  887. var findParentListItemsNodes = function (editor, elms) {
  888. var listItemsElms = global$5.map(elms, function (elm) {
  889. var parentLi = editor.dom.getParent(elm, 'li,dd,dt', getClosestListRootElm(editor, elm));
  890. return parentLi ? parentLi : elm;
  891. });
  892. return global$4.unique(listItemsElms);
  893. };
  894. var getSelectedListItems = function (editor) {
  895. var selectedBlocks = editor.selection.getSelectedBlocks();
  896. return global$5.grep(findParentListItemsNodes(editor, selectedBlocks), function (block) {
  897. return NodeType.isListItemNode(block);
  898. });
  899. };
  900. var getSelectedDlItems = function (editor) {
  901. return filter(getSelectedListItems(editor), NodeType.isDlItemNode);
  902. };
  903. var getClosestListRootElm = function (editor, elm) {
  904. var parentTableCell = editor.dom.getParents(elm, 'TD,TH');
  905. var root = parentTableCell.length > 0 ? parentTableCell[0] : editor.getBody();
  906. return root;
  907. };
  908. var findLastParentListNode = function (editor, elm) {
  909. var parentLists = editor.dom.getParents(elm, 'ol,ul', getClosestListRootElm(editor, elm));
  910. return last(parentLists);
  911. };
  912. var getSelectedLists = function (editor) {
  913. var firstList = findLastParentListNode(editor, editor.selection.getStart());
  914. var subsequentLists = filter(editor.selection.getSelectedBlocks(), NodeType.isOlUlNode);
  915. return firstList.toArray().concat(subsequentLists);
  916. };
  917. var getSelectedListRoots = function (editor) {
  918. var selectedLists = getSelectedLists(editor);
  919. return getUniqueListRoots(editor, selectedLists);
  920. };
  921. var getUniqueListRoots = function (editor, lists) {
  922. var listRoots = map(lists, function (list) {
  923. return findLastParentListNode(editor, list).getOr(list);
  924. });
  925. return global$4.unique(listRoots);
  926. };
  927. var isList = function (editor) {
  928. var list = getParentList(editor);
  929. return HTMLElement.isPrototypeOf(list);
  930. };
  931. var Selection = {
  932. isList: isList,
  933. getParentList: getParentList,
  934. getSelectedSubLists: getSelectedSubLists,
  935. getSelectedListItems: getSelectedListItems,
  936. getClosestListRootElm: getClosestListRootElm,
  937. getSelectedDlItems: getSelectedDlItems,
  938. getSelectedListRoots: getSelectedListRoots
  939. };
  940. var createTextBlock = function (editor, contentNode) {
  941. var dom = editor.dom;
  942. var blockElements = editor.schema.getBlockElements();
  943. var fragment = dom.createFragment();
  944. var node, textBlock, blockName, hasContentNode;
  945. if (editor.settings.forced_root_block) {
  946. blockName = editor.settings.forced_root_block;
  947. }
  948. if (blockName) {
  949. textBlock = dom.create(blockName);
  950. if (textBlock.tagName === editor.settings.forced_root_block) {
  951. dom.setAttribs(textBlock, editor.settings.forced_root_block_attrs);
  952. }
  953. if (!NodeType.isBlock(contentNode.firstChild, blockElements)) {
  954. fragment.appendChild(textBlock);
  955. }
  956. }
  957. if (contentNode) {
  958. while (node = contentNode.firstChild) {
  959. var nodeName = node.nodeName;
  960. if (!hasContentNode && (nodeName !== 'SPAN' || node.getAttribute('data-mce-type') !== 'bookmark')) {
  961. hasContentNode = true;
  962. }
  963. if (NodeType.isBlock(node, blockElements)) {
  964. fragment.appendChild(node);
  965. textBlock = null;
  966. } else {
  967. if (blockName) {
  968. if (!textBlock) {
  969. textBlock = dom.create(blockName);
  970. fragment.appendChild(textBlock);
  971. }
  972. textBlock.appendChild(node);
  973. } else {
  974. fragment.appendChild(node);
  975. }
  976. }
  977. }
  978. }
  979. if (!editor.settings.forced_root_block) {
  980. fragment.appendChild(dom.create('br'));
  981. } else {
  982. if (!hasContentNode) {
  983. textBlock.appendChild(dom.create('br', { 'data-mce-bogus': '1' }));
  984. }
  985. }
  986. return fragment;
  987. };
  988. var name = function (element) {
  989. var r = element.dom().nodeName;
  990. return r.toLowerCase();
  991. };
  992. var type = function (element) {
  993. return element.dom().nodeType;
  994. };
  995. var isType$1 = function (t) {
  996. return function (element) {
  997. return type(element) === t;
  998. };
  999. };
  1000. var isElement = isType$1(ELEMENT);
  1001. var rawSet = function (dom, key, value) {
  1002. if (isString(value) || isBoolean(value) || isNumber(value)) {
  1003. dom.setAttribute(key, value + '');
  1004. } else {
  1005. domGlobals.console.error('Invalid call to Attr.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  1006. throw new Error('Attribute value was not simple');
  1007. }
  1008. };
  1009. var setAll = function (element, attrs) {
  1010. var dom = element.dom();
  1011. each$1(attrs, function (v, k) {
  1012. rawSet(dom, k, v);
  1013. });
  1014. };
  1015. var clone = function (element) {
  1016. return foldl(element.dom().attributes, function (acc, attr) {
  1017. acc[attr.name] = attr.value;
  1018. return acc;
  1019. }, {});
  1020. };
  1021. var isSupported = function (dom) {
  1022. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  1023. };
  1024. var internalSet = function (dom, property, value) {
  1025. if (!isString(value)) {
  1026. domGlobals.console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  1027. throw new Error('CSS value must be a string: ' + value);
  1028. }
  1029. if (isSupported(dom)) {
  1030. dom.style.setProperty(property, value);
  1031. }
  1032. };
  1033. var set = function (element, property, value) {
  1034. var dom = element.dom();
  1035. internalSet(dom, property, value);
  1036. };
  1037. var clone$1 = function (original, isDeep) {
  1038. return Element.fromDom(original.dom().cloneNode(isDeep));
  1039. };
  1040. var deep = function (original) {
  1041. return clone$1(original, true);
  1042. };
  1043. var shallowAs = function (original, tag) {
  1044. var nu = Element.fromTag(tag);
  1045. var attributes = clone(original);
  1046. setAll(nu, attributes);
  1047. return nu;
  1048. };
  1049. var mutate = function (original, tag) {
  1050. var nu = shallowAs(original, tag);
  1051. before(original, nu);
  1052. var children$1 = children(original);
  1053. append$1(nu, children$1);
  1054. remove(original);
  1055. return nu;
  1056. };
  1057. var joinSegment = function (parent, child) {
  1058. append(parent.item, child.list);
  1059. };
  1060. var joinSegments = function (segments) {
  1061. for (var i = 1; i < segments.length; i++) {
  1062. joinSegment(segments[i - 1], segments[i]);
  1063. }
  1064. };
  1065. var appendSegments = function (head$1, tail) {
  1066. lift2(last(head$1), head(tail), joinSegment);
  1067. };
  1068. var createSegment = function (scope, listType) {
  1069. var segment = {
  1070. list: Element.fromTag(listType, scope),
  1071. item: Element.fromTag('li', scope)
  1072. };
  1073. append(segment.list, segment.item);
  1074. return segment;
  1075. };
  1076. var createSegments = function (scope, entry, size) {
  1077. var segments = [];
  1078. for (var i = 0; i < size; i++) {
  1079. segments.push(createSegment(scope, entry.listType));
  1080. }
  1081. return segments;
  1082. };
  1083. var populateSegments = function (segments, entry) {
  1084. for (var i = 0; i < segments.length - 1; i++) {
  1085. set(segments[i].item, 'list-style-type', 'none');
  1086. }
  1087. last(segments).each(function (segment) {
  1088. setAll(segment.list, entry.listAttributes);
  1089. setAll(segment.item, entry.itemAttributes);
  1090. append$1(segment.item, entry.content);
  1091. });
  1092. };
  1093. var normalizeSegment = function (segment, entry) {
  1094. if (name(segment.list) !== entry.listType) {
  1095. segment.list = mutate(segment.list, entry.listType);
  1096. }
  1097. setAll(segment.list, entry.listAttributes);
  1098. };
  1099. var createItem = function (scope, attr, content) {
  1100. var item = Element.fromTag('li', scope);
  1101. setAll(item, attr);
  1102. append$1(item, content);
  1103. return item;
  1104. };
  1105. var appendItem = function (segment, item) {
  1106. append(segment.list, item);
  1107. segment.item = item;
  1108. };
  1109. var writeShallow = function (scope, cast, entry) {
  1110. var newCast = cast.slice(0, entry.depth);
  1111. last(newCast).each(function (segment) {
  1112. var item = createItem(scope, entry.itemAttributes, entry.content);
  1113. appendItem(segment, item);
  1114. normalizeSegment(segment, entry);
  1115. });
  1116. return newCast;
  1117. };
  1118. var writeDeep = function (scope, cast, entry) {
  1119. var segments = createSegments(scope, entry, entry.depth - cast.length);
  1120. joinSegments(segments);
  1121. populateSegments(segments, entry);
  1122. appendSegments(cast, segments);
  1123. return cast.concat(segments);
  1124. };
  1125. var composeList = function (scope, entries) {
  1126. var cast = foldl(entries, function (cast, entry) {
  1127. return entry.depth > cast.length ? writeDeep(scope, cast, entry) : writeShallow(scope, cast, entry);
  1128. }, []);
  1129. return head(cast).map(function (segment) {
  1130. return segment.list;
  1131. });
  1132. };
  1133. var isList$1 = function (el) {
  1134. return is$1(el, 'OL,UL');
  1135. };
  1136. var hasFirstChildList = function (el) {
  1137. return firstChild(el).map(isList$1).getOr(false);
  1138. };
  1139. var hasLastChildList = function (el) {
  1140. return lastChild(el).map(isList$1).getOr(false);
  1141. };
  1142. var isIndented = function (entry) {
  1143. return entry.depth > 0;
  1144. };
  1145. var isSelected = function (entry) {
  1146. return entry.isSelected;
  1147. };
  1148. var cloneItemContent = function (li) {
  1149. var children$1 = children(li);
  1150. var content = hasLastChildList(li) ? children$1.slice(0, -1) : children$1;
  1151. return map(content, deep);
  1152. };
  1153. var createEntry = function (li, depth, isSelected) {
  1154. return parent(li).filter(isElement).map(function (list) {
  1155. return {
  1156. depth: depth,
  1157. isSelected: isSelected,
  1158. content: cloneItemContent(li),
  1159. itemAttributes: clone(li),
  1160. listAttributes: clone(list),
  1161. listType: name(list)
  1162. };
  1163. });
  1164. };
  1165. var indentEntry = function (indentation, entry) {
  1166. switch (indentation) {
  1167. case 'Indent':
  1168. entry.depth++;
  1169. break;
  1170. case 'Outdent':
  1171. entry.depth--;
  1172. break;
  1173. case 'Flatten':
  1174. entry.depth = 0;
  1175. }
  1176. };
  1177. var hasOwnProperty = Object.prototype.hasOwnProperty;
  1178. var shallow = function (old, nu) {
  1179. return nu;
  1180. };
  1181. var baseMerge = function (merger) {
  1182. return function () {
  1183. var objects = new Array(arguments.length);
  1184. for (var i = 0; i < objects.length; i++) {
  1185. objects[i] = arguments[i];
  1186. }
  1187. if (objects.length === 0) {
  1188. throw new Error('Can\'t merge zero objects');
  1189. }
  1190. var ret = {};
  1191. for (var j = 0; j < objects.length; j++) {
  1192. var curObject = objects[j];
  1193. for (var key in curObject) {
  1194. if (hasOwnProperty.call(curObject, key)) {
  1195. ret[key] = merger(ret[key], curObject[key]);
  1196. }
  1197. }
  1198. }
  1199. return ret;
  1200. };
  1201. };
  1202. var merge = baseMerge(shallow);
  1203. var cloneListProperties = function (target, source) {
  1204. target.listType = source.listType;
  1205. target.listAttributes = merge({}, source.listAttributes);
  1206. };
  1207. var previousSiblingEntry = function (entries, start) {
  1208. var depth = entries[start].depth;
  1209. for (var i = start - 1; i >= 0; i--) {
  1210. if (entries[i].depth === depth) {
  1211. return Option.some(entries[i]);
  1212. }
  1213. if (entries[i].depth < depth) {
  1214. break;
  1215. }
  1216. }
  1217. return Option.none();
  1218. };
  1219. var normalizeEntries = function (entries) {
  1220. each(entries, function (entry, i) {
  1221. previousSiblingEntry(entries, i).each(function (matchingEntry) {
  1222. cloneListProperties(entry, matchingEntry);
  1223. });
  1224. });
  1225. };
  1226. var parseItem = function (depth, itemSelection, selectionState, item) {
  1227. return firstChild(item).filter(isList$1).fold(function () {
  1228. itemSelection.each(function (selection) {
  1229. if (eq(selection.start, item)) {
  1230. selectionState.set(true);
  1231. }
  1232. });
  1233. var currentItemEntry = createEntry(item, depth, selectionState.get());
  1234. itemSelection.each(function (selection) {
  1235. if (eq(selection.end, item)) {
  1236. selectionState.set(false);
  1237. }
  1238. });
  1239. var childListEntries = lastChild(item).filter(isList$1).map(function (list) {
  1240. return parseList(depth, itemSelection, selectionState, list);
  1241. }).getOr([]);
  1242. return currentItemEntry.toArray().concat(childListEntries);
  1243. }, function (list) {
  1244. return parseList(depth, itemSelection, selectionState, list);
  1245. });
  1246. };
  1247. var parseList = function (depth, itemSelection, selectionState, list) {
  1248. return bind(children(list), function (element) {
  1249. var parser = isList$1(element) ? parseList : parseItem;
  1250. var newDepth = depth + 1;
  1251. return parser(newDepth, itemSelection, selectionState, element);
  1252. });
  1253. };
  1254. var parseLists = function (lists, itemSelection) {
  1255. var selectionState = Cell(false);
  1256. var initialDepth = 0;
  1257. return map(lists, function (list) {
  1258. return {
  1259. sourceList: list,
  1260. entries: parseList(initialDepth, itemSelection, selectionState, list)
  1261. };
  1262. });
  1263. };
  1264. var outdentedComposer = function (editor, entries) {
  1265. return map(entries, function (entry) {
  1266. var content = fromElements(entry.content);
  1267. return Element.fromDom(createTextBlock(editor, content.dom()));
  1268. });
  1269. };
  1270. var indentedComposer = function (editor, entries) {
  1271. normalizeEntries(entries);
  1272. return composeList(editor.contentDocument, entries).toArray();
  1273. };
  1274. var composeEntries = function (editor, entries) {
  1275. return bind(groupBy(entries, isIndented), function (entries) {
  1276. var groupIsIndented = head(entries).map(isIndented).getOr(false);
  1277. return groupIsIndented ? indentedComposer(editor, entries) : outdentedComposer(editor, entries);
  1278. });
  1279. };
  1280. var indentSelectedEntries = function (entries, indentation) {
  1281. each(filter(entries, isSelected), function (entry) {
  1282. return indentEntry(indentation, entry);
  1283. });
  1284. };
  1285. var getItemSelection = function (editor) {
  1286. var selectedListItems = map(Selection.getSelectedListItems(editor), Element.fromDom);
  1287. return lift2(find(selectedListItems, not(hasFirstChildList)), find(reverse(selectedListItems), not(hasFirstChildList)), function (start, end) {
  1288. return {
  1289. start: start,
  1290. end: end
  1291. };
  1292. });
  1293. };
  1294. var listIndentation = function (editor, lists, indentation) {
  1295. var entrySets = parseLists(lists, getItemSelection(editor));
  1296. each(entrySets, function (entrySet) {
  1297. indentSelectedEntries(entrySet.entries, indentation);
  1298. var composedLists = composeEntries(editor, entrySet.entries);
  1299. each(composedLists, function (composedList) {
  1300. fireListEvent(editor, indentation === 'Indent' ? 'IndentList' : 'OutdentList', composedList.dom());
  1301. });
  1302. before$1(entrySet.sourceList, composedLists);
  1303. remove(entrySet.sourceList);
  1304. });
  1305. };
  1306. var global$6 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  1307. var DOM = global$6.DOM;
  1308. var splitList = function (editor, ul, li) {
  1309. var tmpRng, fragment, bookmarks, node, newBlock;
  1310. var removeAndKeepBookmarks = function (targetNode) {
  1311. global$5.each(bookmarks, function (node) {
  1312. targetNode.parentNode.insertBefore(node, li.parentNode);
  1313. });
  1314. DOM.remove(targetNode);
  1315. };
  1316. bookmarks = DOM.select('span[data-mce-type="bookmark"]', ul);
  1317. newBlock = createTextBlock(editor, li);
  1318. tmpRng = DOM.createRng();
  1319. tmpRng.setStartAfter(li);
  1320. tmpRng.setEndAfter(ul);
  1321. fragment = tmpRng.extractContents();
  1322. for (node = fragment.firstChild; node; node = node.firstChild) {
  1323. if (node.nodeName === 'LI' && editor.dom.isEmpty(node)) {
  1324. DOM.remove(node);
  1325. break;
  1326. }
  1327. }
  1328. if (!editor.dom.isEmpty(fragment)) {
  1329. DOM.insertAfter(fragment, ul);
  1330. }
  1331. DOM.insertAfter(newBlock, ul);
  1332. if (NodeType.isEmpty(editor.dom, li.parentNode)) {
  1333. removeAndKeepBookmarks(li.parentNode);
  1334. }
  1335. DOM.remove(li);
  1336. if (NodeType.isEmpty(editor.dom, ul)) {
  1337. DOM.remove(ul);
  1338. }
  1339. };
  1340. var SplitList = { splitList: splitList };
  1341. var outdentDlItem = function (editor, item) {
  1342. if (is$1(item, 'dd')) {
  1343. mutate(item, 'dt');
  1344. } else if (is$1(item, 'dt')) {
  1345. parent(item).each(function (dl) {
  1346. return SplitList.splitList(editor, dl.dom(), item.dom());
  1347. });
  1348. }
  1349. };
  1350. var indentDlItem = function (item) {
  1351. if (is$1(item, 'dt')) {
  1352. mutate(item, 'dd');
  1353. }
  1354. };
  1355. var dlIndentation = function (editor, indentation, dlItems) {
  1356. if (indentation === 'Indent') {
  1357. each(dlItems, indentDlItem);
  1358. } else {
  1359. each(dlItems, function (item) {
  1360. return outdentDlItem(editor, item);
  1361. });
  1362. }
  1363. };
  1364. var getNormalizedPoint = function (container, offset) {
  1365. if (NodeType.isTextNode(container)) {
  1366. return {
  1367. container: container,
  1368. offset: offset
  1369. };
  1370. }
  1371. var node = global$1.getNode(container, offset);
  1372. if (NodeType.isTextNode(node)) {
  1373. return {
  1374. container: node,
  1375. offset: offset >= container.childNodes.length ? node.data.length : 0
  1376. };
  1377. } else if (node.previousSibling && NodeType.isTextNode(node.previousSibling)) {
  1378. return {
  1379. container: node.previousSibling,
  1380. offset: node.previousSibling.data.length
  1381. };
  1382. } else if (node.nextSibling && NodeType.isTextNode(node.nextSibling)) {
  1383. return {
  1384. container: node.nextSibling,
  1385. offset: 0
  1386. };
  1387. }
  1388. return {
  1389. container: container,
  1390. offset: offset
  1391. };
  1392. };
  1393. var normalizeRange = function (rng) {
  1394. var outRng = rng.cloneRange();
  1395. var rangeStart = getNormalizedPoint(rng.startContainer, rng.startOffset);
  1396. outRng.setStart(rangeStart.container, rangeStart.offset);
  1397. var rangeEnd = getNormalizedPoint(rng.endContainer, rng.endOffset);
  1398. outRng.setEnd(rangeEnd.container, rangeEnd.offset);
  1399. return outRng;
  1400. };
  1401. var Range = {
  1402. getNormalizedPoint: getNormalizedPoint,
  1403. normalizeRange: normalizeRange
  1404. };
  1405. var selectionIndentation = function (editor, indentation) {
  1406. var lists = map(Selection.getSelectedListRoots(editor), Element.fromDom);
  1407. var dlItems = map(Selection.getSelectedDlItems(editor), Element.fromDom);
  1408. var isHandled = false;
  1409. if (lists.length || dlItems.length) {
  1410. var bookmark = editor.selection.getBookmark();
  1411. listIndentation(editor, lists, indentation);
  1412. dlIndentation(editor, indentation, dlItems);
  1413. editor.selection.moveToBookmark(bookmark);
  1414. editor.selection.setRng(Range.normalizeRange(editor.selection.getRng()));
  1415. editor.nodeChanged();
  1416. isHandled = true;
  1417. }
  1418. return isHandled;
  1419. };
  1420. var indentListSelection = function (editor) {
  1421. return selectionIndentation(editor, 'Indent');
  1422. };
  1423. var outdentListSelection = function (editor) {
  1424. return selectionIndentation(editor, 'Outdent');
  1425. };
  1426. var flattenListSelection = function (editor) {
  1427. return selectionIndentation(editor, 'Flatten');
  1428. };
  1429. var global$7 = tinymce.util.Tools.resolve('tinymce.dom.BookmarkManager');
  1430. var DOM$1 = global$6.DOM;
  1431. var createBookmark = function (rng) {
  1432. var bookmark = {};
  1433. var setupEndPoint = function (start) {
  1434. var offsetNode, container, offset;
  1435. container = rng[start ? 'startContainer' : 'endContainer'];
  1436. offset = rng[start ? 'startOffset' : 'endOffset'];
  1437. if (container.nodeType === 1) {
  1438. offsetNode = DOM$1.create('span', { 'data-mce-type': 'bookmark' });
  1439. if (container.hasChildNodes()) {
  1440. offset = Math.min(offset, container.childNodes.length - 1);
  1441. if (start) {
  1442. container.insertBefore(offsetNode, container.childNodes[offset]);
  1443. } else {
  1444. DOM$1.insertAfter(offsetNode, container.childNodes[offset]);
  1445. }
  1446. } else {
  1447. container.appendChild(offsetNode);
  1448. }
  1449. container = offsetNode;
  1450. offset = 0;
  1451. }
  1452. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  1453. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  1454. };
  1455. setupEndPoint(true);
  1456. if (!rng.collapsed) {
  1457. setupEndPoint();
  1458. }
  1459. return bookmark;
  1460. };
  1461. var resolveBookmark = function (bookmark) {
  1462. function restoreEndPoint(start) {
  1463. var container, offset, node;
  1464. var nodeIndex = function (container) {
  1465. var node = container.parentNode.firstChild, idx = 0;
  1466. while (node) {
  1467. if (node === container) {
  1468. return idx;
  1469. }
  1470. if (node.nodeType !== 1 || node.getAttribute('data-mce-type') !== 'bookmark') {
  1471. idx++;
  1472. }
  1473. node = node.nextSibling;
  1474. }
  1475. return -1;
  1476. };
  1477. container = node = bookmark[start ? 'startContainer' : 'endContainer'];
  1478. offset = bookmark[start ? 'startOffset' : 'endOffset'];
  1479. if (!container) {
  1480. return;
  1481. }
  1482. if (container.nodeType === 1) {
  1483. offset = nodeIndex(container);
  1484. container = container.parentNode;
  1485. DOM$1.remove(node);
  1486. if (!container.hasChildNodes() && DOM$1.isBlock(container)) {
  1487. container.appendChild(DOM$1.create('br'));
  1488. }
  1489. }
  1490. bookmark[start ? 'startContainer' : 'endContainer'] = container;
  1491. bookmark[start ? 'startOffset' : 'endOffset'] = offset;
  1492. }
  1493. restoreEndPoint(true);
  1494. restoreEndPoint();
  1495. var rng = DOM$1.createRng();
  1496. rng.setStart(bookmark.startContainer, bookmark.startOffset);
  1497. if (bookmark.endContainer) {
  1498. rng.setEnd(bookmark.endContainer, bookmark.endOffset);
  1499. }
  1500. return Range.normalizeRange(rng);
  1501. };
  1502. var Bookmark = {
  1503. createBookmark: createBookmark,
  1504. resolveBookmark: resolveBookmark
  1505. };
  1506. var isCustomList = function (list) {
  1507. return /\btox\-/.test(list.className);
  1508. };
  1509. var listToggleActionFromListName = function (listName) {
  1510. switch (listName) {
  1511. case 'UL':
  1512. return 'ToggleUlList';
  1513. case 'OL':
  1514. return 'ToggleOlList';
  1515. case 'DL':
  1516. return 'ToggleDLList';
  1517. }
  1518. };
  1519. var updateListStyle = function (dom, el, detail) {
  1520. var type = detail['list-style-type'] ? detail['list-style-type'] : null;
  1521. dom.setStyle(el, 'list-style-type', type);
  1522. };
  1523. var setAttribs = function (elm, attrs) {
  1524. global$5.each(attrs, function (value, key) {
  1525. elm.setAttribute(key, value);
  1526. });
  1527. };
  1528. var updateListAttrs = function (dom, el, detail) {
  1529. setAttribs(el, detail['list-attributes']);
  1530. global$5.each(dom.select('li', el), function (li) {
  1531. setAttribs(li, detail['list-item-attributes']);
  1532. });
  1533. };
  1534. var updateListWithDetails = function (dom, el, detail) {
  1535. updateListStyle(dom, el, detail);
  1536. updateListAttrs(dom, el, detail);
  1537. };
  1538. var removeStyles = function (dom, element, styles) {
  1539. global$5.each(styles, function (style) {
  1540. var _a;
  1541. return dom.setStyle(element, (_a = {}, _a[style] = '', _a));
  1542. });
  1543. };
  1544. var getEndPointNode = function (editor, rng, start, root) {
  1545. var container, offset;
  1546. container = rng[start ? 'startContainer' : 'endContainer'];
  1547. offset = rng[start ? 'startOffset' : 'endOffset'];
  1548. if (container.nodeType === 1) {
  1549. container = container.childNodes[Math.min(offset, container.childNodes.length - 1)] || container;
  1550. }
  1551. if (!start && NodeType.isBr(container.nextSibling)) {
  1552. container = container.nextSibling;
  1553. }
  1554. while (container.parentNode !== root) {
  1555. if (NodeType.isTextBlock(editor, container)) {
  1556. return container;
  1557. }
  1558. if (/^(TD|TH)$/.test(container.parentNode.nodeName)) {
  1559. return container;
  1560. }
  1561. container = container.parentNode;
  1562. }
  1563. return container;
  1564. };
  1565. var getSelectedTextBlocks = function (editor, rng, root) {
  1566. var textBlocks = [], dom = editor.dom;
  1567. var startNode = getEndPointNode(editor, rng, true, root);
  1568. var endNode = getEndPointNode(editor, rng, false, root);
  1569. var block;
  1570. var siblings = [];
  1571. for (var node = startNode; node; node = node.nextSibling) {
  1572. siblings.push(node);
  1573. if (node === endNode) {
  1574. break;
  1575. }
  1576. }
  1577. global$5.each(siblings, function (node) {
  1578. if (NodeType.isTextBlock(editor, node)) {
  1579. textBlocks.push(node);
  1580. block = null;
  1581. return;
  1582. }
  1583. if (dom.isBlock(node) || NodeType.isBr(node)) {
  1584. if (NodeType.isBr(node)) {
  1585. dom.remove(node);
  1586. }
  1587. block = null;
  1588. return;
  1589. }
  1590. var nextSibling = node.nextSibling;
  1591. if (global$7.isBookmarkNode(node)) {
  1592. if (NodeType.isTextBlock(editor, nextSibling) || !nextSibling && node.parentNode === root) {
  1593. block = null;
  1594. return;
  1595. }
  1596. }
  1597. if (!block) {
  1598. block = dom.create('p');
  1599. node.parentNode.insertBefore(block, node);
  1600. textBlocks.push(block);
  1601. }
  1602. block.appendChild(node);
  1603. });
  1604. return textBlocks;
  1605. };
  1606. var hasCompatibleStyle = function (dom, sib, detail) {
  1607. var sibStyle = dom.getStyle(sib, 'list-style-type');
  1608. var detailStyle = detail ? detail['list-style-type'] : '';
  1609. detailStyle = detailStyle === null ? '' : detailStyle;
  1610. return sibStyle === detailStyle;
  1611. };
  1612. var applyList = function (editor, listName, detail) {
  1613. if (detail === void 0) {
  1614. detail = {};
  1615. }
  1616. var rng = editor.selection.getRng(true);
  1617. var bookmark;
  1618. var listItemName = 'LI';
  1619. var root = Selection.getClosestListRootElm(editor, editor.selection.getStart(true));
  1620. var dom = editor.dom;
  1621. if (dom.getContentEditable(editor.selection.getNode()) === 'false') {
  1622. return;
  1623. }
  1624. listName = listName.toUpperCase();
  1625. if (listName === 'DL') {
  1626. listItemName = 'DT';
  1627. }
  1628. bookmark = Bookmark.createBookmark(rng);
  1629. global$5.each(getSelectedTextBlocks(editor, rng, root), function (block) {
  1630. var listBlock, sibling;
  1631. sibling = block.previousSibling;
  1632. if (sibling && NodeType.isListNode(sibling) && sibling.nodeName === listName && hasCompatibleStyle(dom, sibling, detail)) {
  1633. listBlock = sibling;
  1634. block = dom.rename(block, listItemName);
  1635. sibling.appendChild(block);
  1636. } else {
  1637. listBlock = dom.create(listName);
  1638. block.parentNode.insertBefore(listBlock, block);
  1639. listBlock.appendChild(block);
  1640. block = dom.rename(block, listItemName);
  1641. }
  1642. removeStyles(dom, block, [
  1643. 'margin',
  1644. 'margin-right',
  1645. 'margin-bottom',
  1646. 'margin-left',
  1647. 'margin-top',
  1648. 'padding',
  1649. 'padding-right',
  1650. 'padding-bottom',
  1651. 'padding-left',
  1652. 'padding-top'
  1653. ]);
  1654. updateListWithDetails(dom, listBlock, detail);
  1655. mergeWithAdjacentLists(editor.dom, listBlock);
  1656. });
  1657. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1658. };
  1659. var isValidLists = function (list1, list2) {
  1660. return list1 && list2 && NodeType.isListNode(list1) && list1.nodeName === list2.nodeName;
  1661. };
  1662. var hasSameListStyle = function (dom, list1, list2) {
  1663. var targetStyle = dom.getStyle(list1, 'list-style-type', true);
  1664. var style = dom.getStyle(list2, 'list-style-type', true);
  1665. return targetStyle === style;
  1666. };
  1667. var hasSameClasses = function (elm1, elm2) {
  1668. return elm1.className === elm2.className;
  1669. };
  1670. var shouldMerge = function (dom, list1, list2) {
  1671. return isValidLists(list1, list2) && hasSameListStyle(dom, list1, list2) && hasSameClasses(list1, list2);
  1672. };
  1673. var mergeWithAdjacentLists = function (dom, listBlock) {
  1674. var sibling, node;
  1675. sibling = listBlock.nextSibling;
  1676. if (shouldMerge(dom, listBlock, sibling)) {
  1677. while (node = sibling.firstChild) {
  1678. listBlock.appendChild(node);
  1679. }
  1680. dom.remove(sibling);
  1681. }
  1682. sibling = listBlock.previousSibling;
  1683. if (shouldMerge(dom, listBlock, sibling)) {
  1684. while (node = sibling.lastChild) {
  1685. listBlock.insertBefore(node, listBlock.firstChild);
  1686. }
  1687. dom.remove(sibling);
  1688. }
  1689. };
  1690. var updateList = function (editor, list, listName, detail) {
  1691. if (list.nodeName !== listName) {
  1692. var newList = editor.dom.rename(list, listName);
  1693. updateListWithDetails(editor.dom, newList, detail);
  1694. fireListEvent(editor, listToggleActionFromListName(listName), newList);
  1695. } else {
  1696. updateListWithDetails(editor.dom, list, detail);
  1697. fireListEvent(editor, listToggleActionFromListName(listName), list);
  1698. }
  1699. };
  1700. var toggleMultipleLists = function (editor, parentList, lists, listName, detail) {
  1701. if (parentList.nodeName === listName && !hasListStyleDetail(detail)) {
  1702. flattenListSelection(editor);
  1703. } else {
  1704. var bookmark = Bookmark.createBookmark(editor.selection.getRng(true));
  1705. global$5.each([parentList].concat(lists), function (elm) {
  1706. updateList(editor, elm, listName, detail);
  1707. });
  1708. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1709. }
  1710. };
  1711. var hasListStyleDetail = function (detail) {
  1712. return 'list-style-type' in detail;
  1713. };
  1714. var toggleSingleList = function (editor, parentList, listName, detail) {
  1715. if (parentList === editor.getBody()) {
  1716. return;
  1717. }
  1718. if (parentList) {
  1719. if (parentList.nodeName === listName && !hasListStyleDetail(detail) && !isCustomList(parentList)) {
  1720. flattenListSelection(editor);
  1721. } else {
  1722. var bookmark = Bookmark.createBookmark(editor.selection.getRng(true));
  1723. updateListWithDetails(editor.dom, parentList, detail);
  1724. var newList = editor.dom.rename(parentList, listName);
  1725. mergeWithAdjacentLists(editor.dom, newList);
  1726. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1727. fireListEvent(editor, listToggleActionFromListName(listName), newList);
  1728. }
  1729. } else {
  1730. applyList(editor, listName, detail);
  1731. fireListEvent(editor, listToggleActionFromListName(listName), parentList);
  1732. }
  1733. };
  1734. var toggleList = function (editor, listName, detail) {
  1735. var parentList = Selection.getParentList(editor);
  1736. var selectedSubLists = Selection.getSelectedSubLists(editor);
  1737. detail = detail ? detail : {};
  1738. if (parentList && selectedSubLists.length > 0) {
  1739. toggleMultipleLists(editor, parentList, selectedSubLists, listName, detail);
  1740. } else {
  1741. toggleSingleList(editor, parentList, listName, detail);
  1742. }
  1743. };
  1744. var ToggleList = {
  1745. toggleList: toggleList,
  1746. mergeWithAdjacentLists: mergeWithAdjacentLists
  1747. };
  1748. var DOM$2 = global$6.DOM;
  1749. var normalizeList = function (dom, ul) {
  1750. var sibling;
  1751. var parentNode = ul.parentNode;
  1752. if (parentNode.nodeName === 'LI' && parentNode.firstChild === ul) {
  1753. sibling = parentNode.previousSibling;
  1754. if (sibling && sibling.nodeName === 'LI') {
  1755. sibling.appendChild(ul);
  1756. if (NodeType.isEmpty(dom, parentNode)) {
  1757. DOM$2.remove(parentNode);
  1758. }
  1759. } else {
  1760. DOM$2.setStyle(parentNode, 'listStyleType', 'none');
  1761. }
  1762. }
  1763. if (NodeType.isListNode(parentNode)) {
  1764. sibling = parentNode.previousSibling;
  1765. if (sibling && sibling.nodeName === 'LI') {
  1766. sibling.appendChild(ul);
  1767. }
  1768. }
  1769. };
  1770. var normalizeLists = function (dom, element) {
  1771. global$5.each(global$5.grep(dom.select('ol,ul', element)), function (ul) {
  1772. normalizeList(dom, ul);
  1773. });
  1774. };
  1775. var NormalizeLists = {
  1776. normalizeList: normalizeList,
  1777. normalizeLists: normalizeLists
  1778. };
  1779. var findNextCaretContainer = function (editor, rng, isForward, root) {
  1780. var node = rng.startContainer;
  1781. var offset = rng.startOffset;
  1782. if (NodeType.isTextNode(node) && (isForward ? offset < node.data.length : offset > 0)) {
  1783. return node;
  1784. }
  1785. var nonEmptyBlocks = editor.schema.getNonEmptyElements();
  1786. if (node.nodeType === 1) {
  1787. node = global$1.getNode(node, offset);
  1788. }
  1789. var walker = new global$2(node, root);
  1790. if (isForward) {
  1791. if (NodeType.isBogusBr(editor.dom, node)) {
  1792. walker.next();
  1793. }
  1794. }
  1795. while (node = walker[isForward ? 'next' : 'prev2']()) {
  1796. if (node.nodeName === 'LI' && !node.hasChildNodes()) {
  1797. return node;
  1798. }
  1799. if (nonEmptyBlocks[node.nodeName]) {
  1800. return node;
  1801. }
  1802. if (NodeType.isTextNode(node) && node.data.length > 0) {
  1803. return node;
  1804. }
  1805. }
  1806. };
  1807. var hasOnlyOneBlockChild = function (dom, elm) {
  1808. var childNodes = elm.childNodes;
  1809. return childNodes.length === 1 && !NodeType.isListNode(childNodes[0]) && dom.isBlock(childNodes[0]);
  1810. };
  1811. var unwrapSingleBlockChild = function (dom, elm) {
  1812. if (hasOnlyOneBlockChild(dom, elm)) {
  1813. dom.remove(elm.firstChild, true);
  1814. }
  1815. };
  1816. var moveChildren = function (dom, fromElm, toElm) {
  1817. var node, targetElm;
  1818. targetElm = hasOnlyOneBlockChild(dom, toElm) ? toElm.firstChild : toElm;
  1819. unwrapSingleBlockChild(dom, fromElm);
  1820. if (!NodeType.isEmpty(dom, fromElm, true)) {
  1821. while (node = fromElm.firstChild) {
  1822. targetElm.appendChild(node);
  1823. }
  1824. }
  1825. };
  1826. var mergeLiElements = function (dom, fromElm, toElm) {
  1827. var node, listNode;
  1828. var ul = fromElm.parentNode;
  1829. if (!NodeType.isChildOfBody(dom, fromElm) || !NodeType.isChildOfBody(dom, toElm)) {
  1830. return;
  1831. }
  1832. if (NodeType.isListNode(toElm.lastChild)) {
  1833. listNode = toElm.lastChild;
  1834. }
  1835. if (ul === toElm.lastChild) {
  1836. if (NodeType.isBr(ul.previousSibling)) {
  1837. dom.remove(ul.previousSibling);
  1838. }
  1839. }
  1840. node = toElm.lastChild;
  1841. if (node && NodeType.isBr(node) && fromElm.hasChildNodes()) {
  1842. dom.remove(node);
  1843. }
  1844. if (NodeType.isEmpty(dom, toElm, true)) {
  1845. dom.$(toElm).empty();
  1846. }
  1847. moveChildren(dom, fromElm, toElm);
  1848. if (listNode) {
  1849. toElm.appendChild(listNode);
  1850. }
  1851. var contains = contains$1(Element.fromDom(toElm), Element.fromDom(fromElm));
  1852. var nestedLists = contains ? dom.getParents(fromElm, NodeType.isListNode, toElm) : [];
  1853. dom.remove(fromElm);
  1854. each(nestedLists, function (list) {
  1855. if (NodeType.isEmpty(dom, list) && list !== dom.getRoot()) {
  1856. dom.remove(list);
  1857. }
  1858. });
  1859. };
  1860. var mergeIntoEmptyLi = function (editor, fromLi, toLi) {
  1861. editor.dom.$(toLi).empty();
  1862. mergeLiElements(editor.dom, fromLi, toLi);
  1863. editor.selection.setCursorLocation(toLi);
  1864. };
  1865. var mergeForward = function (editor, rng, fromLi, toLi) {
  1866. var dom = editor.dom;
  1867. if (dom.isEmpty(toLi)) {
  1868. mergeIntoEmptyLi(editor, fromLi, toLi);
  1869. } else {
  1870. var bookmark = Bookmark.createBookmark(rng);
  1871. mergeLiElements(dom, fromLi, toLi);
  1872. editor.selection.setRng(Bookmark.resolveBookmark(bookmark));
  1873. }
  1874. };
  1875. var mergeBackward = function (editor, rng, fromLi, toLi) {
  1876. var bookmark = Bookmark.createBookmark(rng);
  1877. mergeLiElements(editor.dom, fromLi, toLi);
  1878. var resolvedBookmark = Bookmark.resolveBookmark(bookmark);
  1879. editor.selection.setRng(resolvedBookmark);
  1880. };
  1881. var backspaceDeleteFromListToListCaret = function (editor, isForward) {
  1882. var dom = editor.dom, selection = editor.selection;
  1883. var selectionStartElm = selection.getStart();
  1884. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1885. var li = dom.getParent(selection.getStart(), 'LI', root);
  1886. if (li) {
  1887. var ul = li.parentNode;
  1888. if (ul === editor.getBody() && NodeType.isEmpty(dom, ul)) {
  1889. return true;
  1890. }
  1891. var rng_1 = Range.normalizeRange(selection.getRng());
  1892. var otherLi_1 = dom.getParent(findNextCaretContainer(editor, rng_1, isForward, root), 'LI', root);
  1893. if (otherLi_1 && otherLi_1 !== li) {
  1894. editor.undoManager.transact(function () {
  1895. if (isForward) {
  1896. mergeForward(editor, rng_1, otherLi_1, li);
  1897. } else {
  1898. if (NodeType.isFirstChild(li)) {
  1899. outdentListSelection(editor);
  1900. } else {
  1901. mergeBackward(editor, rng_1, li, otherLi_1);
  1902. }
  1903. }
  1904. });
  1905. return true;
  1906. } else if (!otherLi_1) {
  1907. if (!isForward && rng_1.startOffset === 0 && rng_1.endOffset === 0) {
  1908. editor.undoManager.transact(function () {
  1909. flattenListSelection(editor);
  1910. });
  1911. return true;
  1912. }
  1913. }
  1914. }
  1915. return false;
  1916. };
  1917. var removeBlock = function (dom, block, root) {
  1918. var parentBlock = dom.getParent(block.parentNode, dom.isBlock, root);
  1919. dom.remove(block);
  1920. if (parentBlock && dom.isEmpty(parentBlock)) {
  1921. dom.remove(parentBlock);
  1922. }
  1923. };
  1924. var backspaceDeleteIntoListCaret = function (editor, isForward) {
  1925. var dom = editor.dom;
  1926. var selectionStartElm = editor.selection.getStart();
  1927. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1928. var block = dom.getParent(selectionStartElm, dom.isBlock, root);
  1929. if (block && dom.isEmpty(block)) {
  1930. var rng = Range.normalizeRange(editor.selection.getRng());
  1931. var otherLi_2 = dom.getParent(findNextCaretContainer(editor, rng, isForward, root), 'LI', root);
  1932. if (otherLi_2) {
  1933. editor.undoManager.transact(function () {
  1934. removeBlock(dom, block, root);
  1935. ToggleList.mergeWithAdjacentLists(dom, otherLi_2.parentNode);
  1936. editor.selection.select(otherLi_2, true);
  1937. editor.selection.collapse(isForward);
  1938. });
  1939. return true;
  1940. }
  1941. }
  1942. return false;
  1943. };
  1944. var backspaceDeleteCaret = function (editor, isForward) {
  1945. return backspaceDeleteFromListToListCaret(editor, isForward) || backspaceDeleteIntoListCaret(editor, isForward);
  1946. };
  1947. var backspaceDeleteRange = function (editor) {
  1948. var selectionStartElm = editor.selection.getStart();
  1949. var root = Selection.getClosestListRootElm(editor, selectionStartElm);
  1950. var startListParent = editor.dom.getParent(selectionStartElm, 'LI,DT,DD', root);
  1951. if (startListParent || Selection.getSelectedListItems(editor).length > 0) {
  1952. editor.undoManager.transact(function () {
  1953. editor.execCommand('Delete');
  1954. NormalizeLists.normalizeLists(editor.dom, editor.getBody());
  1955. });
  1956. return true;
  1957. }
  1958. return false;
  1959. };
  1960. var backspaceDelete = function (editor, isForward) {
  1961. return editor.selection.isCollapsed() ? backspaceDeleteCaret(editor, isForward) : backspaceDeleteRange(editor);
  1962. };
  1963. var setup = function (editor) {
  1964. editor.on('keydown', function (e) {
  1965. if (e.keyCode === global$3.BACKSPACE) {
  1966. if (backspaceDelete(editor, false)) {
  1967. e.preventDefault();
  1968. }
  1969. } else if (e.keyCode === global$3.DELETE) {
  1970. if (backspaceDelete(editor, true)) {
  1971. e.preventDefault();
  1972. }
  1973. }
  1974. });
  1975. };
  1976. var Delete = {
  1977. setup: setup,
  1978. backspaceDelete: backspaceDelete
  1979. };
  1980. var get = function (editor) {
  1981. return {
  1982. backspaceDelete: function (isForward) {
  1983. Delete.backspaceDelete(editor, isForward);
  1984. }
  1985. };
  1986. };
  1987. var Api = { get: get };
  1988. var queryListCommandState = function (editor, listName) {
  1989. return function () {
  1990. var parentList = editor.dom.getParent(editor.selection.getStart(), 'UL,OL,DL');
  1991. return parentList && parentList.nodeName === listName;
  1992. };
  1993. };
  1994. var register = function (editor) {
  1995. editor.on('BeforeExecCommand', function (e) {
  1996. var cmd = e.command.toLowerCase();
  1997. if (cmd === 'indent') {
  1998. indentListSelection(editor);
  1999. } else if (cmd === 'outdent') {
  2000. outdentListSelection(editor);
  2001. }
  2002. });
  2003. editor.addCommand('InsertUnorderedList', function (ui, detail) {
  2004. ToggleList.toggleList(editor, 'UL', detail);
  2005. });
  2006. editor.addCommand('InsertOrderedList', function (ui, detail) {
  2007. ToggleList.toggleList(editor, 'OL', detail);
  2008. });
  2009. editor.addCommand('InsertDefinitionList', function (ui, detail) {
  2010. ToggleList.toggleList(editor, 'DL', detail);
  2011. });
  2012. editor.addCommand('RemoveList', function () {
  2013. flattenListSelection(editor);
  2014. });
  2015. editor.addQueryStateHandler('InsertUnorderedList', queryListCommandState(editor, 'UL'));
  2016. editor.addQueryStateHandler('InsertOrderedList', queryListCommandState(editor, 'OL'));
  2017. editor.addQueryStateHandler('InsertDefinitionList', queryListCommandState(editor, 'DL'));
  2018. };
  2019. var Commands = { register: register };
  2020. var shouldIndentOnTab = function (editor) {
  2021. return editor.getParam('lists_indent_on_tab', true);
  2022. };
  2023. var Settings = { shouldIndentOnTab: shouldIndentOnTab };
  2024. var setupTabKey = function (editor) {
  2025. editor.on('keydown', function (e) {
  2026. if (e.keyCode !== global$3.TAB || global$3.metaKeyPressed(e)) {
  2027. return;
  2028. }
  2029. editor.undoManager.transact(function () {
  2030. if (e.shiftKey ? outdentListSelection(editor) : indentListSelection(editor)) {
  2031. e.preventDefault();
  2032. }
  2033. });
  2034. });
  2035. };
  2036. var setup$1 = function (editor) {
  2037. if (Settings.shouldIndentOnTab(editor)) {
  2038. setupTabKey(editor);
  2039. }
  2040. Delete.setup(editor);
  2041. };
  2042. var Keyboard = { setup: setup$1 };
  2043. var findIndex = function (list, predicate) {
  2044. for (var index = 0; index < list.length; index++) {
  2045. var element = list[index];
  2046. if (predicate(element)) {
  2047. return index;
  2048. }
  2049. }
  2050. return -1;
  2051. };
  2052. var listState = function (editor, listName) {
  2053. return function (buttonApi) {
  2054. var nodeChangeHandler = function (e) {
  2055. var tableCellIndex = findIndex(e.parents, NodeType.isTableCellNode);
  2056. var parents = tableCellIndex !== -1 ? e.parents.slice(0, tableCellIndex) : e.parents;
  2057. var lists = global$5.grep(parents, NodeType.isListNode);
  2058. buttonApi.setActive(lists.length > 0 && lists[0].nodeName === listName && !isCustomList(lists[0]));
  2059. };
  2060. editor.on('NodeChange', nodeChangeHandler);
  2061. return function () {
  2062. return editor.off('NodeChange', nodeChangeHandler);
  2063. };
  2064. };
  2065. };
  2066. var register$1 = function (editor) {
  2067. var hasPlugin = function (editor, plugin) {
  2068. var plugins = editor.settings.plugins ? editor.settings.plugins : '';
  2069. return global$5.inArray(plugins.split(/[ ,]/), plugin) !== -1;
  2070. };
  2071. var exec = function (command) {
  2072. return function () {
  2073. return editor.execCommand(command);
  2074. };
  2075. };
  2076. if (!hasPlugin(editor, 'advlist')) {
  2077. editor.ui.registry.addToggleButton('numlist', {
  2078. icon: 'ordered-list',
  2079. active: false,
  2080. tooltip: 'Numbered list',
  2081. onAction: exec('InsertOrderedList'),
  2082. onSetup: listState(editor, 'OL')
  2083. });
  2084. editor.ui.registry.addToggleButton('bullist', {
  2085. icon: 'unordered-list',
  2086. active: false,
  2087. tooltip: 'Bullet list',
  2088. onAction: exec('InsertUnorderedList'),
  2089. onSetup: listState(editor, 'UL')
  2090. });
  2091. }
  2092. };
  2093. var Buttons = { register: register$1 };
  2094. function Plugin () {
  2095. global.add('lists', function (editor) {
  2096. Keyboard.setup(editor);
  2097. Buttons.register(editor);
  2098. Commands.register(editor);
  2099. return Api.get(editor);
  2100. });
  2101. }
  2102. Plugin();
  2103. }(window));