| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.dm.BranchNode = function VeDmBranchNode( element, children ) { |
| 21 |
|
| 22 |
ve.BranchNode.call( this ); |
| 23 |
|
| 24 |
|
| 25 |
ve.dm.BranchNode.super.call( this, element ); |
| 26 |
|
| 27 |
|
| 28 |
this.slugPositions = {}; |
| 29 |
|
| 30 |
|
| 31 |
if ( Array.isArray( children ) && children.length ) { |
| 32 |
this.splice( 0, 0, ...children ); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
OO.inheritClass( ve.dm.BranchNode, ve.dm.Node ); |
| 47 |
|
| 48 |
OO.mixinClass( ve.dm.BranchNode, ve.BranchNode ); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
ve.dm.BranchNode.prototype.unshift = function ( childModel ) { |
| 83 |
this.splice( 0, 0, childModel ); |
| 84 |
return this.children.length; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
const len = this.children.length; |
| 154 |
let i = -1; |
| 155 |
let j = 0; |
| 156 |
while ( i < len ) { |
| 157 |
|
| 158 |
|
| 159 |
while ( j < len && this.children[ j ].isMetaData() ) { |
| 160 |
j++; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
const canHaveSlugAfter = i === -1 || ( this.children[ i ].canHaveSlugAfter() && |
| 165 |
!this.children[ i ].isInternal() ); |
| 166 |
|
| 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 |
|
| 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 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
}; |