@ckeditor/ckeditor5-html-support 38.0.1 → 38.1.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 (49) hide show
  1. package/build/html-support.js +1 -1
  2. package/build/html-support.js.map +1 -0
  3. package/package.json +2 -45
  4. package/src/augmentation.d.ts +33 -33
  5. package/src/augmentation.js +5 -5
  6. package/src/converters.d.ts +56 -56
  7. package/src/converters.js +149 -148
  8. package/src/datafilter.d.ts +285 -255
  9. package/src/datafilter.js +661 -608
  10. package/src/dataschema.d.ts +179 -179
  11. package/src/dataschema.js +196 -196
  12. package/src/fullpage.d.ts +21 -21
  13. package/src/fullpage.js +80 -80
  14. package/src/generalhtmlsupport.d.ts +98 -98
  15. package/src/generalhtmlsupport.js +237 -237
  16. package/src/generalhtmlsupportconfig.d.ts +67 -67
  17. package/src/generalhtmlsupportconfig.js +5 -5
  18. package/src/htmlcomment.d.ts +72 -72
  19. package/src/htmlcomment.js +220 -188
  20. package/src/htmlpagedataprocessor.d.ts +22 -22
  21. package/src/htmlpagedataprocessor.js +67 -67
  22. package/src/index.d.ts +25 -25
  23. package/src/index.js +14 -14
  24. package/src/integrations/codeblock.d.ts +23 -23
  25. package/src/integrations/codeblock.js +101 -101
  26. package/src/integrations/customelement.d.ts +27 -27
  27. package/src/integrations/customelement.js +146 -146
  28. package/src/integrations/documentlist.d.ts +27 -27
  29. package/src/integrations/documentlist.js +203 -170
  30. package/src/integrations/dualcontent.d.ts +45 -45
  31. package/src/integrations/dualcontent.js +119 -118
  32. package/src/integrations/heading.d.ts +31 -35
  33. package/src/integrations/heading.js +60 -75
  34. package/src/integrations/image.d.ts +26 -26
  35. package/src/integrations/image.js +172 -172
  36. package/src/integrations/integrationutils.d.ts +15 -15
  37. package/src/integrations/integrationutils.js +21 -21
  38. package/src/integrations/mediaembed.d.ts +26 -26
  39. package/src/integrations/mediaembed.js +119 -119
  40. package/src/integrations/script.d.ts +26 -26
  41. package/src/integrations/script.js +59 -59
  42. package/src/integrations/style.d.ts +26 -26
  43. package/src/integrations/style.js +59 -59
  44. package/src/integrations/table.d.ts +23 -23
  45. package/src/integrations/table.js +163 -163
  46. package/src/schemadefinitions.d.ts +13 -13
  47. package/src/schemadefinitions.js +956 -952
  48. package/src/utils.d.ts +72 -61
  49. package/src/utils.js +139 -124
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-html-support",
3
- "version": "38.0.1",
3
+ "version": "38.1.1",
4
4
  "description": "HTML Support feature for CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -16,47 +16,9 @@
16
16
  ],
17
17
  "main": "src/index.js",
18
18
  "dependencies": {
19
- "ckeditor5": "^38.0.1",
19
+ "ckeditor5": "38.1.1",
20
20
  "lodash-es": "^4.17.15"
21
21
  },
