-
Notifications
You must be signed in to change notification settings - Fork 2
AstRange
目录
仿照 Range 类设计,属性和方法也和 Range 类非常相似。主要的区别在于 AstRange 的起点和终点间必须为同一节点的连续子节点。
以下所有属性和方法在 Mini 和 Browser 版本中均不可用。
展开
type: AstNode
起点容器,只读。
// 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);
展开
type: number
起点位置,只读。
// 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);
展开
type: number
起点绝对位置,只读。
// 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);
展开
type: {top: number, left: number}
起点行列位置,只读。
// 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});
展开
type: AstNode
终点容器,只读。
// 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);
展开
type: number
终点位置,只读。
// 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);
展开
type: number
终点绝对位置,只读。
// 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);
展开
type: {top: number, left: number}
终点行列位置,只读。
// 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});
展开
type: boolean
起始和终止位置是否重合,只读。
// 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);
展开
type: AstNode
最近的公共祖先,只读。
// 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);
展开
param: AstNode
起点容器
param: number
起点位置
设置起点。
展开
param: AstNode
终点容器
param: number
终点位置
设置终点。
展开
param: AstNode
节点
在节点后设置起点。
// 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);
展开
param: AstNode
节点
在节点后设置终点。
// 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);
展开
param: AstNode
节点
在节点前设置起点。
// 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);
展开
param: AstNode
节点
在节点前设置终点。
// 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);
展开
param: AstNode
节点
设置 Range 包含整个节点的内容。
// 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);
展开
param: AstNode
节点
设置 Range 包含整个节点。
// 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);
展开
param: boolean
重合至起始位置,默认行为是重合至终止位置
使起始和终止位置重合。
// 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);
展开
param: AstNode
端点容器
param: number
端点位置
returns: -1 | 0 | 1
比较端点和 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);
展开
param: AstNode
端点容器
param: number
端点位置
returns: boolean
端点是否在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));
展开
returns: AstRange
复制 AstRange 对象。
// cloneRange (main)
var root = Parser.parse('a'),
range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 1);
assert.deepStrictEqual(range.cloneRange(), range);
展开
清空 Range 对象。
// detach (main)
var root = Parser.parse('a'),
range = root.createRange();
range.setStart(root, 0);
range.setEnd(root, 1);
range.detach();
展开
删除 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);
展开
returns: {top: number, left: number, height: number, left: number}
获取行列位置和大小。
// 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,
});
展开
param: AstNode | string
插入的节点
在起始位置插入节点。
// 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]]');
展开
returns: AstNode[]
获取范围内的全部节点。
// 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'),
]);
展开
returns: AstNode[]
拷贝范围内的全部节点。
// 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