Skip to content

HtmlToken (EN)

bhsd edited this page Jun 26, 2024 · 13 revisions
Table of Contents

Other Languages

Introduction

HTML tags, not matched. This class also includes the methods of the AttributesToken class.

✅ Available in the Mini and Browser versions.

Properties

name

✅ Expand

type: string
The name of the tag in lowercase, read-only.

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

closing

✅ Expand

type: boolean
Whether the tag is a closing tag.

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

selfClosing

✅ Expand

type: boolean
Whether the tag is self-closing.

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

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var {childNodes: [h1, br, i, big]} = Parser.parse(`<h1/></br/><i></big>`);
assert.equal(h1, '<h1/>');
assert.equal(br, '</br/>');
assert.equal(i, '<i>');
assert.equal(big, '</big>');
assert.deepStrictEqual(h1.lint(), [
	{
		rule: 'h1',
		severity: 'error',
		message: '<h1>',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
	{
		rule: 'unmatched-tag',
		severity: 'error',
		message: 'invalid self-closing tag',
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 5,
		endIndex: 5,
	},
]);
assert.deepStrictEqual(br.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,
		fix: {
			range: [6, 7],
			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: 'unmatched-tag',
		severity: 'error',
		message: 'unmatched closing tag',
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 20,
		endIndex: 20,
		suggestions: [
			{
				desc: 'remove',
				range: [14, 20],
				text: '',
			},
		],
	},
	{
		rule: 'obsolete-tag',
		severity: 'warning',
		message: 'obsolete HTML tag',
		startLine: 0,
		startCol: 14,
		startIndex: 14,
		endLine: 0,
		endCol: 20,
		endIndex: 20,
	},
]);

findMatchingTag

✅ Expand

returns: this
Find the matching tag.

// 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);

cloneNode

Expand

returns: this
Deep clone the node.

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

replaceTag

Expand

param: string new tag name
Change the tag name.

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

fix

Expand

Fix invalid self-closing tags.

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

toHtml

Expand

version added: 1.10.0

returns: string
Convert to 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>');
Clone this wiki locally