Skip to content

AstRange (EN)

bhsd edited this page Jan 3, 2024 · 5 revisions
Table of Contents

Other Languages

Introduction

Modeled after the Range class, the properties and methods are also very similar to the Range class. The main difference is that the starting and ending points of AstRange must be consecutive child nodes of the same node.

All of the following properties and methods are not available in the Mini and Browser versions.

Properties

startContainer

Expand

type: AstNode
The container of the starting point, read-only.

// startContainer
var root = Parser.parse(''),
	range = root.createRange();
range.setStart(root, 0);
assert.strictEqual(range.startContainer, root);

startOffset

Expand

type: number
The position of the starting point, read-only.

// startOffset
var root = Parser.parse(''),
	range = root.createRange();
range.setStart(root, 0);
assert.strictEqual(range.startOffset, 0);

startIndex

Expand

type: number
The absolute position of the starting point, read-only.

// startIndex
var root = Parser.parse([[a]]b'),
	range = root.createRange();
range.setStart(root, 1);
assert.strictEqual(range.startIndex, 5);

startPos

Expand

type: {top: number, left: number}
The row and column position of the starting point, read-only.

// startPos
var root = Parser.parse([[a]]b'),
	range = root.createRange();
range.setStart(root, 1);
assert.deepStrictEqual(range.startPos, {top: 0, left: 5});

endContainer

Expand

type: AstNode
The container of the end point, read-only.

// endContainer
var root = Parser.parse(''),
	range = root.createRange();
range.setEnd(root, 0);
assert.strictEqual(range.endContainer, root);

endOffset

Expand

type: number
The position of the end point, read-only.

// endOffset
var root = Parser.parse(''),
	range = root.createRange();
range.setEnd(root, 0);
assert.strictEqual(range.endOffset, 0);

endIndex

Expand

type: number
The absolute position of the end point, read-only.

// endIndex
var root = Parser.parse([[a]]b'),
	range = root.createRange();
range.setEnd(root, 1);
assert.strictEqual(range.endIndex, 5);

endPos

Expand

type: {top: number, left: number}
The row and column position of the end point, read-only.

// endPos
var root = Parser.parse([[a]]b'),
	range = root.createRange();
range.setEnd(root, 1);
assert.deepStrictEqual(range.endPos, {top: 0, left: 5});

collapsed

Expand

type: boolean
Whether the starting and ending positions are coincident, read-only.

// collapsed
var root = Parser.parse('a'),
	range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 0);
assert(range.collapsed);
range.setEnd(root, 1);
assert(!range.collapsed);

commonAncestorContainer

Expand

type: AstNode
The closest common ancestor of the starting and ending points, read-only.

// commonAncestorContainer
var root = Parser.parse('a[[b]]'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 0);
range.setEnd(firstChild, 1);
assert.strictEqual(range.commonAncestorContainer, firstChild);
range.setEnd(root, 2);
assert.strictEqual(range.commonAncestorContainer, root);

Methods

setStart

Expand

param: AstNode Container of the starting point
param: number Position of the starting point
Set the starting point.

setEnd

Expand

param: AstNode Container of the end point
param: number Position of the end point
Set the end point.

setStartAfter

Expand

param: AstNode Node
Set the starting point after the node.

// setStartAfter
var root = Parser.parse('a'),
	{firstChild} = root,
	range = root.createRange();
range.setStartAfter(firstChild);
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);

setEndAfter

Expand

param: AstNode Node
Set the end point after the node.

// setEndAfter
var root = Parser.parse('a'),
	{firstChild} = root,
	range = root.createRange();
range.setEndAfter(firstChild);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 1);

setStartBefore

Expand

param: AstNode Node
Set the starting point before the node.

// setStartBefore
var root = Parser.parse('a'),
	{firstChild} = root,
	range = root.createRange();
range.setStartBefore(firstChild);
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 0);

setEndBefore

Expand

param: AstNode Node
Set the end point before the node.

