TranscludeToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Table of Contents
- Introduction
- Properties
-
Methods
- setModifier ✅
- isTemplate ✅
- lint ✅
- getAllArgs
- getAnonArgs
- getArgs ✅
- getDuplicatedArgs ✅
- getPossibleValues ✅
- getModule
- getFrame
- cloneNode
- subst
- safesubst
- hasArg
- getArg
- removeArg
- getKeys
- getValues
- getValue
- newAnonArg
- setValue
- anonToNamed
- replaceTemplate
- replaceModule
- replaceFunction
- hasDuplicatedArgs
- fixDuplication
- escapeTables
Template or magic word. This class inherits all the properties and methods of the Token class which are not repeated here.
✅ 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, "!");// name (main)
var {firstChild} = Parser.parse("{{a}}");
firstChild.firstChild.setText("b", 0);
assert.strictEqual(firstChild.name, "Template:B");✅ Expand
version added: 1.21.0
type: string | undefined
Name of module (with namespace), read-only.
// module
var {firstChild, lastChild} = Parser.parse("{{#invoke:a|b}}{{c}}");
assert.equal(firstChild, "{{#invoke:a|b}}");
assert.equal(lastChild, "{{c}}");
assert.strictEqual(firstChild.module, "Module:A");
assert.strictEqual(lastChild.module, undefined);
firstChild.childNodes[1].setText("b", 0);
assert.strictEqual(firstChild.module, "Module:B");✅ Expand
version added: 1.21.2
type: string | undefined
Name of module function, read-only.
// function
var {firstChild, lastChild} = Parser.parse("{{#invoke:a|b}}{{c}}");
assert.equal(firstChild, "{{#invoke:a|b}}");
assert.equal(lastChild, "{{c}}");
assert.strictEqual(firstChild.function, "b");
assert.strictEqual(lastChild.function, undefined);
firstChild.childNodes[2].setText("a", 0);
assert.strictEqual(firstChild.function, "a");Expand
version added: 1.36.0
type: number
Number of anonymous parameters, read-only.
// anonCount (main)
var {firstChild} = Parser.parse("{{a|b|1=b}}");
assert.equal(firstChild, "{{a|b|1=b}}");
assert.strictEqual(firstChild.anonCount, 1);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.ok(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.ok(firstChild.isTemplate());
assert.ok(!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: "invalid Scribunto module name",
startLine: 0,
startCol: 10,
startIndex: 10,
endLine: 0,
endCol: 13,
endIndex: 13,
},
{
rule: "invalid-invoke",
severity: "error",
message: "missing Scribunto module function name",
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 15,
endIndex: 15,
},
]);
assert.deepStrictEqual(b.lint(), [
{
rule: "no-ignored",
severity: "warning",
message: "useless fragment",
startLine: 0,
startCol: 25,
startIndex: 25,
endLine: 0,
endCol: 28,
endIndex: 28,
suggestions: [
{
range: [26, 28],
text: "",
desc: "remove",
},
],
},
{
rule: "no-duplicate",
severity: "error",
message: "duplicate template 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: "duplicate template 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 (main)
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 (main)
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 duplicate 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
version added: 1.16.4
returns: [string, string]
Get the module name and the function name.
// getModule (main)
var {firstChild} = Parser.parse("{{#invoke:a|b}}");
assert.equal(firstChild, "{{#invoke:a|b}}");
assert.deepStrictEqual(
firstChild.getModule(),
["Module:A", "b"],
);Expand
version added: 1.22.0
returns: Frame
Get a basic JSON frame object for the #invoke parser function.
// getFrame (main)
var {firstChild} = Parser.parse("{{#invoke:a a|b|c|1=d}}");
assert.equal(firstChild, "{{#invoke:a a|b|c|1=d}}");
assert.deepStrictEqual(
firstChild.getFrame(),
{
args: {1: "d"},
parent: undefined,
title: "Module:A a",
},
);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.ok(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|c}}");
assert.equal(firstChild, "{{a|b|c}}");
firstChild.removeArg(1);
assert.equal(firstChild, "{{a|2=c}}");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
param: boolean Whether to append the new parameter on a new line
Insert anonymous parameter.
// newAnonArg (main)
var {firstChild} = Parser.parse("{{a}}");
assert.equal(firstChild, "{{a}}");
firstChild.newAnonArg("b");
firstChild.newAnonArg("c", true);
firstChild.setValue("d", "");
firstChild.newAnonArg("e", true);
assert.equal(firstChild, "{{a|b|c|d=\n|e}}");
({firstChild} = Parser.parse("{{#if:x}}"));
assert.equal(firstChild, "{{#if:x}}");
firstChild.newAnonArg("y", true);
assert.equal(firstChild, "{{#if:x\n|y}}");Expand
param: string Name of parameter
param: string Parameter value
param: boolean Whether to append the new parameter on a new line
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", true);
firstChild.setValue("e", "", true);
assert.equal(firstChild, "{{a|c|2=d\n|e=}}");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 duplicate 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 duplicate 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|\n{|\n|-\n|rowspan=2|d\n|}\n}}"),
{firstChild} = root;
assert.equal(firstChild, "{{a|\n{|\n|-\n|rowspan=2|d\n|}\n}}");
assert.strictEqual(firstChild.length, 7);
firstChild.escapeTables();
assert.equal(
root,
"{{a|\n{{{!}}\n{{!}}-\n{{!}}rowspan{{=}}2{{!}}d\n{{!}}}\n}}",
);
assert.strictEqual(root.firstChild.length, 2);