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

Table of Contents

Other Languages

Introduction

Template or magic word.

✅ Available in the Mini and Browser versions.

Properties

modifier

✅ Expand

type: string
Modifier of magic word, such as subst and safesubst, containing :, read-only.

// modifier
var magicWord = Parser
	.parse('<includeonly>{{subst:REVISIONUSER}}</includeonly>', true)
	.querySelector('magic-word');
assert.equal(magicWord, '{{subst:REVISIONUSER}}');
assert.strictEqual(magicWord.modifier, 'subst:');

name

Expand

type: string
Name of template (with namespace) or magic word, read-only.

// name (main)
var {firstChild, lastChild} = Parser.parse('{{a}}{{!}}');
assert.equal(firstChild, '{{a}}');
assert.equal(lastChild, '{{!}}');
assert.strictEqual(firstChild.name, 'Template:A');
assert.strictEqual(lastChild.name, '!');
firstChild.firstChild.setText('b', 0);
assert.strictEqual(firstChild.name, 'Template:B');

duplication

Expand

type: boolean
Whether there are duplicate parameters, read-only.

// duplication (main)
var {firstChild} = Parser.parse('{{a|b|1=b}}');
assert.equal(firstChild, '{{a|b|1=b}}');
assert(firstChild.duplication);

Methods

setModifier

✅ Expand

param: string Modifier of transclusion
Set modifier of transclusion.

// setModifier
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.setModifier('subst:');
assert.equal(firstChild, '{{subst:a}}');

isTemplate

✅ Expand

returns: boolean
Whether is template / module or not.

// isTemplate
var {firstChild, lastChild} = Parser.parse('{{a}}{{!}}');
assert.equal(firstChild, '{{a}}');
assert.equal(lastChild, '{{!}}');
assert(firstChild.isTemplate());
assert(!lastChild.isTemplate());

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var [a, b] = Parser.parse(`{{#invoke:[a]}}{{#invoke:b#b|b|b=1|b=2}}`)
	.querySelectorAll('magic-word');
assert.equal(a, '{{#invoke:[a]}}');
assert.equal(b, '{{#invoke:b#b|b|b=1|b=2}}');
assert.deepEqual(a.lint(), [
	{
		rule: 'invalid-invoke',
		severity: 'error',
		message: 'illegal module name',
		startLine: 0,
		startCol: 10,
		startIndex: 10,
		endLine: 0,
		endCol: 13,
		endIndex: 13,
	},
	{
		rule: 'invalid-invoke',
		severity: 'error',
		message: 'missing module function',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 15,
		endIndex: 15,
	},
]);
assert.deepEqual(b.lint(), [
	{
		rule: 'no-ignored',
		severity: 'error',
		message: 'useless fragment',
		startLine: 0,
		startCol: 25,
		startIndex: 25,
		endLine: 0,
		endCol: 28,
		endIndex: 28,
		fix: {
			range: [26, 28],
			text: '',
		},
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'duplicated parameter',
		startLine: 0,
		startCol: 31,
		startIndex: 31,
		endLine: 0,
		endCol: 34,
		endIndex: 34,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'duplicated parameter',
		startLine: 0,
		startCol: 35,
		startIndex: 35,
		endLine: 0,
		endCol: 38,
		endIndex: 38,
	},
]);

getAllArgs

✅ Expand

returns: ParameterToken[]
Get all parameters.

// getAllArgs
var {firstChild} = Parser.parse('{{a|b|c=1}}');
assert.equal(firstChild, '{{a|b|c=1}}');
assert.deepStrictEqual(
	firstChild.getAllArgs(),
	firstChild.querySelectorAll('parameter'),
);

getAnonArgs

✅ Expand

returns: ParameterToken[]
Get all anonymous parameters.

// getAnonArgs
var {firstChild} = Parser.parse('{{a|b|c=1}}');
assert.equal(firstChild, '{{a|b|c=1}}');
assert.deepStrictEqual(
	firstChild.getAnonArgs(),
	[firstChild.querySelector('parameter')],
);

getArgs

✅ Expand

param: string | number
returns: Set<ParameterToken>
Get specified parameters.

// getArgs
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.deepStrictEqual(
	firstChild.getArgs(1),
	new Set(firstChild.querySelectorAll('parameter')),
);

getDuplicatedArgs

✅ Expand

returns: [string, ParameterToken[]][]
Get duplicated parameters.

// getDuplicatedArgs
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.deepStrictEqual(
	firstChild.getDuplicatedArgs(),
	[['1', firstChild.querySelectorAll('parameter')]],
);

getPossibleValues

✅ Expand

returns: Token[]
Get possible values for specific magic word.

// getPossibleValues
var {firstChild} = Parser.parse('{{#if:a|b|c}}'),
	{childNodes: [, a, b, c]} = firstChild;
assert.equal(firstChild, '{{#if:a|b|c}}');
assert.equal(a, 'a');
assert.equal(b, 'b');
assert.equal(c, 'c');
assert.deepStrictEqual(
	firstChild.getPossibleValues(),
	[b.lastChild, c.lastChild],
);

