Skip to content

AstText (EN)

bhsd edited this page Jul 23, 2024 · 19 revisions
Table of Contents

Other Languages

Introduction

Text nodes modeled after the Text class, with properties and methods very similar to the Text class. AstText inherits all the properties and methods of the AstNode class which are not repeated here.

✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.

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.

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

Methods

lint

✅ Expand

returns: LintError[]

Report potential grammar errors.

// lint
var root, lastChild, linkText;

assert.deepStrictEqual(Parser.parse('<a>\n0<b<c').firstChild.lint(), [
	{
		rule: 'tag-like',
		severity: 'error',
		message: 'lonely "<"',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 2,
		endIndex: 2,
		suggestions: [
			{
				desc: 'escape',
				range: [0, 1],
				text: '&lt;',
			},
		],
	},
	{
		rule: 'tag-like',
		severity: 'warning',
		message: 'lonely "<"',
		startLine: 1,
		startCol: 1,
		startIndex: 5,
		endLine: 1,
		endCol: 3,
		endIndex: 7,
		suggestions: [
			{
				desc: 'escape',
				range: [5, 6],
				text: '&lt;',
			},
		],
	},
]);

assert.deepStrictEqual(Parser.parse('{ {{}-').firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "{"',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 1,
		endIndex: 1,
	},
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "{"',
		startLine: 0,
		startCol: 2,
		startIndex: 2,
		endLine: 0,
		endCol: 4,
		endIndex: 4,
	},
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "}"',
		startLine: 0,
		startCol: 4,
		startIndex: 4,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
]);

assert.deepStrictEqual(Parser.parse(' ]] ][[').firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "]"',
		startLine: 0,
		startCol: 1,
		startIndex: 1,
		endLine: 0,
		endCol: 3,
		endIndex: 3,
	},
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "]"',
		startLine: 0,
		startCol: 4,
		startIndex: 4,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "["',
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 7,
		endIndex: 7,
	},
]);

root = Parser.parse('[//a []]');
linkText = root.querySelector('ext-link-text');
({lastChild} = root);
assert.equal(linkText, '[');
assert.equal(lastChild, ']');
assert.deepStrictEqual(linkText.firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "["',
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 6,
		endIndex: 6,
		suggestions: [
			{
				desc: 'escape',
				range: [6, 7],
				text: '&#93;',
			},
		],
	},
]);
assert.deepStrictEqual(lastChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "]"',
		startLine: 0,
		startCol: 7,
		startIndex: 7,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
	},
]);

assert.deepStrictEqual(Parser.parse('[ftp://a').firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "["',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 1,
		endIndex: 1,
	},
]);

assert.deepStrictEqual(Parser.parse('ftp://a]').lastChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'error',
		message: 'lonely "]"',
		startLine: 0,
		startCol: 7,
		startIndex: 7,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
		fix: {
			range: [0, 0],
			text: '[',
		},
	},
]);

assert.deepStrictEqual(Parser.parse('中http://a').firstChild.lint(), [
	{
		rule: 'lonely-http',
		severity: 'error',
		message: 'lonely "http://"',
		startLine: 0,
		startCol: 1,
		startIndex: 1,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
		suggestions: [
			{
				desc: 'whitespace',
				range: [1, 1],
				text: ' ',
			},
		],
	},
]);

print

🌐 Expand

Print the text node in HTML format.

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

replaceData

✅ Expand

param: string String to replace
Replace the string.

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

cloneNode

Expand

returns: this
Clone the node.

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

appendData

Expand

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

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

deleteData

Expand

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

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

insertData

Expand

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

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

substringData

Expand

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

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

splitText

Expand

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

// splitText (main)
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 (main)
var root = Parser.parse('a=b=');
root.firstChild.escape();
assert.deepStrictEqual(
	root.childNodes.map(String),
	['a', '{{=}}', 'b', '{{=}}'],
);

toHtml

Expand

version added: 1.10.0

param: boolean Whether to disable line breaks
returns: string
Convert to HTML.

// toHtml (main)
var {lastChild} = Parser.parse("'''''<\n>");
assert.equal(lastChild, '<\n>');
assert.strictEqual(lastChild.toHtml(), '<i><b>&lt;</b></i>\n&gt;');
assert.strictEqual(lastChild.toHtml(true), '<i><b>&lt; &gt;</b></i>');
Clone this wiki locally