suneditor 2.46.2 → 3.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/.eslintignore +7 -0
  2. package/.eslintrc.json +64 -0
  3. package/CONTRIBUTING.md +36 -0
  4. package/LICENSE.txt +1 -1
  5. package/README.md +11 -1560
  6. package/dist/suneditor.min.css +1 -0
  7. package/dist/suneditor.min.js +1 -2
  8. package/package.json +97 -70
  9. package/src/assets/icons/_default.js +194 -0
  10. package/src/assets/suneditor-contents.css +643 -0
  11. package/src/assets/suneditor.css +3394 -0
  12. package/src/core/base/eventHandlers/handler_toolbar.js +114 -0
  13. package/src/core/base/eventHandlers/handler_ww_clipboard.js +37 -0
  14. package/src/core/base/eventHandlers/handler_ww_dragDrop.js +74 -0
  15. package/src/core/base/eventHandlers/handler_ww_key_input.js +1002 -0
  16. package/src/core/base/eventHandlers/handler_ww_mouse.js +147 -0
  17. package/src/core/base/eventManager.js +1156 -0
  18. package/src/core/base/events.js +320 -0
  19. package/src/core/base/history.js +301 -0
  20. package/src/core/class/char.js +147 -0
  21. package/src/core/class/component.js +639 -0
  22. package/src/core/class/format.js +3258 -0
  23. package/src/core/class/html.js +1710 -0
  24. package/src/core/class/menu.js +260 -0
  25. package/src/core/class/nodeTransform.js +405 -0
  26. package/src/core/class/notice.js +42 -0
  27. package/src/core/class/offset.js +575 -0
  28. package/src/core/class/selection.js +511 -0
  29. package/src/core/class/shortcuts.js +38 -0
  30. package/src/core/class/toolbar.js +440 -0
  31. package/src/core/class/viewer.js +646 -0
  32. package/src/core/editor.js +1601 -0
  33. package/src/core/section/actives.js +145 -0
  34. package/src/core/section/constructor.js +1252 -0
  35. package/src/core/section/context.js +97 -0
  36. package/src/editorInjector/_classes.js +22 -0
  37. package/src/editorInjector/_core.js +28 -0
  38. package/src/editorInjector/index.js +13 -0
  39. package/src/helper/converter.js +388 -0
  40. package/src/helper/domUtils.js +1177 -0
  41. package/src/helper/env.js +250 -0
  42. package/src/helper/index.js +19 -0
  43. package/src/helper/numbers.js +68 -0
  44. package/src/helper/unicode.js +43 -0
  45. package/src/langs/ckb.js +161 -0
  46. package/src/langs/cs.js +161 -0
  47. package/src/langs/da.js +161 -0
  48. package/src/langs/de.js +162 -0
  49. package/src/langs/en.js +210 -0
  50. package/src/langs/es.js +162 -0
  51. package/src/langs/fa.js +159 -0
  52. package/src/langs/fr.js +161 -0
  53. package/src/langs/he.js +162 -0
  54. package/src/{lang → langs}/index.js +0 -2
  55. package/src/langs/it.js +162 -0
  56. package/src/langs/ja.js +162 -0
  57. package/src/langs/ko.js +210 -0
  58. package/src/langs/lv.js +162 -0
  59. package/src/langs/nl.js +162 -0
  60. package/src/langs/pl.js +162 -0
  61. package/src/langs/pt_br.js +162 -0
  62. package/src/langs/ro.js +162 -0
  63. package/src/langs/ru.js +162 -0
  64. package/src/langs/se.js +162 -0
  65. package/src/langs/tr.js +159 -0
  66. package/src/langs/ua.js +162 -0
  67. package/src/langs/ur.js +162 -0
  68. package/src/langs/zh_cn.js +162 -0
  69. package/src/modules/ApiManager.js +168 -0
  70. package/src/modules/ColorPicker.js +302 -0
  71. package/src/modules/Controller.js +323 -0
  72. package/src/modules/Figure.js +1176 -0
  73. package/src/modules/FileBrowser.js +271 -0
  74. package/src/modules/FileManager.js +307 -0
  75. package/src/modules/HueSlider.js +513 -0
  76. package/src/modules/Modal.js +177 -0
  77. package/src/modules/ModalAnchorEditor.js +494 -0
  78. package/src/modules/SelectMenu.js +447 -0
  79. package/src/modules/_DragHandle.js +16 -0
  80. package/src/modules/index.js +14 -0
  81. package/src/plugins/command/blockquote.js +47 -47
  82. package/src/plugins/command/exportPdf.js +168 -0
  83. package/src/plugins/command/fileUpload.js +389 -0
  84. package/src/plugins/command/list_bulleted.js +112 -0
  85. package/src/plugins/command/list_numbered.js +115 -0
  86. package/src/plugins/dropdown/align.js +143 -0
  87. package/src/plugins/dropdown/backgroundColor.js +90 -0
  88. package/src/plugins/dropdown/font.js +113 -0
  89. package/src/plugins/dropdown/fontColor.js +90 -0
  90. package/src/plugins/dropdown/formatBlock.js +141 -0
  91. package/src/plugins/dropdown/hr.js +111 -0
  92. package/src/plugins/dropdown/layout.js +72 -0
  93. package/src/plugins/dropdown/lineHeight.js +114 -0
  94. package/src/plugins/dropdown/list.js +107 -0
  95. package/src/plugins/dropdown/paragraphStyle.js +117 -0
  96. package/src/plugins/dropdown/table.js +2810 -0
  97. package/src/plugins/dropdown/template.js +71 -0
  98. package/src/plugins/dropdown/textStyle.js +137 -0
  99. package/src/plugins/field/mention.js +181 -0
  100. package/src/plugins/fileBrowser/imageGallery.js +76 -59
  101. package/src/plugins/index.js +86 -24
  102. package/src/plugins/input/fontSize.js +357 -0
  103. package/src/plugins/modal/audio.js +492 -0
  104. package/src/plugins/modal/image.js +1064 -0
  105. package/src/plugins/modal/link.js +211 -0
  106. package/src/plugins/modal/math.js +363 -0
  107. package/src/plugins/modal/video.js +870 -0
  108. package/src/suneditor.js +62 -67
  109. package/src/themes/test.css +61 -0
  110. package/typings/CommandPlugin.d.ts +8 -0
  111. package/typings/DialogPlugin.d.ts +20 -0
  112. package/typings/FileBrowserPlugin.d.ts +30 -0
  113. package/typings/Lang.d.ts +124 -0
  114. package/typings/Module.d.ts +15 -0
  115. package/typings/Plugin.d.ts +42 -0
  116. package/typings/SubmenuPlugin.d.ts +8 -0
  117. package/typings/_classes.d.ts +17 -0
  118. package/typings/_colorPicker.d.ts +60 -0
  119. package/typings/_core.d.ts +55 -0
  120. package/typings/align.d.ts +5 -0
  121. package/{src/plugins/dialog → typings}/audio.d.ts +1 -1
  122. package/typings/backgroundColor.d.ts +5 -0
  123. package/{src/plugins/command → typings}/blockquote.d.ts +1 -1
  124. package/typings/char.d.ts +39 -0
  125. package/typings/component.d.ts +38 -0
  126. package/typings/context.d.ts +39 -0
  127. package/typings/converter.d.ts +33 -0
  128. package/typings/dialog.d.ts +28 -0
  129. package/typings/domUtils.d.ts +361 -0
  130. package/typings/editor.d.ts +7 -0
  131. package/typings/editor.ts +542 -0
  132. package/typings/env.d.ts +70 -0
  133. package/typings/eventManager.d.ts +37 -0
  134. package/typings/events.d.ts +262 -0
  135. package/typings/fileBrowser.d.ts +42 -0
  136. package/typings/fileManager.d.ts +67 -0
  137. package/typings/font.d.ts +5 -0
  138. package/typings/fontColor.d.ts +5 -0
  139. package/typings/fontSize.d.ts +5 -0
  140. package/typings/format.d.ts +191 -0
  141. package/typings/formatBlock.d.ts +5 -0
  142. package/typings/history.d.ts +48 -0
  143. package/typings/horizontalRule.d.ts +5 -0
  144. package/{src/plugins/dialog → typings}/image.d.ts +1 -1
  145. package/{src/plugins/fileBrowser → typings}/imageGallery.d.ts +1 -1
  146. package/typings/index.d.ts +21 -0
  147. package/{src/plugins/modules/index.d.ts → typings/index.modules.d.ts} +3 -3
  148. package/typings/index.plugins.d.ts +58 -0
  149. package/typings/lineHeight.d.ts +5 -0
  150. package/{src/plugins/dialog → typings}/link.d.ts +1 -1
  151. package/typings/list.d.ts +5 -0
  152. package/{src/plugins/dialog → typings}/math.d.ts +1 -1
  153. package/typings/mediaContainer.d.ts +25 -0
  154. package/typings/node.d.ts +57 -0
  155. package/typings/notice.d.ts +16 -0
  156. package/typings/numbers.d.ts +29 -0
  157. package/typings/offset.d.ts +24 -0
  158. package/typings/options.d.ts +589 -0
  159. package/typings/paragraphStyle.d.ts +5 -0
  160. package/typings/resizing.d.ts +141 -0
  161. package/typings/selection.d.ts +94 -0
  162. package/typings/shortcuts.d.ts +13 -0
  163. package/typings/suneditor.d.ts +9 -0
  164. package/typings/table.d.ts +5 -0
  165. package/typings/template.d.ts +5 -0
  166. package/typings/textStyle.d.ts +5 -0
  167. package/typings/toolbar.d.ts +32 -0
  168. package/typings/unicode.d.ts +25 -0
  169. package/{src/plugins/dialog → typings}/video.d.ts +1 -1
  170. package/dist/css/suneditor.min.css +0 -1
  171. package/src/assets/css/suneditor-contents.css +0 -562
  172. package/src/assets/css/suneditor.css +0 -566
  173. package/src/assets/defaultIcons.js +0 -103
  174. package/src/lang/Lang.d.ts +0 -144
  175. package/src/lang/ckb.d.ts +0 -5
  176. package/src/lang/ckb.js +0 -188
  177. package/src/lang/cs.d.ts +0 -5
  178. package/src/lang/cs.js +0 -188
  179. package/src/lang/da.d.ts +0 -5
  180. package/src/lang/da.js +0 -191
  181. package/src/lang/de.d.ts +0 -5
  182. package/src/lang/de.js +0 -188
  183. package/src/lang/en.d.ts +0 -5
  184. package/src/lang/en.js +0 -188
  185. package/src/lang/es.d.ts +0 -5
  186. package/src/lang/es.js +0 -188
  187. package/src/lang/fa.d.ts +0 -5
  188. package/src/lang/fa.js +0 -188
  189. package/src/lang/fr.d.ts +0 -5
  190. package/src/lang/fr.js +0 -188
  191. package/src/lang/he.d.ts +0 -5
  192. package/src/lang/he.js +0 -188
  193. package/src/lang/index.d.ts +0 -23
  194. package/src/lang/it.d.ts +0 -5
  195. package/src/lang/it.js +0 -188
  196. package/src/lang/ja.d.ts +0 -5
  197. package/src/lang/ja.js +0 -188
  198. package/src/lang/ko.d.ts +0 -5
  199. package/src/lang/ko.js +0 -188
  200. package/src/lang/lv.d.ts +0 -5
  201. package/src/lang/lv.js +0 -188
  202. package/src/lang/nl.d.ts +0 -5
  203. package/src/lang/nl.js +0 -188
  204. package/src/lang/pl.d.ts +0 -5
  205. package/src/lang/pl.js +0 -188
  206. package/src/lang/pt_br.d.ts +0 -5
  207. package/src/lang/pt_br.js +0 -189
  208. package/src/lang/ro.d.ts +0 -5
  209. package/src/lang/ro.js +0 -188
  210. package/src/lang/ru.d.ts +0 -5
  211. package/src/lang/ru.js +0 -188
  212. package/src/lang/se.d.ts +0 -5
  213. package/src/lang/se.js +0 -191
  214. package/src/lang/tr.d.ts +0 -5
  215. package/src/lang/tr.js +0 -191
  216. package/src/lang/ua.d.ts +0 -5
  217. package/src/lang/ua.js +0 -188
  218. package/src/lang/ur.d.ts +0 -5
  219. package/src/lang/ur.js +0 -188
  220. package/src/lang/zh_cn.d.ts +0 -5
  221. package/src/lang/zh_cn.js +0 -187
  222. package/src/lib/constructor.js +0 -954
  223. package/src/lib/context.d.ts +0 -42
  224. package/src/lib/context.js +0 -71
  225. package/src/lib/core.d.ts +0 -1135
  226. package/src/lib/core.js +0 -9395
  227. package/src/lib/history.d.ts +0 -48
  228. package/src/lib/history.js +0 -219
  229. package/src/lib/util.d.ts +0 -678
  230. package/src/lib/util.js +0 -2131
  231. package/src/options.d.ts +0 -608
  232. package/src/plugins/CommandPlugin.d.ts +0 -8
  233. package/src/plugins/DialogPlugin.d.ts +0 -20
  234. package/src/plugins/FileBrowserPlugin.d.ts +0 -30
  235. package/src/plugins/Module.d.ts +0 -15
  236. package/src/plugins/Plugin.d.ts +0 -42
  237. package/src/plugins/SubmenuPlugin.d.ts +0 -8
  238. package/src/plugins/dialog/audio.js +0 -559
  239. package/src/plugins/dialog/image.js +0 -1126
  240. package/src/plugins/dialog/link.js +0 -223
  241. package/src/plugins/dialog/math.js +0 -295
  242. package/src/plugins/dialog/mention.js +0 -242
  243. package/src/plugins/dialog/video.js +0 -979
  244. package/src/plugins/index.d.ts +0 -79
  245. package/src/plugins/modules/_anchor.js +0 -461
  246. package/src/plugins/modules/_colorPicker.d.ts +0 -60
  247. package/src/plugins/modules/_colorPicker.js +0 -201
  248. package/src/plugins/modules/_notice.d.ts +0 -21
  249. package/src/plugins/modules/_notice.js +0 -72
  250. package/src/plugins/modules/_selectMenu.js +0 -119
  251. package/src/plugins/modules/component.d.ts +0 -25
  252. package/src/plugins/modules/component.js +0 -81
  253. package/src/plugins/modules/dialog.d.ts +0 -28
  254. package/src/plugins/modules/dialog.js +0 -175
  255. package/src/plugins/modules/fileBrowser.d.ts +0 -42
  256. package/src/plugins/modules/fileBrowser.js +0 -374
  257. package/src/plugins/modules/fileManager.d.ts +0 -67
  258. package/src/plugins/modules/fileManager.js +0 -326
  259. package/src/plugins/modules/index.js +0 -9
  260. package/src/plugins/modules/resizing.d.ts +0 -154
  261. package/src/plugins/modules/resizing.js +0 -903
  262. package/src/plugins/submenu/align.d.ts +0 -5
  263. package/src/plugins/submenu/align.js +0 -160
  264. package/src/plugins/submenu/font.d.ts +0 -5
  265. package/src/plugins/submenu/font.js +0 -123
  266. package/src/plugins/submenu/fontColor.d.ts +0 -5
  267. package/src/plugins/submenu/fontColor.js +0 -101
  268. package/src/plugins/submenu/fontSize.d.ts +0 -5
  269. package/src/plugins/submenu/fontSize.js +0 -112
  270. package/src/plugins/submenu/formatBlock.d.ts +0 -5
  271. package/src/plugins/submenu/formatBlock.js +0 -273
  272. package/src/plugins/submenu/hiliteColor.d.ts +0 -5
  273. package/src/plugins/submenu/hiliteColor.js +0 -102
  274. package/src/plugins/submenu/horizontalRule.d.ts +0 -5
  275. package/src/plugins/submenu/horizontalRule.js +0 -98
  276. package/src/plugins/submenu/lineHeight.d.ts +0 -5
  277. package/src/plugins/submenu/lineHeight.js +0 -104
  278. package/src/plugins/submenu/list.d.ts +0 -5
  279. package/src/plugins/submenu/list.js +0 -456
  280. package/src/plugins/submenu/paragraphStyle.d.ts +0 -5
  281. package/src/plugins/submenu/paragraphStyle.js +0 -135
  282. package/src/plugins/submenu/table.d.ts +0 -5
  283. package/src/plugins/submenu/table.js +0 -1431
  284. package/src/plugins/submenu/template.d.ts +0 -5
  285. package/src/plugins/submenu/template.js +0 -72
  286. package/src/plugins/submenu/textStyle.d.ts +0 -5
  287. package/src/plugins/submenu/textStyle.js +0 -167
  288. package/src/suneditor.d.ts +0 -9
  289. package/src/suneditor_build.js +0 -18
  290. /package/{src/plugins/dialog → typings}/mention.d.ts +0 -0
