Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel BranchNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel branch node.
9 *
10 * Branch nodes can have branch or leaf nodes as children.
11 *
12 * @abstract
13 * @extends ve.dm.Node
14 * @mixes ve.BranchNode
15 *
16 * @constructor
17 * @param {Object} [element] Reference to element in linear model
18 * @param {ve.dm.Node[]} [children] Child nodes to attach
19 */
20 ve.dm.BranchNode = function VeDmBranchNode( element, children ) {
21 // Mixin constructor
22 ve.BranchNode.call( this );
23
24 // Parent constructor
25 ve.dm.BranchNode.super.call( this, element );
26
27 // Properties
28 this.slugPositions = {};
29
30 // TODO: children is only ever used in tests
31 if ( Array.isArray( children ) && children.length ) {
32 this.splice( 0, 0, ...children );
33 }
34 };
35
36 /**
37 * @event ve.dm.BranchNode#splice
38 * @see #method-splice
39 * @param {number} index
40 * @param {number} deleteCount
41 * @param {...ve.dm.BranchNode} [nodes]
42 */
43
44 /* Inheritance */
45
46 OO.inheritClass( ve.dm.BranchNode, ve.dm.Node );
47
48 OO.mixinClass( ve.dm.BranchNode, ve.BranchNode );
49
50 /* Methods */
51
52 /**
53 * Add a child node to the end of the list.
54 *
55 * @param {ve.dm.BranchNode} childModel Item to add
56 * @return {number} New number of children
57 */
58 ve.dm.BranchNode.prototype.push = function ( childModel ) {
59 this.splice( this.children.length, 0, childModel );
60 return this.children.length;
61 };
62
63 /**
64 * Remove a child node from the end of the list.
65 *
66 * @return {ve.dm.BranchNode|undefined} Removed childModel
67 */
68 ve.dm.BranchNode.prototype.pop = function () {
69 if ( this.children.length ) {
70 const childModel = this.children[ this.children.length - 1 ];
71 this.splice( this.children.length - 1, 1 );
72 return childModel;
73 }
74 };
75
76 /**
77 * Add a child node to the beginning of the list.
78 *
79 * @param {ve.dm.BranchNode} childModel Item to add
80 * @return {number} New number of children
81 */
82 ve.dm.BranchNode.prototype.unshift = function ( childModel ) {
83 this.splice( 0, 0, childModel );
84 return this.children.length;
85 };
86
87 /**
88 * Remove a child node from the beginning of the list.
89 *
90 * @return {ve.dm.BranchNode|undefined} Removed childModel
91 */
92 ve.dm.BranchNode.prototype.shift = function () {
93 if ( this.children.length ) {
94 const childModel = this.children[ 0 ];
95 this.splice( 0, 1 );
96 return childModel;
97 }
98 };
99
100 /**
101 * Add and/or remove child nodes at an offset.
102 *
103 * @param {number} index Index to remove and or insert nodes at
104 * @param {number} deleteCount Number of nodes to remove
105 * @param {...ve.dm.BranchNode} [nodes] Variadic list of nodes to insert
106 * @fires ve.dm.BranchNode#splice
107 * @return {ve.dm.BranchNode[]} Removed nodes
108 */
109 ve.dm.BranchNode.prototype.splice = function ( index, deleteCount, ...nodes ) {
110 let diff = 0;
111
112 const removals = this.children.splice( index, deleteCount, ...nodes );
113 removals.forEach( ( node ) => {
114 node.detach();
115 diff -= node.getOuterLength();
116 } );
117
118 nodes.forEach( ( node ) => {
119 node.attach( this );
120 diff += node.getOuterLength();
121 } );
122
123 this.adjustLength( diff, true );
124 this.setupBlockSlugs();
125 this.emit( 'splice', index, deleteCount, ...nodes );
126
127 return removals;
128 };
129
130 /**
131 * Setup a sparse array of booleans indicating where to place slugs
132 *
133 * TODO: The function name is misleading: in ContentBranchNodes it sets up inline slugs
134 */
135 ve.dm.BranchNode.prototype.setupBlockSlugs = function () {
136 const isBlock = this.canHaveChildrenNotContent();
137
138 this.slugPositions = {};
139
140 if ( isBlock && !this.isAllowedChildNodeType( 'paragraph' ) ) {
141 // Don't put slugs in nodes which can't contain paragraphs
142 return;
143 }
144
145 // Consider every position between two child nodes, before first child and after last child.
146 // Skip over metadata children. Add slugs in appropriate places.
147
148 // Support: Firefox
149 // Note that this inserts a slug at position 0 if this content branch has no items or only
150 // internal items, keeping the node from becoming invisible/unfocusable. In Firefox, backspace
151 // after Ctrl+A leaves the document completely empty, so this ensures DocumentNode gets a slug.
152
153 const len = this.children.length;
154 let i = -1; // from -1 to len-1
155 let j = 0; // from 0 to len
156 while ( i < len ) {
157 // If the next node is a meta item, find the first non-meta node after it, and consider that
158 // one instead when deciding to insert a slug. Meta nodes themselves don't have slugs.
159 while ( j < len && this.children[ j ].isMetaData() ) {
160 j++;
161 }
162
163 // Can have slug at the beginning, or after every node which allows it (except internal nodes)
164 const canHaveSlugAfter = i === -1 || ( this.children[ i ].canHaveSlugAfter() &&
165 !this.children[ i ].isInternal() );
166 // Can have slug at the end, or before every node which allows it
167 const canHaveSlugBefore = j === len || this.children[ j ].canHaveSlugBefore();
168
169 if ( canHaveSlugAfter && canHaveSlugBefore ) {
170 const suppressSlugTypeAfter = this.children[ j ] && this.children[ j ].suppressSlugType();
171 const suppressSlugTypeBefore = this.children[ i ] && this.children[ i ].suppressSlugType();
172 // Slugs are suppressed if they have the same string type, e.g. for adjacent floated images
173 if ( !( typeof suppressSlugTypeAfter === 'string' && suppressSlugTypeAfter === suppressSlugTypeBefore ) ) {
174 this.slugPositions[ j ] = true;
175 }
176 }
177
178 i = j;
179 j++;
180 }
181 };
182
183 /**
184 * Check in the branch node has a slug at a particular offset
185 *
186 * @param {number} offset Offset to check for a slug at
187 * @return {boolean} There is a slug at the offset
188 */
189 ve.dm.BranchNode.prototype.hasSlugAtOffset = function ( offset ) {
190 let startOffset = this.getOffset() + ( this.isWrapped() ? 1 : 0 );
191
192 if ( offset === startOffset ) {
193 return !!this.slugPositions[ 0 ];
194 }
195 for ( let i = 0; i < this.children.length; i++ ) {
196 startOffset += this.children[ i ].getOuterLength();
197 if ( offset === startOffset ) {
198 return !!this.slugPositions[ i + 1 ];
199 }
200 }
201 return false;
202 };