// setEndBefore
var root = Parser.parse('a'),
	{firstChild} = root,
	range = root.createRange();
range.setEndBefore(firstChild);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 0);

selectNodeContents

Expand

param: AstNode Node
Set the Range to contain the entire contents of the node.

// selectNodeContents
var {firstChild} = Parser.parse('[[a|b]]'),
	range = firstChild.createRange();
range.selectNodeContents(firstChild);
assert.strictEqual(range.startContainer, firstChild);
assert.strictEqual(range.startOffset, 0);
assert.strictEqual(range.endContainer, firstChild);
assert.strictEqual(range.endOffset, 2);

selectNode

Expand

param: AstNode Node
Set the Range to contain the entire node.

// selectNode
var root = Parser.parse('[[a]]'),
	{firstChild} = root,
	range = root.createRange();
range.selectNode(firstChild);
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 0);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 1);

collapse

Expand

param: boolean Whether to collapse to the start point
Collapse the Range to the start or end point.

// collapse
var root = Parser.parse('a'),
	range = root.createRange();
range.setEnd(root, 1);
range.collapse();
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);
range.setStart(root, 0);
range.collapse(true);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 0);

comparePoint

Expand

param: AstNode Container of the point
param: number Position of the point
returns: -1 | 0 | 1
Compare the position of the point with the Range.

// comparePoint
var root = Parser.parse('abcd'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(firstChild, 3);
assert.equal(range, 'bc');
assert.strictEqual(range.comparePoint(firstChild, 0), -1);
assert.strictEqual(range.comparePoint(firstChild, 2), 0);
assert.strictEqual(range.comparePoint(firstChild, 4), 1);

isPointInRange

Expand

param: AstNode Container of the point
param: number Position of the point
returns: boolean
Whether the point is in the Range.

// isPointInRange
var root = Parser.parse('abcd'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(firstChild, 3);
assert.equal(range, 'bc');
assert(!range.isPointInRange(firstChild, 0));
assert(range.isPointInRange(firstChild, 2));

cloneRange

Expand

returns: AstRange
Clone the AstRange object.

// cloneRange
var root = Parser.parse('a'),
	range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 1);
assert.deepStrictEqual(range.cloneRange(), range);

deleteContents

Expand

Delete the contents of the Range.

// deleteContents
var root = Parser.parse('ab[[c]]'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b[[c]]');
range.deleteContents();
assert.equal(root, 'a');

getBoundingClientRect

Expand

returns: {top: number, left: number, height: number, left: number}
Get the row and column position and size of the Range.

// getBoundingClientRect
var root = Parser.parse('ab\n[[c]]'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b\n[[c]]');
assert.deepStrictEqual(range.getBoundingClientRect(), {
	top: 0,
	left: 1,
	height: 2,
	width: 5,
});

insertNode

Expand

param: AstNode | string Node or text to insert
Insert a node or text at the starting point.

// insertNode
var root = Parser.parse('ab[[c]]'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b[[c]]');
range.insertNode('d');
assert.equal(root, 'adb[[c]]');
assert.equal(range, 'b[[c]]');

extractContents

Expand

returns: AstNode[]
Get all the nodes in the Range when possible.

// extractContents
var root = Parser.parse('ab[[c]]'),
	{firstChild, lastChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b[[c]]');
var extracted = range.extractContents();
assert.deepStrictEqual(extracted, [
	root.createTextNode('b'),
	lastChild,
]);
assert.strictEqual(extracted[1], lastChild);
assert.equal(range, 'b[[c]]');

cloneContents

Expand

returns: AstNode[]
Clone all the nodes in the Range when possible.

// cloneContents
var root = Parser.parse('ab[[c]]'),
	{firstChild, lastChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b[[c]]');
var cloned = range.cloneContents();
assert.deepStrictEqual(cloned, [
	root.createTextNode('b'),
	lastChild,
]);
assert(cloned[1] !== lastChild);
assert.equal(range, 'b[[c]]');
Clone this wiki locally