@ckeditor/ckeditor5-html-support 0.0.0-internal-20241017.0

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 (312) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +26 -0
  4. package/build/html-support.js +5 -0
  5. package/build/translations/ar.js +1 -0
  6. package/build/translations/bg.js +1 -0
  7. package/build/translations/bn.js +1 -0
  8. package/build/translations/ca.js +1 -0
  9. package/build/translations/cs.js +1 -0
  10. package/build/translations/da.js +1 -0
  11. package/build/translations/de.js +1 -0
  12. package/build/translations/el.js +1 -0
  13. package/build/translations/en-au.js +1 -0
  14. package/build/translations/es.js +1 -0
  15. package/build/translations/et.js +1 -0
  16. package/build/translations/fi.js +1 -0
  17. package/build/translations/fr.js +1 -0
  18. package/build/translations/gl.js +1 -0
  19. package/build/translations/he.js +1 -0
  20. package/build/translations/hi.js +1 -0
  21. package/build/translations/hr.js +1 -0
  22. package/build/translations/hu.js +1 -0
  23. package/build/translations/id.js +1 -0
  24. package/build/translations/it.js +1 -0
  25. package/build/translations/ja.js +1 -0
  26. package/build/translations/jv.js +1 -0
  27. package/build/translations/ko.js +1 -0
  28. package/build/translations/lt.js +1 -0
  29. package/build/translations/lv.js +1 -0
  30. package/build/translations/ms.js +1 -0
  31. package/build/translations/nl.js +1 -0
  32. package/build/translations/no.js +1 -0
  33. package/build/translations/pl.js +1 -0
  34. package/build/translations/pt-br.js +1 -0
  35. package/build/translations/pt.js +1 -0
  36. package/build/translations/ro.js +1 -0
  37. package/build/translations/ru.js +1 -0
  38. package/build/translations/sk.js +1 -0
  39. package/build/translations/sr-latn.js +1 -0
  40. package/build/translations/sr.js +1 -0
  41. package/build/translations/sv.js +1 -0
  42. package/build/translations/th.js +1 -0
  43. package/build/translations/tr.js +1 -0
  44. package/build/translations/ug.js +1 -0
  45. package/build/translations/uk.js +1 -0
  46. package/build/translations/ur.js +1 -0
  47. package/build/translations/vi.js +1 -0
  48. package/build/translations/zh-cn.js +1 -0
  49. package/build/translations/zh.js +1 -0
  50. package/ckeditor5-metadata.json +60 -0
  51. package/dist/augmentation.d.ts +37 -0
  52. package/dist/converters.d.ts +64 -0
  53. package/dist/datafilter.d.ts +312 -0
  54. package/dist/dataschema.d.ts +191 -0
  55. package/dist/fullpage.d.ts +29 -0
  56. package/dist/generalhtmlsupport.d.ts +106 -0
  57. package/dist/generalhtmlsupportconfig.d.ts +81 -0
  58. package/dist/htmlcomment.d.ts +79 -0
  59. package/dist/htmlpagedataprocessor.d.ts +26 -0
  60. package/dist/index-content.css +4 -0
  61. package/dist/index-editor.css +51 -0
  62. package/dist/index.css +63 -0
  63. package/dist/index.css.map +1 -0
  64. package/dist/index.d.ts +29 -0
  65. package/dist/index.js +4040 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/integrations/codeblock.d.ts +31 -0
  68. package/dist/integrations/customelement.d.ts +35 -0
  69. package/dist/integrations/dualcontent.d.ts +53 -0
  70. package/dist/integrations/heading.d.ts +39 -0
  71. package/dist/integrations/image.d.ts +34 -0
  72. package/dist/integrations/integrationutils.d.ts +19 -0
  73. package/dist/integrations/list.d.ts +35 -0
  74. package/dist/integrations/mediaembed.d.ts +34 -0
  75. package/dist/integrations/script.d.ts +34 -0
  76. package/dist/integrations/style.d.ts +34 -0
  77. package/dist/integrations/table.d.ts +31 -0
  78. package/dist/schemadefinitions.d.ts +17 -0
  79. package/dist/translations/ar.d.ts +8 -0
  80. package/dist/translations/ar.js +5 -0
  81. package/dist/translations/ar.umd.js +11 -0
  82. package/dist/translations/bg.d.ts +8 -0
  83. package/dist/translations/bg.js +5 -0
  84. package/dist/translations/bg.umd.js +11 -0
  85. package/dist/translations/bn.d.ts +8 -0
  86. package/dist/translations/bn.js +5 -0
  87. package/dist/translations/bn.umd.js +11 -0
  88. package/dist/translations/ca.d.ts +8 -0
  89. package/dist/translations/ca.js +5 -0
  90. package/dist/translations/ca.umd.js +11 -0
  91. package/dist/translations/cs.d.ts +8 -0
  92. package/dist/translations/cs.js +5 -0
  93. package/dist/translations/cs.umd.js +11 -0
  94. package/dist/translations/da.d.ts +8 -0
  95. package/dist/translations/da.js +5 -0
  96. package/dist/translations/da.umd.js +11 -0
  97. package/dist/translations/de.d.ts +8 -0
  98. package/dist/translations/de.js +5 -0
  99. package/dist/translations/de.umd.js +11 -0
  100. package/dist/translations/el.d.ts +8 -0
  101. package/dist/translations/el.js +5 -0
  102. package/dist/translations/el.umd.js +11 -0
  103. package/dist/translations/en-au.d.ts +8 -0
  104. package/dist/translations/en-au.js +5 -0
  105. package/dist/translations/en-au.umd.js +11 -0
  106. package/dist/translations/en.d.ts +8 -0
  107. package/dist/translations/en.js +5 -0
  108. package/dist/translations/en.umd.js +11 -0
  109. package/dist/translations/es.d.ts +8 -0
  110. package/dist/translations/es.js +5 -0
  111. package/dist/translations/es.umd.js +11 -0
  112. package/dist/translations/et.d.ts +8 -0
  113. package/dist/translations/et.js +5 -0
  114. package/dist/translations/et.umd.js +11 -0
  115. package/dist/translations/fi.d.ts +8 -0
  116. package/dist/translations/fi.js +5 -0
  117. package/dist/translations/fi.umd.js +11 -0
  118. package/dist/translations/fr.d.ts +8 -0
  119. package/dist/translations/fr.js +5 -0
  120. package/dist/translations/fr.umd.js +11 -0
  121. package/dist/translations/gl.d.ts +8 -0
  122. package/dist/translations/gl.js +5 -0
  123. package/dist/translations/gl.umd.js +11 -0
  124. package/dist/translations/he.d.ts +8 -0
  125. package/dist/translations/he.js +5 -0
  126. package/dist/translations/he.umd.js +11 -0
  127. package/dist/translations/hi.d.ts +8 -0
  128. package/dist/translations/hi.js +5 -0
  129. package/dist/translations/hi.umd.js +11 -0
  130. package/dist/translations/hr.d.ts +8 -0
  131. package/dist/translations/hr.js +5 -0
  132. package/dist/translations/hr.umd.js +11 -0
  133. package/dist/translations/hu.d.ts +8 -0
  134. package/dist/translations/hu.js +5 -0
  135. package/dist/translations/hu.umd.js +11 -0
  136. package/dist/translations/id.d.ts +8 -0
  137. package/dist/translations/id.js +5 -0
  138. package/dist/translations/id.umd.js +11 -0
  139. package/dist/translations/it.d.ts +8 -0
  140. package/dist/translations/it.js +5 -0
  141. package/dist/translations/it.umd.js +11 -0
  142. package/dist/translations/ja.d.ts +8 -0
  143. package/dist/translations/ja.js +5 -0
  144. package/dist/translations/ja.umd.js +11 -0
  145. package/dist/translations/jv.d.ts +8 -0
  146. package/dist/translations/jv.js +5 -0
  147. package/dist/translations/jv.umd.js +11 -0
  148. package/dist/translations/ko.d.ts +8 -0
  149. package/dist/translations/ko.js +5 -0
  150. package/dist/translations/ko.umd.js +11 -0
  151. package/dist/translations/lt.d.ts +8 -0
  152. package/dist/translations/lt.js +5 -0
  153. package/dist/translations/lt.umd.js +11 -0
  154. package/dist/translations/lv.d.ts +8 -0
  155. package/dist/translations/lv.js +5 -0
  156. package/dist/translations/lv.umd.js +11 -0
  157. package/dist/translations/ms.d.ts +8 -0
  158. package/dist/translations/ms.js +5 -0
  159. package/dist/translations/ms.umd.js +11 -0
  160. package/dist/translations/nl.d.ts +8 -0
  161. package/dist/translations/nl.js +5 -0
  162. package/dist/translations/nl.umd.js +11 -0
  163. package/dist/translations/no.d.ts +8 -0
  164. package/dist/translations/no.js +5 -0
  165. package/dist/translations/no.umd.js +11 -0
  166. package/dist/translations/pl.d.ts +8 -0
  167. package/dist/translations/pl.js +5 -0
  168. package/dist/translations/pl.umd.js +11 -0
  169. package/dist/translations/pt-br.d.ts +8 -0
  170. package/dist/translations/pt-br.js +5 -0
  171. package/dist/translations/pt-br.umd.js +11 -0
  172. package/dist/translations/pt.d.ts +8 -0
  173. package/dist/translations/pt.js +5 -0
  174. package/dist/translations/pt.umd.js +11 -0
  175. package/dist/translations/ro.d.ts +8 -0
  176. package/dist/translations/ro.js +5 -0
  177. package/dist/translations/ro.umd.js +11 -0
  178. package/dist/translations/ru.d.ts +8 -0
  179. package/dist/translations/ru.js +5 -0
  180. package/dist/translations/ru.umd.js +11 -0
  181. package/dist/translations/sk.d.ts +8 -0
  182. package/dist/translations/sk.js +5 -0
  183. package/dist/translations/sk.umd.js +11 -0
  184. package/dist/translations/sr-latn.d.ts +8 -0
  185. package/dist/translations/sr-latn.js +5 -0
  186. package/dist/translations/sr-latn.umd.js +11 -0
  187. package/dist/translations/sr.d.ts +8 -0
  188. package/dist/translations/sr.js +5 -0
  189. package/dist/translations/sr.umd.js +11 -0
  190. package/dist/translations/sv.d.ts +8 -0
  191. package/dist/translations/sv.js +5 -0
  192. package/dist/translations/sv.umd.js +11 -0
  193. package/dist/translations/th.d.ts +8 -0
  194. package/dist/translations/th.js +5 -0
  195. package/dist/translations/th.umd.js +11 -0
  196. package/dist/translations/tr.d.ts +8 -0
  197. package/dist/translations/tr.js +5 -0
  198. package/dist/translations/tr.umd.js +11 -0
  199. package/dist/translations/ug.d.ts +8 -0
  200. package/dist/translations/ug.js +5 -0
  201. package/dist/translations/ug.umd.js +11 -0
  202. package/dist/translations/uk.d.ts +8 -0
  203. package/dist/translations/uk.js +5 -0
  204. package/dist/translations/uk.umd.js +11 -0
  205. package/dist/translations/ur.d.ts +8 -0
  206. package/dist/translations/ur.js +5 -0
  207. package/dist/translations/ur.umd.js +11 -0
  208. package/dist/translations/vi.d.ts +8 -0
  209. package/dist/translations/vi.js +5 -0
  210. package/dist/translations/vi.umd.js +11 -0
  211. package/dist/translations/zh-cn.d.ts +8 -0
  212. package/dist/translations/zh-cn.js +5 -0
  213. package/dist/translations/zh-cn.umd.js +11 -0
  214. package/dist/translations/zh.d.ts +8 -0
  215. package/dist/translations/zh.js +5 -0
  216. package/dist/translations/zh.umd.js +11 -0
  217. package/dist/utils.d.ts +76 -0
  218. package/lang/contexts.json +3 -0
  219. package/lang/translations/ar.po +22 -0
  220. package/lang/translations/bg.po +22 -0
  221. package/lang/translations/bn.po +22 -0
  222. package/lang/translations/ca.po +22 -0
  223. package/lang/translations/cs.po +22 -0
  224. package/lang/translations/da.po +22 -0
  225. package/lang/translations/de.po +22 -0
  226. package/lang/translations/el.po +22 -0
  227. package/lang/translations/en-au.po +22 -0
  228. package/lang/translations/en.po +22 -0
  229. package/lang/translations/es.po +22 -0
  230. package/lang/translations/et.po +22 -0
  231. package/lang/translations/fi.po +22 -0
  232. package/lang/translations/fr.po +22 -0
  233. package/lang/translations/gl.po +22 -0
  234. package/lang/translations/he.po +22 -0
  235. package/lang/translations/hi.po +22 -0
  236. package/lang/translations/hr.po +22 -0
  237. package/lang/translations/hu.po +22 -0
  238. package/lang/translations/id.po +22 -0
  239. package/lang/translations/it.po +22 -0
  240. package/lang/translations/ja.po +22 -0
  241. package/lang/translations/jv.po +22 -0
  242. package/lang/translations/ko.po +22 -0
  243. package/lang/translations/lt.po +22 -0
  244. package/lang/translations/lv.po +22 -0
  245. package/lang/translations/ms.po +22 -0
  246. package/lang/translations/nl.po +22 -0
  247. package/lang/translations/no.po +22 -0
  248. package/lang/translations/pl.po +22 -0
  249. package/lang/translations/pt-br.po +22 -0
  250. package/lang/translations/pt.po +22 -0
  251. package/lang/translations/ro.po +22 -0
  252. package/lang/translations/ru.po +22 -0
  253. package/lang/translations/sk.po +22 -0
  254. package/lang/translations/sr-latn.po +22 -0
  255. package/lang/translations/sr.po +22 -0
  256. package/lang/translations/sv.po +22 -0
  257. package/lang/translations/th.po +22 -0
  258. package/lang/translations/tr.po +22 -0
  259. package/lang/translations/ug.po +22 -0
  260. package/lang/translations/uk.po +22 -0
  261. package/lang/translations/ur.po +22 -0
  262. package/lang/translations/vi.po +22 -0
  263. package/lang/translations/zh-cn.po +22 -0
  264. package/lang/translations/zh.po +22 -0
  265. package/package.json +48 -0
  266. package/src/augmentation.d.ts +33 -0
  267. package/src/augmentation.js +5 -0
  268. package/src/converters.d.ts +60 -0
  269. package/src/converters.js +180 -0
  270. package/src/datafilter.d.ts +308 -0
  271. package/src/datafilter.js +755 -0
  272. package/src/dataschema.d.ts +187 -0
  273. package/src/dataschema.js +202 -0
  274. package/src/fullpage.d.ts +25 -0
  275. package/src/fullpage.js +86 -0
  276. package/src/generalhtmlsupport.d.ts +102 -0
  277. package/src/generalhtmlsupport.js +246 -0
  278. package/src/generalhtmlsupportconfig.d.ts +77 -0
  279. package/src/generalhtmlsupportconfig.js +5 -0
  280. package/src/htmlcomment.d.ts +75 -0
  281. package/src/htmlcomment.js +224 -0
  282. package/src/htmlpagedataprocessor.d.ts +22 -0
  283. package/src/htmlpagedataprocessor.js +67 -0
  284. package/src/index.d.ts +25 -0
  285. package/src/index.js +14 -0
  286. package/src/integrations/codeblock.d.ts +27 -0
  287. package/src/integrations/codeblock.js +107 -0
  288. package/src/integrations/customelement.d.ts +31 -0
  289. package/src/integrations/customelement.js +162 -0
  290. package/src/integrations/dualcontent.d.ts +49 -0
  291. package/src/integrations/dualcontent.js +125 -0
  292. package/src/integrations/heading.d.ts +35 -0
  293. package/src/integrations/heading.js +72 -0
  294. package/src/integrations/image.d.ts +30 -0
  295. package/src/integrations/image.js +195 -0
  296. package/src/integrations/integrationutils.d.ts +15 -0
  297. package/src/integrations/integrationutils.js +21 -0
  298. package/src/integrations/list.d.ts +31 -0
  299. package/src/integrations/list.js +185 -0
  300. package/src/integrations/mediaembed.d.ts +30 -0
  301. package/src/integrations/mediaembed.js +125 -0
  302. package/src/integrations/script.d.ts +30 -0
  303. package/src/integrations/script.js +65 -0
  304. package/src/integrations/style.d.ts +30 -0
  305. package/src/integrations/style.js +65 -0
  306. package/src/integrations/table.d.ts +27 -0
  307. package/src/integrations/table.js +169 -0
  308. package/src/schemadefinitions.d.ts +13 -0
  309. package/src/schemadefinitions.js +964 -0
  310. package/src/utils.d.ts +72 -0
  311. package/src/utils.js +139 -0
  312. package/theme/datafilter.css +56 -0
package/dist/index.js ADDED
@@ -0,0 +1,4040 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { Plugin } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { toArray, priorities, CKEditorError, isValidAttributeName, uid } from '@ckeditor/ckeditor5-utils/dist/index.js';
7
+ import { Matcher, StylesMap, UpcastWriter, HtmlDataProcessor } from '@ckeditor/ckeditor5-engine/dist/index.js';
8
+ import { toWidget, Widget } from '@ckeditor/ckeditor5-widget/dist/index.js';
9
+ import { cloneDeep, startCase, mergeWith, isPlainObject, isEqual } from 'lodash-es';
10
+ import { Enter } from '@ckeditor/ckeditor5-enter/dist/index.js';
11
+
12
+ /**
13
+ * Helper function for the downcast converter. Updates attributes on the given view element.
14
+ *
15
+ * @param writer The view writer.
16
+ * @param oldViewAttributes The previous GHS attribute value.
17
+ * @param newViewAttributes The current GHS attribute value.
18
+ * @param viewElement The view element to update.
19
+ */ function updateViewAttributes(writer, oldViewAttributes, newViewAttributes, viewElement) {
20
+ if (oldViewAttributes) {
21
+ removeViewAttributes(writer, oldViewAttributes, viewElement);
22
+ }
23
+ if (newViewAttributes) {
24
+ setViewAttributes(writer, newViewAttributes, viewElement);
25
+ }
26
+ }
27
+ /**
28
+ * Helper function for the downcast converter. Sets attributes on the given view element.
29
+ *
30
+ * @param writer The view writer.
31
+ * @param viewAttributes The GHS attribute value.
32
+ * @param viewElement The view element to update.
33
+ */ function setViewAttributes(writer, viewAttributes, viewElement) {
34
+ if (viewAttributes.attributes) {
35
+ for (const [key, value] of Object.entries(viewAttributes.attributes)){
36
+ writer.setAttribute(key, value, viewElement);
37
+ }
38
+ }
39
+ if (viewAttributes.styles) {
40
+ writer.setStyle(viewAttributes.styles, viewElement);
41
+ }
42
+ if (viewAttributes.classes) {
43
+ writer.addClass(viewAttributes.classes, viewElement);
44
+ }
45
+ }
46
+ /**
47
+ * Helper function for the downcast converter. Removes attributes on the given view element.
48
+ *
49
+ * @param writer The view writer.
50
+ * @param viewAttributes The GHS attribute value.
51
+ * @param viewElement The view element to update.
52
+ */ function removeViewAttributes(writer, viewAttributes, viewElement) {
53
+ if (viewAttributes.attributes) {
54
+ for (const [key] of Object.entries(viewAttributes.attributes)){
55
+ writer.removeAttribute(key, viewElement);
56
+ }
57
+ }
58
+ if (viewAttributes.styles) {
59
+ for (const style of Object.keys(viewAttributes.styles)){
60
+ writer.removeStyle(style, viewElement);
61
+ }
62
+ }
63
+ if (viewAttributes.classes) {
64
+ writer.removeClass(viewAttributes.classes, viewElement);
65
+ }
66
+ }
67
+ /**
68
+ * Merges view element attribute objects.
69
+ */ function mergeViewElementAttributes(target, source) {
70
+ const result = cloneDeep(target);
71
+ let key = 'attributes';
72
+ for(key in source){
73
+ // Merge classes.
74
+ if (key == 'classes') {
75
+ result[key] = Array.from(new Set([
76
+ ...target[key] || [],
77
+ ...source[key]
78
+ ]));
79
+ } else {
80
+ result[key] = {
81
+ ...target[key],
82
+ ...source[key]
83
+ };
84
+ }
85
+ }
86
+ return result;
87
+ }
88
+ function modifyGhsAttribute(writer, item, ghsAttributeName, subject, callback) {
89
+ const oldValue = item.getAttribute(ghsAttributeName);
90
+ const newValue = {};
91
+ for (const kind of [
92
+ 'attributes',
93
+ 'styles',
94
+ 'classes'
95
+ ]){
96
+ // Properties other than `subject` should be assigned from `oldValue`.
97
+ if (kind != subject) {
98
+ if (oldValue && oldValue[kind]) {
99
+ newValue[kind] = oldValue[kind];
100
+ }
101
+ continue;
102
+ }
103
+ // `callback` should be applied on property [`subject`].
104
+ if (subject == 'classes') {
105
+ const values = new Set(oldValue && oldValue.classes || []);
106
+ callback(values);
107
+ if (values.size) {
108
+ newValue[kind] = Array.from(values);
109
+ }
110
+ continue;
111
+ }
112
+ const values = new Map(Object.entries(oldValue && oldValue[kind] || {}));
113
+ callback(values);
114
+ if (values.size) {
115
+ newValue[kind] = Object.fromEntries(values);
116
+ }
117
+ }
118
+ if (Object.keys(newValue).length) {
119
+ if (item.is('documentSelection')) {
120
+ writer.setSelectionAttribute(ghsAttributeName, newValue);
121
+ } else {
122
+ writer.setAttribute(ghsAttributeName, newValue, item);
123
+ }
124
+ } else if (oldValue) {
125
+ if (item.is('documentSelection')) {
126
+ writer.removeSelectionAttribute(ghsAttributeName);
127
+ } else {
128
+ writer.removeAttribute(ghsAttributeName, item);
129
+ }
130
+ }
131
+ }
132
+ /**
133
+ * Transforms passed string to PascalCase format. Examples:
134
+ * * `div` => `Div`
135
+ * * `h1` => `H1`
136
+ * * `table` => `Table`
137
+ */ function toPascalCase(data) {
138
+ return startCase(data).replace(/ /g, '');
139
+ }
140
+ /**
141
+ * Returns the attribute name of the model element that holds raw HTML attributes.
142
+ */ function getHtmlAttributeName(viewElementName) {
143
+ return `html${toPascalCase(viewElementName)}Attributes`;
144
+ }
145
+
146
+ /**
147
+ * View-to-model conversion helper for object elements.
148
+ *
149
+ * Preserves object element content in `htmlContent` attribute.
150
+ *
151
+ * @returns Returns a conversion callback.
152
+ */ function viewToModelObjectConverter({ model: modelName }) {
153
+ return (viewElement, conversionApi)=>{
154
+ // Let's keep element HTML and its attributes, so we can rebuild element in downcast conversions.
155
+ return conversionApi.writer.createElement(modelName, {
156
+ htmlContent: viewElement.getCustomProperty('$rawContent')
157
+ });
158
+ };
159
+ }
160
+ /**
161
+ * Conversion helper converting an object element to an HTML object widget.
162
+ *
163
+ * @returns Returns a conversion callback.
164
+ */ function toObjectWidgetConverter(editor, { view: viewName, isInline }) {
165
+ const t = editor.t;
166
+ return (modelElement, { writer })=>{
167
+ const widgetLabel = t('HTML object');
168
+ const viewElement = createObjectView(viewName, modelElement, writer);
169
+ const viewAttributes = modelElement.getAttribute(getHtmlAttributeName(viewName));
170
+ writer.addClass('html-object-embed__content', viewElement);
171
+ if (viewAttributes) {
172
+ setViewAttributes(writer, viewAttributes, viewElement);
173
+ }
174
+ // Widget cannot be a raw element because the widget system would not be able
175
+ // to add its UI to it. Thus, we need separate view container.
176
+ const viewContainer = writer.createContainerElement(isInline ? 'span' : 'div', {
177
+ class: 'html-object-embed',
178
+ 'data-html-object-embed-label': widgetLabel
179
+ }, viewElement);
180
+ return toWidget(viewContainer, writer, {
181
+ label: widgetLabel
182
+ });
183
+ };
184
+ }
185
+ /**
186
+ * Creates object view element from the given model element.
187
+ */ function createObjectView(viewName, modelElement, writer) {
188
+ return writer.createRawElement(viewName, null, (domElement, domConverter)=>{
189
+ domConverter.setContentOf(domElement, modelElement.getAttribute('htmlContent'));
190
+ });
191
+ }
192
+ /**
193
+ * View-to-attribute conversion helper preserving inline element attributes on `$text`.
194
+ *
195
+ * @returns Returns a conversion callback.
196
+ */ function viewToAttributeInlineConverter({ view: viewName, model: attributeKey, allowEmpty }, dataFilter) {
197
+ return (dispatcher)=>{
198
+ dispatcher.on(`element:${viewName}`, (evt, data, conversionApi)=>{
199
+ let viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
200
+ // Do not apply the attribute if the element itself is already consumed and there are no view attributes to store.
201
+ if (!viewAttributes && !conversionApi.consumable.test(data.viewItem, {
202
+ name: true
203
+ })) {
204
+ return;
205
+ }
206
+ // Otherwise, we might need to convert it to an empty object just to preserve element itself,
207
+ // for example `<cite>` => <$text htmlCite="{}">.
208
+ viewAttributes = viewAttributes || {};
209
+ // Consume the element itself if it wasn't consumed by any other converter.
210
+ conversionApi.consumable.consume(data.viewItem, {
211
+ name: true
212
+ });
213
+ // Since we are converting to attribute we need a range on which we will set the attribute.
214
+ // If the range is not created yet, we will create it.
215
+ if (!data.modelRange) {
216
+ data = Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
217
+ }
218
+ // Convert empty inline element if allowed and has any attributes.
219
+ if (allowEmpty && data.modelRange.isCollapsed && Object.keys(viewAttributes).length) {
220
+ const modelElement = conversionApi.writer.createElement('htmlEmptyElement');
221
+ if (!conversionApi.safeInsert(modelElement, data.modelCursor)) {
222
+ return;
223
+ }
224
+ const parts = conversionApi.getSplitParts(modelElement);
225
+ data.modelRange = conversionApi.writer.createRange(data.modelRange.start, conversionApi.writer.createPositionAfter(parts[parts.length - 1]));
226
+ conversionApi.updateConversionResult(modelElement, data);
227
+ setAttributeOnItem(modelElement, viewAttributes, conversionApi);
228
+ return;
229
+ }
230
+ // Set attribute on each item in range according to the schema.
231
+ for (const node of data.modelRange.getItems()){
232
+ setAttributeOnItem(node, viewAttributes, conversionApi);
233
+ }
234
+ }, {
235
+ priority: 'low'
236
+ });
237
+ };
238
+ function setAttributeOnItem(node, viewAttributes, conversionApi) {
239
+ if (conversionApi.schema.checkAttribute(node, attributeKey)) {
240
+ // Node's children are converted recursively, so node can already include model attribute.
241
+ // We want to extend it, not replace.
242
+ const nodeAttributes = node.getAttribute(attributeKey);
243
+ const attributesToAdd = mergeViewElementAttributes(viewAttributes, nodeAttributes || {});
244
+ conversionApi.writer.setAttribute(attributeKey, attributesToAdd, node);
245
+ }
246
+ }
247
+ }
248
+ /**
249
+ * Conversion helper converting an empty inline model element to an HTML element or widget.
250
+ */ function emptyInlineModelElementToViewConverter({ model: attributeKey, view: viewName }, asWidget) {
251
+ return (item, { writer, consumable })=>{
252
+ if (!item.hasAttribute(attributeKey)) {
253
+ return null;
254
+ }
255
+ const viewElement = writer.createContainerElement(viewName);
256
+ const attributeValue = item.getAttribute(attributeKey);
257
+ consumable.consume(item, `attribute:${attributeKey}`);
258
+ setViewAttributes(writer, attributeValue, viewElement);
259
+ viewElement.getFillerOffset = ()=>null;
260
+ return asWidget ? toWidget(viewElement, writer) : viewElement;
261
+ };
262
+ }
263
+ /**
264
+ * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
265
+ *
266
+ * @returns Returns a conversion callback.
267
+ */ function attributeToViewInlineConverter({ priority, view: viewName }) {
268
+ return (attributeValue, conversionApi)=>{
269
+ if (!attributeValue) {
270
+ return;
271
+ }
272
+ const { writer } = conversionApi;
273
+ const viewElement = writer.createAttributeElement(viewName, null, {
274
+ priority
275
+ });
276
+ setViewAttributes(writer, attributeValue, viewElement);
277
+ return viewElement;
278
+ };
279
+ }
280
+ /**
281
+ * View-to-model conversion helper preserving allowed attributes on block element.
282
+ *
283
+ * All matched attributes will be preserved on `html*Attributes` attribute.
284
+ *
285
+ * @returns Returns a conversion callback.
286
+ */ function viewToModelBlockAttributeConverter({ view: viewName }, dataFilter) {
287
+ return (dispatcher)=>{
288
+ dispatcher.on(`element:${viewName}`, (evt, data, conversionApi)=>{
289
+ // Converting an attribute of an element that has not been converted to anything does not make sense
290
+ // because there will be nowhere to set that attribute on. At this stage, the element should've already
291
+ // been converted. A collapsed range can show up in to-do lists (<input>) or complex widgets (e.g. table).
292
+ // (https://github.com/ckeditor/ckeditor5/issues/11000).
293
+ if (!data.modelRange || data.modelRange.isCollapsed) {
294
+ return;
295
+ }
296
+ const viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
297
+ if (!viewAttributes) {
298
+ return;
299
+ }
300
+ conversionApi.writer.setAttribute(getHtmlAttributeName(data.viewItem.name), viewAttributes, data.modelRange);
301
+ }, {
302
+ priority: 'low'
303
+ });
304
+ };
305
+ }
306
+ /**
307
+ * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
308
+ * for block elements.
309
+ *
310
+ * @returns Returns a conversion callback.
311
+ */ function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }) {
312
+ return (dispatcher)=>{
313
+ dispatcher.on(`attribute:${getHtmlAttributeName(viewName)}:${modelName}`, (evt, data, conversionApi)=>{
314
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
315
+ return;
316
+ }
317
+ const { attributeOldValue, attributeNewValue } = data;
318
+ const viewWriter = conversionApi.writer;
319
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
320
+ updateViewAttributes(viewWriter, attributeOldValue, attributeNewValue, viewElement);
321
+ });
322
+ };
323
+ }
324
+
325
+ /**
326
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
327
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
328
+ */ /**
329
+ * @module html-support/schemadefinitions
330
+ */ // Skipped elements due to HTML deprecation:
331
+ // * noframes (not sure if we should provide support for this element. CKE4 is not supporting frameset and frame,
332
+ // but it will unpack <frameset><noframes>foobar</noframes></frameset> to <noframes>foobar</noframes>, so there
333
+ // may be some content loss. Although using noframes as a standalone element seems invalid)
334
+ // * keygen (this one is also empty)
335
+ // * applet (support is limited mostly to old IE)
336
+ // * basefont (this one is also empty)
337
+ // * isindex (basically no support for modern browsers at all)
338
+ //
339
+ // Skipped elements due to lack empty element support:
340
+ // * hr
341
+ // * area
342
+ // * br
343
+ // * command
344
+ // * map
345
+ // * wbr
346
+ // * colgroup -> col
347
+ //
348
+ // Skipped elements due to complexity:
349
+ // * datalist with option elements used as a data source for input[list] element
350
+ //
351
+ // Skipped elements as they are handled as an object content:
352
+ // * track
353
+ // * source
354
+ // * option
355
+ // * param
356
+ // * optgroup
357
+ //
358
+ // Skipped full page HTML elements:
359
+ // * body
360
+ // * html
361
+ // * title
362
+ // * head
363
+ // * meta
364
+ // * link
365
+ // * etc...
366
+ //
367
+ // Skipped hidden elements:
368
+ // noscript
369
+ var defaultConfig = {
370
+ block: [
371
+ // Existing features.
372
+ {
373
+ model: 'codeBlock',
374
+ view: 'pre'
375
+ },
376
+ {
377
+ model: 'paragraph',
378
+ view: 'p'
379
+ },
380
+ {
381
+ model: 'blockQuote',
382
+ view: 'blockquote'
383
+ },
384
+ {
385
+ model: 'listItem',
386
+ view: 'li'
387
+ },
388
+ {
389
+ model: 'pageBreak',
390
+ view: 'div'
391
+ },
392
+ {
393
+ model: 'rawHtml',
394
+ view: 'div'
395
+ },
396
+ {
397
+ model: 'table',
398
+ view: 'table'
399
+ },
400
+ {
401
+ model: 'tableRow',
402
+ view: 'tr'
403
+ },
404
+ {
405
+ model: 'tableCell',
406
+ view: 'td'
407
+ },
408
+ {
409
+ model: 'tableCell',
410
+ view: 'th'
411
+ },
412
+ {
413
+ model: 'tableColumnGroup',
414
+ view: 'colgroup'
415
+ },
416
+ {
417
+ model: 'tableColumn',
418
+ view: 'col'
419
+ },
420
+ {
421
+ model: 'caption',
422
+ view: 'caption'
423
+ },
424
+ {
425
+ model: 'caption',
426
+ view: 'figcaption'
427
+ },
428
+ {
429
+ model: 'imageBlock',
430
+ view: 'img'
431
+ },
432
+ {
433
+ model: 'imageInline',
434
+ view: 'img'
435
+ },
436
+ // Compatibility features.
437
+ {
438
+ model: 'htmlP',
439
+ view: 'p',
440
+ modelSchema: {
441
+ inheritAllFrom: '$block'
442
+ }
443
+ },
444
+ {
445
+ model: 'htmlBlockquote',
446
+ view: 'blockquote',
447
+ modelSchema: {
448
+ inheritAllFrom: '$container'
449
+ }
450
+ },
451
+ {
452
+ model: 'htmlTable',
453
+ view: 'table',
454
+ modelSchema: {
455
+ allowWhere: '$block',
456
+ isBlock: true
457
+ }
458
+ },
459
+ {
460
+ model: 'htmlTbody',
461
+ view: 'tbody',
462
+ modelSchema: {
463
+ allowIn: 'htmlTable',
464
+ isBlock: false
465
+ }
466
+ },
467
+ {
468
+ model: 'htmlThead',
469
+ view: 'thead',
470
+ modelSchema: {
471
+ allowIn: 'htmlTable',
472
+ isBlock: false
473
+ }
474
+ },
475
+ {
476
+ model: 'htmlTfoot',
477
+ view: 'tfoot',
478
+ modelSchema: {
479
+ allowIn: 'htmlTable',
480
+ isBlock: false
481
+ }
482
+ },
483
+ {
484
+ model: 'htmlCaption',
485
+ view: 'caption',
486
+ modelSchema: {
487
+ allowIn: 'htmlTable',
488
+ allowChildren: '$text',
489
+ isBlock: false
490
+ }
491
+ },
492
+ {
493
+ model: 'htmlColgroup',
494
+ view: 'colgroup',
495
+ modelSchema: {
496
+ allowIn: 'htmlTable',
497
+ allowChildren: 'col',
498
+ isBlock: false
499
+ }
500
+ },
501
+ {
502
+ model: 'htmlCol',
503
+ view: 'col',
504
+ modelSchema: {
505
+ allowIn: 'htmlColgroup',
506
+ isBlock: false
507
+ }
508
+ },
509
+ {
510
+ model: 'htmlTr',
511
+ view: 'tr',
512
+ modelSchema: {
513
+ allowIn: [
514
+ 'htmlTable',
515
+ 'htmlThead',
516
+ 'htmlTbody'
517
+ ],
518
+ isLimit: true
519
+ }
520
+ },
521
+ // TODO can also include text.
522
+ {
523
+ model: 'htmlTd',
524
+ view: 'td',
525
+ modelSchema: {
526
+ allowIn: 'htmlTr',
527
+ allowContentOf: '$container',
528
+ isLimit: true,
529
+ isBlock: false
530
+ }
531
+ },
532
+ // TODO can also include text.
533
+ {
534
+ model: 'htmlTh',
535
+ view: 'th',
536
+ modelSchema: {
537
+ allowIn: 'htmlTr',
538
+ allowContentOf: '$container',
539
+ isLimit: true,
540
+ isBlock: false
541
+ }
542
+ },
543
+ // TODO can also include text.
544
+ {
545
+ model: 'htmlFigure',
546
+ view: 'figure',
547
+ modelSchema: {
548
+ inheritAllFrom: '$container',
549
+ isBlock: false
550
+ }
551
+ },
552
+ // TODO can also include other block elements.
553
+ {
554
+ model: 'htmlFigcaption',
555
+ view: 'figcaption',
556
+ modelSchema: {
557
+ allowIn: 'htmlFigure',
558
+ allowChildren: '$text',
559
+ isBlock: false
560
+ }
561
+ },
562
+ // TODO can also include text.
563
+ {
564
+ model: 'htmlAddress',
565
+ view: 'address',
566
+ modelSchema: {
567
+ inheritAllFrom: '$container',
568
+ isBlock: false
569
+ }
570
+ },
571
+ // TODO can also include text.
572
+ {
573
+ model: 'htmlAside',
574
+ view: 'aside',
575
+ modelSchema: {
576
+ inheritAllFrom: '$container',
577
+ isBlock: false
578
+ }
579
+ },
580
+ // TODO can also include text.
581
+ {
582
+ model: 'htmlMain',
583
+ view: 'main',
584
+ modelSchema: {
585
+ inheritAllFrom: '$container',
586
+ isBlock: false
587
+ }
588
+ },
589
+ // TODO can also include text.
590
+ {
591
+ model: 'htmlDetails',
592
+ view: 'details',
593
+ modelSchema: {
594
+ inheritAllFrom: '$container',
595
+ isBlock: false
596
+ }
597
+ },
598
+ {
599
+ model: 'htmlSummary',
600
+ view: 'summary',
601
+ modelSchema: {
602
+ allowChildren: [
603
+ 'htmlH1',
604
+ 'htmlH2',
605
+ 'htmlH3',
606
+ 'htmlH4',
607
+ 'htmlH5',
608
+ 'htmlH6',
609
+ '$text'
610
+ ],
611
+ allowIn: 'htmlDetails',
612
+ isBlock: false
613
+ }
614
+ },
615
+ {
616
+ model: 'htmlDiv',
617
+ view: 'div',
618
+ paragraphLikeModel: 'htmlDivParagraph',
619
+ modelSchema: {
620
+ inheritAllFrom: '$container'
621
+ }
622
+ },
623
+ // TODO can also include text.
624
+ {
625
+ model: 'htmlFieldset',
626
+ view: 'fieldset',
627
+ modelSchema: {
628
+ inheritAllFrom: '$container',
629
+ isBlock: false
630
+ }
631
+ },
632
+ // TODO can also include h1-h6.
633
+ {
634
+ model: 'htmlLegend',
635
+ view: 'legend',
636
+ modelSchema: {
637
+ allowIn: 'htmlFieldset',
638
+ allowChildren: '$text'
639
+ }
640
+ },
641
+ // TODO can also include text.
642
+ {
643
+ model: 'htmlHeader',
644
+ view: 'header',
645
+ modelSchema: {
646
+ inheritAllFrom: '$container',
647
+ isBlock: false
648
+ }
649
+ },
650
+ // TODO can also include text.
651
+ {
652
+ model: 'htmlFooter',
653
+ view: 'footer',
654
+ modelSchema: {
655
+ inheritAllFrom: '$container',
656
+ isBlock: false
657
+ }
658
+ },
659
+ // TODO can also include text.
660
+ {
661
+ model: 'htmlForm',
662
+ view: 'form',
663
+ modelSchema: {
664
+ inheritAllFrom: '$container',
665
+ isBlock: true
666
+ }
667
+ },
668
+ {
669
+ model: 'htmlHgroup',
670
+ view: 'hgroup',
671
+ modelSchema: {
672
+ allowIn: [
673
+ '$root',
674
+ '$container'
675
+ ],
676
+ allowChildren: [
677
+ 'paragraph',
678
+ 'htmlP',
679
+ 'htmlH1',
680
+ 'htmlH2',
681
+ 'htmlH3',
682
+ 'htmlH4',
683
+ 'htmlH5',
684
+ 'htmlH6'
685
+ ],
686
+ isBlock: false
687
+ }
688
+ },
689
+ {
690
+ model: 'htmlH1',
691
+ view: 'h1',
692
+ modelSchema: {
693
+ inheritAllFrom: '$block'
694
+ }
695
+ },
696
+ {
697
+ model: 'htmlH2',
698
+ view: 'h2',
699
+ modelSchema: {
700
+ inheritAllFrom: '$block'
701
+ }
702
+ },
703
+ {
704
+ model: 'htmlH3',
705
+ view: 'h3',
706
+ modelSchema: {
707
+ inheritAllFrom: '$block'
708
+ }
709
+ },
710
+ {
711
+ model: 'htmlH4',
712
+ view: 'h4',
713
+ modelSchema: {
714
+ inheritAllFrom: '$block'
715
+ }
716
+ },
717
+ {
718
+ model: 'htmlH5',
719
+ view: 'h5',
720
+ modelSchema: {
721
+ inheritAllFrom: '$block'
722
+ }
723
+ },
724
+ {
725
+ model: 'htmlH6',
726
+ view: 'h6',
727
+ modelSchema: {
728
+ inheritAllFrom: '$block'
729
+ }
730
+ },
731
+ {
732
+ model: '$htmlList',
733
+ modelSchema: {
734
+ allowWhere: '$container',
735
+ allowChildren: [
736
+ '$htmlList',
737
+ 'htmlLi'
738
+ ],
739
+ isBlock: false
740
+ }
741
+ },
742
+ {
743
+ model: 'htmlDir',
744
+ view: 'dir',
745
+ modelSchema: {
746
+ inheritAllFrom: '$htmlList'
747
+ }
748
+ },
749
+ {
750
+ model: 'htmlMenu',
751
+ view: 'menu',
752
+ modelSchema: {
753
+ inheritAllFrom: '$htmlList'
754
+ }
755
+ },
756
+ {
757
+ model: 'htmlUl',
758
+ view: 'ul',
759
+ modelSchema: {
760
+ inheritAllFrom: '$htmlList'
761
+ }
762
+ },
763
+ {
764
+ model: 'htmlOl',
765
+ view: 'ol',
766
+ modelSchema: {
767
+ inheritAllFrom: '$htmlList'
768
+ }
769
+ },
770
+ // TODO can also include other block elements.
771
+ {
772
+ model: 'htmlLi',
773
+ view: 'li',
774
+ modelSchema: {
775
+ allowIn: '$htmlList',
776
+ allowChildren: '$text',
777
+ isBlock: false
778
+ }
779
+ },
780
+ {
781
+ model: 'htmlPre',
782
+ view: 'pre',
783
+ modelSchema: {
784
+ inheritAllFrom: '$block'
785
+ }
786
+ },
787
+ {
788
+ model: 'htmlArticle',
789
+ view: 'article',
790
+ modelSchema: {
791
+ inheritAllFrom: '$container',
792
+ isBlock: false
793
+ }
794
+ },
795
+ {
796
+ model: 'htmlSection',
797
+ view: 'section',
798
+ modelSchema: {
799
+ inheritAllFrom: '$container',
800
+ isBlock: false
801
+ }
802
+ },
803
+ // TODO can also include text.
804
+ {
805
+ model: 'htmlNav',
806
+ view: 'nav',
807
+ modelSchema: {
808
+ inheritAllFrom: '$container',
809
+ isBlock: false
810
+ }
811
+ },
812
+ {
813
+ model: 'htmlDivDl',
814
+ view: 'div',
815
+ modelSchema: {
816
+ allowChildren: [
817
+ 'htmlDt',
818
+ 'htmlDd'
819
+ ],
820
+ allowIn: 'htmlDl'
821
+ }
822
+ },
823
+ {
824
+ model: 'htmlDl',
825
+ view: 'dl',
826
+ modelSchema: {
827
+ allowWhere: '$container',
828
+ allowChildren: [
829
+ 'htmlDt',
830
+ 'htmlDd',
831
+ 'htmlDivDl'
832
+ ],
833
+ isBlock: false
834
+ }
835
+ },
836
+ {
837
+ model: 'htmlDt',
838
+ view: 'dt',
839
+ modelSchema: {
840
+ allowChildren: '$block',
841
+ isBlock: false
842
+ }
843
+ },
844
+ {
845
+ model: 'htmlDd',
846
+ view: 'dd',
847
+ modelSchema: {
848
+ allowChildren: '$block',
849
+ isBlock: false
850
+ }
851
+ },
852
+ {
853
+ model: 'htmlCenter',
854
+ view: 'center',
855
+ modelSchema: {
856
+ inheritAllFrom: '$container',
857
+ isBlock: false
858
+ }
859
+ }
860
+ ],
861
+ inline: [
862
+ // Existing features (attribute set on an existing model element).
863
+ {
864
+ model: 'htmlLiAttributes',
865
+ view: 'li',
866
+ appliesToBlock: true,
867
+ coupledAttribute: 'listItemId'
868
+ },
869
+ {
870
+ model: 'htmlOlAttributes',
871
+ view: 'ol',
872
+ appliesToBlock: true,
873
+ coupledAttribute: 'listItemId'
874
+ },
875
+ {
876
+ model: 'htmlUlAttributes',
877
+ view: 'ul',
878
+ appliesToBlock: true,
879
+ coupledAttribute: 'listItemId'
880
+ },
881
+ {
882
+ model: 'htmlFigureAttributes',
883
+ view: 'figure',
884
+ appliesToBlock: 'table'
885
+ },
886
+ {
887
+ model: 'htmlTheadAttributes',
888
+ view: 'thead',
889
+ appliesToBlock: 'table'
890
+ },
891
+ {
892
+ model: 'htmlTbodyAttributes',
893
+ view: 'tbody',
894
+ appliesToBlock: 'table'
895
+ },
896
+ {
897
+ model: 'htmlFigureAttributes',
898
+ view: 'figure',
899
+ appliesToBlock: 'imageBlock'
900
+ },
901
+ // Compatibility features.
902
+ {
903
+ model: 'htmlAcronym',
904
+ view: 'acronym',
905
+ attributeProperties: {
906
+ copyOnEnter: true,
907
+ isFormatting: true
908
+ }
909
+ },
910
+ {
911
+ model: 'htmlTt',
912
+ view: 'tt',
913
+ attributeProperties: {
914
+ copyOnEnter: true,
915
+ isFormatting: true
916
+ }
917
+ },
918
+ {
919
+ model: 'htmlFont',
920
+ view: 'font',
921
+ attributeProperties: {
922
+ copyOnEnter: true,
923
+ isFormatting: true
924
+ }
925
+ },
926
+ {
927
+ model: 'htmlTime',
928
+ view: 'time',
929
+ attributeProperties: {
930
+ copyOnEnter: true,
931
+ isFormatting: true
932
+ }
933
+ },
934
+ {
935
+ model: 'htmlVar',
936
+ view: 'var',
937
+ attributeProperties: {
938
+ copyOnEnter: true,
939
+ isFormatting: true
940
+ }
941
+ },
942
+ {
943
+ model: 'htmlBig',
944
+ view: 'big',
945
+ attributeProperties: {
946
+ copyOnEnter: true,
947
+ isFormatting: true
948
+ }
949
+ },
950
+ {
951
+ model: 'htmlSmall',
952
+ view: 'small',
953
+ attributeProperties: {
954
+ copyOnEnter: true,
955
+ isFormatting: true
956
+ }
957
+ },
958
+ {
959
+ model: 'htmlSamp',
960
+ view: 'samp',
961
+ attributeProperties: {
962
+ copyOnEnter: true,
963
+ isFormatting: true
964
+ }
965
+ },
966
+ {
967
+ model: 'htmlQ',
968
+ view: 'q',
969
+ attributeProperties: {
970
+ copyOnEnter: true,
971
+ isFormatting: true
972
+ }
973
+ },
974
+ {
975
+ model: 'htmlOutput',
976
+ view: 'output',
977
+ attributeProperties: {
978
+ copyOnEnter: true,
979
+ isFormatting: true
980
+ }
981
+ },
982
+ {
983
+ model: 'htmlKbd',
984
+ view: 'kbd',
985
+ attributeProperties: {
986
+ copyOnEnter: true,
987
+ isFormatting: true
988
+ }
989
+ },
990
+ {
991
+ model: 'htmlBdi',
992
+ view: 'bdi',
993
+ attributeProperties: {
994
+ copyOnEnter: true,
995
+ isFormatting: true
996
+ }
997
+ },
998
+ {
999
+ model: 'htmlBdo',
1000
+ view: 'bdo',
1001
+ attributeProperties: {
1002
+ copyOnEnter: true,
1003
+ isFormatting: true
1004
+ }
1005
+ },
1006
+ {
1007
+ model: 'htmlAbbr',
1008
+ view: 'abbr',
1009
+ attributeProperties: {
1010
+ copyOnEnter: true,
1011
+ isFormatting: true
1012
+ }
1013
+ },
1014
+ {
1015
+ model: 'htmlA',
1016
+ view: 'a',
1017
+ priority: 5,
1018
+ coupledAttribute: 'linkHref'
1019
+ },
1020
+ {
1021
+ model: 'htmlStrong',
1022
+ view: 'strong',
1023
+ coupledAttribute: 'bold',
1024
+ attributeProperties: {
1025
+ copyOnEnter: true,
1026
+ isFormatting: true
1027
+ }
1028
+ },
1029
+ {
1030
+ model: 'htmlB',
1031
+ view: 'b',
1032
+ coupledAttribute: 'bold',
1033
+ attributeProperties: {
1034
+ copyOnEnter: true,
1035
+ isFormatting: true
1036
+ }
1037
+ },
1038
+ {
1039
+ model: 'htmlI',
1040
+ view: 'i',
1041
+ coupledAttribute: 'italic',
1042
+ attributeProperties: {
1043
+ copyOnEnter: true,
1044
+ isFormatting: true
1045
+ }
1046
+ },
1047
+ {
1048
+ model: 'htmlEm',
1049
+ view: 'em',
1050
+ coupledAttribute: 'italic',
1051
+ attributeProperties: {
1052
+ copyOnEnter: true,
1053
+ isFormatting: true
1054
+ }
1055
+ },
1056
+ {
1057
+ model: 'htmlS',
1058
+ view: 's',
1059
+ coupledAttribute: 'strikethrough',
1060
+ attributeProperties: {
1061
+ copyOnEnter: true,
1062
+ isFormatting: true
1063
+ }
1064
+ },
1065
+ // TODO According to HTML-spec can behave as div-like element, although CKE4 only handles it as an inline element.
1066
+ {
1067
+ model: 'htmlDel',
1068
+ view: 'del',
1069
+ coupledAttribute: 'strikethrough',
1070
+ attributeProperties: {
1071
+ copyOnEnter: true,
1072
+ isFormatting: true
1073
+ }
1074
+ },
1075
+ // TODO According to HTML-spec can behave as div-like element, although CKE4 only handles it as an inline element.
1076
+ {
1077
+ model: 'htmlIns',
1078
+ view: 'ins',
1079
+ attributeProperties: {
1080
+ copyOnEnter: true,
1081
+ isFormatting: true
1082
+ }
1083
+ },
1084
+ {
1085
+ model: 'htmlU',
1086
+ view: 'u',
1087
+ coupledAttribute: 'underline',
1088
+ attributeProperties: {
1089
+ copyOnEnter: true,
1090
+ isFormatting: true
1091
+ }
1092
+ },
1093
+ {
1094
+ model: 'htmlSub',
1095
+ view: 'sub',
1096
+ coupledAttribute: 'subscript',
1097
+ attributeProperties: {
1098
+ copyOnEnter: true,
1099
+ isFormatting: true
1100
+ }
1101
+ },
1102
+ {
1103
+ model: 'htmlSup',
1104
+ view: 'sup',
1105
+ coupledAttribute: 'superscript',
1106
+ attributeProperties: {
1107
+ copyOnEnter: true,
1108
+ isFormatting: true
1109
+ }
1110
+ },
1111
+ {
1112
+ model: 'htmlCode',
1113
+ view: 'code',
1114
+ coupledAttribute: 'code',
1115
+ attributeProperties: {
1116
+ copyOnEnter: true,
1117
+ isFormatting: true
1118
+ }
1119
+ },
1120
+ {
1121
+ model: 'htmlMark',
1122
+ view: 'mark',
1123
+ attributeProperties: {
1124
+ copyOnEnter: true,
1125
+ isFormatting: true
1126
+ }
1127
+ },
1128
+ {
1129
+ model: 'htmlSpan',
1130
+ view: 'span',
1131
+ attributeProperties: {
1132
+ copyOnEnter: true,
1133
+ isFormatting: true
1134
+ }
1135
+ },
1136
+ {
1137
+ model: 'htmlCite',
1138
+ view: 'cite',
1139
+ attributeProperties: {
1140
+ copyOnEnter: true,
1141
+ isFormatting: true
1142
+ }
1143
+ },
1144
+ {
1145
+ model: 'htmlLabel',
1146
+ view: 'label',
1147
+ attributeProperties: {
1148
+ copyOnEnter: true,
1149
+ isFormatting: true
1150
+ }
1151
+ },
1152
+ {
1153
+ model: 'htmlDfn',
1154
+ view: 'dfn',
1155
+ attributeProperties: {
1156
+ copyOnEnter: true,
1157
+ isFormatting: true
1158
+ }
1159
+ },
1160
+ // Objects.
1161
+ {
1162
+ model: 'htmlObject',
1163
+ view: 'object',
1164
+ isObject: true,
1165
+ modelSchema: {
1166
+ inheritAllFrom: '$inlineObject'
1167
+ }
1168
+ },
1169
+ {
1170
+ model: 'htmlIframe',
1171
+ view: 'iframe',
1172
+ isObject: true,
1173
+ modelSchema: {
1174
+ inheritAllFrom: '$inlineObject'
1175
+ }
1176
+ },
1177
+ {
1178
+ model: 'htmlInput',
1179
+ view: 'input',
1180
+ isObject: true,
1181
+ modelSchema: {
1182
+ inheritAllFrom: '$inlineObject'
1183
+ }
1184
+ },
1185
+ {
1186
+ model: 'htmlButton',
1187
+ view: 'button',
1188
+ isObject: true,
1189
+ modelSchema: {
1190
+ inheritAllFrom: '$inlineObject'
1191
+ }
1192
+ },
1193
+ {
1194
+ model: 'htmlTextarea',
1195
+ view: 'textarea',
1196
+ isObject: true,
1197
+ modelSchema: {
1198
+ inheritAllFrom: '$inlineObject'
1199
+ }
1200
+ },
1201
+ {
1202
+ model: 'htmlSelect',
1203
+ view: 'select',
1204
+ isObject: true,
1205
+ modelSchema: {
1206
+ inheritAllFrom: '$inlineObject'
1207
+ }
1208
+ },
1209
+ {
1210
+ model: 'htmlVideo',
1211
+ view: 'video',
1212
+ isObject: true,
1213
+ modelSchema: {
1214
+ inheritAllFrom: '$inlineObject'
1215
+ }
1216
+ },
1217
+ {
1218
+ model: 'htmlEmbed',
1219
+ view: 'embed',
1220
+ isObject: true,
1221
+ modelSchema: {
1222
+ inheritAllFrom: '$inlineObject'
1223
+ }
1224
+ },
1225
+ {
1226
+ model: 'htmlOembed',
1227
+ view: 'oembed',
1228
+ isObject: true,
1229
+ modelSchema: {
1230
+ inheritAllFrom: '$inlineObject'
1231
+ }
1232
+ },
1233
+ {
1234
+ model: 'htmlAudio',
1235
+ view: 'audio',
1236
+ isObject: true,
1237
+ modelSchema: {
1238
+ inheritAllFrom: '$inlineObject'
1239
+ }
1240
+ },
1241
+ {
1242
+ model: 'htmlImg',
1243
+ view: 'img',
1244
+ isObject: true,
1245
+ modelSchema: {
1246
+ inheritAllFrom: '$inlineObject'
1247
+ }
1248
+ },
1249
+ {
1250
+ model: 'htmlCanvas',
1251
+ view: 'canvas',
1252
+ isObject: true,
1253
+ modelSchema: {
1254
+ inheritAllFrom: '$inlineObject'
1255
+ }
1256
+ },
1257
+ // TODO it could be probably represented as non-object element, although it has graphical representation,
1258
+ // so probably makes more sense to keep it as an object.
1259
+ {
1260
+ model: 'htmlMeter',
1261
+ view: 'meter',
1262
+ isObject: true,
1263
+ modelSchema: {
1264
+ inheritAllFrom: '$inlineObject'
1265
+ }
1266
+ },
1267
+ // TODO it could be probably represented as non-object element, although it has graphical representation,
1268
+ // so probably makes more sense to keep it as an object.
1269
+ {
1270
+ model: 'htmlProgress',
1271
+ view: 'progress',
1272
+ isObject: true,
1273
+ modelSchema: {
1274
+ inheritAllFrom: '$inlineObject'
1275
+ }
1276
+ },
1277
+ {
1278
+ model: 'htmlScript',
1279
+ view: 'script',
1280
+ modelSchema: {
1281
+ allowWhere: [
1282
+ '$text',
1283
+ '$block'
1284
+ ],
1285
+ isInline: true
1286
+ }
1287
+ },
1288
+ {
1289
+ model: 'htmlStyle',
1290
+ view: 'style',
1291
+ modelSchema: {
1292
+ allowWhere: [
1293
+ '$text',
1294
+ '$block'
1295
+ ],
1296
+ isInline: true
1297
+ }
1298
+ },
1299
+ {
1300
+ model: 'htmlCustomElement',
1301
+ view: '$customElement',
1302
+ modelSchema: {
1303
+ allowWhere: [
1304
+ '$text',
1305
+ '$block'
1306
+ ],
1307
+ allowAttributesOf: '$inlineObject',
1308
+ isInline: true
1309
+ }
1310
+ }
1311
+ ]
1312
+ };
1313
+
1314
+ /**
1315
+ * Holds representation of the extended HTML document type definitions to be used by the
1316
+ * editor in HTML support.
1317
+ *
1318
+ * Data schema is represented by data schema definitions.
1319
+ *
1320
+ * To add new definition for block element,
1321
+ * use {@link module:html-support/dataschema~DataSchema#registerBlockElement} method:
1322
+ *
1323
+ * ```ts
1324
+ * dataSchema.registerBlockElement( {
1325
+ * view: 'section',
1326
+ * model: 'my-section',
1327
+ * modelSchema: {
1328
+ * inheritAllFrom: '$block'
1329
+ * }
1330
+ * } );
1331
+ * ```
1332
+ *
1333
+ * To add new definition for inline element,
1334
+ * use {@link module:html-support/dataschema~DataSchema#registerInlineElement} method:
1335
+ *
1336
+ * ```
1337
+ * dataSchema.registerInlineElement( {
1338
+ * view: 'span',
1339
+ * model: 'my-span',
1340
+ * attributeProperties: {
1341
+ * copyOnEnter: true
1342
+ * }
1343
+ * } );
1344
+ * ```
1345
+ */ class DataSchema extends Plugin {
1346
+ /**
1347
+ * A map of registered data schema definitions.
1348
+ */ _definitions = [];
1349
+ /**
1350
+ * @inheritDoc
1351
+ */ static get pluginName() {
1352
+ return 'DataSchema';
1353
+ }
1354
+ /**
1355
+ * @inheritDoc
1356
+ */ static get isOfficialPlugin() {
1357
+ return true;
1358
+ }
1359
+ /**
1360
+ * @inheritDoc
1361
+ */ init() {
1362
+ for (const definition of defaultConfig.block){
1363
+ this.registerBlockElement(definition);
1364
+ }
1365
+ for (const definition of defaultConfig.inline){
1366
+ this.registerInlineElement(definition);
1367
+ }
1368
+ }
1369
+ /**
1370
+ * Add new data schema definition describing block element.
1371
+ */ registerBlockElement(definition) {
1372
+ this._definitions.push({
1373
+ ...definition,
1374
+ isBlock: true
1375
+ });
1376
+ }
1377
+ /**
1378
+ * Add new data schema definition describing inline element.
1379
+ */ registerInlineElement(definition) {
1380
+ this._definitions.push({
1381
+ ...definition,
1382
+ isInline: true
1383
+ });
1384
+ }
1385
+ /**
1386
+ * Updates schema definition describing block element with new properties.
1387
+ *
1388
+ * Creates new scheme if it doesn't exist.
1389
+ * Array properties are concatenated with original values.
1390
+ *
1391
+ * @param definition Definition update.
1392
+ */ extendBlockElement(definition) {
1393
+ this._extendDefinition({
1394
+ ...definition,
1395
+ isBlock: true
1396
+ });
1397
+ }
1398
+ /**
1399
+ * Updates schema definition describing inline element with new properties.
1400
+ *
1401
+ * Creates new scheme if it doesn't exist.
1402
+ * Array properties are concatenated with original values.
1403
+ *
1404
+ * @param definition Definition update.
1405
+ */ extendInlineElement(definition) {
1406
+ this._extendDefinition({
1407
+ ...definition,
1408
+ isInline: true
1409
+ });
1410
+ }
1411
+ /**
1412
+ * Returns all definitions matching the given view name.
1413
+ *
1414
+ * @param includeReferences Indicates if this method should also include definitions of referenced models.
1415
+ */ getDefinitionsForView(viewName, includeReferences = false) {
1416
+ const definitions = new Set();
1417
+ for (const definition of this._getMatchingViewDefinitions(viewName)){
1418
+ if (includeReferences) {
1419
+ for (const reference of this._getReferences(definition.model)){
1420
+ definitions.add(reference);
1421
+ }
1422
+ }
1423
+ definitions.add(definition);
1424
+ }
1425
+ return definitions;
1426
+ }
1427
+ /**
1428
+ * Returns definitions matching the given model name.
1429
+ */ getDefinitionsForModel(modelName) {
1430
+ return this._definitions.filter((definition)=>definition.model == modelName);
1431
+ }
1432
+ /**
1433
+ * Returns definitions matching the given view name.
1434
+ */ _getMatchingViewDefinitions(viewName) {
1435
+ return this._definitions.filter((def)=>def.view && testViewName(viewName, def.view));
1436
+ }
1437
+ /**
1438
+ * Resolves all definition references registered for the given data schema definition.
1439
+ *
1440
+ * @param modelName Data schema model name.
1441
+ */ *_getReferences(modelName) {
1442
+ const inheritProperties = [
1443
+ 'inheritAllFrom',
1444
+ 'inheritTypesFrom',
1445
+ 'allowWhere',
1446
+ 'allowContentOf',
1447
+ 'allowAttributesOf'
1448
+ ];
1449
+ const definitions = this._definitions.filter((definition)=>definition.model == modelName);
1450
+ for (const { modelSchema } of definitions){
1451
+ if (!modelSchema) {
1452
+ continue;
1453
+ }
1454
+ for (const property of inheritProperties){
1455
+ for (const referenceName of toArray(modelSchema[property] || [])){
1456
+ const definitions = this._definitions.filter((definition)=>definition.model == referenceName);
1457
+ for (const definition of definitions){
1458
+ if (referenceName !== modelName) {
1459
+ yield* this._getReferences(definition.model);
1460
+ yield definition;
1461
+ }
1462
+ }
1463
+ }
1464
+ }
1465
+ }
1466
+ }
1467
+ /**
1468
+ * Updates schema definition with new properties.
1469
+ *
1470
+ * Creates new scheme if it doesn't exist.
1471
+ * Array properties are concatenated with original values.
1472
+ *
1473
+ * @param definition Definition update.
1474
+ */ _extendDefinition(definition) {
1475
+ const currentDefinitions = Array.from(this._definitions.entries()).filter(([, currentDefinition])=>currentDefinition.model == definition.model);
1476
+ if (currentDefinitions.length == 0) {
1477
+ this._definitions.push(definition);
1478
+ return;
1479
+ }
1480
+ for (const [idx, currentDefinition] of currentDefinitions){
1481
+ this._definitions[idx] = mergeWith({}, currentDefinition, definition, (target, source)=>{
1482
+ return Array.isArray(target) ? target.concat(source) : undefined;
1483
+ });
1484
+ }
1485
+ }
1486
+ }
1487
+ /**
1488
+ * Test view name against the given pattern.
1489
+ */ function testViewName(pattern, viewName) {
1490
+ if (typeof pattern === 'string') {
1491
+ return pattern === viewName;
1492
+ }
1493
+ if (pattern instanceof RegExp) {
1494
+ return pattern.test(viewName);
1495
+ }
1496
+ return false;
1497
+ }
1498
+
1499
+ /**
1500
+ * Allows to validate elements and element attributes registered by {@link module:html-support/dataschema~DataSchema}.
1501
+ *
1502
+ * To enable registered element in the editor, use {@link module:html-support/datafilter~DataFilter#allowElement} method:
1503
+ *
1504
+ * ```ts
1505
+ * dataFilter.allowElement( 'section' );
1506
+ * ```
1507
+ *
1508
+ * You can also allow or disallow specific element attributes:
1509
+ *
1510
+ * ```ts
1511
+ * // Allow `data-foo` attribute on `section` element.
1512
+ * dataFilter.allowAttributes( {
1513
+ * name: 'section',
1514
+ * attributes: {
1515
+ * 'data-foo': true
1516
+ * }
1517
+ * } );
1518
+ *
1519
+ * // Disallow `color` style attribute on 'section' element.
1520
+ * dataFilter.disallowAttributes( {
1521
+ * name: 'section',
1522
+ * styles: {
1523
+ * color: /[\s\S]+/
1524
+ * }
1525
+ * } );
1526
+ * ```
1527
+ *
1528
+ * To apply the information about allowed and disallowed attributes in custom integration plugin,
1529
+ * use the {@link module:html-support/datafilter~DataFilter#processViewAttributes `processViewAttributes()`} method.
1530
+ */ class DataFilter extends Plugin {
1531
+ /**
1532
+ * An instance of the {@link module:html-support/dataschema~DataSchema}.
1533
+ */ _dataSchema;
1534
+ /**
1535
+ * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
1536
+ * content attributes should be allowed.
1537
+ */ _allowedAttributes;
1538
+ /**
1539
+ * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
1540
+ * content attributes should be disallowed.
1541
+ */ _disallowedAttributes;
1542
+ /**
1543
+ * Allowed element definitions by {@link module:html-support/datafilter~DataFilter#allowElement} method.
1544
+ */ _allowedElements;
1545
+ /**
1546
+ * Disallowed element names by {@link module:html-support/datafilter~DataFilter#disallowElement} method.
1547
+ */ _disallowedElements;
1548
+ /**
1549
+ * Indicates if {@link module:engine/controller/datacontroller~DataController editor's data controller}
1550
+ * data has been already initialized.
1551
+ */ _dataInitialized;
1552
+ /**
1553
+ * Cached map of coupled attributes. Keys are the feature attributes names
1554
+ * and values are arrays with coupled GHS attributes names.
1555
+ */ _coupledAttributes;
1556
+ constructor(editor){
1557
+ super(editor);
1558
+ this._dataSchema = editor.plugins.get('DataSchema');
1559
+ this._allowedAttributes = new Matcher();
1560
+ this._disallowedAttributes = new Matcher();
1561
+ this._allowedElements = new Set();
1562
+ this._disallowedElements = new Set();
1563
+ this._dataInitialized = false;
1564
+ this._coupledAttributes = null;
1565
+ this._registerElementsAfterInit();
1566
+ this._registerElementHandlers();
1567
+ this._registerCoupledAttributesPostFixer();
1568
+ this._registerAssociatedHtmlAttributesPostFixer();
1569
+ }
1570
+ /**
1571
+ * @inheritDoc
1572
+ */ static get pluginName() {
1573
+ return 'DataFilter';
1574
+ }
1575
+ /**
1576
+ * @inheritDoc
1577
+ */ static get isOfficialPlugin() {
1578
+ return true;
1579
+ }
1580
+ /**
1581
+ * @inheritDoc
1582
+ */ static get requires() {
1583
+ return [
1584
+ DataSchema,
1585
+ Widget
1586
+ ];
1587
+ }
1588
+ /**
1589
+ * Load a configuration of one or many elements, where their attributes should be allowed.
1590
+ *
1591
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
1592
+ *
1593
+ * @param config Configuration of elements that should have their attributes accepted in the editor.
1594
+ */ loadAllowedConfig(config) {
1595
+ for (const pattern of config){
1596
+ // MatcherPattern allows omitting `name` to widen the search of elements.
1597
+ // Let's keep it consistent and match every element if a `name` has not been provided.
1598
+ const elementName = pattern.name || /[\s\S]+/;
1599
+ const rules = splitRules(pattern);
1600
+ this.allowElement(elementName);
1601
+ rules.forEach((pattern)=>this.allowAttributes(pattern));
1602
+ }
1603
+ }
1604
+ /**
1605
+ * Load a configuration of one or many elements, where their attributes should be disallowed.
1606
+ *
1607
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
1608
+ *
1609
+ * @param config Configuration of elements that should have their attributes rejected from the editor.
1610
+ */ loadDisallowedConfig(config) {
1611
+ for (const pattern of config){
1612
+ // MatcherPattern allows omitting `name` to widen the search of elements.
1613
+ // Let's keep it consistent and match every element if a `name` has not been provided.
1614
+ const elementName = pattern.name || /[\s\S]+/;
1615
+ const rules = splitRules(pattern);
1616
+ // Disallow element itself if there is no other rules.
1617
+ if (rules.length == 0) {
1618
+ this.disallowElement(elementName);
1619
+ } else {
1620
+ rules.forEach((pattern)=>this.disallowAttributes(pattern));
1621
+ }
1622
+ }
1623
+ }
1624
+ /**
1625
+ * Load a configuration of one or many elements, where when empty should be allowed.
1626
+ *
1627
+ * **Note**: It modifies DataSchema so must be loaded before registering filtering rules.
1628
+ *
1629
+ * @param config Configuration of elements that should be preserved even if empty.
1630
+ */ loadAllowedEmptyElementsConfig(config) {
1631
+ for (const elementName of config){
1632
+ this.allowEmptyElement(elementName);
1633
+ }
1634
+ }
1635
+ /**
1636
+ * Allow the given element in the editor context.
1637
+ *
1638
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
1639
+ * to create data filter.
1640
+ *
1641
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
1642
+ *
1643
+ * @param viewName String or regular expression matching view name.
1644
+ */ allowElement(viewName) {
1645
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)){
1646
+ this._addAllowedElement(definition);
1647
+ // Reset cached map to recalculate it on the next usage.
1648
+ this._coupledAttributes = null;
1649
+ }
1650
+ }
1651
+ /**
1652
+ * Disallow the given element in the editor context.
1653
+ *
1654
+ * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
1655
+ * to create data filter.
1656
+ *
1657
+ * @param viewName String or regular expression matching view name.
1658
+ */ disallowElement(viewName) {
1659
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, false)){
1660
+ this._disallowedElements.add(definition.view);
1661
+ }
1662
+ }
1663
+ /**
1664
+ * Allow the given empty element in the editor context.
1665
+ *
1666
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
1667
+ * to create data filter.
1668
+ *
1669
+ * **Note**: It modifies DataSchema so must be called before registering filtering rules.
1670
+ *
1671
+ * @param viewName String or regular expression matching view name.
1672
+ */ allowEmptyElement(viewName) {
1673
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)){
1674
+ if (definition.isInline) {
1675
+ this._dataSchema.extendInlineElement({
1676
+ ...definition,
1677
+ allowEmpty: true
1678
+ });
1679
+ }
1680
+ }
1681
+ }
1682
+ /**
1683
+ * Allow the given attributes for view element allowed by {@link #allowElement} method.
1684
+ *
1685
+ * @param config Pattern matching all attributes which should be allowed.
1686
+ */ allowAttributes(config) {
1687
+ this._allowedAttributes.add(config);
1688
+ }
1689
+ /**
1690
+ * Disallow the given attributes for view element allowed by {@link #allowElement} method.
1691
+ *
1692
+ * @param config Pattern matching all attributes which should be disallowed.
1693
+ */ disallowAttributes(config) {
1694
+ this._disallowedAttributes.add(config);
1695
+ }
1696
+ /**
1697
+ * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
1698
+ *
1699
+ * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
1700
+ * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
1701
+ * It returns the allowed attributes that were found on the given view element for further processing by integration code.
1702
+ *
1703
+ * ```ts
1704
+ * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
1705
+ * // Get rid of disallowed and extract all allowed attributes from a viewElement.
1706
+ * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
1707
+ * // Do something with them, i.e. store inside a model as a dictionary.
1708
+ * if ( viewAttributes ) {
1709
+ * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
1710
+ * }
1711
+ * } );
1712
+ * ```
1713
+ *
1714
+ * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
1715
+ *
1716
+ * @returns Object with following properties:
1717
+ * - attributes Set with matched attribute names.
1718
+ * - styles Set with matched style names.
1719
+ * - classes Set with matched class names.
1720
+ */ processViewAttributes(viewElement, conversionApi) {
1721
+ const { consumable } = conversionApi;
1722
+ // Make sure that the disabled attributes are handled before the allowed attributes are called.
1723
+ // For example, for block images the <figure> converter triggers conversion for <img> first and then for other elements, i.e. <a>.
1724
+ matchAndConsumeAttributes(viewElement, this._disallowedAttributes, consumable);
1725
+ return prepareGHSAttribute(viewElement, matchAndConsumeAttributes(viewElement, this._allowedAttributes, consumable));
1726
+ }
1727
+ /**
1728
+ * Adds allowed element definition and fires registration event.
1729
+ */ _addAllowedElement(definition) {
1730
+ if (this._allowedElements.has(definition)) {
1731
+ return;
1732
+ }
1733
+ this._allowedElements.add(definition);
1734
+ // For attribute based integrations (table figure, document lists, etc.) register related element definitions.
1735
+ if ('appliesToBlock' in definition && typeof definition.appliesToBlock == 'string') {
1736
+ for (const relatedDefinition of this._dataSchema.getDefinitionsForModel(definition.appliesToBlock)){
1737
+ if (relatedDefinition.isBlock) {
1738
+ this._addAllowedElement(relatedDefinition);
1739
+ }
1740
+ }
1741
+ }
1742
+ // We need to wait for all features to be initialized before we can register
1743
+ // element, so we can access existing features model schemas.
1744
+ // If the data has not been initialized yet, _registerElementsAfterInit() method will take care of
1745
+ // registering elements.
1746
+ if (this._dataInitialized) {
1747
+ // Defer registration to the next data pipeline data set so any disallow rules could be applied
1748
+ // even if added after allow rule (disallowElement).
1749
+ this.editor.data.once('set', ()=>{
1750
+ this._fireRegisterEvent(definition);
1751
+ }, {
1752
+ // With the highest priority listener we are able to register elements right before
1753
+ // running data conversion.
1754
+ priority: priorities.highest + 1
1755
+ });
1756
+ }
1757
+ }
1758
+ /**
1759
+ * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
1760
+ * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
1761
+ */ _registerElementsAfterInit() {
1762
+ this.editor.data.on('init', ()=>{
1763
+ this._dataInitialized = true;
1764
+ for (const definition of this._allowedElements){
1765
+ this._fireRegisterEvent(definition);
1766
+ }
1767
+ }, {
1768
+ // With highest priority listener we are able to register elements right before
1769
+ // running data conversion. Also:
1770
+ // * Make sure that priority is higher than the one used by `RealTimeCollaborationClient`,
1771
+ // as RTC is stopping event propagation.
1772
+ // * Make sure no other features hook into this event before GHS because otherwise the
1773
+ // downcast conversion (for these features) could run before GHS registered its converters
1774
+ // (https://github.com/ckeditor/ckeditor5/issues/11356).
1775
+ priority: priorities.highest + 1
1776
+ });
1777
+ }
1778
+ /**
1779
+ * Registers default element handlers.
1780
+ */ _registerElementHandlers() {
1781
+ this.on('register', (evt, definition)=>{
1782
+ const schema = this.editor.model.schema;
1783
+ // Object element should be only registered for new features.
1784
+ // If the model schema is already registered, it should be handled by
1785
+ // #_registerBlockElement() or #_registerObjectElement() attribute handlers.
1786
+ if (definition.isObject && !schema.isRegistered(definition.model)) {
1787
+ this._registerObjectElement(definition);
1788
+ } else if (definition.isBlock) {
1789
+ this._registerBlockElement(definition);
1790
+ } else if (definition.isInline) {
1791
+ this._registerInlineElement(definition);
1792
+ } else {
1793
+ /**
1794
+ * The definition cannot be handled by the data filter.
1795
+ *
1796
+ * Make sure that the registered definition is correct.
1797
+ *
1798
+ * @error data-filter-invalid-definition
1799
+ */ throw new CKEditorError('data-filter-invalid-definition', null, definition);
1800
+ }
1801
+ evt.stop();
1802
+ }, {
1803
+ priority: 'lowest'
1804
+ });
1805
+ }
1806
+ /**
1807
+ * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
1808
+ * are removed if a coupled feature attribute is removed.
1809
+ *
1810
+ * For example, consider following HTML:
1811
+ *
1812
+ * ```html
1813
+ * <a href="foo.html" id="myId">bar</a>
1814
+ * ```
1815
+ *
1816
+ * Which would be upcasted to following text node in the model:
1817
+ *
1818
+ * ```html
1819
+ * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
1820
+ * ```
1821
+ *
1822
+ * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
1823
+ *
1824
+ * ```html
1825
+ * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
1826
+ * ```
1827
+ *
1828
+ * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
1829
+ * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
1830
+ */ _registerCoupledAttributesPostFixer() {
1831
+ const model = this.editor.model;
1832
+ const selection = model.document.selection;
1833
+ model.document.registerPostFixer((writer)=>{
1834
+ const changes = model.document.differ.getChanges();
1835
+ let changed = false;
1836
+ const coupledAttributes = this._getCoupledAttributesMap();
1837
+ for (const change of changes){
1838
+ // Handle only attribute removals.
1839
+ if (change.type != 'attribute' || change.attributeNewValue !== null) {
1840
+ continue;
1841
+ }
1842
+ // Find a list of coupled GHS attributes.
1843
+ const attributeKeys = coupledAttributes.get(change.attributeKey);
1844
+ if (!attributeKeys) {
1845
+ continue;
1846
+ }
1847
+ // Remove the coupled GHS attributes on the same range as the feature attribute was removed.
1848
+ for (const { item } of change.range.getWalker()){
1849
+ for (const attributeKey of attributeKeys){
1850
+ if (item.hasAttribute(attributeKey)) {
1851
+ writer.removeAttribute(attributeKey, item);
1852
+ changed = true;
1853
+ }
1854
+ }
1855
+ }
1856
+ }
1857
+ return changed;
1858
+ });
1859
+ this.listenTo(selection, 'change:attribute', (evt, { attributeKeys })=>{
1860
+ const removeAttributes = new Set();
1861
+ const coupledAttributes = this._getCoupledAttributesMap();
1862
+ for (const attributeKey of attributeKeys){
1863
+ // Handle only attribute removals.
1864
+ if (selection.hasAttribute(attributeKey)) {
1865
+ continue;
1866
+ }
1867
+ // Find a list of coupled GHS attributes.
1868
+ const coupledAttributeKeys = coupledAttributes.get(attributeKey);
1869
+ if (!coupledAttributeKeys) {
1870
+ continue;
1871
+ }
1872
+ for (const coupledAttributeKey of coupledAttributeKeys){
1873
+ if (selection.hasAttribute(coupledAttributeKey)) {
1874
+ removeAttributes.add(coupledAttributeKey);
1875
+ }
1876
+ }
1877
+ }
1878
+ if (removeAttributes.size == 0) {
1879
+ return;
1880
+ }
1881
+ model.change((writer)=>{
1882
+ for (const attributeKey of removeAttributes){
1883
+ writer.removeSelectionAttribute(attributeKey);
1884
+ }
1885
+ });
1886
+ });
1887
+ }
1888
+ /**
1889
+ * Removes `html*Attributes` attributes from incompatible elements.
1890
+ *
1891
+ * For example, consider the following HTML:
1892
+ *
1893
+ * ```html
1894
+ * <heading2 htmlH2Attributes="...">foobar[]</heading2>
1895
+ * ```
1896
+ *
1897
+ * Pressing `enter` creates a new `paragraph` element that inherits
1898
+ * the `htmlH2Attributes` attribute from `heading2`.
1899
+ *
1900
+ * ```html
1901
+ * <heading2 htmlH2Attributes="...">foobar</heading2>
1902
+ * <paragraph htmlH2Attributes="...">[]</paragraph>
1903
+ * ```
1904
+ *
1905
+ * This postfixer ensures that this doesn't happen, and that elements can
1906
+ * only have `html*Attributes` associated with them,
1907
+ * e.g.: `htmlPAttributes` for `<p>`, `htmlDivAttributes` for `<div>`, etc.
1908
+ *
1909
+ * With it enabled, pressing `enter` at the end of `<heading2>` will create
1910
+ * a new paragraph without the `htmlH2Attributes` attribute.
1911
+ *
1912
+ * ```html
1913
+ * <heading2 htmlH2Attributes="...">foobar</heading2>
1914
+ * <paragraph>[]</paragraph>
1915
+ * ```
1916
+ */ _registerAssociatedHtmlAttributesPostFixer() {
1917
+ const model = this.editor.model;
1918
+ model.document.registerPostFixer((writer)=>{
1919
+ const changes = model.document.differ.getChanges();
1920
+ let changed = false;
1921
+ for (const change of changes){
1922
+ if (change.type !== 'insert' || change.name === '$text') {
1923
+ continue;
1924
+ }
1925
+ for (const attr of change.attributes.keys()){
1926
+ if (!attr.startsWith('html') || !attr.endsWith('Attributes')) {
1927
+ continue;
1928
+ }
1929
+ if (!model.schema.checkAttribute(change.name, attr)) {
1930
+ writer.removeAttribute(attr, change.position.nodeAfter);
1931
+ changed = true;
1932
+ }
1933
+ }
1934
+ }
1935
+ return changed;
1936
+ });
1937
+ }
1938
+ /**
1939
+ * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
1940
+ * and coupled GHS attribute names are stored in the value array.
1941
+ */ _getCoupledAttributesMap() {
1942
+ if (this._coupledAttributes) {
1943
+ return this._coupledAttributes;
1944
+ }
1945
+ this._coupledAttributes = new Map();
1946
+ for (const definition of this._allowedElements){
1947
+ if (definition.coupledAttribute && definition.model) {
1948
+ const attributeNames = this._coupledAttributes.get(definition.coupledAttribute);
1949
+ if (attributeNames) {
1950
+ attributeNames.push(definition.model);
1951
+ } else {
1952
+ this._coupledAttributes.set(definition.coupledAttribute, [
1953
+ definition.model
1954
+ ]);
1955
+ }
1956
+ }
1957
+ }
1958
+ return this._coupledAttributes;
1959
+ }
1960
+ /**
1961
+ * Fires `register` event for the given element definition.
1962
+ */ _fireRegisterEvent(definition) {
1963
+ if (definition.view && this._disallowedElements.has(definition.view)) {
1964
+ return;
1965
+ }
1966
+ this.fire(definition.view ? `register:${definition.view}` : 'register', definition);
1967
+ }
1968
+ /**
1969
+ * Registers object element and attribute converters for the given data schema definition.
1970
+ */ _registerObjectElement(definition) {
1971
+ const editor = this.editor;
1972
+ const schema = editor.model.schema;
1973
+ const conversion = editor.conversion;
1974
+ const { view: viewName, model: modelName } = definition;
1975
+ schema.register(modelName, definition.modelSchema);
1976
+ /* istanbul ignore next: paranoid check -- @preserve */ if (!viewName) {
1977
+ return;
1978
+ }
1979
+ schema.extend(definition.model, {
1980
+ allowAttributes: [
1981
+ getHtmlAttributeName(viewName),
1982
+ 'htmlContent'
1983
+ ]
1984
+ });
1985
+ // Store element content in special `$rawContent` custom property to
1986
+ // avoid editor's data filtering mechanism.
1987
+ editor.data.registerRawContentMatcher({
1988
+ name: viewName
1989
+ });
1990
+ conversion.for('upcast').elementToElement({
1991
+ view: viewName,
1992
+ model: viewToModelObjectConverter(definition),
1993
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
1994
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
1995
+ // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
1996
+ converterPriority: priorities.low + 2
1997
+ });
1998
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
1999
+ conversion.for('editingDowncast').elementToStructure({
2000
+ model: {
2001
+ name: modelName,
2002
+ attributes: [
2003
+ getHtmlAttributeName(viewName)
2004
+ ]
2005
+ },
2006
+ view: toObjectWidgetConverter(editor, definition)
2007
+ });
2008
+ conversion.for('dataDowncast').elementToElement({
2009
+ model: modelName,
2010
+ view: (modelElement, { writer })=>{
2011
+ return createObjectView(viewName, modelElement, writer);
2012
+ }
2013
+ });
2014
+ conversion.for('dataDowncast').add(modelToViewBlockAttributeConverter(definition));
2015
+ }
2016
+ /**
2017
+ * Registers block element and attribute converters for the given data schema definition.
2018
+ */ _registerBlockElement(definition) {
2019
+ const editor = this.editor;
2020
+ const schema = editor.model.schema;
2021
+ const conversion = editor.conversion;
2022
+ const { view: viewName, model: modelName } = definition;
2023
+ if (!schema.isRegistered(definition.model)) {
2024
+ schema.register(definition.model, definition.modelSchema);
2025
+ if (!viewName) {
2026
+ return;
2027
+ }
2028
+ conversion.for('upcast').elementToElement({
2029
+ model: modelName,
2030
+ view: viewName,
2031
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
2032
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
2033
+ // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
2034
+ converterPriority: priorities.low + 2
2035
+ });
2036
+ conversion.for('downcast').elementToElement({
2037
+ model: modelName,
2038
+ view: viewName
2039
+ });
2040
+ }
2041
+ if (!viewName) {
2042
+ return;
2043
+ }
2044
+ schema.extend(definition.model, {
2045
+ allowAttributes: getHtmlAttributeName(viewName)
2046
+ });
2047
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
2048
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
2049
+ }
2050
+ /**
2051
+ * Registers inline element and attribute converters for the given data schema definition.
2052
+ *
2053
+ * Extends `$text` model schema to allow the given definition model attribute and its properties.
2054
+ */ _registerInlineElement(definition) {
2055
+ const editor = this.editor;
2056
+ const schema = editor.model.schema;
2057
+ const conversion = editor.conversion;
2058
+ const attributeKey = definition.model;
2059
+ // This element is stored in the model as an attribute on a block element, for example DocumentLists.
2060
+ if (definition.appliesToBlock) {
2061
+ return;
2062
+ }
2063
+ schema.extend('$text', {
2064
+ allowAttributes: attributeKey
2065
+ });
2066
+ if (definition.attributeProperties) {
2067
+ schema.setAttributeProperties(attributeKey, definition.attributeProperties);
2068
+ }
2069
+ conversion.for('upcast').add(viewToAttributeInlineConverter(definition, this));
2070
+ conversion.for('downcast').attributeToElement({
2071
+ model: attributeKey,
2072
+ view: attributeToViewInlineConverter(definition)
2073
+ });
2074
+ if (!definition.allowEmpty) {
2075
+ return;
2076
+ }
2077
+ schema.setAttributeProperties(attributeKey, {
2078
+ copyFromObject: false
2079
+ });
2080
+ if (!schema.isRegistered('htmlEmptyElement')) {
2081
+ schema.register('htmlEmptyElement', {
2082
+ inheritAllFrom: '$inlineObject'
2083
+ });
2084
+ }
2085
+ editor.data.htmlProcessor.domConverter.registerInlineObjectMatcher((element)=>{
2086
+ // Element must be empty and have any attribute.
2087
+ if (element.name == definition.view && element.isEmpty && Array.from(element.getAttributeKeys()).length) {
2088
+ return {
2089
+ name: true
2090
+ };
2091
+ }
2092
+ return null;
2093
+ });
2094
+ conversion.for('editingDowncast').elementToElement({
2095
+ model: 'htmlEmptyElement',
2096
+ view: emptyInlineModelElementToViewConverter(definition, true)
2097
+ });
2098
+ conversion.for('dataDowncast').elementToElement({
2099
+ model: 'htmlEmptyElement',
2100
+ view: emptyInlineModelElementToViewConverter(definition)
2101
+ });
2102
+ }
2103
+ }
2104
+ /**
2105
+ * Matches and consumes matched attributes.
2106
+ *
2107
+ * @returns Object with following properties:
2108
+ * - attributes Array with matched attribute names.
2109
+ * - classes Array with matched class names.
2110
+ * - styles Array with matched style names.
2111
+ */ function matchAndConsumeAttributes(viewElement, matcher, consumable) {
2112
+ const matches = matcher.matchAll(viewElement) || [];
2113
+ const stylesProcessor = viewElement.document.stylesProcessor;
2114
+ return matches.reduce((result, { match })=>{
2115
+ // Verify and consume styles.
2116
+ for (const style of match.styles || []){
2117
+ // Check longer forms of the same style as those could be matched
2118
+ // but not present in the element directly.
2119
+ // Consider only longhand (or longer than current notation) so that
2120
+ // we do not include all sides of the box if only one side is allowed.
2121
+ const sortedRelatedStyles = stylesProcessor.getRelatedStyles(style).filter((relatedStyle)=>relatedStyle.split('-').length > style.split('-').length).sort((a, b)=>b.split('-').length - a.split('-').length);
2122
+ for (const relatedStyle of sortedRelatedStyles){
2123
+ if (consumable.consume(viewElement, {
2124
+ styles: [
2125
+ relatedStyle
2126
+ ]
2127
+ })) {
2128
+ result.styles.push(relatedStyle);
2129
+ }
2130
+ }
2131
+ // Verify and consume style as specified in the matcher.
2132
+ if (consumable.consume(viewElement, {
2133
+ styles: [
2134
+ style
2135
+ ]
2136
+ })) {
2137
+ result.styles.push(style);
2138
+ }
2139
+ }
2140
+ // Verify and consume class names.
2141
+ for (const className of match.classes || []){
2142
+ if (consumable.consume(viewElement, {
2143
+ classes: [
2144
+ className
2145
+ ]
2146
+ })) {
2147
+ result.classes.push(className);
2148
+ }
2149
+ }
2150
+ // Verify and consume other attributes.
2151
+ for (const attributeName of match.attributes || []){
2152
+ if (consumable.consume(viewElement, {
2153
+ attributes: [
2154
+ attributeName
2155
+ ]
2156
+ })) {
2157
+ result.attributes.push(attributeName);
2158
+ }
2159
+ }
2160
+ return result;
2161
+ }, {
2162
+ attributes: [],
2163
+ classes: [],
2164
+ styles: []
2165
+ });
2166
+ }
2167
+ /**
2168
+ * Prepares the GHS attribute value as an object with element attributes' values.
2169
+ */ function prepareGHSAttribute(viewElement, { attributes, classes, styles }) {
2170
+ if (!attributes.length && !classes.length && !styles.length) {
2171
+ return null;
2172
+ }
2173
+ return {
2174
+ ...attributes.length && {
2175
+ attributes: getAttributes(viewElement, attributes)
2176
+ },
2177
+ ...styles.length && {
2178
+ styles: getReducedStyles(viewElement, styles)
2179
+ },
2180
+ ...classes.length && {
2181
+ classes
2182
+ }
2183
+ };
2184
+ }
2185
+ /**
2186
+ * Returns attributes as an object with names and values.
2187
+ */ function getAttributes(viewElement, attributes) {
2188
+ const attributesObject = {};
2189
+ for (const key of attributes){
2190
+ const value = viewElement.getAttribute(key);
2191
+ if (value !== undefined && isValidAttributeName(key)) {
2192
+ attributesObject[key] = value;
2193
+ }
2194
+ }
2195
+ return attributesObject;
2196
+ }
2197
+ /**
2198
+ * Returns styles as an object reduced to shorthand notation without redundant entries.
2199
+ */ function getReducedStyles(viewElement, styles) {
2200
+ // Use StyleMap to reduce style value to the minimal form (without shorthand and long-hand notation and duplication).
2201
+ const stylesMap = new StylesMap(viewElement.document.stylesProcessor);
2202
+ for (const key of styles){
2203
+ const styleValue = viewElement.getStyle(key);
2204
+ if (styleValue !== undefined) {
2205
+ stylesMap.set(key, styleValue);
2206
+ }
2207
+ }
2208
+ return Object.fromEntries(stylesMap.getStylesEntries());
2209
+ }
2210
+ /**
2211
+ * Matcher by default has to match **all** patterns to count it as an actual match. Splitting the pattern
2212
+ * into separate patterns means that any matched pattern will be count as a match.
2213
+ *
2214
+ * @param pattern Pattern to split.
2215
+ * @param attributeName Name of the attribute to split (e.g. 'attributes', 'classes', 'styles').
2216
+ */ function splitPattern(pattern, attributeName) {
2217
+ const { name } = pattern;
2218
+ const attributeValue = pattern[attributeName];
2219
+ if (isPlainObject(attributeValue)) {
2220
+ return Object.entries(attributeValue).map(([key, value])=>({
2221
+ name,
2222
+ [attributeName]: {
2223
+ [key]: value
2224
+ }
2225
+ }));
2226
+ }
2227
+ if (Array.isArray(attributeValue)) {
2228
+ return attributeValue.map((value)=>({
2229
+ name,
2230
+ [attributeName]: [
2231
+ value
2232
+ ]
2233
+ }));
2234
+ }
2235
+ return [
2236
+ pattern
2237
+ ];
2238
+ }
2239
+ /**
2240
+ * Rules are matched in conjunction (AND operation), but we want to have a match if *any* of the rules is matched (OR operation).
2241
+ * By splitting the rules we force the latter effect.
2242
+ */ function splitRules(rules) {
2243
+ const { name, attributes, classes, styles } = rules;
2244
+ const splitRules = [];
2245
+ if (attributes) {
2246
+ splitRules.push(...splitPattern({
2247
+ name,
2248
+ attributes
2249
+ }, 'attributes'));
2250
+ }
2251
+ if (classes) {
2252
+ splitRules.push(...splitPattern({
2253
+ name,
2254
+ classes
2255
+ }, 'classes'));
2256
+ }
2257
+ if (styles) {
2258
+ splitRules.push(...splitPattern({
2259
+ name,
2260
+ styles
2261
+ }, 'styles'));
2262
+ }
2263
+ return splitRules;
2264
+ }
2265
+
2266
+ /**
2267
+ * Provides the General HTML Support integration with {@link module:code-block/codeblock~CodeBlock Code Block} feature.
2268
+ */ class CodeBlockElementSupport extends Plugin {
2269
+ /**
2270
+ * @inheritDoc
2271
+ */ static get requires() {
2272
+ return [
2273
+ DataFilter
2274
+ ];
2275
+ }
2276
+ /**
2277
+ * @inheritDoc
2278
+ */ static get pluginName() {
2279
+ return 'CodeBlockElementSupport';
2280
+ }
2281
+ /**
2282
+ * @inheritDoc
2283
+ */ static get isOfficialPlugin() {
2284
+ return true;
2285
+ }
2286
+ /**
2287
+ * @inheritDoc
2288
+ */ init() {
2289
+ if (!this.editor.plugins.has('CodeBlockEditing')) {
2290
+ return;
2291
+ }
2292
+ const dataFilter = this.editor.plugins.get(DataFilter);
2293
+ dataFilter.on('register:pre', (evt, definition)=>{
2294
+ if (definition.model !== 'codeBlock') {
2295
+ return;
2296
+ }
2297
+ const editor = this.editor;
2298
+ const schema = editor.model.schema;
2299
+ const conversion = editor.conversion;
2300
+ // Extend codeBlock to allow attributes required by attribute filtration.
2301
+ schema.extend('codeBlock', {
2302
+ allowAttributes: [
2303
+ 'htmlPreAttributes',
2304
+ 'htmlContentAttributes'
2305
+ ]
2306
+ });
2307
+ conversion.for('upcast').add(viewToModelCodeBlockAttributeConverter(dataFilter));
2308
+ conversion.for('downcast').add(modelToViewCodeBlockAttributeConverter());
2309
+ evt.stop();
2310
+ });
2311
+ }
2312
+ }
2313
+ /**
2314
+ * View-to-model conversion helper preserving allowed attributes on {@link module:code-block/codeblock~CodeBlock Code Block}
2315
+ * feature model element.
2316
+ *
2317
+ * Attributes are preserved as a value of `html*Attributes` model attribute.
2318
+ * @param dataFilter
2319
+ * @returns Returns a conversion callback.
2320
+ */ function viewToModelCodeBlockAttributeConverter(dataFilter) {
2321
+ return (dispatcher)=>{
2322
+ dispatcher.on('element:code', (evt, data, conversionApi)=>{
2323
+ const viewCodeElement = data.viewItem;
2324
+ const viewPreElement = viewCodeElement.parent;
2325
+ if (!viewPreElement || !viewPreElement.is('element', 'pre')) {
2326
+ return;
2327
+ }
2328
+ preserveElementAttributes(viewPreElement, 'htmlPreAttributes');
2329
+ preserveElementAttributes(viewCodeElement, 'htmlContentAttributes');
2330
+ function preserveElementAttributes(viewElement, attributeName) {
2331
+ const viewAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
2332
+ if (viewAttributes) {
2333
+ conversionApi.writer.setAttribute(attributeName, viewAttributes, data.modelRange);
2334
+ }
2335
+ }
2336
+ }, {
2337
+ priority: 'low'
2338
+ });
2339
+ };
2340
+ }
2341
+ /**
2342
+ * Model-to-view conversion helper applying attributes from {@link module:code-block/codeblock~CodeBlock Code Block}
2343
+ * feature model element.
2344
+ * @returns Returns a conversion callback.
2345
+ */ function modelToViewCodeBlockAttributeConverter() {
2346
+ return (dispatcher)=>{
2347
+ dispatcher.on('attribute:htmlPreAttributes:codeBlock', (evt, data, conversionApi)=>{
2348
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
2349
+ return;
2350
+ }
2351
+ const { attributeOldValue, attributeNewValue } = data;
2352
+ const viewCodeElement = conversionApi.mapper.toViewElement(data.item);
2353
+ const viewPreElement = viewCodeElement.parent;
2354
+ updateViewAttributes(conversionApi.writer, attributeOldValue, attributeNewValue, viewPreElement);
2355
+ });
2356
+ dispatcher.on('attribute:htmlContentAttributes:codeBlock', (evt, data, conversionApi)=>{
2357
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
2358
+ return;
2359
+ }
2360
+ const { attributeOldValue, attributeNewValue } = data;
2361
+ const viewCodeElement = conversionApi.mapper.toViewElement(data.item);
2362
+ updateViewAttributes(conversionApi.writer, attributeOldValue, attributeNewValue, viewCodeElement);
2363
+ });
2364
+ };
2365
+ }
2366
+
2367
+ /**
2368
+ * Provides the General HTML Support integration for elements which can behave like sectioning element (e.g. article) or
2369
+ * element accepting only inline content (e.g. paragraph).
2370
+ *
2371
+ * The distinction between this two content models is important for choosing correct schema model and proper content conversion.
2372
+ * As an example, it ensures that:
2373
+ *
2374
+ * * children elements paragraphing is enabled for sectioning elements only,
2375
+ * * element and its content can be correctly handled by editing view (splitting and merging elements),
2376
+ * * model element HTML is semantically correct and easier to work with.
2377
+ *
2378
+ * If element contains any block element, it will be treated as a sectioning element and registered using
2379
+ * {@link module:html-support/dataschema~DataSchemaDefinition#model} and
2380
+ * {@link module:html-support/dataschema~DataSchemaDefinition#modelSchema} in editor schema.
2381
+ * Otherwise, it will be registered under {@link module:html-support/dataschema~DataSchemaBlockElementDefinition#paragraphLikeModel} model
2382
+ * name with model schema accepting only inline content (inheriting from `$block`).
2383
+ */ class DualContentModelElementSupport extends Plugin {
2384
+ /**
2385
+ * @inheritDoc
2386
+ */ static get requires() {
2387
+ return [
2388
+ DataFilter
2389
+ ];
2390
+ }
2391
+ /**
2392
+ * @inheritDoc
2393
+ */ static get pluginName() {
2394
+ return 'DualContentModelElementSupport';
2395
+ }
2396
+ /**
2397
+ * @inheritDoc
2398
+ */ static get isOfficialPlugin() {
2399
+ return true;
2400
+ }
2401
+ /**
2402
+ * @inheritDoc
2403
+ */ init() {
2404
+ const dataFilter = this.editor.plugins.get(DataFilter);
2405
+ dataFilter.on('register', (evt, definition)=>{
2406
+ const blockDefinition = definition;
2407
+ const editor = this.editor;
2408
+ const schema = editor.model.schema;
2409
+ const conversion = editor.conversion;
2410
+ if (!blockDefinition.paragraphLikeModel) {
2411
+ return;
2412
+ }
2413
+ // Can only apply to newly registered features.
2414
+ if (schema.isRegistered(blockDefinition.model) || schema.isRegistered(blockDefinition.paragraphLikeModel)) {
2415
+ return;
2416
+ }
2417
+ const paragraphLikeModelDefinition = {
2418
+ model: blockDefinition.paragraphLikeModel,
2419
+ view: blockDefinition.view
2420
+ };
2421
+ schema.register(blockDefinition.model, blockDefinition.modelSchema);
2422
+ schema.register(paragraphLikeModelDefinition.model, {
2423
+ inheritAllFrom: '$block'
2424
+ });
2425
+ conversion.for('upcast').elementToElement({
2426
+ view: blockDefinition.view,
2427
+ model: (viewElement, { writer })=>{
2428
+ if (this._hasBlockContent(viewElement)) {
2429
+ return writer.createElement(blockDefinition.model);
2430
+ }
2431
+ return writer.createElement(paragraphLikeModelDefinition.model);
2432
+ },
2433
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
2434
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
2435
+ converterPriority: priorities.low + 0.5
2436
+ });
2437
+ conversion.for('downcast').elementToElement({
2438
+ view: blockDefinition.view,
2439
+ model: blockDefinition.model
2440
+ });
2441
+ this._addAttributeConversion(blockDefinition);
2442
+ conversion.for('downcast').elementToElement({
2443
+ view: paragraphLikeModelDefinition.view,
2444
+ model: paragraphLikeModelDefinition.model
2445
+ });
2446
+ this._addAttributeConversion(paragraphLikeModelDefinition);
2447
+ evt.stop();
2448
+ });
2449
+ }
2450
+ /**
2451
+ * Checks whether the given view element includes any other block element.
2452
+ */ _hasBlockContent(viewElement) {
2453
+ const view = this.editor.editing.view;
2454
+ const blockElements = view.domConverter.blockElements;
2455
+ // Traversing the viewElement subtree looking for block elements.
2456
+ // Especially for the cases like <div><a href="#"><p>foo</p></a></div>.
2457
+ // https://github.com/ckeditor/ckeditor5/issues/11513
2458
+ for (const viewItem of view.createRangeIn(viewElement).getItems()){
2459
+ if (viewItem.is('element') && blockElements.includes(viewItem.name)) {
2460
+ return true;
2461
+ }
2462
+ }
2463
+ return false;
2464
+ }
2465
+ /**
2466
+ * Adds attribute filtering conversion for the given data schema.
2467
+ */ _addAttributeConversion(definition) {
2468
+ const editor = this.editor;
2469
+ const conversion = editor.conversion;
2470
+ const dataFilter = editor.plugins.get(DataFilter);
2471
+ editor.model.schema.extend(definition.model, {
2472
+ allowAttributes: getHtmlAttributeName(definition.view)
2473
+ });
2474
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, dataFilter));
2475
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
2476
+ }
2477
+ }
2478
+
2479
+ /**
2480
+ * Provides the General HTML Support integration with {@link module:heading/heading~Heading Heading} feature.
2481
+ */ class HeadingElementSupport extends Plugin {
2482
+ /**
2483
+ * @inheritDoc
2484
+ */ static get requires() {
2485
+ return [
2486
+ DataSchema,
2487
+ Enter
2488
+ ];
2489
+ }
2490
+ /**
2491
+ * @inheritDoc
2492
+ */ static get pluginName() {
2493
+ return 'HeadingElementSupport';
2494
+ }
2495
+ /**
2496
+ * @inheritDoc
2497
+ */ static get isOfficialPlugin() {
2498
+ return true;
2499
+ }
2500
+ /**
2501
+ * @inheritDoc
2502
+ */ init() {
2503
+ const editor = this.editor;
2504
+ if (!editor.plugins.has('HeadingEditing')) {
2505
+ return;
2506
+ }
2507
+ const options = editor.config.get('heading.options');
2508
+ this.registerHeadingElements(editor, options);
2509
+ }
2510
+ /**
2511
+ * Registers all elements supported by HeadingEditing to enable custom attributes for those elements.
2512
+ */ registerHeadingElements(editor, options) {
2513
+ const dataSchema = editor.plugins.get(DataSchema);
2514
+ const headerModels = [];
2515
+ for (const option of options){
2516
+ if ('model' in option && 'view' in option) {
2517
+ dataSchema.registerBlockElement({
2518
+ view: option.view,
2519
+ model: option.model
2520
+ });
2521
+ headerModels.push(option.model);
2522
+ }
2523
+ }
2524
+ dataSchema.extendBlockElement({
2525
+ model: 'htmlHgroup',
2526
+ modelSchema: {
2527
+ allowChildren: headerModels
2528
+ }
2529
+ });
2530
+ dataSchema.extendBlockElement({
2531
+ model: 'htmlSummary',
2532
+ modelSchema: {
2533
+ allowChildren: headerModels
2534
+ }
2535
+ });
2536
+ }
2537
+ }
2538
+
2539
+ /**
2540
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
2541
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
2542
+ */ /**
2543
+ * @module html-support/integrations/integrationutils
2544
+ */ /**
2545
+ * Returns the first view element descendant matching the given view name.
2546
+ * Includes view element itself.
2547
+ *
2548
+ * @internal
2549
+ */ function getDescendantElement(writer, containerElement, elementName) {
2550
+ const range = writer.createRangeOn(containerElement);
2551
+ for (const { item } of range.getWalker()){
2552
+ if (item.is('element', elementName)) {
2553
+ return item;
2554
+ }
2555
+ }
2556
+ }
2557
+
2558
+ /**
2559
+ * Provides the General HTML Support integration with the {@link module:image/image~Image Image} feature.
2560
+ */ class ImageElementSupport extends Plugin {
2561
+ /**
2562
+ * @inheritDoc
2563
+ */ static get requires() {
2564
+ return [
2565
+ DataFilter
2566
+ ];
2567
+ }
2568
+ /**
2569
+ * @inheritDoc
2570
+ */ static get pluginName() {
2571
+ return 'ImageElementSupport';
2572
+ }
2573
+ /**
2574
+ * @inheritDoc
2575
+ */ static get isOfficialPlugin() {
2576
+ return true;
2577
+ }
2578
+ /**
2579
+ * @inheritDoc
2580
+ */ init() {
2581
+ const editor = this.editor;
2582
+ // At least one image plugin should be loaded for the integration to work properly.
2583
+ if (!editor.plugins.has('ImageInlineEditing') && !editor.plugins.has('ImageBlockEditing')) {
2584
+ return;
2585
+ }
2586
+ const schema = editor.model.schema;
2587
+ const conversion = editor.conversion;
2588
+ const dataFilter = editor.plugins.get(DataFilter);
2589
+ dataFilter.on('register:figure', ()=>{
2590
+ conversion.for('upcast').add(viewToModelFigureAttributeConverter$1(dataFilter));
2591
+ });
2592
+ dataFilter.on('register:img', (evt, definition)=>{
2593
+ if (definition.model !== 'imageBlock' && definition.model !== 'imageInline') {
2594
+ return;
2595
+ }
2596
+ if (schema.isRegistered('imageBlock')) {
2597
+ schema.extend('imageBlock', {
2598
+ allowAttributes: [
2599
+ 'htmlImgAttributes',
2600
+ // Figure and Link don't have model counterpart.
2601
+ // We will preserve attributes on image model element using these attribute keys.
2602
+ 'htmlFigureAttributes',
2603
+ 'htmlLinkAttributes'
2604
+ ]
2605
+ });
2606
+ }
2607
+ if (schema.isRegistered('imageInline')) {
2608
+ schema.extend('imageInline', {
2609
+ allowAttributes: [
2610
+ // `htmlA` is needed for standard GHS link integration.
2611
+ 'htmlA',
2612
+ 'htmlImgAttributes'
2613
+ ]
2614
+ });
2615
+ }
2616
+ conversion.for('upcast').add(viewToModelImageAttributeConverter(dataFilter));
2617
+ conversion.for('downcast').add(modelToViewImageAttributeConverter());
2618
+ if (editor.plugins.has('LinkImage')) {
2619
+ conversion.for('upcast').add(viewToModelLinkImageAttributeConverter(dataFilter, editor));
2620
+ }
2621
+ evt.stop();
2622
+ });
2623
+ }
2624
+ }
2625
+ /**
2626
+ * View-to-model conversion helper preserving allowed attributes on the {@link module:image/image~Image Image}
2627
+ * feature model element.
2628
+ *
2629
+ * @returns Returns a conversion callback.
2630
+ */ function viewToModelImageAttributeConverter(dataFilter) {
2631
+ return (dispatcher)=>{
2632
+ dispatcher.on('element:img', (evt, data, conversionApi)=>{
2633
+ if (!data.modelRange) {
2634
+ return;
2635
+ }
2636
+ const viewImageElement = data.viewItem;
2637
+ const viewAttributes = dataFilter.processViewAttributes(viewImageElement, conversionApi);
2638
+ if (viewAttributes) {
2639
+ conversionApi.writer.setAttribute('htmlImgAttributes', viewAttributes, data.modelRange);
2640
+ }
2641
+ }, {
2642
+ priority: 'low'
2643
+ });
2644
+ };
2645
+ }
2646
+ /**
2647
+ * View-to-model conversion helper preserving allowed attributes on {@link module:image/image~Image Image}
2648
+ * feature model element from link view element.
2649
+ *
2650
+ * @returns Returns a conversion callback.
2651
+ */ function viewToModelLinkImageAttributeConverter(dataFilter, editor) {
2652
+ const imageUtils = editor.plugins.get('ImageUtils');
2653
+ return (dispatcher)=>{
2654
+ dispatcher.on('element:a', (evt, data, conversionApi)=>{
2655
+ const viewLink = data.viewItem;
2656
+ const viewImage = imageUtils.findViewImgElement(viewLink);
2657
+ if (!viewImage) {
2658
+ return;
2659
+ }
2660
+ const modelImage = data.modelCursor.parent;
2661
+ if (!modelImage.is('element', 'imageBlock')) {
2662
+ return;
2663
+ }
2664
+ const viewAttributes = dataFilter.processViewAttributes(viewLink, conversionApi);
2665
+ if (viewAttributes) {
2666
+ conversionApi.writer.setAttribute('htmlLinkAttributes', viewAttributes, modelImage);
2667
+ }
2668
+ }, {
2669
+ priority: 'low'
2670
+ });
2671
+ };
2672
+ }
2673
+ /**
2674
+ * View-to-model conversion helper preserving allowed attributes on {@link module:image/image~Image Image}
2675
+ * feature model element from figure view element.
2676
+ *
2677
+ * @returns Returns a conversion callback.
2678
+ */ function viewToModelFigureAttributeConverter$1(dataFilter) {
2679
+ return (dispatcher)=>{
2680
+ dispatcher.on('element:figure', (evt, data, conversionApi)=>{
2681
+ const viewFigureElement = data.viewItem;
2682
+ if (!data.modelRange || !viewFigureElement.hasClass('image')) {
2683
+ return;
2684
+ }
2685
+ const viewAttributes = dataFilter.processViewAttributes(viewFigureElement, conversionApi);
2686
+ if (viewAttributes) {
2687
+ conversionApi.writer.setAttribute('htmlFigureAttributes', viewAttributes, data.modelRange);
2688
+ }
2689
+ }, {
2690
+ priority: 'low'
2691
+ });
2692
+ };
2693
+ }
2694
+ /**
2695
+ * A model-to-view conversion helper applying attributes from the {@link module:image/image~Image Image}
2696
+ * feature.
2697
+ * @returns Returns a conversion callback.
2698
+ */ function modelToViewImageAttributeConverter() {
2699
+ return (dispatcher)=>{
2700
+ addInlineAttributeConversion('htmlImgAttributes');
2701
+ addBlockAttributeConversion('img', 'htmlImgAttributes');
2702
+ addBlockAttributeConversion('figure', 'htmlFigureAttributes');
2703
+ addBlockAttributeConversion('a', 'htmlLinkAttributes');
2704
+ function addInlineAttributeConversion(attributeName) {
2705
+ dispatcher.on(`attribute:${attributeName}:imageInline`, (evt, data, conversionApi)=>{
2706
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
2707
+ return;
2708
+ }
2709
+ const { attributeOldValue, attributeNewValue } = data;
2710
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
2711
+ updateViewAttributes(conversionApi.writer, attributeOldValue, attributeNewValue, viewElement);
2712
+ }, {
2713
+ priority: 'low'
2714
+ });
2715
+ }
2716
+ function addBlockAttributeConversion(elementName, attributeName) {
2717
+ dispatcher.on(`attribute:${attributeName}:imageBlock`, (evt, data, conversionApi)=>{
2718
+ if (!conversionApi.consumable.test(data.item, evt.name)) {
2719
+ return;
2720
+ }
2721
+ const { attributeOldValue, attributeNewValue } = data;
2722
+ const containerElement = conversionApi.mapper.toViewElement(data.item);
2723
+ const viewElement = getDescendantElement(conversionApi.writer, containerElement, elementName);
2724
+ if (viewElement) {
2725
+ updateViewAttributes(conversionApi.writer, attributeOldValue, attributeNewValue, viewElement);
2726
+ conversionApi.consumable.consume(data.item, evt.name);
2727
+ }
2728
+ }, {
2729
+ priority: 'low'
2730
+ });
2731
+ if (elementName === 'a') {
2732
+ // To have a link element in the view, we need to attach a converter to the `linkHref` attribute as well.
2733
+ dispatcher.on('attribute:linkHref:imageBlock', (evt, data, conversionApi)=>{
2734
+ if (!conversionApi.consumable.consume(data.item, 'attribute:htmlLinkAttributes:imageBlock')) {
2735
+ return;
2736
+ }
2737
+ const containerElement = conversionApi.mapper.toViewElement(data.item);
2738
+ const viewElement = getDescendantElement(conversionApi.writer, containerElement, 'a');
2739
+ setViewAttributes(conversionApi.writer, data.item.getAttribute('htmlLinkAttributes'), viewElement);
2740
+ }, {
2741
+ priority: 'low'
2742
+ });
2743
+ }
2744
+ }
2745
+ };
2746
+ }
2747
+
2748
+ /**
2749
+ * Provides the General HTML Support integration with {@link module:media-embed/mediaembed~MediaEmbed Media Embed} feature.
2750
+ */ class MediaEmbedElementSupport extends Plugin {
2751
+ /**
2752
+ * @inheritDoc
2753
+ */ static get requires() {
2754
+ return [
2755
+ DataFilter
2756
+ ];
2757
+ }
2758
+ /**
2759
+ * @inheritDoc
2760
+ */ static get pluginName() {
2761
+ return 'MediaEmbedElementSupport';
2762
+ }
2763
+ /**
2764
+ * @inheritDoc
2765
+ */ static get isOfficialPlugin() {
2766
+ return true;
2767
+ }
2768
+ /**
2769
+ * @inheritDoc
2770
+ */ init() {
2771
+ const editor = this.editor;
2772
+ // Stop here if MediaEmbed plugin is not provided or the integrator wants to output markup with previews as
2773
+ // we do not support filtering previews.
2774
+ if (!editor.plugins.has('MediaEmbed') || editor.config.get('mediaEmbed.previewsInData')) {
2775
+ return;
2776
+ }
2777
+ const schema = editor.model.schema;
2778
+ const conversion = editor.conversion;
2779
+ const dataFilter = this.editor.plugins.get(DataFilter);
2780
+ const dataSchema = this.editor.plugins.get(DataSchema);
2781
+ const mediaElementName = editor.config.get('mediaEmbed.elementName');
2782
+ // Overwrite GHS schema definition for a given elementName.
2783
+ dataSchema.registerBlockElement({
2784
+ model: 'media',
2785
+ view: mediaElementName
2786
+ });
2787
+ dataFilter.on('register:figure', ()=>{
2788
+ conversion.for('upcast').add(viewToModelFigureAttributesConverter(dataFilter));
2789
+ });
2790
+ dataFilter.on(`register:${mediaElementName}`, (evt, definition)=>{
2791
+ if (definition.model !== 'media') {
2792
+ return;
2793
+ }
2794
+ schema.extend('media', {
2795
+ allowAttributes: [
2796
+ getHtmlAttributeName(mediaElementName),
2797
+ 'htmlFigureAttributes'
2798
+ ]
2799
+ });
2800
+ conversion.for('upcast').add(viewToModelMediaAttributesConverter(dataFilter, mediaElementName));
2801
+ conversion.for('dataDowncast').add(modelToViewMediaAttributeConverter(mediaElementName));
2802
+ evt.stop();
2803
+ });
2804
+ }
2805
+ }
2806
+ function viewToModelMediaAttributesConverter(dataFilter, mediaElementName) {
2807
+ const upcastMedia = (evt, data, conversionApi)=>{
2808
+ const viewMediaElement = data.viewItem;
2809
+ preserveElementAttributes(viewMediaElement, getHtmlAttributeName(mediaElementName));
2810
+ function preserveElementAttributes(viewElement, attributeName) {
2811
+ const viewAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
2812
+ if (viewAttributes) {
2813
+ conversionApi.writer.setAttribute(attributeName, viewAttributes, data.modelRange);
2814
+ }
2815
+ }
2816
+ };
2817
+ return (dispatcher)=>{
2818
+ dispatcher.on(`element:${mediaElementName}`, upcastMedia, {
2819
+ priority: 'low'
2820
+ });
2821
+ };
2822
+ }
2823
+ /**
2824
+ * View-to-model conversion helper preserving allowed attributes on {@link module:media-embed/mediaembed~MediaEmbed MediaEmbed}
2825
+ * feature model element from figure view element.
2826
+ *
2827
+ * @returns Returns a conversion callback.
2828
+ */ function viewToModelFigureAttributesConverter(dataFilter) {
2829
+ return (dispatcher)=>{
2830
+ dispatcher.on('element:figure', (evt, data, conversionApi)=>{
2831
+ const viewFigureElement = data.viewItem;
2832
+ if (!data.modelRange || !viewFigureElement.hasClass('media')) {
2833
+ return;
2834
+ }
2835
+ const viewAttributes = dataFilter.processViewAttributes(viewFigureElement, conversionApi);
2836
+ if (viewAttributes) {
2837
+ conversionApi.writer.setAttribute('htmlFigureAttributes', viewAttributes, data.modelRange);
2838
+ }
2839
+ }, {
2840
+ priority: 'low'
2841
+ });
2842
+ };
2843
+ }
2844
+ function modelToViewMediaAttributeConverter(mediaElementName) {
2845
+ return (dispatcher)=>{
2846
+ addAttributeConversionDispatcherHandler(mediaElementName, getHtmlAttributeName(mediaElementName));
2847
+ addAttributeConversionDispatcherHandler('figure', 'htmlFigureAttributes');
2848
+ function addAttributeConversionDispatcherHandler(elementName, attributeName) {
2849
+ dispatcher.on(`attribute:${attributeName}:media`, (evt, data, conversionApi)=>{
2850
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
2851
+ return;
2852
+ }
2853
+ const { attributeOldValue, attributeNewValue } = data;
2854
+ const containerElement = conversionApi.mapper.toViewElement(data.item);
2855
+ const viewElement = getDescendantElement(conversionApi.writer, containerElement, elementName);
2856
+ updateViewAttributes(conversionApi.writer, attributeOldValue, attributeNewValue, viewElement);
2857
+ });
2858
+ }
2859
+ };
2860
+ }
2861
+
2862
+ /**
2863
+ * Provides the General HTML Support for `script` elements.
2864
+ */ class ScriptElementSupport extends Plugin {
2865
+ /**
2866
+ * @inheritDoc
2867
+ */ static get requires() {
2868
+ return [
2869
+ DataFilter
2870
+ ];
2871
+ }
2872
+ /**
2873
+ * @inheritDoc
2874
+ */ static get pluginName() {
2875
+ return 'ScriptElementSupport';
2876
+ }
2877
+ /**
2878
+ * @inheritDoc
2879
+ */ static get isOfficialPlugin() {
2880
+ return true;
2881
+ }
2882
+ /**
2883
+ * @inheritDoc
2884
+ */ init() {
2885
+ const dataFilter = this.editor.plugins.get(DataFilter);
2886
+ dataFilter.on('register:script', (evt, definition)=>{
2887
+ const editor = this.editor;
2888
+ const schema = editor.model.schema;
2889
+ const conversion = editor.conversion;
2890
+ schema.register('htmlScript', definition.modelSchema);
2891
+ schema.extend('htmlScript', {
2892
+ allowAttributes: [
2893
+ 'htmlScriptAttributes',
2894
+ 'htmlContent'
2895
+ ],
2896
+ isContent: true
2897
+ });
2898
+ editor.data.registerRawContentMatcher({
2899
+ name: 'script'
2900
+ });
2901
+ conversion.for('upcast').elementToElement({
2902
+ view: 'script',
2903
+ model: viewToModelObjectConverter(definition)
2904
+ });
2905
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, dataFilter));
2906
+ conversion.for('downcast').elementToElement({
2907
+ model: 'htmlScript',
2908
+ view: (modelElement, { writer })=>{
2909
+ return createObjectView('script', modelElement, writer);
2910
+ }
2911
+ });
2912
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
2913
+ evt.stop();
2914
+ });
2915
+ }
2916
+ }
2917
+
2918
+ /**
2919
+ * Provides the General HTML Support integration with {@link module:table/table~Table Table} feature.
2920
+ */ class TableElementSupport extends Plugin {
2921
+ /**
2922
+ * @inheritDoc
2923
+ */ static get requires() {
2924
+ return [
2925
+ DataFilter
2926
+ ];
2927
+ }
2928
+ /**
2929
+ * @inheritDoc
2930
+ */ static get pluginName() {
2931
+ return 'TableElementSupport';
2932
+ }
2933
+ /**
2934
+ * @inheritDoc
2935
+ */ static get isOfficialPlugin() {
2936
+ return true;
2937
+ }
2938
+ /**
2939
+ * @inheritDoc
2940
+ */ init() {
2941
+ const editor = this.editor;
2942
+ if (!editor.plugins.has('TableEditing')) {
2943
+ return;
2944
+ }
2945
+ const schema = editor.model.schema;
2946
+ const conversion = editor.conversion;
2947
+ const dataFilter = editor.plugins.get(DataFilter);
2948
+ const tableUtils = editor.plugins.get('TableUtils');
2949
+ dataFilter.on('register:figure', ()=>{
2950
+ conversion.for('upcast').add(viewToModelFigureAttributeConverter(dataFilter));
2951
+ });
2952
+ dataFilter.on('register:table', (evt, definition)=>{
2953
+ if (definition.model !== 'table') {
2954
+ return;
2955
+ }
2956
+ schema.extend('table', {
2957
+ allowAttributes: [
2958
+ 'htmlTableAttributes',
2959
+ // Figure, thead and tbody elements don't have model counterparts.
2960
+ // We will be preserving attributes on table element using these attribute keys.
2961
+ 'htmlFigureAttributes',
2962
+ 'htmlTheadAttributes',
2963
+ 'htmlTbodyAttributes'
2964
+ ]
2965
+ });
2966
+ conversion.for('upcast').add(viewToModelTableAttributeConverter(dataFilter));
2967
+ conversion.for('downcast').add(modelToViewTableAttributeConverter());
2968
+ editor.model.document.registerPostFixer(createHeadingRowsPostFixer(editor.model, tableUtils));
2969
+ evt.stop();
2970
+ });
2971
+ }
2972
+ }
2973
+ /**
2974
+ * Creates a model post-fixer for thead and tbody GHS related attributes.
2975
+ */ function createHeadingRowsPostFixer(model, tableUtils) {
2976
+ return (writer)=>{
2977
+ const changes = model.document.differ.getChanges();
2978
+ let wasFixed = false;
2979
+ for (const change of changes){
2980
+ if (change.type != 'attribute' || change.attributeKey != 'headingRows') {
2981
+ continue;
2982
+ }
2983
+ const table = change.range.start.nodeAfter;
2984
+ const hasTHeadAttributes = table.getAttribute('htmlTheadAttributes');
2985
+ const hasTBodyAttributes = table.getAttribute('htmlTbodyAttributes');
2986
+ if (hasTHeadAttributes && !change.attributeNewValue) {
2987
+ writer.removeAttribute('htmlTheadAttributes', table);
2988
+ wasFixed = true;
2989
+ } else if (hasTBodyAttributes && change.attributeNewValue == tableUtils.getRows(table)) {
2990
+ writer.removeAttribute('htmlTbodyAttributes', table);
2991
+ wasFixed = true;
2992
+ }
2993
+ }
2994
+ return wasFixed;
2995
+ };
2996
+ }
2997
+ /**
2998
+ * View-to-model conversion helper preserving allowed attributes on {@link module:table/table~Table Table}
2999
+ * feature model element.
3000
+ *
3001
+ * @returns Returns a conversion callback.
3002
+ */ function viewToModelTableAttributeConverter(dataFilter) {
3003
+ return (dispatcher)=>{
3004
+ dispatcher.on('element:table', (evt, data, conversionApi)=>{
3005
+ if (!data.modelRange) {
3006
+ return;
3007
+ }
3008
+ const viewTableElement = data.viewItem;
3009
+ preserveElementAttributes(viewTableElement, 'htmlTableAttributes');
3010
+ for (const childNode of viewTableElement.getChildren()){
3011
+ if (childNode.is('element', 'thead')) {
3012
+ preserveElementAttributes(childNode, 'htmlTheadAttributes');
3013
+ }
3014
+ if (childNode.is('element', 'tbody')) {
3015
+ preserveElementAttributes(childNode, 'htmlTbodyAttributes');
3016
+ }
3017
+ }
3018
+ function preserveElementAttributes(viewElement, attributeName) {
3019
+ const viewAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
3020
+ if (viewAttributes) {
3021
+ conversionApi.writer.setAttribute(attributeName, viewAttributes, data.modelRange);
3022
+ }
3023
+ }
3024
+ }, {
3025
+ priority: 'low'
3026
+ });
3027
+ };
3028
+ }
3029
+ /**
3030
+ * View-to-model conversion helper preserving allowed attributes on {@link module:table/table~Table Table}
3031
+ * feature model element from figure view element.
3032
+ *
3033
+ * @returns Returns a conversion callback.
3034
+ */ function viewToModelFigureAttributeConverter(dataFilter) {
3035
+ return (dispatcher)=>{
3036
+ dispatcher.on('element:figure', (evt, data, conversionApi)=>{
3037
+ const viewFigureElement = data.viewItem;
3038
+ if (!data.modelRange || !viewFigureElement.hasClass('table')) {
3039
+ return;
3040
+ }
3041
+ const viewAttributes = dataFilter.processViewAttributes(viewFigureElement, conversionApi);
3042
+ if (viewAttributes) {
3043
+ conversionApi.writer.setAttribute('htmlFigureAttributes', viewAttributes, data.modelRange);
3044
+ }
3045
+ }, {
3046
+ priority: 'low'
3047
+ });
3048
+ };
3049
+ }
3050
+ /**
3051
+ * Model-to-view conversion helper applying attributes from {@link module:table/table~Table Table}
3052
+ * feature.
3053
+ *
3054
+ * @returns Returns a conversion callback.
3055
+ */ function modelToViewTableAttributeConverter() {
3056
+ return (dispatcher)=>{
3057
+ addAttributeConversionDispatcherHandler('table', 'htmlTableAttributes');
3058
+ addAttributeConversionDispatcherHandler('figure', 'htmlFigureAttributes');
3059
+ addAttributeConversionDispatcherHandler('thead', 'htmlTheadAttributes');
3060
+ addAttributeConversionDispatcherHandler('tbody', 'htmlTbodyAttributes');
3061
+ function addAttributeConversionDispatcherHandler(elementName, attributeName) {
3062
+ dispatcher.on(`attribute:${attributeName}:table`, (evt, data, conversionApi)=>{
3063
+ if (!conversionApi.consumable.test(data.item, evt.name)) {
3064
+ return;
3065
+ }
3066
+ const containerElement = conversionApi.mapper.toViewElement(data.item);
3067
+ const viewElement = getDescendantElement(conversionApi.writer, containerElement, elementName);
3068
+ if (!viewElement) {
3069
+ return;
3070
+ }
3071
+ conversionApi.consumable.consume(data.item, evt.name);
3072
+ updateViewAttributes(conversionApi.writer, data.attributeOldValue, data.attributeNewValue, viewElement);
3073
+ });
3074
+ }
3075
+ };
3076
+ }
3077
+
3078
+ /**
3079
+ * Provides the General HTML Support for `style` elements.
3080
+ */ class StyleElementSupport extends Plugin {
3081
+ /**
3082
+ * @inheritDoc
3083
+ */ static get requires() {
3084
+ return [
3085
+ DataFilter
3086
+ ];
3087
+ }
3088
+ /**
3089
+ * @inheritDoc
3090
+ */ static get pluginName() {
3091
+ return 'StyleElementSupport';
3092
+ }
3093
+ /**
3094
+ * @inheritDoc
3095
+ */ static get isOfficialPlugin() {
3096
+ return true;
3097
+ }
3098
+ /**
3099
+ * @inheritDoc
3100
+ */ init() {
3101
+ const dataFilter = this.editor.plugins.get(DataFilter);
3102
+ dataFilter.on('register:style', (evt, definition)=>{
3103
+ const editor = this.editor;
3104
+ const schema = editor.model.schema;
3105
+ const conversion = editor.conversion;
3106
+ schema.register('htmlStyle', definition.modelSchema);
3107
+ schema.extend('htmlStyle', {
3108
+ allowAttributes: [
3109
+ 'htmlStyleAttributes',
3110
+ 'htmlContent'
3111
+ ],
3112
+ isContent: true
3113
+ });
3114
+ editor.data.registerRawContentMatcher({
3115
+ name: 'style'
3116
+ });
3117
+ conversion.for('upcast').elementToElement({
3118
+ view: 'style',
3119
+ model: viewToModelObjectConverter(definition)
3120
+ });
3121
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, dataFilter));
3122
+ conversion.for('downcast').elementToElement({
3123
+ model: 'htmlStyle',
3124
+ view: (modelElement, { writer })=>{
3125
+ return createObjectView('style', modelElement, writer);
3126
+ }
3127
+ });
3128
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
3129
+ evt.stop();
3130
+ });
3131
+ }
3132
+ }
3133
+
3134
+ /**
3135
+ * Provides the General HTML Support integration with the {@link module:list/list~List List} feature.
3136
+ */ class ListElementSupport extends Plugin {
3137
+ /**
3138
+ * @inheritDoc
3139
+ */ static get requires() {
3140
+ return [
3141
+ DataFilter
3142
+ ];
3143
+ }
3144
+ /**
3145
+ * @inheritDoc
3146
+ */ static get pluginName() {
3147
+ return 'ListElementSupport';
3148
+ }
3149
+ /**
3150
+ * @inheritDoc
3151
+ */ static get isOfficialPlugin() {
3152
+ return true;
3153
+ }
3154
+ /**
3155
+ * @inheritDoc
3156
+ */ init() {
3157
+ const editor = this.editor;
3158
+ if (!editor.plugins.has('ListEditing')) {
3159
+ return;
3160
+ }
3161
+ const schema = editor.model.schema;
3162
+ const conversion = editor.conversion;
3163
+ const dataFilter = editor.plugins.get(DataFilter);
3164
+ const listEditing = editor.plugins.get('ListEditing');
3165
+ const listUtils = editor.plugins.get('ListUtils');
3166
+ const viewElements = [
3167
+ 'ul',
3168
+ 'ol',
3169
+ 'li'
3170
+ ];
3171
+ // Register downcast strategy.
3172
+ // Note that this must be done before document list editing registers conversion in afterInit.
3173
+ listEditing.registerDowncastStrategy({
3174
+ scope: 'item',
3175
+ attributeName: 'htmlLiAttributes',
3176
+ setAttributeOnDowncast: setViewAttributes
3177
+ });
3178
+ listEditing.registerDowncastStrategy({
3179
+ scope: 'list',
3180
+ attributeName: 'htmlUlAttributes',
3181
+ setAttributeOnDowncast: setViewAttributes
3182
+ });
3183
+ listEditing.registerDowncastStrategy({
3184
+ scope: 'list',
3185
+ attributeName: 'htmlOlAttributes',
3186
+ setAttributeOnDowncast: setViewAttributes
3187
+ });
3188
+ dataFilter.on('register', (evt, definition)=>{
3189
+ if (!viewElements.includes(definition.view)) {
3190
+ return;
3191
+ }
3192
+ evt.stop();
3193
+ // Do not register same converters twice.
3194
+ if (schema.checkAttribute('$block', 'htmlLiAttributes')) {
3195
+ return;
3196
+ }
3197
+ const allowAttributes = viewElements.map((element)=>getHtmlAttributeName(element));
3198
+ schema.extend('$listItem', {
3199
+ allowAttributes
3200
+ });
3201
+ conversion.for('upcast').add((dispatcher)=>{
3202
+ dispatcher.on('element:ul', viewToModelListAttributeConverter('htmlUlAttributes', dataFilter), {
3203
+ priority: 'low'
3204
+ });
3205
+ dispatcher.on('element:ol', viewToModelListAttributeConverter('htmlOlAttributes', dataFilter), {
3206
+ priority: 'low'
3207
+ });
3208
+ dispatcher.on('element:li', viewToModelListAttributeConverter('htmlLiAttributes', dataFilter), {
3209
+ priority: 'low'
3210
+ });
3211
+ });
3212
+ });
3213
+ // Make sure that all items in a single list (items at the same level & listType) have the same properties.
3214
+ listEditing.on('postFixer', (evt, { listNodes, writer })=>{
3215
+ for (const { node, previousNodeInList } of listNodes){
3216
+ // This is a first item of a nested list.
3217
+ if (!previousNodeInList) {
3218
+ continue;
3219
+ }
3220
+ if (previousNodeInList.getAttribute('listType') == node.getAttribute('listType')) {
3221
+ const attribute = getAttributeFromListType(previousNodeInList.getAttribute('listType'));
3222
+ const value = previousNodeInList.getAttribute(attribute);
3223
+ if (!isEqual(node.getAttribute(attribute), value) && writer.model.schema.checkAttribute(node, attribute)) {
3224
+ writer.setAttribute(attribute, value, node);
3225
+ evt.return = true;
3226
+ }
3227
+ }
3228
+ if (previousNodeInList.getAttribute('listItemId') == node.getAttribute('listItemId')) {
3229
+ const value = previousNodeInList.getAttribute('htmlLiAttributes');
3230
+ if (!isEqual(node.getAttribute('htmlLiAttributes'), value) && writer.model.schema.checkAttribute(node, 'htmlLiAttributes')) {
3231
+ writer.setAttribute('htmlLiAttributes', value, node);
3232
+ evt.return = true;
3233
+ }
3234
+ }
3235
+ }
3236
+ });
3237
+ // Remove `ol` attributes from `ul` elements and vice versa.
3238
+ listEditing.on('postFixer', (evt, { listNodes, writer })=>{
3239
+ for (const { node } of listNodes){
3240
+ const listType = node.getAttribute('listType');
3241
+ if (!listUtils.isNumberedListType(listType) && node.getAttribute('htmlOlAttributes')) {
3242
+ writer.removeAttribute('htmlOlAttributes', node);
3243
+ evt.return = true;
3244
+ }
3245
+ if (listUtils.isNumberedListType(listType) && node.getAttribute('htmlUlAttributes')) {
3246
+ writer.removeAttribute('htmlUlAttributes', node);
3247
+ evt.return = true;
3248
+ }
3249
+ }
3250
+ });
3251
+ }
3252
+ /**
3253
+ * @inheritDoc
3254
+ */ afterInit() {
3255
+ const editor = this.editor;
3256
+ if (!editor.commands.get('indentList')) {
3257
+ return;
3258
+ }
3259
+ // Reset list attributes after indenting list items.
3260
+ const indentList = editor.commands.get('indentList');
3261
+ this.listenTo(indentList, 'afterExecute', (evt, changedBlocks)=>{
3262
+ editor.model.change((writer)=>{
3263
+ for (const node of changedBlocks){
3264
+ const attribute = getAttributeFromListType(node.getAttribute('listType'));
3265
+ if (!editor.model.schema.checkAttribute(node, attribute)) {
3266
+ continue;
3267
+ }
3268
+ // Just reset the attribute.
3269
+ // If there is a previous indented list that this node should be merged into,
3270
+ // the postfixer will unify all the attributes of both sub-lists.
3271
+ writer.setAttribute(attribute, {}, node);
3272
+ }
3273
+ });
3274
+ });
3275
+ }
3276
+ }
3277
+ /**
3278
+ * View-to-model conversion helper preserving allowed attributes on {@link TODO}
3279
+ * feature model element.
3280
+ *
3281
+ * @returns Returns a conversion callback.
3282
+ */ function viewToModelListAttributeConverter(attributeName, dataFilter) {
3283
+ return (evt, data, conversionApi)=>{
3284
+ const viewElement = data.viewItem;
3285
+ if (!data.modelRange) {
3286
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
3287
+ }
3288
+ const viewAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
3289
+ for (const item of data.modelRange.getItems({
3290
+ shallow: true
3291
+ })){
3292
+ // Apply only to list item blocks.
3293
+ if (!item.hasAttribute('listItemId')) {
3294
+ continue;
3295
+ }
3296
+ // Set list attributes only on same level items, those nested deeper are already handled
3297
+ // by the recursive conversion.
3298
+ if (item.hasAttribute('htmlUlAttributes') || item.hasAttribute('htmlOlAttributes')) {
3299
+ continue;
3300
+ }
3301
+ if (conversionApi.writer.model.schema.checkAttribute(item, attributeName)) {
3302
+ conversionApi.writer.setAttribute(attributeName, viewAttributes || {}, item);
3303
+ }
3304
+ }
3305
+ };
3306
+ }
3307
+ /**
3308
+ * Returns HTML attribute name based on provided list type.
3309
+ */ function getAttributeFromListType(listType) {
3310
+ return listType === 'numbered' || listType == 'customNumbered' ? 'htmlOlAttributes' : 'htmlUlAttributes';
3311
+ }
3312
+
3313
+ /**
3314
+ * Provides the General HTML Support for custom elements (not registered in the {@link module:html-support/dataschema~DataSchema}).
3315
+ */ class CustomElementSupport extends Plugin {
3316
+ /**
3317
+ * @inheritDoc
3318
+ */ static get requires() {
3319
+ return [
3320
+ DataFilter,
3321
+ DataSchema
3322
+ ];
3323
+ }
3324
+ /**
3325
+ * @inheritDoc
3326
+ */ static get pluginName() {
3327
+ return 'CustomElementSupport';
3328
+ }
3329
+ /**
3330
+ * @inheritDoc
3331
+ */ static get isOfficialPlugin() {
3332
+ return true;
3333
+ }
3334
+ /**
3335
+ * @inheritDoc
3336
+ */ init() {
3337
+ const dataFilter = this.editor.plugins.get(DataFilter);
3338
+ const dataSchema = this.editor.plugins.get(DataSchema);
3339
+ dataFilter.on('register:$customElement', (evt, definition)=>{
3340
+ evt.stop();
3341
+ const editor = this.editor;
3342
+ const schema = editor.model.schema;
3343
+ const conversion = editor.conversion;
3344
+ const unsafeElements = editor.editing.view.domConverter.unsafeElements;
3345
+ const preLikeElements = editor.data.htmlProcessor.domConverter.preElements;
3346
+ schema.register(definition.model, definition.modelSchema);
3347
+ schema.extend(definition.model, {
3348
+ allowAttributes: [
3349
+ 'htmlElementName',
3350
+ 'htmlCustomElementAttributes',
3351
+ 'htmlContent'
3352
+ ],
3353
+ isContent: true
3354
+ });
3355
+ // For the `<template>` element we use only raw-content because DOM API exposes its content
3356
+ // only as a document fragment in the `content` property (or innerHTML).
3357
+ editor.data.htmlProcessor.domConverter.registerRawContentMatcher({
3358
+ name: 'template'
3359
+ });
3360
+ // Being executed on the low priority, it will catch all elements that were not caught by other converters.
3361
+ conversion.for('upcast').elementToElement({
3362
+ view: /.*/,
3363
+ model: (viewElement, conversionApi)=>{
3364
+ // Do not try to convert $comment fake element.
3365
+ if (viewElement.name == '$comment') {
3366
+ return null;
3367
+ }
3368
+ if (!isValidElementName(viewElement.name)) {
3369
+ return null;
3370
+ }
3371
+ // Allow for fallback only if this element is not defined in data schema to make sure
3372
+ // that this will handle only custom elements not registered in the data schema.
3373
+ if (dataSchema.getDefinitionsForView(viewElement.name).size) {
3374
+ return null;
3375
+ }
3376
+ // Make sure that this element will not render in the editing view.
3377
+ if (!unsafeElements.includes(viewElement.name)) {
3378
+ unsafeElements.push(viewElement.name);
3379
+ }
3380
+ // Make sure that whitespaces will not be trimmed or replaced by nbsps while stringify content.
3381
+ if (!preLikeElements.includes(viewElement.name)) {
3382
+ preLikeElements.push(viewElement.name);
3383
+ }
3384
+ const modelElement = conversionApi.writer.createElement(definition.model, {
3385
+ htmlElementName: viewElement.name
3386
+ });
3387
+ const htmlAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
3388
+ if (htmlAttributes) {
3389
+ conversionApi.writer.setAttribute('htmlCustomElementAttributes', htmlAttributes, modelElement);
3390
+ }
3391
+ let htmlContent;
3392
+ // For the `<template>` element we use only raw-content because DOM API exposes its content
3393
+ // only as a document fragment in the `content` property.
3394
+ if (viewElement.is('element', 'template') && viewElement.getCustomProperty('$rawContent')) {
3395
+ htmlContent = viewElement.getCustomProperty('$rawContent');
3396
+ } else {
3397
+ // Store the whole element in the attribute so that DomConverter will be able to use the pre like element context.
3398
+ const viewWriter = new UpcastWriter(viewElement.document);
3399
+ const documentFragment = viewWriter.createDocumentFragment(viewElement);
3400
+ const domFragment = editor.data.htmlProcessor.domConverter.viewToDom(documentFragment);
3401
+ const domElement = domFragment.firstChild;
3402
+ while(domElement.firstChild){
3403
+ domFragment.appendChild(domElement.firstChild);
3404
+ }
3405
+ domElement.remove();
3406
+ htmlContent = editor.data.htmlProcessor.htmlWriter.getHtml(domFragment);
3407
+ }
3408
+ conversionApi.writer.setAttribute('htmlContent', htmlContent, modelElement);
3409
+ // Consume the content of the element.
3410
+ for (const { item } of editor.editing.view.createRangeIn(viewElement)){
3411
+ conversionApi.consumable.consume(item, {
3412
+ name: true
3413
+ });
3414
+ }
3415
+ return modelElement;
3416
+ },
3417
+ converterPriority: 'low'
3418
+ });
3419
+ // Because this element is unsafe (DomConverter#unsafeElements), it will render as a transparent <span> but it must
3420
+ // be rendered anyway for the mapping between the model and the view to exist.
3421
+ conversion.for('editingDowncast').elementToElement({
3422
+ model: {
3423
+ name: definition.model,
3424
+ attributes: [
3425
+ 'htmlElementName',
3426
+ 'htmlCustomElementAttributes',
3427
+ 'htmlContent'
3428
+ ]
3429
+ },
3430
+ view: (modelElement, { writer })=>{
3431
+ const viewName = modelElement.getAttribute('htmlElementName');
3432
+ const viewElement = writer.createRawElement(viewName);
3433
+ if (modelElement.hasAttribute('htmlCustomElementAttributes')) {
3434
+ setViewAttributes(writer, modelElement.getAttribute('htmlCustomElementAttributes'), viewElement);
3435
+ }
3436
+ return viewElement;
3437
+ }
3438
+ });
3439
+ conversion.for('dataDowncast').elementToElement({
3440
+ model: {
3441
+ name: definition.model,
3442
+ attributes: [
3443
+ 'htmlElementName',
3444
+ 'htmlCustomElementAttributes',
3445
+ 'htmlContent'
3446
+ ]
3447
+ },
3448
+ view: (modelElement, { writer })=>{
3449
+ const viewName = modelElement.getAttribute('htmlElementName');
3450
+ const htmlContent = modelElement.getAttribute('htmlContent');
3451
+ const viewElement = writer.createRawElement(viewName, null, (domElement, domConverter)=>{
3452
+ domConverter.setContentOf(domElement, htmlContent);
3453
+ });
3454
+ if (modelElement.hasAttribute('htmlCustomElementAttributes')) {
3455
+ setViewAttributes(writer, modelElement.getAttribute('htmlCustomElementAttributes'), viewElement);
3456
+ }
3457
+ return viewElement;
3458
+ }
3459
+ });
3460
+ });
3461
+ }
3462
+ }
3463
+ /**
3464
+ * Returns true if name is valid for a DOM element name.
3465
+ */ function isValidElementName(name) {
3466
+ try {
3467
+ document.createElement(name);
3468
+ } catch (error) {
3469
+ return false;
3470
+ }
3471
+ return true;
3472
+ }
3473
+
3474
+ /**
3475
+ * The General HTML Support feature.
3476
+ *
3477
+ * This is a "glue" plugin which initializes the {@link module:html-support/datafilter~DataFilter data filter} configuration
3478
+ * and features integration with the General HTML Support.
3479
+ */ class GeneralHtmlSupport extends Plugin {
3480
+ /**
3481
+ * @inheritDoc
3482
+ */ static get pluginName() {
3483
+ return 'GeneralHtmlSupport';
3484
+ }
3485
+ /**
3486
+ * @inheritDoc
3487
+ */ static get isOfficialPlugin() {
3488
+ return true;
3489
+ }
3490
+ /**
3491
+ * @inheritDoc
3492
+ */ static get requires() {
3493
+ return [
3494
+ DataFilter,
3495
+ CodeBlockElementSupport,
3496
+ DualContentModelElementSupport,
3497
+ HeadingElementSupport,
3498
+ ImageElementSupport,
3499
+ MediaEmbedElementSupport,
3500
+ ScriptElementSupport,
3501
+ TableElementSupport,
3502
+ StyleElementSupport,
3503
+ ListElementSupport,
3504
+ CustomElementSupport
3505
+ ];
3506
+ }
3507
+ /**
3508
+ * @inheritDoc
3509
+ */ init() {
3510
+ const editor = this.editor;
3511
+ const dataFilter = editor.plugins.get(DataFilter);
3512
+ // Load the allowed empty inline elements' configuration.
3513
+ // Note that this modifies DataSchema so must be loaded before registering filtering rules.
3514
+ dataFilter.loadAllowedEmptyElementsConfig(editor.config.get('htmlSupport.allowEmpty') || []);
3515
+ // Load the filtering configuration.
3516
+ dataFilter.loadAllowedConfig(editor.config.get('htmlSupport.allow') || []);
3517
+ dataFilter.loadDisallowedConfig(editor.config.get('htmlSupport.disallow') || []);
3518
+ }
3519
+ /**
3520
+ * Returns a GHS model attribute name related to a given view element name.
3521
+ *
3522
+ * @internal
3523
+ * @param viewElementName A view element name.
3524
+ */ getGhsAttributeNameForElement(viewElementName) {
3525
+ const dataSchema = this.editor.plugins.get('DataSchema');
3526
+ const definitions = Array.from(dataSchema.getDefinitionsForView(viewElementName, false));
3527
+ const inlineDefinition = definitions.find((definition)=>definition.isInline && !definitions[0].isObject);
3528
+ if (inlineDefinition) {
3529
+ return inlineDefinition.model;
3530
+ }
3531
+ return getHtmlAttributeName(viewElementName);
3532
+ }
3533
+ /**
3534
+ * Updates GHS model attribute for a specified view element name, so it includes the given class name.
3535
+ *
3536
+ * @internal
3537
+ * @param viewElementName A view element name.
3538
+ * @param className The css class to add.
3539
+ * @param selectable The selection or element to update.
3540
+ */ addModelHtmlClass(viewElementName, className, selectable) {
3541
+ const model = this.editor.model;
3542
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3543
+ model.change((writer)=>{
3544
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3545
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'classes', (classes)=>{
3546
+ for (const value of toArray(className)){
3547
+ classes.add(value);
3548
+ }
3549
+ });
3550
+ }
3551
+ });
3552
+ }
3553
+ /**
3554
+ * Updates GHS model attribute for a specified view element name, so it does not include the given class name.
3555
+ *
3556
+ * @internal
3557
+ * @param viewElementName A view element name.
3558
+ * @param className The css class to remove.
3559
+ * @param selectable The selection or element to update.
3560
+ */ removeModelHtmlClass(viewElementName, className, selectable) {
3561
+ const model = this.editor.model;
3562
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3563
+ model.change((writer)=>{
3564
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3565
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'classes', (classes)=>{
3566
+ for (const value of toArray(className)){
3567
+ classes.delete(value);
3568
+ }
3569
+ });
3570
+ }
3571
+ });
3572
+ }
3573
+ /**
3574
+ * Updates GHS model attribute for a specified view element name, so it includes the given attribute.
3575
+ *
3576
+ * @param viewElementName A view element name.
3577
+ * @param attributes The object with attributes to set.
3578
+ * @param selectable The selection or element to update.
3579
+ */ setModelHtmlAttributes(viewElementName, attributes, selectable) {
3580
+ const model = this.editor.model;
3581
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3582
+ model.change((writer)=>{
3583
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3584
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'attributes', (attributesMap)=>{
3585
+ for (const [key, value] of Object.entries(attributes)){
3586
+ attributesMap.set(key, value);
3587
+ }
3588
+ });
3589
+ }
3590
+ });
3591
+ }
3592
+ /**
3593
+ * Updates GHS model attribute for a specified view element name, so it does not include the given attribute.
3594
+ *
3595
+ * @param viewElementName A view element name.
3596
+ * @param attributeName The attribute name (or names) to remove.
3597
+ * @param selectable The selection or element to update.
3598
+ */ removeModelHtmlAttributes(viewElementName, attributeName, selectable) {
3599
+ const model = this.editor.model;
3600
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3601
+ model.change((writer)=>{
3602
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3603
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'attributes', (attributesMap)=>{
3604
+ for (const key of toArray(attributeName)){
3605
+ attributesMap.delete(key);
3606
+ }
3607
+ });
3608
+ }
3609
+ });
3610
+ }
3611
+ /**
3612
+ * Updates GHS model attribute for a specified view element name, so it includes a given style.
3613
+ *
3614
+ * @param viewElementName A view element name.
3615
+ * @param styles The object with styles to set.
3616
+ * @param selectable The selection or element to update.
3617
+ */ setModelHtmlStyles(viewElementName, styles, selectable) {
3618
+ const model = this.editor.model;
3619
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3620
+ model.change((writer)=>{
3621
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3622
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'styles', (stylesMap)=>{
3623
+ for (const [key, value] of Object.entries(styles)){
3624
+ stylesMap.set(key, value);
3625
+ }
3626
+ });
3627
+ }
3628
+ });
3629
+ }
3630
+ /**
3631
+ * Updates GHS model attribute for a specified view element name, so it does not include a given style.
3632
+ *
3633
+ * @param viewElementName A view element name.
3634
+ * @param properties The style (or styles list) to remove.
3635
+ * @param selectable The selection or element to update.
3636
+ */ removeModelHtmlStyles(viewElementName, properties, selectable) {
3637
+ const model = this.editor.model;
3638
+ const ghsAttributeName = this.getGhsAttributeNameForElement(viewElementName);
3639
+ model.change((writer)=>{
3640
+ for (const item of getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName)){
3641
+ modifyGhsAttribute(writer, item, ghsAttributeName, 'styles', (stylesMap)=>{
3642
+ for (const key of toArray(properties)){
3643
+ stylesMap.delete(key);
3644
+ }
3645
+ });
3646
+ }
3647
+ });
3648
+ }
3649
+ }
3650
+ /**
3651
+ * Returns an iterator over an items in the selectable that accept given GHS attribute.
3652
+ */ function* getItemsToUpdateGhsAttribute(model, selectable, ghsAttributeName) {
3653
+ if (!selectable) {
3654
+ return;
3655
+ }
3656
+ if (!(Symbol.iterator in selectable) && selectable.is('documentSelection') && selectable.isCollapsed) {
3657
+ if (model.schema.checkAttributeInSelection(selectable, ghsAttributeName)) {
3658
+ yield selectable;
3659
+ }
3660
+ } else {
3661
+ for (const range of getValidRangesForSelectable(model, selectable, ghsAttributeName)){
3662
+ yield* range.getItems({
3663
+ shallow: true
3664
+ });
3665
+ }
3666
+ }
3667
+ }
3668
+ /**
3669
+ * Translates a given selectable to an iterable of ranges.
3670
+ */ function getValidRangesForSelectable(model, selectable, ghsAttributeName) {
3671
+ if (!(Symbol.iterator in selectable) && (selectable.is('node') || selectable.is('$text') || selectable.is('$textProxy'))) {
3672
+ if (model.schema.checkAttribute(selectable, ghsAttributeName)) {
3673
+ return [
3674
+ model.createRangeOn(selectable)
3675
+ ];
3676
+ } else {
3677
+ return [];
3678
+ }
3679
+ } else {
3680
+ return model.schema.getValidRanges(model.createSelection(selectable).getRanges(), ghsAttributeName);
3681
+ }
3682
+ }
3683
+
3684
+ /**
3685
+ * The HTML comment feature. It preserves the HTML comments (`<!-- -->`) in the editor data.
3686
+ *
3687
+ * For a detailed overview, check the {@glink features/html/html-comments HTML comment feature documentation}.
3688
+ */ class HtmlComment extends Plugin {
3689
+ /**
3690
+ * @inheritDoc
3691
+ */ static get pluginName() {
3692
+ return 'HtmlComment';
3693
+ }
3694
+ /**
3695
+ * @inheritDoc
3696
+ */ static get isOfficialPlugin() {
3697
+ return true;
3698
+ }
3699
+ /**
3700
+ * @inheritDoc
3701
+ */ init() {
3702
+ const editor = this.editor;
3703
+ const loadedCommentsContent = new Map();
3704
+ editor.data.processor.skipComments = false;
3705
+ // Allow storing comment's content as the $root attribute with the name `$comment:<unique id>`.
3706
+ editor.model.schema.addAttributeCheck((context, attributeName)=>{
3707
+ if (context.endsWith('$root') && attributeName.startsWith('$comment')) {
3708
+ return true;
3709
+ }
3710
+ });
3711
+ // Convert the `$comment` view element to `$comment:<unique id>` marker and store its content (the comment itself) as a $root
3712
+ // attribute. The comment content is needed in the `dataDowncast` pipeline to re-create the comment node.
3713
+ editor.conversion.for('upcast').elementToMarker({
3714
+ view: '$comment',
3715
+ model: (viewElement)=>{
3716
+ const markerUid = uid();
3717
+ const markerName = `$comment:${markerUid}`;
3718
+ const commentContent = viewElement.getCustomProperty('$rawContent');
3719
+ loadedCommentsContent.set(markerName, commentContent);
3720
+ return markerName;
3721
+ }
3722
+ });
3723
+ // Convert the `$comment` marker to `$comment` UI element with `$rawContent` custom property containing the comment content.
3724
+ editor.conversion.for('dataDowncast').markerToElement({
3725
+ model: '$comment',
3726
+ view: (modelElement, { writer })=>{
3727
+ let root = undefined;
3728
+ for (const rootName of this.editor.model.document.getRootNames()){
3729
+ root = this.editor.model.document.getRoot(rootName);
3730
+ if (root.hasAttribute(modelElement.markerName)) {
3731
+ break;
3732
+ }
3733
+ }
3734
+ const markerName = modelElement.markerName;
3735
+ const commentContent = root.getAttribute(markerName);
3736
+ const comment = writer.createUIElement('$comment');
3737
+ writer.setCustomProperty('$rawContent', commentContent, comment);
3738
+ return comment;
3739
+ }
3740
+ });
3741
+ // Remove comments' markers and their corresponding $root attributes, which are moved to the graveyard.
3742
+ editor.model.document.registerPostFixer((writer)=>{
3743
+ let changed = false;
3744
+ const markers = editor.model.document.differ.getChangedMarkers().filter((marker)=>marker.name.startsWith('$comment:'));
3745
+ for (const marker of markers){
3746
+ const { oldRange, newRange } = marker.data;
3747
+ if (oldRange && newRange && oldRange.root == newRange.root) {
3748
+ continue;
3749
+ }
3750
+ if (oldRange) {
3751
+ // The comment marker was moved from one root to another (most probably to the graveyard).
3752
+ // Remove the related attribute from the previous root.
3753
+ const oldRoot = oldRange.root;
3754
+ if (oldRoot.hasAttribute(marker.name)) {
3755
+ writer.removeAttribute(marker.name, oldRoot);
3756
+ changed = true;
3757
+ }
3758
+ }
3759
+ if (newRange) {
3760
+ const newRoot = newRange.root;
3761
+ if (newRoot.rootName == '$graveyard') {
3762
+ // Comment marker was moved to the graveyard -- remove it entirely.
3763
+ writer.removeMarker(marker.name);
3764
+ changed = true;
3765
+ } else if (!newRoot.hasAttribute(marker.name)) {
3766
+ // Comment marker was just added or was moved to another root - updated roots attributes.
3767
+ //
3768
+ // Added fallback to `''` for the comment content in case if someone incorrectly added just the marker "by hand"
3769
+ // and forgot to add the root attribute or add them in different change blocks.
3770
+ //
3771
+ // It caused an infinite loop in one of the unit tests.
3772
+ writer.setAttribute(marker.name, loadedCommentsContent.get(marker.name) || '', newRoot);
3773
+ changed = true;
3774
+ }
3775
+ }
3776
+ }
3777
+ return changed;
3778
+ });
3779
+ // Delete all comment markers from the document before setting new data.
3780
+ editor.data.on('set', ()=>{
3781
+ for (const commentMarker of editor.model.markers.getMarkersGroup('$comment')){
3782
+ this.removeHtmlComment(commentMarker.name);
3783
+ }
3784
+ }, {
3785
+ priority: 'high'
3786
+ });
3787
+ // Delete all comment markers that are within a removed range.
3788
+ // Delete all comment markers at the limit element boundaries if the whole content of the limit element is removed.
3789
+ editor.model.on('deleteContent', (evt, [selection])=>{
3790
+ for (const range of selection.getRanges()){
3791
+ const limitElement = editor.model.schema.getLimitElement(range);
3792
+ const firstPosition = editor.model.createPositionAt(limitElement, 0);
3793
+ const lastPosition = editor.model.createPositionAt(limitElement, 'end');
3794
+ let affectedCommentIDs;
3795
+ if (firstPosition.isTouching(range.start) && lastPosition.isTouching(range.end)) {
3796
+ affectedCommentIDs = this.getHtmlCommentsInRange(editor.model.createRange(firstPosition, lastPosition));
3797
+ } else {
3798
+ affectedCommentIDs = this.getHtmlCommentsInRange(range, {
3799
+ skipBoundaries: true
3800
+ });
3801
+ }
3802
+ for (const commentMarkerID of affectedCommentIDs){
3803
+ this.removeHtmlComment(commentMarkerID);
3804
+ }
3805
+ }
3806
+ }, {
3807
+ priority: 'high'
3808
+ });
3809
+ }
3810
+ /**
3811
+ * Creates an HTML comment on the specified position and returns its ID.
3812
+ *
3813
+ * *Note*: If two comments are created at the same position, the second comment will be inserted before the first one.
3814
+ *
3815
+ * @returns Comment ID. This ID can be later used to e.g. remove the comment from the content.
3816
+ */ createHtmlComment(position, content) {
3817
+ const id = uid();
3818
+ const editor = this.editor;
3819
+ const model = editor.model;
3820
+ const root = model.document.getRoot(position.root.rootName);
3821
+ const markerName = `$comment:${id}`;
3822
+ return model.change((writer)=>{
3823
+ const range = writer.createRange(position);
3824
+ writer.addMarker(markerName, {
3825
+ usingOperation: true,
3826
+ affectsData: true,
3827
+ range
3828
+ });
3829
+ writer.setAttribute(markerName, content, root);
3830
+ return markerName;
3831
+ });
3832
+ }
3833
+ /**
3834
+ * Removes an HTML comment with the given comment ID.
3835
+ *
3836
+ * It does nothing and returns `false` if the comment with the given ID does not exist.
3837
+ * Otherwise it removes the comment and returns `true`.
3838
+ *
3839
+ * Note that a comment can be removed also by removing the content around the comment.
3840
+ *
3841
+ * @param commentID The ID of the comment to be removed.
3842
+ * @returns `true` when the comment with the given ID was removed, `false` otherwise.
3843
+ */ removeHtmlComment(commentID) {
3844
+ const editor = this.editor;
3845
+ const marker = editor.model.markers.get(commentID);
3846
+ if (!marker) {
3847
+ return false;
3848
+ }
3849
+ editor.model.change((writer)=>{
3850
+ writer.removeMarker(marker);
3851
+ });
3852
+ return true;
3853
+ }
3854
+ /**
3855
+ * Gets the HTML comment data for the comment with a given ID.
3856
+ *
3857
+ * Returns `null` if the comment does not exist.
3858
+ */ getHtmlCommentData(commentID) {
3859
+ const editor = this.editor;
3860
+ const marker = editor.model.markers.get(commentID);
3861
+ if (!marker) {
3862
+ return null;
3863
+ }
3864
+ let content = '';
3865
+ for (const root of this.editor.model.document.getRoots()){
3866
+ if (root.hasAttribute(commentID)) {
3867
+ content = root.getAttribute(commentID);
3868
+ break;
3869
+ }
3870
+ }
3871
+ return {
3872
+ content,
3873
+ position: marker.getStart()
3874
+ };
3875
+ }
3876
+ /**
3877
+ * Gets all HTML comments in the given range.
3878
+ *
3879
+ * By default, it includes comments at the range boundaries.
3880
+ *
3881
+ * @param range
3882
+ * @param options.skipBoundaries When set to `true` the range boundaries will be skipped.
3883
+ * @returns HTML comment IDs
3884
+ */ getHtmlCommentsInRange(range, { skipBoundaries = false } = {}) {
3885
+ const includeBoundaries = !skipBoundaries;
3886
+ // Unfortunately, MarkerCollection#getMarkersAtPosition() filters out collapsed markers.
3887
+ return Array.from(this.editor.model.markers.getMarkersGroup('$comment')).filter((marker)=>isCommentMarkerInRange(marker, range)).map((marker)=>marker.name);
3888
+ function isCommentMarkerInRange(commentMarker, range) {
3889
+ const position = commentMarker.getRange().start;
3890
+ return (position.isAfter(range.start) || includeBoundaries && position.isEqual(range.start)) && (position.isBefore(range.end) || includeBoundaries && position.isEqual(range.end));
3891
+ }
3892
+ }
3893
+ }
3894
+
3895
+ /**
3896
+ * The full page HTML data processor class.
3897
+ * This data processor implementation uses HTML as input and output data.
3898
+ */ class HtmlPageDataProcessor extends HtmlDataProcessor {
3899
+ /**
3900
+ * @inheritDoc
3901
+ */ toView(data) {
3902
+ // Ignore content that is not a full page source.
3903
+ if (!/<(?:html|body|head|meta)(?:\s[^>]*)?>/i.test(data.trim().slice(0, 10_000))) {
3904
+ return super.toView(data);
3905
+ }
3906
+ // Store doctype and xml declaration in a separate properties as they can't be stringified later.
3907
+ let docType = '';
3908
+ let xmlDeclaration = '';
3909
+ data = data.trim().replace(/<\?xml\s[^?]*\?>/i, (match)=>{
3910
+ xmlDeclaration = match;
3911
+ return '';
3912
+ });
3913
+ data = data.trim().replace(/^<!DOCTYPE\s[^>]*?>/i, (match)=>{
3914
+ docType = match;
3915
+ return '';
3916
+ });
3917
+ // Convert input HTML data to DOM DocumentFragment.
3918
+ const domFragment = this._toDom(data);
3919
+ // Convert DOM DocumentFragment to view DocumentFragment.
3920
+ const viewFragment = this.domConverter.domToView(domFragment, {
3921
+ skipComments: this.skipComments
3922
+ });
3923
+ const writer = new UpcastWriter(viewFragment.document);
3924
+ // Using the DOM document with body content extracted as a skeleton of the page.
3925
+ writer.setCustomProperty('$fullPageDocument', domFragment.ownerDocument.documentElement.outerHTML, viewFragment);
3926
+ if (docType) {
3927
+ writer.setCustomProperty('$fullPageDocType', docType, viewFragment);
3928
+ }
3929
+ if (xmlDeclaration) {
3930
+ writer.setCustomProperty('$fullPageXmlDeclaration', xmlDeclaration, viewFragment);
3931
+ }
3932
+ return viewFragment;
3933
+ }
3934
+ /**
3935
+ * @inheritDoc
3936
+ */ toData(viewFragment) {
3937
+ let data = super.toData(viewFragment);
3938
+ const page = viewFragment.getCustomProperty('$fullPageDocument');
3939
+ const docType = viewFragment.getCustomProperty('$fullPageDocType');
3940
+ const xmlDeclaration = viewFragment.getCustomProperty('$fullPageXmlDeclaration');
3941
+ if (page) {
3942
+ data = page.replace(/<\/body\s*>/, data + '$&');
3943
+ if (docType) {
3944
+ data = docType + '\n' + data;
3945
+ }
3946
+ if (xmlDeclaration) {
3947
+ data = xmlDeclaration + '\n' + data;
3948
+ }
3949
+ }
3950
+ return data;
3951
+ }
3952
+ }
3953
+
3954
+ /**
3955
+ * The full page editing feature. It preserves the whole HTML page in the editor data.
3956
+ */ class FullPage extends Plugin {
3957
+ /**
3958
+ * @inheritDoc
3959
+ */ static get pluginName() {
3960
+ return 'FullPage';
3961
+ }
3962
+ /**
3963
+ * @inheritDoc
3964
+ */ static get isOfficialPlugin() {
3965
+ return true;
3966
+ }
3967
+ /**
3968
+ * @inheritDoc
3969
+ */ init() {
3970
+ const editor = this.editor;
3971
+ const properties = [
3972
+ '$fullPageDocument',
3973
+ '$fullPageDocType',
3974
+ '$fullPageXmlDeclaration'
3975
+ ];
3976
+ editor.data.processor = new HtmlPageDataProcessor(editor.data.viewDocument);
3977
+ editor.model.schema.extend('$root', {
3978
+ allowAttributes: properties
3979
+ });
3980
+ // Apply custom properties from view document fragment to the model root attributes.
3981
+ editor.data.on('toModel', (evt, [viewElementOrFragment])=>{
3982
+ const root = editor.model.document.getRoot();
3983
+ editor.model.change((writer)=>{
3984
+ for (const name of properties){
3985
+ const value = viewElementOrFragment.getCustomProperty(name);
3986
+ if (value) {
3987
+ writer.setAttribute(name, value, root);
3988
+ }
3989
+ }
3990
+ });
3991
+ }, {
3992
+ priority: 'low'
3993
+ });
3994
+ // Apply root attributes to the view document fragment.
3995
+ editor.data.on('toView', (evt, [modelElementOrFragment])=>{
3996
+ if (!modelElementOrFragment.is('rootElement')) {
3997
+ return;
3998
+ }
3999
+ const root = modelElementOrFragment;
4000
+ const viewFragment = evt.return;
4001
+ if (!root.hasAttribute('$fullPageDocument')) {
4002
+ return;
4003
+ }
4004
+ const writer = new UpcastWriter(viewFragment.document);
4005
+ for (const name of properties){
4006
+ const value = root.getAttribute(name);
4007
+ if (value) {
4008
+ writer.setCustomProperty(name, value, viewFragment);
4009
+ }
4010
+ }
4011
+ }, {
4012
+ priority: 'low'
4013
+ });
4014
+ // Clear root attributes related to full page editing on editor content reset.
4015
+ editor.data.on('set', ()=>{
4016
+ const root = editor.model.document.getRoot();
4017
+ editor.model.change((writer)=>{
4018
+ for (const name of properties){
4019
+ if (root.hasAttribute(name)) {
4020
+ writer.removeAttribute(name, root);
4021
+ }
4022
+ }
4023
+ });
4024
+ }, {
4025
+ priority: 'high'
4026
+ });
4027
+ // Make sure that document is returned even if there is no content in the page body.
4028
+ editor.data.on('get', (evt, args)=>{
4029
+ if (!args[0]) {
4030
+ args[0] = {};
4031
+ }
4032
+ args[0].trim = false;
4033
+ }, {
4034
+ priority: 'high'
4035
+ });
4036
+ }
4037
+ }
4038
+
4039
+ export { DataFilter, DataSchema, FullPage, GeneralHtmlSupport, HtmlComment, HtmlPageDataProcessor };
4040
+ //# sourceMappingURL=index.js.map