-
Notifications
You must be signed in to change notification settings - Fork 2
TranscludeToken (EN)
Table of Contents
- Introduction
- Properties
-
Methods
- setModifier ✅
- isTemplate ✅
- lint ✅
- getAllArgs ✅
- getAnonArgs ✅
- getArgs ✅
- getDuplicatedArgs ✅
- getPossibleValues ✅
- cloneNode
- subst
- safesubst
- hasArg
- getArg
- removeArg
- getKeys
- getValues
- getValue
- newAnonArg
- setValue
- anonToNamed
- replaceTemplate
- replaceModule
- replaceFunction
- hasDuplicatedArgs
- fixDuplication
- escapeTables
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 (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');
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);
✅ 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]}}{{#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.deepStrictEqual(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.deepStrictEqual(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: '',
desc: 'remove',
},
},
{
rule: 'no-duplicate',
severity: 'error',
message: 'duplicated parameter',
startLine: 0,
startCol: 31,
startIndex: 31,
endLine: 0,
endCol: 34,
endIndex: 34,
suggestions: [
{
desc: 'remove',
range: [30, 34],
text: '',
},
],
},
{
rule: 'no-duplicate',
severity: 'error',
message: 'duplicated parameter',
startLine: 0,
startCol: 35,
startIndex: 35,
endLine: 0,
endCol: 38,
endIndex: 38,
suggestions: [
{
desc: 'remove',
range: [34, 38],
text: '',
},
],
},
]);
✅ 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 (main)
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 (main)
var {firstChild} = Parser.parse('{{a}}');
assert.equal(firstChild, '{{a}}');
firstChild.subst();
assert.equal(firstChild, '{{subst:a}}');
Expand
Safe substitution.
// safesubst (main)
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 (main)
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 (main)
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 (main)
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 (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']);
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']);
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');
Expand
param: string
Parameter value
Insert anonymous parameter.
// newAnonArg (main)
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 (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}}');
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}}');
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}}');
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}}');
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}}');
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);
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}}');
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}}',
);
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext