-
Notifications
You must be signed in to change notification settings - Fork 2
Token (EN)
Table of Contents
- Introduction
- Properties
-
Methods
- normalizeTitle
- safeReplaceWith
- createComment
- createElement
- createTextNode
- createRange
- caretPositionFromIndex
- caretPositionFromPoint
- elementFromIndex
- elementFromPoint
- elementsFromIndex
- elementsFromPoint
- isInterwiki
- cloneNode
- sections
- section
- findEnclosingHtml
- getCategories
- redoQuotes
- fontStyle
- solveConst
- flatten
This is the base class for all specific nodes corresponding to different wiki syntax. The parent class of the Token class, AstElement, is modeled after the HTMLElement class. This page only describes the unique properties and methods of the Token class.
✅ Available in the Mini and Browser versions.
✅ Expand
type: string
The type of the node.
// type
assert.strictEqual(Parser.parse('').type, 'root');
Expand
type: Token[]
All image nodes, read-only.
// images
var root = Parser.parse('[[file:a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[file:a]]');
assert.deepStrictEqual(root.images, [firstChild]);
Expand
type: Token[]
All internal links, external links, free external links and image links, read-only.
// links
var root = Parser.parse([[a]][//b]http://c[[file:d|link=e]]'),
{childNodes: [link, extLink, magicLink, {lastChild}]} = root;
assert.equal(link, '[[a]]');
assert.equal(extLink, '[//b]');
assert.equal(magicLink, 'http://c');
assert.equal(lastChild, 'link=e');
assert.deepStrictEqual(root.links, [
link,
extLink,
magicLink,
lastChild,
]);
Expand
type: Token[]
All embedded templates and modules, read-only.
// embeds
var root = Parser.parse('{{a}}{{#invoke:b|c}}'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '{{a}}');
assert.equal(lastChild, '{{#invoke:b|c}}');
assert.deepStrictEqual(root.embeds, [firstChild, lastChild]);
✅ Expand
param: string
Title (with or without namespace)
param: number
Namespace ID, default as 0
returns: Title
Similar to Parser.normalizeTitle
, but using the same parsing configurations as the current node.
Expand
param: this
New node
Some nodes with special syntax can only be replaced by nodes of the same type. Note that the use of this method generally requires the use of the AstElement.prototype.destroy
method.
// safeReplaceWith
var {firstChild, lastChild: {lastChild}} = Parser.parse('<p><p lang="zh">'),
attrs = lastChild.cloneNode();
assert.equal(firstChild, '<p>');
assert.equal(lastChild, ' lang="zh"');
assert.equal(attrs, ' lang="zh"');
try {
firstChild.lastChild.replaceWith(attrs);
} catch (e) {
assert(e instanceof Error);
assert.equal(e.message, 'HtmlToken 不可插入元素!');
}
firstChild.lastChild.safeReplaceWith(attrs);
Expand
param: string
Text content of the comment
returns: CommentToken
Create an HTML comment.
// createComment
var {firstChild} = Parser.parse('<!--a-->');
assert.equal(firstChild, '<!--a-->');
assert.deepStrictEqual(firstChild.createComment('a'), firstChild);
Expand
param: string
Tag name
param: {closing?: boolean, selfClosing?: boolean}
Options
returns: Token
Create an HTML tag or extension tag.
// createElement
var root = Parser.parse('</p><ref/>'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '</p>');
assert.equal(lastChild, '<ref/>');
assert.deepStrictEqual(
root.createElement('p', {closing: true}),
firstChild,
);
assert.deepStrictEqual(
root.createElement('ref', {selfClosing: true}),
lastChild,
);
Expand
param: string
returns: AstText
Create a text node.
// createTextNode
var root = Parser.parse('text');
assert.deepStrictEqual(root.createTextNode('text'), root.firstChild);
Expand
returns: AstRange
Create an AstRange
object.
// createRange
var root = Parser.parse('text'),
{firstChild} = root,
range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(firstChild, 3);
assert.strictEqual(String(range), 'ex');
Expand
param: number
Character position
returns: {offsetNode: AstNode, offset: number}
Find the given position.
// caretPositionFromIndex
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.deepStrictEqual(root.caretPositionFromIndex(1), {
offsetNode: firstChild,
offset: 1,
});
Expand
param: number
Character column
param: number
Character row
returns: {offsetNode: AstNode, offset: number}
Find the given position.
// caretPositionFromPoint
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.deepStrictEqual(root.caretPositionFromPoint(1, 0), {
offsetNode: firstChild,
offset: 1,
});
Expand
param: number
Character position
returns: AstNode
Find the outermost node at the given position.
// elementFromIndex
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(root.elementFromIndex(1), firstChild);
Expand
param: number
Character column
param: number
Character row
returns: AstNode
Find the outermost node at the given position.
// elementFromPoint
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(root.elementFromPoint(1, 0), firstChild);
Expand
param: number
Character position
returns: AstNode[]
Find all nodes at the given position.
// elementsFromIndex
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.deepStrictEqual(root.elementsFromIndex(1), [root, firstChild]);
Expand
param: number
Character column
param: number
Character row
returns: AstNode[]
Find all nodes at the given position.
// elementsFromPoint
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.deepStrictEqual(root.elementsFromPoint(1, 0), [root, firstChild]);
Expand
param: string
Title
returns: RegExpExecArray | null
Similar to Parser.isInterwiki
, but using the same parsing configurations as the current node.
Expand
returns: this
Deep clone the node. Different types of nodes use different algorithms.
Expand
returns: AstRange[]
Get all sections (including the prelude), each of which is an array. Only available for root nodes.
// sections
var root = Parser.parse('a\n==b==\nc'),
[s1, s2] = root.sections(),
{childNodes: [a, b, c]} = root;
assert.equal(a, 'a\n');
assert.equal(b, '==b==');
assert.equal(c, '\nc');
assert.deepStrictEqual(s1.startContainer, root);
assert.deepStrictEqual(s1.endContainer, root);
assert.equal(s1.startOffset, 0);
assert.equal(s1.endOffset, 1);
assert.deepStrictEqual(s2.startContainer, root);
assert.deepStrictEqual(s2.endContainer, root);
assert.equal(s2.startOffset, 1);
assert.equal(s2.endOffset, 3);
Expand
param: number
Number of the section
returns: AstRange
Get the specified section. Only available for root nodes.
// section
var root = Parser.parse('a\n==b==\nc'),
section = root.section(1),
{childNodes: [, b, c]} = root;
assert.equal(b, '==b==');
assert.equal(c, '\nc');
assert.deepStrictEqual(section.startContainer, root);
assert.deepStrictEqual(section.endContainer, root);
assert.equal(section.startOffset, 1);
assert.equal(section.endOffset, 3);
Expand
param: string
Tag name (optional)
returns: [HtmlToken, HtmlToken]
Get the specified outer HTML tag. Note that text nodes cannot use this method.
// findEnclosingHtml
var {childNodes: [start, link, end]} = Parser.parse('<p>[[a]]</p>'),
range = link.createRange();
assert.equal(start, '<p>');
assert.equal(link, '[[a]]');
assert.equal(end, '</p>');
range.setStartBefore(start);
range.setEndAfter(end);
assert.deepStrictEqual(link.findEnclosingHtml(), range);
Expand
returns: [string, string | undefined][]
Get all categories and their sort keys.
// getCategories
assert.deepStrictEqual(
Parser.parse([[category:a]][[category:b|c]]').getCategories(),
[
['Category:A', undefined],
['Category:B', 'c'],
],
);
Expand
Re-parse single quotes.
// redoQuotes
var root = Parser.parse("''[[a]]'''");
assert.deepStrictEqual(root.childNodes.map(String), [
"''",
'[[a]]',
"'",
"''",
]);
root.append("b''");
root.redoQuotes();
assert.deepStrictEqual(root.childNodes.map(String), [
"''",
'[[a]]',
"'''",
'b',
"''",
]);
Expand
Get the font style of the current node.
// fontStyle
var {childNodes: [, link]} = Parser.parse("''[[a]]'''");
assert.equal(link, '[[a]]');
assert.deepStrictEqual(link.fontStyle(), {
italic: true,
bold: false,
});
Expand
Parse template parameters and some magic words.
// solveConst
var root = Parser.parse('{{{|a}}}{{#if:||b}}{{#switch:|#default=c}}');
root.solveConst();
assert.equal(root, 'abc');
Expand
Merge the plain child nodes of the current plain node.
// flatten
var root = Parser.parse(''),
plain = Parser.parse('a'),
{firstChild} = plain;
root.append(plain);
root.flatten();
assert.strictEqual(root.lastChild, firstChild);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext