-
Notifications
You must be signed in to change notification settings - Fork 2
AstRange (EN)
Table of Contents
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.
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);
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);
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);
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});
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);
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);
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);
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});
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);
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);
Expand
param: AstNode
Container of the starting point
param: number
Position of the starting point
Set the starting point.
Expand
param: AstNode
Container of the end point
param: number
Position of the end point
Set the end point.
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);
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);
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);
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);
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);
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);
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);
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);
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));
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);
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');
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,
});
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]]');
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]]');
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]]');
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext