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

Table of Contents

Other Languages

Introduction

Text nodes modeled after the Text class, with properties and methods very similar to the Text class. This class 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

Inherited properties from AstNode

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('ab');
assert.strictEqual(firstChild.length, 2);
firstChild.length = 1;
assert.equal(firstChild, 'a');

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

Methods

Inherited methods from AstNode

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

cloneNode

Expand

returns: this
Clone the node.

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

deleteData

Expand

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

// deleteData (main)
var {firstChild} = Parser.parse('abcd');
firstChild.deleteData(-2, 1);
assert.equal(firstChild, 'abd');
firstChild.deleteData(2);
assert.equal(firstChild, 'ab');
firstChild.deleteData(-1, 2);
assert.equal(firstChild, 'a');

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', '{{=}}', '{{!}}'],
);

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');
firstChild.insertData(0, 'd');
assert.equal(firstChild, 'dacb');

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var root, lastChild, linkText, attrValue;

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

assert.deepStrictEqual(Parser.parse('-{').firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "-{"',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 2,
		endIndex: 2,
	},
]);
assert.deepStrictEqual(Parser.parse('}-').firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "}-"',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 2,
		endIndex: 2,
	},
]);
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: 'warning',
		message: 'lonely "{"',
		startLine: 0,
		startCol: 3,
		startIndex: 3,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "}"',
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 7,
		endIndex: 7,
	},
]);
assert.deepStrictEqual(Parser.parse('{{a}}}').lastChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "}"',
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 6,
		endIndex: 6,
	},
]);

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: 'warning',
		message: 'lonely "["',
		startLine: 0,
		startCol: 3,
		startIndex: 3,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		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: 'warning',
		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: 'warning',
		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,
		suggestions: [
			{
				range: [0, 0],
				text: '[',
				desc: 'opening bracket',
			},
		],
	},
]);

assert.deepStrictEqual(Parser.parse('中HTTP://a').firstChild.lint(), [
	{
		rule: 'lonely-http',
		severity: 'warning',
		message: 'lonely "http://"',
		startLine: 0,
		startCol: 1,
		startIndex: 1,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
		suggestions: [
			{
				desc: 'whitespace',
				range: [1, 1],
				text: ' ',
			},
		],
	},
]);
assert.deepStrictEqual(
	Parser.parse('pmid: 1\nRFC:1\nisbn 123456').firstChild.lint(),
	[
		{
			rule: 'lonely-http',
			severity: 'warning',
			message: 'lonely "PMID"',
			startLine: 0,
			startCol: 0,
			startIndex: 0,
			endLine: 0,
			endCol: 4,
			endIndex: 4,
			suggestions: [
				{
					desc: 'uppercase',
					range: [0, 4],
					text: 'PMID',
				},
				{
					desc: 'whitespace',
					range: [4, 5],
					text: ' ',
				},
			],
		},
		{
			rule: 'lonely-http',
			severity: 'warning',
			message: 'lonely "RFC"',
			startLine: 1,
			startCol: 0,
			startIndex: 8,
			endLine: 1,
			endCol: 3,
			endIndex: 11,
			suggestions: [
				{
					desc: 'whitespace',
					range: [11, 12],
					text: ' ',
				},
			],
		},
		{
			rule: 'lonely-http',
			severity: 'warning',
			message: 'lonely "ISBN"',
			startLine: 2,
			startCol: 0,
			startIndex: 14,
			endLine: 2,
			endCol: 4,
			endIndex: 18,
			suggestions: [
				{
					desc: 'uppercase',
					range: [14, 18],
					text: 'ISBN',
				},
			],
		},
	],
);

root = Parser.parse('<p title="[[A]]http://">');
attrValue = root.querySelector('attr-value');
assert.equal(attrValue, [[A]]http://');
assert.deepStrictEqual(attrValue.firstChild.lint(), [
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "["',
		startLine: 0,
		startCol: 10,
		startIndex: 10,
		endLine: 0,
		endCol: 12,
		endIndex: 12,
	},
	{
		rule: 'lonely-bracket',
		severity: 'warning',
		message: 'lonely "]"',
		startLine: 0,
		startCol: 13,
		startIndex: 13,
		endLine: 0,
		endCol: 15,
		endIndex: 15,
	},
]);

root = Parser.parse('<references group="[[a]]" />');
attrValue = root.querySelector('attr-value');
assert.equal(attrValue, '[[a]]');
assert.deepStrictEqual(attrValue.firstChild.lint(), []);

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

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

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');
assert.strictEqual(firstChild.substringData(1), 'bc');

toHtml

Expand

version added: 1.10.0

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

// toHtml (main)
var {firstChild} = Parser.parse('<\n>');
assert.strictEqual(firstChild.toHtml(), '&lt;\n&gt;');
assert.strictEqual(firstChild.toHtml(true), '&lt; &gt;');
⚠️ **GitHub.com Fallback** ⚠️