@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
@@ -2,206 +2,169 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module html-support/integrations/documentlist
8
7
  */
9
-
10
8
  import { isEqual } from 'lodash-es';
11
9
  import { Plugin } from 'ckeditor5/src/core';
12
- import { setViewAttributes } from '../conversionutils.js';
13
-
10
+ import { setViewAttributes } from '../conversionutils';
14
11
  import DataFilter from '../datafilter';
15
-
16
12
  /**
17
13
  * Provides the General HTML Support integration with the {@link module:list/documentlist~DocumentList Document List} feature.
18
- *
19
- * @extends module:core/plugin~Plugin
20
14
  */
21
15
  export default class DocumentListElementSupport extends Plugin {
22
- /**
23
- * @inheritDoc
24
- */
25
- static get requires() {
26
- return [ DataFilter ];
27
- }
28
-
29
- /**
30
- * @inheritDoc
31
- */
32
- static get pluginName() {
33
- return 'DocumentListElementSupport';
34
- }
35
-
36
- /**
37
- * @inheritDoc
38
- */
39
- init() {
40
- const editor = this.editor;
41
-
42
- if ( !editor.plugins.has( 'DocumentListEditing' ) ) {
43
- return;
44
- }
45
-
46
- const schema = editor.model.schema;
47
- const conversion = editor.conversion;
48
- const dataFilter = editor.plugins.get( DataFilter );
49
- const documentListEditing = editor.plugins.get( 'DocumentListEditing' );
50
-
51
- // Register downcast strategy.
52
- // Note that this must be done before document list editing registers conversion in afterInit.
53
- documentListEditing.registerDowncastStrategy( {
54
- scope: 'item',
55
- attributeName: 'htmlLiAttributes',
56
-
57
- setAttributeOnDowncast( writer, attributeValue, viewElement ) {
58
- setViewAttributes( writer, attributeValue, viewElement );
59
- }
60
- } );
61
-
62
- documentListEditing.registerDowncastStrategy( {
63
- scope: 'list',
64
- attributeName: 'htmlListAttributes',
65
-
66
- setAttributeOnDowncast( writer, viewAttributes, viewElement ) {
67
- setViewAttributes( writer, viewAttributes, viewElement );
68
- }
69
- } );
70
-
71
- dataFilter.on( 'register', ( evt, definition ) => {
72
- if ( ![ 'ul', 'ol', 'li' ].includes( definition.view ) ) {
73
- return;
74
- }
75
-
76
- evt.stop();
77
-
78
- // Do not register same converters twice.
79
- if ( schema.checkAttribute( '$block', 'htmlListAttributes' ) ) {
80
- return;
81
- }
82
-
83
- schema.extend( '$block', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
84
- schema.extend( '$blockObject', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
85
- schema.extend( '$container', { allowAttributes: [ 'htmlListAttributes', 'htmlLiAttributes' ] } );
86
-
87
- conversion.for( 'upcast' ).add( dispatcher => {
88
- dispatcher.on( 'element:ul', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
89
- dispatcher.on( 'element:ol', viewToModelListAttributeConverter( 'htmlListAttributes', dataFilter ), { priority: 'low' } );
90
- dispatcher.on( 'element:li', viewToModelListAttributeConverter( 'htmlLiAttributes', dataFilter ), { priority: 'low' } );
91
- } );
92
- } );
93
-
94
- // Make sure that all items in a single list (items at the same level & listType) have the same properties.
95
- // Note: This is almost an exact copy from DocumentListPropertiesEditing.
96
- documentListEditing.on( 'postFixer', ( evt, { listNodes, writer } ) => {
97
- const previousNodesByIndent = []; // Last seen nodes of lower indented lists.
98
-
99
- for ( const { node, previous } of listNodes ) {
100
- // For the first list block there is nothing to compare with.
101
- if ( !previous ) {
102
- continue;
103
- }
104
-
105
- const nodeIndent = node.getAttribute( 'listIndent' );
106
- const previousNodeIndent = previous.getAttribute( 'listIndent' );
107
-
108
- let previousNodeInList = null; // It's like `previous` but has the same indent as current node.
109
-
110
- // Let's find previous node for the same indent.
111
- // We're going to need that when we get back to previous indent.
112
- if ( nodeIndent > previousNodeIndent ) {
113
- previousNodesByIndent[ previousNodeIndent ] = previous;
114
- }
115
- // Restore the one for given indent.
116
- else if ( nodeIndent < previousNodeIndent ) {
117
- previousNodeInList = previousNodesByIndent[ nodeIndent ];
118
- previousNodesByIndent.length = nodeIndent;
119
- }
120
- // Same indent.
121
- else {
122
- previousNodeInList = previous;
123
- }
124
-
125
- // This is a first item of a nested list.
126
- if ( !previousNodeInList ) {
127
- continue;
128
- }
129
-
130
- if ( previousNodeInList.getAttribute( 'listType' ) == node.getAttribute( 'listType' ) ) {
131
- const value = previousNodeInList.getAttribute( 'htmlListAttributes' );
132
-
133
- if ( !isEqual( node.getAttribute( 'htmlListAttributes' ), value ) ) {
134
- writer.setAttribute( 'htmlListAttributes', value, node );
135
- evt.return = true;
136
- }
137
- }
138
-
139
- if ( previousNodeInList.getAttribute( 'listItemId' ) == node.getAttribute( 'listItemId' ) ) {
140
- const value = previousNodeInList.getAttribute( 'htmlLiAttributes' );
141
-
142
- if ( !isEqual( node.getAttribute( 'htmlLiAttributes' ), value ) ) {
143
- writer.setAttribute( 'htmlLiAttributes', value, node );
144
- evt.return = true;
145
- }
146
- }
147
- }
148
- } );
149
- }
150
-
151
- /**
152
- * @inheritDoc
153
- */
154
- afterInit() {
155
- const editor = this.editor;
156
-
157
- if ( !editor.commands.get( 'indentList' ) ) {
158
- return;
159
- }
160
-
161
- // Reset list attributes after indenting list items.
162
- this.listenTo( editor.commands.get( 'indentList' ), 'afterExecute', ( evt, changedBlocks ) => {
163
- editor.model.change( writer => {
164
- for ( const node of changedBlocks ) {
165
- // Just reset the attribute.
166
- // If there is a previous indented list that this node should be merged into,
167
- // the postfixer will unify all the attributes of both sub-lists.
168
- writer.setAttribute( 'htmlListAttributes', {}, node );
169
- }
170
- } );
171
- } );
172
- }
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get requires() {
20
+ return [DataFilter];
21
+ }
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ static get pluginName() {
26
+ return 'DocumentListElementSupport';
27
+ }
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ init() {
32
+ const editor = this.editor;
33
+ if (!editor.plugins.has('DocumentListEditing')) {
34
+ return;
35
+ }
36
+ const schema = editor.model.schema;
37
+ const conversion = editor.conversion;
38
+ const dataFilter = editor.plugins.get(DataFilter);
39
+ const documentListEditing = editor.plugins.get('DocumentListEditing');
40
+ // Register downcast strategy.
41
+ // Note that this must be done before document list editing registers conversion in afterInit.
42
+ documentListEditing.registerDowncastStrategy({
43
+ scope: 'item',
44
+ attributeName: 'htmlLiAttributes',
45
+ setAttributeOnDowncast(writer, attributeValue, viewElement) {
46
+ setViewAttributes(writer, attributeValue, viewElement);
47
+ }
48
+ });
49
+ documentListEditing.registerDowncastStrategy({
50
+ scope: 'list',
51
+ attributeName: 'htmlListAttributes',
52
+ setAttributeOnDowncast(writer, viewAttributes, viewElement) {
53
+ setViewAttributes(writer, viewAttributes, viewElement);
54
+ }
55
+ });
56
+ dataFilter.on('register', (evt, definition) => {
57
+ if (!['ul', 'ol', 'li'].includes(definition.view)) {
58
+ return;
59
+ }
60
+ evt.stop();
61
+ // Do not register same converters twice.
62
+ if (schema.checkAttribute('$block', 'htmlListAttributes')) {
63
+ return;
64
+ }
65
+ schema.extend('$block', { allowAttributes: ['htmlListAttributes', 'htmlLiAttributes'] });
66
+ schema.extend('$blockObject', { allowAttributes: ['htmlListAttributes', 'htmlLiAttributes'] });
67
+ schema.extend('$container', { allowAttributes: ['htmlListAttributes', 'htmlLiAttributes'] });
68
+ conversion.for('upcast').add(dispatcher => {
69
+ dispatcher.on('element:ul', viewToModelListAttributeConverter('htmlListAttributes', dataFilter), { priority: 'low' });
70
+ dispatcher.on('element:ol', viewToModelListAttributeConverter('htmlListAttributes', dataFilter), { priority: 'low' });
71
+ dispatcher.on('element:li', viewToModelListAttributeConverter('htmlLiAttributes', dataFilter), { priority: 'low' });
72
+ });
73
+ });
74
+ // Make sure that all items in a single list (items at the same level & listType) have the same properties.
75
+ // Note: This is almost an exact copy from DocumentListPropertiesEditing.
76
+ documentListEditing.on('postFixer', (evt, { listNodes, writer }) => {
77
+ const previousNodesByIndent = []; // Last seen nodes of lower indented lists.
78
+ for (const { node, previous } of listNodes) {
79
+ // For the first list block there is nothing to compare with.
80
+ if (!previous) {
81
+ continue;
82
+ }
83
+ const nodeIndent = node.getAttribute('listIndent');
84
+ const previousNodeIndent = previous.getAttribute('listIndent');
85
+ let previousNodeInList = null; // It's like `previous` but has the same indent as current node.
86
+ // Let's find previous node for the same indent.
87
+ // We're going to need that when we get back to previous indent.
88
+ if (nodeIndent > previousNodeIndent) {
89
+ previousNodesByIndent[previousNodeIndent] = previous;
90
+ }
91
+ // Restore the one for given indent.
92
+ else if (nodeIndent < previousNodeIndent) {
93
+ previousNodeInList = previousNodesByIndent[nodeIndent];
94
+ previousNodesByIndent.length = nodeIndent;
95
+ }
96
+ // Same indent.
97
+ else {
98
+ previousNodeInList = previous;
99
+ }
100
+ // This is a first item of a nested list.
101
+ if (!previousNodeInList) {
102
+ continue;
103
+ }
104
+ if (previousNodeInList.getAttribute('listType') == node.getAttribute('listType')) {
105
+ const value = previousNodeInList.getAttribute('htmlListAttributes');
106
+ if (!isEqual(node.getAttribute('htmlListAttributes'), value)) {
107
+ writer.setAttribute('htmlListAttributes', value, node);
108
+ evt.return = true;
109
+ }
110
+ }
111
+ if (previousNodeInList.getAttribute('listItemId') == node.getAttribute('listItemId')) {
112
+ const value = previousNodeInList.getAttribute('htmlLiAttributes');
113
+ if (!isEqual(node.getAttribute('htmlLiAttributes'), value)) {
114
+ writer.setAttribute('htmlLiAttributes', value, node);
115
+ evt.return = true;
116
+ }
117
+ }
118
+ }
119
+ });
120
+ }
121
+ /**
122
+ * @inheritDoc
123
+ */
124
+ afterInit() {
125
+ const editor = this.editor;
126
+ if (!editor.commands.get('indentList')) {
127
+ return;
128
+ }
129
+ // Reset list attributes after indenting list items.
130
+ const indentList = editor.commands.get('indentList');
131
+ this.listenTo(indentList, 'afterExecute', (evt, changedBlocks) => {
132
+ editor.model.change(writer => {
133
+ for (const node of changedBlocks) {
134
+ // Just reset the attribute.
135
+ // If there is a previous indented list that this node should be merged into,
136
+ // the postfixer will unify all the attributes of both sub-lists.
137
+ writer.setAttribute('htmlListAttributes', {}, node);
138
+ }
139
+ });
140
+ });
141
+ }
173
142
  }
174
-
175
- // View-to-model conversion helper preserving allowed attributes on {@link TODO}
176
- // feature model element.
177
- //
178
- // @private
179
- // @param {String} attributeName
180
- // @param {module:html-support/datafilter~DataFilter} dataFilter
181
- // @returns {Function} Returns a conversion callback.
182
- function viewToModelListAttributeConverter( attributeName, dataFilter ) {
183
- return ( evt, data, conversionApi ) => {
184
- const viewElement = data.viewItem;
185
-
186
- if ( !data.modelRange ) {
187
- Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
188
- }
189
-
190
- const viewAttributes = dataFilter.processViewAttributes( viewElement, conversionApi );
191
-
192
- for ( const item of data.modelRange.getItems( { shallow: true } ) ) {
193
- // Apply only to list item blocks.
194
- if ( !item.hasAttribute( 'listItemId' ) ) {
195
- continue;
196
- }
197
-
198
- // Set list attributes only on same level items, those nested deeper are already handled
199
- // by the recursive conversion.
200
- if ( item.hasAttribute( attributeName ) ) {
201
- continue;
202
- }
203
-
204
- conversionApi.writer.setAttribute( attributeName, viewAttributes || {}, item );
205
- }
206
- };
143
+ /**
144
+ * View-to-model conversion helper preserving allowed attributes on {@link TODO}
145
+ * feature model element.
146
+ *
147
+ * @returns Returns a conversion callback.
148
+ */
149
+ function viewToModelListAttributeConverter(attributeName, dataFilter) {
150
+ const callback = (evt, data, conversionApi) => {
151
+ const viewElement = data.viewItem;
152
+ if (!data.modelRange) {
153
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
154
+ }
155
+ const viewAttributes = dataFilter.processViewAttributes(viewElement, conversionApi);
156
+ for (const item of data.modelRange.getItems({ shallow: true })) {
157
+ // Apply only to list item blocks.
158
+ if (!item.hasAttribute('listItemId')) {
159
+ continue;
160
+ }
161
+ // Set list attributes only on same level items, those nested deeper are already handled
162
+ // by the recursive conversion.
163
+ if (item.hasAttribute(attributeName)) {
164
+ continue;
165
+ }
166
+ conversionApi.writer.setAttribute(attributeName, viewAttributes || {}, item);
167
+ }
168
+ };
169
+ return callback;
207
170
  }
@@ -0,0 +1,44 @@
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
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
6
+ /**
7
+ * Provides the General HTML Support integration for elements which can behave like sectioning element (e.g. article) or
8
+ * element accepting only inline content (e.g. paragraph).
9
+ *
10
+ * The distinction between this two content models is important for choosing correct schema model and proper content conversion.
11
+ * As an example, it ensures that:
12
+ *
13
+ * * children elements paragraphing is enabled for sectioning elements only,
14
+ * * element and its content can be correctly handled by editing view (splitting and merging elements),
15
+ * * model element HTML is semantically correct and easier to work with.
16
+ *
17
+ * If element contains any block element, it will be treated as a sectioning element and registered using
18
+ * {@link module:html-support/dataschema~DataSchemaDefinition#model} and
19
+ * {@link module:html-support/dataschema~DataSchemaDefinition#modelSchema} in editor schema.
20
+ * Otherwise, it will be registered under {@link module:html-support/dataschema~DataSchemaBlockElementDefinition#paragraphLikeModel} model
21
+ * name with model schema accepting only inline content (inheriting from `$block`).
22
+ */
23
+ export default class DualContentModelElementSupport extends Plugin {
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ static get requires(): PluginDependencies;
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ static get pluginName(): 'DualContentModelElementSupport';
32
+ /**
33
+ * @inheritDoc
34
+ */
35
+ init(): void;
36
+ /**
37
+ * Checks whether the given view element includes any other block element.
38
+ */
39
+ private _hasBlockContent;
40
+ /**
41
+ * Adds attribute filtering conversion for the given data schema.
42
+ */
43
+ private _addAttributeConversion;
44
+ }
@@ -2,20 +2,10 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module html-support/integrations/dualcontent
8
- */
9
-
10
5
  import { Plugin } from 'ckeditor5/src/core';
11
6
  import { priorities } from 'ckeditor5/src/utils';
12
- import {
13
- modelToViewBlockAttributeConverter,
14
- viewToModelBlockAttributeConverter
15
- } from '../converters';
16
-
7
+ import { modelToViewBlockAttributeConverter, viewToModelBlockAttributeConverter } from '../converters';
17
8
  import DataFilter from '../datafilter';
18
-
19
9
  /**
20
10
  * Provides the General HTML Support integration for elements which can behave like sectioning element (e.g. article) or
21
11
  * element accepting only inline content (e.g. paragraph).
@@ -32,123 +22,97 @@ import DataFilter from '../datafilter';
32
22
  * {@link module:html-support/dataschema~DataSchemaDefinition#modelSchema} in editor schema.
33
23
  * Otherwise, it will be registered under {@link module:html-support/dataschema~DataSchemaBlockElementDefinition#paragraphLikeModel} model
34
24
  * name with model schema accepting only inline content (inheriting from `$block`).
35
- *
36
- * @extends module:core/plugin~Plugin
37
25
  */
38
26
  export default class DualContentModelElementSupport extends Plugin {
39
- /**
40
- * @inheritDoc
41
- */
42
- static get requires() {
43
- return [ DataFilter ];
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- static get pluginName() {
50
- return 'DualContentModelElementSupport';
51
- }
52
-
53
- /**
54
- * @inheritDoc
55
- */
56
- init() {
57
- const dataFilter = this.editor.plugins.get( DataFilter );
58
-
59
- dataFilter.on( 'register', ( evt, definition ) => {
60
- const editor = this.editor;
61
- const schema = editor.model.schema;
62
- const conversion = editor.conversion;
63
-
64
- if ( !definition.paragraphLikeModel ) {
65
- return;
66
- }
67
-
68
- // Can only apply to newly registered features.
69
- if ( schema.isRegistered( definition.model ) || schema.isRegistered( definition.paragraphLikeModel ) ) {
70
- return;
71
- }
72
-
73
- const paragraphLikeModelDefinition = {
74
- model: definition.paragraphLikeModel,
75
- view: definition.view
76
- };
77
-
78
- schema.register( definition.model, definition.modelSchema );
79
- schema.register( paragraphLikeModelDefinition.model, {
80
- inheritAllFrom: '$block'
81
- } );
82
-
83
- conversion.for( 'upcast' ).elementToElement( {
84
- view: definition.view,
85
- model: ( viewElement, { writer } ) => {
86
- if ( this._hasBlockContent( viewElement ) ) {
87
- return writer.createElement( definition.model );
88
- }
89
-
90
- return writer.createElement( paragraphLikeModelDefinition.model );
91
- },
92
- // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
93
- // this listener is called before it. If not, some elements will be transformed into a paragraph.
94
- converterPriority: priorities.get( 'low' ) + 1
95
- } );
96
-
97
- conversion.for( 'downcast' ).elementToElement( {
98
- view: definition.view,
99
- model: definition.model
100
- } );
101
- this._addAttributeConversion( definition );
102
-
103
- conversion.for( 'downcast' ).elementToElement( {
104
- view: paragraphLikeModelDefinition.view,
105
- model: paragraphLikeModelDefinition.model
106
- } );
107
- this._addAttributeConversion( paragraphLikeModelDefinition );
108
-
109
- evt.stop();
110
- } );
111
- }
112
-
113
- /**
114
- * Checks whether the given view element includes any other block element.
115
- *
116
- * @private
117
- * @param {module:engine/view/element~Element} viewElement
118
- * @returns {Boolean}
119
- */
120
- _hasBlockContent( viewElement ) {
121
- const view = this.editor.editing.view;
122
- const blockElements = view.domConverter.blockElements;
123
-
124
- // Traversing the viewElement subtree looking for block elements.
125
- // Especially for the cases like <div><a href="#"><p>foo</p></a></div>.
126
- // https://github.com/ckeditor/ckeditor5/issues/11513
127
- for ( const viewItem of view.createRangeIn( viewElement ).getItems() ) {
128
- if ( viewItem.is( 'element' ) && blockElements.includes( viewItem.name ) ) {
129
- return true;
130
- }
131
- }
132
-
133
- return false;
134
- }
135
-
136
- /**
137
- * Adds attribute filtering conversion for the given data schema.
138
- *
139
- * @private
140
- * @param {module:html-support/dataschema~DataSchemaBlockElementDefinition} definition
141
- */
142
- _addAttributeConversion( definition ) {
143
- const editor = this.editor;
144
- const conversion = editor.conversion;
145
- const dataFilter = editor.plugins.get( DataFilter );
146
-
147
- editor.model.schema.extend( definition.model, {
148
- allowAttributes: 'htmlAttributes'
149
- } );
150
-
151
- conversion.for( 'upcast' ).add( viewToModelBlockAttributeConverter( definition, dataFilter ) );
152
- conversion.for( 'downcast' ).add( modelToViewBlockAttributeConverter( definition ) );
153
- }
27
+ /**
28
+ * @inheritDoc
29
+ */
30
+ static get requires() {
31
+ return [DataFilter];
32
+ }
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ static get pluginName() {
37
+ return 'DualContentModelElementSupport';
38
+ }
39
+ /**
40
+ * @inheritDoc
41
+ */
42
+ init() {
43
+ const dataFilter = this.editor.plugins.get(DataFilter);
44
+ dataFilter.on('register', (evt, definition) => {
45
+ const blockDefinition = definition;
46
+ const editor = this.editor;
47
+ const schema = editor.model.schema;
48
+ const conversion = editor.conversion;
49
+ if (!blockDefinition.paragraphLikeModel) {
50
+ return;
51
+ }
52
+ // Can only apply to newly registered features.
53
+ if (schema.isRegistered(blockDefinition.model) || schema.isRegistered(blockDefinition.paragraphLikeModel)) {
54
+ return;
55
+ }
56
+ const paragraphLikeModelDefinition = {
57
+ model: blockDefinition.paragraphLikeModel,
58
+ view: blockDefinition.view
59
+ };
60
+ schema.register(blockDefinition.model, blockDefinition.modelSchema);
61
+ schema.register(paragraphLikeModelDefinition.model, {
62
+ inheritAllFrom: '$block'
63
+ });
64
+ conversion.for('upcast').elementToElement({
65
+ view: blockDefinition.view,
66
+ model: (viewElement, { writer }) => {
67
+ if (this._hasBlockContent(viewElement)) {
68
+ return writer.createElement(blockDefinition.model);
69
+ }
70
+ return writer.createElement(paragraphLikeModelDefinition.model);
71
+ },
72
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
73
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
74
+ converterPriority: priorities.get('low') + 0.5
75
+ });
76
+ conversion.for('downcast').elementToElement({
77
+ view: blockDefinition.view,
78
+ model: blockDefinition.model
79
+ });
80
+ this._addAttributeConversion(blockDefinition);
81
+ conversion.for('downcast').elementToElement({
82
+ view: paragraphLikeModelDefinition.view,
83
+ model: paragraphLikeModelDefinition.model
84
+ });
85
+ this._addAttributeConversion(paragraphLikeModelDefinition);
86
+ evt.stop();
87
+ });
88
+ }
89
+ /**
90
+ * Checks whether the given view element includes any other block element.
91
+ */
92
+ _hasBlockContent(viewElement) {
93
+ const view = this.editor.editing.view;
94
+ const blockElements = view.domConverter.blockElements;
95
+ // Traversing the viewElement subtree looking for block elements.
96
+ // Especially for the cases like <div><a href="#"><p>foo</p></a></div>.
97
+ // https://github.com/ckeditor/ckeditor5/issues/11513
98
+ for (const viewItem of view.createRangeIn(viewElement).getItems()) {
99
+ if (viewItem.is('element') && blockElements.includes(viewItem.name)) {
100
+ return true;
101
+ }
102
+ }
103
+ return false;
104
+ }
105
+ /**
106
+ * Adds attribute filtering conversion for the given data schema.
107
+ */
108
+ _addAttributeConversion(definition) {
109
+ const editor = this.editor;
110
+ const conversion = editor.conversion;
111
+ const dataFilter = editor.plugins.get(DataFilter);
112
+ editor.model.schema.extend(definition.model, {
113
+ allowAttributes: 'htmlAttributes'
114
+ });
115
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, dataFilter));
116
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
117
+ }
154
118
  }