| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.dm.TreeCursor = function VeDmTreeCursor( root, liveIgnoreNodes, linearOffset ) { |
| 21 |
this.root = root; |
| 22 |
this.liveIgnoreNodes = liveIgnoreNodes; |
| 23 |
this.path = []; |
| 24 |
this.offset = 0; |
| 25 |
this.nodes = [ root ]; |
| 26 |
this.node = root; |
| 27 |
this.lastStep = null; |
| 28 |
if ( linearOffset === undefined ) { |
| 29 |
linearOffset = 0; |
| 30 |
} |
| 31 |
this.linearOffset = linearOffset; |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
OO.initClass( ve.dm.TreeCursor ); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ve.dm.TreeCursor.prototype.normalizeCursor = function ( tooShort ) { |
| 46 |
if ( !this.node ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
if ( tooShort === undefined ) { |
| 50 |
tooShort = -1; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
if ( this.node.type === 'text' && this.offset === this.node.length ) { |
| 55 |
this.nodes.pop(); |
| 56 |
this.node = this.nodes[ this.nodes.length - 1 ]; |
| 57 |
this.offset = this.path.pop() + 1; |
| 58 |
} |
| 59 |
this.crossIgnoredNodes(); |
| 60 |
|
| 61 |
let item; |
| 62 |
|
| 63 |
if ( |
| 64 |
this.node.hasChildren() && |
| 65 |
( item = this.node.children[ this.offset ] ) && |
| 66 |
item.type === 'text' && |
| 67 |
item.length > tooShort |
| 68 |
) { |
| 69 |
this.node = item; |
| 70 |
this.nodes.push( item ); |
| 71 |
this.path.push( this.offset ); |
| 72 |
this.offset = 0; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
ve.dm.TreeCursor.prototype.crossIgnoredNodes = function () { |
| 80 |
let parent, nextSibling; |
| 81 |
if ( |
| 82 |
this.node && |
| 83 |
this.node.type === 'text' && |
| 84 |
this.offset === this.node.length && |
| 85 |
( parent = this.nodes[ this.nodes.length - 2 ] ) && |
| 86 |
( nextSibling = parent.children[ this.path[ this.path.length - 1 ] + 1 ] ) && |
| 87 |
this.liveIgnoreNodes.indexOf( nextSibling ) !== -1 |
| 88 |
) { |
| 89 |
|
| 90 |
this.stepOut(); |
| 91 |
} |
| 92 |
const len = ( this.node && this.node.hasChildren() && this.node.children.length ) || 0; |
| 93 |
let item; |
| 94 |
while ( |
| 95 |
this.offset < len && |
| 96 |
( item = this.node.children[ this.offset ] ) && |
| 97 |
this.liveIgnoreNodes.indexOf( item ) !== -1 |
| 98 |
) { |
| 99 |
this.offset++; |
| 100 |
this.linearOffset += item.getOuterLength(); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
ve.dm.TreeCursor.prototype.checkLinearOffset = function () { |
| 112 |
let expected = this.node.getOffset(); |
| 113 |
if ( this.node.type === 'text' ) { |
| 114 |
expected += this.offset; |
| 115 |
} else { |
| 116 |
if ( this.node !== this.root ) { |
| 117 |
|
| 118 |
expected += 1; |
| 119 |
} |
| 120 |
if ( this.node.hasChildren() ) { |
| 121 |
|
| 122 |
this.node.children.slice( 0, this.offset ).forEach( ( child ) => { |
| 123 |
expected += child.getOuterLength(); |
| 124 |
} ); |
| 125 |
} |
| 126 |
} |
| 127 |
if ( expected !== this.linearOffset ) { |
| 128 |
throw new Error( 'Linear offset does not match tree position' ); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
ve.dm.TreeCursor.prototype.stepAtMost = function ( maxLength ) { |
| 156 |
if ( !this.node ) { |
| 157 |
this.lastStep = null; |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
if ( ve.test ) { |
| 162 |
this.checkLinearOffset(); |
| 163 |
} |
| 164 |
this.normalizeCursor( maxLength ); |
| 165 |
let length, step; |
| 166 |
if ( this.node.type === 'text' ) { |
| 167 |
|
| 168 |
length = Math.min( maxLength, this.node.length - this.offset ); |
| 169 |
step = { |
| 170 |
type: 'crosstext', |
| 171 |
length: length, |
| 172 |
path: this.path.slice(), |
| 173 |
node: this.node, |
| 174 |
offset: this.offset, |
| 175 |
offsetLength: length |
| 176 |
}; |
| 177 |
this.offset += step.length; |
| 178 |
this.lastStep = step; |
| 179 |
this.linearOffset += length; |
| 180 |
return step; |
| 181 |
} |
| 182 |
|
| 183 |
const childLength = this.node.hasChildren() ? this.node.children.length : 0; |
| 184 |
if ( this.offset > childLength ) { |
| 185 |
throw new Error( 'Offset ' + this.offset + ' > childLength ' + childLength ); |
| 186 |
} |
| 187 |
if ( this.offset === childLength ) { |
| 188 |
return this.stepOut(); |
| 189 |
} |
| 190 |
|
| 191 |
const item = this.node.children[ this.offset ]; |
| 192 |
if ( item.getOuterLength() > maxLength ) { |
| 193 |
return this.stepIn(); |
| 194 |
} |
| 195 |
|
| 196 |
length = item.getOuterLength(); |
| 197 |
step = { |
| 198 |
type: 'cross', |
| 199 |
length: length, |
| 200 |
path: this.path.slice(), |
| 201 |
node: this.node, |
| 202 |
offset: this.offset, |
| 203 |
item: item |
| 204 |
}; |
| 205 |
this.offset++; |
| 206 |
this.lastStep = step; |
| 207 |
this.linearOffset += length; |
| 208 |
return step; |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
ve.dm.TreeCursor.prototype.stepIn = function () { |
| 217 |
if ( |
| 218 |
this.node.type === 'text' || |
| 219 |
!this.node.hasChildren() || |
| 220 |
this.offset >= this.node.children.length |
| 221 |
) { |
| 222 |
throw new Error( 'No node to step into' ); |
| 223 |
} |
| 224 |
const item = this.node.children[ this.offset ]; |
| 225 |
const length = item.type === 'text' ? 0 : 1; |
| 226 |
const step = { |
| 227 |
type: 'open', |
| 228 |
length: length, |
| 229 |
path: this.path.slice(), |
| 230 |
node: this.node, |
| 231 |
offset: this.offset, |
| 232 |
item: item |
| 233 |
}; |
| 234 |
this.path.push( this.offset ); |
| 235 |
this.nodes.push( item ); |
| 236 |
this.node = item; |
| 237 |
this.offset = 0; |
| 238 |
this.lastStep = step; |
| 239 |
this.linearOffset += length; |
| 240 |
return step; |
| 241 |
}; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
ve.dm.TreeCursor.prototype.stepOut = function () { |
| 249 |
const priorOffset = this.offset; |
| 250 |
const item = this.nodes.pop(); |
| 251 |
this.node = this.nodes[ this.nodes.length - 1 ]; |
| 252 |
this.offset = this.path.pop(); |
| 253 |
if ( this.node === undefined ) { |
| 254 |
|
| 255 |
this.lastStep = null; |
| 256 |
return null; |
| 257 |
} |
| 258 |
if ( item.type === 'text' ) { |
| 259 |
this.linearOffset += item.getLength() - priorOffset; |
| 260 |
} else { |
| 261 |
if ( item.hasChildren() ) { |
| 262 |
|
| 263 |
item.children.slice( priorOffset ).forEach( ( child ) => { |
| 264 |
this.linearOffset += child.getOuterLength(); |
| 265 |
} ); |
| 266 |
} |
| 267 |
|
| 268 |
this.linearOffset++; |
| 269 |
} |
| 270 |
const step = { |
| 271 |
type: 'close', |
| 272 |
length: item.type === 'text' ? 0 : 1, |
| 273 |
path: this.path.slice(), |
| 274 |
node: this.node, |
| 275 |
offset: this.offset, |
| 276 |
item: item |
| 277 |
}; |
| 278 |
this.offset++; |
| 279 |
this.lastStep = step; |
| 280 |
return step; |
| 281 |
}; |