-
Notifications
You must be signed in to change notification settings - Fork 2
TranscludeToken (EN)
Table of Contents
Template or magic word.
✅ Available in the Mini and Browser versions.
✅ 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:');
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, '!');
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);
✅ 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}}');
✅ 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());
✅ 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,
},
]);
✅ 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'));
✅ 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')]);
✅ 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')),
);
✅ 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')]]);
✅ 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]);
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);
Expand
Substitution.
// subst
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.subst();
assert.equal(firstChild, '{{subst:a}}');
Expand
Safe substitution.
// safesubst
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.safesubst();
assert.equal(firstChild, '{{safesubst:a}}');
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));
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);
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}}');
Expand
returns: string[]
Get all parameter names.
// getKeys
var {firstChild} = Parser.parse('{{a|b}}');
assert.equal(firstChild, '{{a|b}}');
assert.deepStrictEqual(firstChild.getKeys(), ['1']);
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']);
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');
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}}');
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}}');
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}}');
Expand
param: string
Template name
Rename template.
// replaceTemplate
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}')
firstChild.replaceTemplate('b');
assert.equal(firstChild, '{{b}}');
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}}');
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}}');
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);
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}}');
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}}');
对维基文本批量执行语法检查的命令行工具
用于维基文本的 VSCode 扩展
A command-line tool that performs linting on Wikitext in bulk
VSCode extension for Wikitext