Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.NodeFactory.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel NodeFactory class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel node factory.
9 *
10 * @class
11 * @extends ve.dm.ModelFactory
12 * @constructor
13 */
14 ve.dm.NodeFactory = function VeDmNodeFactory() {
15 // Parent constructor
16 ve.dm.NodeFactory.super.call( this );
17 };
18
19 /* Inheritance */
20
21 OO.inheritClass( ve.dm.NodeFactory, ve.dm.ModelFactory );
22
23 /* Methods */
24
25 /**
26 * Get a document data element.
27 *
28 * @param {string} type Node type
29 * @param {Object} attributes Node attributes, defaults will be used where needed
30 * @return {Object} Data element
31 * @throws {Error} Unknown node type
32 */
33 ve.dm.NodeFactory.prototype.getDataElement = function ( type, attributes ) {
34 const element = { type: type };
35 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
36 attributes = ve.extendObject( {}, this.registry[ type ].static.defaultAttributes, attributes );
37 if ( !ve.isEmptyObject( attributes ) ) {
38 element.attributes = ve.copy( attributes );
39 }
40 return element;
41 }
42 throw new Error( 'Unknown node type: ' + type );
43 };
44
45 /**
46 * Get allowed child node types for a node.
47 *
48 * @param {string} type Node type
49 * @return {string[]|null} List of node types allowed as children or null if any type is allowed
50 * @throws {Error} Unknown node type
51 */
52 ve.dm.NodeFactory.prototype.getChildNodeTypes = function ( type ) {
53 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
54 return this.registry[ type ].static.childNodeTypes;
55 }
56 throw new Error( 'Unknown node type: ' + type );
57 };
58
59 /**
60 * Get allowed parent node types for a node.
61 *
62 * @param {string} type Node type
63 * @return {string[]|null} List of node types allowed as parents or null if any type is allowed
64 * @throws {Error} Unknown node type
65 */
66 ve.dm.NodeFactory.prototype.getParentNodeTypes = function ( type ) {
67 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
68 return this.registry[ type ].static.parentNodeTypes;
69 }
70 throw new Error( 'Unknown node type: ' + type );
71 };
72
73 /**
74 * Get suggested parent node types for a node.
75 *
76 * @param {string} type Node type
77 * @return {string[]|null} List of node types suggested as parents or null if any type is suggested
78 * @throws {Error} Unknown node type
79 */
80 ve.dm.NodeFactory.prototype.getSuggestedParentNodeTypes = function ( type ) {
81 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
82 return this.registry[ type ].static.suggestedParentNodeTypes;
83 }
84 throw new Error( 'Unknown node type: ' + type );
85 };
86
87 /**
88 * Check if a node can have children.
89 *
90 * @param {string} type Node type
91 * @return {boolean} The node can have children
92 * @throws {Error} Unknown node type
93 */
94 ve.dm.NodeFactory.prototype.canNodeHaveChildren = function ( type ) {
95 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
96 // If childNodeTypes is null any child is allowed, if it's an array of at least one element
97 // than at least one kind of node is allowed
98 const types = this.registry[ type ].static.childNodeTypes;
99 return types === null || ( Array.isArray( types ) && types.length > 0 );
100 }
101 throw new Error( 'Unknown node type: ' + type );
102 };
103
104 /**
105 * Check if a node can have children but not content nor be content.
106 *
107 * @param {string} type Node type
108 * @return {boolean} The node can have children but not content nor be content
109 * @throws {Error} Unknown node type
110 */
111 ve.dm.NodeFactory.prototype.canNodeHaveChildrenNotContent = function ( type ) {
112 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
113 return this.canNodeHaveChildren( type ) &&
114 !this.registry[ type ].static.canContainContent &&
115 !this.registry[ type ].static.isContent;
116 }
117 throw new Error( 'Unknown node type: ' + type );
118 };
119
120 /**
121 * Check if a node has a wrapped element in the document data.
122 *
123 * @param {string} type Node type
124 * @return {boolean} Whether the node has a wrapping element
125 * @throws {Error} Unknown node type
126 */
127 ve.dm.NodeFactory.prototype.isNodeWrapped = function ( type ) {
128 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
129 return this.registry[ type ].static.isWrapped;
130 }
131 throw new Error( 'Unknown node type: ' + type );
132 };
133
134 /**
135 * Check if a node is unwrappable.
136 *
137 * @param {string} type Node type
138 * @return {boolean} Whether the node is unwrappable
139 * @throws {Error} Unknown node type
140 */
141 ve.dm.NodeFactory.prototype.isNodeUnwrappable = function ( type ) {
142 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
143 return this.isNodeWrapped( type ) && this.registry[ type ].static.isUnwrappable;
144 }
145 throw new Error( 'Unknown node type: ' + type );
146 };
147
148 /**
149 * Check if a node is a meta item element
150 *
151 * @param {string} type Node type
152 * @return {boolean} Whether the node is meta data
153 * @throws {Error} Unknown node type
154 */
155 ve.dm.NodeFactory.prototype.isMetaData = function ( type ) {
156 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
157 return this.registry[ type ].static.isMetaData;
158 }
159 throw new Error( 'Unknown node type: ' + type );
160 };
161
162 /**
163 * Check if a given type of meta item is removable
164 *
165 * @param {string} type Meta item type
166 * @return {boolean} The type is removable
167 * @throws {Error} Unknown item type
168 */
169 ve.dm.NodeFactory.prototype.isRemovableMetaData = function ( type ) {
170 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
171 return this.registry[ type ].static.removable;
172 }
173 throw new Error( 'Unknown item type: ' + type );
174 };
175
176 /**
177 * Check if a node can contain content.
178 *
179 * @param {string} type Node type
180 * @return {boolean} The node contains content
181 * @throws {Error} Unknown node type
182 */
183 ve.dm.NodeFactory.prototype.canNodeContainContent = function ( type ) {
184 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
185 return this.registry[ type ].static.canContainContent;
186 }
187 throw new Error( 'Unknown node type: ' + type );
188 };
189
190 /**
191 * Check if node can take an annotation.
192 *
193 * @param {string} type Node type
194 * @param {ve.dm.Annotation} annotation Annotation to test
195 * @return {boolean} Node can take annotations of this type
196 * @throws {Error} Unknown node type
197 */
198 ve.dm.NodeFactory.prototype.canNodeTakeAnnotation = function ( type, annotation ) {
199 if ( !Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
200 throw new Error( 'Unknown node type: ' + type );
201 }
202 const disallowedList = this.registry[ type ].static.disallowedAnnotationTypes;
203
204 for ( let i = 0, len = disallowedList.length; i < len; i++ ) {
205 if ( annotation instanceof ve.dm.annotationFactory.lookup( disallowedList[ i ] ) ) {
206 return false;
207 }
208 }
209 return true;
210 };
211
212 /**
213 * Check if a node is content.
214 *
215 * @param {string} type Node type
216 * @return {boolean} The node is content
217 * @throws {Error} Unknown node type
218 */
219 ve.dm.NodeFactory.prototype.isNodeContent = function ( type ) {
220 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
221 return this.registry[ type ].static.isContent;
222 }
223 throw new Error( 'Unknown node type: ' + type );
224 };
225
226 /**
227 * Check if a node can be serialized into a content position
228 *
229 * @param {string} type Node type
230 * @return {boolean} The node is content or can be round-tripped into a content position
231 * @throws {Error} Unknown node type
232 */
233 ve.dm.NodeFactory.prototype.canNodeSerializeAsContent = function ( type ) {
234 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
235 return this.registry[ type ].static.isContent ||
236 this.registry[ type ].static.canSerializeAsContent;
237 }
238 throw new Error( 'Unknown node type: ' + type );
239 };
240
241 /**
242 * Check if the node is focusable.
243 *
244 * @param {string} type Node type
245 * @return {boolean} Whether the node is focusable
246 * @throws {Error} Unknown node type
247 */
248 ve.dm.NodeFactory.prototype.isNodeFocusable = function ( type ) {
249 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
250 return this.registry[ type ].static.isFocusable;
251 }
252 throw new Error( 'Unknown node type: ' + type );
253 };
254
255 /**
256 * Check if the node has significant whitespace.
257 *
258 * Can only be true if canContainContent is also true.
259 *
260 * @param {string} type Node type
261 * @return {boolean} The node has significant whitespace
262 * @throws {Error} Unknown node type
263 */
264 ve.dm.NodeFactory.prototype.doesNodeHaveSignificantWhitespace = function ( type ) {
265 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
266 return this.registry[ type ].static.hasSignificantWhitespace;
267 }
268 throw new Error( 'Unknown node type: ' + type );
269 };
270
271 /**
272 * Check if the node handles its own children.
273 *
274 * @param {string} type Node type
275 * @return {boolean} Whether the node handles its own children
276 * @throws {Error} Unknown node type
277 */
278 ve.dm.NodeFactory.prototype.doesNodeHandleOwnChildren = function ( type ) {
279 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
280 return this.registry[ type ].static.handlesOwnChildren;
281 }
282 throw new Error( 'Unknown node type: ' + type );
283 };
284
285 /**
286 * Check if the node's children should be ignored.
287 *
288 * @param {string} type Node type
289 * @return {boolean} Whether the node's children should be ignored
290 * @throws {Error} Unknown node type
291 */
292 ve.dm.NodeFactory.prototype.shouldIgnoreChildren = function ( type ) {
293 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
294 return this.registry[ type ].static.ignoreChildren;
295 }
296 throw new Error( 'Unknown node type: ' + type );
297 };
298
299 /**
300 * Check if the node is internal.
301 *
302 * @param {string} type Node type
303 * @return {boolean} Whether the node is internal
304 * @throws {Error} Unknown node type
305 */
306 ve.dm.NodeFactory.prototype.isNodeInternal = function ( type ) {
307 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
308 return this.registry[ type ].static.isInternal;
309 }
310 throw new Error( 'Unknown node type: ' + type );
311 };
312
313 /**
314 * Check if node is deletable.
315 *
316 * @param {string} type Node type
317 * @return {boolean} Whether the node is internal
318 * @throws {Error} Unknown node type
319 */
320 ve.dm.NodeFactory.prototype.isNodeDeletable = function ( type ) {
321 if ( Object.prototype.hasOwnProperty.call( this.registry, type ) ) {
322 return this.registry[ type ].static.isDeletable;
323 }
324 throw new Error( 'Unknown node type: ' + type );
325 };
326
327 /* Initialization */
328
329 ve.dm.nodeFactory = new ve.dm.NodeFactory();