-
Notifications
You must be signed in to change notification settings - Fork 2
AstNode (EN)
Table of Contents
AstNode is the base class for all text nodes and other nodes, modeled after the Node class, with properties and methods very similar to the Node class.
✅ Available in the Mini and Browser versions.
✅ Expand
type: AstNode[]
An array of all child nodes, read-only.
// childNodes
var root = Parser.parse([[a]]b'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, 'b');
assert.deepStrictEqual(root.childNodes, [firstChild, lastChild]);
✅ Expand
type: AstNode
First child node, read-only.
✅ Expand
type: AstNode
Last child node, read-only.
✅ Expand
type: Token
Parent node, read-only.
// parentNode
var root = Parser.parse('a'),
{firstChild} = root;
assert.equal(firstChild, 'a');
assert.strictEqual(firstChild.parentNode, root);
✅ Expand
type: AstNode
Next sibling node, read-only.
// nextSibling
var {firstChild, lastChild} = Parser.parse([[a]]b');
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, 'b');
assert.strictEqual(firstChild.nextSibling, lastChild);
✅ Expand
type: AstNode
Previous sibling node, read-only.
// previousSibling
var {firstChild, lastChild} = Parser.parse('a[[b]]');
assert.equal(firstChild, 'a');
assert.equal(lastChild, '[[b]]');
assert.strictEqual(lastChild.previousSibling, firstChild);
✅ Expand
type: number
Number of rows, read-only.
// offsetHeight
var root = Parser.parse('a\nb');
assert.strictEqual(root.offsetHeight, 2);
✅ Expand
type: number
Number of columns in the last row, read-only.
// offsetWidth
var root = Parser.parse('a\nb');
assert.strictEqual(root.offsetWidth, 1);
Expand
type: Token
Next non-text sibling node, read-only.
// nextElementSibling
var {firstChild, lastChild} = Parser.parse([[a]]b{{c}}');
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, '{{c}}');
assert.strictEqual(firstChild.nextElementSibling, lastChild);
Expand
type: Token
Previous non-text sibling node, read-only.
// previousElementSibling
var {firstChild, lastChild} = Parser.parse([[a]]b{{c}}');
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, '{{c}}');
assert.strictEqual(lastChild.previousElementSibling, firstChild);
Expand
type: boolean
Whether the node has a root node, read-only.
// isConnected
var {firstChild} = Parser.parse('a');
assert(firstChild.isConnected);
firstChild.remove();
assert(!firstChild.isConnected);
Expand
type: boolean
Whether there are other nodes (excluding descendants) behind, read-only.
// eof
var {firstChild, lastChild} = Parser.parse('a[[b]]');
assert.equal(firstChild, 'a');
assert.equal(lastChild, '[[b]]');
assert(!firstChild.eof);
assert(lastChild.eof);
Expand
type: number
Row number relative to the parent container (starting from 0
), read-only.
// offsetTop
var {lastChild} = Parser.parse('a\n[[b]]');
assert.equal(lastChild, '[[b]]');
assert.strictEqual(lastChild.offsetTop, 1);
Expand
type: number
Column number relative to the parent container (starting from 0
), read-only.
// offsetLeft
var {lastChild} = Parser.parse('a[[b]]');
assert.equal(lastChild, '[[b]]');
assert.strictEqual(lastChild.offsetLeft, 1);
Expand
type: {top: number, left: number, height: number, width: number, padding: number}
Position, size and padding, read-only.
// style
var {lastChild} = Parser.parse('a\n[[b]]');
assert.equal(lastChild, '[[b]]');
assert.deepStrictEqual(lastChild.style, {
top: 1,
left: 0,
height: 1,
width: 5,
padding: 2,
});
✅ Expand
returns: Token
Get the root node.
// getRootNode
var root = Parser.parse('a'),
{firstChild} = root;
assert.strictEqual(firstChild.getRootNode(), root);
✅ Expand
param: number
Character position
returns: {top: number, left: number}
Converts the character position to row and column numbers.
// posFromIndex
var root = Parser.parse('a\nb');
assert.deepStrictEqual(root.posFromIndex(3), {top: 1, left: 1});
assert.strictEqual(root.posFromIndex(4), undefined);
✅ Expand
returns: number
Get the character position relative to the parent node.
// getRelativeIndex
var {lastChild: link} = Parser.parse('a[[b]]'),
{lastChild} = link;
assert.equal(link, '[[b]]');
assert.equal(lastChild, 'b');
assert.strictEqual(link.getRelativeIndex(), 1);
assert.strictEqual(lastChild.getRelativeIndex(), 2);
✅ Expand
returns: number
Get the absolute character position of the current node.
// getAbsoluteIndex
var {lastChild: {lastChild}} = Parser.parse('a[[b]]');
assert.equal(lastChild, 'b');
assert.strictEqual(lastChild.getAbsoluteIndex(), 3);
Expand
param: AstNode
Node to compare
returns: boolean
Whether the node is identical to the other node.
// isEqualNode
var a = Parser.parse('[[a]]'),
b = Parser.parse('[[a]]');
assert(a.isEqualNode(b));
Expand
param: AstNode | string
Nodes to insert
Inserts sibling nodes in the back.
// after
var root = Parser.parse('a'),
{firstChild} = root;
firstChild.after('b', 'c');
assert.equal(root, 'abc');
Expand
param: AstNode | string
Nodes to insert
Inserts sibling nodes in the front.
// before
var root = Parser.parse('a'),
{firstChild} = root;
firstChild.before('b', 'c');
assert.equal(root, 'bca');
Expand
Remove the current node.
// remove
var root = Parser.parse('a'),
{firstChild} = root;
firstChild.remove();
assert.equal(root, '');
Expand
param: AstNode | string
Nodes to insert
Replaces the current node with new nodes.
// replaceWith
var root = Parser.parse('a'),
{firstChild} = root;
firstChild.replaceWith('b', 'c');
assert.equal(root, 'bc');
Expand
param: AstNode
Node to compare
Whether the node is the same as or an ancestor of the other node.
// contains
var root = Parser.parse('a'),
{firstChild} = root;
assert(root.contains(root));
assert(root.contains(firstChild));
assert(!firstChild.contains(root));
Expand
param: string | string[]
type of event
param: Function
listener
param: {once?: boolean}
options
Add event listeners. Used in conjunction with the dispatchEvent
method.
Expand
param: string | string[]
type of event
param: Function
listener
Remove event listeners.
Expand
param: string | string[]
type of event
Remove all listeners for the event.
Expand
param: string
type of event
List event listeners.
// listEventListeners
var {firstChild} = Parser.parse('[[a]]'),
listeners = firstChild.listEventListeners('remove');
assert.strictEqual(listeners.length, 1);
assert.strictEqual(listeners[0].name, 'linkListener'); // predefined listener in the Parser
Expand
param: Event
event object
param: any
event data
Trigger an event.
Expand
returns: Token[]
Get all ancestor nodes (from bottom to top).
// getAncestors
var root = Parser.parse('[[a]]'),
{firstChild: link} = root,
{firstChild} = link;
assert.equal(link, '[[a]]');
assert.equal(firstChild, 'a');
assert.deepStrictEqual(firstChild.getAncestors(), [link, root]);
Expand
param: AstNode
returns: number
Compare the position of the current node with another node.
// compareDocumentPosition
var root = Parser.parse([[a]]b'),
{firstChild: {firstChild}, lastChild} = root;
assert.equal(firstChild, 'a');
assert.equal(lastChild, 'b');
assert.strictEqual(root.compareDocumentPosition(root), 0);
assert(root.compareDocumentPosition(firstChild) < 0);
assert(firstChild.compareDocumentPosition(root) > 0);
assert(firstChild.compareDocumentPosition(lastChild) < 0);
assert(lastChild.compareDocumentPosition(firstChild) > 0);
Expand
param: number
row number
param: number
column number
returns: number
Converts row and column numbers to character position.
// indexFromPos
var root = Parser.parse('a\nb');
assert.strictEqual(root.indexFromPos(1, 1), 3);
assert.strictEqual(root.indexFromPos(2, 0), undefined);
Expand
returns: {top: number, left: number, height: number, width: number}
Get the row and column position and size of the current node.
// getBoundingClientRect
var {lastChild} = Parser.parse('a\n[[b]]');
assert.equal(lastChild, '[[b]]');
assert.deepStrictEqual(lastChild.getBoundingClientRect(), {
top: 1,
left: 0,
height: 1,
width: 5,
});
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext