All files / src/dm ve.dm.TreeCursor.js

91.34% Statements 95/104
85.29% Branches 58/68
100% Functions 9/9
91.34% Lines 95/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282                                      1x 2626x 2626x 2626x 2626x 2626x 2626x 2626x 2626x 2626x   2626x         1x                 1x 19111x     19111x         19111x 720x 720x 720x   19111x       19111x           743x 743x 743x 743x             1x   27545x                 1x   27545x   27545x         392x 392x                     1x 19111x 19111x 1700x   17411x   8671x   17411x   17350x 29392x       19111x                                                       1x 19111x         19111x 19111x   19111x   19111x   1723x 1723x               1723x 1723x 1723x 1723x     17388x 17388x     17388x 3703x     13685x 13685x 3793x     9892x 9892x               9892x 9892x 9892x 9892x               1x 3793x             3793x 3793x 3793x               3793x 3793x 3793x 3793x 3793x 3793x 3793x               1x 3808x 3808x 3808x 3808x 3808x         3808x 22x   3786x   3725x 23x       3786x   3808x               3808x 3808x 3808x    
/*!
 * VisualEditor DataModel TreeCursor class
 *
 * @copyright See AUTHORS.txt
 */
 
// TODO identify a core of "trusted" code that is guaranteed to detect tree invalidation.
// Probably needs: removeNode insertNode removeText insertText
 
/**
 * DataModel TreeCursor - a tree walker that tracks the path to the current position.
 *
 * @class
 *
 * @constructor
 * @param {ve.dm.Node} root A document node or a branch root within which to walk
 * @param {ve.dm.Node[]} liveIgnoreNodes Live array of nodes to ignore (cross without counting)
 * @param {number} [linearOffset] The first linear model offset inside root; default 0
 */
ve.dm.TreeCursor = function VeDmTreeCursor( root, liveIgnoreNodes, linearOffset ) {
	this.root = root;
	this.liveIgnoreNodes = liveIgnoreNodes;
	this.path = [];
	this.offset = 0;
	this.nodes = [ root ];
	this.node = root;
	this.lastStep = null;
	Eif ( linearOffset === undefined ) {
		linearOffset = 0;
	}
	this.linearOffset = linearOffset;
};
 
/* Inheritance */
 
OO.initClass( ve.dm.TreeCursor );
 
/* Methods */
 
/**
 * Skip past ignored nodes and text node boundaries
 *
 * @param {number} [tooShort] Only step into text nodes longer than this
 */
ve.dm.TreeCursor.prototype.normalizeCursor = function ( tooShort ) {
	Iif ( !this.node ) {
		return;
	}
	Iif ( tooShort === undefined ) {
		tooShort = -1;
	}
 
	// If at the end of a text node, step out
	if ( this.node.type === 'text' && this.offset === this.node.length ) {
		this.nodes.pop();
		this.node = this.nodes[ this.nodes.length - 1 ];
		this.offset = this.path.pop() + 1;
	}
	this.crossIgnoredNodes();
 
	let item;
	// If at the start of long enough text node, step in
	if (
		this.node.hasChildren() &&
		( item = this.node.children[ this.offset ] ) &&
		item.type === 'text' &&
		item.length > tooShort
	) {
		this.node = item;
		this.nodes.push( item );
		this.path.push( this.offset );
		this.offset = 0;
	}
};
 
/**
 * Cross any immediately following nodes that are in liveIgnoreNodes
 */
ve.dm.TreeCursor.prototype.crossIgnoredNodes = function () {
	let parent, nextSibling;
	if (
		this.node &&
		this.node.type === 'text' &&
		this.offset === this.node.length &&
		( parent = this.nodes[ this.nodes.length - 2 ] ) &&
		( nextSibling = parent.children[ this.path[ this.path.length - 1 ] + 1 ] ) &&
		this.liveIgnoreNodes.indexOf( nextSibling ) !== -1
	) {
		// At the end of a text node and the next node is ignored
		this.stepOut();
	}
	const len = ( this.node && this.node.hasChildren() && this.node.children.length ) || 0;
	let item;
	while (
		this.offset < len &&
		( item = this.node.children[ this.offset ] ) &&
		this.liveIgnoreNodes.indexOf( item ) !== -1
	) {
		this.offset++;
		this.linearOffset += item.getOuterLength();
	}
};
 
/**
 * Validate this.linearOffset against this.node and this.offset
 *
 * TODO: Improve the unit tests, then move this method to the unit testing framework
 *
 * @throws {Error} If this.linearOffset does not match this.node and this.offset
 */
