-
Notifications
You must be signed in to change notification settings - Fork 1
AstText (EN)
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.
🌐 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, read-only.
// length
var {firstChild} = Parser.parse('a');
assert.strictEqual(firstChild.length, 1);
✅ Expand
returns: LintError[]
Report potential grammar errors.
// lint
var {firstChild} = Parser.parse('{{[[<div');
assert.equal(firstChild, '{{[[<div');
assert.deepStrictEqual(firstChild.lint(), [
{
rule: 'lonely-bracket',
severity: 'error',
message: 'lonely "{"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 2,
endIndex: 2,
},
{
rule: 'lonely-bracket',
severity: 'error',
message: 'lonely "["',
startLine: 0,
startCol: 2,
startIndex: 2,
endLine: 0,
endCol: 4,
endIndex: 4,
},
{
rule: 'tag-like',
severity: 'warning',
message: 'lonely "<"',
startLine: 0,
startCol: 4,
startIndex: 4,
endLine: 0,
endCol: 8,
endIndex: 8,
suggestions: [
{
desc: 'escape',
range: [4, 5],
text: '<',
},
],
},
]);
✅ Expand
param: string
String to replace
Replace the string.
// replaceData
var {firstChild} = Parser.parse('a');
firstChild.replaceData('b');
assert.equal(firstChild, 'b');
🌐 Expand
Print the text node in HTML format.
// print
var {firstChild} = Parser.parse('&<>');
assert.equal(firstChild.print(), '&<>');
Expand
returns: this
Clone the node.
// cloneNode
var {firstChild} = Parser.parse('a');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);
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');
Expand
param: number
Start 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');
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');
Expand
param: number
Start 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');
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');
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', '{{=}}'],
);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext