TagToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Other Languages

Introduction

The parent class of TvarToken and HtmlToken. This class inherits all the properties and methods of the Token class which are not repeated here.

✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.

Properties

closing

✅ Expand

type: boolean
Whether the tag is a closing tag.

// closing
var {firstChild} = Parser.parse("</p>");
assert.ok(firstChild.closing);

Methods

findMatchingTag

✅ Expand

returns: this | undefined
Find the matching tag.

// findMatchingTag
var {firstChild} = Parser.parse("<li/>"),
	lastChild;
Parser.viewOnly = true;
assert.strictEqual(firstChild.findMatchingTag(), firstChild);
({
	firstChild,
	lastChild,
} = Parser.parse("<li><br><li/></li>"));
assert.strictEqual(firstChild.findMatchingTag(), lastChild);
assert.strictEqual(lastChild.findMatchingTag(), firstChild);
({
	firstChild,
	lastChild,
} = Parser.parse("<div><div></div></div>"));
assert.strictEqual(firstChild.findMatchingTag(), lastChild);
assert.strictEqual(lastChild.findMatchingTag(), firstChild);
({firstChild} = Parser.parse("<p>"));
assert.strictEqual(firstChild.findMatchingTag(), undefined);
// findMatchingTag (main)
var {
	firstChild,
	lastChild,
} = Parser.parse("<translate><tvar name=1><tvar|2></></tvar></translate>")
	.querySelector("translate-inner");
assert.equal(firstChild, "<tvar name=1>");
assert.equal(lastChild, "</tvar>");
assert.strictEqual(firstChild.findMatchingTag(), lastChild);
assert.strictEqual(lastChild.findMatchingTag(), firstChild);

getRange

Expand

returns: AstRange | undefined
Get the range of the tag pair.

// getRange (main)
var {firstChild} = Parser.parse("<p>"),
	lastChild;
assert.strictEqual(firstChild.getRange(), undefined);
({firstChild} = Parser.parse("<br>"));
assert.strictEqual(firstChild.getRange(), undefined);
({firstChild, lastChild} = Parser.parse("<p>a</p>"));
assert.equal(firstChild.getRange(), "a");
assert.deepStrictEqual(firstChild.getRange(), lastChild.getRange());

json

🌐 Expand

Save the syntax tree as JSON.

// json (print)
var [, tvar] = Parser.parse("<translate><tvar|1></></translate>")
	.querySelectorAll("tvar");
assert.equal(tvar, "</>");
assert.deepStrictEqual(tvar.json(), {
	range: [19, 22],
	type: "tvar",
	closing: true,
	childNodes: [
		{
			range: [21, 21],
			type: "tvar-name",
			childNodes: [
				{
					range: [21, 21],
					data: "",
				},
			],
		},
	],
});

print

🌐 Expand

returns: string
Output in HTML format.

// print (print)
var {firstChild} = Parser.parse("<p>");
assert.strictEqual(
	firstChild.print(),
	'<span class="wpb-html">&lt;p&gt;</span>',
);
({firstChild} = Parser.parse("</p>"));
assert.strictEqual(
	firstChild.print(),
	'<span class="wpb-html">&lt;/p&gt;</span>',
);
({firstChild} = Parser.parse("<br>"));
assert.strictEqual(
	firstChild.print(),
	'<span class="wpb-html">&lt;br&gt;</span>',
);
⚠️ **GitHub.com Fallback** ⚠️