Skip to content

AstText (EN)

bhsd edited this page Dec 18, 2023 · 19 revisions

Other Languages

Introduction

Text nodes that inherit the properties and methods of AstNode. Modeled after the Text class, the properties and methods are also very similar to the Text class.

✅ Available in the Mini and Browser versions.

Properties

type

✅ Expand

type: 'text'
Read-only. The text node type is always 'text'. Conversely, type: 'text' must correspond to a text node.

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

data

✅ Expand

type: string
The text content of the node, read-only.

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

length

Expand

type: number
The length of the text content, read-only.

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

Methods

lint

✅ Expand

returns: LintError[]

Report potential grammar errors.

// lint
var {firstChild} = Parser.parse('{{[[<div');
assert.equal(firstChild, '{{[[<div');
assert.deepStrictEqual(firstChild.lint(), [
	{
		severity: 'error',
		message: 'lonely "{"',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 2,
		endIndex: 2,
		excerpt: '{{[[<div',
	},
	{
		severity: 'error',
		message: 'lonely "["',
		startLine: 0,
		startCol: 2,
		startIndex: 2,
		endLine: 0,
		endCol: 4,
		endIndex: 4,
		excerpt: '{[[<div',
	},
	{
		severity: 'warning',
		message: 'lonely "<"',
		startLine: 0,
		startCol: 4,
		startIndex: 4,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
		excerpt: '[<div',
	},
])

replaceData

✅ Expand

param: string String to replace
Replace the string.

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

print

✅ Expand

Print the text node in HTML format.

// print
var {firstChild} = Parser.parse('&<>');
assert.equal(firstChild.print(), '&amp;&lt;&gt;');

cloneNode

Expand

returns: this
Clone the node.

// cloneNode
var {firstChild} = Parser.parse('a');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

appendData

Expand

param: string String to append
Append the string to the end.

// appendData
var {firstChild} = Parser.parse('a');
firstChild.appendData('b');
assert.equal(firstChild, 'ab');

deleteData

Expand

param: number Starting position
param: number Number of characters to delete
Delete part of the string.

// deleteData
var {firstChild} = Parser.parse('abc');
firstChild.deleteData(1, 1);
assert.equal(firstChild, 'ac');

insertData

Expand

param: number Position to insert
param: string String to insert
Insert a string.

// insertData
var {firstChild} = Parser.parse('ab');
firstChild.insertData(1, 'c');
assert.equal(firstChild, 'acb');

substringData

Expand

param: number Starting position
param: number Number of characters to extract
returns: string
Extract a substring.

// substringData
var {firstChild} = Parser.parse('abc');
assert.strictEqual(firstChild.substringData(1, 1), 'b');

splitText

Expand

param: number Position to split
Split the text node into two parts.

// splitText
var {firstChild} = Parser.parse('ab');
firstChild.splitText(1);
assert.equal(firstChild, 'a');
assert.equal(firstChild.nextSibling, 'b');

escape

Expand

version added: 1.1.4

Escape the = sign in the text node.

// escape
var root = Parser.parse('a=b=');
root.firstChild.escape();
assert.deepStrictEqual(root.childNodes.map(String), ['a', '{{=}}', 'b', '{{=}}']);
Clone this wiki locally