TagToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki
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.
✅ Expand
type: boolean
Whether the tag is a closing tag.
// closing
var {firstChild} = Parser.parse('</p>');
assert.ok(firstChild.closing);
✅ 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);
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());
🌐 Expand
Save the syntax tree as JSON.
// json
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: '',
},
],
},
],
});
🌐 Expand
returns: string
Output in HTML format.
// print
var {firstChild} = Parser.parse('<p>');
assert.strictEqual(
firstChild.print(),
'<span class="wpb-html"><p></span>',
);
({firstChild} = Parser.parse('</p>'));
assert.strictEqual(
firstChild.print(),
'<span class="wpb-html"></p></span>',
);
({firstChild} = Parser.parse('<br>'));
assert.strictEqual(
firstChild.print(),
'<span class="wpb-html"><br></span>',
);