@ckeditor/ckeditor5-engine 47.6.1 → 48.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/engineconfig.d.ts +6 -15
  3. package/dist/index-editor.css +38 -15
  4. package/dist/index.css +37 -37
  5. package/dist/index.css.map +1 -1
  6. package/{src → dist}/index.d.ts +0 -1
  7. package/dist/index.js +588 -94
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/model/model.d.ts +10 -4
  10. package/{src → dist}/model/selection.d.ts +1 -1
  11. package/{src → dist}/view/downcastwriter.d.ts +3 -2
  12. package/{src → dist}/view/element.d.ts +2 -2
  13. package/{src → dist}/view/matcher.d.ts +4 -2
  14. package/dist/view/styles/background.d.ts +18 -0
  15. package/{src → dist}/view/styles/border.d.ts +0 -12
  16. package/{src → dist}/view/styles/margin.d.ts +0 -13
  17. package/{src → dist}/view/styles/padding.d.ts +0 -13
  18. package/{src → dist}/view/styles/utils.d.ts +12 -0
  19. package/package.json +20 -39
  20. package/src/controller/datacontroller.js +0 -522
  21. package/src/controller/editingcontroller.js +0 -181
  22. package/src/conversion/conversion.js +0 -606
  23. package/src/conversion/conversionhelpers.js +0 -33
  24. package/src/conversion/downcastdispatcher.js +0 -563
  25. package/src/conversion/downcasthelpers.js +0 -2160
  26. package/src/conversion/mapper.js +0 -1050
  27. package/src/conversion/modelconsumable.js +0 -331
  28. package/src/conversion/upcastdispatcher.js +0 -470
  29. package/src/conversion/upcasthelpers.js +0 -952
  30. package/src/conversion/viewconsumable.js +0 -541
  31. package/src/dataprocessor/basichtmlwriter.js +0 -22
  32. package/src/dataprocessor/dataprocessor.js +0 -5
  33. package/src/dataprocessor/htmldataprocessor.js +0 -107
  34. package/src/dataprocessor/htmlwriter.js +0 -5
  35. package/src/dataprocessor/xmldataprocessor.js +0 -127
  36. package/src/dev-utils/model.js +0 -396
  37. package/src/dev-utils/operationreplayer.js +0 -116
  38. package/src/dev-utils/utils.js +0 -122
  39. package/src/dev-utils/view.js +0 -990
  40. package/src/engineconfig.js +0 -5
  41. package/src/index.js +0 -134
  42. package/src/legacyerrors.js +0 -17
  43. package/src/model/batch.js +0 -98
  44. package/src/model/differ.js +0 -1288
  45. package/src/model/document.js +0 -398
  46. package/src/model/documentfragment.js +0 -332
  47. package/src/model/documentselection.js +0 -1026
  48. package/src/model/element.js +0 -323
  49. package/src/model/history.js +0 -206
  50. package/src/model/item.js +0 -5
  51. package/src/model/liveposition.js +0 -93
  52. package/src/model/liverange.js +0 -121
  53. package/src/model/markercollection.js +0 -436
  54. package/src/model/model.js +0 -866
  55. package/src/model/node.js +0 -371
  56. package/src/model/nodelist.js +0 -244
  57. package/src/model/operation/attributeoperation.js +0 -172
  58. package/src/model/operation/detachoperation.js +0 -87
  59. package/src/model/operation/insertoperation.js +0 -153
  60. package/src/model/operation/markeroperation.js +0 -136
  61. package/src/model/operation/mergeoperation.js +0 -184
  62. package/src/model/operation/moveoperation.js +0 -179
  63. package/src/model/operation/nooperation.js +0 -48
  64. package/src/model/operation/operation.js +0 -78
  65. package/src/model/operation/operationfactory.js +0 -44
  66. package/src/model/operation/renameoperation.js +0 -128
  67. package/src/model/operation/rootattributeoperation.js +0 -173
  68. package/src/model/operation/rootoperation.js +0 -106
  69. package/src/model/operation/splitoperation.js +0 -214
  70. package/src/model/operation/transform.js +0 -2211
  71. package/src/model/operation/utils.js +0 -217
  72. package/src/model/position.js +0 -1041
  73. package/src/model/range.js +0 -880
  74. package/src/model/rootelement.js +0 -82
  75. package/src/model/schema.js +0 -1542
  76. package/src/model/selection.js +0 -814
  77. package/src/model/text.js +0 -92
  78. package/src/model/textproxy.js +0 -202
  79. package/src/model/treewalker.js +0 -313
  80. package/src/model/typecheckable.js +0 -16
  81. package/src/model/utils/autoparagraphing.js +0 -63
  82. package/src/model/utils/deletecontent.js +0 -509
  83. package/src/model/utils/getselectedcontent.js +0 -126
  84. package/src/model/utils/insertcontent.js +0 -750
  85. package/src/model/utils/insertobject.js +0 -135
  86. package/src/model/utils/modifyselection.js +0 -187
  87. package/src/model/utils/selection-post-fixer.js +0 -264
  88. package/src/model/writer.js +0 -1318
  89. package/src/view/attributeelement.js +0 -220
  90. package/src/view/containerelement.js +0 -91
  91. package/src/view/datatransfer.js +0 -106
  92. package/src/view/document.js +0 -139
  93. package/src/view/documentfragment.js +0 -251
  94. package/src/view/documentselection.js +0 -270
  95. package/src/view/domconverter.js +0 -1661
  96. package/src/view/downcastwriter.js +0 -1589
  97. package/src/view/editableelement.js +0 -74
  98. package/src/view/element.js +0 -1053
  99. package/src/view/elementdefinition.js +0 -5
  100. package/src/view/emptyelement.js +0 -83
  101. package/src/view/filler.js +0 -161
  102. package/src/view/item.js +0 -5
  103. package/src/view/matcher.js +0 -437
  104. package/src/view/node.js +0 -238
  105. package/src/view/observer/arrowkeysobserver.js +0 -40
  106. package/src/view/observer/bubblingemittermixin.js +0 -215
  107. package/src/view/observer/bubblingeventinfo.js +0 -49
  108. package/src/view/observer/clickobserver.js +0 -26
  109. package/src/view/observer/compositionobserver.js +0 -64
  110. package/src/view/observer/domeventdata.js +0 -63
  111. package/src/view/observer/domeventobserver.js +0 -81
  112. package/src/view/observer/fakeselectionobserver.js +0 -95
  113. package/src/view/observer/focusobserver.js +0 -166
  114. package/src/view/observer/inputobserver.js +0 -236
  115. package/src/view/observer/keyobserver.js +0 -36
  116. package/src/view/observer/mouseobserver.js +0 -26
  117. package/src/view/observer/mutationobserver.js +0 -219
  118. package/src/view/observer/observer.js +0 -92
  119. package/src/view/observer/pointerobserver.js +0 -26
  120. package/src/view/observer/selectionobserver.js +0 -318
  121. package/src/view/observer/tabobserver.js +0 -42
  122. package/src/view/observer/touchobserver.js +0 -26
  123. package/src/view/placeholder.js +0 -285
  124. package/src/view/position.js +0 -341
  125. package/src/view/range.js +0 -451
  126. package/src/view/rawelement.js +0 -115
  127. package/src/view/renderer.js +0 -1148
  128. package/src/view/rooteditableelement.js +0 -78
  129. package/src/view/selection.js +0 -594
  130. package/src/view/styles/background.d.ts +0 -33
  131. package/src/view/styles/background.js +0 -74
  132. package/src/view/styles/border.js +0 -316
  133. package/src/view/styles/margin.js +0 -34
  134. package/src/view/styles/padding.js +0 -34
  135. package/src/view/styles/utils.js +0 -219
  136. package/src/view/stylesmap.js +0 -941
  137. package/src/view/text.js +0 -110
  138. package/src/view/textproxy.js +0 -136
  139. package/src/view/tokenlist.js +0 -194
  140. package/src/view/treewalker.js +0 -389
  141. package/src/view/typecheckable.js +0 -19
  142. package/src/view/uielement.js +0 -194
  143. package/src/view/upcastwriter.js +0 -363
  144. package/src/view/view.js +0 -579
  145. package/theme/placeholder.css +0 -36
  146. package/theme/renderer.css +0 -9
  147. /package/{src → dist}/controller/datacontroller.d.ts +0 -0
  148. /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
  149. /package/{src → dist}/conversion/conversion.d.ts +0 -0
  150. /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
  151. /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
  152. /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
  153. /package/{src → dist}/conversion/mapper.d.ts +0 -0
  154. /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
  155. /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
  156. /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
  157. /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
  158. /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
  159. /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
  160. /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
  161. /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
  162. /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
  163. /package/{src → dist}/dev-utils/model.d.ts +0 -0
  164. /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
  165. /package/{src → dist}/dev-utils/utils.d.ts +0 -0
  166. /package/{src → dist}/dev-utils/view.d.ts +0 -0
  167. /package/{src → dist}/legacyerrors.d.ts +0 -0
  168. /package/{src → dist}/model/batch.d.ts +0 -0
  169. /package/{src → dist}/model/differ.d.ts +0 -0
  170. /package/{src → dist}/model/document.d.ts +0 -0
  171. /package/{src → dist}/model/documentfragment.d.ts +0 -0
  172. /package/{src → dist}/model/documentselection.d.ts +0 -0
  173. /package/{src → dist}/model/element.d.ts +0 -0
  174. /package/{src → dist}/model/history.d.ts +0 -0
  175. /package/{src → dist}/model/item.d.ts +0 -0
  176. /package/{src → dist}/model/liveposition.d.ts +0 -0
  177. /package/{src → dist}/model/liverange.d.ts +0 -0
  178. /package/{src → dist}/model/markercollection.d.ts +0 -0
  179. /package/{src → dist}/model/node.d.ts +0 -0
  180. /package/{src → dist}/model/nodelist.d.ts +0 -0
  181. /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
  182. /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
  183. /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
  184. /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
  185. /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
  186. /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
  187. /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
  188. /package/{src → dist}/model/operation/operation.d.ts +0 -0
  189. /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
  190. /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
  191. /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
  192. /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
  193. /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
  194. /package/{src → dist}/model/operation/transform.d.ts +0 -0
  195. /package/{src → dist}/model/operation/utils.d.ts +0 -0
  196. /package/{src → dist}/model/position.d.ts +0 -0
  197. /package/{src → dist}/model/range.d.ts +0 -0
  198. /package/{src → dist}/model/rootelement.d.ts +0 -0
  199. /package/{src → dist}/model/schema.d.ts +0 -0
  200. /package/{src → dist}/model/text.d.ts +0 -0
  201. /package/{src → dist}/model/textproxy.d.ts +0 -0
  202. /package/{src → dist}/model/treewalker.d.ts +0 -0
  203. /package/{src → dist}/model/typecheckable.d.ts +0 -0
  204. /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
  205. /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
  206. /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
  207. /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
  208. /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
  209. /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
  210. /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
  211. /package/{src → dist}/model/writer.d.ts +0 -0
  212. /package/{src → dist}/view/attributeelement.d.ts +0 -0
  213. /package/{src → dist}/view/containerelement.d.ts +0 -0
  214. /package/{src → dist}/view/datatransfer.d.ts +0 -0
  215. /package/{src → dist}/view/document.d.ts +0 -0
  216. /package/{src → dist}/view/documentfragment.d.ts +0 -0
  217. /package/{src → dist}/view/documentselection.d.ts +0 -0
  218. /package/{src → dist}/view/domconverter.d.ts +0 -0
  219. /package/{src → dist}/view/editableelement.d.ts +0 -0
  220. /package/{src → dist}/view/elementdefinition.d.ts +0 -0
  221. /package/{src → dist}/view/emptyelement.d.ts +0 -0
  222. /package/{src → dist}/view/filler.d.ts +0 -0
  223. /package/{src → dist}/view/item.d.ts +0 -0
  224. /package/{src → dist}/view/node.d.ts +0 -0
  225. /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
  226. /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
  227. /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
  228. /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
  229. /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
  230. /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
  231. /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
  232. /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
  233. /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
  234. /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
  235. /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
  236. /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
  237. /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
  238. /package/{src → dist}/view/observer/observer.d.ts +0 -0
  239. /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
  240. /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
  241. /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
  242. /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
  243. /package/{src → dist}/view/placeholder.d.ts +0 -0
  244. /package/{src → dist}/view/position.d.ts +0 -0
  245. /package/{src → dist}/view/range.d.ts +0 -0
  246. /package/{src → dist}/view/rawelement.d.ts +0 -0
  247. /package/{src → dist}/view/renderer.d.ts +0 -0
  248. /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
  249. /package/{src → dist}/view/selection.d.ts +0 -0
  250. /package/{src → dist}/view/stylesmap.d.ts +0 -0
  251. /package/{src → dist}/view/text.d.ts +0 -0
  252. /package/{src → dist}/view/textproxy.d.ts +0 -0
  253. /package/{src → dist}/view/tokenlist.d.ts +0 -0
  254. /package/{src → dist}/view/treewalker.d.ts +0 -0
  255. /package/{src → dist}/view/typecheckable.d.ts +0 -0
  256. /package/{src → dist}/view/uielement.d.ts +0 -0
  257. /package/{src → dist}/view/upcastwriter.d.ts +0 -0
  258. /package/{src → dist}/view/view.d.ts +0 -0
