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