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

Table of Contents

Other Languages

Introduction

This is the parent class of 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.

Properties

Inherited properties from AstElement

Inherited properties from AstNode

pageName

✅ Expand

version added: 1.29.0

type: string | undefined
The page name associated with this token. It is undefined if no page name is set.

// pageName
var root = Parser.parse('');
assert.strictEqual(root.pageName, undefined);
root.pageName = 'help:wiki parser#fragment';
assert.strictEqual(root.pageName, 'Help:Wiki_parser');
root.pageName = '|';
assert.strictEqual(root.pageName, undefined);
root.pageName = '';
assert.strictEqual(root.pageName, '');
root.pageName = undefined;
assert.strictEqual(root.pageName, undefined);

type

✅ Expand

type: string
The type of the node.

// type
var root = Parser.parse('');
assert.strictEqual(root.type, 'root');
root.type = 'plain';
assert.strictEqual(root.type, 'plain');

Methods

Inherited methods from AstElement

Inherited methods from AstNode

buildLists

Expand

version added: 1.17.1

Build all list-range tokens.

// buildLists (main)
var root = Parser.parse(';*#;a:b:c');
root.buildLists();
assert.deepStrictEqual(root.json(), {
	range: [0, 9],
	type: 'root',
	childNodes: [
		{
			range: [0, 3],
			type: 'list',
			childNodes: [
				{
					range: [0, 3],
					data: ';*#',
				},
			],
		},
		{
			range: [3, 7],
			type: 'list-range',
			childNodes: [
				{
					range: [3, 4],
					type: 'list',
					childNodes: [
						{
							range: [3, 4],
							data: ';',
						},
					],
				},
				{
					range: [4, 5],
					type: 'list-range',
					childNodes: [
						{
							range: [4, 5],
							data: 'a',
						},
					],
				},
				{
					range: [5, 6],
					type: 'dd',
					indent: 1,
					childNodes: [
						{
							range: [5, 6],
							data: ':',
						},
					],
				},
				{
					range: [6, 7],
					type: 'list-range',
					childNodes: [
						{
							range: [6, 7],
							data: 'b',
						},
					],
				},
			],
		},
		{
			range: [7, 8],
			type: 'dd',
			indent: 1,
			childNodes: [
				{
					range: [7, 8],
					data: ':',
				},
			],
		},
		{
			range: [8, 9],
			type: 'list-range',
			childNodes: [
				{
					range: [8, 9],
					data: 'c',
				},
			],
		},
	],
});

cloneNode

Expand

returns: this
Deep clone the node. Different types of nodes use different algorithms.

// cloneNode (main)
var token = Parser.parse('<ref>a[[b]]</ref>').querySelector('ext-inner'),
	cloned;
token.pageName = 'A';
cloned = token.cloneNode();
assert.strictEqual(cloned.type, token.type);
assert.strictEqual(cloned.name, token.name);
assert.strictEqual(cloned.pageName, token.pageName);
assert.deepStrictEqual(cloned, token);

createComment

Expand

param: string Text content of the comment
returns: CommentToken
Create an HTML comment.

// createComment (main)
var root = Parser.parse('');
assert.equal(root.createComment('a-->'), '<!--a--&gt;-->');
assert.equal(root.createComment(), '<!---->');

createElement

Expand

param: string Tag name
param: {closing?: boolean, selfClosing?: boolean} Options
returns: Token
Create an HTML tag or extension tag.

// createElement (main)
var root = Parser.parse('');
assert.equal(
	root.createElement('includeonly'),
	'<includeonly></includeonly>',
);
assert.equal(
	root.createElement('includeonly', {selfClosing: true}),
	'<includeonly/>',
);
assert.equal(
	root.createElement('REF'),
	'<ref></ref>',
);
assert.equal(
	root.createElement('ref', {selfClosing: true}),
	'<ref/>',
);
assert.equal(
	root.createElement('li'),
	'<li>',
);
assert.equal(
	root.createElement('li', {closing: true}),
	'</li>',
);
assert.equal(
	root.createElement('li', {selfClosing: true}),
	'<li/>',
);

createRange

Expand

returns: AstRange
Create an AstRange object.

