-
Notifications
You must be signed in to change notification settings - Fork 1
AstText (EN)
Table of Contents
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.
✅ 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');
✅ Expand
type: string
The text content of the node, read-only.
// data
var {firstChild} = Parser.parse('a');
assert.strictEqual(firstChild.data, 'a');
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, '');
✅ 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: '<',
},
],
},
{
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: '<',
},
],
},
]);
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: ']',
},
],
},
]);
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: ' ',
},
],
},
]);
🌐 Expand
Print the text node in HTML format.
// print
var {firstChild} = Parser.parse('&<>');
assert.equal(firstChild.print(), '&<>');
✅ Expand
param: string
String to replace
Replace the string.
// replaceData
var {firstChild} = Parser.parse('a');
firstChild.replaceData('b');
assert.equal(firstChild, 'b');
Expand
returns: this
Clone the node.
// cloneNode (main)
var {firstChild} = Parser.parse('a');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);
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');
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');
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');
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');
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');
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', '{{=}}'],
);
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><</b></i>\n>');
assert.strictEqual(lastChild.toHtml(true), '<i><b>< ></b></i>');
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext