@ckeditor/ckeditor5-html-support 39.0.1 → 40.0.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 (97) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +3 -3
  3. package/build/html-support.js +1 -1
  4. package/build/html-support.js.map +1 -0
  5. package/lang/translations/ar.po +1 -0
  6. package/lang/translations/bg.po +1 -0
  7. package/lang/translations/bn.po +1 -0
  8. package/lang/translations/ca.po +1 -0
  9. package/lang/translations/cs.po +1 -0
  10. package/lang/translations/da.po +1 -0
  11. package/lang/translations/de.po +1 -0
  12. package/lang/translations/el.po +1 -0
  13. package/lang/translations/en-au.po +1 -0
  14. package/lang/translations/en.po +1 -0
  15. package/lang/translations/es.po +1 -0
  16. package/lang/translations/et.po +1 -0
  17. package/lang/translations/fi.po +1 -0
  18. package/lang/translations/fr.po +1 -0
  19. package/lang/translations/gl.po +1 -0
  20. package/lang/translations/he.po +1 -0
  21. package/lang/translations/hi.po +1 -0
  22. package/lang/translations/hr.po +1 -0
  23. package/lang/translations/hu.po +1 -0
  24. package/lang/translations/id.po +1 -0
  25. package/lang/translations/it.po +1 -0
  26. package/lang/translations/ja.po +1 -0
  27. package/lang/translations/jv.po +1 -0
  28. package/lang/translations/ko.po +1 -0
  29. package/lang/translations/lt.po +1 -0
  30. package/lang/translations/lv.po +1 -0
  31. package/lang/translations/ms.po +1 -0
  32. package/lang/translations/nl.po +1 -0
  33. package/lang/translations/no.po +1 -0
  34. package/lang/translations/pl.po +1 -0
  35. package/lang/translations/pt-br.po +1 -0
  36. package/lang/translations/pt.po +1 -0
  37. package/lang/translations/ro.po +1 -0
  38. package/lang/translations/ru.po +1 -0
  39. package/lang/translations/sk.po +1 -0
  40. package/lang/translations/sr-latn.po +1 -0
  41. package/lang/translations/sr.po +1 -0
  42. package/lang/translations/sv.po +1 -0
  43. package/lang/translations/th.po +1 -0
  44. package/lang/translations/tr.po +1 -0
  45. package/lang/translations/ug.po +1 -0
  46. package/lang/translations/uk.po +1 -0
  47. package/lang/translations/ur.po +1 -0
  48. package/lang/translations/vi.po +1 -0
  49. package/lang/translations/zh-cn.po +1 -0
  50. package/lang/translations/zh.po +1 -0
  51. package/package.json +2 -6
  52. package/src/augmentation.d.ts +33 -33
  53. package/src/augmentation.js +5 -5
  54. package/src/converters.d.ts +60 -60
  55. package/src/converters.js +180 -180
  56. package/src/datafilter.d.ts +304 -304
  57. package/src/datafilter.js +720 -720
  58. package/src/dataschema.d.ts +183 -183
  59. package/src/dataschema.js +196 -196
  60. package/src/fullpage.d.ts +21 -21
  61. package/src/fullpage.js +80 -80
  62. package/src/generalhtmlsupport.d.ts +98 -98
  63. package/src/generalhtmlsupport.js +240 -240
  64. package/src/generalhtmlsupportconfig.d.ts +77 -77
  65. package/src/generalhtmlsupportconfig.js +5 -5
  66. package/src/htmlcomment.d.ts +71 -71
  67. package/src/htmlcomment.js +218 -218
  68. package/src/htmlpagedataprocessor.d.ts +22 -22
  69. package/src/htmlpagedataprocessor.js +67 -67
  70. package/src/index.d.ts +25 -25
  71. package/src/index.js +14 -14
  72. package/src/integrations/codeblock.d.ts +23 -23
  73. package/src/integrations/codeblock.js +101 -101
  74. package/src/integrations/customelement.d.ts +27 -27
  75. package/src/integrations/customelement.js +146 -146
  76. package/src/integrations/documentlist.d.ts +27 -27
  77. package/src/integrations/documentlist.js +178 -203
  78. package/src/integrations/dualcontent.d.ts +45 -45
  79. package/src/integrations/dualcontent.js +119 -119
  80. package/src/integrations/heading.d.ts +31 -31
  81. package/src/integrations/heading.js +60 -60
  82. package/src/integrations/image.d.ts +26 -26
  83. package/src/integrations/image.js +189 -189
  84. package/src/integrations/integrationutils.d.ts +15 -15
  85. package/src/integrations/integrationutils.js +21 -21
  86. package/src/integrations/mediaembed.d.ts +26 -26
  87. package/src/integrations/mediaembed.js +119 -119
  88. package/src/integrations/script.d.ts +26 -26
  89. package/src/integrations/script.js +59 -59
  90. package/src/integrations/style.d.ts +26 -26
  91. package/src/integrations/style.js +59 -59
  92. package/src/integrations/table.d.ts +23 -23
  93. package/src/integrations/table.js +163 -163
  94. package/src/schemadefinitions.d.ts +13 -13
  95. package/src/schemadefinitions.js +956 -956
  96. package/src/utils.d.ts +72 -72
  97. package/src/utils.js +139 -139
