AstNode (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Table of Contents

Other Languages

Introduction

AstNode is the parent class of 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.

Properties

bold

Expand

version added: 1.8.0

type: boolean
Whether it is bold.

// bold (main)
var {lastChild} = Parser.parse("'''a\nc[[d]]"),
	{previousSibling} = lastChild,
	firstChild;
assert.equal(lastChild, '[[d]]');
assert.equal(previousSibling, 'a\nc');
assert.ok(!lastChild.bold);
assert.ok(previousSibling.bold);
({lastChild} = Parser.parse("<b>'''<b></b></b>x"));
assert.equal(lastChild, 'x');
assert.ok(!lastChild.bold);
({lastChild} = Parser.parse("'''[//a b'''c]d"));
({previousSibling: {lastChild: {firstChild}}} = lastChild);
assert.equal(lastChild, 'd');
assert.equal(firstChild, 'b');
assert.ok(!lastChild.bold);
assert.ok(firstChild.bold);

childNodes

✅ 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]);
assert.deepStrictEqual(lastChild.childNodes, []);

eof

Expand

type: boolean
Whether there are other nodes (excluding descendants) behind, read-only.

// eof (main)
var root = Parser.parse('a[[b]]\n'),
	{firstChild, firstElementChild} = root,
	{lastChild} = firstElementChild;
assert.equal(firstChild, 'a');
assert.equal(firstElementChild, '[[b]]');
assert.equal(lastChild, 'b');
assert.ok(root.eof);
assert.ok(!firstChild.eof);
assert.ok(firstElementChild.eof); // trailing whitespace is ignored
assert.ok(lastChild.eof);

firstChild

✅ Expand

type: AstNode
First child node, read-only.

// firstChild
var root = Parser.parse('a'),
	{firstChild} = root;
assert.equal(firstChild, 'a');
assert.strictEqual(firstChild.firstChild, undefined);

font

Expand

version added: 1.8.0

type: {bold: boolean, italic: boolean}
Font style.

// font (main)
var {lastChild} = Parser.parse("'''''a'''{{b}}\nc[[d]]"),
	{previousSibling, previousElementSibling: {firstChild}} = lastChild;
assert.equal(lastChild, '[[d]]');
assert.equal(previousSibling, '\nc');
assert.equal(firstChild, 'b');
assert.deepStrictEqual(lastChild.font, {
	italic: false,
	bold: false,
});
assert.deepStrictEqual(previousSibling.font, {
	italic: true,
	bold: false,
});
assert.deepStrictEqual(firstChild.font, {
	italic: false,
	bold: false,
});
({lastChild} = Parser.parse("<i>'''''<b></b></i>x"));
assert.equal(lastChild, 'x');
assert.deepStrictEqual(lastChild.font, {
	italic: false,
	bold: true,
});
({lastChild} = Parser.parse("'''''[//a b''c]d"));
({previousSibling: {lastChild: {firstChild}}} = lastChild);
assert.equal(lastChild, 'd');
assert.equal(firstChild, 'b');
assert.deepStrictEqual(lastChild.font, {
	italic: false,
	bold: true,
});
assert.deepStrictEqual(firstChild.font, {
	italic: true,
	bold: true,
});

isConnected

Expand

type: boolean
Whether the node has a root node, read-only.

// isConnected (main)
var root = Parser.parse('a'),
	{firstChild} = root;
assert.ok(root.isConnected);
assert.ok(firstChild.isConnected);
firstChild.remove();
assert.ok(!firstChild.isConnected);
root.append(firstChild);
assert.ok(firstChild.isConnected);

italic

Expand

version added: 1.8.0

type: boolean
Whether it is italic.

// italic (main)
var {lastChild} = Parser.parse("''a\nc[[d]]"),
	{previousSibling} = lastChild,
	firstChild;
assert.equal(lastChild, '[[d]]');
assert.equal(previousSibling, 'a\nc');
assert.ok(!lastChild.italic);
assert.ok(previousSibling.italic);
({lastChild} = Parser.parse("<i>''<i></i></i>x"));
assert.equal(lastChild, 'x');
assert.ok(!lastChild.italic);
({lastChild} = Parser.parse("''[//a b''c]d"));
({previousSibling: {lastChild: {firstChild}}} = lastChild);
assert.equal(lastChild, 'd');
assert.equal(firstChild, 'b');
assert.ok(!lastChild.italic);
assert.ok(firstChild.italic);

lastChild

✅ Expand

type: AstNode
Last child node, read-only.

// lastChild
var root = Parser.parse('a'),
	{lastChild} = root;
assert.equal(lastChild, 'a');
assert.strictEqual(lastChild.lastChild, undefined);

nextElementSibling

Expand

type: Token
Next non-text sibling node, read-only.

// nextElementSibling (main)
var {childNodes: [a, b, c]} = Parser.parse([[a]]b{{c}}');
assert.equal(a, '[[a]]');
assert.equal(b, 'b');
assert.equal(c, '{{c}}');
assert.strictEqual(a.nextElementSibling, c);
assert.strictEqual(b.nextElementSibling, c);
assert.strictEqual(c.nextElementSibling, undefined);

nextSibling

✅ 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);
assert.strictEqual(lastChild.nextSibling, undefined);

nextVisibleSibling

Expand

type: AstNode
Next visible sibling node, read-only.

// nextVisibleSibling (main)
var {firstChild, lastChild} = Parser.parse([[a]]<!--b--><noinclude>c');
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, 'c');
assert.strictEqual(firstChild.nextVisibleSibling, lastChild);

offsetHeight

✅ Expand

type: number
Number of rows, read-only.

// offsetHeight
var root = Parser.parse('a\nb');
assert.strictEqual(root.offsetHeight, 2);

offsetLeft

Expand

type: number
Column number relative to the parent container (starting from 0), read-only.