ve.dm.TreeCursor.prototype.checkLinearOffset = function () {
	let expected = this.node.getOffset();
	if ( this.node.type === 'text' ) {
		expected += this.offset;
	} else {
		if ( this.node !== this.root ) {
			// Add 1 for the open tag
			expected += 1;
		}
		if ( this.node.hasChildren() ) {
			// Add the outer length of each crossed child
			this.node.children.slice( 0, this.offset ).forEach( ( child ) => {
				expected += child.getOuterLength();
			} );
		}
	}
	Iif ( expected !== this.linearOffset ) {
		throw new Error( 'Linear offset does not match tree position' );
	}
};
 
/**
 * @typedef {Object} Step
 * @memberof ve.dm.TreeCursor
 * @property {string} type open|close|cross|crosstext
 * @property {number} length Linear length of the step (integer >= 1)
 * @property {number[]} path Offset path from the root to the node containing the stepped item
 * @property {ve.dm.Node|null} node The node containing the stepped item
 * @property {number} offset The offset of the stepped item within its parent
 * @property {number} [offsetLength] Number of characters 'crosstext' passed
 * @property {ve.dm.Node} [item] The node stepped into/out of/across (absent for 'crosstext')
 */
 
/**
 * Take a single step in the walk, consuming no more than a given linear model length
 *
 * A "single step" means either stepping across text content, or stepping over a node, or
 * stepping into/out of a non-text node. (Steps into/out of text nodes happen transparently)
 *
 * See https://phabricator.wikimedia.org/T162762 for the algorithm
 *
 * @param {number} maxLength Maximum linear model length to step over (integer >= 1)
 * @return {ve.dm.TreeCursor.Step|null} The type of step taken, or null if there are no more steps
 */
ve.dm.TreeCursor.prototype.stepAtMost = function ( maxLength ) {
	Iif ( !this.node ) {
		this.lastStep = null;
		return null;
	}
	// On very large pages this is a performance bottleneck, so only run in tests
	Eif ( ve.test ) {
		this.checkLinearOffset();
	}
	this.normalizeCursor( maxLength );
	let length, step;
	if ( this.node.type === 'text' ) {
		// We cannot be the end, because we just normalized
		length = Math.min( maxLength, this.node.length - this.offset );
		step = {
			type: 'crosstext',
			length: length,
			path: this.path.slice(),
			node: this.node,
			offset: this.offset,
			offsetLength: length
		};
		this.offset += step.length;
		this.lastStep = step;
		this.linearOffset += length;
		return step;
	}
	// Else not a text node
	const childLength = this.node.hasChildren() ? this.node.children.length : 0;
	Iif ( this.offset > childLength ) {
		throw new Error( 'Offset ' + this.offset + ' > childLength ' + childLength );
	}
	if ( this.offset === childLength ) {
		return this.stepOut();
	}
	// Else there are unpassed child nodes
	const item = this.node.children[ this.offset ];
	if ( item.getOuterLength() > maxLength ) {
		return this.stepIn();
	}
	// Else step across this item
	length = item.getOuterLength();
	step = {
		type: 'cross',
		length: length,
		path: this.path.slice(),
		node: this.node,
		offset: this.offset,
		item: item
	};
	this.offset++;
	this.lastStep = step;
	this.linearOffset += length;
	return step;
};
 
/**
 * Step into the next node
 *
 * @return {Object} The step
 */
ve.dm.TreeCursor.prototype.stepIn = function () {
	Iif (
		this.node.type === 'text' ||
		!this.node.hasChildren() ||
		this.offset >= this.node.children.length
	) {
		throw new Error( 'No node to step into' );
	}
	const item = this.node.children[ this.offset ];
	const length = item.type === 'text' ? 0 : 1;
	const step = {
		type: 'open',
		length: length,
		path: this.path.slice(),
		node: this.node,
		offset: this.offset,
		item: item
	};
	this.path.push( this.offset );
	this.nodes.push( item );
	this.node = item;
	this.offset = 0;
	this.lastStep = step;
	this.linearOffset += length;
	return step;
};
 
/**
 * Step out of the current node (skipping past any uncrossed children or text within)
 *
 * @return {Object|null} The step
 */
ve.dm.TreeCursor.prototype.stepOut = function () {
	const priorOffset = this.offset;
	const item = this.nodes.pop();
	this.node = this.nodes[ this.nodes.length - 1 ];
	this.offset = this.path.pop();
	Iif ( this.node === undefined ) {
		// Stepped out of the root
		this.lastStep = null;
		return null;
	}
	if ( item.type === 'text' ) {
		this.linearOffset += item.getLength() - priorOffset;
	} else {
		if ( item.hasChildren() ) {
			// Increase linearOffset by the length of each child
			item.children.slice( priorOffset ).forEach( ( child ) => {
				this.linearOffset += child.getOuterLength();
			} );
		}
		// Increase linearOffset for the close tag
		this.linearOffset++;
	}
	const step = {
		type: 'close',
		length: item.type === 'text' ? 0 : 1,
		path: this.path.slice(),
		node: this.node,
		offset: this.offset,
		item: item
	};
	this.offset++;
	this.lastStep = step;
	return step;
};