@@ -1,60 +1,60 @@
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/converters
7
- */
8
- import type { Editor } from 'ckeditor5/src/core';
9
- import type { AttributeElement, DowncastConversionApi, DowncastDispatcher, DowncastWriter, Element, ElementCreatorFunction, UpcastConversionApi, UpcastDispatcher, ViewElement } from 'ckeditor5/src/engine';
10
- import type DataFilter from './datafilter';
11
- import type { DataSchemaBlockElementDefinition, DataSchemaDefinition, DataSchemaInlineElementDefinition } from './dataschema';
12
- /**
13
- * View-to-model conversion helper for object elements.
14
- *
15
- * Preserves object element content in `htmlContent` attribute.
16
- *
17
- * @returns Returns a conversion callback.
18
- */
19
- export declare function viewToModelObjectConverter({ model: modelName }: DataSchemaDefinition): (viewElement: ViewElement, conversionApi: UpcastConversionApi) => Element;
20
- /**
21
- * Conversion helper converting an object element to an HTML object widget.
22
- *
23
- * @returns Returns a conversion callback.
24
- */
25
- export declare function toObjectWidgetConverter(editor: Editor, { view: viewName, isInline }: DataSchemaInlineElementDefinition): ElementCreatorFunction;
26
- /**
27
- * Creates object view element from the given model element.
28
- */
29
- export declare function createObjectView(viewName: string, modelElement: Element, writer: DowncastWriter): ViewElement;
30
- /**
31
- * View-to-attribute conversion helper preserving inline element attributes on `$text`.
32
- *
33
- * @returns Returns a conversion callback.
34
- */
35
- export declare function viewToAttributeInlineConverter({ view: viewName, model: attributeKey, allowEmpty }: DataSchemaInlineElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
36
- /**
37
- * Conversion helper converting an empty inline model element to an HTML element or widget.
38
- */
39
- export declare function emptyInlineModelElementToViewConverter({ model: attributeKey, view: viewName }: DataSchemaInlineElementDefinition, asWidget?: boolean): ElementCreatorFunction;
40
- /**
41
- * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
42
- *
43
- * @returns Returns a conversion callback.
44
- */
45
- export declare function attributeToViewInlineConverter({ priority, view: viewName }: DataSchemaInlineElementDefinition): (attributeValue: any, conversionApi: DowncastConversionApi) => AttributeElement | undefined;
46
- /**
47
- * View-to-model conversion helper preserving allowed attributes on block element.
48
- *
49
- * All matched attributes will be preserved on `html*Attributes` attribute.
50
- *
51
- * @returns Returns a conversion callback.
52
- */
53
- export declare function viewToModelBlockAttributeConverter({ view: viewName }: DataSchemaBlockElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
54
- /**
55
- * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
56
- * for block elements.
57
- *
58
- * @returns Returns a conversion callback.
59
- */
60
- export declare function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }: DataSchemaBlockElementDefinition): (dispatcher: DowncastDispatcher) => void;
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/converters
7
+ */
8
+ import type { Editor } from 'ckeditor5/src/core';
9
+ import type { AttributeElement, DowncastConversionApi, DowncastDispatcher, DowncastWriter, Element, ElementCreatorFunction, UpcastConversionApi, UpcastDispatcher, ViewElement } from 'ckeditor5/src/engine';
10
+ import type DataFilter from './datafilter';
11
+ import type { DataSchemaBlockElementDefinition, DataSchemaDefinition, DataSchemaInlineElementDefinition } from './dataschema';
12
+ /**
13
+ * View-to-model conversion helper for object elements.
14
+ *
15
+ * Preserves object element content in `htmlContent` attribute.
16
+ *
17
+ * @returns Returns a conversion callback.
18
+ */
19
+ export declare function viewToModelObjectConverter({ model: modelName }: DataSchemaDefinition): (viewElement: ViewElement, conversionApi: UpcastConversionApi) => Element;
20
+ /**
21
+ * Conversion helper converting an object element to an HTML object widget.
22
+ *
23
+ * @returns Returns a conversion callback.
24
+ */
25
+ export declare function toObjectWidgetConverter(editor: Editor, { view: viewName, isInline }: DataSchemaInlineElementDefinition): ElementCreatorFunction;
26
+ /**
27
+ * Creates object view element from the given model element.
28
+ */
29
+ export declare function createObjectView(viewName: string, modelElement: Element, writer: DowncastWriter): ViewElement;
30
+ /**
31
+ * View-to-attribute conversion helper preserving inline element attributes on `$text`.
32
+ *
33
+ * @returns Returns a conversion callback.
34
+ */
35
+ export declare function viewToAttributeInlineConverter({ view: viewName, model: attributeKey, allowEmpty }: DataSchemaInlineElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
36
+ /**
37
+ * Conversion helper converting an empty inline model element to an HTML element or widget.
38
+ */
39
+ export declare function emptyInlineModelElementToViewConverter({ model: attributeKey, view: viewName }: DataSchemaInlineElementDefinition, asWidget?: boolean): ElementCreatorFunction;
40
+ /**
41
+ * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
42
+ *
43
+ * @returns Returns a conversion callback.
44
+ */
45
+ export declare function attributeToViewInlineConverter({ priority, view: viewName }: DataSchemaInlineElementDefinition): (attributeValue: any, conversionApi: DowncastConversionApi) => AttributeElement | undefined;
46
+ /**
47
+ * View-to-model conversion helper preserving allowed attributes on block element.
48
+ *
49
+ * All matched attributes will be preserved on `html*Attributes` attribute.
50
+ *
51
+ * @returns Returns a conversion callback.
52
+ */
53
+ export declare function viewToModelBlockAttributeConverter({ view: viewName }: DataSchemaBlockElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
54
+ /**
55
+ * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
56
+ * for block elements.
57
+ *
58
+ * @returns Returns a conversion callback.
59
+ */
60
+ export declare function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }: DataSchemaBlockElementDefinition): (dispatcher: DowncastDispatcher) => void;
package/src/converters.js CHANGED
@@ -1,180 +1,180 @@
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 { toWidget } from 'ckeditor5/src/widget';
6
- import { setViewAttributes, mergeViewElementAttributes, updateViewAttributes, getHtmlAttributeName } from './utils';
7
- /**
8
- * View-to-model conversion helper for object elements.
9
- *
10
- * Preserves object element content in `htmlContent` attribute.
11
- *
12
- * @returns Returns a conversion callback.
13
- */
14
- export function viewToModelObjectConverter({ model: modelName }) {
15
- return (viewElement, conversionApi) => {
16
- // Let's keep element HTML and its attributes, so we can rebuild element in downcast conversions.
17
- return conversionApi.writer.createElement(modelName, {
18
- htmlContent: viewElement.getCustomProperty('$rawContent')
19
- });
20
- };
21
- }
22
- /**
23
- * Conversion helper converting an object element to an HTML object widget.
24
- *
25
- * @returns Returns a conversion callback.
26
- */
27
- export function toObjectWidgetConverter(editor, { view: viewName, isInline }) {
28
- const t = editor.t;
29
- return (modelElement, { writer }) => {
30
- const widgetLabel = t('HTML object');
31
- const viewElement = createObjectView(viewName, modelElement, writer);
32
- const viewAttributes = modelElement.getAttribute(getHtmlAttributeName(viewName));
33
- writer.addClass('html-object-embed__content', viewElement);
34
- if (viewAttributes) {
35
- setViewAttributes(writer, viewAttributes, viewElement);
36
- }
37
- // Widget cannot be a raw element because the widget system would not be able
38
- // to add its UI to it. Thus, we need separate view container.
39
- const viewContainer = writer.createContainerElement(isInline ? 'span' : 'div', {
40
- class: 'html-object-embed',
41
- 'data-html-object-embed-label': widgetLabel
42
- }, viewElement);
43
- return toWidget(viewContainer, writer, { label: widgetLabel });
44
- };
45
- }
46
- /**
47
- * Creates object view element from the given model element.
48
- */
49
- export function createObjectView(viewName, modelElement, writer) {
50
- return writer.createRawElement(viewName, null, (domElement, domConverter) => {
51
- domConverter.setContentOf(domElement, modelElement.getAttribute('htmlContent'));
52
- });
53
- }
54
- /**
55
- * View-to-attribute conversion helper preserving inline element attributes on `$text`.
56
- *
57
- * @returns Returns a conversion callback.
58
- */
59
- export function viewToAttributeInlineConverter({ view: viewName, model: attributeKey, allowEmpty }, dataFilter) {
60
- return dispatcher => {
61
- dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
62
- let viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
63
- // Do not apply the attribute if the element itself is already consumed and there are no view attributes to store.
64
- if (!viewAttributes && !conversionApi.consumable.test(data.viewItem, { name: true })) {
65
- return;
66
- }
67
- // Otherwise, we might need to convert it to an empty object just to preserve element itself,
68
- // for example `<cite>` => <$text htmlCite="{}">.
69
- viewAttributes = viewAttributes || {};
70
- // Consume the element itself if it wasn't consumed by any other converter.
71
- conversionApi.consumable.consume(data.viewItem, { name: true });
72
- // Since we are converting to attribute we need a range on which we will set the attribute.
73
- // If the range is not created yet, we will create it.
74
- if (!data.modelRange) {
75
- data = Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
76
- }
77
- // Convert empty inline element if allowed and has any attributes.
78
- if (allowEmpty && data.modelRange.isCollapsed && Object.keys(viewAttributes).length) {
79
- const modelElement = conversionApi.writer.createElement('htmlEmptyElement');
80
- if (!conversionApi.safeInsert(modelElement, data.modelCursor)) {
81
- return;
82
- }
83
- const parts = conversionApi.getSplitParts(modelElement);
84
- data.modelRange = conversionApi.writer.createRange(data.modelRange.start, conversionApi.writer.createPositionAfter(parts[parts.length - 1]));
85
- conversionApi.updateConversionResult(modelElement, data);
86
- setAttributeOnItem(modelElement, viewAttributes, conversionApi);
87
- return;
88
- }
89
- // Set attribute on each item in range according to the schema.
90
- for (const node of data.modelRange.getItems()) {
91
- setAttributeOnItem(node, viewAttributes, conversionApi);
92
- }
93
- }, { priority: 'low' });
94
- };
95
- function setAttributeOnItem(node, viewAttributes, conversionApi) {
96
- if (conversionApi.schema.checkAttribute(node, attributeKey)) {
97
- // Node's children are converted recursively, so node can already include model attribute.
98
- // We want to extend it, not replace.
99
- const nodeAttributes = node.getAttribute(attributeKey);
100
- const attributesToAdd = mergeViewElementAttributes(viewAttributes, nodeAttributes || {});
101
- conversionApi.writer.setAttribute(attributeKey, attributesToAdd, node);
102
- }
103
- }
104
- }
105
- /**
106
- * Conversion helper converting an empty inline model element to an HTML element or widget.
107
- */
108
- export function emptyInlineModelElementToViewConverter({ model: attributeKey, view: viewName }, asWidget) {
109
- return (item, { writer, consumable }) => {
110
- if (!item.hasAttribute(attributeKey)) {
111
- return null;
112
- }
113
- const viewElement = writer.createContainerElement(viewName);
114
- const attributeValue = item.getAttribute(attributeKey);
115
- consumable.consume(item, `attribute:${attributeKey}`);
116
- setViewAttributes(writer, attributeValue, viewElement);
117
- viewElement.getFillerOffset = () => null;
118
- return asWidget ? toWidget(viewElement, writer) : viewElement;
119
- };
120
- }
121
- /**
122
- * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
123
- *
124
- * @returns Returns a conversion callback.
125
- */
126
- export function attributeToViewInlineConverter({ priority, view: viewName }) {
127
- return (attributeValue, conversionApi) => {
128
- if (!attributeValue) {
129
- return;
130
- }
131
- const { writer } = conversionApi;
132
- const viewElement = writer.createAttributeElement(viewName, null, { priority });
133
- setViewAttributes(writer, attributeValue, viewElement);
134
- return viewElement;
135
- };
136
- }
137
- /**
138
- * View-to-model conversion helper preserving allowed attributes on block element.
139
- *
140
- * All matched attributes will be preserved on `html*Attributes` attribute.
141
- *
142
- * @returns Returns a conversion callback.
143
- */
144
- export function viewToModelBlockAttributeConverter({ view: viewName }, dataFilter) {
145
- return (dispatcher) => {
146
- dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
147
- // Converting an attribute of an element that has not been converted to anything does not make sense
148
- // because there will be nowhere to set that attribute on. At this stage, the element should've already
149
- // been converted. A collapsed range can show up in to-do lists (<input>) or complex widgets (e.g. table).
150
- // (https://github.com/ckeditor/ckeditor5/issues/11000).
151
- if (!data.modelRange || data.modelRange.isCollapsed) {
152
- return;
153
- }
154
- const viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
155
- if (!viewAttributes) {
156
- return;
157
- }
158
- conversionApi.writer.setAttribute(getHtmlAttributeName(data.viewItem.name), viewAttributes, data.modelRange);
159
- }, { priority: 'low' });
160
- };
161
- }
162
- /**
163
- * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
164
- * for block elements.
165
- *
166
- * @returns Returns a conversion callback.
167
- */
168
- export function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }) {
169
- return (dispatcher) => {
170
- dispatcher.on(`attribute:${getHtmlAttributeName(viewName)}:${modelName}`, (evt, data, conversionApi) => {
171
- if (!conversionApi.consumable.consume(data.item, evt.name)) {
172
- return;
173
- }
174
- const { attributeOldValue, attributeNewValue } = data;
175
- const viewWriter = conversionApi.writer;
176
- const viewElement = conversionApi.mapper.toViewElement(data.item);
177
- updateViewAttributes(viewWriter, attributeOldValue, attributeNewValue, viewElement);
178
- });
179
- };
180
- }
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 { toWidget } from 'ckeditor5/src/widget';
6
+ import { setViewAttributes, mergeViewElementAttributes, updateViewAttributes, getHtmlAttributeName } from './utils';
7
+ /**
8
+ * View-to-model conversion helper for object elements.
9
+ *
10
+ * Preserves object element content in `htmlContent` attribute.
11
+ *
12
+ * @returns Returns a conversion callback.
13
+ */
14
+ export function viewToModelObjectConverter({ model: modelName }) {
15
+ return (viewElement, conversionApi) => {
16
+ // Let's keep element HTML and its attributes, so we can rebuild element in downcast conversions.
17
+ return conversionApi.writer.createElement(modelName, {
18
+ htmlContent: viewElement.getCustomProperty('$rawContent')
19
+ });
20
+ };
21
+ }
22
+ /**
23
+ * Conversion helper converting an object element to an HTML object widget.
24
+ *
25
+ * @returns Returns a conversion callback.
26
+ */
27
+ export function toObjectWidgetConverter(editor, { view: viewName, isInline }) {
28
+ const t = editor.t;
29
+ return (modelElement, { writer }) => {
30
+ const widgetLabel = t('HTML object');
31
+ const viewElement = createObjectView(viewName, modelElement, writer);
32
+ const viewAttributes = modelElement.getAttribute(getHtmlAttributeName(viewName));
33
+ writer.addClass('html-object-embed__content', viewElement);
34
+ if (viewAttributes) {
35
+ setViewAttributes(writer, viewAttributes, viewElement);
36
+ }
37
+ // Widget cannot be a raw element because the widget system would not be able
38
+ // to add its UI to it. Thus, we need separate view container.
39
+ const viewContainer = writer.createContainerElement(isInline ? 'span' : 'div', {
40
+ class: 'html-object-embed',
41
+ 'data-html-object-embed-label': widgetLabel
42
+ }, viewElement);
43
+ return toWidget(viewContainer, writer, { label: widgetLabel });
44
+ };
45
+ }
46
+ /**
47
+ * Creates object view element from the given model element.
48
+ */
49
+ export function createObjectView(viewName, modelElement, writer) {
50
+ return writer.createRawElement(viewName, null, (domElement, domConverter) => {
51
+ domConverter.setContentOf(domElement, modelElement.getAttribute('htmlContent'));
52
+ });
53
+ }
54
+ /**
55
+ * View-to-attribute conversion helper preserving inline element attributes on `$text`.
56
+ *
57
+ * @returns Returns a conversion callback.
58
+ */
59
+ export function viewToAttributeInlineConverter({ view: viewName, model: attributeKey, allowEmpty }, dataFilter) {
60
+ return dispatcher => {
61
+ dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
62
+ let viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
63
+ // Do not apply the attribute if the element itself is already consumed and there are no view attributes to store.
64
+ if (!viewAttributes && !conversionApi.consumable.test(data.viewItem, { name: true })) {
65
+ return;
66
+ }
67
+ // Otherwise, we might need to convert it to an empty object just to preserve element itself,
68
+ // for example `<cite>` => <$text htmlCite="{}">.
69
+ viewAttributes = viewAttributes || {};
70
+ // Consume the element itself if it wasn't consumed by any other converter.
71
+ conversionApi.consumable.consume(data.viewItem, { name: true });
72
+ // Since we are converting to attribute we need a range on which we will set the attribute.
73
+ // If the range is not created yet, we will create it.
74
+ if (!data.modelRange) {
75
+ data = Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
76
+ }
77
+ // Convert empty inline element if allowed and has any attributes.
78
+ if (allowEmpty && data.modelRange.isCollapsed && Object.keys(viewAttributes).length) {
79
+ const modelElement = conversionApi.writer.createElement('htmlEmptyElement');
80
+ if (!conversionApi.safeInsert(modelElement, data.modelCursor)) {
81
+ return;
82
+ }
83
+ const parts = conversionApi.getSplitParts(modelElement);
84
+ data.modelRange = conversionApi.writer.createRange(data.modelRange.start, conversionApi.writer.createPositionAfter(parts[parts.length - 1]));
85
+ conversionApi.updateConversionResult(modelElement, data);
86
+ setAttributeOnItem(modelElement, viewAttributes, conversionApi);
87
+ return;
88
+ }
89
+ // Set attribute on each item in range according to the schema.
90
+ for (const node of data.modelRange.getItems()) {
91
+ setAttributeOnItem(node, viewAttributes, conversionApi);
92
+ }
93
+ }, { priority: 'low' });
94
+ };
95
+ function setAttributeOnItem(node, viewAttributes, conversionApi) {
96
+ if (conversionApi.schema.checkAttribute(node, attributeKey)) {
97
+ // Node's children are converted recursively, so node can already include model attribute.
98
+ // We want to extend it, not replace.
99
+ const nodeAttributes = node.getAttribute(attributeKey);
100
+ const attributesToAdd = mergeViewElementAttributes(viewAttributes, nodeAttributes || {});
101
+ conversionApi.writer.setAttribute(attributeKey, attributesToAdd, node);
102
+ }
103
+ }
104
+ }
105
+ /**
106
+ * Conversion helper converting an empty inline model element to an HTML element or widget.
107
+ */
108
+ export function emptyInlineModelElementToViewConverter({ model: attributeKey, view: viewName }, asWidget) {
109
+ return (item, { writer, consumable }) => {
110
+ if (!item.hasAttribute(attributeKey)) {
111
+ return null;
112
+ }
113
+ const viewElement = writer.createContainerElement(viewName);
114
+ const attributeValue = item.getAttribute(attributeKey);
115
+ consumable.consume(item, `attribute:${attributeKey}`);
116
+ setViewAttributes(writer, attributeValue, viewElement);
117
+ viewElement.getFillerOffset = () => null;
118
+ return asWidget ? toWidget(viewElement, writer) : viewElement;
119
+ };
120
+ }
121
+ /**
122
+ * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
123
+ *
124
+ * @returns Returns a conversion callback.
125
+ */
126
+ export function attributeToViewInlineConverter({ priority, view: viewName }) {
127
+ return (attributeValue, conversionApi) => {
128
+ if (!attributeValue) {
129
+ return;
130
+ }
131
+ const { writer } = conversionApi;
132
+ const viewElement = writer.createAttributeElement(viewName, null, { priority });
133
+ setViewAttributes(writer, attributeValue, viewElement);
134
+ return viewElement;
135
+ };
136
+ }
137
+ /**
138
+ * View-to-model conversion helper preserving allowed attributes on block element.
139
+ *
140
+ * All matched attributes will be preserved on `html*Attributes` attribute.
141
+ *
142
+ * @returns Returns a conversion callback.
143
+ */
144
+ export function viewToModelBlockAttributeConverter({ view: viewName }, dataFilter) {
145
+ return (dispatcher) => {
146
+ dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
147
+ // Converting an attribute of an element that has not been converted to anything does not make sense
148
+ // because there will be nowhere to set that attribute on. At this stage, the element should've already
149
+ // been converted. A collapsed range can show up in to-do lists (<input>) or complex widgets (e.g. table).
150
+ // (https://github.com/ckeditor/ckeditor5/issues/11000).
151
+ if (!data.modelRange || data.modelRange.isCollapsed) {
152
+ return;
153
+ }
154
+ const viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
155
+ if (!viewAttributes) {
156
+ return;
157
+ }
158
+ conversionApi.writer.setAttribute(getHtmlAttributeName(data.viewItem.name), viewAttributes, data.modelRange);
159
+ }, { priority: 'low' });
160
+ };
161
+ }
162
+ /**
163
+ * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
164
+ * for block elements.
165
+ *
166
+ * @returns Returns a conversion callback.
167
+ */
168
+ export function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }) {
169
+ return (dispatcher) => {
170
+ dispatcher.on(`attribute:${getHtmlAttributeName(viewName)}:${modelName}`, (evt, data, conversionApi) => {
171
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
172
+ return;
173
+ }
174
+ const { attributeOldValue, attributeNewValue } = data;
175
+ const viewWriter = conversionApi.writer;
176
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
177
+ updateViewAttributes(viewWriter, attributeOldValue, attributeNewValue, viewElement);
178
+ });
179
+ };
180
+ }