// offsetLeft (main)
var root = Parser.parse([[a]]\nb'),
	{firstChild, lastChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(root.offsetLeft, 0);
assert.strictEqual(firstChild.offsetLeft, 0);
assert.strictEqual(lastChild.offsetLeft, 5);

offsetTop

Expand

type: number
Row number relative to the parent container (starting from 0), read-only.

// offsetTop (main)
var root = Parser.parse('a\n[[b]]'),
	{firstChild, lastChild} = root;
assert.equal(lastChild, '[[b]]');
assert.strictEqual(root.offsetTop, 0);
assert.strictEqual(firstChild.offsetTop, 0);
assert.strictEqual(lastChild.offsetTop, 1);

offsetWidth

✅ Expand

type: number
Number of columns in the last row, read-only.

// offsetWidth
var root = Parser.parse('ab\nc');
assert.strictEqual(root.offsetWidth, 1);

parentNode

✅ Expand

type: Token
Parent node, read-only.

// parentNode
var root = Parser.parse('a'),
	{firstChild} = root;
assert.equal(firstChild, 'a');
assert.strictEqual(firstChild.parentNode, root);
assert.strictEqual(root.parentNode, undefined);

previousElementSibling

Expand

type: Token
Previous non-text sibling node, read-only.

// previousElementSibling (main)
var {childNodes: [a, b, c]} = Parser.parse([[a]]b{{c}}');
assert.equal(a, '[[a]]');
assert.equal(b, 'b');
assert.equal(c, '{{c}}');
assert.strictEqual(c.previousElementSibling, a);
assert.strictEqual(b.previousElementSibling, a);
assert.strictEqual(a.previousElementSibling, undefined);

previousSibling

✅ 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);
assert.strictEqual(firstChild.previousSibling, undefined);

previousVisibleSibling

Expand

type: AstNode
Previous visible sibling node, read-only.

// previousVisibleSibling (main)
var {firstChild, lastChild} = Parser.parse('a<!--b--><noinclude>{{c}}');
assert.equal(firstChild, 'a');
assert.equal(lastChild, '{{c}}');
assert.strictEqual(lastChild.previousVisibleSibling, firstChild);

style

Expand

type: {top: number, left: number, height: number, width: number, padding: number}
Position, size and padding, read-only.

// style (main)
var root = Parser.parse('a\n[[b]]'),
	{firstChild, lastChild} = root;
assert.equal(lastChild, '[[b]]');
assert.deepStrictEqual(root.style, {
	top: 0,
	left: 0,
	height: 2,
	width: 5,
	padding: 0,
});
assert.deepStrictEqual(firstChild.style, {
	top: 0,
	left: 0,
	height: 2,
	width: 0,
	padding: 0,
});
assert.deepStrictEqual(lastChild.style, {
	top: 1,
	left: 0,
	height: 1,
	width: 5,
	padding: 2, // `[[`
});

Methods

addEventListener

Expand

param: string | string[] type of event
param: Function listener
param: {once?: boolean} options
Add event listeners. Used in conjunction with the dispatchEvent method.

// addEventListener (main)
var root = Parser.parse(''),
	counter = 0;
var listener = (_, data) => {
	counter += data;
};
root.addEventListener('x', listener);
root.addEventListener('y', listener, {once: true});
root.dispatchEvent(new Event('x'), 1);
assert.strictEqual(counter, 1);
root.dispatchEvent(new Event('x'), 2);
assert.strictEqual(counter, 3);
root.dispatchEvent(new Event('y'), 3);
assert.strictEqual(counter, 6);
root.dispatchEvent(new Event('y'), 4);
assert.strictEqual(counter, 6);

after

Expand

param: AstNode | string Nodes to insert
Inserts sibling nodes in the back.

// after (main)
var root = Parser.parse('a'),
	{firstChild} = root;
firstChild.after('b', 'c');
assert.equal(root, 'abc');

before

Expand

param: AstNode | string Nodes to insert
Inserts sibling nodes in the front.

// before (main)
var root = Parser.parse('a'),
	{firstChild} = root;
firstChild.before('b', 'c');
assert.equal(root, 'bca');

compareDocumentPosition

Expand

param: AstNode
returns: number
Compare the position of the current node with another node.

// compareDocumentPosition (main)
var root = Parser.parse([[a]]b'),
	{firstElementChild, lastChild} = root,
	{firstChild} = firstElementChild;
assert.equal(firstElementChild, '[[a]]');
assert.equal(firstChild, 'a');
assert.strictEqual(root.compareDocumentPosition(root), 0);
assert.ok(root.compareDocumentPosition(firstChild) < 0);
assert.ok(firstChild.compareDocumentPosition(root) > 0);
assert.ok(firstElementChild.compareDocumentPosition(lastChild) < 0);
assert.ok(lastChild.compareDocumentPosition(firstElementChild) > 0);
assert.ok(firstChild.compareDocumentPosition(lastChild) < 0);
assert.ok(lastChild.compareDocumentPosition(firstChild) > 0);

contains

Expand

param: AstNode Node to compare
Whether the node is the same as or an ancestor of the other node.

// contains (main)
var root = Parser.parse('a'),
	{firstChild} = root;
assert.ok(root.contains(root));
assert.ok(root.contains(firstChild));
assert.ok(!firstChild.contains(root));

destroy

Expand

Destroy the node. Will destroy all ancestor nodes in a chain, so please use the remove method to detach from the parent node before using it.

// destroy (main)
var root = Parser.parse('[[a]]'),
	{firstChild} = root;
root.destroy();
assert.strictEqual(firstChild.parentNode, undefined);
assert.strictEqual(root.firstChild, undefined);

dispatchEvent

Expand

param: Event event object
param: any event data
Trigger an event.

// dispatchEvent (main)
var root = Parser.parse('a'),
	{firstChild} = root,
	record = '';
var listener = ({target, currentTarget, prevTarget}) => {
	record += `${target.type} ${currentTarget.type} ${prevTarget?.type}\n`;
};
root.addEventListener('x', listener);
firstChild.addEventListener('x', listener, {once: true});
firstChild.dispatchEvent(new Event('x'));
assert.strictEqual(record, 'text text undefined\n');
firstChild.dispatchEvent(new Event('x', {bubbles: true}));
assert.strictEqual(record, 'text text undefined\ntext root text\n');
firstChild.dispatchEvent(new Event('x', {bubbles: true}));
assert.strictEqual(
	record,
	'text text undefined\ntext root text\ntext root text\n',
);

getAbsoluteIndex

✅ Expand

returns: number
Get the absolute character position of the current node.

// getAbsoluteIndex
var root = Parser.parse('a[[b]]'),
	{lastChild} = root,
	{firstChild} = lastChild;
assert.equal(lastChild, '[[b]]');
assert.equal(firstChild, 'b');
assert.strictEqual(root.getAbsoluteIndex(), 0);
assert.strictEqual(lastChild.getAbsoluteIndex(), 1);
assert.strictEqual(firstChild.getAbsoluteIndex(), 3);

getAncestors

Expand

returns: Token[]
Get all ancestor nodes (from bottom to top).

// getAncestors (main)
var root = Parser.parse('[[a]]'),
	{firstChild} = root,
	{lastChild} = firstChild;
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, 'a');
assert.deepStrictEqual(root.getAncestors(), []);
assert.deepStrictEqual(firstChild.getAncestors(), [root]);
assert.deepStrictEqual(lastChild.getAncestors(), [firstChild, root]);

getBoundingClientRect

✅ Expand

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

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

getLine

Expand

param: number Line number
returns: string
Extract the source text of a line.

// getLine (main)
assert.strictEqual(Parser.parse('a\nb').getLine(0), 'a');
assert.strictEqual(Parser.parse('a\nb').getLine(2), undefined);

getLines

✅ Expand

version added: 1.16.3

returns: [string, number, number]
Extract the source text, the start and end positions of each line.

// getLines
assert.deepStrictEqual(
	Parser.parse('a\nb').getLines(),
	[
		['a', 0, 1],
		['b', 2, 3],
	],
);

getRelativeIndex

✅ Expand

param: number the index of the child node, optional
returns: number
Get the character position of a child node (or itself) relative to the parent node.

// getRelativeIndex
var root = Parser.parse('a[[b]]'),
	{lastChild} = root,
	{firstChild} = lastChild;
assert.equal(lastChild, '[[b]]');
assert.equal(firstChild, 'b');
assert.strictEqual(root.getRelativeIndex(), 0);
assert.strictEqual(root.getRelativeIndex(0), 0);
assert.strictEqual(root.getRelativeIndex(1), 1);
assert.strictEqual(root.getRelativeIndex(2), 6);
assert.strictEqual(root.getRelativeIndex(-1), 1);
assert.strictEqual(root.getRelativeIndex(-2), 0);
assert.strictEqual(lastChild.getRelativeIndex(), 1);
assert.strictEqual(lastChild.getRelativeIndex(0), 2);
// 插入新的子节点必须使用`|`作为分隔符
assert.strictEqual(lastChild.getRelativeIndex(1), 4);
assert.strictEqual(firstChild.getRelativeIndex(), 2);
assert.strictEqual(firstChild.getRelativeIndex(0), 0);

getRootNode

✅ Expand

returns: Token
Get the root node.

// getRootNode
var root = Parser.parse('[[a]]'),
	{firstChild: {firstChild}} = root;
assert.equal(firstChild, 'a');
assert.strictEqual(firstChild.getRootNode(), root);
assert.strictEqual(root.getRootNode(), root);

indexFromPos

✅ 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(0, 0), 0);
assert.strictEqual(root.indexFromPos(0, 1), 1);
assert.strictEqual(root.indexFromPos(1, 0), 2);
assert.strictEqual(root.indexFromPos(1, 1), 3);
assert.strictEqual(root.indexFromPos(2, 0), undefined);
assert.strictEqual(root.indexFromPos(-1, 0), undefined);
assert.strictEqual(root.indexFromPos(0, -1), undefined);
assert.strictEqual(root.indexFromPos(0, 2), undefined);

is

✅ Expand

version added: 1.10.0

param: string Node type
returns: boolean
Check if the node is of a certain type.

// is
var root = Parser.parse('a');
assert.ok(root.is('root'));
assert.ok(!root.firstChild.is('link'));

isEqualNode

Expand

param: AstNode Node to compare
returns: boolean
Whether the node is identical to the other node.

// isEqualNode (main)
var a = Parser.parse('[[a]]'),
	b = Parser.parse('[[a]]').firstChild,
	c = Parser.parse('[[ a]]').firstChild,
	d = a.firstChild,
	e = Parser.parse('<!--[[a]]-->').firstChild.firstChild,
	f = Parser.parse('[[a]]', true).firstChild;
assert.equal(d, '[[a]]');
assert.equal(e, '[[a]]');
assert.ok(!d.isEqualNode(a));
assert.ok(d.isEqualNode(b));
assert.ok(!d.isEqualNode(c));
assert.ok(!d.isEqualNode(e));
assert.ok(d.isEqualNode(f));

listEventListeners

Expand

param: string type of event
List event listeners.

// listEventListeners (main)
var {firstChild} = Parser.parse('[[a]]'),
	listeners = firstChild.listEventListeners('remove'),
	f = () => {};
assert.strictEqual(listeners.length, 1);
// predefined listener in the Parser
assert.strictEqual(listeners[0].name, 'linkListener');
firstChild.addEventListener('remove', f);
listeners = firstChild.listEventListeners('remove');
assert.strictEqual(listeners.length, 2);
assert.strictEqual(listeners[1], f);

posFromIndex

✅ 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.deepStrictEqual(root.posFromIndex(-1), {top: 1, left: 0});
assert.strictEqual(root.posFromIndex(4), undefined);

remove

Expand

param: boolean whether to remove the current line if it is empty, optional
Remove the current node.

// remove (main)
var root = Parser.parse('x\n[[a|b]]\n\ny'),
	{firstElementChild} = root,
	{lastChild} = firstElementChild;
assert.equal(firstElementChild, '[[a|b]]');
assert.equal(lastChild, 'b');
lastChild.remove();
assert.equal(firstElementChild, '[[a]]');
firstElementChild.remove(true);
assert.equal(root, 'x\n\ny');

removeAllEventListeners

Expand

param: string | string[] type of event
Remove all listeners for the event.

// removeAllEventListeners (main)
var root = Parser.parse(''),
	counter = 0;
var f = () => {
	counter++;
};
var g = () => {
	counter += 2;
};
root.addEventListener('x', f);
root.addEventListener('x', g);
root.addEventListener('y', f);
root.addEventListener('z', g);
root.dispatchEvent(new Event('x'));
assert.strictEqual(counter, 3);
root.dispatchEvent(new Event('y'));
assert.strictEqual(counter, 4);
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 6);
root.removeAllEventListeners('x');
root.dispatchEvent(new Event('x'));
assert.strictEqual(counter, 6);
root.dispatchEvent(new Event('y'));
assert.strictEqual(counter, 7);
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 9);
root.removeAllEventListeners(['y', 'z']);
root.dispatchEvent(new Event('y'));
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 9);

removeEventListener

Expand

param: string | string[] type of event
param: Function listener
Remove event listeners.

// removeEventListener (main)
var root = Parser.parse(''),
	counter = 0;
var listener = () => {
	counter++;
};
root.addEventListener('x', listener);
root.addEventListener('y', listener);
root.addEventListener('z', listener);
root.dispatchEvent(new Event('x'));
assert.strictEqual(counter, 1);
root.dispatchEvent(new Event('y'));
assert.strictEqual(counter, 2);
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 3);
root.removeEventListener('x', listener);
root.dispatchEvent(new Event('x'));
assert.strictEqual(counter, 3);
root.dispatchEvent(new Event('y'));
assert.strictEqual(counter, 4);
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 5);
root.removeEventListener(['y', 'z'], listener);
root.dispatchEvent(new Event('y'));
root.dispatchEvent(new Event('z'));
assert.strictEqual(counter, 5);

replaceWith

Expand

param: AstNode | string Nodes to insert
Replaces the current node with new nodes.

// replaceWith (main)
var root = Parser.parse('a'),
	{firstChild} = root;
firstChild.replaceWith('b', 'c');
assert.equal(root, 'bc');
⚠️ **GitHub.com Fallback** ⚠️