// createRange (main)
var root = Parser.parse('text'),
	{firstChild} = root,
	range = root.createRange();
range.setStart(firstChild, 1);
range.setEnd(firstChild, 3);
assert.equal(range, 'ex');

createTextNode

Expand

param: string
returns: AstText
Create a text node.

// createTextNode (main)
var root = Parser.parse(''),
	text = root.createTextNode('a');
assert.strictEqual(text.type, 'text');
assert.strictEqual(text.data, 'a');
text = root.createTextNode();
assert.strictEqual(text.type, 'text');
assert.strictEqual(text.data, '');

expand

Expand

version added: 1.10.0

returns: Token
Expand templates and some magic words.

// expand (main)
var root, copy;
Parser.templates.set(
	'Template:Link_start',
	`[[{{{1|<noinclude>Template:Link start]]</noinclude>}}}<noinclude>
{{doc}}</noinclude>`,
);
root = Parser.parse('{{link start|a}}b|c]]');
copy = root.expand();
assert.deepStrictEqual(copy.childNodes.map(({type}) => type), ['link']);
assert.equal(copy, '[[ab|c]]');

findEnclosingHtml

Expand

param: string Tag name (optional)
returns: AstRange | undefined
Get the specified outer HTML tag. Note that text nodes cannot use this method.

// findEnclosingHtml (main)
var root = Parser.parse('<p><b><li/><br><i>{{{|[[a]]}}}</b></p>'),
	arg = root.querySelector('arg'),
	link = arg.querySelector('link');
assert.equal(arg, '{{{|[[a]]}}}');
assert.equal(link, '[[a]]');
assert.strictEqual(root.findEnclosingHtml(), undefined);
assert.equal(
	arg.findEnclosingHtml(),
	'<b><li/><br><i>{{{|[[a]]}}}</b>',
);
assert.equal(
	arg.findEnclosingHtml('p'),
	'<p><b><li/><br><i>{{{|[[a]]}}}</b></p>',
);
assert.equal(
	link.findEnclosingHtml(),
	'<b><li/><br><i>{{{|[[a]]}}}</b>',
);
assert.equal(
	link.findEnclosingHtml('p'),
	'<p><b><li/><br><i>{{{|[[a]]}}}</b></p>',
);

flatten

Expand

Merge the plain child nodes of the current plain node.

// flatten (main)
var root = Parser.parse(''),
	plain = Parser.parse('a'),
	{firstChild} = plain;
root.append(plain);
root.flatten();
assert.strictEqual(root.lastChild, firstChild);

getCategories

Expand

returns: [string, string | undefined][]
Get all categories and their sort keys.

// getCategories (main)
assert.deepStrictEqual(
	Parser.parse([[category:a]][[category:b|c]]').getCategories(),
	[
		['Category:A', undefined],
		['Category:B', 'c'],
	],
);

isInterwiki

Expand

param: string Title
returns: RegExpExecArray | null
Similar to Parser.isInterwiki, but using the same parsing configurations as the current node.

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var root = Parser.parse(`<br id=a>
<pre id=a/>
{|id=a
|}`);
assert.deepStrictEqual(root.lint(), [
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate HTML id',
		startLine: 0,
		startCol: 4,
		startIndex: 4,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
	},
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate HTML id',
		startLine: 1,
		startCol: 5,
		startIndex: 15,
		endLine: 1,
		endCol: 9,
		endIndex: 19,
	},
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate HTML id',
		startLine: 2,
		startCol: 2,
		startIndex: 24,
		endLine: 2,
		endCol: 6,
		endIndex: 28,
	},
]);
root = Parser.parse([[category:a]][[category:a|b]]');
assert.deepStrictEqual(root.lint(), [
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate category',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 14,
		endIndex: 14,
		suggestions: [
			{
				range: [0, 14],
				text: '',
				desc: 'remove',
			},
		],
	},
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate category',
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 30,
		endIndex: 30,
		suggestions: [
			{
				range: [14, 30],
				text: '',
				desc: 'remove',
			},
		],
	},
]);
root = Parser.parse(`<!-- lint-disable no-duplicate -->
[[category:a]][[category:a]]
<!-- lint-enable no-duplicate -->
<a> <!-- lint-disable-line -->
<!-- lint-disable-next-line no-duplicate, tag-like -->
[[category:a]][[category:a]]<a>`);
assert.deepStrictEqual(root.lint(), []);
Parser.lintConfig = {computeEditInfo: false};
root = Parser.parse([[category:a]][[category:a]]');
assert.deepStrictEqual(root.lint(), [
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate category',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 14,
		endIndex: 14,
	},
	{
		rule: 'no-duplicate',
		severity: 'warning',
		message: 'duplicate category',
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 28,
		endIndex: 28,
	},
]);
Parser.lintConfig = {fix: true};
root = Parser.parse('</br>');
assert.strictEqual(root.lint().output, '<br>');

