@ckeditor/ckeditor5-html-support 47.6.1 → 48.0.0-alpha.1

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 (208) hide show
  1. package/LICENSE.md +1 -1
  2. package/ckeditor5-metadata.json +6 -6
  3. package/{src → dist}/converters.d.ts +2 -2
  4. package/{src → dist}/datafilter.d.ts +3 -3
  5. package/{src → dist}/dataschema.d.ts +2 -2
  6. package/{src → dist}/emptyblock.d.ts +1 -1
  7. package/{src → dist}/fullpage.d.ts +1 -1
  8. package/{src → dist}/generalhtmlsupport.d.ts +3 -3
  9. package/{src → dist}/generalhtmlsupportconfig.d.ts +1 -1
  10. package/{src → dist}/htmlcomment.d.ts +2 -2
  11. package/{src → dist}/htmlpagedataprocessor.d.ts +1 -1
  12. package/dist/index-editor.css +29 -29
  13. package/dist/index.css +30 -40
  14. package/dist/index.css.map +1 -1
  15. package/dist/index.js +29 -8
  16. package/dist/index.js.map +1 -1
  17. package/{src → dist}/integrations/codeblock.d.ts +1 -1
  18. package/{src → dist}/integrations/customelement.d.ts +1 -1
  19. package/{src → dist}/integrations/dualcontent.d.ts +1 -1
  20. package/{src → dist}/integrations/heading.d.ts +2 -2
  21. package/{src → dist}/integrations/horizontalline.d.ts +1 -1
  22. package/{src → dist}/integrations/iframe.d.ts +1 -1
  23. package/{src → dist}/integrations/image.d.ts +1 -1
  24. package/{src → dist}/integrations/integrationutils.d.ts +1 -1
  25. package/{src → dist}/integrations/list.d.ts +1 -1
  26. package/{src → dist}/integrations/mediaembed.d.ts +1 -1
  27. package/{src → dist}/integrations/script.d.ts +1 -1
  28. package/{src → dist}/integrations/style.d.ts +1 -1
  29. package/{src → dist}/integrations/table.d.ts +1 -1
  30. package/{src → dist}/schemadefinitions.d.ts +2 -2
  31. package/{src → dist}/utils.d.ts +1 -1
  32. package/package.json +30 -53
  33. package/build/html-support.js +0 -5
  34. package/build/translations/af.js +0 -1
  35. package/build/translations/ar.js +0 -1
  36. package/build/translations/ast.js +0 -1
  37. package/build/translations/az.js +0 -1
  38. package/build/translations/be.js +0 -1
  39. package/build/translations/bg.js +0 -1
  40. package/build/translations/bn.js +0 -1
  41. package/build/translations/bs.js +0 -1
  42. package/build/translations/ca.js +0 -1
  43. package/build/translations/cs.js +0 -1
  44. package/build/translations/da.js +0 -1
  45. package/build/translations/de-ch.js +0 -1
  46. package/build/translations/de.js +0 -1
  47. package/build/translations/el.js +0 -1
  48. package/build/translations/en-au.js +0 -1
  49. package/build/translations/en-gb.js +0 -1
  50. package/build/translations/eo.js +0 -1
  51. package/build/translations/es-co.js +0 -1
  52. package/build/translations/es.js +0 -1
  53. package/build/translations/et.js +0 -1
  54. package/build/translations/eu.js +0 -1
  55. package/build/translations/fa.js +0 -1
  56. package/build/translations/fi.js +0 -1
  57. package/build/translations/fr.js +0 -1
  58. package/build/translations/gl.js +0 -1
  59. package/build/translations/gu.js +0 -1
  60. package/build/translations/he.js +0 -1
  61. package/build/translations/hi.js +0 -1
  62. package/build/translations/hr.js +0 -1
  63. package/build/translations/hu.js +0 -1
  64. package/build/translations/hy.js +0 -1
  65. package/build/translations/id.js +0 -1
  66. package/build/translations/it.js +0 -1
  67. package/build/translations/ja.js +0 -1
  68. package/build/translations/jv.js +0 -1
  69. package/build/translations/kk.js +0 -1
  70. package/build/translations/km.js +0 -1
  71. package/build/translations/kn.js +0 -1
  72. package/build/translations/ko.js +0 -1
  73. package/build/translations/ku.js +0 -1
  74. package/build/translations/lt.js +0 -1
  75. package/build/translations/lv.js +0 -1
  76. package/build/translations/ms.js +0 -1
  77. package/build/translations/nb.js +0 -1
  78. package/build/translations/ne.js +0 -1
  79. package/build/translations/nl.js +0 -1
  80. package/build/translations/no.js +0 -1
  81. package/build/translations/oc.js +0 -1
  82. package/build/translations/pl.js +0 -1
  83. package/build/translations/pt-br.js +0 -1
  84. package/build/translations/pt.js +0 -1
  85. package/build/translations/ro.js +0 -1
  86. package/build/translations/ru.js +0 -1
  87. package/build/translations/si.js +0 -1
  88. package/build/translations/sk.js +0 -1
  89. package/build/translations/sl.js +0 -1
  90. package/build/translations/sq.js +0 -1
  91. package/build/translations/sr-latn.js +0 -1
  92. package/build/translations/sr.js +0 -1
  93. package/build/translations/sv.js +0 -1
  94. package/build/translations/th.js +0 -1
  95. package/build/translations/ti.js +0 -1
  96. package/build/translations/tk.js +0 -1
  97. package/build/translations/tr.js +0 -1
  98. package/build/translations/tt.js +0 -1
  99. package/build/translations/ug.js +0 -1
  100. package/build/translations/uk.js +0 -1
  101. package/build/translations/ur.js +0 -1
  102. package/build/translations/uz.js +0 -1
  103. package/build/translations/vi.js +0 -1
  104. package/build/translations/zh-cn.js +0 -1
  105. package/build/translations/zh.js +0 -1
  106. package/lang/contexts.json +0 -3
  107. package/lang/translations/af.po +0 -16
  108. package/lang/translations/ar.po +0 -16
  109. package/lang/translations/ast.po +0 -16
  110. package/lang/translations/az.po +0 -16
  111. package/lang/translations/be.po +0 -16
  112. package/lang/translations/bg.po +0 -16
  113. package/lang/translations/bn.po +0 -16
  114. package/lang/translations/bs.po +0 -16
  115. package/lang/translations/ca.po +0 -16
  116. package/lang/translations/cs.po +0 -16
  117. package/lang/translations/da.po +0 -16
  118. package/lang/translations/de-ch.po +0 -16
  119. package/lang/translations/de.po +0 -16
  120. package/lang/translations/el.po +0 -16
  121. package/lang/translations/en-au.po +0 -16
  122. package/lang/translations/en-gb.po +0 -16
  123. package/lang/translations/en.po +0 -16
  124. package/lang/translations/eo.po +0 -16
  125. package/lang/translations/es-co.po +0 -16
  126. package/lang/translations/es.po +0 -16
  127. package/lang/translations/et.po +0 -16
  128. package/lang/translations/eu.po +0 -16
  129. package/lang/translations/fa.po +0 -16
  130. package/lang/translations/fi.po +0 -16
  131. package/lang/translations/fr.po +0 -16
  132. package/lang/translations/gl.po +0 -16
  133. package/lang/translations/gu.po +0 -16
  134. package/lang/translations/he.po +0 -16
  135. package/lang/translations/hi.po +0 -16
  136. package/lang/translations/hr.po +0 -16
  137. package/lang/translations/hu.po +0 -16
  138. package/lang/translations/hy.po +0 -16
  139. package/lang/translations/id.po +0 -16
  140. package/lang/translations/it.po +0 -16
  141. package/lang/translations/ja.po +0 -16
  142. package/lang/translations/jv.po +0 -16
  143. package/lang/translations/kk.po +0 -16
  144. package/lang/translations/km.po +0 -16
  145. package/lang/translations/kn.po +0 -16
  146. package/lang/translations/ko.po +0 -16
  147. package/lang/translations/ku.po +0 -16
  148. package/lang/translations/lt.po +0 -16
  149. package/lang/translations/lv.po +0 -16
  150. package/lang/translations/ms.po +0 -16
  151. package/lang/translations/nb.po +0 -16
  152. package/lang/translations/ne.po +0 -16
  153. package/lang/translations/nl.po +0 -16
  154. package/lang/translations/no.po +0 -16
  155. package/lang/translations/oc.po +0 -16
  156. package/lang/translations/pl.po +0 -16
  157. package/lang/translations/pt-br.po +0 -16
  158. package/lang/translations/pt.po +0 -16
  159. package/lang/translations/ro.po +0 -16
  160. package/lang/translations/ru.po +0 -16
  161. package/lang/translations/si.po +0 -16
  162. package/lang/translations/sk.po +0 -16
  163. package/lang/translations/sl.po +0 -16
  164. package/lang/translations/sq.po +0 -16
  165. package/lang/translations/sr-latn.po +0 -16
  166. package/lang/translations/sr.po +0 -16
  167. package/lang/translations/sv.po +0 -16
  168. package/lang/translations/th.po +0 -16
  169. package/lang/translations/ti.po +0 -16
  170. package/lang/translations/tk.po +0 -16
  171. package/lang/translations/tr.po +0 -16
  172. package/lang/translations/tt.po +0 -16
  173. package/lang/translations/ug.po +0 -16
  174. package/lang/translations/uk.po +0 -16
  175. package/lang/translations/ur.po +0 -16
  176. package/lang/translations/uz.po +0 -16
  177. package/lang/translations/vi.po +0 -16
  178. package/lang/translations/zh-cn.po +0 -16
  179. package/lang/translations/zh.po +0 -16
  180. package/src/augmentation.js +0 -5
  181. package/src/converters.js +0 -190
  182. package/src/datafilter.js +0 -837
  183. package/src/dataschema.js +0 -199
  184. package/src/emptyblock.js +0 -146
  185. package/src/fullpage.js +0 -184
  186. package/src/generalhtmlsupport.js +0 -257
  187. package/src/generalhtmlsupportconfig.js +0 -5
  188. package/src/htmlcomment.js +0 -225
  189. package/src/htmlpagedataprocessor.js +0 -70
  190. package/src/index.js +0 -31
  191. package/src/integrations/codeblock.js +0 -107
  192. package/src/integrations/customelement.js +0 -166
  193. package/src/integrations/dualcontent.js +0 -126
  194. package/src/integrations/heading.js +0 -72
  195. package/src/integrations/horizontalline.js +0 -80
  196. package/src/integrations/iframe.js +0 -76
  197. package/src/integrations/image.js +0 -195
  198. package/src/integrations/integrationutils.js +0 -21
  199. package/src/integrations/list.js +0 -185
  200. package/src/integrations/mediaembed.js +0 -125
  201. package/src/integrations/script.js +0 -66
  202. package/src/integrations/style.js +0 -66
  203. package/src/integrations/table.js +0 -209
  204. package/src/schemadefinitions.js +0 -979
  205. package/src/utils.js +0 -169
  206. package/theme/datafilter.css +0 -56
  207. /package/{src → dist}/augmentation.d.ts +0 -0
  208. /package/{src → dist}/index.d.ts +0 -0