cloneNode

Expand

returns: this
Deep clone the node.

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

subst

Expand

Substitution.

// subst (main)
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.subst();
assert.equal(firstChild, '{{subst:a}}');

safesubst

Expand

Safe substitution.

// safesubst (main)
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.safesubst();
assert.equal(firstChild, '{{safesubst:a}}');

hasArg

Expand

param: string | number Name of parameter
returns: boolean
Whether the node has specified parameter.

// hasArg (main)
var {firstChild} = Parser.parse('{{a|b}}');
assert.equal(firstChild, '{{a|b}}');
assert(firstChild.hasArg(1));

getArg

Expand

param: string | number Name of parameter
returns: ParameterToken
Get specified parameter.

// getArg (main)
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.strictEqual(firstChild.getArg(1), firstChild.lastChild);

removeArg

Expand

param: string | number Name of parameter
Remove specified parameter.

// removeArg (main)
var {firstChild} = Parser.parse('{{a|b}}');
assert.equal(firstChild, '{{a|b}}');
firstChild.removeArg(1);
assert.equal(firstChild, '{{a}}');

getKeys

Expand

returns: string[]
Get all parameter names.

// getKeys (main)
var {firstChild} = Parser.parse('{{a|b|c=1}}');
assert.equal(firstChild, '{{a|b|c=1}}');
assert.deepStrictEqual(firstChild.getKeys(), ['1', 'c']);
firstChild.lastChild.firstChild.setText('d', 0);
assert.deepStrictEqual(firstChild.getKeys(), ['1', 'd']);

getValues

Expand

param: string | number Name of parameter
returns: string[]
Get specified parameter values.

// getValues (main)
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.deepStrictEqual(firstChild.getValues(1), ['b', 'c']);

getValue

Expand

param: string | number Name of parameter
returns: string
Get effective value of the specified parameter.

// getValue (main)
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.deepStrictEqual(firstChild.getValue(1), 'c');

newAnonArg

Expand

param: string Parameter value
Insert anonymous parameter.

// newAnonArg
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.newAnonArg('b');
assert.equal(firstChild, '{{a|b}}');

setValue

Expand

param: string Name of parameter
param: string Parameter value
Set specified parameter value.

// setValue (main)
var {firstChild} = Parser.parse('{{a|b}}');
assert.equal(firstChild, '{{a|b}}');
firstChild.setValue('1', 'c');
firstChild.setValue('2', 'd');
assert.equal(firstChild, '{{a|c|2=d}}');

anonToNamed

Expand

Convert anonymous parameters to named parameters.

// anonToNamed (main)
var {firstChild} = Parser.parse('{{a|b|c}}');
assert.equal(firstChild, '{{a|b|c}}');
firstChild.anonToNamed();
assert.equal(firstChild, '{{a|1=b|2=c}}');

replaceTemplate

Expand

param: string Template name
Rename template.

// replaceTemplate (main)
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.replaceTemplate('b');
assert.equal(firstChild, '{{b}}');

replaceModule

Expand

param: string Module name
Rename module.

// replaceModule (main)
var {firstChild} = Parser.parse('{{#invoke:a|b}}');
assert.equal(firstChild, '{{#invoke:a|b}}');
firstChild.replaceModule('c');
assert.equal(firstChild, '{{#invoke:c|b}}');

replaceFunction

Expand

param: string Function name
Rename module function.

// replaceFunction (main)
var {firstChild} = Parser.parse('{{#invoke:a|b}}');
assert.equal(firstChild, '{{#invoke:a|b}}');
firstChild.replaceFunction('c');
assert.equal(firstChild, '{{#invoke:a|c}}');

hasDuplicatedArgs

Expand

returns: number
Get the number of duplicated parameters.

// hasDuplicatedArgs (main)
var {firstChild} = Parser.parse('{{a|b|1=c}}');
assert.equal(firstChild, '{{a|b|1=c}}');
assert.strictEqual(firstChild.hasDuplicatedArgs(), 1);

fixDuplication

Expand

returns: string[]
Fix duplicated parameters.

// fixDuplication (main)
var {firstChild} = Parser.parse('{{a|b|1=b|1=}}');
assert.equal(firstChild, '{{a|b|1=b|1=}}');
firstChild.fixDuplication();
assert.equal(firstChild, '{{a|b}}');

escapeTables

Expand

returns: this
Escape tables in template.

// escapeTables (main)
var root = Parser.parse('{{a|b=c\n{|\n|-\n|rowspan=2|d\n|}\n}}'),
	{firstChild} = root;
assert.equal(firstChild, '{{a|b=c\n{|\n|-\n|rowspan=2|d\n|}\n}}');
firstChild.escapeTables();
assert.equal(
	root.toString(),
	'{{a|b=c\n{{{!}}\n{{!}}-\n{{!}}rowspan=2{{!}}d\n{{!}}}\n}}',
);
⚠ī¸ **GitHub.com Fallback** ⚠ī¸