TagPairToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki
The parent class of IncludeToken, TranslateToken and ExtToken. 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.
✅ Expand
type: boolean
Whether the tag pair is properly closed.
// closed
var {firstChild} = Parser.parse("<includeonly> a");
assert.equal(firstChild, "<includeonly> a");
assert.ok(!firstChild.closed);
firstChild.closed = true;
assert.equal(firstChild, "<includeonly> a</includeonly>");
({firstChild} = Parser.parse("<pre/>"));
assert.equal(firstChild, "<pre/>");
assert.ok(firstChild.closed);✅ Expand
type: string | undefined
If the tag is not self-closing, this property contains the text content inside the tag pair. Read-only.
// innerText
var {firstChild} = Parser.parse("<translate> a </translate>");
assert.equal(firstChild, "<translate> a </translate>");
assert.strictEqual(firstChild.innerText, " a ");
({firstChild} = Parser.parse("<includeonly> b"));
assert.equal(firstChild, "<includeonly> b");
assert.strictEqual(firstChild.innerText, " b");
({firstChild} = Parser.parse("<pre/>"));
assert.equal(firstChild, "<pre/>");
assert.strictEqual(firstChild.innerText, undefined);✅ Expand
type: string
Tag name in lowercase.
// name
var {firstChild} = Parser.parse("<PRE/>");
assert.equal(firstChild, "<PRE/>");
assert.strictEqual(firstChild.name, "pre");
({firstChild} = Parser.parse("<translate></translate>"));
assert.equal(firstChild, "<translate></translate>");
assert.strictEqual(firstChild.name, "translate");
({firstChild} = Parser.parse("<includeonly>"));
assert.equal(firstChild, "<includeonly>");
assert.strictEqual(firstChild.name, "includeonly");✅ Expand
type: boolean
Whether the tag is self-closing.
// selfClosing
var {firstChild} = Parser.parse("<translate>a</translate>");
assert.equal(firstChild, "<translate>a</translate>");
assert.ok(!firstChild.selfClosing);// selfClosing (main)
var {firstChild, lastChild} = Parser.parse(`<nowiki>a</nowiki>
<categorytree>b</categorytree>`);
assert.equal(firstChild, "<nowiki>a</nowiki>");
assert.equal(lastChild, "<categorytree>b</categorytree>");
firstChild.selfClosing = true;
assert.equal(firstChild, "<nowiki/>");
lastChild.selfClosing = true;
assert.equal(lastChild, "<categorytree/>");🌐 Expand
Save the syntax tree as JSON.
// json (print)
var {firstChild} = Parser.parse("<translate>a</translate>");
assert.strictEqual(firstChild.type, "translate");
assert.deepStrictEqual(firstChild.json(), {
range: [0, 24],
type: "translate",
name: "translate",
selfClosing: false,
childNodes: [
{
range: [10, 10],
type: "translate-attr",
childNodes: [],
},
{
range: [11, 12],
type: "translate-inner",
childNodes: [
{
range: [11, 12],
data: "a",
},
],
},
],
});🌐 Expand
returns: string
Output in HTML format.
// print (print)
var {firstChild} = Parser.parse("<pre/>");
assert.equal(
firstChild.print(),
'<span class="wpb-ext"><pre/></span>',
);
({firstChild} = Parser.parse("<translate>a</translate>"));
assert.equal(
firstChild.print(),
`<span class="wpb-ext"><translate><span class="wpb-ext-inner">a</span></translate></span>`,
);
({firstChild} = Parser.parse("<includeonly>b"));
assert.equal(
firstChild.print(),
'<span class="wpb-include"><includeonly>b</span>',
);