ArgToken - bhsd-harry/wikiparser-node GitHub Wiki

目录

Other Languages

简介

{{{}}} 包裹的模板参数。这个类继承了 Token 类的全部属性和方法,这里不再列出。

✅ 在 MiniBrowser 版本中可用。
🌐 在 Browser 版本中可用。

Properties

default

🌐 展开

type: string | false
预设值。在 Browser 版本中为只读属性。

// default (print)
var {firstChild} = Parser.parse("{{{a}}}");
assert.equal(firstChild, "{{{a}}}");
assert.strictEqual(firstChild.default, false);
// default (main)
var {firstChild} = Parser.parse("{{{a}}}");
firstChild.default = "b";
assert.equal(firstChild, "{{{a|b}}}");

name

✅ 展开

type: string
参数名,只读。

// name
var {firstChild} = Parser.parse("{{{a}}}");
assert.equal(firstChild, "{{{a}}}");
assert.strictEqual(firstChild.name, "a");
// name (main)
var {firstChild} = Parser.parse("{{{a}}}"),
	argName = firstChild.firstChild;
argName.replaceChildren("b");
assert.strictEqual(firstChild.name, "b");

Methods

lint

✅ 展开

returns: LintError[]
报告潜在语法错误。

// lint
var noinclude = Parser.parse("<ref>{{{a|b}}}</ref>").querySelector("arg"),
	include = Parser.parse("{{{a|b|c}}}", true).firstChild;
assert.equal(noinclude, "{{{a|b}}}");
assert.deepStrictEqual(noinclude.lint(), [
	{
		rule: "no-arg",
		severity: "warning",
		message: "unexpected template argument",
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 14,
		endIndex: 14,
		suggestions: [
			{
				desc: "expand",
				range: [5, 14],
				text: "b",
			},
		],
	},
	{
		rule: "arg-in-ext",
		severity: "warning",
		message: "template argument inside an extension tag",
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 14,
		endIndex: 14,
	},
]);
assert.equal(include, "{{{a|b|c}}}");
assert.deepStrictEqual(include.lint(), [
	{
		rule: "no-ignored",
		severity: "error",
		message: "invisible content inside triple braces",
		startLine: 0,
		startCol: 6,
		startIndex: 6,
		endLine: 0,
		endCol: 8,
		endIndex: 8,
		suggestions: [
			{
				desc: "remove",
				range: [6, 8],
				text: "",
			},
			{
				desc: "escape",
				range: [6, 7],
				text: "{{!}}",
			},
		],
	},
]);

json

🌐 展开

将语法树保存为 JSON。

// json (print)
var {lastChild} = Parser.parse(" {{{a}}}");
assert.deepStrictEqual(lastChild.json(), {
	range: [1, 8],
	type: "arg",
	name: "a",
	default: false,
	childNodes: [
		{
			range: [4, 5],
			type: "arg-name",
			childNodes: [
				{
					range: [4, 5],
					data: "a",
				},
			],
		},
	],
});

cloneNode

展开

returns: this
深拷贝节点。

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

removeRedundant

展开

移除无效部分。

// removeRedundant (main)
var {firstChild} = Parser.parse("{{{a|b|c}}}");
assert.equal(firstChild, "{{{a|b|c}}}");
firstChild.removeRedundant();
assert.equal(firstChild, "{{{a|b}}}");

setName

展开

param: string 新参数名
设置参数名。

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

setDefault

展开

param: string
设置预设值预设值。

// setDefault (main)
var {firstChild} = Parser.parse("{{{a}}}");
assert.equal(firstChild, "{{{a}}}");
firstChild.setDefault("b");
assert.equal(firstChild, "{{{a|b}}}");
⚠️ **GitHub.com Fallback** ⚠️