Skip to content

TranscludeToken (EN)

bhsd edited this page Jan 22, 2024 · 26 revisions
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
var {firstChild, lastChild} = Parser.parse('{{a}}{{!}}');
assert.equal(firstChild, '{{a}}');
assert.equal(lastChild, '{{!}}');
assert.strictEqual(firstChild.name, 'Template:A');
assert.strictEqual(lastChild.name, '!');

duplication

Expand

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

// duplication
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]#a}}{{#invoke:b|b|b=1|b=2}}`)
	.querySelectorAll('magic-word');
assert.equal(a, '{{#invoke:[a]#a}}');
assert.equal(b, '{{#invoke:b|b|b=1|b=2}}');
assert.deepEqual(a.lint(), [
	{
		severity: 'error',
		message: 'useless fragment',
		startLine: 0,
		startCol: 10,
		startIndex: 10,
		endLine: 0,
		endCol: 15,
		endIndex: 15,
	},
	{
		severity: 'error',
		message: 'illegal module name',
		startLine: 0,
		startCol: 10,
		startIndex: 10,
		endLine: 0,
		endCol: 15,
		endIndex: 15,
	},
	{
		severity: 'error',
		message: 'missing module function',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 17,
		endIndex: 17,
	},
]);
assert.deepEqual(b.lint(), [
	{
		severity: 'error',
		message: 'duplicated parameter',
		startLine: 0,
		startCol: 31,
		startIndex: 31,
		endLine: 0,
		endCol: 34,
		endIndex: 34,
	},
	{
		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
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
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.subst();
assert.equal(firstChild, '{{subst:a}}');

safesubst

Expand

Safe substitution.

// safesubst
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
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
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
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
var {firstChild} = Parser.parse('{{a|b}}');
assert.equal(firstChild, '{{a|b}}');
assert.deepStrictEqual(firstChild.getKeys(), ['1']);

getValues

Expand

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

// getValues
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
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
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
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
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
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
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
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
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
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}}');
Clone this wiki locally