@ckeditor/ckeditor5-engine 47.6.1 → 48.0.0-alpha.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 (258) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/engineconfig.d.ts +6 -15
  3. package/dist/index-editor.css +38 -15
  4. package/dist/index.css +37 -37
  5. package/dist/index.css.map +1 -1
  6. package/{src → dist}/index.d.ts +0 -1
  7. package/dist/index.js +588 -94
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/model/model.d.ts +10 -4
  10. package/{src → dist}/model/selection.d.ts +1 -1
  11. package/{src → dist}/view/downcastwriter.d.ts +3 -2
  12. package/{src → dist}/view/element.d.ts +2 -2
  13. package/{src → dist}/view/matcher.d.ts +4 -2
  14. package/dist/view/styles/background.d.ts +18 -0
  15. package/{src → dist}/view/styles/border.d.ts +0 -12
  16. package/{src → dist}/view/styles/margin.d.ts +0 -13
  17. package/{src → dist}/view/styles/padding.d.ts +0 -13
  18. package/{src → dist}/view/styles/utils.d.ts +12 -0
  19. package/package.json +20 -39
  20. package/src/controller/datacontroller.js +0 -522
  21. package/src/controller/editingcontroller.js +0 -181
  22. package/src/conversion/conversion.js +0 -606
  23. package/src/conversion/conversionhelpers.js +0 -33
  24. package/src/conversion/downcastdispatcher.js +0 -563
  25. package/src/conversion/downcasthelpers.js +0 -2160
  26. package/src/conversion/mapper.js +0 -1050
  27. package/src/conversion/modelconsumable.js +0 -331
  28. package/src/conversion/upcastdispatcher.js +0 -470
  29. package/src/conversion/upcasthelpers.js +0 -952
  30. package/src/conversion/viewconsumable.js +0 -541
  31. package/src/dataprocessor/basichtmlwriter.js +0 -22
  32. package/src/dataprocessor/dataprocessor.js +0 -5
  33. package/src/dataprocessor/htmldataprocessor.js +0 -107
  34. package/src/dataprocessor/htmlwriter.js +0 -5
  35. package/src/dataprocessor/xmldataprocessor.js +0 -127
  36. package/src/dev-utils/model.js +0 -396
  37. package/src/dev-utils/operationreplayer.js +0 -116
  38. package/src/dev-utils/utils.js +0 -122
  39. package/src/dev-utils/view.js +0 -990
  40. package/src/engineconfig.js +0 -5
  41. package/src/index.js +0 -134
  42. package/src/legacyerrors.js +0 -17
  43. package/src/model/batch.js +0 -98
  44. package/src/model/differ.js +0 -1288
  45. package/src/model/document.js +0 -398
  46. package/src/model/documentfragment.js +0 -332
  47. package/src/model/documentselection.js +0 -1026
  48. package/src/model/element.js +0 -323
  49. package/src/model/history.js +0 -206
  50. package/src/model/item.js +0 -5
  51. package/src/model/liveposition.js +0 -93
  52. package/src/model/liverange.js +0 -121
  53. package/src/model/markercollection.js +0 -436
  54. package/src/model/model.js +0 -866
  55. package/src/model/node.js +0 -371
  56. package/src/model/nodelist.js +0 -244
  57. package/src/model/operation/attributeoperation.js +0 -172
  58. package/src/model/operation/detachoperation.js +0 -87
  59. package/src/model/operation/insertoperation.js +0 -153
  60. package/src/model/operation/markeroperation.js +0 -136
  61. package/src/model/operation/mergeoperation.js +0 -184
  62. package/src/model/operation/moveoperation.js +0 -179
  63. package/src/model/operation/nooperation.js +0 -48
  64. package/src/model/operation/operation.js +0 -78
  65. package/src/model/operation/operationfactory.js +0 -44
  66. package/src/model/operation/renameoperation.js +0 -128
  67. package/src/model/operation/rootattributeoperation.js +0 -173
  68. package/src/model/operation/rootoperation.js +0 -106
  69. package/src/model/operation/splitoperation.js +0 -214
  70. package/src/model/operation/transform.js +0 -2211
  71. package/src/model/operation/utils.js +0 -217
  72. package/src/model/position.js +0 -1041
  73. package/src/model/range.js +0 -880
  74. package/src/model/rootelement.js +0 -82
  75. package/src/model/schema.js +0 -1542
  76. package/src/model/selection.js +0 -814
  77. package/src/model/text.js +0 -92
  78. package/src/model/textproxy.js +0 -202
  79. package/src/model/treewalker.js +0 -313
  80. package/src/model/typecheckable.js +0 -16
  81. package/src/model/utils/autoparagraphing.js +0 -63
  82. package/src/model/utils/deletecontent.js +0 -509
  83. package/src/model/utils/getselectedcontent.js +0 -126
  84. package/src/model/utils/insertcontent.js +0 -750
  85. package/src/model/utils/insertobject.js +0 -135
  86. package/src/model/utils/modifyselection.js +0 -187
  87. package/src/model/utils/selection-post-fixer.js +0 -264
  88. package/src/model/writer.js +0 -1318
  89. package/src/view/attributeelement.js +0 -220
  90. package/src/view/containerelement.js +0 -91
  91. package/src/view/datatransfer.js +0 -106
  92. package/src/view/document.js +0 -139
  93. package/src/view/documentfragment.js +0 -251
  94. package/src/view/documentselection.js +0 -270
  95. package/src/view/domconverter.js +0 -1661
  96. package/src/view/downcastwriter.js +0 -1589
  97. package/src/view/editableelement.js +0 -74
  98. package/src/view/element.js +0 -1053
  99. package/src/view/elementdefinition.js +0 -5
  100. package/src/view/emptyelement.js +0 -83
  101. package/src/view/filler.js +0 -161
  102. package/src/view/item.js +0 -5
  103. package/src/view/matcher.js +0 -437
  104. package/src/view/node.js +0 -238
  105. package/src/view/observer/arrowkeysobserver.js +0 -40
  106. package/src/view/observer/bubblingemittermixin.js +0 -215
  107. package/src/view/observer/bubblingeventinfo.js +0 -49
  108. package/src/view/observer/clickobserver.js +0 -26
  109. package/src/view/observer/compositionobserver.js +0 -64
  110. package/src/view/observer/domeventdata.js +0 -63
  111. package/src/view/observer/domeventobserver.js +0 -81
  112. package/src/view/observer/fakeselectionobserver.js +0 -95
  113. package/src/view/observer/focusobserver.js +0 -166
  114. package/src/view/observer/inputobserver.js +0 -236
  115. package/src/view/observer/keyobserver.js +0 -36
  116. package/src/view/observer/mouseobserver.js +0 -26
  117. package/src/view/observer/mutationobserver.js +0 -219
  118. package/src/view/observer/observer.js +0 -92
  119. package/src/view/observer/pointerobserver.js +0 -26
  120. package/src/view/observer/selectionobserver.js +0 -318
  121. package/src/view/observer/tabobserver.js +0 -42
  122. package/src/view/observer/touchobserver.js +0 -26
  123. package/src/view/placeholder.js +0 -285
  124. package/src/view/position.js +0 -341
  125. package/src/view/range.js +0 -451
  126. package/src/view/rawelement.js +0 -115
  127. package/src/view/renderer.js +0 -1148
  128. package/src/view/rooteditableelement.js +0 -78
  129. package/src/view/selection.js +0 -594
  130. package/src/view/styles/background.d.ts +0 -33
  131. package/src/view/styles/background.js +0 -74
  132. package/src/view/styles/border.js +0 -316
  133. package/src/view/styles/margin.js +0 -34
  134. package/src/view/styles/padding.js +0 -34
  135. package/src/view/styles/utils.js +0 -219
  136. package/src/view/stylesmap.js +0 -941
  137. package/src/view/text.js +0 -110
  138. package/src/view/textproxy.js +0 -136
  139. package/src/view/tokenlist.js +0 -194
  140. package/src/view/treewalker.js +0 -389
  141. package/src/view/typecheckable.js +0 -19
  142. package/src/view/uielement.js +0 -194
  143. package/src/view/upcastwriter.js +0 -363
  144. package/src/view/view.js +0 -579
  145. package/theme/placeholder.css +0 -36
  146. package/theme/renderer.css +0 -9
  147. /package/{src → dist}/controller/datacontroller.d.ts +0 -0
  148. /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
  149. /package/{src → dist}/conversion/conversion.d.ts +0 -0
  150. /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
  151. /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
  152. /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
  153. /package/{src → dist}/conversion/mapper.d.ts +0 -0
  154. /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
  155. /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
  156. /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
  157. /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
  158. /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
  159. /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
  160. /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
  161. /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
  162. /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
  163. /package/{src → dist}/dev-utils/model.d.ts +0 -0
  164. /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
  165. /package/{src → dist}/dev-utils/utils.d.ts +0 -0
  166. /package/{src → dist}/dev-utils/view.d.ts +0 -0
  167. /package/{src → dist}/legacyerrors.d.ts +0 -0
  168. /package/{src → dist}/model/batch.d.ts +0 -0
  169. /package/{src → dist}/model/differ.d.ts +0 -0
  170. /package/{src → dist}/model/document.d.ts +0 -0
  171. /package/{src → dist}/model/documentfragment.d.ts +0 -0
  172. /package/{src → dist}/model/documentselection.d.ts +0 -0
  173. /package/{src → dist}/model/element.d.ts +0 -0
  174. /package/{src → dist}/model/history.d.ts +0 -0
  175. /package/{src → dist}/model/item.d.ts +0 -0
  176. /package/{src → dist}/model/liveposition.d.ts +0 -0
  177. /package/{src → dist}/model/liverange.d.ts +0 -0
  178. /package/{src → dist}/model/markercollection.d.ts +0 -0
  179. /package/{src → dist}/model/node.d.ts +0 -0
  180. /package/{src → dist}/model/nodelist.d.ts +0 -0
  181. /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
  182. /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
  183. /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
  184. /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
  185. /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
  186. /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
  187. /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
  188. /package/{src → dist}/model/operation/operation.d.ts +0 -0
  189. /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
  190. /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
  191. /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
  192. /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
  193. /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
  194. /package/{src → dist}/model/operation/transform.d.ts +0 -0
  195. /package/{src → dist}/model/operation/utils.d.ts +0 -0
  196. /package/{src → dist}/model/position.d.ts +0 -0
  197. /package/{src → dist}/model/range.d.ts +0 -0
  198. /package/{src → dist}/model/rootelement.d.ts +0 -0
  199. /package/{src → dist}/model/schema.d.ts +0 -0
  200. /package/{src → dist}/model/text.d.ts +0 -0
  201. /package/{src → dist}/model/textproxy.d.ts +0 -0
  202. /package/{src → dist}/model/treewalker.d.ts +0 -0
  203. /package/{src → dist}/model/typecheckable.d.ts +0 -0
  204. /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
  205. /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
  206. /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
  207. /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
  208. /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
  209. /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
  210. /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
  211. /package/{src → dist}/model/writer.d.ts +0 -0
  212. /package/{src → dist}/view/attributeelement.d.ts +0 -0
  213. /package/{src → dist}/view/containerelement.d.ts +0 -0
  214. /package/{src → dist}/view/datatransfer.d.ts +0 -0
  215. /package/{src → dist}/view/document.d.ts +0 -0
  216. /package/{src → dist}/view/documentfragment.d.ts +0 -0
  217. /package/{src → dist}/view/documentselection.d.ts +0 -0
  218. /package/{src → dist}/view/domconverter.d.ts +0 -0
  219. /package/{src → dist}/view/editableelement.d.ts +0 -0
  220. /package/{src → dist}/view/elementdefinition.d.ts +0 -0
  221. /package/{src → dist}/view/emptyelement.d.ts +0 -0
  222. /package/{src → dist}/view/filler.d.ts +0 -0
  223. /package/{src → dist}/view/item.d.ts +0 -0
  224. /package/{src → dist}/view/node.d.ts +0 -0
  225. /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
  226. /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
  227. /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
  228. /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
  229. /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
  230. /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
  231. /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
  232. /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
  233. /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
  234. /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
  235. /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
  236. /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
  237. /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
  238. /package/{src → dist}/view/observer/observer.d.ts +0 -0
  239. /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
  240. /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
  241. /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
  242. /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
  243. /package/{src → dist}/view/placeholder.d.ts +0 -0
  244. /package/{src → dist}/view/position.d.ts +0 -0
  245. /package/{src → dist}/view/range.d.ts +0 -0
  246. /package/{src → dist}/view/rawelement.d.ts +0 -0
  247. /package/{src → dist}/view/renderer.d.ts +0 -0
  248. /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
  249. /package/{src → dist}/view/selection.d.ts +0 -0
  250. /package/{src → dist}/view/stylesmap.d.ts +0 -0
  251. /package/{src → dist}/view/text.d.ts +0 -0
  252. /package/{src → dist}/view/textproxy.d.ts +0 -0
  253. /package/{src → dist}/view/tokenlist.d.ts +0 -0
  254. /package/{src → dist}/view/treewalker.d.ts +0 -0
  255. /package/{src → dist}/view/typecheckable.d.ts +0 -0
  256. /package/{src → dist}/view/uielement.d.ts +0 -0
  257. /package/{src → dist}/view/upcastwriter.d.ts +0 -0
  258. /package/{src → dist}/view/view.d.ts +0 -0