22
- "devDependencies": {
23
- "@ckeditor/ckeditor5-alignment": "^38.0.1",
24
- "@ckeditor/ckeditor5-basic-styles": "^38.0.1",
25
- "@ckeditor/ckeditor5-block-quote": "^38.0.1",
26
- "@ckeditor/ckeditor5-clipboard": "^38.0.1",
27
- "@ckeditor/ckeditor5-cloud-services": "^38.0.1",
28
- "@ckeditor/ckeditor5-code-block": "^38.0.1",
29
- "@ckeditor/ckeditor5-core": "^38.0.1",
30
- "@ckeditor/ckeditor5-dev-utils": "^37.0.0",
31
- "@ckeditor/ckeditor5-easy-image": "^38.0.1",
32
- "@ckeditor/ckeditor5-editor-classic": "^38.0.1",
33
- "@ckeditor/ckeditor5-engine": "^38.0.1",
34
- "@ckeditor/ckeditor5-enter": "^38.0.1",
35
- "@ckeditor/ckeditor5-essentials": "^38.0.1",
36
- "@ckeditor/ckeditor5-font": "^38.0.1",
37
- "@ckeditor/ckeditor5-heading": "^38.0.1",
38
- "@ckeditor/ckeditor5-highlight": "^38.0.1",
39
- "@ckeditor/ckeditor5-horizontal-line": "^38.0.1",
40
- "@ckeditor/ckeditor5-html-embed": "^38.0.1",
41
- "@ckeditor/ckeditor5-image": "^38.0.1",
42
- "@ckeditor/ckeditor5-indent": "^38.0.1",
43
- "@ckeditor/ckeditor5-link": "^38.0.1",
44
- "@ckeditor/ckeditor5-list": "^38.0.1",
45
- "@ckeditor/ckeditor5-media-embed": "^38.0.1",
46
- "@ckeditor/ckeditor5-page-break": "^38.0.1",
47
- "@ckeditor/ckeditor5-paragraph": "^38.0.1",
48
- "@ckeditor/ckeditor5-paste-from-office": "^38.0.1",
49
- "@ckeditor/ckeditor5-remove-format": "^38.0.1",
50
- "@ckeditor/ckeditor5-source-editing": "^38.0.1",
51
- "@ckeditor/ckeditor5-table": "^38.0.1",
52
- "@ckeditor/ckeditor5-theme-lark": "^38.0.1",
53
- "@ckeditor/ckeditor5-typing": "^38.0.1",
54
- "@ckeditor/ckeditor5-utils": "^38.0.1",
55
- "@ckeditor/ckeditor5-widget": "^38.0.1",
56
- "typescript": "^4.8.4",
57
- "webpack": "^5.58.1",
58
- "webpack-cli": "^4.9.0"
59
- },
60
22
  "engines": {
61
23
  "node": ">=16.0.0",
62
24
  "npm": ">=5.7.1"
@@ -79,10 +41,5 @@
79
41
  "ckeditor5-metadata.json",
80
42
  "CHANGELOG.md"
81
43
  ],
82
- "scripts": {
83
- "dll:build": "webpack",
84
- "build": "tsc -p ./tsconfig.json",
85
- "postversion": "npm run build"
86
- },
87
44
  "types": "src/index.d.ts"
88
45
  }
