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

Table of Contents

Other Languages

Introduction

<translate> tags from Extension:Translate. This class inherits all the properties and methods of the TagPairToken class.

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

Properties

Inherited properties from TagPairToken

type

✅ Expand

type: string

// type
var {firstChild} = Parser.parse("<translate></translate>");
assert.equal(firstChild, "<translate></translate>");
assert.strictEqual(firstChild.type, "translate");

Methods

cloneNode

Expand

returns: this
Deep clone the node.

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

getAttr

Expand

param: string Attribute name
returns: string | true
Get the value of the nowrap attribute.

// getAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
assert.strictEqual(firstChild.getAttr("nowrap"), true);
assert.strictEqual(firstChild.getAttr("x"), undefined);

hasAttr

Expand

param: string Attribute name
returns: boolean
Whether the nowrap attribute exists.

// hasAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
assert.ok(firstChild.hasAttr("nowrap"));
assert.ok(!firstChild.hasAttr("x"));

print

🌐 Expand

returns: string
Output in HTML format.

// print (print)
var {firstChild} = Parser.parse("<translate nowrap>\n</translate>");
assert.equal(firstChild, "<translate nowrap>\n</translate>");
assert.strictEqual(
	firstChild.print(),
	`<span class="wpb-ext">&lt;translate<span class="wpb-ext-attrs"> <span class="wpb-ext-attr"><span class="wpb-attr-key">nowrap</span></span></span>&gt;<span class="wpb-ext-inner">
</span>&lt;/translate&gt;</span>`,
);

removeAttr

Expand

param: string Attribute name
Remove the nowrap attribute.

// removeAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
firstChild.removeAttr("nowrap");
assert.equal(firstChild, "<translate></translate>");

setAttr

Expand

param: string | Record<string, boolean> Attribute name or attributes
param: boolean Attribute value
Set the nowrap attribute.

// setAttr (main)
var {firstChild} = Parser.parse("<translate></translate>");
assert.equal(firstChild, "<translate></translate>");
firstChild.setAttr("nowrap", true);
assert.equal(firstChild, "<translate nowrap></translate>");
firstChild.setAttr({nowrap: false});
assert.equal(firstChild, "<translate></translate>");

toggleAttr

Expand

param: string Attribute name
Toggle the nowrap attribute.

// toggleAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
firstChild.toggleAttr("nowrap");
assert.equal(firstChild, "<translate></translate>");

toHtml

Expand

returns: string
Convert to HTML.

// toHtml (main)
var {firstChild} = Parser.parse("<translate><!--T:1--> a</translate>");
assert.equal(firstChild, "<translate><!--T:1--> a</translate>");
assert.strictEqual(firstChild.toHtml(), "a");
({firstChild} = Parser.parse(`<translate>
== b == <!--T:1-->
</translate>`));
assert.strictEqual(
	firstChild.toHtml(),
	'<div class="mw-heading mw-heading2"><h2 id="b">b</h2></div>',
);
⚠️ **GitHub.com Fallback** ⚠️