Skip to content

AstNode (EN)

bhsd edited this page Dec 18, 2023 · 10 revisions
Table of Contents

Other Languages

Introduction

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.

Properties

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]);

firstChild

✅ Expand

type: AstNode
First child node, read-only.

lastChild

✅ Expand

type: AstNode
Last child node, read-only.

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);

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);

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);

offsetHeight

✅ Expand

type: number
Number of rows, read-only.

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

offsetWidth

✅ Expand

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

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

nextElementSibling

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);

previousElementSibling

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);

isConnected

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);

eof

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);

offsetTop

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);

offsetLeft

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);

style

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,
});

Methods

getRootNode

✅ Expand

returns: Token
Get the root node.

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

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.strictEqual(root.posFromIndex(4), undefined);

getRelativeIndex

✅ 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);

getAbsoluteIndex

✅ 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);

isEqualNode

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));

after

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');

before

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');

remove

Expand

Remove the current node.

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

replaceWith

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');

contains

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));

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.

removeEventListener

Expand

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

removeAllEventListener

Expand

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

listEventListeners

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

dispatchEvent

Expand

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

getAncestors

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]);

compareDocumentPosition

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);

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

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 {lastChild} = Parser.parse('a\n[[b]]');
assert.equal(lastChild, '[[b]]');
assert.deepStrictEqual(lastChild.getBoundingClientRect(), {
	top: 1,
	left: 0,
	height: 1,
	width: 5,
});
Clone this wiki locally