@@ -1,952 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- import { Matcher } from '../view/matcher.js';
6
- import { ConversionHelpers } from './conversionhelpers.js';
7
- import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing.js';
8
- import { priorities } from '@ckeditor/ckeditor5-utils';
9
- import { cloneDeep } from 'es-toolkit/compat';
10
- /**
11
- * Contains the {@link module:engine/view/view view} to {@link module:engine/model/model model} converters for
12
- * {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}.
13
- *
14
- * @module engine/conversion/upcasthelpers
15
- */
16
- /**
17
- * Upcast conversion helper functions.
18
- *
19
- * Learn more about {@glink framework/deep-dive/conversion/upcast upcast helpers}.
20
- *
21
- * @extends module:engine/conversion/conversionhelpers~ConversionHelpers
22
- */
23
- export class UpcastHelpers extends ConversionHelpers {
24
- /**
25
- * View element to model element conversion helper.
26
- *
27
- * This conversion results in creating a model element. For example,
28
- * view `<p>Foo</p>` becomes `<paragraph>Foo</paragraph>` in the model.
29
- *
30
- * Keep in mind that the element will be inserted only if it is allowed
31
- * by {@link module:engine/model/schema~ModelSchema schema} configuration.
32
- *
33
- * ```ts
34
- * editor.conversion.for( 'upcast' ).elementToElement( {
35
- * view: 'p',
36
- * model: 'paragraph'
37
- * } );
38
- *
39
- * editor.conversion.for( 'upcast' ).elementToElement( {
40
- * view: 'p',
41
- * model: 'paragraph',
42
- * converterPriority: 'high'
43
- * } );
44
- *
45
- * editor.conversion.for( 'upcast' ).elementToElement( {
46
- * view: {
47
- * name: 'p',
48
- * classes: 'fancy'
49
- * },
50
- * model: 'fancyParagraph'
51
- * } );
52
- *
53
- * editor.conversion.for( 'upcast' ).elementToElement( {
54
- * view: {
55
- * name: 'p',
56
- * classes: 'heading'
57
- * },
58
- * model: ( viewElement, conversionApi ) => {
59
- * const modelWriter = conversionApi.writer;
60
- *
61
- * return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
62
- * }
63
- * } );
64
- * ```
65
- *
66
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
67
- * to the conversion process.
68
- *
69
- * @param config Conversion configuration.
70
- * @param config.view Pattern matching all view elements which should be converted. If not set, the converter
71
- * will fire for every view element.
72
- * @param config.model Name of the model element, a model element instance or a function that takes a view element
73
- * and {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API}
74
- * and returns a model element. The model element will be inserted in the model.
75
- * @param config.converterPriority Converter priority.
76
- */
77
- elementToElement(config) {
78
- return this.add(upcastElementToElement(config));
79
- }
80
- /**
81
- * View element to model attribute conversion helper.
82
- *
83
- * This conversion results in setting an attribute on a model node. For example, view `<strong>Foo</strong>` becomes
84
- * `Foo` {@link module:engine/model/text~ModelText model text node} with `bold` attribute set to `true`.
85
- *
86
- * This helper is meant to set a model attribute on all the elements that are inside the converted element:
87
- *
88
- * ```
89
- * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
90
- * ```
91
- *
92
- * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
93
- * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text. See
94
- * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute} for comparison.
95
- *
96
- * Keep in mind that the attribute will be set only if it is allowed by
97
- * {@link module:engine/model/schema~ModelSchema schema} configuration.
98
- *
99
- * ```ts
100
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
101
- * view: 'strong',
102
- * model: 'bold'
103
- * } );
104
- *
105
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
106
- * view: 'strong',
107
- * model: 'bold',
108
- * converterPriority: 'high'
109
- * } );
110
- *
111
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
112
- * view: {
113
- * name: 'span',
114
- * classes: 'bold'
115
- * },
116
- * model: 'bold'
117
- * } );
118
- *
119
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
120
- * view: {
121
- * name: 'span',
122
- * classes: [ 'styled', 'styled-dark' ]
123
- * },
124
- * model: {
125
- * key: 'styled',
126
- * value: 'dark'
127
- * }
128
- * } );
129
- *
130
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
131
- * view: {
132
- * name: 'span',
133
- * styles: {
134
- * 'font-size': /[\s\S]+/
135
- * }
136
- * },
137
- * model: {
138
- * key: 'fontSize',
139
- * value: ( viewElement, conversionApi ) => {
140
- * const fontSize = viewElement.getStyle( 'font-size' );
141
- * const value = fontSize.substr( 0, fontSize.length - 2 );
142
- *
143
- * if ( value <= 10 ) {
144
- * return 'small';
145
- * } else if ( value > 12 ) {
146
- * return 'big';
147
- * }
148
- *
149
- * return null;
150
- * }
151
- * }
152
- * } );
153
- * ```
154
- *
155
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
156
- * to the conversion process.
157
- *
158
- * @param config Conversion configuration.
159
- * @param config.view Pattern matching all view elements which should be converted.
160
- * @param config.model Model attribute key or an object with `key` and `value` properties, describing
161
- * the model attribute. `value` property may be set as a function that takes a view element and
162
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
163
- * If `String` is given, the model attribute value will be set to `true`.
164
- * @param config.converterPriority Converter priority. Defaults to `low`.
165
- */
166
- elementToAttribute(config) {
167
- return this.add(upcastElementToAttribute(config));
168
- }
169
- /**
170
- * View attribute to model attribute conversion helper.
171
- *
172
- * This conversion results in setting an attribute on a model node. For example, view `<img src="foo.jpg"></img>` becomes
173
- * `<imageBlock source="foo.jpg"></imageBlock>` in the model.
174
- *
175
- * This helper is meant to convert view attributes from view elements which got converted to the model, so the view attribute
176
- * is set only on the corresponding model node:
177
- *
178
- * ```
179
- * <div class="dark"><div>foo</div></div> --> <div dark="true"><div>foo</div></div>
180
- * ```
181
- *
182
- * Above, `class="dark"` attribute is added only to the `<div>` elements that has it. This is in contrast to
183
- * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute} which sets attributes for
184
- * all the children in the model:
185
- *
186
- * ```
187
- * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
188
- * ```
189
- *
190
- * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
191
- * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text.
192
- *
193
- * Keep in mind that the attribute will be set only if it is allowed by
194
- * {@link module:engine/model/schema~ModelSchema schema} configuration.
195
- *
196
- * ```ts
197
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
198
- * view: 'src',
199
- * model: 'source'
200
- * } );
201
- *
202
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
203
- * view: { key: 'src' },
204
- * model: 'source'
205
- * } );
206
- *
207
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
208
- * view: { key: 'src' },
209
- * model: 'source',
210
- * converterPriority: 'normal'
211
- * } );
212
- *
213
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
214
- * view: {
215
- * key: 'data-style',
216
- * value: /[\s\S]+/
217
- * },
218
- * model: 'styled'
219
- * } );
220
- *
221
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
222
- * view: {
223
- * name: 'img',
224
- * key: 'class',
225
- * value: 'styled-dark'
226
- * },
227
- * model: {
228
- * key: 'styled',
229
- * value: 'dark'
230
- * }
231
- * } );
232
- *
233
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
234
- * view: {
235
- * key: 'class',
236
- * value: /styled-[\S]+/
237
- * },
238
- * model: {
239
- * key: 'styled'
240
- * value: ( viewElement, conversionApi ) => {
241
- * const regexp = /styled-([\S]+)/;
242
- * const match = viewElement.getAttribute( 'class' ).match( regexp );
243
- *
244
- * return match[ 1 ];
245
- * }
246
- * }
247
- * } );
248
- * ```
249
- *
250
- * Converting styles works a bit differently as it requires `view.styles` to be an object and by default
251
- * a model attribute will be set to `true` by such a converter. You can set the model attribute to any value by providing the `value`
252
- * callback that returns the desired value.
253
- *
254
- * ```ts
255
- * // Default conversion of font-weight style will result in setting bold attribute to true.
256
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
257
- * view: {
258
- * styles: {
259
- * 'font-weight': 'bold'
260
- * }
261
- * },
262
- * model: 'bold'
263
- * } );
264
- *
265
- * // This converter will pass any style value to the `lineHeight` model attribute.
266
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
267
- * view: {
268
- * styles: {
269
- * 'line-height': /[\s\S]+/
270
- * }
271
- * },
272
- * model: {
273
- * key: 'lineHeight',
274
- * value: ( viewElement, conversionApi ) => viewElement.getStyle( 'line-height' )
275
- * }
276
- * } );
277
- * ```
278
- *
279
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
280
- * to the conversion process.
281
- *
282
- * @param config Conversion configuration.
283
- * @param config.view Specifies which view attribute will be converted. If a `String` is passed,
284
- * attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
285
- * specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
286
- * property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
287
- * a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
288
- * @param config.model Model attribute key or an object with `key` and `value` properties, describing
289
- * the model attribute. `value` property may be set as a function that takes a view element and
290
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
291
- * If `String` is given, the model attribute value will be same as view attribute value.
292
- * @param config.converterPriority Converter priority. Defaults to `low`.
293
- */
294
- attributeToAttribute(config) {
295
- return this.add(upcastAttributeToAttribute(config));
296
- }
297
- /**
298
- * View element to model marker conversion helper.
299
- *
300
- * This conversion results in creating a model marker. For example, if the marker was stored in a view as an element:
301
- * `<p>Fo<span data-marker="comment" data-comment-id="7"></span>o</p><p>B<span data-marker="comment" data-comment-id="7"></span>ar</p>`,
302
- * after the conversion is done, the marker will be available in
303
- * {@link module:engine/model/model~Model#markers model document markers}.
304
- *
305
- * **Note**: When this helper is used in the data upcast in combination with
306
- * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} in the data downcast,
307
- * then invalid HTML code (e.g. a span between table cells) may be produced by the latter converter.
308
- *
309
- * In most of the cases, the {@link #dataToMarker} should be used instead.
310
- *
311
- * ```ts
312
- * editor.conversion.for( 'upcast' ).elementToMarker( {
313
- * view: 'marker-search',
314
- * model: 'search'
315
- * } );
316
- *
317
- * editor.conversion.for( 'upcast' ).elementToMarker( {
318
- * view: 'marker-search',
319
- * model: 'search',
320
- * converterPriority: 'high'
321
- * } );
322
- *
323
- * editor.conversion.for( 'upcast' ).elementToMarker( {
324
- * view: 'marker-search',
325
- * model: ( viewElement, conversionApi ) => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
326
- * } );
327
- *
328
- * editor.conversion.for( 'upcast' ).elementToMarker( {
329
- * view: {
330
- * name: 'span',
331
- * attributes: {
332
- * 'data-marker': 'search'
333
- * }
334
- * },
335
- * model: 'search'
336
- * } );
337
- * ```
338
- *
339
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
340
- * to the conversion process.
341
- *
342
- * @param config Conversion configuration.
343
- * @param config.view Pattern matching all view elements which should be converted.
344
- * @param config.model Name of the model marker, or a function that takes a view element and returns
345
- * a model marker name.
346
- * @param config.converterPriority Converter priority.
347
- */
348
- elementToMarker(config) {
349
- return this.add(upcastElementToMarker(config));
350
- }
351
- /**
352
- * View-to-model marker conversion helper.
353
- *
354
- * Converts view data created by {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`}
355
- * back to a model marker.
356
- *
357
- * This converter looks for specific view elements and view attributes that mark marker boundaries. See
358
- * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} to learn what view data
359
- * is expected by this converter.
360
- *
361
- * The `config.view` property is equal to the marker group name to convert.
362
- *
363
- * By default, this converter creates markers with the `group:name` name convention (to match the default `markerToData` conversion).
364
- *
365
- * The conversion configuration can take a function that will generate a marker name.
366
- * If such function is set as the `config.model` parameter, it is passed the `name` part from the view element or attribute and it is
367
- * expected to return a string with the marker name.
368
- *
369
- * Basic usage:
370
- *
371
- * ```ts
372
- * // Using the default conversion.
373
- * // In this case, all markers from the `comment` group will be converted.
374
- * // The conversion will look for `<comment-start>` and `<comment-end>` tags and
375
- * // `data-comment-start-before`, `data-comment-start-after`,
376
- * // `data-comment-end-before` and `data-comment-end-after` attributes.
377
- * editor.conversion.for( 'upcast' ).dataToMarker( {
378
- * view: 'comment'
379
- * } );
380
- * ```
381
- *
382
- * An example of a model that may be generated by this conversion:
383
- *
384
- * ```
385
- * // View:
386
- * <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p>
387
- * <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure>
388
- *
389
- * // Model:
390
- * <paragraph>Foo[bar</paragraph>
391
- * <imageBlock src="abc.jpg"></imageBlock>]
392
- * ```
393
- *
394
- * Where `[]` are boundaries of a marker that will receive the `comment:commentId:uid` name.
395
- *
396
- * Other examples of usage:
397
- *
398
- * ```ts
399
- * // Using a custom function which is the same as the default conversion:
400
- * editor.conversion.for( 'upcast' ).dataToMarker( {
401
- * view: 'comment',
402
- * model: ( name, conversionApi ) => 'comment:' + name,
403
- * } );
404
- *
405
- * // Using the converter priority:
406
- * editor.conversion.for( 'upcast' ).dataToMarker( {
407
- * view: 'comment',
408
- * model: ( name, conversionApi ) => 'comment:' + name,
409
- * converterPriority: 'high'
410
- * } );
411
- * ```
412
- *
413
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
414
- * to the conversion process.
415
- *
416
- * @param config Conversion configuration.
417
- * @param config.view The marker group name to convert.
418
- * @param config.model A function that takes the `name` part from the view element or attribute and
419
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the marker name.
420
- * @param config.converterPriority Converter priority.
421
- */
422
- dataToMarker(config) {
423
- return this.add(upcastDataToMarker(config));
424
- }
425
- }
426
- /**
427
- * Function factory, creates a converter that converts
428
- * {@link module:engine/view/documentfragment~ViewDocumentFragment view document fragment}
429
- * or all children of {@link module:engine/view/element~ViewElement} into
430
- * {@link module:engine/model/documentfragment~ModelDocumentFragment model document fragment}.
431
- * This is the "entry-point" converter for upcast (view to model conversion). This converter starts the conversion of all children
432
- * of passed view document fragment. Those children {@link module:engine/view/node~ViewNode view nodes} are then
433
- * handled by other converters.
434
- *
435
- * This also a "default", last resort converter for all view elements that has not been converted by other converters.
436
- * When a view element is being converted to the model but it does not have converter specified, that view element
437
- * will be converted to {@link module:engine/model/documentfragment~ModelDocumentFragment model document fragment} and returned.
438
- *
439
- * @returns Universal converter for view {@link module:engine/view/documentfragment~ViewDocumentFragment fragments} and
440
- * {@link module:engine/view/element~ViewElement elements} that returns
441
- * {@link module:engine/model/documentfragment~ModelDocumentFragment model fragment} with children of converted view item.
442
- *
443
- * @internal
444
- */
445
- export function convertToModelFragment() {
446
- return (evt, data, conversionApi) => {
447
- // Second argument in `consumable.consume` is discarded for ViewDocumentFragment but is needed for ViewElement.
448
- if (!data.modelRange && conversionApi.consumable.consume(data.viewItem, { name: true })) {
449
- const { modelRange, modelCursor } = conversionApi.convertChildren(data.viewItem, data.modelCursor);
450
- data.modelRange = modelRange;
451
- data.modelCursor = modelCursor;
452
- }
453
- };
454
- }
455
- /**
456
- * Function factory, creates a converter that converts
457
- * {@link module:engine/view/text~ViewText} to {@link module:engine/model/text~ModelText}.
458
- *
459
- * @returns {@link module:engine/view/text~ViewText View text} converter.
460
- * @internal
461
- */
462
- export function convertText() {
463
- return (evt, data, { schema, consumable, writer }) => {
464
- let position = data.modelCursor;
465
- // When node is already converted then do nothing.
466
- if (!consumable.test(data.viewItem)) {
467
- return;
468
- }
469
- if (!schema.checkChild(position, '$text')) {
470
- if (!isParagraphable(position, '$text', schema)) {
471
- return;
472
- }
473
- // Do not auto-paragraph whitespaces.
474
- if (data.viewItem.data.trim().length == 0) {
475
- return;
476
- }
477
- position = wrapInParagraph(position, writer);
478
- }
479
- consumable.consume(data.viewItem);
480
- const text = writer.createText(data.viewItem.data);
481
- writer.insert(text, position);
482
- data.modelRange = writer.createRange(position, position.getShiftedBy(text.offsetSize));
483
- data.modelCursor = data.modelRange.end;
484
- };
485
- }
486
- /**
487
- * Function factory, creates a callback function which converts a {@link module:engine/view/selection~ViewSelection
488
- * view selection} taken from the {@link module:engine/view/document~ViewDocument#event:selectionChange} event
489
- * and sets in on the {@link module:engine/model/document~ModelDocument#selection model}.
490
- *
491
- * **Note**: because there is no view selection change dispatcher nor any other advanced view selection to model
492
- * conversion mechanism, the callback should be set directly on view document.
493
- *
494
- * ```ts
495
- * view.document.on( 'selectionChange', convertSelectionChange( modelDocument, mapper ) );
496
- * ```
497
- *
498
- * @param model Data model.
499
- * @param mapper Conversion mapper.
500
- * @returns {@link module:engine/view/document~ViewDocument#event:selectionChange} callback function.
501
- * @internal
502
- */
503
- export function convertSelectionChange(model, mapper) {
504
- return (evt, data) => {
505
- const viewSelection = data.newSelection;
506
- const ranges = [];
507
- for (const viewRange of viewSelection.getRanges()) {
508
- ranges.push(mapper.toModelRange(viewRange));
509
- }
510
- const modelSelection = model.createSelection(ranges, { backward: viewSelection.isBackward });
511
- if (!modelSelection.isEqual(model.document.selection)) {
512
- model.change(writer => {
513
- writer.setSelection(modelSelection);
514
- });
515
- }
516
- };
517
- }
518
- /**
519
- * View element to model element conversion helper.
520
- *
521
- * See {@link ~UpcastHelpers#elementToElement `.elementToElement()` upcast helper} for examples.
522
- *
523
- * @param config Conversion configuration.
524
- * @param config.view Pattern matching all view elements which should be converted. If not
525
- * set, the converter will fire for every view element.
526
- * @param config.model Name of the model element, a model element
527
- * instance or a function that takes a view element and returns a model element. The model element will be inserted in the model.
528
- * @param config.converterPriority Converter priority.
529
- * @returns Conversion helper.
530
- */
531
- function upcastElementToElement(config) {
532
- config = cloneDeep(config);
533
- const converter = prepareToElementConverter(config);
534
- const elementName = getViewElementNameFromConfig(config.view);
535
- const eventName = elementName ? `element:${elementName}` : 'element';
536
- return (dispatcher) => {
537
- dispatcher.on(eventName, converter, { priority: config.converterPriority || 'normal' });
538
- };
539
- }
540
- /**
541
- * View element to model attribute conversion helper.
542
- *
543
- * See {@link ~UpcastHelpers#elementToAttribute `.elementToAttribute()` upcast helper} for examples.
544
- *
545
- * @param config Conversion configuration.
546
- * @param config.view Pattern matching all view elements which should be converted.
547
- * @param config.model Model attribute key or an object with `key` and `value` properties, describing
548
- * the model attribute. `value` property may be set as a function that takes a view element and returns the value.
549
- * If `String` is given, the model attribute value will be set to `true`.
550
- * @param config.converterPriority Converter priority. Defaults to `low`.
551
- * @returns Conversion helper.
552
- */
553
- function upcastElementToAttribute(config) {
554
- config = cloneDeep(config);
555
- normalizeModelAttributeConfig(config);
556
- const converter = prepareToAttributeConverter(config, false);
557
- const elementName = getViewElementNameFromConfig(config.view);
558
- const eventName = elementName ? `element:${elementName}` : 'element';
559
- return (dispatcher) => {
560
- dispatcher.on(eventName, converter, { priority: config.converterPriority || 'low' });
561
- };
562
- }
563
- /**
564
- * View attribute to model attribute conversion helper.
565
- *
566
- * See {@link ~UpcastHelpers#attributeToAttribute `.attributeToAttribute()` upcast helper} for examples.
567
- *
568
- * @param config Conversion configuration.
569
- * @param config.view Specifies which view attribute will be converted. If a `String` is passed,
570
- * attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
571
- * specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
572
- * property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
573
- * a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
574
- * @param config.model Model attribute key or an object with `key` and `value` properties, describing
575
- * the model attribute. `value` property may be set as a function that takes a view element and returns the value.
576
- * If `String` is given, the model attribute value will be same as view attribute value.
577
- * @param config.converterPriority Converter priority. Defaults to `low`.
578
- * @returns Conversion helper.
579
- */
580
- function upcastAttributeToAttribute(config) {
581
- config = cloneDeep(config);
582
- let viewKey = null;
583
- if (typeof config.view == 'string' || config.view.key) {
584
- viewKey = normalizeViewAttributeKeyValueConfig(config);
585
- }
586
- normalizeModelAttributeConfig(config, viewKey);
587
- const converter = prepareToAttributeConverter(config, true);
588
- return (dispatcher) => {
589
- dispatcher.on('element', converter, { priority: config.converterPriority || 'low' });
590
- };
591
- }
592
- /**
593
- * View element to model marker conversion helper.
594
- *
595
- * See {@link ~UpcastHelpers#elementToMarker `.elementToMarker()` upcast helper} for examples.
596
- *
597
- * @param config Conversion configuration.
598
- * @param config.view Pattern matching all view elements which should be converted.
599
- * @param config.model Name of the model marker, or a function that takes a view element and returns
600
- * a model marker name.
601
- * @param config.converterPriority Converter priority.
602
- * @returns Conversion helper.
603
- */
604
- function upcastElementToMarker(config) {
605
- const model = normalizeElementToMarkerModelConfig(config.model);
606
- return upcastElementToElement({ ...config, model });
607
- }
608
- /**
609
- * View data to model marker conversion helper.
610
- *
611
- * See {@link ~UpcastHelpers#dataToMarker} to learn more.
612
- *
613
- * @returns Conversion helper.
614
- */
615
- function upcastDataToMarker(config) {
616
- config = cloneDeep(config);
617
- // Default conversion.
618
- if (!config.model) {
619
- config.model = name => {
620
- return name ? config.view + ':' + name : config.view;
621
- };
622
- }
623
- const normalizedConfig = {
624
- view: config.view,
625
- model: config.model
626
- };
627
- const converterStart = prepareToElementConverter(normalizeDataToMarkerConfig(normalizedConfig, 'start'));
628
- const converterEnd = prepareToElementConverter(normalizeDataToMarkerConfig(normalizedConfig, 'end'));
629
- return (dispatcher) => {
630
- dispatcher.on(`element:${config.view}-start`, converterStart, { priority: config.converterPriority || 'normal' });
631
- dispatcher.on(`element:${config.view}-end`, converterEnd, { priority: config.converterPriority || 'normal' });
632
- // Below is a hack that is needed to properly handle `converterPriority` for both elements and attributes.
633
- // Attribute conversion needs to be performed *after* element conversion.
634
- // This converter handles both element conversion and attribute conversion, which means that if a single
635
- // `config.converterPriority` is used, it will lead to problems. For example, if the `'high'` priority is used,
636
- // the attribute conversion will be performed before a lot of element upcast converters.
637
- // On the other hand, we want to support `config.converterPriority` and converter overwriting.
638
- //
639
- // To make it work, we need to do some extra processing for priority for attribute converter.
640
- // Priority `'low'` value should be the base value and then we will change it depending on `config.converterPriority` value.
641
- //
642
- // This hack probably would not be needed if attributes are upcasted separately.
643
- //
644
- const basePriority = priorities.low;
645
- const maxPriority = priorities.highest;
646
- const priorityFactor = priorities.get(config.converterPriority) / maxPriority; // Number in range [ -1, 1 ].
647
- dispatcher.on('element', upcastAttributeToMarker(normalizedConfig), { priority: basePriority + priorityFactor });
648
- };
649
- }
650
- /**
651
- * Function factory, returns a callback function which converts view attributes to a model marker.
652
- *
653
- * The converter looks for elements with `data-group-start-before`, `data-group-start-after`, `data-group-end-before`
654
- * and `data-group-end-after` attributes and inserts `$marker` model elements before/after those elements.
655
- * `group` part is specified in `config.view`.
656
- *
657
- * @returns Marker converter.
658
- */
659
- function upcastAttributeToMarker(config) {
660
- return (evt, data, conversionApi) => {
661
- const attrName = `data-${config.view}`;
662
- // Check if any attribute for the given view item can be consumed before changing the conversion data
663
- // and consuming view items with these attributes.
664
- if (!conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-end-after' }) &&
665
- !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-start-after' }) &&
666
- !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-end-before' }) &&
667
- !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-start-before' })) {
668
- return;
669
- }
670
- // This converter wants to add a model element, marking a marker, before/after an element (or maybe even group of elements).
671
- // To do that, we can use `data.modelRange` which is set on an element (or a group of elements) that has been upcasted.
672
- // But, if the processed view element has not been upcasted yet (it does not have been converted), we need to
673
- // fire conversion for its children first, then we will have `data.modelRange` available.
674
- if (!data.modelRange) {
675
- Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
676
- }
677
- if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-end-after' })) {
678
- addMarkerElements(data.modelRange.end, data.viewItem.getAttribute(attrName + '-end-after').split(','));
679
- }
680
- if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-start-after' })) {
681
- addMarkerElements(data.modelRange.end, data.viewItem.getAttribute(attrName + '-start-after').split(','));
682
- }
683
- if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-end-before' })) {
684
- addMarkerElements(data.modelRange.start, data.viewItem.getAttribute(attrName + '-end-before').split(','));
685
- }
686
- if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-start-before' })) {
687
- addMarkerElements(data.modelRange.start, data.viewItem.getAttribute(attrName + '-start-before').split(','));
688
- }
689
- function addMarkerElements(position, markerViewNames) {
690
- for (const markerViewName of markerViewNames) {
691
- const markerName = config.model(markerViewName, conversionApi);
692
- const element = conversionApi.writer.createElement('$marker', { 'data-name': markerName });
693
- conversionApi.writer.insert(element, position);
694
- if (data.modelCursor.isEqual(position)) {
695
- data.modelCursor = data.modelCursor.getShiftedBy(1);
696
- }
697
- else {
698
- data.modelCursor = data.modelCursor._getTransformedByInsertion(position, 1);
699
- }
700
- data.modelRange = data.modelRange._getTransformedByInsertion(position, 1)[0];
701
- }
702
- }
703
- };
704
- }
705
- /**
706
- * Helper function for from-view-element conversion. Checks if `config.view` directly specifies converted view element's name
707
- * and if so, returns it.
708
- *
709
- * @param config Conversion view config.
710
- * @returns View element name or `null` if name is not directly set.
711
- */
712
- function getViewElementNameFromConfig(viewConfig) {
713
- if (typeof viewConfig == 'string') {
714
- return viewConfig;
715
- }
716
- if (typeof viewConfig == 'object' && typeof viewConfig.name == 'string') {
717
- return viewConfig.name;
718
- }
719
- return null;
720
- }
721
- /**
722
- * Helper for to-model-element conversion. Takes a config object and returns a proper converter function.
723
- *
724
- * @param config Conversion configuration.
725
- * @returns View to model converter.
726
- */
727
- function prepareToElementConverter(config) {
728
- const matcher = new Matcher(config.view);
729
- return (evt, data, conversionApi) => {
730
- const matcherResult = matcher.match(data.viewItem);
731
- if (!matcherResult) {
732
- return;
733
- }
734
- const match = matcherResult.match;
735
- // Force consuming element's name.
736
- match.name = true;
737
- if (!conversionApi.consumable.test(data.viewItem, match)) {
738
- return;
739
- }
740
- const modelElement = getModelElement(config.model, data.viewItem, conversionApi);
741
- if (!modelElement) {
742
- return;
743
- }
744
- if (!conversionApi.safeInsert(modelElement, data.modelCursor)) {
745
- return;
746
- }
747
- conversionApi.consumable.consume(data.viewItem, match);
748
- conversionApi.convertChildren(data.viewItem, modelElement);
749
- conversionApi.updateConversionResult(modelElement, data);
750
- };
751
- }
752
- /**
753
- * Helper function for upcasting-to-element converter. Takes the model configuration, the converted view element
754
- * and a writer instance and returns a model element instance to be inserted in the model.
755
- *
756
- * @param model Model conversion configuration.
757
- * @param input The converted view node.
758
- * @param conversionApi The upcast conversion API.
759
- */
760
- function getModelElement(model, input, conversionApi) {
761
- if (model instanceof Function) {
762
- return model(input, conversionApi);
763
- }
764
- else {
765
- return conversionApi.writer.createElement(model);
766
- }
767
- }
768
- /**
769
- * Helper function view-attribute-to-model-attribute helper. Normalizes `config.view` which was set as `String` or
770
- * as an `Object` with `key`, `value` and `name` properties. Normalized `config.view` has is compatible with
771
- * {@link module:engine/view/matcher~MatcherPattern}.
772
- *
773
- * @param config Conversion config.
774
- * @returns Key of the converted view attribute.
775
- */
776
- function normalizeViewAttributeKeyValueConfig(config) {
777
- if (typeof config.view == 'string') {
778
- config.view = { key: config.view };
779
- }
780
- const key = config.view.key;
781
- const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
782
- let normalized;
783
- if (key == 'class' || key == 'style') {
784
- const keyName = key == 'class' ? 'classes' : 'styles';
785
- normalized = {
786
- [keyName]: value
787
- };
788
- }
789
- else {
790
- normalized = {
791
- attributes: {
792
- [key]: value
793
- }
794
- };
795
- }
796
- if (config.view.name) {
797
- normalized.name = config.view.name;
798
- }
799
- config.view = normalized;
800
- return key;
801
- }
802
- /**
803
- * Helper function that normalizes `config.model` in from-model-attribute conversion. `config.model` can be set
804
- * as a `String`, an `Object` with only `key` property or an `Object` with `key` and `value` properties. Normalized
805
- * `config.model` is an `Object` with `key` and `value` properties.
806
- *
807
- * @param config Conversion config.
808
- * @param viewAttributeKeyToCopy Key of the converted view attribute. If it is set, model attribute value
809
- * will be equal to view attribute value.
810
- */
811
- function normalizeModelAttributeConfig(config, viewAttributeKeyToCopy = null) {
812
- const defaultModelValue = viewAttributeKeyToCopy === null ? true :
813
- (viewElement) => viewElement.getAttribute(viewAttributeKeyToCopy);
814
- const key = typeof config.model != 'object' ? config.model : config.model.key;
815
- const value = typeof config.model != 'object' || typeof config.model.value == 'undefined' ? defaultModelValue : config.model.value;
816
- config.model = { key, value };
817
- }
818
- /**
819
- * Helper for to-model-attribute conversion. Takes the model attribute name and conversion configuration and returns
820
- * a proper converter function.
821
- *
822
- * @param config Conversion configuration. It is possible to provide multiple configurations in an array.
823
- * @param shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
824
- * on all elements in the range.
825
- */
826
- function prepareToAttributeConverter(config, shallow) {
827
- const matcher = new Matcher(config.view);
828
- return (evt, data, conversionApi) => {
829
- // Converting an attribute of an element that has not been converted to anything does not make sense
830
- // because there will be nowhere to set that attribute on. At this stage, the element should've already
831
- // been converted (https://github.com/ckeditor/ckeditor5/issues/11000).
832
- if (!data.modelRange && shallow) {
833
- return;
834
- }
835
- const match = matcher.match(data.viewItem);
836
- // If there is no match, this callback should not do anything.
837
- if (!match) {
838
- return;
839
- }
840
- if (onlyViewNameIsDefined(config.view, data.viewItem)) {
841
- match.match.name = true;
842
- }
843
- else {
844
- // Do not test `name` consumable because it could get consumed already while upcasting some other attribute
845
- // on the same element (for example <span class="big" style="color: red">foo</span>).
846
- delete match.match.name;
847
- }
848
- // Try to consume appropriate values from consumable values list.
849
- if (!conversionApi.consumable.test(data.viewItem, match.match)) {
850
- return;
851
- }
852
- const modelKey = config.model.key;
853
- const modelValue = typeof config.model.value == 'function' ?
854
- config.model.value(data.viewItem, conversionApi, data) : config.model.value;
855
- // Do not convert if attribute building function returned falsy value.
856
- if (modelValue === null || modelValue === undefined) {
857
- return;
858
- }
859
- // Since we are converting to attribute we need a range on which we will set the attribute.
860
- // If the range is not created yet, let's create it by converting children of the current node first.
861
- if (!data.modelRange) {
862
- // Convert children and set conversion result as a current data.
863
- Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
864
- }
865
- // Set attribute on current `output`. `Schema` is checked inside this helper function.
866
- const attributeWasSet = setAttributeOn(data.modelRange, { key: modelKey, value: modelValue }, shallow, conversionApi);
867
- // It may happen that a converter will try to set an attribute that is not allowed in the given context.
868
- // In such a situation we cannot consume the attribute. See: https://github.com/ckeditor/ckeditor5/pull/9249#issuecomment-815658459.
869
- if (attributeWasSet) {
870
- // Verify if the element itself wasn't consumed yet. It could be consumed already while upcasting some other attribute
871
- // on the same element (for example <span class="big" style="color: red">foo</span>).
872
- // We need to consume it so other features (especially GHS) won't try to convert it.
873
- // Note that it's not tested by the other element-to-attribute converters whether an element was consumed before
874
- // (in case of converters that the element itself is just a context and not the primary information to convert).
875
- if (conversionApi.consumable.test(data.viewItem, { name: true })) {
876
- match.match.name = true;
877
- }
878
- conversionApi.consumable.consume(data.viewItem, match.match);
879
- }
880
- };
881
- }
882
- /**
883
- * Helper function that checks if element name should be consumed in attribute converters.
884
- *
885
- * @param viewConfig Conversion view config.
886
- */
887
- function onlyViewNameIsDefined(viewConfig, viewItem) {
888
- // https://github.com/ckeditor/ckeditor5-engine/issues/1786
889
- const configToTest = typeof viewConfig == 'function' ? viewConfig(viewItem) : viewConfig;
890
- if (typeof configToTest == 'object' && !getViewElementNameFromConfig(configToTest)) {
891
- return false;
892
- }
893
- return !configToTest.classes && !configToTest.attributes && !configToTest.styles;
894
- }
895
- /**
896
- * Helper function for to-model-attribute converter. Sets model attribute on given range.
897
- * Checks {@link module:engine/model/schema~ModelSchema} to ensure proper model structure.
898
- *
899
- * If any node on the given range has already defined an attribute with the same name, its value will not be updated.
900
- *
901
- * @param modelRange Model range on which attribute should be set.
902
- * @param modelAttribute Model attribute to set.
903
- * @param conversionApi Conversion API.
904
- * @param shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
905
- * on all elements in the range.
906
- * @returns `true` if attribute was set on at least one node from given `modelRange`.
907
- */
908
- function setAttributeOn(modelRange, modelAttribute, shallow, conversionApi) {
909
- let result = false;
910
- // Set attribute on each item in range according to Schema.
911
- for (const node of Array.from(modelRange.getItems({ shallow }))) {
912
- // Skip if not allowed.
913
- if (!conversionApi.schema.checkAttribute(node, modelAttribute.key)) {
914
- continue;
915
- }
916
- // Mark the node as consumed even if the attribute will not be updated because it's in a valid context (schema)
917
- // and would be converted if the attribute wouldn't be present. See #8921.
918
- result = true;
919
- // Do not override the attribute if it's already present.
920
- if (node.hasAttribute(modelAttribute.key)) {
921
- continue;
922
- }
923
- conversionApi.writer.setAttribute(modelAttribute.key, modelAttribute.value, node);
924
- }
925
- return result;
926
- }
927
- /**
928
- * Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastElementToMarker()`
929
- * function and converts it to a format that is supported by `upcastElementToElement()` function.
930
- */
931
- function normalizeElementToMarkerModelConfig(model) {
932
- return (viewElement, conversionApi) => {
933
- const markerName = typeof model == 'string' ? model : model(viewElement, conversionApi);
934
- return conversionApi.writer.createElement('$marker', { 'data-name': markerName });
935
- };
936
- }
937
- /**
938
- * Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastDataToMarker()`
939
- * function and converts it to a format that is supported by `upcastElementToElement()` function.
940
- */
941
- function normalizeDataToMarkerConfig(config, type) {
942
- const elementCreatorFunction = (viewElement, conversionApi) => {
943
- const viewName = viewElement.getAttribute('name');
944
- const markerName = config.model(viewName, conversionApi);
945
- return conversionApi.writer.createElement('$marker', { 'data-name': markerName });
946
- };
947
- return {
948
- // Upcast <markerGroup-start> and <markerGroup-end> elements.
949
- view: `${config.view}-${type}`,
950
- model: elementCreatorFunction
951
- };
952
- }