@@ -1,33 +1,33 @@
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 type { GeneralHtmlSupport, DataFilter, DataSchema, GeneralHtmlSupportConfig, CodeBlockElementSupport, CustomElementSupport, DocumentListElementSupport, DualContentModelElementSupport, HeadingElementSupport, ImageElementSupport, MediaEmbedElementSupport, ScriptElementSupport, StyleElementSupport, TableElementSupport, HtmlComment, FullPage } from './index';
6
- declare module '@ckeditor/ckeditor5-core' {
7
- interface EditorConfig {
8
- /**
9
- * The configuration of the General HTML Support feature.
10
- * Introduced by the {@link module:html-support/generalhtmlsupport~GeneralHtmlSupport} feature.
11
- *
12
- * Read more in {@link module:html-support/generalhtmlsupportconfig~GeneralHtmlSupportConfig}.
13
- */
14
- htmlSupport?: GeneralHtmlSupportConfig;
15
- }
16
- interface PluginsMap {
17
- [GeneralHtmlSupport.pluginName]: GeneralHtmlSupport;
18
- [DataFilter.pluginName]: DataFilter;
19
- [DataSchema.pluginName]: DataSchema;
20
- [CodeBlockElementSupport.pluginName]: CodeBlockElementSupport;
21
- [CustomElementSupport.pluginName]: CustomElementSupport;
22
- [DocumentListElementSupport.pluginName]: DocumentListElementSupport;
23
- [DualContentModelElementSupport.pluginName]: DualContentModelElementSupport;
24
- [HeadingElementSupport.pluginName]: HeadingElementSupport;
25
- [ImageElementSupport.pluginName]: ImageElementSupport;
26
- [MediaEmbedElementSupport.pluginName]: MediaEmbedElementSupport;
27
- [ScriptElementSupport.pluginName]: ScriptElementSupport;
28
- [StyleElementSupport.pluginName]: StyleElementSupport;
29
- [TableElementSupport.pluginName]: TableElementSupport;
30
- [HtmlComment.pluginName]: HtmlComment;
31
- [FullPage.pluginName]: FullPage;
32
- }
33
- }
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 type { GeneralHtmlSupport, DataFilter, DataSchema, GeneralHtmlSupportConfig, CodeBlockElementSupport, CustomElementSupport, DocumentListElementSupport, DualContentModelElementSupport, HeadingElementSupport, ImageElementSupport, MediaEmbedElementSupport, ScriptElementSupport, StyleElementSupport, TableElementSupport, HtmlComment, FullPage } from './index';
6
+ declare module '@ckeditor/ckeditor5-core' {
7
+ interface EditorConfig {
8
+ /**
9
+ * The configuration of the General HTML Support feature.
10
+ * Introduced by the {@link module:html-support/generalhtmlsupport~GeneralHtmlSupport} feature.
11
+ *
12
+ * Read more in {@link module:html-support/generalhtmlsupportconfig~GeneralHtmlSupportConfig}.
13
+ */
14
+ htmlSupport?: GeneralHtmlSupportConfig;
15
+ }
16
+ interface PluginsMap {
17
+ [GeneralHtmlSupport.pluginName]: GeneralHtmlSupport;
18
+ [DataFilter.pluginName]: DataFilter;
19
+ [DataSchema.pluginName]: DataSchema;
20
+ [CodeBlockElementSupport.pluginName]: CodeBlockElementSupport;
21
+ [CustomElementSupport.pluginName]: CustomElementSupport;
22
+ [DocumentListElementSupport.pluginName]: DocumentListElementSupport;
23
+ [DualContentModelElementSupport.pluginName]: DualContentModelElementSupport;
24
+ [HeadingElementSupport.pluginName]: HeadingElementSupport;
25
+ [ImageElementSupport.pluginName]: ImageElementSupport;
26
+ [MediaEmbedElementSupport.pluginName]: MediaEmbedElementSupport;
27
+ [ScriptElementSupport.pluginName]: ScriptElementSupport;
28
+ [StyleElementSupport.pluginName]: StyleElementSupport;
29
+ [TableElementSupport.pluginName]: TableElementSupport;
30
+ [HtmlComment.pluginName]: HtmlComment;
31
+ [FullPage.pluginName]: FullPage;
32
+ }
33
+ }
@@ -1,5 +1,5 @@
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
- export {};
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
+ export {};
@@ -1,56 +1,56 @@
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 }: DataSchemaInlineElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
36
- /**
37
- * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
38
- *
39
- * @returns Returns a conversion callback.
40
- */
41
- export declare function attributeToViewInlineConverter({ priority, view: viewName }: DataSchemaInlineElementDefinition): (attributeValue: any, conversionApi: DowncastConversionApi) => AttributeElement | undefined;
42
- /**
43
- * View-to-model conversion helper preserving allowed attributes on block element.
44
- *
45
- * All matched attributes will be preserved on `htmlAttributes` attribute.
46
- *
47
- * @returns Returns a conversion callback.
48
- */
49
- export declare function viewToModelBlockAttributeConverter({ view: viewName }: DataSchemaBlockElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
50
- /**
51
- * Model-to-view conversion helper applying attributes preserved in `htmlAttributes` attribute
52
- * for block elements.
53
- *
54
- * @returns Returns a conversion callback.
55
- */
56
- export declare function modelToViewBlockAttributeConverter({ 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 }: DataSchemaInlineElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
36
+ /**
37
+ * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
38
+ *
39
+ * @returns Returns a conversion callback.
40
+ */
41
+ export declare function attributeToViewInlineConverter({ priority, view: viewName }: DataSchemaInlineElementDefinition): (attributeValue: any, conversionApi: DowncastConversionApi) => AttributeElement | undefined;
42
+ /**
43
+ * View-to-model conversion helper preserving allowed attributes on block element.
44
+ *
45
+ * All matched attributes will be preserved on `html*Attributes` attribute.
46
+ *
47
+ * @returns Returns a conversion callback.
48
+ */
49
+ export declare function viewToModelBlockAttributeConverter({ view: viewName }: DataSchemaBlockElementDefinition, dataFilter: DataFilter): (dispatcher: UpcastDispatcher) => void;
50
+ /**
51
+ * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
52
+ * for block elements.
53
+ *
54
+ * @returns Returns a conversion callback.
55
+ */
56
+ export declare function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }: DataSchemaBlockElementDefinition): (dispatcher: DowncastDispatcher) => void;
package/src/converters.js CHANGED
@@ -1,148 +1,149 @@
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 } 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('htmlAttributes');
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 }, 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
- // Set attribute on each item in range according to the schema.
78
- for (const node of data.modelRange.getItems()) {
79
- if (conversionApi.schema.checkAttribute(node, attributeKey)) {
80
- // Node's children are converted recursively, so node can already include model attribute.
81
- // We want to extend it, not replace.
82
- const nodeAttributes = node.getAttribute(attributeKey);
83
- const attributesToAdd = mergeViewElementAttributes(viewAttributes, nodeAttributes || {});
84
- conversionApi.writer.setAttribute(attributeKey, attributesToAdd, node);
85
- }
86
- }
87
- }, { priority: 'low' });
88
- };
89
- }
90
- /**
91
- * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
92
- *
93
- * @returns Returns a conversion callback.
94
- */
95
- export function attributeToViewInlineConverter({ priority, view: viewName }) {
96
- return (attributeValue, conversionApi) => {
97
- if (!attributeValue) {
98
- return;
99
- }
100
- const { writer } = conversionApi;
101
- const viewElement = writer.createAttributeElement(viewName, null, { priority });
102
- setViewAttributes(writer, attributeValue, viewElement);
103
- return viewElement;
104
- };
105
- }
106
- /**
107
- * View-to-model conversion helper preserving allowed attributes on block element.
108
- *
109
- * All matched attributes will be preserved on `htmlAttributes` attribute.
110
- *
111
- * @returns Returns a conversion callback.
112
- */
113
- export function viewToModelBlockAttributeConverter({ view: viewName }, dataFilter) {
114
- return (dispatcher) => {
115
- dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
116
- // Converting an attribute of an element that has not been converted to anything does not make sense
117
- // because there will be nowhere to set that attribute on. At this stage, the element should've already
118
- // been converted. A collapsed range can show up in to-do lists (<input>) or complex widgets (e.g. table).
119
- // (https://github.com/ckeditor/ckeditor5/issues/11000).
120
- if (!data.modelRange || data.modelRange.isCollapsed) {
121
- return;
122
- }
123
- const viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
124
- if (viewAttributes) {
125
- conversionApi.writer.setAttribute('htmlAttributes', viewAttributes, data.modelRange);
126
- }
127
- }, { priority: 'low' });
128
- };
129
- }
130
- /**
131
- * Model-to-view conversion helper applying attributes preserved in `htmlAttributes` attribute
132
- * for block elements.
133
- *
134
- * @returns Returns a conversion callback.
135
- */
136
- export function modelToViewBlockAttributeConverter({ model: modelName }) {
137
- return (dispatcher) => {
138
- dispatcher.on(`attribute:htmlAttributes:${modelName}`, (evt, data, conversionApi) => {
139
- if (!conversionApi.consumable.consume(data.item, evt.name)) {
140
- return;
141
- }
142
- const { attributeOldValue, attributeNewValue } = data;
143
- const viewWriter = conversionApi.writer;
144
- const viewElement = conversionApi.mapper.toViewElement(data.item);
145
- updateViewAttributes(viewWriter, attributeOldValue, attributeNewValue, viewElement);
146
- });
147
- };
148
- }
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 }, 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
+ // Set attribute on each item in range according to the schema.
78
+ for (const node of data.modelRange.getItems()) {
79
+ if (conversionApi.schema.checkAttribute(node, attributeKey)) {
80
+ // Node's children are converted recursively, so node can already include model attribute.
81
+ // We want to extend it, not replace.
82
+ const nodeAttributes = node.getAttribute(attributeKey);
83
+ const attributesToAdd = mergeViewElementAttributes(viewAttributes, nodeAttributes || {});
84
+ conversionApi.writer.setAttribute(attributeKey, attributesToAdd, node);
85
+ }
86
+ }
87
+ }, { priority: 'low' });
88
+ };
89
+ }
90
+ /**
91
+ * Attribute-to-view conversion helper applying attributes to view element preserved on `$text`.
92
+ *
93
+ * @returns Returns a conversion callback.
94
+ */
95
+ export function attributeToViewInlineConverter({ priority, view: viewName }) {
96
+ return (attributeValue, conversionApi) => {
97
+ if (!attributeValue) {
98
+ return;
99
+ }
100
+ const { writer } = conversionApi;
101
+ const viewElement = writer.createAttributeElement(viewName, null, { priority });
102
+ setViewAttributes(writer, attributeValue, viewElement);
103
+ return viewElement;
104
+ };
105
+ }
106
+ /**
107
+ * View-to-model conversion helper preserving allowed attributes on block element.
108
+ *
109
+ * All matched attributes will be preserved on `html*Attributes` attribute.
110
+ *
111
+ * @returns Returns a conversion callback.
112
+ */
113
+ export function viewToModelBlockAttributeConverter({ view: viewName }, dataFilter) {
114
+ return (dispatcher) => {
115
+ dispatcher.on(`element:${viewName}`, (evt, data, conversionApi) => {
116
+ // Converting an attribute of an element that has not been converted to anything does not make sense
117
+ // because there will be nowhere to set that attribute on. At this stage, the element should've already
118
+ // been converted. A collapsed range can show up in to-do lists (<input>) or complex widgets (e.g. table).
119
+ // (https://github.com/ckeditor/ckeditor5/issues/11000).
120
+ if (!data.modelRange || data.modelRange.isCollapsed) {
121
+ return;
122
+ }
123
+ const viewAttributes = dataFilter.processViewAttributes(data.viewItem, conversionApi);
124
+ if (!viewAttributes) {
125
+ return;
126
+ }
127
+ conversionApi.writer.setAttribute(getHtmlAttributeName(data.viewItem.name), viewAttributes, data.modelRange);
128
+ }, { priority: 'low' });
129
+ };
130
+ }
131
+ /**
132
+ * Model-to-view conversion helper applying attributes preserved in `html*Attributes` attribute
133
+ * for block elements.
134
+ *
135
+ * @returns Returns a conversion callback.
136
+ */
137
+ export function modelToViewBlockAttributeConverter({ view: viewName, model: modelName }) {
138
+ return (dispatcher) => {
139
+ dispatcher.on(`attribute:${getHtmlAttributeName(viewName)}:${modelName}`, (evt, data, conversionApi) => {
140
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
141
+ return;
142
+ }
143
+ const { attributeOldValue, attributeNewValue } = data;
144
+ const viewWriter = conversionApi.writer;
145
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
146
+ updateViewAttributes(viewWriter, attributeOldValue, attributeNewValue, viewElement);
147
+ });
148
+ };
149
+ }