normalizeTitle

✅ Expand

param: string Title (with or without namespace prefix)
param: number Namespace, default to 0
returns: Title
Normalize the page title.

// normalizeTitle
var root = Parser.parse(''),
	title;
root.pageName = 'Help:A/1';
title = root.normalizeTitle('../2');
assert.strictEqual(title.title, 'Help:A/2');

safeReplaceWith

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 AstNode.prototype.destroy method.

// safeReplaceWith (main)
var {firstChild, lastChild: {lastChild}} = Parser.parse('<p><p lang="zh">'),
	attrs = lastChild.cloneNode(),
	error;
assert.equal(firstChild, '<p>');
assert.equal(lastChild, ' lang="zh"');
assert.equal(attrs, ' lang="zh"');
try {
	firstChild.lastChild.replaceWith(attrs);
} catch (e) {
	error = e;
}
assert.ok(error instanceof Error);
assert.equal(error.message, 'HtmlToken cannot insert child nodes!');
firstChild.lastChild.safeReplaceWith(attrs);

section

Expand

param: number Number of the section
returns: AstRange
Get the specified section. Only available for root nodes.

// section (main)
var root = Parser.parse('a\n==1==\nb\n===2===\nc\n==3==\nd');
assert.equal(root.section(0), 'a\n');
assert.equal(root.section(1), '==1==\nb\n===2===\nc\n');
assert.equal(root.section(2), '===2===\nc\n');
assert.equal(root.section(3), '==3==\nd');

root = Parser.parse('a');
assert.equal(root.section(0), 'a');

sections

Expand

returns: AstRange[]
Get all sections (including the prelude), each of which is an array. Only available for root nodes.

// sections (main)
var root = Parser.parse('a\n==1==\nb\n===2===\nc\n==3==\nd'),
	[s0, s1, s2, s3] = root.sections();
assert.equal(s0, 'a\n');
assert.equal(s1, '==1==\nb\n===2===\nc\n');
assert.equal(s2, '===2===\nc\n');
assert.equal(s3, '==3==\nd');

root = Parser.parse('a');
[s0] = root.sections();
assert.equal(s0, 'a');

solveConst

Expand

returns: Token
Parse template parameters and some magic words.

// solveConst (main)
var root = Parser.parse(
		`{{{null}}}{{{|[[a]]}}}{{#if:||[//b]}}{{#switch:|#default={{c}}}}{{#switch:d|d|e=}}`,
	),
	copy = root.solveConst();
assert.deepStrictEqual(
	copy.childNodes.map(({type}) => type),
	['text', 'link', 'ext-link', 'template'],
);
assert.deepStrictEqual(
	copy.childNodes.map(String),
	['{{{null}}}', '[[a]]', '[//b]', '{{c}}'],
);

toHtml

Expand

version added: 1.10.0

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

// toHtml (main)
var root = Parser.parse(`a

<!-- -->

b

[[category:1]] [[category:2]]
[[category:3]]

c`);
root.type = 'plain';
assert.strictEqual(
	root.toHtml(),
	'a\n\nb\n\nc',
);
Parser.templates.set('Template:Dt', ';');
root = Parser.parse('a\n\nb{{dt}}{{dt}}c:d');
assert.strictEqual(
	root.toHtml(),
	'<p>a\n</p><p>b\n</p>\n<dl><dt></dt>\n<dt>c</dt>\n<dd>d</dd></dl>',
);
⚠️ **GitHub.com Fallback** ⚠️