Skip to content

ExtToken

bhsd edited this page Jul 23, 2024 · 22 revisions

Other Languages

简介

扩展标签。这个类同时混合了 AttributesToken 类的属性和方法。

✅ 在 MiniBrowser 版本中可用。

Properties

name

✅ 展开

type: string
标签名,只读。

// name
var {firstChild} = Parser.parse('<pre>a</pre>');
assert.equal(firstChild, '<pre>a</pre>');
assert.strictEqual(firstChild.name, 'pre');

selfClosing

✅ 展开

type: boolean
是否自封闭。

// selfClosing
var {firstChild} = Parser.parse('<ref/>');
assert.equal(firstChild, '<ref/>');
assert(firstChild.selfClosing);
firstChild.selfClosing = false;
assert.equal(firstChild, '<ref></ref>');

innerText

✅ 展开

type: string
内部 wikitext,只读。

// innerText
var {firstChild} = Parser.parse('<pre>a</pre>');
assert.equal(firstChild, '<pre>a</pre>');
assert.strictEqual(firstChild.innerText, 'a');

Methods

lint

✅ 展开

returns: LintError[]
报告潜在语法错误。

// lint
var root = Parser.parse('<b title="a<ref>aa</ref>">aaa</b>'),
	ref = root.querySelector('ext');
assert.equal(ref, '<ref>aa</ref>');
assert.deepStrictEqual(ref.lint(), [
	{
		rule: 'parsing-order',
		severity: 'error',
		message: 'extension tag in HTML tag attributes',
		startLine: 0,
		startCol: 11,
		startIndex: 11,
		endLine: 0,
		endCol: 24,
		endIndex: 24,
	},
]);

cloneNode

展开

returns: this
深拷贝节点。

// cloneNode (main)
var {firstChild, lastChild} = Parser
		.parse('<ref name="a">a</ref><ref name="a"/>'),
	closed = firstChild.cloneNode(),
	selfClosing = lastChild.cloneNode();
assert.equal(firstChild, '<ref name="a">a</ref>');
assert.equal(closed, '<ref name="a">a</ref>');
assert.equal(lastChild, '<ref name="a"/>');
assert.equal(selfClosing, '<ref name="a"/>');
assert.deepStrictEqual(closed, firstChild);
assert.deepStrictEqual(selfClosing, lastChild);

toHtml

展开

加入的版本: 1.10.0

returns: string
转换为 HTML。

// toHtml (main)
var {firstChild} = Parser.parse('<nowiki>a\nb</nowiki>');
assert.strictEqual(firstChild.toHtml(), 'a&#10;b');
({firstChild} = Parser.parse('<pre id=a><nowiki>-{a\nb}-</nowiki></pre>'));
assert.strictEqual(firstChild.toHtml(), '<pre id="a">a&#10;b</pre>');
({firstChild} = Parser.parse('<poem class=b>a\nb</poem>'));
assert.strictEqual(firstChild.toHtml(), '<div class="b poem">a<br>\nb</div>');
Clone this wiki locally