package/src/datafilter.js DELETED
@@ -1,837 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module html-support/datafilter
7
- */
8
- import { Plugin } from 'ckeditor5/src/core.js';
9
- import { Matcher, StylesMap } from 'ckeditor5/src/engine.js';
10
- import { CKEditorError, priorities, isValidAttributeName } from 'ckeditor5/src/utils.js';
11
- import { Widget } from 'ckeditor5/src/widget.js';
12
- import { viewToModelObjectConverter, toObjectWidgetConverter, createObjectView, viewToAttributeInlineConverter, attributeToViewInlineConverter, emptyInlineModelElementToViewConverter, viewToModelBlockAttributeConverter, modelToViewBlockAttributeConverter } from './converters.js';
13
- import { DataSchema } from './dataschema.js';
14
- import { getHtmlAttributeName } from './utils.js';
15
- import { isPlainObject } from 'es-toolkit/compat';
16
- import '../theme/datafilter.css';
17
- /**
18
- * Allows to validate elements and element attributes registered by {@link module:html-support/dataschema~DataSchema}.
19
- *
20
- * To enable registered element in the editor, use {@link module:html-support/datafilter~DataFilter#allowElement} method:
21
- *
22
- * ```ts
23
- * dataFilter.allowElement( 'section' );
24
- * ```
25
- *
26
- * You can also allow or disallow specific element attributes:
27
- *
28
- * ```ts
29
- * // Allow `data-foo` attribute on `section` element.
30
- * dataFilter.allowAttributes( {
31
- * name: 'section',
32
- * attributes: {
33
- * 'data-foo': true
34
- * }
35
- * } );
36
- *
37
- * // Disallow `color` style attribute on 'section' element.
38
- * dataFilter.disallowAttributes( {
39
- * name: 'section',
40
- * styles: {
41
- * color: /[\s\S]+/
42
- * }
43
- * } );
44
- * ```
45
- *
46
- * To apply the information about allowed and disallowed attributes in custom integration plugin,
47
- * use the {@link module:html-support/datafilter~DataFilter#processViewAttributes `processViewAttributes()`} method.
48
- */
49
- export class DataFilter extends Plugin {
50
- /**
51
- * An instance of the {@link module:html-support/dataschema~DataSchema}.
52
- */
53
- _dataSchema;
54
- /**
55
- * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
56
- * content attributes should be allowed.
57
- */
58
- _allowedAttributes;
59
- /**
60
- * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
61
- * content attributes should be disallowed.
62
- */
63
- _disallowedAttributes;
64
- /**
65
- * Allowed element definitions by {@link module:html-support/datafilter~DataFilter#allowElement} method.
66
- */
67
- _allowedElements;
68
- /**
69
- * Disallowed element names by {@link module:html-support/datafilter~DataFilter#disallowElement} method.
70
- */
71
- _disallowedElements;
72
- /**
73
- * Indicates if {@link module:engine/controller/datacontroller~DataController editor's data controller}
74
- * data has been already initialized.
75
- */
76
- _dataInitialized;
77
- /**
78
- * Cached map of coupled attributes. Keys are the feature attributes names
79
- * and values are arrays with coupled GHS attributes names.
80
- */
81
- _coupledAttributes;
82
- constructor(editor) {
83
- super(editor);
84
- this._dataSchema = editor.plugins.get('DataSchema');
85
- this._allowedAttributes = new Matcher();
86
- this._disallowedAttributes = new Matcher();
87
- this._allowedElements = new Set();
88
- this._disallowedElements = new Set();
89
- this._dataInitialized = false;
90
- this._coupledAttributes = null;
91
- this._registerElementsAfterInit();
92
- this._registerElementHandlers();
93
- this._registerCoupledAttributesPostFixer();
94
- this._registerAssociatedHtmlAttributesPostFixer();
95
- }
96
- /**
97
- * @inheritDoc
98
- */
99
- static get pluginName() {
100
- return 'DataFilter';
101
- }
102
- /**
103
- * @inheritDoc
104
- */
105
- static get isOfficialPlugin() {
106
- return true;
107
- }
108
- /**
109
- * @inheritDoc
110
- */
111
- static get requires() {
112
- return [DataSchema, Widget];
113
- }
114
- /**
115
- * Load a configuration of one or many elements, where their attributes should be allowed.
116
- *
117
- * **Note**: Rules will be applied just before next data pipeline data init or set.
118
- *
119
- * @param config Configuration of elements that should have their attributes accepted in the editor.
120
- */
121
- loadAllowedConfig(config) {
122
- for (const pattern of config) {
123
- // MatcherPattern allows omitting `name` to widen the search of elements.
124
- // Let's keep it consistent and match every element if a `name` has not been provided.
125
- const elementName = pattern.name || /[\s\S]+/;
126
- const rules = splitRules(pattern);
127
- this.allowElement(elementName);
128
- rules.forEach(pattern => this.allowAttributes(pattern));
129
- }
130
- }
131
- /**
132
- * Load a configuration of one or many elements, where their attributes should be disallowed.
133
- *
134
- * **Note**: Rules will be applied just before next data pipeline data init or set.
135
- *
136
- * @param config Configuration of elements that should have their attributes rejected from the editor.
137
- */
138
- loadDisallowedConfig(config) {
139
- for (const pattern of config) {
140
- // MatcherPattern allows omitting `name` to widen the search of elements.
141
- // Let's keep it consistent and match every element if a `name` has not been provided.
142
- const elementName = pattern.name || /[\s\S]+/;
143
- const rules = splitRules(pattern);
144
- // Disallow element itself if there is no other rules.
145
- if (rules.length == 0) {
146
- this.disallowElement(elementName);
147
- }
148
- else {
149
- rules.forEach(pattern => this.disallowAttributes(pattern));
150
- }
151
- }
152
- }
153
- /**
154
- * Load a configuration of one or many elements, where when empty should be allowed.
155
- *
156
- * **Note**: It modifies DataSchema so must be loaded before registering filtering rules.
157
- *
158
- * @param config Configuration of elements that should be preserved even if empty.
159
- */
160
- loadAllowedEmptyElementsConfig(config) {
161
- for (const elementName of config) {
162
- this.allowEmptyElement(elementName);
163
- }
164
- }
165
- /**
166
- * Allow the given element in the editor context.
167
- *
168
- * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
169
- * to create data filter.
170
- *
171
- * **Note**: Rules will be applied just before next data pipeline data init or set.
172
- *
173
- * @param viewName String or regular expression matching view name.
174
- */
175
- allowElement(viewName) {
176
- for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)) {
177
- this._addAllowedElement(definition);
178
- // Reset cached map to recalculate it on the next usage.
179
- this._coupledAttributes = null;
180
- }
181
- }
182
- /**
183
- * Disallow the given element in the editor context.
184
- *
185
- * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
186
- * to create data filter.
187
- *
188
- * @param viewName String or regular expression matching view name.
189
- */
190
- disallowElement(viewName) {
191
- for (const definition of this._dataSchema.getDefinitionsForView(viewName, false)) {
192
- this._disallowedElements.add(definition.view);
193
- }
194
- }
195
- /**
196
- * Allow the given empty element in the editor context.
197
- *
198
- * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
199
- * to create data filter.
200
- *
201
- * **Note**: It modifies DataSchema so must be called before registering filtering rules.
202
- *
203
- * @param viewName String or regular expression matching view name.
204
- */
205
- allowEmptyElement(viewName) {
206
- for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)) {
207
- if (definition.isInline) {
208
- this._dataSchema.extendInlineElement({ ...definition, allowEmpty: true });
209
- }
210
- }
211
- }
212
- /**
213
- * Allow the given attributes for view element allowed by {@link #allowElement} method.
214
- *
215
- * @param config Pattern matching all attributes which should be allowed.
216
- */
217
- allowAttributes(config) {
218
- this._allowedAttributes.add(config);
219
- }
220
- /**
221
- * Disallow the given attributes for view element allowed by {@link #allowElement} method.
222
- *
223
- * @param config Pattern matching all attributes which should be disallowed.
224
- */
225
- disallowAttributes(config) {
226
- this._disallowedAttributes.add(config);
227
- }
228
- /**
229
- * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
230
- *
231
- * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
232
- * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
233
- * It returns the allowed attributes that were found on the given view element for further processing by integration code.
234
- *
235
- * ```ts
236
- * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
237
- * // Get rid of disallowed and extract all allowed attributes from a viewElement.
238
- * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
239
- * // Do something with them, i.e. store inside a model as a dictionary.
240
- * if ( viewAttributes ) {
241
- * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
242
- * }
243
- * } );
244
- * ```
245
- *
246
- * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
247
- *
248
- * @returns Object with following properties:
249
- * - attributes Set with matched attribute names.
250
- * - styles Set with matched style names.
251
- * - classes Set with matched class names.
252
- */
253
- processViewAttributes(viewElement, conversionApi) {
254
- const { consumable } = conversionApi;
255
- // Make sure that the disabled attributes are handled before the allowed attributes are called.
256
- // For example, for block images the <figure> converter triggers conversion for <img> first and then for other elements, i.e. <a>.
257
- matchAndConsumeAttributes(viewElement, this._disallowedAttributes, consumable);
258
- return prepareGHSAttribute(viewElement, matchAndConsumeAttributes(viewElement, this._allowedAttributes, consumable));
259
- }
260
- /**
261
- * Adds allowed element definition and fires registration event.
262
- */
263
- _addAllowedElement(definition) {
264
- if (this._allowedElements.has(definition)) {
265
- return;
266
- }
267
- this._allowedElements.add(definition);
268
- // For attribute based integrations (table figure, document lists, etc.) register related element definitions.
269
- if ('appliesToBlock' in definition && typeof definition.appliesToBlock == 'string') {
270
- for (const relatedDefinition of this._dataSchema.getDefinitionsForModel(definition.appliesToBlock)) {
271
- if (relatedDefinition.isBlock) {
272
- this._addAllowedElement(relatedDefinition);
273
- }
274
- }
275
- }
276
- // We need to wait for all features to be initialized before we can register
277
- // element, so we can access existing features model schemas.
278
- // If the data has not been initialized yet, _registerElementsAfterInit() method will take care of
279
- // registering elements.
280
- if (this._dataInitialized) {
281
- // Defer registration to the next data pipeline data set so any disallow rules could be applied
282
- // even if added after allow rule (disallowElement).
283
- this.editor.data.once('set', () => {
284
- this._fireRegisterEvent(definition);
285
- }, {
286
- // With the highest priority listener we are able to register elements right before
287
- // running data conversion.
288
- priority: priorities.highest + 1
289
- });
290
- }
291
- }
292
- /**
293
- * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
294
- * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
295
- */
296
- _registerElementsAfterInit() {
297
- this.editor.data.on('init', () => {
298
- this._dataInitialized = true;
299
- for (const definition of this._allowedElements) {
300
- this._fireRegisterEvent(definition);
301
- }
302
- }, {
303
- // With highest priority listener we are able to register elements right before
304
- // running data conversion. Also:
305
- // * Make sure that priority is higher than the one used by `RealTimeCollaborationClient`,
306
- // as RTC is stopping event propagation.
307
- // * Make sure no other features hook into this event before GHS because otherwise the
308
- // downcast conversion (for these features) could run before GHS registered its converters
309
- // (https://github.com/ckeditor/ckeditor5/issues/11356).
310
- priority: priorities.highest + 1
311
- });
312
- }
313
- /**
314
- * Registers default element handlers.
315
- */
316
- _registerElementHandlers() {
317
- this.on('register', (evt, definition) => {
318
- const schema = this.editor.model.schema;
319
- // Object element should be only registered for new features.
320
- // If the model schema is already registered, it should be handled by
321
- // #_registerBlockElement() or #_registerObjectElement() attribute handlers.
322
- if (definition.isObject && !schema.isRegistered(definition.model)) {
323
- this._registerObjectElement(definition);
324
- }
325
- else if (definition.isBlock) {
326
- this._registerBlockElement(definition);
327
- }
328
- else if (definition.isInline) {
329
- this._registerInlineElement(definition);
330
- }
331
- else {
332
- /**
333
- * The definition cannot be handled by the data filter.
334
- *
335
- * Make sure that the registered definition is correct.
336
- *
337
- * @error data-filter-invalid-definition
338
- */
339
- throw new CKEditorError('data-filter-invalid-definition', null, definition);
340
- }
341
- evt.stop();
342
- }, { priority: 'lowest' });
343
- }
344
- /**
345
- * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
346
- * are removed if a coupled feature attribute is removed.
347
- *
348
- * For example, consider following HTML:
349
- *
350
- * ```html
351
- * <a href="foo.html" id="myId">bar</a>
352
- * ```
353
- *
354
- * Which would be upcasted to following text node in the model:
355
- *
356
- * ```html
357
- * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
358
- * ```
359
- *
360
- * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
361
- *
362
- * ```html
363
- * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
364
- * ```
365
- *
366
- * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
367
- * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
368
- */
369
- _registerCoupledAttributesPostFixer() {
370
- const model = this.editor.model;
371
- const selection = model.document.selection;
372
- model.document.registerPostFixer(writer => {
373
- const changes = model.document.differ.getChanges();
374
- let changed = false;
375
- const coupledAttributes = this._getCoupledAttributesMap();
376
- for (const change of changes) {
377
- // Handle only attribute removals.
378
- if (change.type != 'attribute' || change.attributeNewValue !== null) {
379
- continue;
380
- }
381
- // Find a list of coupled GHS attributes.
382
- const attributeKeys = coupledAttributes.get(change.attributeKey);
383
- if (!attributeKeys) {
384
- continue;
385
- }
386
- // Remove the coupled GHS attributes on the same range as the feature attribute was removed.
387
- for (const { item } of change.range.getWalker()) {
388
- for (const attributeKey of attributeKeys) {
389
- if (item.hasAttribute(attributeKey)) {
390
- writer.removeAttribute(attributeKey, item);
391
- changed = true;
392
- }
393
- }
394
- }
395
- }
396
- return changed;
397
- });
398
- this.listenTo(selection, 'change:attribute', (evt, { attributeKeys }) => {
399
- const removeAttributes = new Set();
400
- const coupledAttributes = this._getCoupledAttributesMap();
401
- for (const attributeKey of attributeKeys) {
402
- // Handle only attribute removals.
403
- if (selection.hasAttribute(attributeKey)) {
404
- continue;
405
- }
406
- // Find a list of coupled GHS attributes.
407
- const coupledAttributeKeys = coupledAttributes.get(attributeKey);
408
- if (!coupledAttributeKeys) {
409
- continue;
410
- }
411
- for (const coupledAttributeKey of coupledAttributeKeys) {
412
- if (selection.hasAttribute(coupledAttributeKey)) {
413
- removeAttributes.add(coupledAttributeKey);
414
- }
415
- }
416
- }
417
- if (removeAttributes.size == 0) {
418
- return;
419
- }
420
- model.change(writer => {
421
- for (const attributeKey of removeAttributes) {
422
- writer.removeSelectionAttribute(attributeKey);
423
- }
424
- });
425
- });
426
- }
427
- /**
428
- * Removes `html*Attributes` attributes from incompatible elements.
429
- *
430
- * For example, consider the following HTML:
431
- *
432
- * ```html
433
- * <heading2 htmlH2Attributes="...">foobar[]</heading2>
434
- * ```
435
- *
436
- * Pressing `enter` creates a new `paragraph` element that inherits
437
- * the `htmlH2Attributes` attribute from `heading2`.
438
- *
439
- * ```html
440
- * <heading2 htmlH2Attributes="...">foobar</heading2>
441
- * <paragraph htmlH2Attributes="...">[]</paragraph>
442
- * ```
443
- *
444
- * This postfixer ensures that this doesn't happen, and that elements can
445
- * only have `html*Attributes` associated with them,
446
- * e.g.: `htmlPAttributes` for `<p>`, `htmlDivAttributes` for `<div>`, etc.
447
- *
448
- * With it enabled, pressing `enter` at the end of `<heading2>` will create
449
- * a new paragraph without the `htmlH2Attributes` attribute.
450
- *
451
- * ```html
452
- * <heading2 htmlH2Attributes="...">foobar</heading2>
453
- * <paragraph>[]</paragraph>
454
- * ```
455
- */
456
- _registerAssociatedHtmlAttributesPostFixer() {
457
- const model = this.editor.model;
458
- model.document.registerPostFixer(writer => {
459
- const changes = model.document.differ.getChanges();
460
- let changed = false;
461
- for (const change of changes) {
462
- if (change.type !== 'insert' || change.name === '$text') {
463
- continue;
464
- }
465
- for (const attr of change.attributes.keys()) {
466
- if (!attr.startsWith('html') || !attr.endsWith('Attributes')) {
467
- continue;
468
- }
469
- if (!model.schema.checkAttribute(change.name, attr)) {
470
- writer.removeAttribute(attr, change.position.nodeAfter);
471
- changed = true;
472
- }
473
- }
474
- }
475
- return changed;
476
- });
477
- }
478
- /**
479
- * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
480
- * and coupled GHS attribute names are stored in the value array.
481
- */
482
- _getCoupledAttributesMap() {
483
- if (this._coupledAttributes) {
484
- return this._coupledAttributes;
485
- }
486
- this._coupledAttributes = new Map();
487
- for (const definition of this._allowedElements) {
488
- if (definition.coupledAttribute && definition.model) {
489
- const attributeNames = this._coupledAttributes.get(definition.coupledAttribute);
490
- if (attributeNames) {
491
- attributeNames.push(definition.model);
492
- }
493
- else {
494
- this._coupledAttributes.set(definition.coupledAttribute, [definition.model]);
495
- }
496
- }
497
- }
498
- return this._coupledAttributes;
499
- }
500
- /**
501
- * Fires `register` event for the given element definition.
502
- */
503
- _fireRegisterEvent(definition) {
504
- if (definition.view && this._disallowedElements.has(definition.view)) {
505
- return;
506
- }
507
- this.fire(definition.view ? `register:${definition.view}` : 'register', definition);
508
- }
509
- /**
510
- * Registers object element and attribute converters for the given data schema definition.
511
- */
512
- _registerObjectElement(definition) {
513
- const editor = this.editor;
514
- const schema = editor.model.schema;
515
- const conversion = editor.conversion;
516
- const { view: viewName, model: modelName } = definition;
517
- schema.register(modelName, definition.modelSchema);
518
- /* istanbul ignore next: paranoid check -- @preserve */
519
- if (!viewName) {
520
- return;
521
- }
522
- schema.extend(definition.model, {
523
- allowAttributes: [getHtmlAttributeName(viewName), 'htmlContent']
524
- });
525
- // Store element content in special `$rawContent` custom property to
526
- // avoid editor's data filtering mechanism.
527
- editor.data.registerRawContentMatcher({
528
- name: viewName
529
- });
530
- conversion.for('upcast').elementToElement({
531
- view: viewName,
532
- model: viewToModelObjectConverter(definition),
533
- // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
534
- // this listener is called before it. If not, some elements will be transformed into a paragraph.
535
- // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
536
- converterPriority: priorities.low + 2
537
- });
538
- conversion.for('upcast')
539
- .add(viewToModelBlockAttributeConverter(definition, this));
540
- conversion.for('editingDowncast').elementToStructure({
541
- model: {
542
- name: modelName,
543
- attributes: [getHtmlAttributeName(viewName)]
544
- },
545
- view: toObjectWidgetConverter(editor, definition)
546
- });
547
- conversion.for('dataDowncast').elementToElement({
548
- model: modelName,
549
- view: (modelElement, { writer }) => {
550
- return createObjectView(viewName, modelElement, writer);
551
- }
552
- });
553
- conversion.for('dataDowncast')
554
- .add(modelToViewBlockAttributeConverter(definition));
555
- }
556
- /**
557
- * Registers block element and attribute converters for the given data schema definition.
558
- */
559
- _registerBlockElement(definition) {
560
- const editor = this.editor;
561
- const schema = editor.model.schema;
562
- const conversion = editor.conversion;
563
- const { view: viewName, model: modelName } = definition;
564
- if (!schema.isRegistered(definition.model)) {
565
- // Do not register converters and empty schema for editor existing feature
566
- // as empty schema won't allow element anywhere in the model.
567
- if (!definition.modelSchema) {
568
- return;
569
- }
570
- schema.register(definition.model, definition.modelSchema);
571
- if (!viewName) {
572
- return;
573
- }
574
- conversion.for('upcast').elementToElement({
575
- model: modelName,
576
- view: viewName,
577
- // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
578
- // this listener is called before it. If not, some elements will be transformed into a paragraph.
579
- // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
580
- converterPriority: priorities.low + 2
581
- });
582
- conversion.for('downcast').elementToElement({
583
- model: modelName,
584
- view: (modelElement, { writer }) => definition.isEmpty ?
585
- writer.createEmptyElement(viewName) :
586
- writer.createContainerElement(viewName)
587
- });
588
- }
589
- if (!viewName) {
590
- return;
591
- }
592
- schema.extend(definition.model, {
593
- allowAttributes: getHtmlAttributeName(viewName)
594
- });
595
- conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
596
- conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
597
- }
598
- /**
599
- * Registers inline element and attribute converters for the given data schema definition.
600
- *
601
- * Extends `$text` model schema to allow the given definition model attribute and its properties.
602
- */
603
- _registerInlineElement(definition) {
604
- const editor = this.editor;
605
- const schema = editor.model.schema;
606
- const conversion = editor.conversion;
607
- const attributeKey = definition.model;
608
- // This element is stored in the model as an attribute on a block element, for example Lists.
609
- if (definition.appliesToBlock) {
610
- return;
611
- }
612
- schema.extend('$text', {
613
- allowAttributes: attributeKey
614
- });
615
- if (definition.attributeProperties) {
616
- schema.setAttributeProperties(attributeKey, definition.attributeProperties);
617
- }
618
- conversion.for('upcast').add(viewToAttributeInlineConverter(definition, this));
619
- conversion.for('downcast').attributeToElement({
620
- model: attributeKey,
621
- view: attributeToViewInlineConverter(definition)
622
- });
623
- if (!definition.allowEmpty) {
624
- return;
625
- }
626
- schema.setAttributeProperties(attributeKey, { copyFromObject: false });
627
- if (!schema.isRegistered('htmlEmptyElement')) {
628
- schema.register('htmlEmptyElement', {
629
- inheritAllFrom: '$inlineObject'
630
- });
631
- // Helper function to check if an element has any HTML attributes.
632
- const hasHtmlAttributes = (element) => Array
633
- .from(element.getAttributeKeys())
634
- .some(key => key.startsWith('html'));
635
- // Register a post-fixer that removes htmlEmptyElement when its htmlXX attribute is removed.
636
- // See: https://github.com/ckeditor/ckeditor5/issues/18089
637
- editor.model.document.registerPostFixer(writer => {
638
- const changes = editor.model.document.differ.getChanges();
639
- const elementsToRemove = new Set();
640
- for (const change of changes) {
641
- if (change.type === 'remove') {
642
- continue;
643
- }
644
- // Look for removal of html* attributes.
645
- if (change.type === 'attribute' && change.attributeNewValue === null) {
646
- // Find htmlEmptyElement instances in the range that lost their html attribute.
647
- for (const { item } of change.range) {
648
- if (item.is('element', 'htmlEmptyElement') && !hasHtmlAttributes(item)) {
649
- elementsToRemove.add(item);
650
- }
651
- }
652
- }
653
- // Look for insertion of htmlEmptyElement.
654
- if (change.type === 'insert' && change.position.nodeAfter) {
655
- const insertedElement = change.position.nodeAfter;
656
- for (const { item } of writer.createRangeOn(insertedElement)) {
657
- if (item.is('element', 'htmlEmptyElement') && !hasHtmlAttributes(item)) {
658
- elementsToRemove.add(item);
659
- }
660
- }
661
- }
662
- }
663
- for (const element of elementsToRemove) {
664
- writer.remove(element);
665
- }
666
- return elementsToRemove.size > 0;
667
- });
668
- }
669
- editor.data.htmlProcessor.domConverter.registerInlineObjectMatcher(element => {
670
- // Element must be empty and have any attribute.
671
- if (element.name == definition.view &&
672
- element.isEmpty &&
673
- Array.from(element.getAttributeKeys()).length) {
674
- return {
675
- name: true
676
- };
677
- }
678
- return null;
679
- });
680
- conversion.for('editingDowncast')
681
- .elementToElement({
682
- model: 'htmlEmptyElement',
683
- view: emptyInlineModelElementToViewConverter(definition, true)
684
- });
685
- conversion.for('dataDowncast')
686
- .elementToElement({
687
- model: 'htmlEmptyElement',
688
- view: emptyInlineModelElementToViewConverter(definition)
689
- });
690
- }
691
- }
692
- /**
693
- * Matches and consumes matched attributes.
694
- *
695
- * @returns Object with following properties:
696
- * - attributes Array with matched attribute names.
697
- * - classes Array with matched class names.
698
- * - styles Array with matched style names.
699
- */
700
- function matchAndConsumeAttributes(viewElement, matcher, consumable) {
701
- const matches = matcher.matchAll(viewElement) || [];
702
- const stylesProcessor = viewElement.document.stylesProcessor;
703
- return matches.reduce((result, { match }) => {
704
- for (const [key, token] of match.attributes || []) {
705
- // Verify and consume styles.
706
- if (key == 'style') {
707
- const style = token;
708
- // Check longer forms of the same style as those could be matched
709
- // but not present in the element directly.
710
- // Consider only longhand (or longer than current notation) so that
711
- // we do not include all sides of the box if only one side is allowed.
712
- const sortedRelatedStyles = stylesProcessor.getRelatedStyles(style)
713
- .filter(relatedStyle => relatedStyle.split('-').length > style.split('-').length)
714
- .sort((a, b) => b.split('-').length - a.split('-').length);
715
- for (const relatedStyle of sortedRelatedStyles) {
716
- if (consumable.consume(viewElement, { styles: [relatedStyle] })) {
717
- result.styles.push(relatedStyle);
718
- }
719
- }
720
- // Verify and consume style as specified in the matcher.
721
- if (consumable.consume(viewElement, { styles: [style] })) {
722
- result.styles.push(style);
723
- }
724
- }
725
- // Verify and consume class names.
726
- else if (key == 'class') {
727
- const className = token;
728
- if (consumable.consume(viewElement, { classes: [className] })) {
729
- result.classes.push(className);
730
- }
731
- }
732
- else {
733
- // Verify and consume other attributes.
734
- if (consumable.consume(viewElement, { attributes: [key] })) {
735
- result.attributes.push(key);
736
- }
737
- }
738
- }
739
- return result;
740
- }, {
741
- attributes: [],
742
- classes: [],
743
- styles: []
744
- });
745
- }
746
- /**
747
- * Prepares the GHS attribute value as an object with element attributes' values.
748
- */
749
- function prepareGHSAttribute(viewElement, { attributes, classes, styles }) {
750
- if (!attributes.length && !classes.length && !styles.length) {
751
- return null;
752
- }
753
- return {
754
- ...(attributes.length && {
755
- attributes: getAttributes(viewElement, attributes)
756
- }),
757
- ...(styles.length && {
758
- styles: getReducedStyles(viewElement, styles)
759
- }),
760
- ...(classes.length && {
761
- classes
762
- })
763
- };
764
- }
765
- /**
766
- * Returns attributes as an object with names and values.
767
- */
768
- function getAttributes(viewElement, attributes) {
769
- const attributesObject = {};
770
- for (const key of attributes) {
771
- const value = viewElement.getAttribute(key);
772
- if (value !== undefined && isValidAttributeName(key)) {
773
- attributesObject[key] = value;
774
- }
775
- }
776
- return attributesObject;
777
- }
778
- /**
779
- * Returns styles as an object reduced to shorthand notation without redundant entries.
780
- */
781
- function getReducedStyles(viewElement, styles) {
782
- // Use StyleMap to reduce style value to the minimal form (without shorthand and long-hand notation and duplication).
783
- const stylesMap = new StylesMap(viewElement.document.stylesProcessor);
784
- for (const key of styles) {
785
- const styleValue = viewElement.getStyle(key);
786
- if (styleValue !== undefined) {
787
- stylesMap.set(key, styleValue);
788
- }
789
- }
790
- return Object.fromEntries(stylesMap.getStylesEntries());
791
- }
792
- /**
793
- * Matcher by default has to match **all** patterns to count it as an actual match. Splitting the pattern
794
- * into separate patterns means that any matched pattern will be count as a match.
795
- *
796
- * @param pattern Pattern to split.
797
- * @param attributeName Name of the attribute to split (e.g. 'attributes', 'classes', 'styles').
798
- */
799
- function splitPattern(pattern, attributeName) {
800
- const { name } = pattern;
801
- const attributeValue = pattern[attributeName];
802
- if (isPlainObject(attributeValue)) {
803
- return Object.entries(attributeValue)
804
- .map(([key, value]) => ({
805
- name,
806
- [attributeName]: {
807
- [key]: value
808
- }
809
- }));
810
- }
811
- if (Array.isArray(attributeValue)) {
812
- return attributeValue
813
- .map(value => ({
814
- name,
815
- [attributeName]: [value]
816
- }));
817
- }
818
- return [pattern];
819
- }
820
- /**
821
- * Rules are matched in conjunction (AND operation), but we want to have a match if *any* of the rules is matched (OR operation).
822
- * By splitting the rules we force the latter effect.
823
- */
824
- function splitRules(rules) {
825
- const { name, attributes, classes, styles } = rules;
826
- const splitRules = [];
827
- if (attributes) {
828
- splitRules.push(...splitPattern({ name, attributes }, 'attributes'));
829
- }
830
- if (classes) {
831
- splitRules.push(...splitPattern({ name, classes }, 'classes'));
832
- }
833
- if (styles) {
834
- splitRules.push(...splitPattern({ name, styles }, 'styles'));
835
- }
836
- return splitRules;
837
- }