@ckeditor/ckeditor5-html-support 36.0.1 → 37.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 (50) hide show
  1. package/README.md +2 -2
  2. package/build/html-support.js +1 -1
  3. package/ckeditor5-metadata.json +2 -2
  4. package/package.json +42 -36
  5. package/src/augmentation.d.ts +33 -0
  6. package/src/augmentation.js +5 -0
  7. package/src/conversionutils.d.ts +42 -0
  8. package/src/conversionutils.js +57 -77
  9. package/src/converters.d.ts +56 -0
  10. package/src/converters.js +104 -156
  11. package/src/datafilter.d.ts +250 -0
  12. package/src/datafilter.js +566 -782
  13. package/src/dataschema.d.ts +169 -0
  14. package/src/dataschema.js +143 -229
  15. package/src/fullpage.d.ts +21 -0
  16. package/src/fullpage.js +65 -86
  17. package/src/generalhtmlsupport.d.ts +88 -0
  18. package/src/generalhtmlsupport.js +244 -327
  19. package/src/generalhtmlsupportconfig.d.ts +67 -0
  20. package/src/generalhtmlsupportconfig.js +5 -0
  21. package/src/htmlcomment.d.ts +72 -0
  22. package/src/htmlcomment.js +175 -239
  23. package/src/htmlpagedataprocessor.d.ts +22 -0
  24. package/src/htmlpagedataprocessor.js +53 -76
  25. package/src/index.d.ts +25 -0
  26. package/src/index.js +1 -2
  27. package/src/integrations/codeblock.d.ts +22 -0
  28. package/src/integrations/codeblock.js +87 -115
  29. package/src/integrations/customelement.d.ts +25 -0
  30. package/src/integrations/customelement.js +127 -160
  31. package/src/integrations/documentlist.d.ts +26 -0
  32. package/src/integrations/documentlist.js +154 -191
  33. package/src/integrations/dualcontent.d.ts +44 -0
  34. package/src/integrations/dualcontent.js +92 -128
  35. package/src/integrations/heading.d.ts +25 -0
  36. package/src/integrations/heading.js +41 -54
  37. package/src/integrations/image.d.ts +25 -0
  38. package/src/integrations/image.js +154 -212
  39. package/src/integrations/integrationutils.d.ts +15 -0
  40. package/src/integrations/integrationutils.js +21 -0
  41. package/src/integrations/mediaembed.d.ts +25 -0
  42. package/src/integrations/mediaembed.js +101 -147
  43. package/src/integrations/script.d.ts +25 -0
  44. package/src/integrations/script.js +45 -67
  45. package/src/integrations/style.d.ts +25 -0
  46. package/src/integrations/style.js +45 -67
  47. package/src/integrations/table.d.ts +22 -0
  48. package/src/integrations/table.js +113 -160
  49. package/src/schemadefinitions.d.ts +13 -0
  50. package/src/schemadefinitions.js +846 -835