@@ -0,0 +1,3258 @@
1
+ /**
2
+ * @fileoverview Format class
3
+ */
4
+
5
+ import CoreInjector from '../../editorInjector/_core';
6
+ import { domUtils, unicode, numbers, env } from '../../helper';
7
+
8
+ const Format = function (editor) {
9
+ CoreInjector.call(this, editor);
10
+
11
+ // members
12
+ this._listCamel = this.options.get('__listCommonStyle');
13
+ this._listKebab = env.camelToKebabCase(this.options.get('__listCommonStyle'));
14
+ this._formatLineCheck = this.options.get('formatLine').reg;
15
+ this._formatBrLineCheck = this.options.get('formatBrLine').reg;
16
+ this._formatBlockCheck = this.options.get('formatBlock').reg;
17
+ this._formatClosureBlockCheck = this.options.get('formatClosureBlock').reg;
18
+ this._formatClosureBrLineCheck = this.options.get('formatClosureBrLine').reg;
19
+ this._textStyleTagsCheck = new RegExp('^(' + this.options.get('textStyleTags') + ')$', 'i');
20
+ };
21
+
22
+ Format.prototype = {
23
+ /**
24
+ * @description Replace the line tag of the current selection.
25
+ * @param {Element} element Line element (P, DIV..)
26
+ */
27
+ setLine(element) {
28
+ if (!this.isLine(element)) {
29
+ throw new Error('[SUNEDITOR.format.setLine.fail] The "element" must satisfy "format.isLine()".');
30
+ }
31
+
32
+ const info = this._lineWork();
33
+ const lines = info.lines;
34
+ const className = element.className;
35
+ const value = element.nodeName;
36
+ let first = info.firstNode;
37
+ let last = info.lastNode;
38
+
39
+ for (let i = 0, len = lines.length, node, newFormat; i < len; i++) {
40
+ node = lines[i];
41
+
42
+ if ((node.nodeName !== value || (node.className.match(/(\s|^)__se__format__[^\s]+/) || [''])[0].trim() !== className) && !this.component.is(node)) {
43
+ newFormat = element.cloneNode(false);
44
+ domUtils.copyFormatAttributes(newFormat, node);
45
+ newFormat.innerHTML = node.innerHTML;
46
+
47
+ node.parentNode.replaceChild(newFormat, node);
48
+ }
49
+
50
+ if (i === 0) first = newFormat || node;
51
+ if (i === len - 1) last = newFormat || node;
52
+ newFormat = null;
53
+ }
54
+
55
+ this.selection.setRange(domUtils.getNodeFromPath(info.firstPath, first), info.startOffset, domUtils.getNodeFromPath(info.lastPath, last), info.endOffset);
56
+ this.history.push(false);
57
+ },
58
+
59
+ /**
60
+ * @description If a parent node that contains an argument node finds a format node (format.isLine), it returns that node.
61
+ * @param {Node} node Reference node.
62
+ * @param {Function|null} validation Additional validation function.
63
+ * @returns {Element|null}
64
+ */
65
+ getLine(node, validation) {
66
+ if (!node) return null;
67
+ if (!validation) {
68
+ validation = function () {
69
+ return true;
70
+ };
71
+ }
72
+
73
+ while (node) {
74
+ if (domUtils.isWysiwygFrame(node)) return null;
75
+ if (this.isBlock(node)) return node.firstElementChild;
76
+ if (this.isLine(node) && validation(node)) return node;
77
+
78
+ node = node.parentNode;
79
+ }
80
+
81
+ return null;
82
+ },
83
+
84
+ /**
85
+ * @description Replace the br-line tag of the current selection.
86
+ * @param {Element} element BR-Line element (PRE..)
87
+ */
88
+ setBrLine(element) {
89
+ if (!this.isBrLine(element)) {
90
+ throw new Error('[SUNEDITOR.format.setBrLine.fail] The "element" must satisfy "format.isBrLine()".');
91
+ }
92
+
93
+ const lines = this._lineWork().lines;
94
+ const len = lines.length - 1;
95
+ let parentNode = lines[len].parentNode;
96
+ let freeElement = element.cloneNode(false);
97
+ const focusElement = freeElement;
98
+
99
+ for (let i = len, f, html, before, next, inner, isComp, first = true; i >= 0; i--) {
100
+ f = lines[i];
101
+ if (f === (!lines[i + 1] ? null : lines[i + 1].parentNode)) continue;
102
+
103
+ isComp = this.component.is(f);
104
+ html = isComp ? '' : f.innerHTML.replace(/(?!>)\s+(?=<)|\n/g, ' ');
105
+ before = domUtils.getParentElement(f, (current) => current.parentNode === parentNode);
106
+
107
+ if (parentNode !== f.parentNode || isComp) {
108
+ if (this.isLine(parentNode)) {
109
+ parentNode.parentNode.insertBefore(freeElement, parentNode.nextSibling);
110
+ parentNode = parentNode.parentNode;
111
+ } else {
112
+ parentNode.insertBefore(freeElement, before ? before.nextSibling : null);
113
+ parentNode = f.parentNode;
114
+ }
115
+
116
+ next = freeElement.nextSibling;
117
+ if (next && freeElement.nodeName === next.nodeName && domUtils.isSameAttributes(freeElement, next)) {
118
+ freeElement.innerHTML += '<BR>' + next.innerHTML;
119
+ domUtils.removeItem(next);
120
+ }
121
+
122
+ freeElement = element.cloneNode(false);
123
+ first = true;
124
+ }
125
+
126
+ inner = freeElement.innerHTML;
127
+ freeElement.innerHTML = (first || !html || !inner || /<br>$/i.test(html) ? html : html + '<BR>') + inner;
128
+
129
+ if (i === 0) {
130
+ parentNode.insertBefore(freeElement, f);
131
+ next = f.nextSibling;
132
+ if (next && freeElement.nodeName === next.nodeName && domUtils.isSameAttributes(freeElement, next)) {
133
+ freeElement.innerHTML += '<BR>' + next.innerHTML;
134
+ domUtils.removeItem(next);
135
+ }
136
+
137
+ const prev = freeElement.previousSibling;
138
+ if (prev && freeElement.nodeName === prev.nodeName && domUtils.isSameAttributes(freeElement, prev)) {
139
+ prev.innerHTML += '<BR>' + freeElement.innerHTML;
140
+ domUtils.removeItem(freeElement);
141
+ }
142
+ }
143
+
144
+ if (!isComp) domUtils.removeItem(f);
145
+ if (html) first = false;
146
+ }
147
+
148
+ this.selection.setRange(focusElement, 0, focusElement, 0);
149
+ this.history.push(false);
150
+ },
151
+
152
+ /**
153
+ * @description If a parent node that contains an argument node finds a free format node (format.isBrLine), it returns that node.
154
+ * @param {Node} element Reference node.
155
+ * @param {Function|null} validation Additional validation function.
156
+ * @returns {Element|null}
157
+ */
158
+ getBrLine(element, validation) {
159
+ if (!element) return null;
160
+ if (!validation) {
161
+ validation = function () {
162
+ return true;
163
+ };
164
+ }
165
+
166
+ while (element) {
167
+ if (domUtils.isWysiwygFrame(element)) return null;
168
+ if (this.isBrLine(element) && validation(element)) return element;
169
+
170
+ element = element.parentNode;
171
+ }
172
+
173
+ return null;
174
+ },
175
+
176
+ /**
177
+ * @description Append format element to sibling node of argument element.
178
+ * If the "lineNode" argument value is present, the tag of that argument value is inserted,
179
+ * If not, the currently selected format tag is inserted.
180
+ * @param {Element} element Insert as siblings of that element
181
+ * @param {string|Element|null} lineNode Node name or node obejct to be inserted
182
+ * @returns {Element}
183
+ */
184
+ addLine(element, lineNode) {
185
+ if (!element || !element.parentNode) return null;
186
+
187
+ const currentFormatEl = this.getLine(this.selection.getNode(), null);
188
+ let oFormat = null;
189
+ if (!this.isBrLine(element) && this.isBrLine(currentFormatEl || element.parentNode)) {
190
+ oFormat = domUtils.createElement('BR');
191
+ } else {
192
+ const oFormatName = lineNode
193
+ ? typeof lineNode === 'string'
194
+ ? lineNode
195
+ : lineNode.nodeName
196
+ : this.isLine(currentFormatEl) && !this.isBlock(currentFormatEl) && !this.isBrLine(currentFormatEl)
197
+ ? currentFormatEl.nodeName
198
+ : this.options.get('defaultLine');
199
+ oFormat = domUtils.createElement(oFormatName, null, '<br>');
200
+ if ((lineNode && typeof lineNode !== 'string') || (!lineNode && this.isLine(currentFormatEl))) {
201
+ domUtils.copyTagAttributes(oFormat, lineNode || currentFormatEl, ['id']);
202
+ }
203
+ }
204
+
205
+ if (domUtils.isTableCell(element)) element.insertBefore(oFormat, element.nextElementSibling);
206
+ else element.parentNode.insertBefore(oFormat, element.nextElementSibling);
207
+
208
+ return oFormat;
209
+ },
210
+
211
+ /**
212
+ * @description If a parent node that contains an argument node finds a format node (format.isBlock), it returns that node.
213
+ * @param {Node} element Reference node.
214
+ * @param {Function|null} validation Additional validation function.
215
+ * @returns {Element|null}
216
+ */
217
+ getBlock(element, validation) {
218
+ if (!element) return null;
219
+ if (!validation) {
220
+ validation = function () {
221
+ return true;
222
+ };
223
+ }
224
+
225
+ while (element) {
226
+ if (domUtils.isWysiwygFrame(element)) return null;
227
+ if (this.isBlock(element) && !/^(THEAD|TBODY|TR)$/i.test(element.nodeName) && validation(element)) return element;
228
+ element = element.parentNode;
229
+ }
230
+
231
+ return null;
232
+ },
233
+
234
+ /**
235
+ * @description Appended all selected format Element to the argument element and insert
236
+ * @param {Element} block Element of wrap the arguments (BLOCKQUOTE...)
237
+ */
238
+ applyBlock(block) {
239
+ this.selection.getRangeAndAddLine(this.selection.getRange(), null);
240
+ const rangeLines = this.getLinesAndComponents(false);
241
+ if (!rangeLines || rangeLines.length === 0) return;
242
+
243
+ linesLoop: for (let i = 0, len = rangeLines.length, line, nested, fEl, lEl, f, l; i < len; i++) {
244
+ line = rangeLines[i];
245
+ if (!domUtils.isListCell(line)) continue;
246
+
247
+ nested = line.lastElementChild;
248
+ if (nested && domUtils.isListCell(line.nextElementSibling) && rangeLines.includes(line.nextElementSibling)) {
249
+ lEl = nested.lastElementChild;
250
+ if (rangeLines.includes(lEl)) {
251
+ let list = null;
252
+ while ((list = lEl.lastElementChild)) {
253
+ if (domUtils.isList(list)) {
254
+ if (rangeLines.includes(list.lastElementChild)) {
255
+ lEl = list.lastElementChild;
256
+ } else {
257
+ continue linesLoop;
258
+ }
259
+ }
260
+ }
261
+
262
+ fEl = nested.firstElementChild;
263
+ f = rangeLines.indexOf(fEl);
264
+ l = rangeLines.indexOf(lEl);
265
+ rangeLines.splice(f, l - f + 1);
266
+ len = rangeLines.length;
267
+ continue;
268
+ }
269
+ }
270
+ }
271
+
272
+ const last = rangeLines[rangeLines.length - 1];
273
+ let standTag, beforeTag, pElement;
274
+
275
+ if (this.isBlock(last) || this.isLine(last)) {
276
+ standTag = last;
277
+ } else {
278
+ standTag = this.getBlock(last, null) || this.getLine(last, null);
279
+ }
280
+
281
+ if (domUtils.isTableCell(standTag)) {
282
+ beforeTag = null;
283
+ pElement = standTag;
284
+ } else {
285
+ beforeTag = standTag.nextSibling;
286
+ pElement = standTag.parentNode;
287
+ }
288
+
289
+ block = block.cloneNode(false);
290
+ let parentDepth = domUtils.getNodeDepth(standTag);
291
+ let listParent = null;
292
+ const lineArr = [];
293
+ const removeItems = (parent, origin, before) => {
294
+ let cc = null;
295
+ if (parent !== origin && !domUtils.isTableElements(origin)) {
296
+ if (origin && domUtils.getNodeDepth(parent) === domUtils.getNodeDepth(origin)) return before;
297
+ cc = this.nodeTransform.removeAllParents(origin, null, parent);
298
+ }
299
+
300
+ return cc ? cc.ec : before;
301
+ };
302
+
303
+ for (let i = 0, len = rangeLines.length, line, originParent, depth, before, nextLine, nextList, nested; i < len; i++) {
304
+ line = rangeLines[i];
305
+ originParent = line.parentNode;
306
+ if (!originParent || block.contains(originParent)) continue;
307
+
308
+ depth = domUtils.getNodeDepth(line);
309
+
310
+ if (domUtils.isList(originParent)) {
311
+ if (listParent === null) {
312
+ if (nextList) {
313
+ listParent = nextList;
314
+ nested = true;
315
+ nextList = null;
316
+ } else {
317
+ listParent = originParent.cloneNode(false);
318
+ }
319
+ }
320
+
321
+ lineArr.push(line);
322
+ nextLine = rangeLines[i + 1];
323
+
324
+ if (i === len - 1 || nextLine?.parentNode !== originParent) {
325
+ // nested list
326
+ if (line.contains(nextLine?.parentNode)) {
327
+ nextList = nextLine.parentNode.cloneNode(false);
328
+ }
329
+
330
+ let list = originParent.parentNode,
331
+ p;
332
+ while (domUtils.isList(list)) {
333
+ p = domUtils.createElement(list.nodeName);
334
+ p.appendChild(listParent);
335
+ listParent = p;
336
+ list = list.parentNode;
337
+ }
338
+
339
+ const edge = this.removeBlock(originParent, lineArr, null, true, true);
340
+
341
+ if (parentDepth >= depth) {
342
+ parentDepth = depth;
343
+ pElement = edge.cc;
344
+ beforeTag = removeItems(pElement, originParent, edge.ec);
345
+ if (beforeTag) pElement = beforeTag.parentNode;
346
+ } else if (pElement === edge.cc) {
347
+ beforeTag = edge.ec;
348
+ }
349
+
350
+ if (pElement !== edge.cc) {
351
+ before = removeItems(pElement, edge.cc, before);
352
+ if (before !== undefined) beforeTag = before;
353
+ else beforeTag = edge.cc;
354
+ }
355
+
356
+ for (let c = 0, cLen = edge.removeArray.length; c < cLen; c++) {
357
+ listParent.appendChild(edge.removeArray[c]);
358
+ }
359
+
360
+ if (!nested) block.appendChild(listParent);
361
+ if (nextList) edge.removeArray[edge.removeArray.length - 1].appendChild(nextList);
362
+ listParent = null;
363
+ nested = false;
364
+ }
365
+ } else {
366
+ if (parentDepth >= depth) {
367
+ parentDepth = depth;
368
+ pElement = originParent;
369
+ beforeTag = line.nextSibling;
370
+ }
371
+
372
+ block.appendChild(line);
373
+
374
+ if (pElement !== originParent) {
375
+ before = removeItems(pElement, originParent);
376
+ if (before !== undefined) beforeTag = before;
377
+ }
378
+ }
379
+ }
380
+
381
+ this.editor.effectNode = null;
382
+ this.nodeTransform.mergeSameTags(block, null, false);
383
+ this.nodeTransform.mergeNestedTags(block, (current) => domUtils.isList(current));
384
+
385
+ // Nested list
386
+ if (beforeTag && domUtils.getNodeDepth(beforeTag) > 0 && (domUtils.isList(beforeTag.parentNode) || domUtils.isList(beforeTag.parentNode.parentNode))) {
387
+ const depthFormat = domUtils.getParentElement(beforeTag, (current) => this.isBlock(current) && !domUtils.isList(current));
388
+ const splitRange = this.nodeTransform.split(beforeTag, null, !depthFormat ? 0 : domUtils.getNodeDepth(depthFormat) + 1);
389
+ splitRange.parentNode.insertBefore(block, splitRange);
390
+ } else {
391
+ // basic
392
+ pElement.insertBefore(block, beforeTag);
393
+ removeItems(block, beforeTag);
394
+ }
395
+
396
+ const edge = domUtils.getEdgeChildNodes(block.firstElementChild, block.lastElementChild);
397
+ if (rangeLines.length > 1) {
398
+ this.selection.setRange(edge.sc, 0, edge.ec, edge.ec.textContent.length);
399
+ } else {
400
+ this.selection.setRange(edge.ec, edge.ec.textContent.length, edge.ec, edge.ec.textContent.length);
401
+ }
402
+
403
+ this.history.push(false);
404
+ },
405
+
406
+ /**
407
+ * @description The elements of the "selectedFormats" array are detached from the "rangeElement" element. ("LI" tags are converted to "P" tags)
408
+ * When "selectedFormats" is null, all elements are detached and return {cc: parentNode, sc: nextSibling, ec: previousSibling, removeArray: [Array of removed elements]}.
409
+ * @param {Element} rangeElement Range format element (PRE, BLOCKQUOTE, OL, UL...)
410
+ * @param {Array|null} selectedFormats Array of format elements (P, DIV, LI...) to remove.
411
+ * If null, Applies to all elements and return {cc: parentNode, sc: nextSibling, ec: previousSibling}
412
+ * @param {Element|null} newRangeElement The node(rangeElement) to replace the currently wrapped node.
413
+ * @param {boolean} remove If true, deleted without detached.
414
+ * @param {boolean} notHistoryPush When true, it does not update the history stack and the selection object and return EdgeNodes (domUtils.getEdgeChildNodes)
415
+ * @returns {Object}
416
+ */
417
+ removeBlock(rangeElement, selectedFormats, newRangeElement, remove, notHistoryPush) {
418
+ const range = this.selection.getRange();
419
+ let so = range.startOffset;
420
+ let eo = range.endOffset;
421
+
422
+ let children = domUtils.getListChildNodes(rangeElement, (current) => current.parentNode === rangeElement);
423
+ let parent = rangeElement.parentNode;
424
+ let firstNode = null;
425
+ let lastNode = null;
426
+ let rangeEl = rangeElement.cloneNode(false);
427
+
428
+ const removeArray = [];
429
+ const newList = domUtils.isList(newRangeElement);
430
+ let insertedNew = false;
431
+ let reset = false;
432
+ let moveComplete = false;
433
+
434
+ const appendNode = (parentEl, insNode, sibling, originNode) => {
435
+ if (domUtils.isZeroWith(insNode)) {
436
+ insNode.innerHTML = unicode.zeroWidthSpace;
437
+ so = eo = 1;
438
+ }
439
+
440
+ if (insNode.nodeType === 3) {
441
+ parentEl.insertBefore(insNode, sibling);
442
+ return insNode;
443
+ }
444
+
445
+ const insChildren = (moveComplete ? insNode : originNode).childNodes;
446
+ let format = insNode.cloneNode(false);
447
+ let first = null;
448
+ let c = null;
449
+
450
+ while (insChildren[0]) {
451
+ c = insChildren[0];
452
+ if (this._notTextNode(c) && !domUtils.isBreak(c) && !domUtils.isListCell(format)) {
453
+ if (format.childNodes.length > 0) {
454
+ if (!first) first = format;
455
+ parentEl.insertBefore(format, sibling);
456
+ format = insNode.cloneNode(false);
457
+ }
458
+ parentEl.insertBefore(c, sibling);
459
+ if (!first) first = c;
460
+ } else {
461
+ format.appendChild(c);
462
+ }
463
+ }
464
+
465
+ if (format.childNodes.length > 0) {
466
+ if (domUtils.isListCell(parentEl) && domUtils.isListCell(format) && domUtils.isList(sibling)) {
467
+ if (newList) {
468
+ first = sibling;
469
+ while (sibling) {
470
+ format.appendChild(sibling);
471
+ sibling = sibling.nextSibling;
472
+ }
473
+ parentEl.parentNode.insertBefore(format, parentEl.nextElementSibling);
474
+ } else {
475
+ const originNext = originNode.nextElementSibling;
476
+ const detachRange = this._removeNestedList(originNode, false);
477
+ if (rangeElement !== detachRange || originNext !== originNode.nextElementSibling) {
478
+ const fChildren = format.childNodes;
479
+ while (fChildren[0]) {
480
+ originNode.appendChild(fChildren[0]);
481
+ }
482
+
483
+ rangeElement = detachRange;
484
+ reset = true;
485
+ }
486
+ }
487
+ } else {
488
+ parentEl.insertBefore(format, sibling);
489
+ }
490
+
491
+ if (!first) first = format;
492
+ }
493
+
494
+ return first;
495
+ };
496
+
497
+ // detach loop
498
+ for (let i = 0, len = children.length, insNode, lineIndex, next; i < len; i++) {
499
+ insNode = children[i];
500
+ if (insNode.nodeType === 3 && domUtils.isList(rangeEl)) continue;
501
+
502
+ moveComplete = false;
503
+ if (remove && i === 0) {
504
+ if (!selectedFormats || selectedFormats.length === len || selectedFormats[0] === insNode) {
505
+ firstNode = rangeElement.previousSibling;
506
+ } else {
507
+ firstNode = rangeEl;
508
+ }
509
+ }
510
+
511
+ if (selectedFormats) lineIndex = selectedFormats.indexOf(insNode);
512
+ if (selectedFormats && lineIndex === -1) {
513
+ if (!rangeEl) rangeEl = rangeElement.cloneNode(false);
514
+ rangeEl.appendChild(insNode);
515
+ } else {
516
+ if (selectedFormats) next = selectedFormats[lineIndex + 1];
517
+ if (rangeEl && rangeEl.children.length > 0) {
518
+ parent.insertBefore(rangeEl, rangeElement);
519
+ rangeEl = null;
520
+ }
521
+
522
+ if (!newList && domUtils.isListCell(insNode)) {
523
+ if (next && domUtils.getNodeDepth(insNode) !== domUtils.getNodeDepth(next) && (domUtils.isListCell(parent) || domUtils.getArrayItem(insNode.children, domUtils.isList, false))) {
524
+ const insNext = insNode.nextElementSibling;
525
+ const detachRange = this._removeNestedList(insNode, false);
526
+ if (rangeElement !== detachRange || insNext !== insNode.nextElementSibling) {
527
+ rangeElement = detachRange;
528
+ reset = true;
529
+ }
530
+ } else {
531
+ const inner = insNode;
532
+ insNode = domUtils.createElement(
533
+ remove ? inner.nodeName : domUtils.isList(rangeElement.parentNode) || domUtils.isListCell(rangeElement.parentNode) ? 'LI' : domUtils.isTableCell(rangeElement.parentNode) ? 'DIV' : this.options.get('defaultLine')
534
+ );
535
+ const isCell = domUtils.isListCell(insNode);
536
+ const innerChildren = inner.childNodes;
537
+ while (innerChildren[0]) {
538
+ if (domUtils.isList(innerChildren[0]) && !isCell) break;
539
+ insNode.appendChild(innerChildren[0]);
540
+ }
541
+ domUtils.copyFormatAttributes(insNode, inner);
542
+ moveComplete = true;
543
+ }
544
+ } else {
545
+ insNode = insNode.cloneNode(false);
546
+ }
547
+
548
+ if (!reset) {
549
+ if (!remove) {
550
+ if (newRangeElement) {
551
+ if (!insertedNew) {
552
+ parent.insertBefore(newRangeElement, rangeElement);
553
+ insertedNew = true;
554
+ }
555
+ insNode = appendNode(newRangeElement, insNode, null, children[i]);
556
+ } else {
557
+ insNode = appendNode(parent, insNode, rangeElement, children[i]);
558
+ }
559
+
560
+ if (!reset) {
561
+ if (selectedFormats) {
562
+ lastNode = insNode;
563
+ if (!firstNode) {
564
+ firstNode = insNode;
565
+ }
566
+ } else if (!firstNode) {
567
+ firstNode = lastNode = insNode;
568
+ }
569
+ }
570
+ } else {
571
+ removeArray.push(insNode);
572
+ domUtils.removeItem(children[i]);
573
+ }
574
+
575
+ if (reset) {
576
+ reset = moveComplete = false;
577
+ children = domUtils.getListChildNodes(rangeElement, (current) => current.parentNode === rangeElement);
578
+ rangeEl = rangeElement.cloneNode(false);
579
+ parent = rangeElement.parentNode;
580
+ i = -1;
581
+ len = children.length;
582
+ continue;
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ const rangeParent = rangeElement.parentNode;
589
+ let rangeRight = rangeElement.nextSibling;
590
+ if (rangeEl?.children.length > 0) {
591
+ rangeParent.insertBefore(rangeEl, rangeRight);
592
+ }
593
+
594
+ if (newRangeElement) firstNode = newRangeElement.previousSibling;
595
+ else if (!firstNode) firstNode = rangeElement.previousSibling;
596
+ rangeRight = rangeElement.nextSibling !== rangeEl ? rangeElement.nextSibling : rangeEl ? rangeEl.nextSibling : null;
597
+
598
+ if (rangeElement.children.length === 0 || rangeElement.textContent.length === 0) {
599
+ domUtils.removeItem(rangeElement);
600
+ } else {
601
+ this.nodeTransform.removeEmptyNode(rangeElement, null, false);
602
+ }
603
+
604
+ let edge = null;
605
+ if (remove) {
606
+ edge = {
607
+ cc: rangeParent,
608
+ sc: firstNode,
609
+ so: so,
610
+ ec: rangeRight,
611
+ eo: eo,
612
+ removeArray: removeArray
613
+ };
614
+ } else {
615
+ if (!firstNode) firstNode = lastNode;
616
+ if (!lastNode) lastNode = firstNode;
617
+ const childEdge = domUtils.getEdgeChildNodes(firstNode, lastNode.parentNode ? firstNode : lastNode);
618
+ edge = {
619
+ cc: (childEdge.sc || childEdge.ec).parentNode,
620
+ sc: childEdge.sc,
621
+ so: so,
622
+ ec: childEdge.ec,
623
+ eo: eo,
624
+ removeArray: null
625
+ };
626
+ }
627
+
628
+ this.editor.effectNode = null;
629
+ if (notHistoryPush) return edge;
630
+
631
+ if (!remove && edge) {
632
+ if (!selectedFormats) {
633
+ this.selection.setRange(edge.sc, 0, edge.sc, 0);
634
+ } else {
635
+ this.selection.setRange(edge.sc, so, edge.ec, eo);
636
+ }
637
+ }
638
+
639
+ this.history.push(false);
640
+ },
641
+
642
+ /**
643
+ * @description Append all selected format Element to the list and insert.
644
+ * @param {string} type List type. (ol | ul):[listStyleType]
645
+ * @param {Element} selectedCells Format elements or list cells.
646
+ * @param {boolean} nested If true, indenting existing list cells.
647
+ */
648
+ applyList(type, selectedCells, nested) {
649
+ const listTag = (type.split(':')[0] || 'ol').toUpperCase();
650
+ const listStyle = type.split(':')[1] || '';
651
+
652
+ let range = this.selection.getRange();
653
+ let selectedFormats = !selectedCells ? this.getLinesAndComponents(false) : selectedCells;
654
+
655
+ if (selectedFormats.length === 0) {
656
+ if (selectedCells) return;
657
+ range = this.selection.getRangeAndAddLine(range, null);
658
+ selectedFormats = this.getLinesAndComponents(false);
659
+ if (selectedFormats.length === 0) return;
660
+ }
661
+
662
+ domUtils.sortNodeByDepth(selectedFormats, true);
663
+
664
+ // merge
665
+ const firstSel = selectedFormats[0];
666
+ const lastSel = selectedFormats[selectedFormats.length - 1];
667
+ let topEl = (domUtils.isListCell(firstSel) || this.component.is(firstSel)) && !firstSel.previousElementSibling ? firstSel.parentNode.previousElementSibling : firstSel.previousElementSibling;
668
+ let bottomEl = (domUtils.isListCell(lastSel) || this.component.is(lastSel)) && !lastSel.nextElementSibling ? lastSel.parentNode.nextElementSibling : lastSel.nextElementSibling;
669
+
670
+ const isCollapsed = range.collapsed;
671
+ const originRange = {
672
+ sc: range.startContainer,
673
+ so: range.startContainer === range.endContainer && domUtils.isZeroWith(range.startContainer) && range.startOffset === 0 && range.endOffset === 1 ? range.endOffset : range.startOffset,
674
+ ec: range.endContainer,
675
+ eo: range.endOffset
676
+ };
677
+ let afterRange = null;
678
+ let isRemove = true;
679
+
680
+ for (let i = 0, len = selectedFormats.length; i < len; i++) {
681
+ if (!domUtils.isList(this.getBlock(selectedFormats[i], (current) => this.getBlock(current) && current !== selectedFormats[i]))) {
682
+ isRemove = false;
683
+ break;
684
+ }
685
+ }
686
+
687
+ if (isRemove && (!topEl || firstSel.tagName !== topEl.tagName || listTag !== topEl.tagName.toUpperCase()) && (!bottomEl || lastSel.tagName !== bottomEl.tagName || listTag !== bottomEl.tagName.toUpperCase())) {
688
+ if (nested) {
689
+ for (let i = 0, len = selectedFormats.length; i < len; i++) {
690
+ for (let j = i - 1; j >= 0; j--) {
691
+ if (selectedFormats[j].contains(selectedFormats[i])) {
692
+ selectedFormats.splice(i, 1);
693
+ i--;
694
+ len--;
695
+ break;
696
+ }
697
+ }
698
+ }
699
+ }
700
+
701
+ const currentFormat = this.getBlock(firstSel);
702
+ const cancel = currentFormat?.tagName === listTag;
703
+ let rangeArr, tempList;
704
+ const passComponent = (current) => {
705
+ return !this.component.is(current);
706
+ };
707
+
708
+ if (!cancel) {
709
+ tempList = domUtils.createElement(listTag, { style: 'list-style-type: ' + listStyle });
710
+ }
711
+
712
+ for (let i = 0, len = selectedFormats.length, r, o; i < len; i++) {
713
+ o = this.getBlock(selectedFormats[i], passComponent);
714
+ if (!o || !domUtils.isList(o)) continue;
715
+
716
+ if (!r) {
717
+ r = o;
718
+ rangeArr = {
719
+ r: r,
720
+ f: [domUtils.getParentElement(selectedFormats[i], 'LI')]
721
+ };
722
+ } else {
723
+ if (r !== o) {
724
+ if (nested && domUtils.isListCell(o.parentNode)) {
725
+ this._detachNested(rangeArr.f);
726
+ } else {
727
+ afterRange = this.removeBlock(rangeArr.f[0].parentNode, rangeArr.f, tempList, false, true);
728
+ }
729
+
730
+ o = selectedFormats[i].parentNode;
731
+ if (!cancel) {
732
+ tempList = domUtils.createElement(listTag, { style: 'list-style-type: ' + listStyle });
733
+ }
734
+
735
+ r = o;
736
+ rangeArr = {
737
+ r: r,
738
+ f: [domUtils.getParentElement(selectedFormats[i], 'LI')]
739
+ };
740
+ } else {
741
+ rangeArr.f.push(domUtils.getParentElement(selectedFormats[i], 'LI'));
742
+ }
743
+ }
744
+
745
+ if (i === len - 1) {
746
+ if (nested && domUtils.isListCell(o.parentNode)) {
747
+ this._detachNested(rangeArr.f);
748
+ } else {
749
+ afterRange = this.removeBlock(rangeArr.f[0].parentNode, rangeArr.f, tempList, false, true);
750
+ }
751
+ }
752
+ }
753
+ } else {
754
+ const topElParent = topEl ? topEl.parentNode : topEl;
755
+ const bottomElParent = bottomEl ? bottomEl.parentNode : bottomEl;
756
+ topEl = topElParent && !domUtils.isWysiwygFrame(topElParent) && topElParent.nodeName === listTag ? topElParent : topEl;
757
+ bottomEl = bottomElParent && !domUtils.isWysiwygFrame(bottomElParent) && bottomElParent.nodeName === listTag ? bottomElParent : bottomEl;
758
+
759
+ const mergeTop = topEl?.tagName === listTag;
760
+ const mergeBottom = bottomEl?.tagName === listTag;
761
+
762
+ let list = mergeTop ? topEl : domUtils.createElement(listTag, { style: 'list-style-type: ' + listStyle });
763
+ let firstList = null;
764
+ let topNumber = null;
765
+ // let lastList = null;
766
+ // let bottomNumber = null;
767
+
768
+ const passComponent = (current) => {
769
+ return !this.component.is(current) && !domUtils.isList(current);
770
+ };
771
+
772
+ for (let i = 0, len = selectedFormats.length, newCell, fTag, isCell, next, originParent, nextParent, parentTag, siblingTag, rangeTag; i < len; i++) {
773
+ fTag = selectedFormats[i];
774
+ if (fTag.childNodes.length === 0 && !this._isIgnoreNodeChange(fTag)) {
775
+ domUtils.removeItem(fTag);
776
+ continue;
777
+ }
778
+ next = selectedFormats[i + 1];
779
+ originParent = fTag.parentNode;
780
+ nextParent = next ? next.parentNode : null;
781
+ isCell = domUtils.isListCell(fTag);
782
+ rangeTag = this.isBlock(originParent) ? originParent : null;
783
+ parentTag = isCell && !domUtils.isWysiwygFrame(originParent) ? originParent.parentNode : originParent;
784
+ siblingTag = isCell && !domUtils.isWysiwygFrame(originParent) ? (!next || domUtils.isListCell(parentTag) ? originParent : originParent.nextSibling) : fTag.nextSibling;
785
+
786
+ newCell = domUtils.createElement('LI');
787
+ domUtils.copyFormatAttributes(newCell, fTag);
788
+ if (this.component.is(fTag)) {
789
+ const isHR = /^HR$/i.test(fTag.nodeName);
790
+ if (!isHR) newCell.innerHTML = '<br>';
791
+ newCell.innerHTML += fTag.outerHTML;
792
+ if (isHR) newCell.innerHTML += '<br>';
793
+ } else {
794
+ const fChildren = fTag.childNodes;
795
+ while (fChildren[0]) {
796
+ newCell.appendChild(fChildren[0]);
797
+ }
798
+ }
799
+ list.appendChild(newCell);
800
+
801
+ // if (!next) lastList = list;
802
+ if (!next || parentTag !== nextParent || this.isBlock(siblingTag)) {
803
+ if (!firstList) firstList = list;
804
+ if ((!mergeTop || !next || parentTag !== nextParent) && !(next && domUtils.isList(nextParent) && nextParent === originParent)) {
805
+ if (list.parentNode !== parentTag) parentTag.insertBefore(list, siblingTag);
806
+ }
807
+ }
808
+
809
+ domUtils.removeItem(fTag);
810
+ if (mergeTop && topNumber === null) topNumber = list.children.length - 1;
811
+ if (
812
+ next &&
813
+ (this.getBlock(nextParent, passComponent) !== this.getBlock(originParent, passComponent) ||
814
+ (domUtils.isList(nextParent) && domUtils.isList(originParent) && domUtils.getNodeDepth(nextParent) !== domUtils.getNodeDepth(originParent)))
815
+ ) {
816
+ list = domUtils.createElement(listTag, { style: 'list-style-type: ' + listStyle });
817
+ }
818
+
819
+ if (rangeTag?.children.length === 0) domUtils.removeItem(rangeTag);
820
+ }
821
+
822
+ if (topNumber) {
823
+ firstList = firstList.children[topNumber];
824
+ }
825
+
826
+ if (mergeBottom) {
827
+ // bottomNumber = list.children.length - 1;
828
+ list.innerHTML += bottomEl.innerHTML;
829
+ // lastList = list.children[bottomNumber] || lastList;
830
+ domUtils.removeItem(bottomEl);
831
+ }
832
+ }
833
+
834
+ this.editor.effectNode = null;
835
+ return !isRemove || !isCollapsed ? originRange : afterRange;
836
+ },
837
+
838
+ /**
839
+ * @description "selectedCells" array are detached from the list element.
840
+ * The return value is applied when the first and last lines of "selectedFormats" are "LI" respectively.
841
+ * @param {Array} selectedCells Array of format elements (LI, P...) to remove.
842
+ * @param {boolean} remove If true, It does not just remove the list, it deletes the content.
843
+ * @returns {Object} {sc: <LI>, ec: <LI>}.
844
+ */
845
+ removeList(selectedCells, remove) {
846
+ let rangeArr = {};
847
+ let listFirst = false;
848
+ let listLast = false;
849
+ let first = null;
850
+ let last = null;
851
+ const passComponent = (current) => {
852
+ return !this.component.is(current);
853
+ };
854
+
855
+ for (let i = 0, len = selectedCells.length, r, o, lastIndex, isList; i < len; i++) {
856
+ lastIndex = i === len - 1;
857
+ o = this.getBlock(selectedCells[i], passComponent);
858
+ isList = domUtils.isList(o);
859
+ if (!r && isList) {
860
+ r = o;
861
+ rangeArr = {
862
+ r: r,
863
+ f: [domUtils.getParentElement(selectedCells[i], 'LI')]
864
+ };
865
+ if (i === 0) listFirst = true;
866
+ } else if (r && isList) {
867
+ if (r !== o) {
868
+ const edge = this.detachRangeFormatElement(rangeArr.f[0].parentNode, rangeArr.f, null, remove, true);
869
+ o = selectedCells[i].parentNode;
870
+ if (listFirst) {
871
+ first = edge.sc;
872
+ listFirst = false;
873
+ }
874
+ if (lastIndex) last = edge.ec;
875
+
876
+ if (isList) {
877
+ r = o;
878
+ rangeArr = {
879
+ r: r,
880
+ f: [domUtils.getParentElement(selectedCells[i], 'LI')]
881
+ };
882
+ if (lastIndex) listLast = true;
883
+ } else {
884
+ r = null;
885
+ }
886
+ } else {
887
+ rangeArr.f.push(domUtils.getParentElement(selectedCells[i], 'LI'));
888
+ if (lastIndex) listLast = true;
889
+ }
890
+ }
891
+
892
+ if (lastIndex && domUtils.isList(r)) {
893
+ const edge = this.detachRangeFormatElement(rangeArr.f[0].parentNode, rangeArr.f, null, remove, true);
894
+ if (listLast || len === 1) last = edge.ec;
895
+ if (listFirst) first = edge.sc || last;
896
+ }
897
+ }
898
+
899
+ return {
900
+ sc: first,
901
+ ec: last
902
+ };
903
+ },
904
+
905
+ /**
906
+ * @description Indent more the selected lines.
907
+ * margin size - 'status.indentSize'px
908
+ */
909
+ indent() {
910
+ const range = this.selection.getRange();
911
+ const sc = range.startContainer;
912
+ const ec = range.endContainer;
913
+ const so = range.startOffset;
914
+ const eo = range.endOffset;
915
+
916
+ const lines = this.getLines(null);
917
+ const cells = SetLineMargin(lines, this.status.indentSize, this.options.get('_rtl') ? 'marginRight' : 'marginLeft');
918
+
919
+ // list cells
920
+ if (cells.length > 0) {
921
+ this._applyNestedList(cells, true);
922
+ }
923
+
924
+ this.editor.effectNode = null;
925
+ this.selection.setRange(sc, so, ec, eo);
926
+ this.history.push(false);
927
+ },
928
+
929
+ /**
930
+ * @description Indent less the selected lines.
931
+ * margin size - "status.indentSize"px
932
+ */
933
+ outdent() {
934
+ const range = this.selection.getRange();
935
+ const sc = range.startContainer;
936
+ const ec = range.endContainer;
937
+ const so = range.startOffset;
938
+ const eo = range.endOffset;
939
+
940
+ const lines = this.getLines(null);
941
+ const cells = SetLineMargin(lines, this.status.indentSize * -1, this.options.get('_rtl') ? 'marginRight' : 'marginLeft');
942
+
943
+ // list cells
944
+ if (cells.length > 0) {
945
+ this._applyNestedList(cells, false);
946
+ }
947
+
948
+ this.editor.effectNode = null;
949
+ this.selection.setRange(sc, so, ec, eo);
950
+ this.history.push(false);
951
+ },
952
+
953
+ /**
954
+ * @description Add, update, and delete style node from selected text. (a, span, strong, ect.)
955
+ * 1. If there is a node in the "styleNode" argument, a node with the same tags and attributes as "styleNode" is added to the selection text.
956
+ * 2. If it is in the same tag, only the tag's attributes are changed without adding a tag.
957
+ * 3. If the "styleNode" argument is null, the node of the selection is update or remove without adding a new node.
958
+ * 4. The same style as the style attribute of the "styleArray" argument is deleted.
959
+ * (Styles should be put with attribute names from css. ["background-color"])
960
+ * 5. The same class name as the class attribute of the "styleArray" argument is deleted.
961
+ * (The class name is preceded by "." [".className"])
962
+ * 6. Use a list of styles and classes of "styleNode" in "styleArray" to avoid duplicate property values.
963
+ * 7. If a node with all styles and classes removed has the same tag name as "styleNode" or "removeNodeArray", or "styleNode" is null, that node is deleted.
964
+ * 8. Regardless of the style and class of the node, the tag with the same name as the "removeNodeArray" argument value is deleted.
965
+ * 9. If the "strictRemove" argument is true, only nodes with all styles and classes removed from the nodes of "removeNodeArray" are removed.
966
+ *10. It won't work if the parent node has the same class and same value style.
967
+ * However, if there is a value in "removeNodeArray", it works and the text node is separated even if there is no node to replace.
968
+ * @param {Element|null} styleNode The element to be added to the selection. If it is null, only delete the node.
969
+ * @param {Array|null} styleArray The style or className attribute name Array to check (['font-size'], ['.className'], ['font-family', 'color', '.className']...])
970
+ * @param {Array|null} removeNodeArray An array of node names to remove types from, remove all formats when "styleNode" is null and there is an empty array or null value. (['span'], ['strong', 'em'] ...])
971
+ * @param {Boolean|null} strictRemove If true, only nodes with all styles and classes removed from the nodes of "removeNodeArray" are removed.
972
+ * @returns {Element} The element that was added to the selection.
973
+ */
974
+ applyTextStyle(styleNode, styleArray, removeNodeArray, strictRemove) {
975
+ if (domUtils.getParentElement(this.selection.getNode(), domUtils.isNonEditable)) return;
976
+
977
+ this.selection._resetRangeToTextNode();
978
+ let range = this.selection.getRangeAndAddLine(this.selection.getRange(), null);
979
+ styleArray = styleArray?.length > 0 ? styleArray : false;
980
+ removeNodeArray = removeNodeArray?.length > 0 ? removeNodeArray : false;
981
+
982
+ const isRemoveNode = !styleNode;
983
+ const isRemoveFormat = isRemoveNode && !removeNodeArray && !styleArray;
984
+ let startCon = range.startContainer;
985
+ let startOff = range.startOffset;
986
+ let endCon = range.endContainer;
987
+ let endOff = range.endOffset;
988
+
989
+ if ((isRemoveFormat && range.collapsed && this.isLine(startCon.parentNode) && this.isLine(endCon.parentNode)) || (startCon === endCon && startCon.nodeType === 1 && domUtils.isNonEditable(startCon))) {
990
+ const format = startCon.parentNode;
991
+ if (
992
+ !domUtils.isListCell(format) ||
993
+ !env.getValues(format.style).some((k) => {
994
+ return this._listKebab.includes(k);
995
+ })
996
+ )
997
+ return;
998
+ return;
999
+ }
1000
+
1001
+ if (range.collapsed && !isRemoveFormat) {
1002
+ if (startCon.nodeType === 1 && !domUtils.isBreak(startCon) && !this.component.is(startCon)) {
1003
+ let afterNode = null;
1004
+ const focusNode = startCon.childNodes[startOff];
1005
+
1006
+ if (focusNode) {
1007
+ if (!focusNode.nextSibling) {
1008
+ afterNode = null;
1009
+ } else {
1010
+ afterNode = domUtils.isBreak(focusNode) ? focusNode : focusNode.nextSibling;
1011
+ }
1012
+ }
1013
+
1014
+ const zeroWidth = domUtils.createTextNode(unicode.zeroWidthSpace);
1015
+ startCon.insertBefore(zeroWidth, afterNode);
1016
+ this.selection.setRange(zeroWidth, 1, zeroWidth, 1);
1017
+
1018
+ range = this.selection.getRange();
1019
+ startCon = range.startContainer;
1020
+ startOff = range.startOffset;
1021
+ endCon = range.endContainer;
1022
+ endOff = range.endOffset;
1023
+ }
1024
+ }
1025
+
1026
+ if (this.isLine(startCon)) {
1027
+ startCon = startCon.childNodes[startOff] || startCon.firstChild;
1028
+ startOff = 0;
1029
+ }
1030
+ if (this.isLine(endCon)) {
1031
+ endCon = endCon.childNodes[endOff] || endCon.lastChild;
1032
+ endOff = endCon.textContent.length;
1033
+ }
1034
+
1035
+ if (isRemoveNode) {
1036
+ styleNode = domUtils.createElement('DIV');
1037
+ }
1038
+
1039
+ const wRegExp = RegExp;
1040
+ const newNodeName = styleNode.nodeName;
1041
+
1042
+ /* checked same style property */
1043
+ if (!isRemoveFormat && startCon === endCon && !removeNodeArray && styleNode) {
1044
+ let sNode = startCon;
1045
+ let checkCnt = 0;
1046
+ const checkAttrs = [];
1047
+
1048
+ const checkStyles = styleNode.style;
1049
+ for (let i = 0, len = checkStyles.length; i < len; i++) {
1050
+ checkAttrs.push(checkStyles[i]);
1051
+ }
1052
+
1053
+ const ckeckClasses = styleNode.classList;
1054
+ for (let i = 0, len = ckeckClasses.length; i < len; i++) {
1055
+ checkAttrs.push('.' + ckeckClasses[i]);
1056
+ }
1057
+
1058
+ if (checkAttrs.length > 0) {
1059
+ while (!this.isLine(sNode) && !domUtils.isWysiwygFrame(sNode)) {
1060
+ for (let i = 0; i < checkAttrs.length; i++) {
1061
+ if (sNode.nodeType === 1) {
1062
+ const s = checkAttrs[i];
1063
+ const classReg = /^\./.test(s) ? new wRegExp('\\s*' + s.replace(/^\./, '') + '(\\s+|$)', 'ig') : false;
1064
+
1065
+ const styleCheck = isRemoveNode ? !!sNode.style[s] : !!sNode.style[s] && !!styleNode.style[s] && sNode.style[s] === styleNode.style[s];
1066
+ const classCheck = classReg === false ? false : isRemoveNode ? !!sNode.className.match(classReg) : !!sNode.className.match(classReg) && !!styleNode.className.match(classReg);
1067
+ if (styleCheck || classCheck) {
1068
+ checkCnt++;
1069
+ }
1070
+ }
1071
+ }
1072
+ sNode = sNode.parentNode;
1073
+ }
1074
+
1075
+ if (checkCnt >= checkAttrs.length) return;
1076
+ }
1077
+ }
1078
+
1079
+ let start = {},
1080
+ end = {};
1081
+ let newNode,
1082
+ styleRegExp = '',
1083
+ classRegExp = '',
1084
+ removeNodeRegExp = null;
1085
+
1086
+ if (styleArray) {
1087
+ for (let i = 0, len = styleArray.length, s; i < len; i++) {
1088
+ s = styleArray[i];
1089
+ if (/^\./.test(s)) {
1090
+ classRegExp += (classRegExp ? '|' : '\\s*(?:') + s.replace(/^\./, '');
1091
+ } else {
1092
+ styleRegExp += (styleRegExp ? '|' : '(?:;|^|\\s)(?:') + s;
1093
+ }
1094
+ }
1095
+
1096
+ if (styleRegExp) {
1097
+ styleRegExp += ')\\s*:[^;]*\\s*(?:;|$)';
1098
+ styleRegExp = new wRegExp(styleRegExp, 'ig');
1099
+ }
1100
+
1101
+ if (classRegExp) {
1102
+ classRegExp += ')(?=\\s+|$)';
1103
+ classRegExp = new wRegExp(classRegExp, 'ig');
1104
+ }
1105
+ }
1106
+
1107
+ if (removeNodeArray) {
1108
+ removeNodeRegExp = '^(?:' + removeNodeArray[0];
1109
+ for (let i = 1; i < removeNodeArray.length; i++) {
1110
+ removeNodeRegExp += '|' + removeNodeArray[i];
1111
+ }
1112
+ removeNodeRegExp += ')$';
1113
+ removeNodeRegExp = new wRegExp(removeNodeRegExp, 'i');
1114
+ }
1115
+
1116
+ /** validation check function*/
1117
+ const wBoolean = Boolean;
1118
+ const _removeCheck = {
1119
+ v: false
1120
+ };
1121
+ const validation = function (checkNode) {
1122
+ const vNode = checkNode.cloneNode(false);
1123
+
1124
+ // all path
1125
+ if (vNode.nodeType === 3 || domUtils.isBreak(vNode)) return vNode;
1126
+ // all remove
1127
+ if (isRemoveFormat) return null;
1128
+
1129
+ // remove node check
1130
+ const tagRemove = (!removeNodeRegExp && isRemoveNode) || removeNodeRegExp?.test(vNode.nodeName);
1131
+
1132
+ // tag remove
1133
+ if (tagRemove && !strictRemove) {
1134
+ _removeCheck.v = true;
1135
+ return null;
1136
+ }
1137
+
1138
+ // style regexp
1139
+ const originStyle = vNode.style.cssText;
1140
+ let style = '';
1141
+ if (styleRegExp && originStyle.length > 0) {
1142
+ style = originStyle.replace(styleRegExp, '').trim();
1143
+ if (style !== originStyle) _removeCheck.v = true;
1144
+ }
1145
+
1146
+ // class check
1147
+ const originClasses = vNode.className;
1148
+ let classes = '';
1149
+ if (classRegExp && originClasses.length > 0) {
1150
+ classes = originClasses.replace(classRegExp, '').trim();
1151
+ if (classes !== originClasses) _removeCheck.v = true;
1152
+ }
1153
+
1154
+ // remove only
1155
+ if (isRemoveNode) {
1156
+ if ((classRegExp || !originClasses) && (styleRegExp || !originStyle) && !style && !classes && tagRemove) {
1157
+ _removeCheck.v = true;
1158
+ return null;
1159
+ }
1160
+ }
1161
+
1162
+ // change
1163
+ if (style || classes || vNode.nodeName !== newNodeName || wBoolean(styleRegExp) !== wBoolean(originStyle) || wBoolean(classRegExp) !== wBoolean(originClasses)) {
1164
+ if (styleRegExp && originStyle.length > 0) vNode.style.cssText = style;
1165
+ if (!vNode.style.cssText) {
1166
+ vNode.removeAttribute('style');
1167
+ }
1168
+
1169
+ if (classRegExp && originClasses.length > 0) vNode.className = classes.trim();
1170
+ if (!vNode.className.trim()) {
1171
+ vNode.removeAttribute('class');
1172
+ }
1173
+
1174
+ if (!vNode.style.cssText && !vNode.className && (vNode.nodeName === newNodeName || tagRemove)) {
1175
+ _removeCheck.v = true;
1176
+ return null;
1177
+ }
1178
+
1179
+ return vNode;
1180
+ }
1181
+
1182
+ _removeCheck.v = true;
1183
+ return null;
1184
+ };
1185
+
1186
+ // get line nodes
1187
+ const lineNodes = this.getLines(null);
1188
+ range = this.selection.getRange();
1189
+ startCon = range.startContainer;
1190
+ startOff = range.startOffset;
1191
+ endCon = range.endContainer;
1192
+ endOff = range.endOffset;
1193
+
1194
+ if (!this.getLine(startCon, null)) {
1195
+ startCon = domUtils.getEdgeChild(
1196
+ lineNodes[0],
1197
+ function (current) {
1198
+ return current.nodeType === 3;
1199
+ },
1200
+ false
1201
+ );
1202
+ startOff = 0;
1203
+ }
1204
+
1205
+ if (!this.getLine(endCon, null)) {
1206
+ endCon = domUtils.getEdgeChild(
1207
+ lineNodes[lineNodes.length - 1],
1208
+ function (current) {
1209
+ return current.nodeType === 3;
1210
+ },
1211
+ false
1212
+ );
1213
+ endOff = endCon.textContent.length;
1214
+ }
1215
+
1216
+ const oneLine = this.getLine(startCon, null) === this.getLine(endCon, null);
1217
+ const endLength = lineNodes.length - (oneLine ? 0 : 1);
1218
+
1219
+ // node Changes
1220
+ newNode = styleNode.cloneNode(false);
1221
+
1222
+ const isRemoveAnchor =
1223
+ isRemoveFormat ||
1224
+ (isRemoveNode &&
1225
+ (function (inst, arr) {
1226
+ for (let n = 0, len = arr.length; n < len; n++) {
1227
+ if (inst._isNonSplitNode(arr[n]) || inst._sn_isSizeNode(arr[n])) return true;
1228
+ }
1229
+ return false;
1230
+ })(this, removeNodeArray));
1231
+
1232
+ const isSizeNode = isRemoveNode || this._sn_isSizeNode(newNode);
1233
+ const _getMaintainedNode = this._sn_getMaintainedNode.bind(this, isRemoveAnchor, isSizeNode);
1234
+ const _isMaintainedNode = this._sn_isMaintainedNode.bind(this, isRemoveAnchor, isSizeNode);
1235
+
1236
+ // one line
1237
+ if (oneLine) {
1238
+ if (this._sn_resetCommonListCell(lineNodes[0], styleArray)) range = this.selection.setRange(startCon, startOff, endCon, endOff);
1239
+
1240
+ const newRange = this._setNode_oneLine(lineNodes[0], newNode, validation, startCon, startOff, endCon, endOff, isRemoveFormat, isRemoveNode, range.collapsed, _removeCheck, _getMaintainedNode, _isMaintainedNode);
1241
+ start.container = newRange.startContainer;
1242
+ start.offset = newRange.startOffset;
1243
+ end.container = newRange.endContainer;
1244
+ end.offset = newRange.endOffset;
1245
+
1246
+ if (start.container === end.container && domUtils.isZeroWith(start.container)) {
1247
+ start.offset = end.offset = 1;
1248
+ }
1249
+ this._sn_setCommonListStyle(newRange.ancestor, null);
1250
+ } else {
1251
+ // multi line
1252
+ let appliedCommonList = false;
1253
+ if (endLength > 0 && this._sn_resetCommonListCell(lineNodes[endLength], styleArray)) appliedCommonList = true;
1254
+ if (this._sn_resetCommonListCell(lineNodes[0], styleArray)) appliedCommonList = true;
1255
+ if (appliedCommonList) this.selection.setRange(startCon, startOff, endCon, endOff);
1256
+
1257
+ // end
1258
+ if (endLength > 0) {
1259
+ newNode = styleNode.cloneNode(false);
1260
+ end = this._setNode_endLine(lineNodes[endLength], newNode, validation, endCon, endOff, isRemoveFormat, isRemoveNode, _removeCheck, _getMaintainedNode, _isMaintainedNode);
1261
+ }
1262
+
1263
+ // mid
1264
+ for (let i = endLength - 1, newRange; i > 0; i--) {
1265
+ this._sn_resetCommonListCell(lineNodes[i], styleArray);
1266
+ newNode = styleNode.cloneNode(false);
1267
+ newRange = this._setNode_middleLine(lineNodes[i], newNode, validation, isRemoveFormat, isRemoveNode, _removeCheck, end.container);
1268
+ if (newRange.endContainer && newRange.ancestor.contains(newRange.endContainer)) {
1269
+ end.ancestor = null;
1270
+ end.container = newRange.endContainer;
1271
+ }
1272
+ this._sn_setCommonListStyle(newRange.ancestor, null);
1273
+ }
1274
+
1275
+ // start
1276
+ newNode = styleNode.cloneNode(false);
1277
+ start = this._setNode_startLine(lineNodes[0], newNode, validation, startCon, startOff, isRemoveFormat, isRemoveNode, _removeCheck, _getMaintainedNode, _isMaintainedNode, end.container);
1278
+
1279
+ if (start.endContainer) {
1280
+ end.ancestor = null;
1281
+ end.container = start.endContainer;
1282
+ }
1283
+
1284
+ if (endLength <= 0) {
1285
+ end = start;
1286
+ } else if (!end.container) {
1287
+ end.ancestor = null;
1288
+ end.container = start.container;
1289
+ end.offset = start.container.textContent.length;
1290
+ }
1291
+
1292
+ this._sn_setCommonListStyle(start.ancestor, null);
1293
+ this._sn_setCommonListStyle(end.ancestor || this.getLine(end.container), null);
1294
+ }
1295
+
1296
+ // set range
1297
+ this.editor._offCurrentController();
1298
+ this.selection.setRange(start.container, start.offset, end.container, end.offset);
1299
+ this.history.push(false);
1300
+
1301
+ return newNode;
1302
+ },
1303
+
1304
+ /**
1305
+ * @description Remove format of the currently selected text.
1306
+ */
1307
+ removeTextStyle() {
1308
+ this.applyTextStyle(null, null, null, null);
1309
+ },
1310
+
1311
+ /**
1312
+ * @description Check if the container and offset values are the edges of the "line"
1313
+ * @param {Node} container The node of the selection object. (range.startContainer..)
1314
+ * @param {number} offset The offset of the selection object. (selection.getRange().startOffset...)
1315
+ * @param {string} dir Select check point - "front": Front edge, "end": End edge, undefined: Both edge.
1316
+ * @returns {boolean}
1317
+ */
1318
+ isEdgeLine(node, offset, dir) {
1319
+ if (!domUtils.isEdgePoint(node, offset, dir)) return false;
1320
+
1321
+ const result = [];
1322
+ dir = dir === 'front' ? 'previousSibling' : 'nextSibling';
1323
+ while (node && !this.isLine(node) && !domUtils.isWysiwygFrame(node)) {
1324
+ if (!node[dir] || (domUtils.isBreak(node[dir]) && !node[dir][dir])) {
1325
+ if (node.nodeType === 1) result.push(node.cloneNode(false));
1326
+ node = node.parentNode;
1327
+ } else {
1328
+ return null;
1329
+ }
1330
+ }
1331
+
1332
+ return result;
1333
+ },
1334
+
1335
+ /**
1336
+ * @description It is judged whether it is a node related to the text style.
1337
+ * @param {Node|string} element The node to check
1338
+ * @returns {boolean}
1339
+ */
1340
+ isTextStyleNode(element) {
1341
+ return typeof element === 'string' ? this._textStyleTagsCheck.test(element) : element && element.nodeType === 1 && this._textStyleTagsCheck.test(element.nodeName);
1342
+ },
1343
+
1344
+ /**
1345
+ * @description It is judged whether it is the format element (P, DIV, H[1-6], PRE, LI | class="__se__format__line_xxx")
1346
+ * Format element also contain "free format Element"
1347
+ * @param {Node|string} element The node to check
1348
+ * @returns {boolean}
1349
+ */
1350
+ isLine(element) {
1351
+ return typeof element === 'string'
1352
+ ? this._formatLineCheck.test(element)
1353
+ : element && element.nodeType === 1 && (this._formatLineCheck.test(element.nodeName) || domUtils.hasClass(element, '__se__format__line_.+|__se__format__br_line_.+')) && !this._nonFormat(element);
1354
+ },
1355
+
1356
+ /**
1357
+ * @description It is judged whether it is the only format element, not block (td, th, li)
1358
+ * Format element also contain "free format Element"
1359
+ * @param {Node|string} element The node to check
1360
+ * @returns {boolean}
1361
+ */
1362
+ isLineOnly(element) {
1363
+ return this._formatLineCheck.test(element) && !this._formatBlockCheck.test(element);
1364
+ },
1365
+
1366
+ /**
1367
+ * @description It is judged whether it is the free format element. (PRE | class="__se__format__br_line_xxx")
1368
+ * Free format elements is included in the format element.
1369
+ * Free format elements's line break is "BR" tag.
1370
+ * ※ Entering the Enter key in the space on the last line ends "Free Format" and appends "Format".
1371
+ * @param {Node|string} element The node to check
1372
+ * @returns {boolean}
1373
+ */
1374
+ isBrLine(element) {
1375
+ return typeof element === 'string'
1376
+ ? this._formatBrLineCheck.test(element)
1377
+ : element && element.nodeType === 1 && (this._formatBrLineCheck.test(element.nodeName) || domUtils.hasClass(element, '__se__format__br_line_.+')) && !this._nonFormat(element);
1378
+ },
1379
+
1380
+ /**
1381
+ * @description It is judged whether it is the range format element. (BLOCKQUOTE, OL, UL, FIGCAPTION, TABLE, THEAD, TBODY, TR, TH, TD | class="__se__format__block_xxx")
1382
+ * Range format element is wrap the "format element" and "component"
1383
+ * @param {Node|string} element The node to check
1384
+ * @returns {boolean}
1385
+ */
1386
+ isBlock(element) {
1387
+ return typeof element === 'string'
1388
+ ? this._formatBlockCheck.test(element)
1389
+ : element && element.nodeType === 1 && (this._formatBlockCheck.test(element.nodeName) || domUtils.hasClass(element, '__se__format__block_.+')) && !this._nonFormat(element);
1390
+ },
1391
+
1392
+ /**
1393
+ * @description It is judged whether it is the closure range format element. (TH, TD | class="__se__format__block_closure_xxx")
1394
+ * Closure range format elements is included in the range format element.
1395
+ * - Closure range format element is wrap the "format element" and "component"
1396
+ * ※ You cannot exit this format with the Enter key or Backspace key.
1397
+ * ※ Use it only in special cases. ([ex] format of table cells)
1398
+ * @param {Node|string} element The node to check
1399
+ * @returns {boolean}
1400
+ */
1401
+ isClosureBlock(element) {
1402
+ return typeof element === 'string'
1403
+ ? this._formatClosureBlockCheck.test(element)
1404
+ : element && element.nodeType === 1 && (this._formatClosureBlockCheck.test(element.nodeName) || domUtils.hasClass(element, '__se__format__block_closure_.+')) && !this._nonFormat(element);
1405
+ },
1406
+
1407
+ /**
1408
+ * @description It is judged whether it is the closure free format element. (class="__se__format__br_line__closure_xxx")
1409
+ * Closure free format elements is included in the free format element.
1410
+ * - Closure free format elements's line break is "BR" tag.
1411
+ * ※ You cannot exit this format with the Enter key or Backspace key.
1412
+ * ※ Use it only in special cases. ([ex] format of table cells)
1413
+ * @param {Node|string} element The node to check
1414
+ * @returns {boolean}
1415
+ */
1416
+ isClosureBrLine(element) {
1417
+ return typeof element === 'string'
1418
+ ? this._formatClosureBrLineCheck.test(element)
1419
+ : element && element.nodeType === 1 && (this._formatClosureBrLineCheck.test(element.nodeName) || domUtils.hasClass(element, '__se__format__br_line__closure_.+')) && !this._nonFormat(element);
1420
+ },
1421
+
1422
+ /**
1423
+ * @description Returns a "line" array from selected range.
1424
+ * @param {Function|null} validation The validation function. (Replaces the default validation format.isLine(current))
1425
+ * @returns {Array}
1426
+ */
1427
+ getLines(validation) {
1428
+ if (!this.selection._resetRangeToTextNode()) return [];
1429
+ let range = this.selection.getRange();
1430
+
1431
+ if (domUtils.isWysiwygFrame(range.startContainer)) {
1432
+ const children = this.editor.frameContext.get('wysiwyg').children;
1433
+ if (children.length === 0) return [];
1434
+
1435
+ this.selection.setRange(children[0], 0, children[children.length - 1], children[children.length - 1].textContent.trim().length);
1436
+ range = this.selection.getRange();
1437
+ }
1438
+
1439
+ const startCon = range.startContainer;
1440
+ const endCon = range.endContainer;
1441
+ const commonCon = range.commonAncestorContainer;
1442
+
1443
+ // get line nodes
1444
+ const lineNodes = domUtils.getListChildren(commonCon, (current) => {
1445
+ return validation ? validation(current) : this.isLine(current);
1446
+ });
1447
+
1448
+ if (!domUtils.isWysiwygFrame(commonCon) && !this.isBlock(commonCon)) lineNodes.unshift(this.getLine(commonCon, null));
1449
+ if (startCon === endCon || lineNodes.length === 1) return lineNodes;
1450
+
1451
+ const startLine = this.getLine(startCon, null);
1452
+ const endLine = this.getLine(endCon, null);
1453
+ let startIdx = null;
1454
+ let endIdx = null;
1455
+
1456
+ const onlyTable = function (current) {
1457
+ return domUtils.isTableElements(current) ? /^TABLE$/i.test(current.nodeName) : true;
1458
+ };
1459
+
1460
+ let startRangeEl = this.getBlock(startLine, onlyTable);
1461
+ let endRangeEl = this.getBlock(endLine, onlyTable);
1462
+ if (domUtils.isTableElements(startRangeEl) && domUtils.isListCell(startRangeEl.parentNode)) startRangeEl = startRangeEl.parentNode;
1463
+ if (domUtils.isTableElements(endRangeEl) && domUtils.isListCell(endRangeEl.parentNode)) endRangeEl = endRangeEl.parentNode;
1464
+
1465
+ const sameRange = startRangeEl === endRangeEl;
1466
+ for (let i = 0, len = lineNodes.length, line; i < len; i++) {
1467
+ line = lineNodes[i];
1468
+
1469
+ if (startLine === line || (!sameRange && line === startRangeEl)) {
1470
+ startIdx = i;
1471
+ continue;
1472
+ }
1473
+
1474
+ if (endLine === line || (!sameRange && line === endRangeEl)) {
1475
+ endIdx = i;
1476
+ break;
1477
+ }
1478
+ }
1479
+
1480
+ if (startIdx === null) startIdx = 0;
1481
+ if (endIdx === null) endIdx = lineNodes.length - 1;
1482
+
1483
+ return lineNodes.slice(startIdx, endIdx + 1);
1484
+ },
1485
+
1486
+ /**
1487
+ * @description Get lines and components from the selected range. (P, DIV, H[1-6], OL, UL, TABLE..)
1488
+ * If some of the component are included in the selection, get the entire that component.
1489
+ * @param {boolean} removeDuplicate If true, if there is a parent and child tag among the selected elements, the child tag is excluded.
1490
+ * @returns {Array}
1491
+ */
1492
+ getLinesAndComponents(removeDuplicate) {
1493
+ const commonCon = this.selection.getRange().commonAncestorContainer;
1494
+ const myComponent = domUtils.getParentElement(commonCon, this.component.is.bind(this.component));
1495
+ const selectedLines = domUtils.isTableElements(commonCon)
1496
+ ? this.getLines(null)
1497
+ : this.getLines((current) => {
1498
+ const component = domUtils.getParentElement(current, this.component.is.bind(this.component));
1499
+ return (this.isLine(current) && (!component || component === myComponent)) || (this.component.is(current) && !this.getLine(current));
1500
+ });
1501
+
1502
+ if (removeDuplicate) {
1503
+ for (let i = 0, len = selectedLines.length; i < len; i++) {
1504
+ for (let j = i - 1; j >= 0; j--) {
1505
+ if (selectedLines[j].contains(selectedLines[i])) {
1506
+ selectedLines.splice(i, 1);
1507
+ i--;
1508
+ len--;
1509
+ break;
1510
+ }
1511
+ }
1512
+ }
1513
+ }
1514
+
1515
+ return selectedLines;
1516
+ },
1517
+
1518
+ /**
1519
+ * @description A function that distinguishes areas where "selection" should not be placed
1520
+ * @param {Element} element Element
1521
+ * @returns {boolean}
1522
+ * @private
1523
+ */
1524
+ _isExcludeSelectionElement(element) {
1525
+ return !/FIGCAPTION/i.test(element.nodeName) && (this.component.is(element) || /FIGURE/i.test(element.nodeName));
1526
+ },
1527
+
1528
+ /**
1529
+ * @description A function that distinguishes non-formatting HTML elements or tags from formatting ones.
1530
+ * @param {Element} element Element
1531
+ * @returns {boolean}
1532
+ * @private
1533
+ */
1534
+ _nonFormat(element) {
1535
+ return domUtils.isExcludeFormat(element) || this.component.is(element) || domUtils.isWysiwygFrame(element);
1536
+ },
1537
+
1538
+ /**
1539
+ * @description Nodes that must remain undetached when changing text nodes (A, Label, Code, Span:font-size)
1540
+ * @param {Node|String} element Element to check
1541
+ * @returns {boolean}
1542
+ * @private
1543
+ */
1544
+ _isNonSplitNode(element) {
1545
+ return element && element.nodeType !== 3 && /^(a|label|code|summary)$/i.test(typeof element === 'string' ? element : element.nodeName);
1546
+ },
1547
+
1548
+ /**
1549
+ * @description Nodes without text
1550
+ * @param {Node} element Element to check
1551
+ * @returns {boolean}
1552
+ * @private
1553
+ */
1554
+ _notTextNode(element) {
1555
+ return element && element.nodeType !== 3 && (this.component.is(element) || /^(br|input|select|canvas|img|iframe|audio|video)$/i.test(typeof element === 'string' ? element : element.nodeName));
1556
+ },
1557
+
1558
+ /**
1559
+ * @description Nodes that need to be added without modification when changing text nodes
1560
+ * @param {Node} element Element to check
1561
+ * @returns {boolean}
1562
+ * @private
1563
+ */
1564
+ _isIgnoreNodeChange(element) {
1565
+ return element && element.nodeType !== 3 && (domUtils.isNonEditable(element) || !this.isTextStyleNode(element));
1566
+ },
1567
+
1568
+ /**
1569
+ * @description Get current selected lines and selected node info.
1570
+ * @returns { lines: Array.<Element>, firstNode: Node, lastNode: Node, firstPath: Array.<number>, lastPath: Array.<number>, startOffset: number, endOffset: number }
1571
+ * @private
1572
+ */
1573
+ _lineWork() {
1574
+ let range = this.selection.getRange();
1575
+ let selectedFormsts = this.getLinesAndComponents(false);
1576
+
1577
+ if (selectedFormsts.length === 0) {
1578
+ range = this.selection.getRangeAndAddLine(range, null);
1579
+ selectedFormsts = this.getLinesAndComponents(false);
1580
+ if (selectedFormsts.length === 0) return;
1581
+ }
1582
+
1583
+ const startOffset = range.startOffset;
1584
+ const endOffset = range.endOffset;
1585
+
1586
+ let first = selectedFormsts[0];
1587
+ let last = selectedFormsts[selectedFormsts.length - 1];
1588
+ const firstPath = domUtils.getNodePath(range.startContainer, first, null, null);
1589
+ const lastPath = domUtils.getNodePath(range.endContainer, last, null, null);
1590
+
1591
+ // remove selected list
1592
+ const rlist = this.removeList(selectedFormsts, false);
1593
+ if (rlist.sc) first = rlist.sc;
1594
+ if (rlist.ec) last = rlist.ec;
1595
+
1596
+ // change format tag
1597
+ this.selection.setRange(domUtils.getNodeFromPath(firstPath, first), startOffset, domUtils.getNodeFromPath(lastPath, last), endOffset);
1598
+
1599
+ return {
1600
+ lines: this.getLinesAndComponents(false),
1601
+ firstNode: first,
1602
+ lastNode: last,
1603
+ firstPath: firstPath,
1604
+ lastPath: lastPath,
1605
+ startOffset: startOffset,
1606
+ endOffset: endOffset
1607
+ };
1608
+ },
1609
+
1610
+ _attachNested(originList, innerList, prev, next, nodePath) {
1611
+ let insertPrev = false;
1612
+
1613
+ if (innerList.tagName === prev?.tagName) {
1614
+ const children = innerList.children;
1615
+ while (children[0]) {
1616
+ prev.appendChild(children[0]);
1617
+ }
1618
+
1619
+ innerList = prev;
1620
+ insertPrev = true;
1621
+ }
1622
+
1623
+ if (innerList.tagName === next?.tagName) {
1624
+ const children = next.children;
1625
+ while (children[0]) {
1626
+ innerList.appendChild(children[0]);
1627
+ }
1628
+
1629
+ const temp = next.nextElementSibling;
1630
+ next.parentNode.removeChild(next);
1631
+ next = temp;
1632
+ }
1633
+
1634
+ if (!insertPrev) {
1635
+ if (domUtils.isListCell(prev)) {
1636
+ originList = prev;
1637
+ next = null;
1638
+ }
1639
+
1640
+ originList.insertBefore(innerList, next);
1641
+
1642
+ if (!nodePath.s) {
1643
+ nodePath.s = domUtils.getNodePath(innerList.firstElementChild.firstChild, originList, null);
1644
+ nodePath.sl = originList;
1645
+ }
1646
+
1647
+ const slPath = originList.contains(nodePath.sl) ? domUtils.getNodePath(nodePath.sl, originList) : null;
1648
+ nodePath.e = domUtils.getNodePath(innerList.lastElementChild.firstChild, originList, null);
1649
+ nodePath.el = originList;
1650
+
1651
+ this.nodeTransform.mergeSameTags(originList, [nodePath.s, nodePath.e, slPath], false);
1652
+ this.nodeTransform.mergeNestedTags(originList);
1653
+ if (slPath) nodePath.sl = domUtils.getNodeFromPath(slPath, originList);
1654
+ }
1655
+
1656
+ return innerList;
1657
+ },
1658
+
1659
+ _detachNested(cells) {
1660
+ const first = cells[0];
1661
+ const last = cells[cells.length - 1];
1662
+ const next = last.nextElementSibling;
1663
+ const originList = first.parentNode;
1664
+ const sibling = originList.parentNode.nextElementSibling;
1665
+ const parentNode = originList.parentNode.parentNode;
1666
+
1667
+ for (let c = 0, cLen = cells.length; c < cLen; c++) {
1668
+ parentNode.insertBefore(cells[c], sibling);
1669
+ }
1670
+
1671
+ if (next && originList.children.length > 0) {
1672
+ const newList = originList.cloneNode(false);
1673
+ const children = originList.childNodes;
1674
+ const index = domUtils.getPositionIndex(next);
1675
+ while (children[index]) {
1676
+ newList.appendChild(children[index]);
1677
+ }
1678
+ last.appendChild(newList);
1679
+ }
1680
+
1681
+ if (originList.children.length === 0) domUtils.removeItem(originList);
1682
+ this.nodeTransform.mergeSameTags(parentNode);
1683
+
1684
+ const edge = domUtils.getEdgeChildNodes(first, last);
1685
+
1686
+ return {
1687
+ cc: first.parentNode,
1688
+ sc: edge.sc,
1689
+ ec: edge.ec
1690
+ };
1691
+ },
1692
+
1693
+ /**
1694
+ * @description Nest list cells or cancel nested cells.
1695
+ * @param selectedCells List cells.
1696
+ * @param nested Nested or cancel nested.
1697
+ * @private
1698
+ */
1699
+ _applyNestedList(selectedCells, nested) {
1700
+ selectedCells = !selectedCells
1701
+ ? this.getLines().filter(function (el) {
1702
+ return domUtils.isListCell(el);
1703
+ })
1704
+ : selectedCells;
1705
+ const cellsLen = selectedCells.length;
1706
+ if (cellsLen === 0 || (!nested && !domUtils.isListCell(selectedCells[0].previousElementSibling) && !domUtils.isListCell(selectedCells[cellsLen - 1].nextElementSibling))) {
1707
+ return {
1708
+ sc: selectedCells[0],
1709
+ so: 0,
1710
+ ec: selectedCells[cellsLen - 1],
1711
+ eo: 1
1712
+ };
1713
+ }
1714
+
1715
+ let originList = selectedCells[0].parentNode;
1716
+ let lastCell = selectedCells[cellsLen - 1];
1717
+ let range = null;
1718
+
1719
+ if (nested) {
1720
+ if (originList !== lastCell.parentNode && domUtils.isList(lastCell.parentNode.parentNode) && lastCell.nextElementSibling) {
1721
+ lastCell = lastCell.nextElementSibling;
1722
+ while (lastCell) {
1723
+ selectedCells.push(lastCell);
1724
+ lastCell = lastCell.nextElementSibling;
1725
+ }
1726
+ }
1727
+ range = this.applyList(originList.nodeName + ':' + originList.style.listStyleType, selectedCells, true);
1728
+ } else {
1729
+ let innerList = domUtils.createElement(originList.nodeName);
1730
+ let prev = selectedCells[0].previousElementSibling;
1731
+ let next = lastCell.nextElementSibling;
1732
+ const nodePath = {
1733
+ s: null,
1734
+ e: null,
1735
+ sl: originList,
1736
+ el: originList
1737
+ };
1738
+
1739
+ for (let i = 0, len = cellsLen, c; i < len; i++) {
1740
+ c = selectedCells[i];
1741
+ if (c.parentNode !== originList) {
1742
+ this._attachNested(originList, innerList, prev, next, nodePath);
1743
+ originList = c.parentNode;
1744
+ innerList = domUtils.createElement(originList.nodeName);
1745
+ }
1746
+
1747
+ prev = c.previousElementSibling;
1748
+ next = c.nextElementSibling;
1749
+ innerList.appendChild(c);
1750
+ }
1751
+
1752
+ this._attachNested(originList, innerList, prev, next, nodePath);
1753
+
1754
+ const sc = domUtils.getNodeFromPath(nodePath.s, nodePath.sl);
1755
+ const ec = domUtils.getNodeFromPath(nodePath.e, nodePath.el);
1756
+ range = {
1757
+ sc: sc,
1758
+ so: 0,
1759
+ ec: ec,
1760
+ eo: ec.textContent.length
1761
+ };
1762
+ }
1763
+
1764
+ return range;
1765
+ },
1766
+
1767
+ /**
1768
+ * @description Detach Nested all nested lists under the "baseNode".
1769
+ * Returns a list with nested removed.
1770
+ * @param {Node} baseNode Element on which to base.
1771
+ * @param {boolean} all If true, it also detach all nested lists of a returned list.
1772
+ * @returns {Element}
1773
+ * @private
1774
+ */
1775
+ _removeNestedList(baseNode, all) {
1776
+ const rNode = DeleteNestedList(baseNode);
1777
+ let rangeElement, cNodes;
1778
+
1779
+ if (rNode) {
1780
+ rangeElement = rNode.cloneNode(false);
1781
+ cNodes = rNode.childNodes;
1782
+ const index = domUtils.getPositionIndex(baseNode);
1783
+ while (cNodes[index]) {
1784
+ rangeElement.appendChild(cNodes[index]);
1785
+ }
1786
+ } else {
1787
+ rangeElement = baseNode;
1788
+ }
1789
+
1790
+ let rChildren;
1791
+ if (!all) {
1792
+ const depth = domUtils.getNodeDepth(baseNode) + 2;
1793
+ rChildren = domUtils.getListChildren(baseNode, (current) => {
1794
+ return domUtils.isListCell(current) && !current.previousElementSibling && domUtils.getNodeDepth(current) === depth;
1795
+ });
1796
+ } else {
1797
+ rChildren = domUtils.getListChildren(rangeElement, (current) => {
1798
+ return domUtils.isListCell(current) && !current.previousElementSibling;
1799
+ });
1800
+ }
1801
+
1802
+ for (let i = 0, len = rChildren.length; i < len; i++) {
1803
+ DeleteNestedList(rChildren[i]);
1804
+ }
1805
+
1806
+ if (rNode) {
1807
+ rNode.parentNode.insertBefore(rangeElement, rNode.nextSibling);
1808
+ if (cNodes?.length === 0) domUtils.removeItem(rNode);
1809
+ }
1810
+
1811
+ return rangeElement === baseNode ? rangeElement.parentNode : rangeElement;
1812
+ },
1813
+
1814
+ /**
1815
+ * @description wraps text nodes of line selected text.
1816
+ * @param {Element} element The node of the line that contains the selected text node.
1817
+ * @param {Element} newInnerNode The dom that will wrap the selected text area
1818
+ * @param {Function} validation Check if the node should be stripped.
1819
+ * @param {Node} startCon The startContainer property of the selection object.
1820
+ * @param {number} startOff The startOffset property of the selection object.
1821
+ * @param {Node} endCon The endContainer property of the selection object.
1822
+ * @param {number} endOff The endOffset property of the selection object.
1823
+ * @param {boolean} isRemoveFormat Is the remove all formats command?
1824
+ * @param {boolean} isRemoveNode "newInnerNode" is remove node?
1825
+ * @param {boolean} collapsed range.collapsed
1826
+ * @returns {{ancestor: *, startContainer: *, startOffset: *, endContainer: *, endOffset: *}}
1827
+ * @private
1828
+ */
1829
+ _setNode_oneLine(element, newInnerNode, validation, startCon, startOff, endCon, endOff, isRemoveFormat, isRemoveNode, collapsed, _removeCheck, _getMaintainedNode, _isMaintainedNode) {
1830
+ // not add tag
1831
+ let parentCon = startCon.parentNode;
1832
+ while (!parentCon.nextSibling && !parentCon.previousSibling && !this.isLine(parentCon.parentNode) && !domUtils.isWysiwygFrame(parentCon.parentNode)) {
1833
+ if (parentCon.nodeName === newInnerNode.nodeName) break;
1834
+ parentCon = parentCon.parentNode;
1835
+ }
1836
+
1837
+ if (!isRemoveNode && parentCon === endCon.parentNode && parentCon.nodeName === newInnerNode.nodeName) {
1838
+ if (domUtils.isZeroWith(startCon.textContent.slice(0, startOff)) && domUtils.isZeroWith(endCon.textContent.slice(endOff))) {
1839
+ const children = parentCon.childNodes;
1840
+ let sameTag = false;
1841
+
1842
+ for (let i = 0, len = children.length, c, s, e, z; i < len; i++) {
1843
+ c = children[i];
1844
+ z = !domUtils.isZeroWith(c);
1845
+ if (c === startCon) {
1846
+ s = true;
1847
+ continue;
1848
+ }
1849
+ if (c === endCon) {
1850
+ e = true;
1851
+ continue;
1852
+ }
1853
+ if ((!s && z) || (s && e && z)) {
1854
+ sameTag = false;
1855
+ break;
1856
+ }
1857
+ }
1858
+
1859
+ if (sameTag) {
1860
+ domUtils.copyTagAttributes(parentCon, newInnerNode);
1861
+
1862
+ return {
1863
+ ancestor: element,
1864
+ startContainer: startCon,
1865
+ startOffset: startOff,
1866
+ endContainer: endCon,
1867
+ endOffset: endOff
1868
+ };
1869
+ }
1870
+ }
1871
+ }
1872
+
1873
+ // add tag
1874
+ _removeCheck.v = false;
1875
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
1876
+ const inst = this;
1877
+ const el = element;
1878
+ const nNodeArray = [newInnerNode];
1879
+ const pNode = element.cloneNode(false);
1880
+ const isSameNode = startCon === endCon;
1881
+ let startContainer = startCon;
1882
+ let startOffset = startOff;
1883
+ let endContainer = endCon;
1884
+ let endOffset = endOff;
1885
+ let startPass = false;
1886
+ let endPass = false;
1887
+ let pCurrent, newNode, appendNode, cssText, anchorNode;
1888
+
1889
+ const wRegExp = RegExp;
1890
+ function checkCss(vNode) {
1891
+ const regExp = new wRegExp('(?:;|^|\\s)(?:' + cssText + 'null)\\s*:[^;]*\\s*(?:;|$)', 'ig');
1892
+ let style = '';
1893
+
1894
+ if (regExp && vNode.style.cssText.length > 0) {
1895
+ style = regExp.test(vNode.style.cssText);
1896
+ }
1897
+
1898
+ return !style;
1899
+ }
1900
+
1901
+ (function recursionFunc(current, ancestor) {
1902
+ const childNodes = current.childNodes;
1903
+
1904
+ for (let i = 0, len = childNodes.length, vNode; i < len; i++) {
1905
+ const child = childNodes[i];
1906
+ if (!child) continue;
1907
+ let coverNode = ancestor;
1908
+ let cloneNode;
1909
+
1910
+ // startContainer
1911
+ if (!startPass && child === startContainer) {
1912
+ let line = pNode;
1913
+ anchorNode = _getMaintainedNode(child);
1914
+ const prevNode = domUtils.createTextNode(startContainer.nodeType === 1 ? '' : startContainer.substringData(0, startOffset));
1915
+ const textNode = domUtils.createTextNode(
1916
+ startContainer.nodeType === 1
1917
+ ? ''
1918
+ : startContainer.substringData(startOffset, isSameNode ? (endOffset >= startOffset ? endOffset - startOffset : startContainer.data.length - startOffset) : startContainer.data.length - startOffset)
1919
+ );
1920
+
1921
+ if (anchorNode) {
1922
+ const a = _getMaintainedNode(ancestor);
1923
+ if (a.parentNode !== line) {
1924
+ let m = a;
1925
+ let p = null;
1926
+ while (m.parentNode !== line) {
1927
+ ancestor = p = m.parentNode.cloneNode(false);
1928
+ while (m.childNodes[0]) {
1929
+ p.appendChild(m.childNodes[0]);
1930
+ }
1931
+ m.appendChild(p);
1932
+ m = m.parentNode;
1933
+ }
1934
+ m.parentNode.appendChild(a);
1935
+ }
1936
+ anchorNode = anchorNode.cloneNode(false);
1937
+ }
1938
+
1939
+ if (!domUtils.isZeroWith(prevNode)) {
1940
+ ancestor.appendChild(prevNode);
1941
+ }
1942
+
1943
+ const prevAnchorNode = _getMaintainedNode(ancestor);
1944
+ if (prevAnchorNode) anchorNode = prevAnchorNode;
1945
+ if (anchorNode) line = anchorNode;
1946
+
1947
+ newNode = child;
1948
+ pCurrent = [];
1949
+ cssText = '';
1950
+ while (newNode !== line && newNode !== el && newNode !== null) {
1951
+ vNode = _isMaintainedNode(newNode) ? null : validation(newNode);
1952
+ if (vNode && newNode.nodeType === 1 && checkCss(newNode)) {
1953
+ pCurrent.push(vNode);
1954
+ cssText += newNode.style.cssText.substr(0, newNode.style.cssText.indexOf(':')) + '|';
1955
+ }
1956
+ newNode = newNode.parentNode;
1957
+ }
1958
+
1959
+ const childNode = pCurrent.pop() || textNode;
1960
+ appendNode = newNode = childNode;
1961
+ while (pCurrent.length > 0) {
1962
+ newNode = pCurrent.pop();
1963
+ appendNode.appendChild(newNode);
1964
+ appendNode = newNode;
1965
+ }
1966
+
1967
+ newInnerNode.appendChild(childNode);
1968
+ line.appendChild(newInnerNode);
1969
+
1970
+ if (anchorNode && !_getMaintainedNode(endContainer)) {
1971
+ newInnerNode = newInnerNode.cloneNode(false);
1972
+ pNode.appendChild(newInnerNode);
1973
+ nNodeArray.push(newInnerNode);
1974
+ }
1975
+
1976
+ startContainer = textNode;
1977
+ startOffset = 0;
1978
+ startPass = true;
1979
+
1980
+ if (newNode !== textNode) newNode.appendChild(startContainer);
1981
+ if (!isSameNode) continue;
1982
+ }
1983
+
1984
+ // endContainer
1985
+ if (!endPass && child === endContainer) {
1986
+ anchorNode = _getMaintainedNode(child);
1987
+ const afterNode = domUtils.createTextNode(endContainer.nodeType === 1 ? '' : endContainer.substringData(endOffset, endContainer.length - endOffset));
1988
+ const textNode = domUtils.createTextNode(isSameNode || endContainer.nodeType === 1 ? '' : endContainer.substringData(0, endOffset));
1989
+
1990
+ if (anchorNode) {
1991
+ anchorNode = anchorNode.cloneNode(false);
1992
+ } else if (_isMaintainedNode(newInnerNode.parentNode) && !anchorNode) {
1993
+ newInnerNode = newInnerNode.cloneNode(false);
1994
+ pNode.appendChild(newInnerNode);
1995
+ nNodeArray.push(newInnerNode);
1996
+ }
1997
+
1998
+ if (!domUtils.isZeroWith(afterNode)) {
1999
+ newNode = child;
2000
+ cssText = '';
2001
+ pCurrent = [];
2002
+ const anchors = [];
2003
+ while (newNode !== pNode && newNode !== el && newNode !== null) {
2004
+ if (newNode.nodeType === 1 && checkCss(newNode)) {
2005
+ if (_isMaintainedNode(newNode)) anchors.push(newNode.cloneNode(false));
2006
+ else pCurrent.push(newNode.cloneNode(false));
2007
+ cssText += newNode.style.cssText.substr(0, newNode.style.cssText.indexOf(':')) + '|';
2008
+ }
2009
+ newNode = newNode.parentNode;
2010
+ }
2011
+ pCurrent = pCurrent.concat(anchors);
2012
+
2013
+ cloneNode = appendNode = newNode = pCurrent.pop() || afterNode;
2014
+ while (pCurrent.length > 0) {
2015
+ newNode = pCurrent.pop();
2016
+ appendNode.appendChild(newNode);
2017
+ appendNode = newNode;
2018
+ }
2019
+
2020
+ pNode.appendChild(cloneNode);
2021
+ newNode.textContent = afterNode.data;
2022
+ }
2023
+
2024
+ if (anchorNode && cloneNode) {
2025
+ const afterAnchorNode = _getMaintainedNode(cloneNode);
2026
+ if (afterAnchorNode) {
2027
+ anchorNode = afterAnchorNode;
2028
+ }
2029
+ }
2030
+
2031
+ newNode = child;
2032
+ pCurrent = [];
2033
+ cssText = '';
2034
+ while (newNode !== pNode && newNode !== el && newNode !== null) {
2035
+ vNode = _isMaintainedNode(newNode) ? null : validation(newNode);
2036
+ if (vNode && newNode.nodeType === 1 && checkCss(newNode)) {
2037
+ pCurrent.push(vNode);
2038
+ cssText += newNode.style.cssText.substr(0, newNode.style.cssText.indexOf(':')) + '|';
2039
+ }
2040
+ newNode = newNode.parentNode;
2041
+ }
2042
+
2043
+ const childNode = pCurrent.pop() || textNode;
2044
+ appendNode = newNode = childNode;
2045
+ while (pCurrent.length > 0) {
2046
+ newNode = pCurrent.pop();
2047
+ appendNode.appendChild(newNode);
2048
+ appendNode = newNode;
2049
+ }
2050
+
2051
+ if (anchorNode) {
2052
+ newInnerNode = newInnerNode.cloneNode(false);
2053
+ newInnerNode.appendChild(childNode);
2054
+ anchorNode.insertBefore(newInnerNode, anchorNode.firstChild);
2055
+ pNode.appendChild(anchorNode);
2056
+ nNodeArray.push(newInnerNode);
2057
+ anchorNode = null;
2058
+ } else {
2059
+ newInnerNode.appendChild(childNode);
2060
+ }
2061
+
2062
+ endContainer = textNode;
2063
+ endOffset = textNode.data.length;
2064
+ endPass = true;
2065
+
2066
+ if (!isRemoveFormat && collapsed) {
2067
+ newInnerNode = textNode;
2068
+ textNode.textContent = unicode.zeroWidthSpace;
2069
+ }
2070
+
2071
+ if (newNode !== textNode) newNode.appendChild(endContainer);
2072
+ continue;
2073
+ }
2074
+
2075
+ // other
2076
+ if (startPass) {
2077
+ if (child.nodeType === 1 && !domUtils.isBreak(child)) {
2078
+ if (inst._isIgnoreNodeChange(child)) {
2079
+ pNode.appendChild(child.cloneNode(true));
2080
+ if (!collapsed) {
2081
+ newInnerNode = newInnerNode.cloneNode(false);
2082
+ pNode.appendChild(newInnerNode);
2083
+ nNodeArray.push(newInnerNode);
2084
+ }
2085
+ } else {
2086
+ recursionFunc(child, child);
2087
+ }
2088
+ continue;
2089
+ }
2090
+
2091
+ newNode = child;
2092
+ pCurrent = [];
2093
+ cssText = '';
2094
+ const anchors = [];
2095
+ while (newNode.parentNode !== null && newNode !== el && newNode !== newInnerNode) {
2096
+ vNode = endPass ? newNode.cloneNode(false) : validation(newNode);
2097
+ if (newNode.nodeType === 1 && !domUtils.isBreak(child) && vNode && checkCss(newNode)) {
2098
+ if (_isMaintainedNode(newNode)) {
2099
+ if (!anchorNode) anchors.push(vNode);
2100
+ } else {
2101
+ pCurrent.push(vNode);
2102
+ }
2103
+ cssText += newNode.style.cssText.substr(0, newNode.style.cssText.indexOf(':')) + '|';
2104
+ }
2105
+ newNode = newNode.parentNode;
2106
+ }
2107
+ pCurrent = pCurrent.concat(anchors);
2108
+
2109
+ const childNode = pCurrent.pop() || child;
2110
+ appendNode = newNode = childNode;
2111
+ while (pCurrent.length > 0) {
2112
+ newNode = pCurrent.pop();
2113
+ appendNode.appendChild(newNode);
2114
+ appendNode = newNode;
2115
+ }
2116
+
2117
+ if (_isMaintainedNode(newInnerNode.parentNode) && !_isMaintainedNode(childNode) && !domUtils.isZeroWith(newInnerNode)) {
2118
+ newInnerNode = newInnerNode.cloneNode(false);
2119
+ pNode.appendChild(newInnerNode);
2120
+ nNodeArray.push(newInnerNode);
2121
+ }
2122
+
2123
+ if (!endPass && !anchorNode && _isMaintainedNode(childNode)) {
2124
+ newInnerNode = newInnerNode.cloneNode(false);
2125
+ const aChildren = childNode.childNodes;
2126
+ for (let a = 0, aLen = aChildren.length; a < aLen; a++) {
2127
+ newInnerNode.appendChild(aChildren[a]);
2128
+ }
2129
+ childNode.appendChild(newInnerNode);
2130
+ pNode.appendChild(childNode);
2131
+ nNodeArray.push(newInnerNode);
2132
+ if (newInnerNode.children.length > 0) ancestor = newNode;
2133
+ else ancestor = newInnerNode;
2134
+ } else if (childNode === child) {
2135
+ if (!endPass) ancestor = newInnerNode;
2136
+ else ancestor = pNode;
2137
+ } else if (endPass) {
2138
+ pNode.appendChild(childNode);
2139
+ ancestor = newNode;
2140
+ } else {
2141
+ newInnerNode.appendChild(childNode);
2142
+ ancestor = newNode;
2143
+ }
2144
+
2145
+ if (anchorNode && child.nodeType === 3) {
2146
+ if (_getMaintainedNode(child)) {
2147
+ const ancestorAnchorNode = domUtils.getParentElement(ancestor, (c) => {
2148
+ return inst._isNonSplitNode(c.parentNode) || c.parentNode === pNode;
2149
+ });
2150
+ anchorNode.appendChild(ancestorAnchorNode);
2151
+ newInnerNode = ancestorAnchorNode.cloneNode(false);
2152
+ nNodeArray.push(newInnerNode);
2153
+ pNode.appendChild(newInnerNode);
2154
+ } else {
2155
+ anchorNode = null;
2156
+ }
2157
+ }
2158
+ }
2159
+
2160
+ cloneNode = child.cloneNode(false);
2161
+ ancestor.appendChild(cloneNode);
2162
+ if (child.nodeType === 1 && !domUtils.isBreak(child)) coverNode = cloneNode;
2163
+
2164
+ recursionFunc(child, coverNode);
2165
+ }
2166
+ })(element, pNode);
2167
+
2168
+ // not remove tag
2169
+ if (isRemoveNode && !isRemoveFormat && !_removeCheck.v) {
2170
+ return {
2171
+ ancestor: element,
2172
+ startContainer: startCon,
2173
+ startOffset: startOff,
2174
+ endContainer: endCon,
2175
+ endOffset: endOff
2176
+ };
2177
+ }
2178
+
2179
+ isRemoveFormat = isRemoveFormat && isRemoveNode;
2180
+
2181
+ if (isRemoveFormat) {
2182
+ for (let i = 0; i < nNodeArray.length; i++) {
2183
+ const removeNode = nNodeArray[i];
2184
+ let textNode, textNode_s, textNode_e;
2185
+
2186
+ if (collapsed) {
2187
+ textNode = domUtils.createTextNode(unicode.zeroWidthSpace);
2188
+ pNode.replaceChild(textNode, removeNode);
2189
+ } else {
2190
+ const rChildren = removeNode.childNodes;
2191
+ textNode_s = rChildren[0];
2192
+ while (rChildren[0]) {
2193
+ textNode_e = rChildren[0];
2194
+ pNode.insertBefore(textNode_e, removeNode);
2195
+ }
2196
+ domUtils.removeItem(removeNode);
2197
+ }
2198
+
2199
+ if (i === 0) {
2200
+ if (collapsed) {
2201
+ startContainer = endContainer = textNode;
2202
+ } else {
2203
+ startContainer = textNode_s;
2204
+ endContainer = textNode_e;
2205
+ }
2206
+ }
2207
+ }
2208
+ } else {
2209
+ if (isRemoveNode) {
2210
+ for (let i = 0; i < nNodeArray.length; i++) {
2211
+ SN_StripRemoveNode(nNodeArray[i]);
2212
+ }
2213
+ }
2214
+
2215
+ if (collapsed) {
2216
+ startContainer = endContainer = newInnerNode;
2217
+ }
2218
+ }
2219
+
2220
+ this.nodeTransform.removeEmptyNode(pNode, newInnerNode, false);
2221
+
2222
+ if (collapsed) {
2223
+ startOffset = startContainer.textContent.length;
2224
+ endOffset = endContainer.textContent.length;
2225
+ }
2226
+
2227
+ // endContainer reset
2228
+ const endConReset = isRemoveFormat || endContainer.textContent.length === 0;
2229
+
2230
+ if (!domUtils.isBreak(endContainer) && endContainer.textContent.length === 0) {
2231
+ domUtils.removeItem(endContainer);
2232
+ endContainer = startContainer;
2233
+ }
2234
+ endOffset = endConReset ? endContainer.textContent.length : endOffset;
2235
+
2236
+ // node change
2237
+ const newStartOffset = {
2238
+ s: 0,
2239
+ e: 0
2240
+ };
2241
+ const startPath = domUtils.getNodePath(startContainer, pNode, newStartOffset);
2242
+
2243
+ const mergeEndCon = !endContainer.parentNode;
2244
+ if (mergeEndCon) endContainer = startContainer;
2245
+ const newEndOffset = {
2246
+ s: 0,
2247
+ e: 0
2248
+ };
2249
+ const endPath = domUtils.getNodePath(endContainer, pNode, !mergeEndCon && !endConReset ? newEndOffset : null);
2250
+
2251
+ startOffset += newStartOffset.s;
2252
+ endOffset = collapsed ? startOffset : mergeEndCon ? startContainer.textContent.length : endConReset ? endOffset + newStartOffset.s : endOffset + newEndOffset.s;
2253
+
2254
+ // tag merge
2255
+ const newOffsets = this.nodeTransform.mergeSameTags(pNode, [startPath, endPath], true);
2256
+
2257
+ element.parentNode.replaceChild(pNode, element);
2258
+
2259
+ startContainer = domUtils.getNodeFromPath(startPath, pNode);
2260
+ endContainer = domUtils.getNodeFromPath(endPath, pNode);
2261
+
2262
+ return {
2263
+ ancestor: pNode,
2264
+ startContainer: startContainer,
2265
+ startOffset: startOffset + newOffsets[0],
2266
+ endContainer: endContainer,
2267
+ endOffset: endOffset + newOffsets[1]
2268
+ };
2269
+ },
2270
+
2271
+ /**
2272
+ * @description wraps first line selected text.
2273
+ * @param {Element} element The node of the line that contains the selected text node.
2274
+ * @param {Element} newInnerNode The dom that will wrap the selected text area
2275
+ * @param {Function} validation Check if the node should be stripped.
2276
+ * @param {Node} startCon The startContainer property of the selection object.
2277
+ * @param {number} startOff The startOffset property of the selection object.
2278
+ * @param {boolean} isRemoveFormat Is the remove all formats command?
2279
+ * @param {boolean} isRemoveNode "newInnerNode" is remove node?
2280
+ * @returns {null|Node} If end container is renewed, returned renewed node
2281
+ * @returns {Object} { ancestor, container, offset, endContainer }
2282
+ * @private
2283
+ */
2284
+ _setNode_startLine(element, newInnerNode, validation, startCon, startOff, isRemoveFormat, isRemoveNode, _removeCheck, _getMaintainedNode, _isMaintainedNode, _endContainer) {
2285
+ // not add tag
2286
+ let parentCon = startCon.parentNode;
2287
+ while (!parentCon.nextSibling && !parentCon.previousSibling && !this.isLine(parentCon.parentNode) && !domUtils.isWysiwygFrame(parentCon.parentNode)) {
2288
+ if (parentCon.nodeName === newInnerNode.nodeName) break;
2289
+ parentCon = parentCon.parentNode;
2290
+ }
2291
+
2292
+ if (!isRemoveNode && parentCon.nodeName === newInnerNode.nodeName && !this.isLine(parentCon) && !parentCon.nextSibling && domUtils.isZeroWith(startCon.textContent.slice(0, startOff))) {
2293
+ let sameTag = false;
2294
+ let s = startCon.previousSibling;
2295
+ while (s) {
2296
+ if (!domUtils.isZeroWith(s)) {
2297
+ sameTag = false;
2298
+ break;
2299
+ }
2300
+ s = s.previousSibling;
2301
+ }
2302
+
2303
+ if (sameTag) {
2304
+ domUtils.copyTagAttributes(parentCon, newInnerNode);
2305
+
2306
+ return {
2307
+ ancestor: element,
2308
+ container: startCon,
2309
+ offset: startOff
2310
+ };
2311
+ }
2312
+ }
2313
+
2314
+ // add tag
2315
+ _removeCheck.v = false;
2316
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
2317
+ const inst = this;
2318
+ const el = element;
2319
+ const nNodeArray = [newInnerNode];
2320
+ const pNode = element.cloneNode(false);
2321
+
2322
+ let container = startCon;
2323
+ let offset = startOff;
2324
+ let passNode = false;
2325
+ let pCurrent, newNode, appendNode, anchorNode;
2326
+
2327
+ (function recursionFunc(current, ancestor) {
2328
+ const childNodes = current.childNodes;
2329
+
2330
+ for (let i = 0, len = childNodes.length, vNode, cloneChild; i < len; i++) {
2331
+ const child = childNodes[i];
2332
+ if (!child) continue;
2333
+ let coverNode = ancestor;
2334
+
2335
+ if (passNode && !domUtils.isBreak(child)) {
2336
+ if (child.nodeType === 1) {
2337
+ if (inst._isIgnoreNodeChange(child)) {
2338
+ newInnerNode = newInnerNode.cloneNode(false);
2339
+ cloneChild = child.cloneNode(true);
2340
+ pNode.appendChild(cloneChild);
2341
+ pNode.appendChild(newInnerNode);
2342
+ nNodeArray.push(newInnerNode);
2343
+
2344
+ // end container
2345
+ if (_endContainer && child.contains(_endContainer)) {
2346
+ const endPath = domUtils.getNodePath(_endContainer, child);
2347
+ _endContainer = domUtils.getNodeFromPath(endPath, cloneChild);
2348
+ }
2349
+ } else {
2350
+ recursionFunc(child, child);
2351
+ }
2352
+ continue;
2353
+ }
2354
+
2355
+ newNode = child;
2356
+ pCurrent = [];
2357
+ const anchors = [];
2358
+ while (newNode.parentNode !== null && newNode !== el && newNode !== newInnerNode) {
2359
+ vNode = validation(newNode);
2360
+ if (newNode.nodeType === 1 && vNode) {
2361
+ if (_isMaintainedNode(newNode)) {
2362
+ if (!anchorNode) anchors.push(vNode);
2363
+ } else {
2364
+ pCurrent.push(vNode);
2365
+ }
2366
+ }
2367
+ newNode = newNode.parentNode;
2368
+ }
2369
+ pCurrent = pCurrent.concat(anchors);
2370
+
2371
+ const isTopNode = pCurrent.length > 0;
2372
+ const childNode = pCurrent.pop() || child;
2373
+ appendNode = newNode = childNode;
2374
+ while (pCurrent.length > 0) {
2375
+ newNode = pCurrent.pop();
2376
+ appendNode.appendChild(newNode);
2377
+ appendNode = newNode;
2378
+ }
2379
+
2380
+ if (_isMaintainedNode(newInnerNode.parentNode) && !_isMaintainedNode(childNode)) {
2381
+ newInnerNode = newInnerNode.cloneNode(false);
2382
+ pNode.appendChild(newInnerNode);
2383
+ nNodeArray.push(newInnerNode);
2384
+ }
2385
+
2386
+ if (!anchorNode && _isMaintainedNode(childNode)) {
2387
+ newInnerNode = newInnerNode.cloneNode(false);
2388
+ const aChildren = childNode.childNodes;
2389
+ for (let a = 0, aLen = aChildren.length; a < aLen; a++) {
2390
+ newInnerNode.appendChild(aChildren[a]);
2391
+ }
2392
+ childNode.appendChild(newInnerNode);
2393
+ pNode.appendChild(childNode);
2394
+ ancestor = !_isMaintainedNode(newNode) ? newNode : newInnerNode;
2395
+ nNodeArray.push(newInnerNode);
2396
+ } else if (isTopNode) {
2397
+ newInnerNode.appendChild(childNode);
2398
+ ancestor = newNode;
2399
+ } else {
2400
+ ancestor = newInnerNode;
2401
+ }
2402
+
2403
+ if (anchorNode && child.nodeType === 3) {
2404
+ if (_getMaintainedNode(child)) {
2405
+ const ancestorAnchorNode = domUtils.getParentElement(ancestor, (c) => {
2406
+ return inst._isNonSplitNode(c.parentNode) || c.parentNode === pNode;
2407
+ });
2408
+ anchorNode.appendChild(ancestorAnchorNode);
2409
+ newInnerNode = ancestorAnchorNode.cloneNode(false);
2410
+ nNodeArray.push(newInnerNode);
2411
+ pNode.appendChild(newInnerNode);
2412
+ } else {
2413
+ anchorNode = null;
2414
+ }
2415
+ }
2416
+ }
2417
+
2418
+ // startContainer
2419
+ if (!passNode && child === container) {
2420
+ let line = pNode;
2421
+ anchorNode = _getMaintainedNode(child);
2422
+ const prevNode = domUtils.createTextNode(container.nodeType === 1 ? '' : container.substringData(0, offset));
2423
+ const textNode = domUtils.createTextNode(container.nodeType === 1 ? '' : container.substringData(offset, container.length - offset));
2424
+
2425
+ if (anchorNode) {
2426
+ const a = _getMaintainedNode(ancestor);
2427
+ if (a && a.parentNode !== line) {
2428
+ let m = a;
2429
+ let p = null;
2430
+ while (m.parentNode !== line) {
2431
+ ancestor = p = m.parentNode.cloneNode(false);
2432
+ while (m.childNodes[0]) {
2433
+ p.appendChild(m.childNodes[0]);
2434
+ }
2435
+ m.appendChild(p);
2436
+ m = m.parentNode;
2437
+ }
2438
+ m.parentNode.appendChild(a);
2439
+ }
2440
+ anchorNode = anchorNode.cloneNode(false);
2441
+ }
2442
+
2443
+ if (!domUtils.isZeroWith(prevNode)) {
2444
+ ancestor.appendChild(prevNode);
2445
+ }
2446
+
2447
+ const prevAnchorNode = _getMaintainedNode(ancestor);
2448
+ if (prevAnchorNode) anchorNode = prevAnchorNode;
2449
+ if (anchorNode) line = anchorNode;
2450
+
2451
+ newNode = ancestor;
2452
+ pCurrent = [];
2453
+ while (newNode !== line && newNode !== null) {
2454
+ vNode = validation(newNode);
2455
+ if (newNode.nodeType === 1 && vNode) {
2456
+ pCurrent.push(vNode);
2457
+ }
2458
+ newNode = newNode.parentNode;
2459
+ }
2460
+
2461
+ const childNode = pCurrent.pop() || ancestor;
2462
+ appendNode = newNode = childNode;
2463
+ while (pCurrent.length > 0) {
2464
+ newNode = pCurrent.pop();
2465
+ appendNode.appendChild(newNode);
2466
+ appendNode = newNode;
2467
+ }
2468
+
2469
+ if (childNode !== ancestor) {
2470
+ newInnerNode.appendChild(childNode);
2471
+ ancestor = newNode;
2472
+ } else {
2473
+ ancestor = newInnerNode;
2474
+ }
2475
+
2476
+ if (domUtils.isBreak(child)) newInnerNode.appendChild(child.cloneNode(false));
2477
+ line.appendChild(newInnerNode);
2478
+
2479
+ container = textNode;
2480
+ offset = 0;
2481
+ passNode = true;
2482
+
2483
+ ancestor.appendChild(container);
2484
+ continue;
2485
+ }
2486
+
2487
+ vNode = !passNode ? child.cloneNode(false) : validation(child);
2488
+ if (vNode) {
2489
+ ancestor.appendChild(vNode);
2490
+ if (child.nodeType === 1 && !domUtils.isBreak(child)) coverNode = vNode;
2491
+ }
2492
+
2493
+ recursionFunc(child, coverNode);
2494
+ }
2495
+ })(element, pNode);
2496
+
2497
+ // not remove tag
2498
+ if (isRemoveNode && !isRemoveFormat && !_removeCheck.v) {
2499
+ return {
2500
+ ancestor: element,
2501
+ container: startCon,
2502
+ offset: startOff,
2503
+ endContainer: _endContainer
2504
+ };
2505
+ }
2506
+
2507
+ isRemoveFormat = isRemoveFormat && isRemoveNode;
2508
+
2509
+ if (isRemoveFormat) {
2510
+ for (let i = 0; i < nNodeArray.length; i++) {
2511
+ const removeNode = nNodeArray[i];
2512
+
2513
+ const rChildren = removeNode.childNodes;
2514
+ const textNode = rChildren[0];
2515
+ while (rChildren[0]) {
2516
+ pNode.insertBefore(rChildren[0], removeNode);
2517
+ }
2518
+ domUtils.removeItem(removeNode);
2519
+
2520
+ if (i === 0) container = textNode;
2521
+ }
2522
+ } else if (isRemoveNode) {
2523
+ newInnerNode = newInnerNode.firstChild;
2524
+ for (let i = 0; i < nNodeArray.length; i++) {
2525
+ SN_StripRemoveNode(nNodeArray[i]);
2526
+ }
2527
+ }
2528
+
2529
+ if (!isRemoveFormat && pNode.childNodes.length === 0) {
2530
+ if (element.childNodes) {
2531
+ container = element.childNodes[0];
2532
+ } else {
2533
+ container = domUtils.createTextNode(unicode.zeroWidthSpace);
2534
+ element.appendChild(container);
2535
+ }
2536
+ } else {
2537
+ this.nodeTransform.removeEmptyNode(pNode, newInnerNode, false);
2538
+
2539
+ if (domUtils.isZeroWith(pNode.textContent)) {
2540
+ container = pNode.firstChild;
2541
+ offset = 0;
2542
+ }
2543
+
2544
+ // node change
2545
+ const offsets = {
2546
+ s: 0,
2547
+ e: 0
2548
+ };
2549
+ const path = domUtils.getNodePath(container, pNode, offsets);
2550
+ offset += offsets.s;
2551
+
2552
+ // tag merge
2553
+ const newOffsets = this.nodeTransform.mergeSameTags(pNode, [path], true);
2554
+
2555
+ element.parentNode.replaceChild(pNode, element);
2556
+
2557
+ container = domUtils.getNodeFromPath(path, pNode);
2558
+ offset += newOffsets[0];
2559
+ }
2560
+
2561
+ return {
2562
+ ancestor: pNode,
2563
+ container: container,
2564
+ offset: offset,
2565
+ endContainer: _endContainer
2566
+ };
2567
+ },
2568
+
2569
+ /**
2570
+ * @description wraps mid lines selected text.
2571
+ * @param {Element} element The node of the line that contains the selected text node.
2572
+ * @param {Element} newInnerNode The dom that will wrap the selected text area
2573
+ * @param {Function} validation Check if the node should be stripped.
2574
+ * @param {boolean} isRemoveFormat Is the remove all formats command?
2575
+ * @param {boolean} isRemoveNode "newInnerNode" is remove node?
2576
+ * @param {Node} _endContainer Offset node of last line already modified (end.container)
2577
+ * @returns {Object} { ancestor, endContainer: "If end container is renewed, returned renewed node" }
2578
+ * @private
2579
+ */
2580
+ _setNode_middleLine(element, newInnerNode, validation, isRemoveFormat, isRemoveNode, _removeCheck, _endContainer) {
2581
+ // not add tag
2582
+ if (!isRemoveNode) {
2583
+ // end container path
2584
+ let endPath = null;
2585
+ if (_endContainer && element.contains(_endContainer)) endPath = domUtils.getNodePath(_endContainer, element);
2586
+
2587
+ const tempNode = element.cloneNode(true);
2588
+ const newNodeName = newInnerNode.nodeName;
2589
+ const newCssText = newInnerNode.style.cssText;
2590
+ const newClass = newInnerNode.className;
2591
+
2592
+ let children = tempNode.childNodes;
2593
+ let i = 0,
2594
+ len = children.length;
2595
+ for (let child; i < len; i++) {
2596
+ child = children[i];
2597
+ if (child.nodeType === 3) break;
2598
+ if (child.nodeName === newNodeName) {
2599
+ child.style.cssText += newCssText;
2600
+ domUtils.addClass(child, newClass);
2601
+ } else if (!domUtils.isBreak(child) && this._isIgnoreNodeChange(child)) {
2602
+ continue;
2603
+ } else if (len === 1) {
2604
+ children = child.childNodes;
2605
+ len = children.length;
2606
+ i = -1;
2607
+ continue;
2608
+ } else {
2609
+ break;
2610
+ }
2611
+ }
2612
+
2613
+ if (len > 0 && i === len) {
2614
+ element.innerHTML = tempNode.innerHTML;
2615
+ return {
2616
+ ancestor: element,
2617
+ endContainer: endPath ? domUtils.getNodeFromPath(endPath, element) : null
2618
+ };
2619
+ }
2620
+ }
2621
+
2622
+ // add tag
2623
+ _removeCheck.v = false;
2624
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
2625
+ const inst = this;
2626
+ const pNode = element.cloneNode(false);
2627
+ const nNodeArray = [newInnerNode];
2628
+ let noneChange = true;
2629
+
2630
+ (function recursionFunc(current, ancestor) {
2631
+ const childNodes = current.childNodes;
2632
+
2633
+ for (let i = 0, len = childNodes.length, vNode, cloneChild; i < len; i++) {
2634
+ const child = childNodes[i];
2635
+ if (!child) continue;
2636
+ let coverNode = ancestor;
2637
+
2638
+ if (!domUtils.isBreak(child) && inst._isIgnoreNodeChange(child)) {
2639
+ if (newInnerNode.childNodes.length > 0) {
2640
+ pNode.appendChild(newInnerNode);
2641
+ newInnerNode = newInnerNode.cloneNode(false);
2642
+ }
2643
+
2644
+ cloneChild = child.cloneNode(true);
2645
+ pNode.appendChild(cloneChild);
2646
+ pNode.appendChild(newInnerNode);
2647
+ nNodeArray.push(newInnerNode);
2648
+ ancestor = newInnerNode;
2649
+
2650
+ // end container
2651
+ if (_endContainer && child.contains(_endContainer)) {
2652
+ const endPath = domUtils.getNodePath(_endContainer, child);
2653
+ _endContainer = domUtils.getNodeFromPath(endPath, cloneChild);
2654
+ }
2655
+
2656
+ continue;
2657
+ } else {
2658
+ vNode = validation(child);
2659
+ if (vNode) {
2660
+ noneChange = false;
2661
+ ancestor.appendChild(vNode);
2662
+ if (child.nodeType === 1) coverNode = vNode;
2663
+ }
2664
+ }
2665
+
2666
+ if (!domUtils.isBreak(child)) recursionFunc(child, coverNode);
2667
+ }
2668
+ })(element, newInnerNode);
2669
+
2670
+ // not remove tag
2671
+ if (noneChange || (isRemoveNode && !isRemoveFormat && !_removeCheck.v))
2672
+ return {
2673
+ ancestor: element,
2674
+ endContainer: _endContainer
2675
+ };
2676
+
2677
+ pNode.appendChild(newInnerNode);
2678
+
2679
+ if (isRemoveFormat && isRemoveNode) {
2680
+ for (let i = 0; i < nNodeArray.length; i++) {
2681
+ const removeNode = nNodeArray[i];
2682
+
2683
+ const rChildren = removeNode.childNodes;
2684
+ while (rChildren[0]) {
2685
+ pNode.insertBefore(rChildren[0], removeNode);
2686
+ }
2687
+ domUtils.removeItem(removeNode);
2688
+ }
2689
+ } else if (isRemoveNode) {
2690
+ newInnerNode = newInnerNode.firstChild;
2691
+ for (let i = 0; i < nNodeArray.length; i++) {
2692
+ SN_StripRemoveNode(nNodeArray[i]);
2693
+ }
2694
+ }
2695
+
2696
+ this.nodeTransform.removeEmptyNode(pNode, newInnerNode, false);
2697
+ this.nodeTransform.mergeSameTags(pNode, null, true);
2698
+
2699
+ // node change
2700
+ element.parentNode.replaceChild(pNode, element);
2701
+ return {
2702
+ ancestor: pNode,
2703
+ endContainer: _endContainer
2704
+ };
2705
+ },
2706
+
2707
+ /**
2708
+ * @description wraps last line selected text.
2709
+ * @param {Element} element The node of the line that contains the selected text node.
2710
+ * @param {Element} newInnerNode The dom that will wrap the selected text area
2711
+ * @param {Function} validation Check if the node should be stripped.
2712
+ * @param {Node} endCon The endContainer property of the selection object.
2713
+ * @param {number} endOff The endOffset property of the selection object.
2714
+ * @param {boolean} isRemoveFormat Is the remove all formats command?
2715
+ * @param {boolean} isRemoveNode "newInnerNode" is remove node?
2716
+ * @returns {Object} { ancestor, container, offset }
2717
+ * @private
2718
+ */
2719
+ _setNode_endLine(element, newInnerNode, validation, endCon, endOff, isRemoveFormat, isRemoveNode, _removeCheck, _getMaintainedNode, _isMaintainedNode) {
2720
+ // not add tag
2721
+ let parentCon = endCon.parentNode;
2722
+ while (!parentCon.nextSibling && !parentCon.previousSibling && !this.isLine(parentCon.parentNode) && !domUtils.isWysiwygFrame(parentCon.parentNode)) {
2723
+ if (parentCon.nodeName === newInnerNode.nodeName) break;
2724
+ parentCon = parentCon.parentNode;
2725
+ }
2726
+
2727
+ if (!isRemoveNode && parentCon.nodeName === newInnerNode.nodeName && !this.isLine(parentCon) && !parentCon.previousSibling && domUtils.isZeroWith(endCon.textContent.slice(endOff))) {
2728
+ let sameTag = false;
2729
+ let e = endCon.nextSibling;
2730
+ while (e) {
2731
+ if (!domUtils.isZeroWith(e)) {
2732
+ sameTag = false;
2733
+ break;
2734
+ }
2735
+ e = e.nextSibling;
2736
+ }
2737
+
2738
+ if (sameTag) {
2739
+ domUtils.copyTagAttributes(parentCon, newInnerNode);
2740
+
2741
+ return {
2742
+ ancestor: element,
2743
+ container: endCon,
2744
+ offset: endOff
2745
+ };
2746
+ }
2747
+ }
2748
+
2749
+ // add tag
2750
+ _removeCheck.v = false;
2751
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
2752
+ const inst = this;
2753
+ const el = element;
2754
+ const nNodeArray = [newInnerNode];
2755
+ const pNode = element.cloneNode(false);
2756
+
2757
+ let container = endCon;
2758
+ let offset = endOff;
2759
+ let passNode = false;
2760
+ let pCurrent, newNode, appendNode, anchorNode;
2761
+
2762
+ (function recursionFunc(current, ancestor) {
2763
+ const childNodes = current.childNodes;
2764
+
2765
+ for (let i = childNodes.length - 1, vNode; 0 <= i; i--) {
2766
+ const child = childNodes[i];
2767
+ if (!child) continue;
2768
+ let coverNode = ancestor;
2769
+
2770
+ if (passNode && !domUtils.isBreak(child)) {
2771
+ if (child.nodeType === 1) {
2772
+ if (inst._isIgnoreNodeChange(child)) {
2773
+ newInnerNode = newInnerNode.cloneNode(false);
2774
+ const cloneChild = child.cloneNode(true);
2775
+ pNode.insertBefore(cloneChild, ancestor);
2776
+ pNode.insertBefore(newInnerNode, cloneChild);
2777
+ nNodeArray.push(newInnerNode);
2778
+ } else {
2779
+ recursionFunc(child, child);
2780
+ }
2781
+ continue;
2782
+ }
2783
+
2784
+ newNode = child;
2785
+ pCurrent = [];
2786
+ const anchors = [];
2787
+ while (newNode.parentNode !== null && newNode !== el && newNode !== newInnerNode) {
2788
+ vNode = validation(newNode);
2789
+ if (vNode && newNode.nodeType === 1) {
2790
+ if (_isMaintainedNode(newNode)) {
2791
+ if (!anchorNode) anchors.push(vNode);
2792
+ } else {
2793
+ pCurrent.push(vNode);
2794
+ }
2795
+ }
2796
+ newNode = newNode.parentNode;
2797
+ }
2798
+ pCurrent = pCurrent.concat(anchors);
2799
+
2800
+ const isTopNode = pCurrent.length > 0;
2801
+ const childNode = pCurrent.pop() || child;
2802
+ appendNode = newNode = childNode;
2803
+ while (pCurrent.length > 0) {
2804
+ newNode = pCurrent.pop();
2805
+ appendNode.appendChild(newNode);
2806
+ appendNode = newNode;
2807
+ }
2808
+
2809
+ if (_isMaintainedNode(newInnerNode.parentNode) && !_isMaintainedNode(childNode)) {
2810
+ newInnerNode = newInnerNode.cloneNode(false);
2811
+ pNode.insertBefore(newInnerNode, pNode.firstChild);
2812
+ nNodeArray.push(newInnerNode);
2813
+ }
2814
+
2815
+ if (!anchorNode && _isMaintainedNode(childNode)) {
2816
+ newInnerNode = newInnerNode.cloneNode(false);
2817
+ const aChildren = childNode.childNodes;
2818
+ for (let a = 0, aLen = aChildren.length; a < aLen; a++) {
2819
+ newInnerNode.appendChild(aChildren[a]);
2820
+ }
2821
+ childNode.appendChild(newInnerNode);
2822
+ pNode.insertBefore(childNode, pNode.firstChild);
2823
+ nNodeArray.push(newInnerNode);
2824
+ if (newInnerNode.children.length > 0) ancestor = newNode;
2825
+ else ancestor = newInnerNode;
2826
+ } else if (isTopNode) {
2827
+ newInnerNode.insertBefore(childNode, newInnerNode.firstChild);
2828
+ ancestor = newNode;
2829
+ } else {
2830
+ ancestor = newInnerNode;
2831
+ }
2832
+
2833
+ if (anchorNode && child.nodeType === 3) {
2834
+ if (_getMaintainedNode(child)) {
2835
+ const ancestorAnchorNode = domUtils.getParentElement(ancestor, (c) => {
2836
+ return inst._isNonSplitNode(c.parentNode) || c.parentNode === pNode;
2837
+ });
2838
+ anchorNode.appendChild(ancestorAnchorNode);
2839
+ newInnerNode = ancestorAnchorNode.cloneNode(false);
2840
+ nNodeArray.push(newInnerNode);
2841
+ pNode.insertBefore(newInnerNode, pNode.firstChild);
2842
+ } else {
2843
+ anchorNode = null;
2844
+ }
2845
+ }
2846
+ }
2847
+
2848
+ // endContainer
2849
+ if (!passNode && child === container) {
2850
+ anchorNode = _getMaintainedNode(child);
2851
+ const afterNode = domUtils.createTextNode(container.nodeType === 1 ? '' : container.substringData(offset, container.length - offset));
2852
+ const textNode = domUtils.createTextNode(container.nodeType === 1 ? '' : container.substringData(0, offset));
2853
+
2854
+ if (anchorNode) {
2855
+ anchorNode = anchorNode.cloneNode(false);
2856
+ const a = _getMaintainedNode(ancestor);
2857
+ if (a.parentNode !== pNode) {
2858
+ let m = a;
2859
+ let p = null;
2860
+ while (m.parentNode !== pNode) {
2861
+ ancestor = p = m.parentNode.cloneNode(false);
2862
+ while (m.childNodes[0]) {
2863
+ p.appendChild(m.childNodes[0]);
2864
+ }
2865
+ m.appendChild(p);
2866
+ m = m.parentNode;
2867
+ }
2868
+ m.parentNode.insertBefore(a, m.parentNode.firstChild);
2869
+ }
2870
+ anchorNode = anchorNode.cloneNode(false);
2871
+ } else if (_isMaintainedNode(newInnerNode.parentNode) && !anchorNode) {
2872
+ newInnerNode = newInnerNode.cloneNode(false);
2873
+ pNode.appendChild(newInnerNode);
2874
+ nNodeArray.push(newInnerNode);
2875
+ }
2876
+
2877
+ if (!domUtils.isZeroWith(afterNode)) {
2878
+ ancestor.insertBefore(afterNode, ancestor.firstChild);
2879
+ }
2880
+
2881
+ newNode = ancestor;
2882
+ pCurrent = [];
2883
+ while (newNode !== pNode && newNode !== null) {
2884
+ vNode = _isMaintainedNode(newNode) ? null : validation(newNode);
2885
+ if (vNode && newNode.nodeType === 1) {
2886
+ pCurrent.push(vNode);
2887
+ }
2888
+ newNode = newNode.parentNode;
2889
+ }
2890
+
2891
+ const childNode = pCurrent.pop() || ancestor;
2892
+ appendNode = newNode = childNode;
2893
+ while (pCurrent.length > 0) {
2894
+ newNode = pCurrent.pop();
2895
+ appendNode.appendChild(newNode);
2896
+ appendNode = newNode;
2897
+ }
2898
+
2899
+ if (childNode !== ancestor) {
2900
+ newInnerNode.insertBefore(childNode, newInnerNode.firstChild);
2901
+ ancestor = newNode;
2902
+ } else {
2903
+ ancestor = newInnerNode;
2904
+ }
2905
+
2906
+ if (domUtils.isBreak(child)) newInnerNode.appendChild(child.cloneNode(false));
2907
+
2908
+ if (anchorNode) {
2909
+ anchorNode.insertBefore(newInnerNode, anchorNode.firstChild);
2910
+ pNode.insertBefore(anchorNode, pNode.firstChild);
2911
+ anchorNode = null;
2912
+ } else {
2913
+ pNode.insertBefore(newInnerNode, pNode.firstChild);
2914
+ }
2915
+
2916
+ container = textNode;
2917
+ offset = textNode.data.length;
2918
+ passNode = true;
2919
+
2920
+ ancestor.insertBefore(container, ancestor.firstChild);
2921
+ continue;
2922
+ }
2923
+
2924
+ vNode = !passNode ? child.cloneNode(false) : validation(child);
2925
+ if (vNode) {
2926
+ ancestor.insertBefore(vNode, ancestor.firstChild);
2927
+ if (child.nodeType === 1 && !domUtils.isBreak(child)) coverNode = vNode;
2928
+ }
2929
+
2930
+ recursionFunc(child, coverNode);
2931
+ }
2932
+ })(element, pNode);
2933
+
2934
+ // not remove tag
2935
+ if (isRemoveNode && !isRemoveFormat && !_removeCheck.v) {
2936
+ return {
2937
+ ancestor: element,
2938
+ container: endCon,
2939
+ offset: endOff
2940
+ };
2941
+ }
2942
+
2943
+ isRemoveFormat = isRemoveFormat && isRemoveNode;
2944
+
2945
+ if (isRemoveFormat) {
2946
+ for (let i = 0; i < nNodeArray.length; i++) {
2947
+ const removeNode = nNodeArray[i];
2948
+
2949
+ const rChildren = removeNode.childNodes;
2950
+ let textNode = null;
2951
+ while (rChildren[0]) {
2952
+ textNode = rChildren[0];
2953
+ pNode.insertBefore(textNode, removeNode);
2954
+ }
2955
+ domUtils.removeItem(removeNode);
2956
+
2957
+ if (i === nNodeArray.length - 1) {
2958
+ container = textNode;
2959
+ offset = textNode.textContent.length;
2960
+ }
2961
+ }
2962
+ } else if (isRemoveNode) {
2963
+ newInnerNode = newInnerNode.firstChild;
2964
+ for (let i = 0; i < nNodeArray.length; i++) {
2965
+ SN_StripRemoveNode(nNodeArray[i]);
2966
+ }
2967
+ }
2968
+
2969
+ if (!isRemoveFormat && pNode.childNodes.length === 0) {
2970
+ if (element.childNodes) {
2971
+ container = element.childNodes[0];
2972
+ } else {
2973
+ container = domUtils.createTextNode(unicode.zeroWidthSpace);
2974
+ element.appendChild(container);
2975
+ }
2976
+ } else {
2977
+ if (!isRemoveNode && newInnerNode.textContent.length === 0) {
2978
+ this.nodeTransform.removeEmptyNode(pNode, null, false);
2979
+ return {
2980
+ ancestor: null,
2981
+ container: null,
2982
+ offset: 0
2983
+ };
2984
+ }
2985
+
2986
+ this.nodeTransform.removeEmptyNode(pNode, newInnerNode, false);
2987
+
2988
+ if (domUtils.isZeroWith(pNode.textContent)) {
2989
+ container = pNode.firstChild;
2990
+ offset = container.textContent.length;
2991
+ } else if (domUtils.isZeroWith(container)) {
2992
+ container = newInnerNode;
2993
+ offset = 1;
2994
+ }
2995
+
2996
+ // node change
2997
+ const offsets = {
2998
+ s: 0,
2999
+ e: 0
3000
+ };
3001
+ const path = domUtils.getNodePath(container, pNode, offsets);
3002
+ offset += offsets.s;
3003
+
3004
+ // tag merge
3005
+ const newOffsets = this.nodeTransform.mergeSameTags(pNode, [path], true);
3006
+
3007
+ element.parentNode.replaceChild(pNode, element);
3008
+
3009
+ container = domUtils.getNodeFromPath(path, pNode);
3010
+ offset += newOffsets[0];
3011
+ }
3012
+
3013
+ return {
3014
+ ancestor: pNode,
3015
+ container: container,
3016
+ offset: container.nodeType === 1 && offset === 1 ? container.childNodes.length : offset
3017
+ };
3018
+ },
3019
+
3020
+ /**
3021
+ * @description Node with font-size style
3022
+ * @param {Node} element Element to check
3023
+ * @returns {boolean}
3024
+ * @private
3025
+ */
3026
+ _sn_isSizeNode(element) {
3027
+ return element && typeof element !== 'string' && element.nodeType !== 3 && this.isTextStyleNode(element) && element.style.fontSize;
3028
+ },
3029
+
3030
+ /**
3031
+ * @description Return the parent maintained tag. (bind and use a util object)
3032
+ * @param {Element} element Element
3033
+ * @returns {Element}
3034
+ * @private
3035
+ */
3036
+ _sn_getMaintainedNode(_isRemove, _isSizeNode, element) {
3037
+ if (!element || _isRemove) return null;
3038
+ return domUtils.getParentElement(element, this._isNonSplitNode) || (!_isSizeNode ? domUtils.getParentElement(element, this._sn_isSizeNode.bind(this)) : null);
3039
+ },
3040
+
3041
+ /**
3042
+ * @description Check if element is a tag that should be persisted. (bind and use a util object)
3043
+ * @param {Element} element Element
3044
+ * @returns {Element}
3045
+ * @private
3046
+ */
3047
+ _sn_isMaintainedNode(_isRemove, _isSizeNode, element) {
3048
+ if (!element || _isRemove || element.nodeType !== 1) return false;
3049
+ const anchor = this._isNonSplitNode(element);
3050
+ return domUtils.getParentElement(element, this._isNonSplitNode) ? anchor : anchor || (!_isSizeNode ? this._sn_isSizeNode(element) : false);
3051
+ },
3052
+
3053
+ /**
3054
+ * @description If certain styles are applied to all child nodes of the list cell, the style of the list cell is also changed. (bold, color, size)
3055
+ * @param {Element} el List cell element. <li>
3056
+ * @param {Element|null} child Variable for recursive call. ("null" on the first call)
3057
+ * @private
3058
+ */
3059
+ _sn_setCommonListStyle(el, child) {
3060
+ if (!domUtils.isListCell(el)) return;
3061
+
3062
+ const children = domUtils.getArrayItem(
3063
+ (child || el).childNodes,
3064
+ function (current) {
3065
+ return !domUtils.isBreak(current);
3066
+ },
3067
+ true
3068
+ );
3069
+ child = children[0];
3070
+
3071
+ if (!child || children.length > 1 || child.nodeType !== 1) return;
3072
+
3073
+ // set cell style---
3074
+ const childStyle = child.style;
3075
+ const elStyle = el.style;
3076
+ const nodeName = child.nodeName.toLowerCase();
3077
+ let appliedEl = false;
3078
+
3079
+ // bold, italic
3080
+ if (this.options.get('_defaultStyleTagMap')[nodeName] === this.options.get('_defaultTagCommand').bold.toLowerCase()) elStyle.fontWeight = 'bold';
3081
+ if (this.options.get('_defaultStyleTagMap')[nodeName] === this.options.get('_defaultTagCommand').italic.toLowerCase()) elStyle.fontStyle = 'italic';
3082
+
3083
+ // styles
3084
+ const cKeys = env.getValues(childStyle);
3085
+ if (cKeys.length > 0) {
3086
+ for (let i = 0, len = this._listCamel.length; i < len; i++) {
3087
+ if (cKeys.includes(this._listKebab[i])) {
3088
+ elStyle[this._listCamel[i]] = childStyle[this._listCamel[i]];
3089
+ childStyle.removeProperty(this._listKebab[i]);
3090
+ appliedEl = true;
3091
+ }
3092
+ }
3093
+ }
3094
+
3095
+ this._sn_setCommonListStyle(el, child);
3096
+ if (!appliedEl) return;
3097
+
3098
+ // common style
3099
+ if (!childStyle.length) {
3100
+ const ch = child.childNodes;
3101
+ const p = child.parentNode;
3102
+ const n = child.nextSibling;
3103
+ while (ch.length > 0) {
3104
+ p.insertBefore(ch[0], n);
3105
+ }
3106
+ domUtils.removeItem(child);
3107
+ }
3108
+ },
3109
+
3110
+ /**
3111
+ * @description Watch the applied text nodes and adjust the common styles of the list.
3112
+ * @param {Element} el "LI" element
3113
+ * @param {Array|null} styleArray Refer style array
3114
+ * @private
3115
+ */
3116
+ _sn_resetCommonListCell(el, styleArray) {
3117
+ if (!domUtils.isListCell(el)) return;
3118
+ if (!styleArray) styleArray = this._listKebab;
3119
+
3120
+ const children = domUtils.getArrayItem(
3121
+ el.childNodes,
3122
+ function (current) {
3123
+ return !domUtils.isBreak(current);
3124
+ },
3125
+ true
3126
+ );
3127
+ const elStyles = el.style;
3128
+
3129
+ const ec = [],
3130
+ ek = [],
3131
+ elKeys = env.getValues(elStyles);
3132
+ for (let i = 0, len = this._listKebab.length; i < len; i++) {
3133
+ if (elKeys.includes(this._listKebab[i]) && styleArray.includes(this._listKebab[i])) {
3134
+ ec.push(this._listCamel[i]);
3135
+ ek.push(this._listKebab[i]);
3136
+ }
3137
+ }
3138
+
3139
+ if (!ec.length) return;
3140
+
3141
+ // reset cell style---
3142
+ const refer = domUtils.createElement('SPAN');
3143
+ for (let i = 0, len = ec.length; i < len; i++) {
3144
+ refer.style[ec[i]] = elStyles[ek[i]];
3145
+ elStyles.removeProperty(ek[i]);
3146
+ }
3147
+
3148
+ let sel = refer.cloneNode(false);
3149
+ let r = null,
3150
+ appliedEl = false;
3151
+ for (let i = 0, len = children.length, c, s; i < len; i++) {
3152
+ c = children[i];
3153
+ if (this.options.get('_defaultStyleTagMap')[c.nodeName.toLowerCase()]) continue;
3154
+
3155
+ s = env.getValues(c.style);
3156
+ if (
3157
+ s.length === 0 ||
3158
+ (ec.some(function (k) {
3159
+ return !s.includes(k);
3160
+ }) &&
3161
+ s.some(function (k) {
3162
+ ec.includes(k);
3163
+ }))
3164
+ ) {
3165
+ r = c.nextSibling;
3166
+ sel.appendChild(c);
3167
+ } else if (sel.childNodes.length > 0) {
3168
+ el.insertBefore(sel, r);
3169
+ sel = refer.cloneNode(false);
3170
+ r = null;
3171
+ appliedEl = true;
3172
+ }
3173
+ }
3174
+
3175
+ if (sel.childNodes.length > 0) {
3176
+ el.insertBefore(sel, r);
3177
+ appliedEl = true;
3178
+ }
3179
+ if (!elStyles.length) {
3180
+ el.removeAttribute('style');
3181
+ }
3182
+
3183
+ return appliedEl;
3184
+ },
3185
+
3186
+ constructor: Format
3187
+ };
3188
+
3189
+ function DeleteNestedList(baseNode) {
3190
+ const baseParent = baseNode.parentNode;
3191
+ let sibling = baseParent;
3192
+ let parent = sibling.parentNode;
3193
+ let liSibling, liParent, child, index, c;
3194
+
3195
+ while (domUtils.isListCell(parent)) {
3196
+ index = domUtils.getPositionIndex(baseNode);
3197
+ liSibling = parent.nextElementSibling;
3198
+ liParent = parent.parentNode;
3199
+ child = sibling;
3200
+ while (child) {
3201
+ sibling = sibling.nextSibling;
3202
+ if (domUtils.isList(child)) {
3203
+ c = child.childNodes;
3204
+ while (c[index]) {
3205
+ liParent.insertBefore(c[index], liSibling);
3206
+ }
3207
+ if (c.length === 0) domUtils.removeItem(child);
3208
+ } else {
3209
+ liParent.appendChild(child);
3210
+ }
3211
+ child = sibling;
3212
+ }
3213
+ sibling = liParent;
3214
+ parent = liParent.parentNode;
3215
+ }
3216
+
3217
+ if (baseParent.children.length === 0) domUtils.removeItem(baseParent);
3218
+
3219
+ return liParent;
3220
+ }
3221
+
3222
+ function SetLineMargin(lines, size, dir) {
3223
+ const cells = [];
3224
+
3225
+ for (let i = 0, len = lines.length, f, margin; i < len; i++) {
3226
+ f = lines[i];
3227
+ if (!domUtils.isListCell(f)) {
3228
+ margin = /\d+/.test(f.style[dir]) ? numbers.get(f.style[dir], 0) : 0;
3229
+ margin += size;
3230
+ domUtils.setStyle(f, dir, margin <= 0 ? '' : margin + 'px');
3231
+ } else {
3232
+ if (size < 0 || f.previousElementSibling) {
3233
+ cells.push(f);
3234
+ }
3235
+ }
3236
+ }
3237
+
3238
+ return cells;
3239
+ }
3240
+
3241
+ /**
3242
+ * @description Strip remove node
3243
+ * @param {Node} removeNode The remove node
3244
+ * @private
3245
+ */
3246
+ function SN_StripRemoveNode(removeNode) {
3247
+ const element = removeNode.parentNode;
3248
+ if (!removeNode || removeNode.nodeType === 3 || !element) return;
3249
+
3250
+ const children = removeNode.childNodes;
3251
+ while (children[0]) {
3252
+ element.insertBefore(children[0], removeNode);
3253
+ }
3254
+
3255
+ element.removeChild(removeNode);
3256
+ }
3257
+
3258
+ export default Format;