HtmlToken - bhsd-harry/wikiparser-node GitHub Wiki

目录

Other Languages

简介

HTML标签,未进行匹配。这个类混合了 AttributesToken 类的属性和方法,且继承了 Token 类的全部属性和方法,这里不再列出。

✅ 在 MiniBrowser 版本中可用。
🌐 在 Browser 版本中可用。

Properties

name

✅ 展开

type: string
小写的标签名。

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

closing

✅ 展开

type: boolean
是否是闭合标签。在 MiniBrowser 版本中为只读属性。

// closing
var {firstChild} = Parser.parse("<b>");
assert.equal(firstChild, "<b>");
assert.ok(!firstChild.closing);
// closing (main)
var {firstChild} = Parser.parse("<b>");
firstChild.closing = true;
assert.equal(firstChild, "</b>");

selfClosing

✅ 展开

type: boolean
是否自封闭。在 MiniBrowser 版本中为只读属性。

// selfClosing
var {firstChild} = Parser.parse("<br/>");
assert.equal(firstChild, "<br/>");
assert.ok(firstChild.selfClosing);
// selfClosing (main)
var {firstChild} = Parser.parse("<br/>");
firstChild.selfClosing = false;
assert.equal(firstChild, "<br>");

Methods

lint

✅ 展开

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

// lint
var [h1, li, i, big, i2, wbr] = Parser.parse(`<h1/></li/><i></big><i>
{|</wbr>
|}`).querySelectorAll("html");
assert.equal(h1, "<h1/>");
assert.equal(li, "</li/>");
assert.equal(i, "<i>");
assert.equal(big, "</big>");
assert.equal(i2, "<i>");
assert.equal(wbr, "</wbr>");
assert.deepStrictEqual(h1.lint(), [
	{
		rule: "h1",
		severity: "warning",
		message: "<h1>",
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
		suggestions: [
			{
				desc: "h2",
				range: [2, 3],
				text: "2",
			},
		],
	},
	{
		rule: "unmatched-tag",
		severity: "error",
		message: "invalid self-closing tag",
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
		suggestions: [
			{
				desc: "no self-closing",
				range: [3, 4],
				text: "",
			},
			{
				desc: "close",
				range: [3, 5],
				text: "></h1>",
			},
		],
	},
]);
assert.deepStrictEqual(li.lint(), [
	{
		rule: "unmatched-tag",
		severity: "error",
		message: "tag that is both closing and self-closing",
		startLine: 0,
		startCol: 5,
		startIndex: 5,
		endLine: 0,
		endCol: 11,
		endIndex: 11,
		suggestions: [
			{
				desc: "open",
				range: [6, 7],
				text: "",
			},
			{
				desc: "no self-closing",
				range: [9, 10],
				text: "",
			},
		],
	},
]);
assert.deepStrictEqual(i.lint(), [
	{
		rule: "unmatched-tag",
		severity: "warning",
		message: "unclosed tag",
		startLine: 0,
		startCol: 11,
		startIndex: 11,
		endLine: 0,
		endCol: 14,
		endIndex: 14,
	},
]);
assert.deepStrictEqual(big.lint(), [
	{
		rule: "obsolete-tag",
		severity: "warning",
		message: "obsolete HTML tag",
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 20,
		endIndex: 20,
	},
	{
		rule: "unmatched-tag",
		severity: "warning",
		message: "unmatched closing tag",
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 20,
		endIndex: 20,
		suggestions: [
			{
				desc: "remove",
				range: [14, 20],
				text: "",
			},
		],
	},
]);
assert.deepStrictEqual(i2.lint(), [
	{
		rule: "unmatched-tag",
		severity: "warning",
		message: "unclosed tag",
		startLine: 0,
		startCol: 20,
		startIndex: 20,
		endLine: 0,
		endCol: 23,
		endIndex: 23,
		suggestions: [
			{
				desc: "close",
				range: [21, 21],
				text: "/",
			},
		],
	},
]);
assert.deepStrictEqual(wbr.lint(), [
	{
		rule: "parsing-order",
		severity: "error",
		message: "HTML tag in table attributes",
		startLine: 1,
		startCol: 2,
		startIndex: 26,
		endLine: 1,
		endCol: 8,
		endIndex: 32,
		suggestions: [
			{
				desc: "remove",
				range: [26, 32],
				text: "",
			},
		],
	},
	{
		rule: "unmatched-tag",
		severity: "error",
		message: "tag that is both closing and self-closing",
		startLine: 1,
		startCol: 2,
		startIndex: 26,
		endLine: 1,
		endCol: 8,
		endIndex: 32,
		fix: {
			desc: "open",
			range: [27, 28],
			text: "",
		},
	},
]);

findMatchingTag

✅ 展开

returns: this
搜索匹配的标签。

// findMatchingTag
var {firstChild, lastChild} = Parser.parse("<p></p>");
assert.equal(firstChild, "<p>");
assert.equal(lastChild, "</p>");
assert.strictEqual(firstChild.findMatchingTag(), lastChild);
assert.strictEqual(lastChild.findMatchingTag(), firstChild);

json

🌐 展开

将语法树保存为 JSON。

// json (print)
var {lastChild} = Parser.parse(" <p>");
assert.deepStrictEqual(lastChild.json(), {
	range: [1, 4],
	type: "html",
	name: "p",
	closing: false,
	selfClosing: false,
	childNodes: [
		{
			range: [3, 3],
			type: "html-attrs",
			name: "p",
			childNodes: [],
		},
	],
});

cloneNode

展开

returns: this
深拷贝节点。

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

replaceTag

展开

param: string 标签名
更换标签名。

// replaceTag (main)
var {firstChild} = Parser.parse("<b>");
assert.equal(firstChild, "<b>");
firstChild.replaceTag("i");
assert.equal(firstChild, "<i>");

fix

展开

修复无效自封闭标签。

// fix (main)
var {lastChild} = Parser.parse("<b><b/>");
assert.equal(lastChild, "<b/>");
lastChild.fix();
assert.equal(lastChild, "</b>");

getRange

展开

加入的版本:1.23.0

returns: AstRange | undefined
获取HTML标签对的范围。

// getRange (main)
var root = Parser.parse("<b>x</b>"),
	{lastChild} = root,
	range = lastChild.getRange();
assert.equal(lastChild, "</b>");
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 2);

toHtml

展开

加入的版本:1.10.0

returns: string
转换为 HTML。

// toHtml (main)
var {firstChild} = Parser.parse("</wbr id=a>");
assert.strictEqual(firstChild.toHtml(), "");
({firstChild} = Parser.parse("</br id=a>"));
assert.strictEqual(firstChild.toHtml(), "<br>");
({firstChild} = Parser.parse("<p id=a />"));
assert.strictEqual(firstChild.toHtml(), '<p id="a">');
({firstChild} = Parser.parse("<li id=a />"));
assert.strictEqual(firstChild.toHtml(), '<li id="a"></li>');
⚠️ **GitHub.com Fallback** ⚠️