| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.BranchNode = function VeBranchNode( children ) { |
| 22 |
this.children = Array.isArray( children ) ? children : []; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
OO.initClass( ve.BranchNode ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
ve.BranchNode.prototype.traverse = function ( callback ) { |
| 38 |
const children = this.getChildren(); |
| 39 |
|
| 40 |
for ( let i = 0, len = children.length; i < len; i++ ) { |
| 41 |
callback.call( this, children[ i ] ); |
| 42 |
if ( children[ i ].hasChildren() ) { |
| 43 |
children[ i ].traverse( callback ); |
| 44 |
} |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ve.BranchNode.prototype.hasChildren = function () { |
| 54 |
return true; |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
ve.BranchNode.prototype.getChildren = function () { |
| 63 |
return this.children; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
ve.BranchNode.prototype.indexOf = function ( node ) { |
| 73 |
return this.children.indexOf( node ); |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
ve.BranchNode.prototype.setRoot = function ( root ) { |
| 83 |
const oldRoot = this.root; |
| 84 |
if ( root === oldRoot ) { |
| 85 |
|
| 86 |
return; |
| 87 |
} |
| 88 |
if ( oldRoot ) { |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
this.root = null; |
| 93 |
for ( let i = 0, len = this.children.length; i < len; i++ ) { |
| 94 |
this.children[ i ].setRoot( null ); |
| 95 |
} |
| 96 |
this.emit( 'unroot', oldRoot ); |
| 97 |
} |
| 98 |
this.root = root; |
| 99 |
if ( root ) { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
for ( let i = 0, len = this.children.length; i < len; i++ ) { |
| 104 |
this.children[ i ].setRoot( root ); |
| 105 |
} |
| 106 |
this.emit( 'root', root ); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ve.BranchNode.prototype.setDocument = function ( doc ) { |
| 117 |
const oldDoc = this.doc; |
| 118 |
if ( doc === this.doc ) { |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
let i, len; |
| 123 |
if ( oldDoc ) { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
this.doc = null; |
| 128 |
for ( i = 0, len = this.children.length; i < len; i++ ) { |
| 129 |
this.children[ i ].setDocument( null ); |
| 130 |
} |
| 131 |
oldDoc.nodeDetached( this ); |
| 132 |
} |
| 133 |
this.doc = doc; |
| 134 |
if ( doc ) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
for ( i = 0, len = this.children.length; i < len; i++ ) { |
| 139 |
this.children[ i ].setDocument( doc ); |
| 140 |
} |
| 141 |
doc.nodeAttached( this ); |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
ve.BranchNode.prototype.getNodeFromOffset = function ( offset, shallow ) { |
| 157 |
let currentNode = this; |
| 158 |
if ( typeof offset !== 'number' ) { |
| 159 |
throw new Error( 'Offset must be a number' ); |
| 160 |
} |
| 161 |
if ( offset === 0 ) { |
| 162 |
return currentNode; |
| 163 |
} |
| 164 |
if ( offset < 0 ) { |
| 165 |
throw new Error( 'Offset out of bounds' ); |
| 166 |
} |
| 167 |
let nodeOffset = 0; |
| 168 |
|
| 169 |
SIBLINGS: |
| 170 |
while ( currentNode.children.length ) { |
| 171 |
for ( let i = 0, length = currentNode.children.length; i < length; i++ ) { |
| 172 |
const childNode = currentNode.children[ i ]; |
| 173 |
if ( offset === nodeOffset ) { |
| 174 |
|
| 175 |
|
| 176 |
return currentNode; |
| 177 |
} |
| 178 |
if ( childNode instanceof ve.ce.InternalListNode ) { |
| 179 |
break SIBLINGS; |
| 180 |
} |
| 181 |
const nodeLength = childNode.getOuterLength(); |
| 182 |
if ( offset >= nodeOffset && offset < nodeOffset + nodeLength ) { |
| 183 |
if ( !shallow && childNode.hasChildren() && childNode.getChildren().length ) { |
| 184 |
|
| 185 |
|
| 186 |
nodeOffset += 1; |
| 187 |
currentNode = childNode; |
| 188 |
continue SIBLINGS; |
| 189 |
} else { |
| 190 |
return childNode; |
| 191 |
} |
| 192 |
} |
| 193 |
nodeOffset += nodeLength; |
| 194 |
} |
| 195 |
if ( offset === nodeOffset ) { |
| 196 |
|
| 197 |
|
| 198 |
return currentNode; |
| 199 |
} |
| 200 |
} |
| 201 |
return null; |
| 202 |
}; |