TranslateToken - bhsd-harry/wikiparser-node GitHub Wiki
目录
扩展:Translate 中的 <translate> 标签。这个类继承了 TagPairToken 类的全部属性和方法。
✅ 在 Mini 和 Browser 版本中可用。
🌐 在 Browser 版本中可用。
从 TagPairToken 继承的属性
✅ 展开
type: string
// type
var {firstChild} = Parser.parse("<translate></translate>");
assert.equal(firstChild, "<translate></translate>");
assert.strictEqual(firstChild.type, "translate");展开
returns: this
深拷贝节点。
// 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);展开
param: string 属性键
returns: string | true
获取 nowrap 属性。
// 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);展开
param: string 属性键
returns: boolean
标签是否具有 nowrap 属性。
// 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"));🌐 展开
returns: string
以HTML格式输出。
// 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"><translate<span class="wpb-ext-attrs"> <span class="wpb-ext-attr"><span class="wpb-attr-key">nowrap</span></span></span>><span class="wpb-ext-inner">
</span></translate></span>`,
);展开
param: string 属性键
移除 nowrap 属性。
// removeAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
firstChild.removeAttr("nowrap");
assert.equal(firstChild, "<translate></translate>");展开
param: string | Record<string, boolean> 属性键或属性对象
param: boolean 属性值
设置 nowrap 属性。
// 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>");展开
param: string 属性键
开关 nowrap 属性。
// toggleAttr (main)
var {firstChild} = Parser.parse("<translate nowrap></translate>");
assert.equal(firstChild, "<translate nowrap></translate>");
firstChild.toggleAttr("nowrap");
assert.equal(firstChild, "<translate></translate>");展开
returns: string
转换为 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>',
);