@@ -0,0 +1,250 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ /**
6
+ * @module html-support/datafilter
7
+ */
8
+ import { Plugin, type Editor, type PluginDependencies } from 'ckeditor5/src/core';
9
+ import { type MatcherPattern, type UpcastConversionApi, type ViewElement } from 'ckeditor5/src/engine';
10
+ import { type DataSchemaDefinition } from './dataschema';
11
+ import type { GHSViewAttributes } from './conversionutils';
12
+ import '../theme/datafilter.css';
13
+ /**
14
+ * Allows to validate elements and element attributes registered by {@link module:html-support/dataschema~DataSchema}.
15
+ *
16
+ * To enable registered element in the editor, use {@link module:html-support/datafilter~DataFilter#allowElement} method:
17
+ *
18
+ * ```ts
19
+ * dataFilter.allowElement( 'section' );
20
+ * ```
21
+ *
22
+ * You can also allow or disallow specific element attributes:
23
+ *
24
+ * ```ts
25
+ * // Allow `data-foo` attribute on `section` element.
26
+ * dataFilter.allowAttributes( {
27
+ * name: 'section',
28
+ * attributes: {
29
+ * 'data-foo': true
30
+ * }
31
+ * } );
32
+ *
33
+ * // Disallow `color` style attribute on 'section' element.
34
+ * dataFilter.disallowAttributes( {
35
+ * name: 'section',
36
+ * styles: {
37
+ * color: /[\s\S]+/
38
+ * }
39
+ * } );
40
+ * ```
41
+ *
42
+ * To apply the information about allowed and disallowed attributes in custom integration plugin,
43
+ * use the {@link module:html-support/datafilter~DataFilter#processViewAttributes `processViewAttributes()`} method.
44
+ */
45
+ export default class DataFilter extends Plugin {
46
+ /**
47
+ * An instance of the {@link module:html-support/dataschema~DataSchema}.
48
+ */
49
+ private readonly _dataSchema;
50
+ /**
51
+ * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
52
+ * content attributes should be allowed.
53
+ */
54
+ private readonly _allowedAttributes;
55
+ /**
56
+ * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
57
+ * content attributes should be disallowed.
58
+ */
59
+ private readonly _disallowedAttributes;
60
+ /**
61
+ * Allowed element definitions by {@link module:html-support/datafilter~DataFilter#allowElement} method.
62
+ */
63
+ private readonly _allowedElements;
64
+ /**
65
+ * Disallowed element names by {@link module:html-support/datafilter~DataFilter#disallowElement} method.
66
+ */
67
+ private readonly _disallowedElements;
68
+ /**
69
+ * Indicates if {@link module:engine/controller/datacontroller~DataController editor's data controller}
70
+ * data has been already initialized.
71
+ */
72
+ private _dataInitialized;
73
+ /**
74
+ * Cached map of coupled attributes. Keys are the feature attributes names
75
+ * and values are arrays with coupled GHS attributes names.
76
+ */
77
+ private _coupledAttributes;
78
+ constructor(editor: Editor);
79
+ /**
80
+ * @inheritDoc
81
+ */
82
+ static get pluginName(): 'DataFilter';
83
+ /**
84
+ * @inheritDoc
85
+ */
86
+ static get requires(): PluginDependencies;
87
+ /**
88
+ * Load a configuration of one or many elements, where their attributes should be allowed.
89
+ *
90
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
91
+ *
92
+ * @param config Configuration of elements that should have their attributes accepted in the editor.
93
+ */
94
+ loadAllowedConfig(config: Array<MatcherPattern>): void;
95
+ /**
96
+ * Load a configuration of one or many elements, where their attributes should be disallowed.
97
+ *
98
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
99
+ *
100
+ * @param config Configuration of elements that should have their attributes rejected from the editor.
101
+ */
102
+ loadDisallowedConfig(config: Array<MatcherPattern>): void;
103
+ /**
104
+ * Allow the given element in the editor context.
105
+ *
106
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
107
+ * to create data filter.
108
+ *
109
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
110
+ *
111
+ * @param viewName String or regular expression matching view name.
112
+ */
113
+ allowElement(viewName: string | RegExp): void;
114
+ /**
115
+ * Disallow the given element in the editor context.
116
+ *
117
+ * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
118
+ * to create data filter.
119
+ *
120
+ * @param viewName String or regular expression matching view name.
121
+ */
122
+ disallowElement(viewName: string | RegExp): void;
123
+ /**
124
+ * Allow the given attributes for view element allowed by {@link #allowElement} method.
125
+ *
126
+ * @param config Pattern matching all attributes which should be allowed.
127
+ */
128
+ allowAttributes(config: MatcherPattern): void;
129
+ /**
130
+ * Disallow the given attributes for view element allowed by {@link #allowElement} method.
131
+ *
132
+ * @param config Pattern matching all attributes which should be disallowed.
133
+ */
134
+ disallowAttributes(config: MatcherPattern): void;
135
+ /**
136
+ * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
137
+ *
138
+ * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
139
+ * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
140
+ * It returns the allowed attributes that were found on the given view element for further processing by integration code.
141
+ *
142
+ * ```ts
143
+ * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
144
+ * // Get rid of disallowed and extract all allowed attributes from a viewElement.
145
+ * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
146
+ * // Do something with them, i.e. store inside a model as a dictionary.
147
+ * if ( viewAttributes ) {
148
+ * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
149
+ * }
150
+ * } );
151
+ * ```
152
+ *
153
+ * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
154
+ *
155
+ * @returns Object with following properties:
156
+ * - attributes Set with matched attribute names.
157
+ * - styles Set with matched style names.
158
+ * - classes Set with matched class names.
159
+ */
160
+ processViewAttributes(viewElement: ViewElement, conversionApi: UpcastConversionApi): GHSViewAttributes | null;
161
+ /**
162
+ * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
163
+ * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
164
+ */
165
+ private _registerElementsAfterInit;
166
+ /**
167
+ * Registers default element handlers.
168
+ */
169
+ private _registerElementHandlers;
170
+ /**
171
+ * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
172
+ * are removed if a coupled feature attribute is removed.
173
+ *
174
+ * For example, consider following HTML:
175
+ *
176
+ * ```html
177
+ * <a href="foo.html" id="myId">bar</a>
178
+ * ```
179
+ *
180
+ * Which would be upcasted to following text node in the model:
181
+ *
182
+ * ```html
183
+ * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
184
+ * ```
185
+ *
186
+ * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
187
+ *
188
+ * ```html
189
+ * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
190
+ * ```
191
+ *
192
+ * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
193
+ * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
194
+ */
195
+ private _registerModelPostFixer;
196
+ /**
197
+ * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
198
+ * and coupled GHS attribute names are stored in the value array.
199
+ */
200
+ private _getCoupledAttributesMap;
201
+ /**
202
+ * Fires `register` event for the given element definition.
203
+ */
204
+ private _fireRegisterEvent;
205
+ /**
206
+ * Registers object element and attribute converters for the given data schema definition.
207
+ */
208
+ private _registerObjectElement;
209
+ /**
210
+ * Registers block element and attribute converters for the given data schema definition.
211
+ */
212
+ private _registerBlockElement;
213
+ /**
214
+ * Registers inline element and attribute converters for the given data schema definition.
215
+ *
216
+ * Extends `$text` model schema to allow the given definition model attribute and its properties.
217
+ */
218
+ private _registerInlineElement;
219
+ }
220
+ /**
221
+ * Fired when {@link module:html-support/datafilter~DataFilter} is registering element and attribute
222
+ * converters for the {@link module:html-support/dataschema~DataSchemaDefinition element definition}.
223
+ *
224
+ * The event also accepts {@link module:html-support/dataschema~DataSchemaDefinition#view} value
225
+ * as an event namespace, e.g. `register:span`.
226
+ *
227
+ * ```ts
228
+ * dataFilter.on( 'register', ( evt, definition ) => {
229
+ * editor.model.schema.register( definition.model, definition.modelSchema );
230
+ * editor.conversion.elementToElement( { model: definition.model, view: definition.view } );
231
+ *
232
+ * evt.stop();
233
+ * } );
234
+ *
235
+ * dataFilter.on( 'register:span', ( evt, definition ) => {
236
+ * editor.model.schema.extend( '$text', { allowAttributes: 'htmlSpan' } );
237
+ *
238
+ * editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'span', model: 'htmlSpan' } );
239
+ * editor.conversion.for( 'downcast' ).attributeToElement( { view: 'span', model: 'htmlSpan' } );
240
+ *
241
+ * evt.stop();
242
+ * }, { priority: 'high' } )
243
+ * ```
244
+ *
245
+ * @eventName ~DataFilter#register
246
+ */
247
+ export interface DataFilterRegisterEvent {
248
+ name: 'register' | `register:${string}`;
249
+ args: [data: DataSchemaDefinition];
250
+ }