-
Notifications
You must be signed in to change notification settings - Fork 1
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 start and end 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 start point, read-only.
// startContainer (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startContainer, root);
range.setStart(lastChild, 0);
assert.strictEqual(range.startContainer, lastChild);
Expand
type: number
The position of the start point, read-only.
// startOffset (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startOffset, 1);
range.setStart(lastChild, 1);
assert.strictEqual(range.startOffset, 1);
Expand
type: number
The absolute position of the start point, read-only.
// startIndex (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startIndex, 5);
range.setStart(lastChild, 1);
assert.strictEqual(range.startIndex, 6);
Expand
type: {top: number, left: number}
The row and column position of the start point, read-only.
// startPos (main)
var root = Parser.parse([[a]]\nb'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, '\nb');
range.setStart(root, 1);
assert.deepStrictEqual(range.startPos, {top: 0, left: 5});
range.setStart(lastChild, 1);
assert.deepStrictEqual(range.startPos, {top: 1, left: 0});
Expand
type: AstNode
The container of the end point, read-only.
// endContainer (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setEnd(root, 1);
assert.strictEqual(range.endContainer, root);
range.setEnd(lastChild, 0);
assert.strictEqual(range.endContainer, lastChild);
Expand
type: number
The position of the end point, read-only.
// endOffset (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setEnd(root, 1);
assert.strictEqual(range.endOffset, 1);
range.setEnd(lastChild, 1);
assert.strictEqual(range.endOffset, 1);
Expand
type: number
The absolute position of the end point, read-only.
// endIndex (main)
var root = Parser.parse([[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setEnd(root, 1);
assert.strictEqual(range.endIndex, 5);
range.setEnd(lastChild, 1);
assert.strictEqual(range.endIndex, 6);
Expand
type: {top: number, left: number}
The row and column position of the end point, read-only.
// endPos (main)
var root = Parser.parse([[a]]\nb'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, '\nb');
range.setEnd(root, 1);
assert.deepStrictEqual(range.endPos, {top: 0, left: 5});
range.setEnd(lastChild, 1);
assert.deepStrictEqual(range.endPos, {top: 1, left: 0});
Expand
type: boolean
Whether the start and end points are coincident, read-only.
// collapsed (main)
var root = Parser.parse([[a]]b'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'b');
range.setStart(root, 1);
range.setEnd(root, 1);
assert(range.collapsed);
range.setEnd(lastChild, 0);
assert(!range.collapsed);
Expand
type: AstNode
The closest common ancestor of the start and end points, read-only.
// commonAncestorContainer (main)
var root = Parser.parse([[a]]b'),
{firstChild, lastChild} = root,
range = root.createRange();
assert.equal(firstChild, '[[a]]');
range.setStart(lastChild, 0);
range.setEnd(lastChild, 1);
assert.strictEqual(range.commonAncestorContainer, lastChild);
range.setStart(root, 0);
assert.strictEqual(range.commonAncestorContainer, root);
Expand
param: AstNode
Container of the start point
param: number
Position of the start point
Set the start 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 start point after the node.
// setStartAfter (main)
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 (main)
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 start point before the node.
// setStartBefore (main)
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 (main)
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 (main)
var root = Parser.parse([[a]]b'),
range = root.createRange();
range.selectNodeContents(root);
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 0);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 2);
Expand
param: AstNode
Node
Set the Range to contain the entire node.
// selectNode (main)
var root = Parser.parse([[a]]b'),
{firstChild} = root,
range = root.createRange();
assert.equal(firstChild, '[[a]]');
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 (main)
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 (main)
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);
range.selectNodeContents(firstChild);
assert.strictEqual(range.comparePoint(root, 0), -1);
assert.strictEqual(range.comparePoint(root, 1), 1);
Expand
param: AstNode
Container of the point
param: number
Position of the point
returns: boolean
Whether the point is in the Range.
// isPointInRange (main)
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));
assert(!range.isPointInRange(firstChild, 4));
range.selectNodeContents(firstChild);
assert(!range.isPointInRange(root, 0));
assert(!range.isPointInRange(root, 1));
Expand
returns: AstRange
Clone the AstRange object.
// cloneRange (main)
var root = Parser.parse('a'),
range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 1);
assert.deepStrictEqual(range.cloneRange(), range);
Expand
Clear the Range.
// detach (main)
var root = Parser.parse('a'),
range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 1);
range.detach();
Expand
Delete the contents of the Range.
// deleteContents (main)
var root = Parser.parse('ab[[c]]{{d}}e'),
{firstChild, lastChild} = root,
range = root.createRange();
assert.equal(firstChild, 'ab');
assert.equal(lastChild, 'e');
range.setStart(firstChild, 0);
range.setEnd(firstChild, 1);
assert.equal(range, 'a');
range.deleteContents();
assert.equal(root, 'b[[c]]{{d}}e');
assert.strictEqual(range.startContainer, firstChild);
assert.strictEqual(range.startOffset, 0);
assert.strictEqual(range.endContainer, firstChild);
assert.strictEqual(range.endOffset, 0);
range.detach();
range.setStart(root, 2);
range.setEnd(lastChild, 1);
assert.equal(range, '{{d}}e');
range.deleteContents();
assert.equal(root, 'b[[c]]');
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 2);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 2);
range.setStart(firstChild, 0);
assert(range, 'b[[c]]');
range.deleteContents();
assert.equal(root, '');
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 1);
Expand
returns: {top: number, left: number, height: number, left: number}
Get the row and column position and size of the Range.
// getBoundingClientRect (main)
var root = Parser.parse('ab\n[[c]]'),
{firstChild} = root,
range = root.createRange();
assert.equal(firstChild, 'ab\n');
range.setStart(firstChild, 1);
range.setEnd(firstChild, 2);
assert.equal(range, 'b');
assert.deepStrictEqual(range.getBoundingClientRect(), {
top: 0,
left: 1,
height: 1,
width: 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 start point.
// insertNode (main)
var root = Parser.parse('abcd[[e]]'),
{firstChild} = root,
range = root.createRange(),
endContainer;
range.setStart(firstChild, 1);
range.setEnd(firstChild, 3);
assert.equal(range, 'bc');
range.insertNode('f');
assert.equal(root, 'afbcd[[e]]');
assert.equal(range, 'bc');
range.setEnd(root, 4);
range.setStart(range.startContainer, 1);
assert.equal(range, 'cd[[e]]');
range.insertNode('g');
assert.equal(root, 'afbgcd[[e]]');
assert.equal(range, 'cd[[e]]');
range.insertNode('h');
assert.equal(root, 'afbghcd[[e]]');
assert.equal(range, 'cd[[e]]');
range.setEnd(range.startContainer, 2);
assert.equal(range, 'cd');
range.insertNode('i');
assert.equal(root, 'afbghicd[[e]]');
assert.equal(range, 'cd');
range.setStart(root, 1);
assert.equal(range, 'fbghicd');
range.insertNode('j');
assert.equal(root, 'ajfbghicd[[e]]');
assert.equal(range, 'fbghicd');
({endContainer} = range);
range.detach();
range.setStart(root, 0);
range.insertNode('k');
assert.equal(root, 'kajfbghicd[[e]]');
range.setStart(endContainer, 1);
range.insertNode('l');
assert.equal(root, 'kajfbghicld[[e]]');
Expand
returns: AstNode[]
Get all the nodes in the Range.
// extractContents (main)
var root = Parser.parse('abc'),
{firstChild} = root,
range = root.createRange();
assert.equal(firstChild, 'abc');
range.setStart(firstChild, 1);
range.setEnd(firstChild, 2);
assert.equal(range, 'b');
assert.deepStrictEqual(
range.extractContents(),
[root.createTextNode('b')],
);
assert.equal(range, 'b');
range.setEnd(root, 3);
assert.equal(range, 'bc');
assert.deepStrictEqual(range.extractContents(), [
root.createTextNode('b'),
root.createTextNode('c'),
]);
range.setStart(root, 0);
assert.equal(range, 'abc');
assert.deepStrictEqual(range.extractContents(), [
root.createTextNode('a'),
root.createTextNode('b'),
root.createTextNode('c'),
]);
Expand
returns: AstNode[]
Clone all the nodes in the Range.
// cloneContents (main)
var root = Parser.parse('ab<p>'),
{firstChild} = root,
range = root.createRange(),
cloned;
assert.equal(firstChild, 'ab');
range.setStart(firstChild, 1);
range.setEnd(root, 2);
assert.equal(range, 'b<p>');
cloned = range.cloneContents();
assert.deepStrictEqual(cloned, [
root.createTextNode('b'),
root.createElement('p'),
]);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext