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