@@ -1,1542 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module engine/model/schema
7
- */
8
- import { ModelElement } from './element.js';
9
- import { ModelPosition } from './position.js';
10
- import { ModelRange } from './range.js';
11
- import { ModelText } from './text.js';
12
- import { ModelTreeWalker } from './treewalker.js';
13
- import { CKEditorError, first, ObservableMixin } from '@ckeditor/ckeditor5-utils';
14
- /**
15
- * The model's schema. It defines the allowed and disallowed structures of nodes as well as nodes' attributes.
16
- * The schema is usually defined by the features and based on them, the editing framework and features
17
- * make decisions on how to change and process the model.
18
- *
19
- * The instance of schema is available in {@link module:engine/model/model~Model#schema `editor.model.schema`}.
20
- *
21
- * Read more about the schema in:
22
- *
23
- * * The {@glink framework/architecture/editing-engine#schema schema section} of the
24
- * {@glink framework/architecture/editing-engine Introduction to the Editing engine architecture} guide.
25
- * * The {@glink framework/deep-dive/schema Schema deep-dive} guide.
26
- */
27
- export class ModelSchema extends /* #__PURE__ */ ObservableMixin() {
28
- _sourceDefinitions = {};
29
- /**
30
- * A dictionary containing attribute properties.
31
- */
32
- _attributeProperties = Object.create(null);
33
- /**
34
- * Stores additional callbacks registered for schema items, which are evaluated when {@link ~ModelSchema#checkChild} is called.
35
- *
36
- * Keys are schema item names for which the callbacks are registered. Values are arrays with the callbacks.
37
- *
38
- * Some checks are added under {@link ~ModelSchema#_genericCheckSymbol} key, these are
39
- * evaluated for every {@link ~ModelSchema#checkChild} call.
40
- */
41
- _customChildChecks = new Map();
42
- /**
43
- * Stores additional callbacks registered for attribute names, which are evaluated when {@link ~ModelSchema#checkAttribute} is called.
44
- *
45
- * Keys are schema attribute names for which the callbacks are registered. Values are arrays with the callbacks.
46
- *
47
- * Some checks are added under {@link ~ModelSchema#_genericCheckSymbol} key, these are evaluated for every
48
- * {@link ~ModelSchema#checkAttribute} call.
49
- */
50
- _customAttributeChecks = new Map();
51
- _genericCheckSymbol = Symbol('$generic');
52
- _compiledDefinitions;
53
- /**
54
- * Creates a schema instance.
55
- */
56
- constructor() {
57
- super();
58
- this.decorate('checkChild');
59
- this.decorate('checkAttribute');
60
- this.on('checkAttribute', (evt, args) => {
61
- args[0] = new ModelSchemaContext(args[0]);
62
- }, { priority: 'highest' });
63
- this.on('checkChild', (evt, args) => {
64
- args[0] = new ModelSchemaContext(args[0]);
65
- args[1] = this.getDefinition(args[1]);
66
- }, { priority: 'highest' });
67
- }
68
- /**
69
- * Registers a schema item. Can only be called once for every item name.
70
- *
71
- * ```ts
72
- * schema.register( 'paragraph', {
73
- * inheritAllFrom: '$block'
74
- * } );
75
- * ```
76
- */
77
- register(itemName, definition) {
78
- if (this._sourceDefinitions[itemName]) {
79
- /**
80
- * A single item cannot be registered twice in the schema.
81
- *
82
- * This situation may happen when:
83
- *
84
- * * Two or more plugins called {@link module:engine/model/schema~ModelSchema#register `register()`} with the same name.
85
- * This will usually mean that there is a collision between plugins which try to use the same element in the model.
86
- * Unfortunately, the only way to solve this is by modifying one of these plugins to use a unique model element name.
87
- * * A single plugin was loaded twice. This happens when it is installed by npm/yarn in two versions
88
- * and usually means one or more of the following issues:
89
- * * a version mismatch (two of your dependencies require two different versions of this plugin),
90
- * * incorrect imports (this plugin is somehow imported twice in a way which confuses webpack),
91
- * * mess in `node_modules/` (`rm -rf node_modules/` may help).
92
- *
93
- * **Note:** Check the logged `itemName` to better understand which plugin was duplicated/conflicting.
94
- *
95
- * @param itemName The name of the model element that is being registered twice.
96
- * @error schema-cannot-register-item-twice
97
- */
98
- throw new CKEditorError('schema-cannot-register-item-twice', this, {
99
- itemName
100
- });
101
- }
102
- this._sourceDefinitions[itemName] = [
103
- Object.assign({}, definition)
104
- ];
105
- this._clearCache();
106
- }
107
- /**
108
- * Extends a {@link #register registered} item's definition.
109
- *
110
- * Extending properties such as `allowIn` will add more items to the existing properties,
111
- * while redefining properties such as `isBlock` will override the previously defined ones.
112
- *
113
- * ```ts
114
- * schema.register( 'foo', {
115
- * allowIn: '$root',
116
- * isBlock: true;
117
- * } );
118
- * schema.extend( 'foo', {
119
- * allowIn: 'blockQuote',
120
- * isBlock: false
121
- * } );
122
- *
123
- * schema.getDefinition( 'foo' );
124
- * // {
125
- * // allowIn: [ '$root', 'blockQuote' ],
126
- * // isBlock: false
127
- * // }
128
- * ```
129
- */
130
- extend(itemName, definition) {
131
- if (!this._sourceDefinitions[itemName]) {
132
- /**
133
- * Cannot extend an item which was not registered yet.
134
- *
135
- * This error happens when a plugin tries to extend the schema definition of an item which was not
136
- * {@link module:engine/model/schema~ModelSchema#register registered} yet.
137
- *
138
- * @param itemName The name of the model element which is being extended.
139
- * @error schema-cannot-extend-missing-item
140
- */
141
- throw new CKEditorError('schema-cannot-extend-missing-item', this, {
142
- itemName
143
- });
144
- }
145
- this._sourceDefinitions[itemName].push(Object.assign({}, definition));
146
- this._clearCache();
147
- }
148
- /**
149
- * Returns data of all registered items.
150
- *
151
- * This method should normally be used for reflection purposes (e.g. defining a clone of a certain element,
152
- * checking a list of all block elements, etc).
153
- * Use specific methods (such as {@link #checkChild `checkChild()`} or {@link #isLimit `isLimit()`})
154
- * in other cases.
155
- */
156
- getDefinitions() {
157
- if (!this._compiledDefinitions) {
158
- this._compile();
159
- }
160
- return this._compiledDefinitions;
161
- }
162
- /**
163
- * Returns a definition of the given item or `undefined` if an item is not registered.
164
- *
165
- * This method should normally be used for reflection purposes (e.g. defining a clone of a certain element,
166
- * checking a list of all block elements, etc).
167
- * Use specific methods (such as {@link #checkChild `checkChild()`} or {@link #isLimit `isLimit()`})
168
- * in other cases.
169
- */
170
- getDefinition(item) {
171
- let itemName;
172
- if (typeof item == 'string') {
173
- itemName = item;
174
- }
175
- else if ('is' in item && (item.is('$text') || item.is('$textProxy'))) {
176
- itemName = '$text';
177
- }
178
- // Element or module:engine/model/schema~ModelSchemaContextItem.
179
- else {
180
- itemName = item.name;
181
- }
182
- return this.getDefinitions()[itemName];
183
- }
184
- /**
185
- * Returns `true` if the given item is registered in the schema.
186
- *
187
- * ```ts
188
- * schema.isRegistered( 'paragraph' ); // -> true
189
- * schema.isRegistered( editor.model.document.getRoot() ); // -> true
190
- * schema.isRegistered( 'foo' ); // -> false
191
- * ```
192
- */
193
- isRegistered(item) {
194
- return !!this.getDefinition(item);
195
- }
196
- /**
197
- * Returns `true` if the given item is defined to be
198
- * a block by the {@link module:engine/model/schema~ModelSchemaItemDefinition}'s `isBlock` property.
199
- *
200
- * ```ts
201
- * schema.isBlock( 'paragraph' ); // -> true
202
- * schema.isBlock( '$root' ); // -> false
203
- *
204
- * const paragraphElement = writer.createElement( 'paragraph' );
205
- * schema.isBlock( paragraphElement ); // -> true
206
- * ```
207
- *
208
- * See the {@glink framework/deep-dive/schema#block-elements Block elements} section of
209
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
210
- */
211
- isBlock(item) {
212
- const def = this.getDefinition(item);
213
- return !!(def && def.isBlock);
214
- }
215
- /**
216
- * Returns `true` if the given item should be treated as a limit element.
217
- *
218
- * It considers an item to be a limit element if its
219
- * {@link module:engine/model/schema~ModelSchemaItemDefinition}'s
220
- * {@link module:engine/model/schema~ModelSchemaItemDefinition#isLimit `isLimit`} or
221
- * {@link module:engine/model/schema~ModelSchemaItemDefinition#isObject `isObject`} property
222
- * was set to `true`.
223
- *
224
- * ```ts
225
- * schema.isLimit( 'paragraph' ); // -> false
226
- * schema.isLimit( '$root' ); // -> true
227
- * schema.isLimit( editor.model.document.getRoot() ); // -> true
228
- * schema.isLimit( 'imageBlock' ); // -> true
229
- * ```
230
- *
231
- * See the {@glink framework/deep-dive/schema#limit-elements Limit elements} section of
232
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
233
- */
234
- isLimit(item) {
235
- const def = this.getDefinition(item);
236
- if (!def) {
237
- return false;
238
- }
239
- return !!(def.isLimit || def.isObject);
240
- }
241
- /**
242
- * Returns `true` if the given item should be treated as an object element.
243
- *
244
- * It considers an item to be an object element if its
245
- * {@link module:engine/model/schema~ModelSchemaItemDefinition}'s
246
- * {@link module:engine/model/schema~ModelSchemaItemDefinition#isObject `isObject`} property
247
- * was set to `true`.
248
- *
249
- * ```ts
250
- * schema.isObject( 'paragraph' ); // -> false
251
- * schema.isObject( 'imageBlock' ); // -> true
252
- *
253
- * const imageElement = writer.createElement( 'imageBlock' );
254
- * schema.isObject( imageElement ); // -> true
255
- * ```
256
- *
257
- * See the {@glink framework/deep-dive/schema#object-elements Object elements} section of
258
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
259
- */
260
- isObject(item) {
261
- const def = this.getDefinition(item);
262
- if (!def) {
263
- return false;
264
- }
265
- // Note: Check out the implementation of #isLimit(), #isSelectable(), and #isContent()
266
- // to understand why these three constitute an object.
267
- return !!(def.isObject || (def.isLimit && def.isSelectable && def.isContent));
268
- }
269
- /**
270
- * Returns `true` if the given item is defined to be
271
- * an inline element by the {@link module:engine/model/schema~ModelSchemaItemDefinition}'s `isInline` property.
272
- *
273
- * ```ts
274
- * schema.isInline( 'paragraph' ); // -> false
275
- * schema.isInline( 'softBreak' ); // -> true
276
- *
277
- * const text = writer.createText( 'foo' );
278
- * schema.isInline( text ); // -> true
279
- * ```
280
- *
281
- * See the {@glink framework/deep-dive/schema#inline-elements Inline elements} section of
282
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
283
- */
284
- isInline(item) {
285
- const def = this.getDefinition(item);
286
- return !!(def && def.isInline);
287
- }
288
- /**
289
- * Returns `true` if the given item is defined to be
290
- * a selectable element by the {@link module:engine/model/schema~ModelSchemaItemDefinition}'s `isSelectable` property.
291
- *
292
- * ```ts
293
- * schema.isSelectable( 'paragraph' ); // -> false
294
- * schema.isSelectable( 'heading1' ); // -> false
295
- * schema.isSelectable( 'imageBlock' ); // -> true
296
- * schema.isSelectable( 'tableCell' ); // -> true
297
- *
298
- * const text = writer.createText( 'foo' );
299
- * schema.isSelectable( text ); // -> false
300
- * ```
301
- *
302
- * See the {@glink framework/deep-dive/schema#selectable-elements Selectable elements section} of
303
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
304
- */
305
- isSelectable(item) {
306
- const def = this.getDefinition(item);
307
- if (!def) {
308
- return false;
309
- }
310
- return !!(def.isSelectable || def.isObject);
311
- }
312
- /**
313
- * Returns `true` if the given item is defined to be
314
- * a content by the {@link module:engine/model/schema~ModelSchemaItemDefinition}'s `isContent` property.
315
- *
316
- * ```ts
317
- * schema.isContent( 'paragraph' ); // -> false
318
- * schema.isContent( 'heading1' ); // -> false
319
- * schema.isContent( 'imageBlock' ); // -> true
320
- * schema.isContent( 'horizontalLine' ); // -> true
321
- *
322
- * const text = writer.createText( 'foo' );
323
- * schema.isContent( text ); // -> true
324
- * ```
325
- *
326
- * See the {@glink framework/deep-dive/schema#content-elements Content elements section} of
327
- * the {@glink framework/deep-dive/schema Schema deep-dive} guide for more details.
328
- */
329
- isContent(item) {
330
- const def = this.getDefinition(item);
331
- if (!def) {
332
- return false;
333
- }
334
- return !!(def.isContent || def.isObject);
335
- }
336
- /**
337
- * Checks whether the given node can be a child of the given context.
338
- *
339
- * ```ts
340
- * schema.checkChild( model.document.getRoot(), paragraph ); // -> false
341
- *
342
- * schema.register( 'paragraph', {
343
- * allowIn: '$root'
344
- * } );
345
- *
346
- * schema.checkChild( model.document.getRoot(), paragraph ); // -> true
347
- * ```
348
- *
349
- * Both {@link module:engine/model/schema~ModelSchema#addChildCheck callback checks} and declarative rules (added when
350
- * {@link module:engine/model/schema~ModelSchema#register registering} and
351
- * {@link module:engine/model/schema~ModelSchema#extend extending} items)
352
- * are evaluated when this method is called.
353
- *
354
- * Note that callback checks have bigger priority than declarative rules checks and may overwrite them.
355
- *
356
- * Note that when verifying whether the given node can be a child of the given context, the schema also verifies the entire
357
- * context – from its root to its last element. Therefore, it is possible for `checkChild()` to return `false` even though
358
- * the `context` last element can contain the checked child. It happens if one of the `context` elements does not allow its child.
359
- * When `context` is verified, {@link module:engine/model/schema~ModelSchema#addChildCheck custom checks} are considered as well.
360
- *
361
- * @fires checkChild
362
- * @param context The context in which the child will be checked.
363
- * @param def The child to check.
364
- */
365
- checkChild(context, def) {
366
- // Note: `context` and `def` are already normalized here to `ModelSchemaContext` and `ModelSchemaCompiledItemDefinition`.
367
- if (!def) {
368
- return false;
369
- }
370
- return this._checkContextMatch(context, def);
371
- }
372
- /**
373
- * Checks whether the given attribute can be applied in the given context (on the last item of the context).
374
- *
375
- * ```ts
376
- * schema.checkAttribute( textNode, 'bold' ); // -> false
377
- *
378
- * schema.extend( '$text', {
379
- * allowAttributes: 'bold'
380
- * } );
381
- *
382
- * schema.checkAttribute( textNode, 'bold' ); // -> true
383
- * ```
384
- *
385
- * Both {@link module:engine/model/schema~ModelSchema#addAttributeCheck callback checks} and declarative rules (added when
386
- * {@link module:engine/model/schema~ModelSchema#register registering} and
387
- * {@link module:engine/model/schema~ModelSchema#extend extending} items)
388
- * are evaluated when this method is called.
389
- *
390
- * Note that callback checks have bigger priority than declarative rules checks and may overwrite them.
391
- *
392
- * @fires checkAttribute
393
- * @param context The context in which the attribute will be checked.
394
- * @param attributeName Name of attribute to check in the given context.
395
- */
396
- checkAttribute(context, attributeName) {
397
- // Note: `context` is already normalized here to `ModelSchemaContext`.
398
- const def = this.getDefinition(context.last);
399
- if (!def) {
400
- return false;
401
- }
402
- // First, check all attribute checks declared as callbacks.
403
- // Note that `_evaluateAttributeChecks()` will return `undefined` if neither child check was applicable (no decision was made).
404
- const isAllowed = this._evaluateAttributeChecks(context, attributeName);
405
- // If the decision was not made inside attribute check callbacks, then use declarative rules.
406
- return isAllowed !== undefined ? isAllowed : def.allowAttributes.includes(attributeName);
407
- }
408
- /**
409
- * Checks whether the given element (`elementToMerge`) can be merged with the specified base element (`positionOrBaseElement`).
410
- *
411
- * In other words – both elements are not a limit elements and whether `elementToMerge`'s children
412
- * {@link #checkChild are allowed} in the `positionOrBaseElement`.
413
- *
414
- * This check ensures that elements merged with {@link module:engine/model/writer~ModelWriter#merge `Writer#merge()`}
415
- * will be valid.
416
- *
417
- * Instead of elements, you can pass the instance of the {@link module:engine/model/position~ModelPosition} class as the
418
- * `positionOrBaseElement`. It means that the elements before and after the position will be checked whether they can be merged.
419
- *
420
- * @param positionOrBaseElement The position or base element to which the `elementToMerge` will be merged.
421
- * @param elementToMerge The element to merge. Required if `positionOrBaseElement` is an element.
422
- */
423
- checkMerge(positionOrBaseElement, elementToMerge) {
424
- if (positionOrBaseElement instanceof ModelPosition) {
425
- const nodeBefore = positionOrBaseElement.nodeBefore;
426
- const nodeAfter = positionOrBaseElement.nodeAfter;
427
- if (!(nodeBefore instanceof ModelElement)) {
428
- /**
429
- * The node before the merge position must be an element.
430
- *
431
- * @error schema-check-merge-no-element-before
432
- */
433
- throw new CKEditorError('schema-check-merge-no-element-before', this);
434
- }
435
- if (!(nodeAfter instanceof ModelElement)) {
436
- /**
437
- * The node after the merge position must be an element.
438
- *
439
- * @error schema-check-merge-no-element-after
440
- */
441
- throw new CKEditorError('schema-check-merge-no-element-after', this);
442
- }
443
- return this.checkMerge(nodeBefore, nodeAfter);
444
- }
445
- if (this.isLimit(positionOrBaseElement) || this.isLimit(elementToMerge)) {
446
- return false;
447
- }
448
- for (const child of elementToMerge.getChildren()) {
449
- if (!this.checkChild(positionOrBaseElement, child)) {
450
- return false;
451
- }
452
- }
453
- return true;
454
- }
455
- /**
456
- * Allows registering a callback to the {@link #checkChild} method calls.
457
- *
458
- * Callbacks allow you to implement rules which are not otherwise possible to achieve
459
- * by using the declarative API of {@link module:engine/model/schema~ModelSchemaItemDefinition}.
460
- *
461
- * Note that callback checks have bigger priority than declarative rules checks and may overwrite them.
462
- *
463
- * For example, by using this method you can disallow elements in specific contexts:
464
- *
465
- * ```ts
466
- * // Disallow `heading1` inside a `blockQuote` that is inside a table.
467
- * schema.addChildCheck( ( context, childDefinition ) => {
468
- * if ( context.endsWith( 'tableCell blockQuote' ) ) {
469
- * return false;
470
- * }
471
- * }, 'heading1' );
472
- * ```
473
- *
474
- * You can skip the optional `itemName` parameter to evaluate the callback for every `checkChild()` call.
475
- *
476
- * ```ts
477
- * // Inside specific custom element, allow only children, which allows for a specific attribute.
478
- * schema.addChildCheck( ( context, childDefinition ) => {
479
- * if ( context.endsWith( 'myElement' ) ) {
480
- * return childDefinition.allowAttributes.includes( 'myAttribute' );
481
- * }
482
- * } );
483
- * ```
484
- *
485
- * Please note that the generic callbacks may affect the editor performance and should be avoided if possible.
486
- *
487
- * When one of the callbacks makes a decision (returns `true` or `false`) the processing is finished and other callbacks are not fired.
488
- * Callbacks are fired in the order they were added, however generic callbacks are fired before callbacks added for a specified item.
489
- *
490
- * You can also use `checkChild` event, if you need even better control. The result from the example above could also be
491
- * achieved with following event callback:
492
- *
493
- * ```ts
494
- * schema.on( 'checkChild', ( evt, args ) => {
495
- * const context = args[ 0 ];
496
- * const childDefinition = args[ 1 ];
497
- *
498
- * if ( context.endsWith( 'myElement' ) ) {
499
- * // Prevent next listeners from being called.
500
- * evt.stop();
501
- * // Set the `checkChild()` return value.
502
- * evt.return = childDefinition.allowAttributes.includes( 'myAttribute' );
503
- * }
504
- * }, { priority: 'high' } );
505
- * ```
506
- *
507
- * Note that the callback checks and declarative rules checks are processed on `normal` priority.
508
- *
509
- * Adding callbacks this way can also negatively impact editor performance.
510
- *
511
- * @param callback The callback to be called. It is called with two parameters:
512
- * {@link module:engine/model/schema~ModelSchemaContext} (context) instance and
513
- * {@link module:engine/model/schema~ModelSchemaCompiledItemDefinition} (definition). The callback may return `true/false` to
514
- * override `checkChild()`'s return value. If it does not return a boolean value, the default algorithm (or other callbacks) will define
515
- * `checkChild()`'s return value.
516
- * @param itemName Name of the schema item for which the callback is registered. If specified, the callback will be run only for
517
- * `checkChild()` calls which `def` parameter matches the `itemName`. Otherwise, the callback will run for every `checkChild` call.
518
- */
519
- addChildCheck(callback, itemName) {
520
- const key = itemName !== undefined ? itemName : this._genericCheckSymbol;
521
- const checks = this._customChildChecks.get(key) || [];
522
- checks.push(callback);
523
- this._customChildChecks.set(key, checks);
524
- }
525
- /**
526
- * Allows registering a callback to the {@link #checkAttribute} method calls.
527
- *
528
- * Callbacks allow you to implement rules which are not otherwise possible to achieve
529
- * by using the declarative API of {@link module:engine/model/schema~ModelSchemaItemDefinition}.
530
- *
531
- * Note that callback checks have bigger priority than declarative rules checks and may overwrite them.
532
- *
533
- * For example, by using this method you can disallow setting attributes on nodes in specific contexts:
534
- *
535
- * ```ts
536
- * // Disallow setting `bold` on text inside `heading1` element:
537
- * schema.addAttributeCheck( context => {
538
- * if ( context.endsWith( 'heading1 $text' ) ) {
539
- * return false;
540
- * }
541
- * }, 'bold' );
542
- * ```
543
- *
544
- * You can skip the optional `attributeName` parameter to evaluate the callback for every `checkAttribute()` call.
545
- *
546
- * ```ts
547
- * // Disallow formatting attributes on text inside custom `myTitle` element:
548
- * schema.addAttributeCheck( ( context, attributeName ) => {
549
- * if ( context.endsWith( 'myTitle $text' ) && schema.getAttributeProperties( attributeName ).isFormatting ) {
550
- * return false;
551
- * }
552
- * } );
553
- * ```
554
- *
555
- * Please note that the generic callbacks may affect the editor performance and should be avoided if possible.
556
- *
557
- * When one of the callbacks makes a decision (returns `true` or `false`) the processing is finished and other callbacks are not fired.
558
- * Callbacks are fired in the order they were added, however generic callbacks are fired before callbacks added for a specified item.
559
- *
560
- * You can also use {@link #event:checkAttribute} event, if you need even better control. The result from the example above could also
561
- * be achieved with following event callback:
562
- *
563
- * ```ts
564
- * schema.on( 'checkAttribute', ( evt, args ) => {
565
- * const context = args[ 0 ];
566
- * const attributeName = args[ 1 ];
567
- *
568
- * if ( context.endsWith( 'myTitle $text' ) && schema.getAttributeProperties( attributeName ).isFormatting ) {
569
- * // Prevent next listeners from being called.
570
- * evt.stop();
571
- * // Set the `checkAttribute()` return value.
572
- * evt.return = false;
573
- * }
574
- * }, { priority: 'high' } );
575
- * ```
576
- *
577
- * Note that the callback checks and declarative rules checks are processed on `normal` priority.
578
- *
579
- * Adding callbacks this way can also negatively impact editor performance.
580
- *
581
- * @param callback The callback to be called. It is called with two parameters:
582
- * {@link module:engine/model/schema~ModelSchemaContext `context`} and attribute name. The callback may return `true` or `false`, to
583
- * override `checkAttribute()`'s return value. If it does not return a boolean value, the default algorithm (or other callbacks)
584
- * will define `checkAttribute()`'s return value.
585
- * @param attributeName Name of the attribute for which the callback is registered. If specified, the callback will be run only for
586
- * `checkAttribute()` calls with matching `attributeName`. Otherwise, the callback will run for every `checkAttribute()` call.
587
- */
588
- addAttributeCheck(callback, attributeName) {
589
- const key = attributeName !== undefined ? attributeName : this._genericCheckSymbol;
590
- const checks = this._customAttributeChecks.get(key) || [];
591
- checks.push(callback);
592
- this._customAttributeChecks.set(key, checks);
593
- }
594
- /**
595
- * This method allows assigning additional metadata to the model attributes. For example,
596
- * {@link module:engine/model/schema~ModelAttributeProperties `AttributeProperties#isFormatting` property} is
597
- * used to mark formatting attributes (like `bold` or `italic`).
598
- *
599
- * ```ts
600
- * // Mark bold as a formatting attribute.
601
- * schema.setAttributeProperties( 'bold', {
602
- * isFormatting: true
603
- * } );
604
- *
605
- * // Override code not to be considered a formatting markup.
606
- * schema.setAttributeProperties( 'code', {
607
- * isFormatting: false
608
- * } );
609
- * ```
610
- *
611
- * Properties are not limited to members defined in the
612
- * {@link module:engine/model/schema~ModelAttributeProperties `AttributeProperties` type} and you can also use custom properties:
613
- *
614
- * ```ts
615
- * schema.setAttributeProperties( 'blockQuote', {
616
- * customProperty: 'value'
617
- * } );
618
- * ```
619
- *
620
- * Subsequent calls with the same attribute will extend its custom properties:
621
- *
622
- * ```ts
623
- * schema.setAttributeProperties( 'blockQuote', {
624
- * one: 1
625
- * } );
626
- *
627
- * schema.setAttributeProperties( 'blockQuote', {
628
- * two: 2
629
- * } );
630
- *
631
- * console.log( schema.getAttributeProperties( 'blockQuote' ) );
632
- * // Logs: { one: 1, two: 2 }
633
- * ```
634
- *
635
- * @param attributeName A name of the attribute to receive the properties.
636
- * @param properties A dictionary of properties.
637
- */
638
- setAttributeProperties(attributeName, properties) {
639
- this._attributeProperties[attributeName] = Object.assign(this.getAttributeProperties(attributeName), properties);
640
- }
641
- /**
642
- * Returns properties associated with a given model attribute. See {@link #setAttributeProperties `setAttributeProperties()`}.
643
- *
644
- * @param attributeName A name of the attribute.
645
- */
646
- getAttributeProperties(attributeName) {
647
- return this._attributeProperties[attributeName] || Object.create(null);
648
- }
649
- /**
650
- * Returns the lowest {@link module:engine/model/schema~ModelSchema#isLimit limit element} containing the entire
651
- * selection/range/position or the root otherwise.
652
- *
653
- * @param selectionOrRangeOrPosition The selection/range/position to check.
654
- * @returns The lowest limit element containing the entire `selectionOrRangeOrPosition`.
655
- */
656
- getLimitElement(selectionOrRangeOrPosition) {
657
- let element;
658
- if (selectionOrRangeOrPosition instanceof ModelPosition) {
659
- element = selectionOrRangeOrPosition.parent;
660
- }
661
- else {
662
- const ranges = selectionOrRangeOrPosition instanceof ModelRange ?
663
- [selectionOrRangeOrPosition] :
664
- Array.from(selectionOrRangeOrPosition.getRanges());
665
- // Find the common ancestor for all selection's ranges.
666
- element = ranges
667
- .reduce((element, range) => {
668
- const rangeCommonAncestor = range.getCommonAncestor();
669
- if (!element) {
670
- return rangeCommonAncestor;
671
- }
672
- return element.getCommonAncestor(rangeCommonAncestor, { includeSelf: true });
673
- }, null);
674
- }
675
- while (!this.isLimit(element)) {
676
- if (element.parent) {
677
- element = element.parent;
678
- }
679
- else {
680
- break;
681
- }
682
- }
683
- return element;
684
- }
685
- /**
686
- * Checks whether the attribute is allowed in selection:
687
- *
688
- * * if the selection is not collapsed, then checks if the attribute is allowed on any of nodes in that range,
689
- * * if the selection is collapsed, then checks if on the selection position there's a text with the
690
- * specified attribute allowed.
691
- *
692
- * @param selection Selection which will be checked.
693
- * @param attribute The name of the attribute to check.
694
- */
695
- checkAttributeInSelection(selection, attribute) {
696
- if (selection.isCollapsed) {
697
- const firstPosition = selection.getFirstPosition();
698
- const context = [
699
- ...firstPosition.getAncestors(),
700
- new ModelText('', selection.getAttributes())
701
- ];
702
- // Check whether schema allows for a text with the attribute in the selection.
703
- return this.checkAttribute(context, attribute);
704
- }
705
- else {
706
- const ranges = selection.getRanges();
707
- // For all ranges, check nodes in them until you find a node that is allowed to have the attribute.
708
- for (const range of ranges) {
709
- for (const value of range) {
710
- if (this.checkAttribute(value.item, attribute)) {
711
- // If we found a node that is allowed to have the attribute, return true.
712
- return true;
713
- }
714
- }
715
- }
716
- }
717
- // If we haven't found such node, return false.
718
- return false;
719
- }
720
- /**
721
- * Transforms the given set of ranges into a set of ranges where the given attribute is allowed (and can be applied).
722
- *
723
- * @param ranges Ranges to be validated.
724
- * @param attribute The name of the attribute to check.
725
- * @param options Optional configuration.
726
- * @param options.includeEmptyRanges When `true`, empty ranges that allow the attribute are also included
727
- * in the returned ranges.
728
- * @returns Ranges in which the attribute is allowed.
729
- */
730
- *getValidRanges(ranges, attribute, options = {}) {
731
- ranges = convertToMinimalFlatRanges(ranges);
732
- for (const range of ranges) {
733
- yield* this._getValidRangesForRange(range, attribute, options);
734
- }
735
- }
736
- /**
737
- * Basing on given `position`, finds and returns a {@link module:engine/model/range~ModelRange range} which is
738
- * nearest to that `position` and is a correct range for selection.
739
- *
740
- * The correct selection range might be collapsed when it is located in a position where the text node can be placed.
741
- * Non-collapsed range is returned when selection can be placed around element marked as an "object" in
742
- * the {@link module:engine/model/schema~ModelSchema schema}.
743
- *
744
- * Direction of searching for the nearest correct selection range can be specified as:
745
- *
746
- * * `both` - searching will be performed in both ways,
747
- * * `forward` - searching will be performed only forward,
748
- * * `backward` - searching will be performed only backward.
749
- *
750
- * When valid selection range cannot be found, `null` is returned.
751
- *
752
- * @param position Reference position where new selection range should be looked for.
753
- * @param direction Search direction.
754
- * @returns Nearest selection range or `null` if one cannot be found.
755
- */
756
- getNearestSelectionRange(position, direction = 'both') {
757
- if (position.root.rootName == '$graveyard') {
758
- // No valid selection range in the graveyard.
759
- // This is important when getting the document selection default range.
760
- return null;
761
- }
762
- // Return collapsed range if provided position is valid.
763
- if (this.checkChild(position, '$text')) {
764
- return new ModelRange(position);
765
- }
766
- let backwardWalker, forwardWalker;
767
- // Never leave a limit element.
768
- const limitElement = position.getAncestors().reverse().find(item => this.isLimit(item)) ||
769
- position.root;
770
- if (direction == 'both' || direction == 'backward') {
771
- backwardWalker = new ModelTreeWalker({
772
- boundaries: ModelRange._createIn(limitElement),
773
- startPosition: position,
774
- direction: 'backward'
775
- });
776
- }
777
- if (direction == 'both' || direction == 'forward') {
778
- forwardWalker = new ModelTreeWalker({
779
- boundaries: ModelRange._createIn(limitElement),
780
- startPosition: position
781
- });
782
- }
783
- for (const data of combineWalkers(backwardWalker, forwardWalker)) {
784
- const type = (data.walker == backwardWalker ? 'elementEnd' : 'elementStart');
785
- const value = data.value;
786
- if (value.type == type && this.isObject(value.item)) {
787
- return ModelRange._createOn(value.item);
788
- }
789
- if (this.checkChild(value.nextPosition, '$text')) {
790
- return new ModelRange(value.nextPosition);
791
- }
792
- }
793
- return null;
794
- }
795
- /**
796
- * Tries to find position ancestors that allow to insert a given node.
797
- * It starts searching from the given position and goes node by node to the top of the model tree
798
- * as long as a {@link module:engine/model/schema~ModelSchema#isLimit limit element}, an
799
- * {@link module:engine/model/schema~ModelSchema#isObject object element} or a topmost ancestor is not reached.
800
- *
801
- * @param position The position that the search will start from.
802
- * @param node The node for which an allowed parent should be found or its name.
803
- * @returns Allowed parent or null if nothing was found.
804
- */
805
- findAllowedParent(position, node) {
806
- let parent = position.parent;
807
- while (parent) {
808
- if (this.checkChild(parent, node)) {
809
- return parent;
810
- }
811
- // Do not split limit elements.
812
- if (this.isLimit(parent)) {
813
- return null;
814
- }
815
- parent = parent.parent;
816
- }
817
- return null;
818
- }
819
- /**
820
- * Sets attributes allowed by the schema on a given node.
821
- *
822
- * @param node A node to set attributes on.
823
- * @param attributes Attributes keys and values.
824
- * @param writer An instance of the model writer.
825
- */
826
- setAllowedAttributes(node, attributes, writer) {
827
- const model = writer.model;
828
- for (const [attributeName, attributeValue] of Object.entries(attributes)) {
829
- if (model.schema.checkAttribute(node, attributeName)) {
830
- writer.setAttribute(attributeName, attributeValue, node);
831
- }
832
- }
833
- }
834
- /**
835
- * Removes attributes disallowed by the schema.
836
- *
837
- * @param nodes Nodes that will be filtered.
838
- */
839
- removeDisallowedAttributes(nodes, writer) {
840
- for (const node of nodes) {
841
- // When node is a `Text` it has no children, so just filter it out.
842
- if (node.is('$text')) {
843
- removeDisallowedAttributeFromNode(this, node, writer);
844
- }
845
- // In a case of `Element` iterates through positions between nodes inside this element
846
- // and filter out node before the current position, or position parent when position
847
- // is at start of an element. Using positions prevent from omitting merged nodes
848
- // see https://github.com/ckeditor/ckeditor5-engine/issues/1789.
849
- else {
850
- const rangeInNode = ModelRange._createIn(node);
851
- const positionsInRange = rangeInNode.getPositions();
852
- for (const position of positionsInRange) {
853
- const item = position.nodeBefore || position.parent;
854
- removeDisallowedAttributeFromNode(this, item, writer);
855
- }
856
- }
857
- }
858
- }
859
- /**
860
- * Gets attributes of a node that have a given property.
861
- *
862
- * @param node Node to get attributes from.
863
- * @param propertyName Name of the property that attribute must have to return it.
864
- * @param propertyValue Desired value of the property that we want to check.
865
- * When `undefined` attributes will be returned if they have set a given property no matter what the value is. If specified it will
866
- * return attributes which given property's value is equal to this parameter.
867
- * @returns Object with attributes' names as key and attributes' values as value.
868
- */
869
- getAttributesWithProperty(node, propertyName, propertyValue) {
870
- const attributes = {};
871
- for (const [attributeName, attributeValue] of node.getAttributes()) {
872
- const attributeProperties = this.getAttributeProperties(attributeName);
873
- if (attributeProperties[propertyName] === undefined) {
874
- continue;
875
- }
876
- if (propertyValue === undefined || propertyValue === attributeProperties[propertyName]) {
877
- attributes[attributeName] = attributeValue;
878
- }
879
- }
880
- return attributes;
881
- }
882
- /**
883
- * Creates an instance of the schema context.
884
- */
885
- createContext(context) {
886
- return new ModelSchemaContext(context);
887
- }
888
- _clearCache() {
889
- this._compiledDefinitions = null;
890
- }
891
- _compile() {
892
- const definitions = {};
893
- const sourceRules = this._sourceDefinitions;
894
- const itemNames = Object.keys(sourceRules);
895
- for (const itemName of itemNames) {
896
- definitions[itemName] = compileBaseItemRule(sourceRules[itemName], itemName);
897
- }
898
- const items = Object.values(definitions);
899
- // Sometimes features add rules (allows, disallows) for items that has not been registered yet. We allow that, to make it easier
900
- // to put the schema together. However, sometimes these items are never registered. To prevent operating
901
- // removeUnregisteredEntries( definitions, items );
902
- // 1. Propagate `childItem.allowIn` to `parentItem.allowChildren` and vice versa, so that these properties are completely mirrored
903
- // for all children and parents. Do the same for `disallowIn` and `disallowChildren`.
904
- for (const item of items) {
905
- propagateAllowIn(definitions, item);
906
- propagateAllowChildren(definitions, item);
907
- propagateDisallowIn(definitions, item);
908
- propagateDisallowChildren(definitions, item);
909
- }
910
- // 2. Remove from `allowIn` and `allowChildren` these items which where disallowed by `disallowIn` and `disallowChildren`.
911
- // Do the same for attributes. Now we have a clear situation where which item/attribute is allowed. Inheritance is in next steps.
912
- for (const item of items) {
913
- resolveDisallows(definitions, item);
914
- }
915
- // 3. Compile `item.allowContentOf` property. For each entry in `allowContentOf`, we want to take `allowChildren` and rewrite
916
- // them into `item.allowChildren`. `item.disallowChildren` is used to filter out some entries. This way "own rules" have higher
917
- // priority than "inherited rules". Mirroring from step 1. is maintained.
918
- for (const item of items) {
919
- compileAllowContentOf(definitions, item);
920
- }
921
- // 4. Compile `item.allowWhere` property. For each entry in `allowWhere`, we want to take `allowIn` and rewrite them into
922
- // `item.allowIn`. `item.disallowIn` is used to filter out some entries. This way "own rules" have higher priority than
923
- // "inherited rules". Mirroring from step 1. is maintained.
924
- for (const item of items) {
925
- compileAllowWhere(definitions, item);
926
- }
927
- // 5. Compile `item.allowAttributesOf`. For each entry in `allowAttributesOf`, we want to take `allowAttributes` and rewrite them
928
- // into `item.allowAttributes`. `item.disallowAttributes` is used to filter out some entries. This way "own rules" have higher
929
- // priority than "inherited rules".
930
- for (const item of items) {
931
- compileAllowAttributesOf(definitions, item);
932
- }
933
- // 6. Compile `item.inheritTypesFrom` property. For each entry in `inheritTypesFrom`, we want to take `is*` properties and
934
- // set them on `item` (if they are not set yet).
935
- for (const item of items) {
936
- compileInheritPropertiesFrom(definitions, item);
937
- }
938
- // Compile final definitions. Unnecessary properties are removed and some additional cleaning is applied.
939
- this._compiledDefinitions = compileDefinitions(definitions);
940
- }
941
- _checkContextMatch(context, def) {
942
- const parentItem = context.last;
943
- // First, check all child checks declared as callbacks.
944
- // Note that `_evaluateChildChecks()` will return `undefined` if neither child check was applicable (no decision was made).
945
- let isAllowed = this._evaluateChildChecks(context, def);
946
- // If the decision was not made inside child check callbacks, then use declarative rules.
947
- isAllowed = isAllowed !== undefined ? isAllowed : def.allowIn.includes(parentItem.name);
948
- // If the item is not allowed in the `context`, return `false`.
949
- if (!isAllowed) {
950
- return false;
951
- }
952
- // If the item is allowed, recursively verify the rest of the `context`.
953
- const parentItemDefinition = this.getDefinition(parentItem);
954
- const parentContext = context.trimLast();
955
- // One of the items in the original `context` did not have a definition specified. In this case, the whole context is disallowed.
956
- if (!parentItemDefinition) {
957
- return false;
958
- }
959
- // Whole `context` was verified and passed checks.
960
- if (parentContext.length == 0) {
961
- return true;
962
- }
963
- // Verify "truncated" parent context. The last item of the original context is now the definition to check.
964
- return this._checkContextMatch(parentContext, parentItemDefinition);
965
- }
966
- /**
967
- * Calls child check callbacks to decide whether `def` is allowed in `context`. It uses both generic and specific (defined for `def`
968
- * item) callbacks. If neither callback makes a decision, `undefined` is returned.
969
- *
970
- * Note that the first callback that makes a decision "wins", i.e., if any callback returns `true` or `false`, then the processing
971
- * is over and that result is returned.
972
- */
973
- _evaluateChildChecks(context, def) {
974
- const genericChecks = this._customChildChecks.get(this._genericCheckSymbol) || [];
975
- const childChecks = this._customChildChecks.get(def.name) || [];
976
- for (const check of [...genericChecks, ...childChecks]) {
977
- const result = check(context, def);
978
- if (result !== undefined) {
979
- return result;
980
- }
981
- }
982
- }
983
- /**
984
- * Calls attribute check callbacks to decide whether `attributeName` can be set on the last element of `context`. It uses both
985
- * generic and specific (defined for `attributeName`) callbacks. If neither callback makes a decision, `undefined` is returned.
986
- *
987
- * Note that the first callback that makes a decision "wins", i.e., if any callback returns `true` or `false`, then the processing
988
- * is over and that result is returned.
989
- */
990
- _evaluateAttributeChecks(context, attributeName) {
991
- const genericChecks = this._customAttributeChecks.get(this._genericCheckSymbol) || [];
992
- const childChecks = this._customAttributeChecks.get(attributeName) || [];
993
- for (const check of [...genericChecks, ...childChecks]) {
994
- const result = check(context, attributeName);
995
- if (result !== undefined) {
996
- return result;
997
- }
998
- }
999
- }
1000
- /**
1001
- * Takes a flat range and an attribute name. Traverses the range recursively and deeply to find and return all ranges
1002
- * inside the given range on which the attribute can be applied.
1003
- *
1004
- * This is a helper function for {@link ~ModelSchema#getValidRanges}.
1005
- *
1006
- * @param range The range to process.
1007
- * @param attribute The name of the attribute to check.
1008
- * @returns Ranges in which the attribute is allowed.
1009
- */
1010
- *_getValidRangesForRange(range, attribute, options) {
1011
- let start = range.start;
1012
- let end = range.start;
1013
- for (const item of range.getItems({ shallow: true })) {
1014
- if (item.is('element')) {
1015
- if (options.includeEmptyRanges && item.isEmpty) {
1016
- // In empty block check if it accepts text with the given attribute.
1017
- const context = this.createContext(item);
1018
- if (this.checkChild(context, '$text') &&
1019
- this.checkAttribute(context.push('$text'), attribute)) {
1020
- yield ModelRange._createIn(item);
1021
- }
1022
- }
1023
- else {
1024
- yield* this._getValidRangesForRange(ModelRange._createIn(item), attribute, options);
1025
- }
1026
- }
1027
- if (!this.checkAttribute(item, attribute)) {
1028
- if (!start.isEqual(end)) {
1029
- yield new ModelRange(start, end);
1030
- }
1031
- start = ModelPosition._createAfter(item);
1032
- }
1033
- end = ModelPosition._createAfter(item);
1034
- }
1035
- if (!start.isEqual(end)) {
1036
- yield new ModelRange(start, end);
1037
- }
1038
- }
1039
- /**
1040
- * Returns a model range which is optimal (in terms of UX) for inserting a widget block.
1041
- *
1042
- * For instance, if a selection is in the middle of a paragraph, the collapsed range before this paragraph
1043
- * will be returned so that it is not split. If the selection is at the end of a paragraph,
1044
- * the collapsed range after this paragraph will be returned.
1045
- *
1046
- * Note: If the selection is placed in an empty block, the range in that block will be returned. If that range
1047
- * is then passed to {@link module:engine/model/model~Model#insertContent}, the block will be fully replaced
1048
- * by the inserted widget block.
1049
- *
1050
- * @internal
1051
- * @param selection The selection based on which the insertion position should be calculated.
1052
- * @param place The place where to look for optimal insertion range.
1053
- * The `auto` value will determine itself the best position for insertion.
1054
- * The `before` value will try to find a position before selection.
1055
- * The `after` value will try to find a position after selection.
1056
- * @returns The optimal range.
1057
- */
1058
- findOptimalInsertionRange(selection, place) {
1059
- const selectedElement = selection.getSelectedElement();
1060
- if (selectedElement && this.isObject(selectedElement) && !this.isInline(selectedElement)) {
1061
- if (place == 'before' || place == 'after') {
1062
- return new ModelRange(ModelPosition._createAt(selectedElement, place));
1063
- }
1064
- return ModelRange._createOn(selectedElement);
1065
- }
1066
- const firstBlock = first(selection.getSelectedBlocks());
1067
- // There are no block elements within ancestors (in the current limit element).
1068
- if (!firstBlock) {
1069
- return new ModelRange(selection.focus);
1070
- }
1071
- // If inserting into an empty block – return position in that block. It will get
1072
- // replaced with the image by insertContent(). #42.
1073
- if (firstBlock.isEmpty) {
1074
- return new ModelRange(ModelPosition._createAt(firstBlock, 0));
1075
- }
1076
- const positionAfter = ModelPosition._createAfter(firstBlock);
1077
- // If selection is at the end of the block - return position after the block.
1078
- if (selection.focus.isTouching(positionAfter)) {
1079
- return new ModelRange(positionAfter);
1080
- }
1081
- // Otherwise, return position before the block.
1082
- return new ModelRange(ModelPosition._createBefore(firstBlock));
1083
- }
1084
- }
1085
- /**
1086
- * A schema context – a list of ancestors of a given position in the document.
1087
- *
1088
- * Considering such position:
1089
- *
1090
- * ```xml
1091
- * <$root>
1092
- * <blockQuote>
1093
- * <paragraph>
1094
- * ^
1095
- * </paragraph>
1096
- * </blockQuote>
1097
- * </$root>
1098
- * ```
1099
- *
1100
- * The context of this position is its {@link module:engine/model/position~ModelPosition#getAncestors lists of ancestors}:
1101
- *
1102
- * [ rootElement, blockQuoteElement, paragraphElement ]
1103
- *
1104
- * Contexts are used in the {@link module:engine/model/schema~ModelSchema#event:checkChild `Schema#checkChild`} and
1105
- * {@link module:engine/model/schema~ModelSchema#event:checkAttribute `Schema#checkAttribute`} events as a definition
1106
- * of a place in the document where the check occurs. The context instances are created based on the first arguments
1107
- * of the {@link module:engine/model/schema~ModelSchema#checkChild `Schema#checkChild()`} and
1108
- * {@link module:engine/model/schema~ModelSchema#checkAttribute `Schema#checkAttribute()`} methods so when
1109
- * using these methods you need to use {@link module:engine/model/schema~ModelSchemaContextDefinition}s.
1110
- */
1111
- export class ModelSchemaContext {
1112
- _items;
1113
- /**
1114
- * Creates an instance of the context.
1115
- */
1116
- constructor(context) {
1117
- if (context instanceof ModelSchemaContext) {
1118
- return context;
1119
- }
1120
- let items;
1121
- if (typeof context == 'string') {
1122
- items = [context];
1123
- }
1124
- else if (!Array.isArray(context)) {
1125
- // `context` is item or position.
1126
- // Position#getAncestors() doesn't accept any parameters but it works just fine here.
1127
- items = context.getAncestors({ includeSelf: true });
1128
- }
1129
- else {
1130
- items = context;
1131
- }
1132
- this._items = items.map(mapContextItem);
1133
- }
1134
- /**
1135
- * The number of items.
1136
- */
1137
- get length() {
1138
- return this._items.length;
1139
- }
1140
- /**
1141
- * The last item (the lowest node).
1142
- */
1143
- get last() {
1144
- return this._items[this._items.length - 1];
1145
- }
1146
- /**
1147
- * Iterable interface.
1148
- *
1149
- * Iterates over all context items.
1150
- */
1151
- [Symbol.iterator]() {
1152
- return this._items[Symbol.iterator]();
1153
- }
1154
- /**
1155
- * Returns a new schema context instance with an additional item.
1156
- *
1157
- * Item can be added as:
1158
- *
1159
- * ```ts
1160
- * const context = new ModelSchemaContext( [ '$root' ] );
1161
- *
1162
- * // An element.
1163
- * const fooElement = writer.createElement( 'fooElement' );
1164
- * const newContext = context.push( fooElement ); // [ '$root', 'fooElement' ]
1165
- *
1166
- * // A text node.
1167
- * const text = writer.createText( 'foobar' );
1168
- * const newContext = context.push( text ); // [ '$root', '$text' ]
1169
- *
1170
- * // A string (element name).
1171
- * const newContext = context.push( 'barElement' ); // [ '$root', 'barElement' ]
1172
- * ```
1173
- *
1174
- * **Note** {@link module:engine/model/node~ModelNode} that is already in the model tree will be added as the only item
1175
- * (without ancestors).
1176
- *
1177
- * @param item An item that will be added to the current context.
1178
- * @returns A new schema context instance with an additional item.
1179
- */
1180
- push(item) {
1181
- const ctx = new ModelSchemaContext([item]);
1182
- ctx._items = [...this._items, ...ctx._items];
1183
- return ctx;
1184
- }
1185
- /**
1186
- * Returns a new schema context that is based on this context but has the last item removed.
1187
- *
1188
- * ```ts
1189
- * const ctxParagraph = new ModelSchemaContext( [ '$root', 'blockQuote', 'paragraph' ] );
1190
- * const ctxBlockQuote = ctxParagraph.trimLast(); // Items in `ctxBlockQuote` are: `$root` an `blockQuote`.
1191
- * const ctxRoot = ctxBlockQuote.trimLast(); // Items in `ctxRoot` are: `$root`.
1192
- * ```
1193
- *
1194
- * @returns A new reduced schema context instance.
1195
- */
1196
- trimLast() {
1197
- const ctx = new ModelSchemaContext([]);
1198
- ctx._items = this._items.slice(0, -1);
1199
- return ctx;
1200
- }
1201
- /**
1202
- * Gets an item on the given index.
1203
- */
1204
- getItem(index) {
1205
- return this._items[index];
1206
- }
1207
- /**
1208
- * Returns the names of items.
1209
- */
1210
- *getNames() {
1211
- yield* this._items.map(item => item.name);
1212
- }
1213
- /**
1214
- * Checks whether the context ends with the given nodes.
1215
- *
1216
- * ```ts
1217
- * const ctx = new ModelSchemaContext( [ rootElement, paragraphElement, textNode ] );
1218
- *
1219
- * ctx.endsWith( '$text' ); // -> true
1220
- * ctx.endsWith( 'paragraph $text' ); // -> true
1221
- * ctx.endsWith( '$root' ); // -> false
1222
- * ctx.endsWith( 'paragraph' ); // -> false
1223
- * ```
1224
- */
1225
- endsWith(query) {
1226
- return Array.from(this.getNames()).join(' ').endsWith(query);
1227
- }
1228
- /**
1229
- * Checks whether the context starts with the given nodes.
1230
- *
1231
- * ```ts
1232
- * const ctx = new ModelSchemaContext( [ rootElement, paragraphElement, textNode ] );
1233
- *
1234
- * ctx.endsWith( '$root' ); // -> true
1235
- * ctx.endsWith( '$root paragraph' ); // -> true
1236
- * ctx.endsWith( '$text' ); // -> false
1237
- * ctx.endsWith( 'paragraph' ); // -> false
1238
- * ```
1239
- */
1240
- startsWith(query) {
1241
- return Array.from(this.getNames()).join(' ').startsWith(query);
1242
- }
1243
- }
1244
- function compileBaseItemRule(sourceItemRules, itemName) {
1245
- const itemRule = {
1246
- name: itemName,
1247
- allowIn: new Set(),
1248
- allowChildren: new Set(),
1249
- disallowIn: new Set(),
1250
- disallowChildren: new Set(),
1251
- allowContentOf: new Set(),
1252
- allowWhere: new Set(),
1253
- allowAttributes: new Set(),
1254
- disallowAttributes: new Set(),
1255
- allowAttributesOf: new Set(),
1256
- inheritTypesFrom: new Set()
1257
- };
1258
- copyTypes(sourceItemRules, itemRule);
1259
- copyProperty(sourceItemRules, itemRule, 'allowIn');
1260
- copyProperty(sourceItemRules, itemRule, 'allowChildren');
1261
- copyProperty(sourceItemRules, itemRule, 'disallowIn');
1262
- copyProperty(sourceItemRules, itemRule, 'disallowChildren');
1263
- copyProperty(sourceItemRules, itemRule, 'allowContentOf');
1264
- copyProperty(sourceItemRules, itemRule, 'allowWhere');
1265
- copyProperty(sourceItemRules, itemRule, 'allowAttributes');
1266
- copyProperty(sourceItemRules, itemRule, 'disallowAttributes');
1267
- copyProperty(sourceItemRules, itemRule, 'allowAttributesOf');
1268
- copyProperty(sourceItemRules, itemRule, 'inheritTypesFrom');
1269
- resolveInheritAll(sourceItemRules, itemRule);
1270
- return itemRule;
1271
- }
1272
- function propagateAllowIn(definitions, item) {
1273
- for (const parentName of item.allowIn) {
1274
- const parentItem = definitions[parentName];
1275
- if (parentItem) {
1276
- parentItem.allowChildren.add(item.name);
1277
- }
1278
- else {
1279
- item.allowIn.delete(parentName);
1280
- }
1281
- }
1282
- }
1283
- function propagateAllowChildren(definitions, item) {
1284
- for (const childName of item.allowChildren) {
1285
- const childItem = definitions[childName];
1286
- if (childItem) {
1287
- childItem.allowIn.add(item.name);
1288
- }
1289
- else {
1290
- item.allowChildren.delete(childName);
1291
- }
1292
- }
1293
- }
1294
- function propagateDisallowIn(definitions, item) {
1295
- for (const parentName of item.disallowIn) {
1296
- const parentItem = definitions[parentName];
1297
- if (parentItem) {
1298
- parentItem.disallowChildren.add(item.name);
1299
- }
1300
- else {
1301
- item.disallowIn.delete(parentName);
1302
- }
1303
- }
1304
- }
1305
- function propagateDisallowChildren(definitions, item) {
1306
- for (const childName of item.disallowChildren) {
1307
- const childItem = definitions[childName];
1308
- if (childItem) {
1309
- childItem.disallowIn.add(item.name);
1310
- }
1311
- else {
1312
- item.disallowChildren.delete(childName);
1313
- }
1314
- }
1315
- }
1316
- function resolveDisallows(definitions, item) {
1317
- for (const childName of item.disallowChildren) {
1318
- item.allowChildren.delete(childName);
1319
- }
1320
- for (const parentName of item.disallowIn) {
1321
- item.allowIn.delete(parentName);
1322
- }
1323
- for (const attributeName of item.disallowAttributes) {
1324
- item.allowAttributes.delete(attributeName);
1325
- }
1326
- }
1327
- function compileAllowContentOf(definitions, item) {
1328
- for (const allowContentOfItemName of item.allowContentOf) {
1329
- const baseItem = definitions[allowContentOfItemName];
1330
- if (!baseItem) {
1331
- continue;
1332
- }
1333
- // Copy `disallowChildren` to propagate this "knowledge" down the inheritance chain. The inheritance may involve multiple items and
1334
- // if this is not propagated, then items down the chain may start to allow for items that were disallowed by previous base items.
1335
- //
1336
- // The scenarios were it is important involves multiple inherits both on parent items side and on the child items side.
1337
- baseItem.disallowChildren.forEach(childName => {
1338
- // Own item's rules takes precedence before inherited.
1339
- // If the item directly allows for given child, ignore that base item disallowed that child.
1340
- if (item.allowChildren.has(childName)) {
1341
- return;
1342
- }
1343
- item.disallowChildren.add(childName);
1344
- definitions[childName].disallowIn.add(item.name);
1345
- });
1346
- // Copy `allowChildren` from the base item to allow for the same items.
1347
- baseItem.allowChildren.forEach(childName => {
1348
- // Own item's rules takes precedence before inherited.
1349
- // Also, `item.disallowChildren` might get some new items during inheritance process.
1350
- if (item.disallowChildren.has(childName)) {
1351
- return;
1352
- }
1353
- item.allowChildren.add(childName);
1354
- definitions[childName].allowIn.add(item.name);
1355
- });
1356
- }
1357
- }
1358
- function compileAllowWhere(definitions, item) {
1359
- for (const allowWhereItemName of item.allowWhere) {
1360
- const baseItem = definitions[allowWhereItemName];
1361
- if (!baseItem) {
1362
- continue;
1363
- }
1364
- // Copy `disallowIn` to propagate this "knowledge" down the inheritance chain. The inheritance may involve multiple items and
1365
- // if this is not propagated, then items down the chain may start to be allowed in items in which they were disallowed previously.
1366
- //
1367
- // The scenarios were it is important involves multiple inherits both on parent items side and on the child items side.
1368
- baseItem.disallowIn.forEach(parentName => {
1369
- // Own item's rules takes precedence before inherited.
1370
- // If the item is directly allowed in given parent, ignore that base item was disallowed in it.
1371
- if (item.allowIn.has(parentName)) {
1372
- return;
1373
- }
1374
- item.disallowIn.add(parentName);
1375
- definitions[parentName].disallowChildren.add(item.name);
1376
- });
1377
- // Copy `allowIn` from the base item to allow item in same parents.
1378
- baseItem.allowIn.forEach(parentName => {
1379
- // Own item's rules takes precedence before inherited.
1380
- // Also, `item.disallowIn` might get some new items during inheritance process.
1381
- if (item.disallowIn.has(parentName)) {
1382
- return;
1383
- }
1384
- item.allowIn.add(parentName);
1385
- definitions[parentName].allowChildren.add(item.name);
1386
- });
1387
- }
1388
- }
1389
- function compileDefinitions(definitions) {
1390
- const finalDefinitions = {};
1391
- for (const item of Object.values(definitions)) {
1392
- finalDefinitions[item.name] = {
1393
- name: item.name,
1394
- // `is*` properties may not be set - convert `undefined` to `false`.
1395
- isBlock: !!item.isBlock,
1396
- isContent: !!item.isContent,
1397
- isInline: !!item.isInline,
1398
- isLimit: !!item.isLimit,
1399
- isObject: !!item.isObject,
1400
- isSelectable: !!item.isSelectable,
1401
- // Filter out non-existing items.
1402
- allowIn: Array.from(item.allowIn).filter(name => !!definitions[name]),
1403
- allowChildren: Array.from(item.allowChildren).filter(name => !!definitions[name]),
1404
- allowAttributes: Array.from(item.allowAttributes)
1405
- };
1406
- }
1407
- return finalDefinitions;
1408
- }
1409
- function compileAllowAttributesOf(definitions, item) {
1410
- for (const allowAttributeOfItemName of item.allowAttributesOf) {
1411
- const baseItem = definitions[allowAttributeOfItemName];
1412
- if (!baseItem) {
1413
- return;
1414
- }
1415
- baseItem.allowAttributes.forEach(attributeName => {
1416
- if (item.disallowAttributes.has(attributeName)) {
1417
- return;
1418
- }
1419
- item.allowAttributes.add(attributeName);
1420
- });
1421
- }
1422
- }
1423
- function compileInheritPropertiesFrom(definitions, item) {
1424
- for (const inheritPropertiesOfItemName of item.inheritTypesFrom) {
1425
- const inheritFrom = definitions[inheritPropertiesOfItemName];
1426
- if (inheritFrom) {
1427
- const typeNames = Object.keys(inheritFrom).filter(name => name.startsWith('is'));
1428
- for (const name of typeNames) {
1429
- if (!(name in item)) {
1430
- item[name] = inheritFrom[name];
1431
- }
1432
- }
1433
- }
1434
- }
1435
- }
1436
- function copyTypes(sourceItemRules, itemRule) {
1437
- for (const sourceItemRule of sourceItemRules) {
1438
- const typeNames = Object.keys(sourceItemRule).filter(name => name.startsWith('is'));
1439
- for (const name of typeNames) {
1440
- itemRule[name] = !!sourceItemRule[name];
1441
- }
1442
- }
1443
- }
1444
- function copyProperty(sourceItemRules, itemRule, propertyName) {
1445
- for (const sourceItemRule of sourceItemRules) {
1446
- let value = sourceItemRule[propertyName];
1447
- // `value` can be a string, an array or undefined.
1448
- // Convert a string to an array with one item, then handle an array. Skip undefined this way.
1449
- if (typeof value == 'string') {
1450
- value = [value];
1451
- }
1452
- if (Array.isArray(value)) {
1453
- value.forEach(singleValue => itemRule[propertyName].add(singleValue));
1454
- }
1455
- }
1456
- }
1457
- function resolveInheritAll(sourceItemRules, itemRule) {
1458
- for (const sourceItemRule of sourceItemRules) {
1459
- const inheritFrom = sourceItemRule.inheritAllFrom;
1460
- if (inheritFrom) {
1461
- itemRule.allowContentOf.add(inheritFrom);
1462
- itemRule.allowWhere.add(inheritFrom);
1463
- itemRule.allowAttributesOf.add(inheritFrom);
1464
- itemRule.inheritTypesFrom.add(inheritFrom);
1465
- }
1466
- }
1467
- }
1468
- function mapContextItem(ctxItem) {
1469
- if (typeof ctxItem == 'string' || ctxItem.is('documentFragment')) {
1470
- return {
1471
- name: typeof ctxItem == 'string' ? ctxItem : '$documentFragment',
1472
- *getAttributeKeys() { },
1473
- getAttribute() { }
1474
- };
1475
- }
1476
- else {
1477
- return {
1478
- // '$text' means text nodes and text proxies.
1479
- name: ctxItem.is('element') ? ctxItem.name : '$text',
1480
- *getAttributeKeys() {
1481
- yield* ctxItem.getAttributeKeys();
1482
- },
1483
- getAttribute(key) {
1484
- return ctxItem.getAttribute(key);
1485
- }
1486
- };
1487
- }
1488
- }
1489
- /**
1490
- * Generator function returning values from provided walkers, switching between them at each iteration. If only one walker
1491
- * is provided it will return data only from that walker.
1492
- *
1493
- * @param backward Walker iterating in backward direction.
1494
- * @param forward Walker iterating in forward direction.
1495
- * @returns Object returned at each iteration contains `value` and `walker` (informing which walker returned
1496
- * given value) fields.
1497
- */
1498
- function* combineWalkers(backward, forward) {
1499
- let done = false;
1500
- while (!done) {
1501
- done = true;
1502
- if (backward) {
1503
- const step = backward.next();
1504
- if (!step.done) {
1505
- done = false;
1506
- yield {
1507
- walker: backward,
1508
- value: step.value
1509
- };
1510
- }
1511
- }
1512
- if (forward) {
1513
- const step = forward.next();
1514
- if (!step.done) {
1515
- done = false;
1516
- yield {
1517
- walker: forward,
1518
- value: step.value
1519
- };
1520
- }
1521
- }
1522
- }
1523
- }
1524
- /**
1525
- * Takes an array of non-intersecting ranges. For each of them gets minimal flat ranges covering that range and returns
1526
- * all those minimal flat ranges.
1527
- *
1528
- * @param ranges Ranges to process.
1529
- * @returns Minimal flat ranges of given `ranges`.
1530
- */
1531
- function* convertToMinimalFlatRanges(ranges) {
1532
- for (const range of ranges) {
1533
- yield* range.getMinimalFlatRanges();
1534
- }
1535
- }
1536
- function removeDisallowedAttributeFromNode(schema, node, writer) {
1537
- for (const attribute of node.getAttributeKeys()) {
1538
- if (!schema.checkAttribute(node, attribute)) {
1539
- writer.removeAttribute(attribute, node);
1540
- }
1541
- }
1542
- }