tinymce_plugins_searchreplace.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. import {
  2. __commonJS,
  3. init_define_APP_INFO
  4. } from "./chunk-XY75H3MP.js";
  5. // node_modules/tinymce/plugins/searchreplace/plugin.js
  6. var require_plugin = __commonJS({
  7. "node_modules/tinymce/plugins/searchreplace/plugin.js"() {
  8. init_define_APP_INFO();
  9. (function() {
  10. "use strict";
  11. var Cell = function(initial) {
  12. var value2 = initial;
  13. var get2 = function() {
  14. return value2;
  15. };
  16. var set2 = function(v) {
  17. value2 = v;
  18. };
  19. return {
  20. get: get2,
  21. set: set2
  22. };
  23. };
  24. var global$3 = tinymce.util.Tools.resolve("tinymce.PluginManager");
  25. var __assign = function() {
  26. __assign = Object.assign || function __assign2(t) {
  27. for (var s, i = 1, n = arguments.length; i < n; i++) {
  28. s = arguments[i];
  29. for (var p in s)
  30. if (Object.prototype.hasOwnProperty.call(s, p))
  31. t[p] = s[p];
  32. }
  33. return t;
  34. };
  35. return __assign.apply(this, arguments);
  36. };
  37. var typeOf = function(x) {
  38. var t = typeof x;
  39. if (x === null) {
  40. return "null";
  41. } else if (t === "object" && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === "Array")) {
  42. return "array";
  43. } else if (t === "object" && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === "String")) {
  44. return "string";
  45. } else {
  46. return t;
  47. }
  48. };
  49. var isType$1 = function(type2) {
  50. return function(value2) {
  51. return typeOf(value2) === type2;
  52. };
  53. };
  54. var isSimpleType = function(type2) {
  55. return function(value2) {
  56. return typeof value2 === type2;
  57. };
  58. };
  59. var isString = isType$1("string");
  60. var isArray = isType$1("array");
  61. var isBoolean = isSimpleType("boolean");
  62. var isNumber = isSimpleType("number");
  63. var noop = function() {
  64. };
  65. var constant = function(value2) {
  66. return function() {
  67. return value2;
  68. };
  69. };
  70. var identity = function(x) {
  71. return x;
  72. };
  73. var never = constant(false);
  74. var always = constant(true);
  75. var punctuationStr = "[!-#%-*,-\\/:;?@\\[-\\]_{}\xA1\xAB\xB7\xBB\xBF;\xB7\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1361-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u3008\u3009\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30\u2E31\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]";
  76. var punctuation$1 = constant(punctuationStr);
  77. var none = function() {
  78. return NONE;
  79. };
  80. var NONE = function() {
  81. var call = function(thunk) {
  82. return thunk();
  83. };
  84. var id = identity;
  85. var me = {
  86. fold: function(n, _s) {
  87. return n();
  88. },
  89. isSome: never,
  90. isNone: always,
  91. getOr: id,
  92. getOrThunk: call,
  93. getOrDie: function(msg) {
  94. throw new Error(msg || "error: getOrDie called on none.");
  95. },
  96. getOrNull: constant(null),
  97. getOrUndefined: constant(void 0),
  98. or: id,
  99. orThunk: call,
  100. map: none,
  101. each: noop,
  102. bind: none,
  103. exists: never,
  104. forall: always,
  105. filter: function() {
  106. return none();
  107. },
  108. toArray: function() {
  109. return [];
  110. },
  111. toString: constant("none()")
  112. };
  113. return me;
  114. }();
  115. var some = function(a) {
  116. var constant_a = constant(a);
  117. var self = function() {
  118. return me;
  119. };
  120. var bind2 = function(f) {
  121. return f(a);
  122. };
  123. var me = {
  124. fold: function(n, s) {
  125. return s(a);
  126. },
  127. isSome: always,
  128. isNone: never,
  129. getOr: constant_a,
  130. getOrThunk: constant_a,
  131. getOrDie: constant_a,
  132. getOrNull: constant_a,
  133. getOrUndefined: constant_a,
  134. or: self,
  135. orThunk: self,
  136. map: function(f) {
  137. return some(f(a));
  138. },
  139. each: function(f) {
  140. f(a);
  141. },
  142. bind: bind2,
  143. exists: bind2,
  144. forall: bind2,
  145. filter: function(f) {
  146. return f(a) ? me : NONE;
  147. },
  148. toArray: function() {
  149. return [a];
  150. },
  151. toString: function() {
  152. return "some(" + a + ")";
  153. }
  154. };
  155. return me;
  156. };
  157. var from = function(value2) {
  158. return value2 === null || value2 === void 0 ? NONE : some(value2);
  159. };
  160. var Optional = {
  161. some,
  162. none,
  163. from
  164. };
  165. var punctuation = punctuation$1;
  166. var global$2 = tinymce.util.Tools.resolve("tinymce.Env");
  167. var global$1 = tinymce.util.Tools.resolve("tinymce.util.Tools");
  168. var nativeSlice = Array.prototype.slice;
  169. var nativePush = Array.prototype.push;
  170. var map = function(xs, f) {
  171. var len = xs.length;
  172. var r = new Array(len);
  173. for (var i = 0; i < len; i++) {
  174. var x = xs[i];
  175. r[i] = f(x, i);
  176. }
  177. return r;
  178. };
  179. var each = function(xs, f) {
  180. for (var i = 0, len = xs.length; i < len; i++) {
  181. var x = xs[i];
  182. f(x, i);
  183. }
  184. };
  185. var eachr = function(xs, f) {
  186. for (var i = xs.length - 1; i >= 0; i--) {
  187. var x = xs[i];
  188. f(x, i);
  189. }
  190. };
  191. var groupBy = function(xs, f) {
  192. if (xs.length === 0) {
  193. return [];
  194. } else {
  195. var wasType = f(xs[0]);
  196. var r = [];
  197. var group = [];
  198. for (var i = 0, len = xs.length; i < len; i++) {
  199. var x = xs[i];
  200. var type2 = f(x);
  201. if (type2 !== wasType) {
  202. r.push(group);
  203. group = [];
  204. }
  205. wasType = type2;
  206. group.push(x);
  207. }
  208. if (group.length !== 0) {
  209. r.push(group);
  210. }
  211. return r;
  212. }
  213. };
  214. var foldl = function(xs, f, acc) {
  215. each(xs, function(x, i) {
  216. acc = f(acc, x, i);
  217. });
  218. return acc;
  219. };
  220. var flatten = function(xs) {
  221. var r = [];
  222. for (var i = 0, len = xs.length; i < len; ++i) {
  223. if (!isArray(xs[i])) {
  224. throw new Error("Arr.flatten item " + i + " was not an array, input: " + xs);
  225. }
  226. nativePush.apply(r, xs[i]);
  227. }
  228. return r;
  229. };
  230. var bind = function(xs, f) {
  231. return flatten(map(xs, f));
  232. };
  233. var sort = function(xs, comparator) {
  234. var copy = nativeSlice.call(xs, 0);
  235. copy.sort(comparator);
  236. return copy;
  237. };
  238. var hasOwnProperty = Object.hasOwnProperty;
  239. var has = function(obj, key) {
  240. return hasOwnProperty.call(obj, key);
  241. };
  242. typeof window !== "undefined" ? window : Function("return this;")();
  243. var DOCUMENT = 9;
  244. var DOCUMENT_FRAGMENT = 11;
  245. var ELEMENT = 1;
  246. var TEXT = 3;
  247. var type = function(element) {
  248. return element.dom.nodeType;
  249. };
  250. var isType = function(t) {
  251. return function(element) {
  252. return type(element) === t;
  253. };
  254. };
  255. var isText$1 = isType(TEXT);
  256. var rawSet = function(dom, key, value2) {
  257. if (isString(value2) || isBoolean(value2) || isNumber(value2)) {
  258. dom.setAttribute(key, value2 + "");
  259. } else {
  260. console.error("Invalid call to Attribute.set. Key ", key, ":: Value ", value2, ":: Element ", dom);
  261. throw new Error("Attribute value was not simple");
  262. }
  263. };
  264. var set = function(element, key, value2) {
  265. rawSet(element.dom, key, value2);
  266. };
  267. var compareDocumentPosition = function(a, b, match) {
  268. return (a.compareDocumentPosition(b) & match) !== 0;
  269. };
  270. var documentPositionPreceding = function(a, b) {
  271. return compareDocumentPosition(a, b, Node.DOCUMENT_POSITION_PRECEDING);
  272. };
  273. var fromHtml = function(html, scope) {
  274. var doc = scope || document;
  275. var div = doc.createElement("div");
  276. div.innerHTML = html;
  277. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  278. console.error("HTML does not have a single root node", html);
  279. throw new Error("HTML must have a single root node");
  280. }
  281. return fromDom(div.childNodes[0]);
  282. };
  283. var fromTag = function(tag, scope) {
  284. var doc = scope || document;
  285. var node = doc.createElement(tag);
  286. return fromDom(node);
  287. };
  288. var fromText = function(text, scope) {
  289. var doc = scope || document;
  290. var node = doc.createTextNode(text);
  291. return fromDom(node);
  292. };
  293. var fromDom = function(node) {
  294. if (node === null || node === void 0) {
  295. throw new Error("Node cannot be null or undefined");
  296. }
  297. return { dom: node };
  298. };
  299. var fromPoint = function(docElm, x, y) {
  300. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  301. };
  302. var SugarElement = {
  303. fromHtml,
  304. fromTag,
  305. fromText,
  306. fromDom,
  307. fromPoint
  308. };
  309. var bypassSelector = function(dom) {
  310. return dom.nodeType !== ELEMENT && dom.nodeType !== DOCUMENT && dom.nodeType !== DOCUMENT_FRAGMENT || dom.childElementCount === 0;
  311. };
  312. var all = function(selector, scope) {
  313. var base = scope === void 0 ? document : scope.dom;
  314. return bypassSelector(base) ? [] : map(base.querySelectorAll(selector), SugarElement.fromDom);
  315. };
  316. var parent = function(element) {
  317. return Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  318. };
  319. var children = function(element) {
  320. return map(element.dom.childNodes, SugarElement.fromDom);
  321. };
  322. var spot = function(element, offset) {
  323. return {
  324. element,
  325. offset
  326. };
  327. };
  328. var leaf = function(element, offset) {
  329. var cs = children(element);
  330. return cs.length > 0 && offset < cs.length ? spot(cs[offset], 0) : spot(element, offset);
  331. };
  332. var before = function(marker, element) {
  333. var parent$1 = parent(marker);
  334. parent$1.each(function(v) {
  335. v.dom.insertBefore(element.dom, marker.dom);
  336. });
  337. };
  338. var append = function(parent2, element) {
  339. parent2.dom.appendChild(element.dom);
  340. };
  341. var wrap = function(element, wrapper) {
  342. before(element, wrapper);
  343. append(wrapper, element);
  344. };
  345. var NodeValue = function(is, name) {
  346. var get2 = function(element) {
  347. if (!is(element)) {
  348. throw new Error("Can only get " + name + " value of a " + name + " node");
  349. }
  350. return getOption(element).getOr("");
  351. };
  352. var getOption = function(element) {
  353. return is(element) ? Optional.from(element.dom.nodeValue) : Optional.none();
  354. };
  355. var set2 = function(element, value2) {
  356. if (!is(element)) {
  357. throw new Error("Can only set raw " + name + " value of a " + name + " node");
  358. }
  359. element.dom.nodeValue = value2;
  360. };
  361. return {
  362. get: get2,
  363. getOption,
  364. set: set2
  365. };
  366. };
  367. var api = NodeValue(isText$1, "text");
  368. var get$1 = function(element) {
  369. return api.get(element);
  370. };
  371. var descendants = function(scope, selector) {
  372. return all(selector, scope);
  373. };
  374. var global = tinymce.util.Tools.resolve("tinymce.dom.TreeWalker");
  375. var isSimpleBoundary = function(dom, node) {
  376. return dom.isBlock(node) || has(dom.schema.getShortEndedElements(), node.nodeName);
  377. };
  378. var isContentEditableFalse = function(dom, node) {
  379. return dom.getContentEditable(node) === "false";
  380. };
  381. var isContentEditableTrueInCef = function(dom, node) {
  382. return dom.getContentEditable(node) === "true" && dom.getContentEditableParent(node.parentNode) === "false";
  383. };
  384. var isHidden = function(dom, node) {
  385. return !dom.isBlock(node) && has(dom.schema.getWhiteSpaceElements(), node.nodeName);
  386. };
  387. var isBoundary = function(dom, node) {
  388. return isSimpleBoundary(dom, node) || isContentEditableFalse(dom, node) || isHidden(dom, node) || isContentEditableTrueInCef(dom, node);
  389. };
  390. var isText = function(node) {
  391. return node.nodeType === 3;
  392. };
  393. var nuSection = function() {
  394. return {
  395. sOffset: 0,
  396. fOffset: 0,
  397. elements: []
  398. };
  399. };
  400. var toLeaf = function(node, offset) {
  401. return leaf(SugarElement.fromDom(node), offset);
  402. };
  403. var walk = function(dom, walkerFn, startNode, callbacks, endNode, skipStart) {
  404. if (skipStart === void 0) {
  405. skipStart = true;
  406. }
  407. var next2 = skipStart ? walkerFn(false) : startNode;
  408. while (next2) {
  409. var isCefNode = isContentEditableFalse(dom, next2);
  410. if (isCefNode || isHidden(dom, next2)) {
  411. var stopWalking = isCefNode ? callbacks.cef(next2) : callbacks.boundary(next2);
  412. if (stopWalking) {
  413. break;
  414. } else {
  415. next2 = walkerFn(true);
  416. continue;
  417. }
  418. } else if (isSimpleBoundary(dom, next2)) {
  419. if (callbacks.boundary(next2)) {
  420. break;
  421. }
  422. } else if (isText(next2)) {
  423. callbacks.text(next2);
  424. }
  425. if (next2 === endNode) {
  426. break;
  427. } else {
  428. next2 = walkerFn(false);
  429. }
  430. }
  431. };
  432. var collectTextToBoundary = function(dom, section, node, rootNode, forwards) {
  433. if (isBoundary(dom, node)) {
  434. return;
  435. }
  436. var rootBlock = dom.getParent(rootNode, dom.isBlock);
  437. var walker = new global(node, rootBlock);
  438. var walkerFn = forwards ? walker.next.bind(walker) : walker.prev.bind(walker);
  439. walk(dom, walkerFn, node, {
  440. boundary: always,
  441. cef: always,
  442. text: function(next2) {
  443. if (forwards) {
  444. section.fOffset += next2.length;
  445. } else {
  446. section.sOffset += next2.length;
  447. }
  448. section.elements.push(SugarElement.fromDom(next2));
  449. }
  450. });
  451. };
  452. var collect = function(dom, rootNode, startNode, endNode, callbacks, skipStart) {
  453. if (skipStart === void 0) {
  454. skipStart = true;
  455. }
  456. var walker = new global(startNode, rootNode);
  457. var sections = [];
  458. var current = nuSection();
  459. collectTextToBoundary(dom, current, startNode, rootNode, false);
  460. var finishSection = function() {
  461. if (current.elements.length > 0) {
  462. sections.push(current);
  463. current = nuSection();
  464. }
  465. return false;
  466. };
  467. walk(dom, walker.next.bind(walker), startNode, {
  468. boundary: finishSection,
  469. cef: function(node) {
  470. finishSection();
  471. if (callbacks) {
  472. sections.push.apply(sections, callbacks.cef(node));
  473. }
  474. return false;
  475. },
  476. text: function(next2) {
  477. current.elements.push(SugarElement.fromDom(next2));
  478. if (callbacks) {
  479. callbacks.text(next2, current);
  480. }
  481. }
  482. }, endNode, skipStart);
  483. if (endNode) {
  484. collectTextToBoundary(dom, current, endNode, rootNode, true);
  485. }
  486. finishSection();
  487. return sections;
  488. };
  489. var collectRangeSections = function(dom, rng) {
  490. var start = toLeaf(rng.startContainer, rng.startOffset);
  491. var startNode = start.element.dom;
  492. var end = toLeaf(rng.endContainer, rng.endOffset);
  493. var endNode = end.element.dom;
  494. return collect(dom, rng.commonAncestorContainer, startNode, endNode, {
  495. text: function(node, section) {
  496. if (node === endNode) {
  497. section.fOffset += node.length - end.offset;
  498. } else if (node === startNode) {
  499. section.sOffset += start.offset;
  500. }
  501. },
  502. cef: function(node) {
  503. var sections = bind(descendants(SugarElement.fromDom(node), "*[contenteditable=true]"), function(e) {
  504. var ceTrueNode = e.dom;
  505. return collect(dom, ceTrueNode, ceTrueNode);
  506. });
  507. return sort(sections, function(a, b) {
  508. return documentPositionPreceding(a.elements[0].dom, b.elements[0].dom) ? 1 : -1;
  509. });
  510. }
  511. }, false);
  512. };
  513. var fromRng = function(dom, rng) {
  514. return rng.collapsed ? [] : collectRangeSections(dom, rng);
  515. };
  516. var fromNode = function(dom, node) {
  517. var rng = dom.createRng();
  518. rng.selectNode(node);
  519. return fromRng(dom, rng);
  520. };
  521. var fromNodes = function(dom, nodes) {
  522. return bind(nodes, function(node) {
  523. return fromNode(dom, node);
  524. });
  525. };
  526. var find$2 = function(text, pattern, start, finish) {
  527. if (start === void 0) {
  528. start = 0;
  529. }
  530. if (finish === void 0) {
  531. finish = text.length;
  532. }
  533. var regex = pattern.regex;
  534. regex.lastIndex = start;
  535. var results = [];
  536. var match;
  537. while (match = regex.exec(text)) {
  538. var matchedText = match[pattern.matchIndex];
  539. var matchStart = match.index + match[0].indexOf(matchedText);
  540. var matchFinish = matchStart + matchedText.length;
  541. if (matchFinish > finish) {
  542. break;
  543. }
  544. results.push({
  545. start: matchStart,
  546. finish: matchFinish
  547. });
  548. regex.lastIndex = matchFinish;
  549. }
  550. return results;
  551. };
  552. var extract = function(elements, matches) {
  553. var nodePositions = foldl(elements, function(acc, element) {
  554. var content = get$1(element);
  555. var start = acc.last;
  556. var finish = start + content.length;
  557. var positions = bind(matches, function(match, matchIdx) {
  558. if (match.start < finish && match.finish > start) {
  559. return [{
  560. element,
  561. start: Math.max(start, match.start) - start,
  562. finish: Math.min(finish, match.finish) - start,
  563. matchId: matchIdx
  564. }];
  565. } else {
  566. return [];
  567. }
  568. });
  569. return {
  570. results: acc.results.concat(positions),
  571. last: finish
  572. };
  573. }, {
  574. results: [],
  575. last: 0
  576. }).results;
  577. return groupBy(nodePositions, function(position) {
  578. return position.matchId;
  579. });
  580. };
  581. var find$1 = function(pattern, sections) {
  582. return bind(sections, function(section) {
  583. var elements = section.elements;
  584. var content = map(elements, get$1).join("");
  585. var positions = find$2(content, pattern, section.sOffset, content.length - section.fOffset);
  586. return extract(elements, positions);
  587. });
  588. };
  589. var mark = function(matches, replacementNode) {
  590. eachr(matches, function(match, idx) {
  591. eachr(match, function(pos) {
  592. var wrapper = SugarElement.fromDom(replacementNode.cloneNode(false));
  593. set(wrapper, "data-mce-index", idx);
  594. var textNode = pos.element.dom;
  595. if (textNode.length === pos.finish && pos.start === 0) {
  596. wrap(pos.element, wrapper);
  597. } else {
  598. if (textNode.length !== pos.finish) {
  599. textNode.splitText(pos.finish);
  600. }
  601. var matchNode = textNode.splitText(pos.start);
  602. wrap(SugarElement.fromDom(matchNode), wrapper);
  603. }
  604. });
  605. });
  606. };
  607. var findAndMark = function(dom, pattern, node, replacementNode) {
  608. var textSections = fromNode(dom, node);
  609. var matches = find$1(pattern, textSections);
  610. mark(matches, replacementNode);
  611. return matches.length;
  612. };
  613. var findAndMarkInSelection = function(dom, pattern, selection, replacementNode) {
  614. var bookmark = selection.getBookmark();
  615. var nodes = dom.select("td[data-mce-selected],th[data-mce-selected]");
  616. var textSections = nodes.length > 0 ? fromNodes(dom, nodes) : fromRng(dom, selection.getRng());
  617. var matches = find$1(pattern, textSections);
  618. mark(matches, replacementNode);
  619. selection.moveToBookmark(bookmark);
  620. return matches.length;
  621. };
  622. var getElmIndex = function(elm) {
  623. var value2 = elm.getAttribute("data-mce-index");
  624. if (typeof value2 === "number") {
  625. return "" + value2;
  626. }
  627. return value2;
  628. };
  629. var markAllMatches = function(editor, currentSearchState, pattern, inSelection) {
  630. var marker = editor.dom.create("span", { "data-mce-bogus": 1 });
  631. marker.className = "mce-match-marker";
  632. var node = editor.getBody();
  633. done(editor, currentSearchState, false);
  634. if (inSelection) {
  635. return findAndMarkInSelection(editor.dom, pattern, editor.selection, marker);
  636. } else {
  637. return findAndMark(editor.dom, pattern, node, marker);
  638. }
  639. };
  640. var unwrap = function(node) {
  641. var parentNode = node.parentNode;
  642. if (node.firstChild) {
  643. parentNode.insertBefore(node.firstChild, node);
  644. }
  645. node.parentNode.removeChild(node);
  646. };
  647. var findSpansByIndex = function(editor, index) {
  648. var spans = [];
  649. var nodes = global$1.toArray(editor.getBody().getElementsByTagName("span"));
  650. if (nodes.length) {
  651. for (var i = 0; i < nodes.length; i++) {
  652. var nodeIndex = getElmIndex(nodes[i]);
  653. if (nodeIndex === null || !nodeIndex.length) {
  654. continue;
  655. }
  656. if (nodeIndex === index.toString()) {
  657. spans.push(nodes[i]);
  658. }
  659. }
  660. }
  661. return spans;
  662. };
  663. var moveSelection = function(editor, currentSearchState, forward) {
  664. var searchState = currentSearchState.get();
  665. var testIndex = searchState.index;
  666. var dom = editor.dom;
  667. forward = forward !== false;
  668. if (forward) {
  669. if (testIndex + 1 === searchState.count) {
  670. testIndex = 0;
  671. } else {
  672. testIndex++;
  673. }
  674. } else {
  675. if (testIndex - 1 === -1) {
  676. testIndex = searchState.count - 1;
  677. } else {
  678. testIndex--;
  679. }
  680. }
  681. dom.removeClass(findSpansByIndex(editor, searchState.index), "mce-match-marker-selected");
  682. var spans = findSpansByIndex(editor, testIndex);
  683. if (spans.length) {
  684. dom.addClass(findSpansByIndex(editor, testIndex), "mce-match-marker-selected");
  685. editor.selection.scrollIntoView(spans[0]);
  686. return testIndex;
  687. }
  688. return -1;
  689. };
  690. var removeNode = function(dom, node) {
  691. var parent2 = node.parentNode;
  692. dom.remove(node);
  693. if (dom.isEmpty(parent2)) {
  694. dom.remove(parent2);
  695. }
  696. };
  697. var escapeSearchText = function(text, wholeWord) {
  698. var escapedText = text.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&").replace(/\s/g, "[^\\S\\r\\n\\uFEFF]");
  699. var wordRegex = "(" + escapedText + ")";
  700. return wholeWord ? "(?:^|\\s|" + punctuation() + ")" + wordRegex + ("(?=$|\\s|" + punctuation() + ")") : wordRegex;
  701. };
  702. var find = function(editor, currentSearchState, text, matchCase, wholeWord, inSelection) {
  703. var selection = editor.selection;
  704. var escapedText = escapeSearchText(text, wholeWord);
  705. var isForwardSelection = selection.isForward();
  706. var pattern = {
  707. regex: new RegExp(escapedText, matchCase ? "g" : "gi"),
  708. matchIndex: 1
  709. };
  710. var count = markAllMatches(editor, currentSearchState, pattern, inSelection);
  711. if (global$2.browser.isSafari()) {
  712. selection.setRng(selection.getRng(), isForwardSelection);
  713. }
  714. if (count) {
  715. var newIndex = moveSelection(editor, currentSearchState, true);
  716. currentSearchState.set({
  717. index: newIndex,
  718. count,
  719. text,
  720. matchCase,
  721. wholeWord,
  722. inSelection
  723. });
  724. }
  725. return count;
  726. };
  727. var next = function(editor, currentSearchState) {
  728. var index = moveSelection(editor, currentSearchState, true);
  729. currentSearchState.set(__assign(__assign({}, currentSearchState.get()), { index }));
  730. };
  731. var prev = function(editor, currentSearchState) {
  732. var index = moveSelection(editor, currentSearchState, false);
  733. currentSearchState.set(__assign(__assign({}, currentSearchState.get()), { index }));
  734. };
  735. var isMatchSpan = function(node) {
  736. var matchIndex = getElmIndex(node);
  737. return matchIndex !== null && matchIndex.length > 0;
  738. };
  739. var replace = function(editor, currentSearchState, text, forward, all2) {
  740. var searchState = currentSearchState.get();
  741. var currentIndex = searchState.index;
  742. var currentMatchIndex, nextIndex = currentIndex;
  743. forward = forward !== false;
  744. var node = editor.getBody();
  745. var nodes = global$1.grep(global$1.toArray(node.getElementsByTagName("span")), isMatchSpan);
  746. for (var i = 0; i < nodes.length; i++) {
  747. var nodeIndex = getElmIndex(nodes[i]);
  748. var matchIndex = currentMatchIndex = parseInt(nodeIndex, 10);
  749. if (all2 || matchIndex === searchState.index) {
  750. if (text.length) {
  751. nodes[i].firstChild.nodeValue = text;
  752. unwrap(nodes[i]);
  753. } else {
  754. removeNode(editor.dom, nodes[i]);
  755. }
  756. while (nodes[++i]) {
  757. matchIndex = parseInt(getElmIndex(nodes[i]), 10);
  758. if (matchIndex === currentMatchIndex) {
  759. removeNode(editor.dom, nodes[i]);
  760. } else {
  761. i--;
  762. break;
  763. }
  764. }
  765. if (forward) {
  766. nextIndex--;
  767. }
  768. } else if (currentMatchIndex > currentIndex) {
  769. nodes[i].setAttribute("data-mce-index", String(currentMatchIndex - 1));
  770. }
  771. }
  772. currentSearchState.set(__assign(__assign({}, searchState), {
  773. count: all2 ? 0 : searchState.count - 1,
  774. index: nextIndex
  775. }));
  776. if (forward) {
  777. next(editor, currentSearchState);
  778. } else {
  779. prev(editor, currentSearchState);
  780. }
  781. return !all2 && currentSearchState.get().count > 0;
  782. };
  783. var done = function(editor, currentSearchState, keepEditorSelection) {
  784. var startContainer, endContainer;
  785. var searchState = currentSearchState.get();
  786. var nodes = global$1.toArray(editor.getBody().getElementsByTagName("span"));
  787. for (var i = 0; i < nodes.length; i++) {
  788. var nodeIndex = getElmIndex(nodes[i]);
  789. if (nodeIndex !== null && nodeIndex.length) {
  790. if (nodeIndex === searchState.index.toString()) {
  791. if (!startContainer) {
  792. startContainer = nodes[i].firstChild;
  793. }
  794. endContainer = nodes[i].firstChild;
  795. }
  796. unwrap(nodes[i]);
  797. }
  798. }
  799. currentSearchState.set(__assign(__assign({}, searchState), {
  800. index: -1,
  801. count: 0,
  802. text: ""
  803. }));
  804. if (startContainer && endContainer) {
  805. var rng = editor.dom.createRng();
  806. rng.setStart(startContainer, 0);
  807. rng.setEnd(endContainer, endContainer.data.length);
  808. if (keepEditorSelection !== false) {
  809. editor.selection.setRng(rng);
  810. }
  811. return rng;
  812. }
  813. };
  814. var hasNext = function(editor, currentSearchState) {
  815. return currentSearchState.get().count > 1;
  816. };
  817. var hasPrev = function(editor, currentSearchState) {
  818. return currentSearchState.get().count > 1;
  819. };
  820. var get = function(editor, currentState) {
  821. var done$1 = function(keepEditorSelection) {
  822. return done(editor, currentState, keepEditorSelection);
  823. };
  824. var find$12 = function(text, matchCase, wholeWord, inSelection) {
  825. if (inSelection === void 0) {
  826. inSelection = false;
  827. }
  828. return find(editor, currentState, text, matchCase, wholeWord, inSelection);
  829. };
  830. var next$1 = function() {
  831. return next(editor, currentState);
  832. };
  833. var prev$1 = function() {
  834. return prev(editor, currentState);
  835. };
  836. var replace$1 = function(text, forward, all2) {
  837. return replace(editor, currentState, text, forward, all2);
  838. };
  839. return {
  840. done: done$1,
  841. find: find$12,
  842. next: next$1,
  843. prev: prev$1,
  844. replace: replace$1
  845. };
  846. };
  847. var singleton = function(doRevoke) {
  848. var subject = Cell(Optional.none());
  849. var revoke = function() {
  850. return subject.get().each(doRevoke);
  851. };
  852. var clear = function() {
  853. revoke();
  854. subject.set(Optional.none());
  855. };
  856. var isSet = function() {
  857. return subject.get().isSome();
  858. };
  859. var get2 = function() {
  860. return subject.get();
  861. };
  862. var set2 = function(s) {
  863. revoke();
  864. subject.set(Optional.some(s));
  865. };
  866. return {
  867. clear,
  868. isSet,
  869. get: get2,
  870. set: set2
  871. };
  872. };
  873. var value = function() {
  874. var subject = singleton(noop);
  875. var on = function(f) {
  876. return subject.get().each(f);
  877. };
  878. return __assign(__assign({}, subject), { on });
  879. };
  880. var open = function(editor, currentSearchState) {
  881. var dialogApi = value();
  882. editor.undoManager.add();
  883. var selectedText = global$1.trim(editor.selection.getContent({ format: "text" }));
  884. var updateButtonStates = function(api2) {
  885. var updateNext = hasNext(editor, currentSearchState) ? api2.enable : api2.disable;
  886. updateNext("next");
  887. var updatePrev = hasPrev(editor, currentSearchState) ? api2.enable : api2.disable;
  888. updatePrev("prev");
  889. };
  890. var updateSearchState = function(api2) {
  891. var data = api2.getData();
  892. var current = currentSearchState.get();
  893. currentSearchState.set(__assign(__assign({}, current), {
  894. matchCase: data.matchcase,
  895. wholeWord: data.wholewords,
  896. inSelection: data.inselection
  897. }));
  898. };
  899. var disableAll = function(api2, disable) {
  900. var buttons = [
  901. "replace",
  902. "replaceall",
  903. "prev",
  904. "next"
  905. ];
  906. var toggle = disable ? api2.disable : api2.enable;
  907. each(buttons, toggle);
  908. };
  909. var notFoundAlert = function(api2) {
  910. editor.windowManager.alert("Could not find the specified string.", function() {
  911. api2.focus("findtext");
  912. });
  913. };
  914. var focusButtonIfRequired = function(api2, name) {
  915. if (global$2.browser.isSafari() && global$2.deviceType.isTouch() && (name === "find" || name === "replace" || name === "replaceall")) {
  916. api2.focus(name);
  917. }
  918. };
  919. var reset = function(api2) {
  920. done(editor, currentSearchState, false);
  921. disableAll(api2, true);
  922. updateButtonStates(api2);
  923. };
  924. var doFind = function(api2) {
  925. var data = api2.getData();
  926. var last = currentSearchState.get();
  927. if (!data.findtext.length) {
  928. reset(api2);
  929. return;
  930. }
  931. if (last.text === data.findtext && last.matchCase === data.matchcase && last.wholeWord === data.wholewords) {
  932. next(editor, currentSearchState);
  933. } else {
  934. var count = find(editor, currentSearchState, data.findtext, data.matchcase, data.wholewords, data.inselection);
  935. if (count <= 0) {
  936. notFoundAlert(api2);
  937. }
  938. disableAll(api2, count === 0);
  939. }
  940. updateButtonStates(api2);
  941. };
  942. var initialState = currentSearchState.get();
  943. var initialData = {
  944. findtext: selectedText,
  945. replacetext: "",
  946. wholewords: initialState.wholeWord,
  947. matchcase: initialState.matchCase,
  948. inselection: initialState.inSelection
  949. };
  950. var spec = {
  951. title: "Find and Replace",
  952. size: "normal",
  953. body: {
  954. type: "panel",
  955. items: [
  956. {
  957. type: "bar",
  958. items: [
  959. {
  960. type: "input",
  961. name: "findtext",
  962. placeholder: "Find",
  963. maximized: true,
  964. inputMode: "search"
  965. },
  966. {
  967. type: "button",
  968. name: "prev",
  969. text: "Previous",
  970. icon: "action-prev",
  971. disabled: true,
  972. borderless: true
  973. },
  974. {
  975. type: "button",
  976. name: "next",
  977. text: "Next",
  978. icon: "action-next",
  979. disabled: true,
  980. borderless: true
  981. }
  982. ]
  983. },
  984. {
  985. type: "input",
  986. name: "replacetext",
  987. placeholder: "Replace with",
  988. inputMode: "search"
  989. }
  990. ]
  991. },
  992. buttons: [
  993. {
  994. type: "menu",
  995. name: "options",
  996. icon: "preferences",
  997. tooltip: "Preferences",
  998. align: "start",
  999. items: [
  1000. {
  1001. type: "togglemenuitem",
  1002. name: "matchcase",
  1003. text: "Match case"
  1004. },
  1005. {
  1006. type: "togglemenuitem",
  1007. name: "wholewords",
  1008. text: "Find whole words only"
  1009. },
  1010. {
  1011. type: "togglemenuitem",
  1012. name: "inselection",
  1013. text: "Find in selection"
  1014. }
  1015. ]
  1016. },
  1017. {
  1018. type: "custom",
  1019. name: "find",
  1020. text: "Find",
  1021. primary: true
  1022. },
  1023. {
  1024. type: "custom",
  1025. name: "replace",
  1026. text: "Replace",
  1027. disabled: true
  1028. },
  1029. {
  1030. type: "custom",
  1031. name: "replaceall",
  1032. text: "Replace all",
  1033. disabled: true
  1034. }
  1035. ],
  1036. initialData,
  1037. onChange: function(api2, details) {
  1038. if (details.name === "findtext" && currentSearchState.get().count > 0) {
  1039. reset(api2);
  1040. }
  1041. },
  1042. onAction: function(api2, details) {
  1043. var data = api2.getData();
  1044. switch (details.name) {
  1045. case "find":
  1046. doFind(api2);
  1047. break;
  1048. case "replace":
  1049. if (!replace(editor, currentSearchState, data.replacetext)) {
  1050. reset(api2);
  1051. } else {
  1052. updateButtonStates(api2);
  1053. }
  1054. break;
  1055. case "replaceall":
  1056. replace(editor, currentSearchState, data.replacetext, true, true);
  1057. reset(api2);
  1058. break;
  1059. case "prev":
  1060. prev(editor, currentSearchState);
  1061. updateButtonStates(api2);
  1062. break;
  1063. case "next":
  1064. next(editor, currentSearchState);
  1065. updateButtonStates(api2);
  1066. break;
  1067. case "matchcase":
  1068. case "wholewords":
  1069. case "inselection":
  1070. updateSearchState(api2);
  1071. reset(api2);
  1072. break;
  1073. }
  1074. focusButtonIfRequired(api2, details.name);
  1075. },
  1076. onSubmit: function(api2) {
  1077. doFind(api2);
  1078. focusButtonIfRequired(api2, "find");
  1079. },
  1080. onClose: function() {
  1081. editor.focus();
  1082. done(editor, currentSearchState);
  1083. editor.undoManager.add();
  1084. }
  1085. };
  1086. dialogApi.set(editor.windowManager.open(spec, { inline: "toolbar" }));
  1087. };
  1088. var register$1 = function(editor, currentSearchState) {
  1089. editor.addCommand("SearchReplace", function() {
  1090. open(editor, currentSearchState);
  1091. });
  1092. };
  1093. var showDialog = function(editor, currentSearchState) {
  1094. return function() {
  1095. open(editor, currentSearchState);
  1096. };
  1097. };
  1098. var register = function(editor, currentSearchState) {
  1099. editor.ui.registry.addMenuItem("searchreplace", {
  1100. text: "Find and replace...",
  1101. shortcut: "Meta+F",
  1102. onAction: showDialog(editor, currentSearchState),
  1103. icon: "search"
  1104. });
  1105. editor.ui.registry.addButton("searchreplace", {
  1106. tooltip: "Find and replace",
  1107. onAction: showDialog(editor, currentSearchState),
  1108. icon: "search"
  1109. });
  1110. editor.shortcuts.add("Meta+F", "", showDialog(editor, currentSearchState));
  1111. };
  1112. function Plugin() {
  1113. global$3.add("searchreplace", function(editor) {
  1114. var currentSearchState = Cell({
  1115. index: -1,
  1116. count: 0,
  1117. text: "",
  1118. matchCase: false,
  1119. wholeWord: false,
  1120. inSelection: false
  1121. });
  1122. register$1(editor, currentSearchState);
  1123. register(editor, currentSearchState);
  1124. return get(editor, currentSearchState);
  1125. });
  1126. }
  1127. Plugin();
  1128. })();
  1129. }
  1130. });
  1131. // node_modules/tinymce/plugins/searchreplace/index.js
  1132. var require_searchreplace = __commonJS({
  1133. "node_modules/tinymce/plugins/searchreplace/index.js"() {
  1134. init_define_APP_INFO();
  1135. require_plugin();
  1136. }
  1137. });
  1138. // dep:tinymce_plugins_searchreplace
  1139. init_define_APP_INFO();
  1140. var tinymce_plugins_searchreplace_default = require_searchreplace();
  1141. export {
  1142. tinymce_plugins_searchreplace_default as default
  1143. };
  1144. //# sourceMappingURL=tinymce_plugins_searchreplace.js.map