or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

error-handling.mdhtml-utilities.mdindex.mdparsing.mdserialization.mdtokenization.mdtree-adapters.md

html-utilities.mddocs/

0

# HTML Constants and Utilities

1

2

HTML specification constants, enumerations, and utility functions that provide access to standardized HTML element names, namespace URIs, document modes, and other HTML5 specification details.

3

4

## Capabilities

5

6

### HTML Namespace

7

8

Complete HTML constants and utilities namespace providing access to all HTML specification details.

9

10

```typescript { .api }

11

/**

12

* HTML namespace containing constants and utilities

13

*/

14

namespace html {

15

// Core namespace and utility functions

16

}

17

```

18

19

### Namespace Constants

20

21

HTML namespace URIs defined by web standards.

22

23

```typescript { .api }

24

/**

25

* Namespace URI constants

26

*/

27

enum NS {

28

/** HTML namespace URI */

29

HTML = 'http://www.w3.org/1999/xhtml',

30

31

/** MathML namespace URI */

32

MATHML = 'http://www.w3.org/1998/Math/MathML',

33

34

/** SVG namespace URI */

35

SVG = 'http://www.w3.org/2000/svg',

36

37

/** XLink namespace URI */

38

XLINK = 'http://www.w3.org/1999/xlink',

39

40

/** XML namespace URI */

41

XML = 'http://www.w3.org/XML/1998/namespace',

42

43

/** XMLNS namespace URI */

44

XMLNS = 'http://www.w3.org/2000/xmlns/'

45

}

46

```

47

48

**Usage Examples:**

49

50

```typescript

51

import { html } from "parse5";

52

53

// Check element namespace

54

function isHTMLElement(element: Element): boolean {

55

return element.namespaceURI === html.NS.HTML;

56

}

57

58

function isSVGElement(element: Element): boolean {

59

return element.namespaceURI === html.NS.SVG;

60

}

61

62

function isMathMLElement(element: Element): boolean {

63

return element.namespaceURI === html.NS.MATHML;

64

}

65

```

66

67

### HTML Tag Names

68

69

Standardized HTML tag name constants.

70

71

```typescript { .api }

72

/**

73

* HTML tag name constants

74

*/

75

enum TAG_NAMES {

76

A = 'a',

77

ADDRESS = 'address',

78

ANNOTATION_XML = 'annotation-xml',

79

APPLET = 'applet',

80

AREA = 'area',

81

ARTICLE = 'article',

82

ASIDE = 'aside',

83

B = 'b',

84

BASE = 'base',

85

BASEFONT = 'basefont',

86

BGSOUND = 'bgsound',

87

BIG = 'big',

88

BLOCKQUOTE = 'blockquote',

89

BODY = 'body',

90

BR = 'br',

91

BUTTON = 'button',

92

CAPTION = 'caption',

93

CENTER = 'center',

94

CODE = 'code',

95

COL = 'col',

96

COLGROUP = 'colgroup',

97

DD = 'dd',

98

DESC = 'desc',

99

DETAILS = 'details',

100

DIALOG = 'dialog',

101

DIR = 'dir',

102

DIV = 'div',

103

DL = 'dl',

104

DT = 'dt',

105

EM = 'em',

106

EMBED = 'embed',

107

FIELDSET = 'fieldset',

108

FIGCAPTION = 'figcaption',

109

FIGURE = 'figure',

110

FONT = 'font',

111

FOOTER = 'footer',

112

FOREIGN_OBJECT = 'foreignObject',

113

FORM = 'form',

114

FRAME = 'frame',

115

FRAMESET = 'frameset',

116

H1 = 'h1',

117

H2 = 'h2',

118

H3 = 'h3',

119

H4 = 'h4',

120

H5 = 'h5',

121

H6 = 'h6',

122

HEAD = 'head',

123

HEADER = 'header',

124

HGROUP = 'hgroup',

125

HR = 'hr',

126

HTML = 'html',

127

I = 'i',

128

IMG = 'img',

129

IMAGE = 'image',

130

INPUT = 'input',

131

IFRAME = 'iframe',

132

KEYGEN = 'keygen',

133

LABEL = 'label',

134

LI = 'li',

135

LINK = 'link',

136

LISTING = 'listing',

137

MAIN = 'main',

138

MALIGNMARK = 'malignmark',

139

MARQUEE = 'marquee',

140

MATH = 'math',

141

MENU = 'menu',

142

META = 'meta',

143

MGLYPH = 'mglyph',

144

MI = 'mi',

145

MO = 'mo',

146

MN = 'mn',

147

MS = 'ms',

148

MTEXT = 'mtext',

149

NAV = 'nav',

150

NOBR = 'nobr',

151

NOFRAMES = 'noframes',

152

NOEMBED = 'noembed',

153

NOSCRIPT = 'noscript',

154

OBJECT = 'object',

155

OL = 'ol',

156

OPTGROUP = 'optgroup',

157

OPTION = 'option',

158

P = 'p',

159

PARAM = 'param',

160

PICTURE = 'picture',

161

PLAINTEXT = 'plaintext',

162

PRE = 'pre',

163

RB = 'rb',

164

RP = 'rp',

165

RT = 'rt',

166

RTC = 'rtc',

167

RUBY = 'ruby',

168

S = 's',

169

SCRIPT = 'script',

170

SEARCH = 'search',

171

SECTION = 'section',

172

SELECT = 'select',

173

SOURCE = 'source',

174

SMALL = 'small',

175

SPAN = 'span',

176

STRIKE = 'strike',

177

STRONG = 'strong',

178

STYLE = 'style',

179

SUB = 'sub',

180

SUMMARY = 'summary',

181

SUP = 'sup',

182

TABLE = 'table',

183

TBODY = 'tbody',

184

TEMPLATE = 'template',

185

TEXTAREA = 'textarea',

186

TFOOT = 'tfoot',

187

TD = 'td',

188

TH = 'th',

189

THEAD = 'thead',

190

TITLE = 'title',

191

TR = 'tr',

192

TRACK = 'track',

193

TT = 'tt',

194

U = 'u',

195

UL = 'ul',

196

SVG = 'svg',

197

VAR = 'var',

198

WBR = 'wbr',

199

XMP = 'xmp'

200

}

201

```

202

203

### HTML Tag IDs

204

205

Numerical tag identifiers for efficient tag comparison.

206

207

```typescript { .api }

208

/**

209

* HTML tag ID enumeration for efficient comparison

210

*/

211

enum TAG_ID {

212

UNKNOWN = 0,

213

A = 1,

214

ADDRESS = 2,

215

ANNOTATION_XML = 3,

216

APPLET = 4,

217

AREA = 5,

218

ARTICLE = 6,

219

ASIDE = 7,

220

B = 8,

221

BASE = 9,

222

BASEFONT = 10,

223

BGSOUND = 11,

224

BIG = 12,

225

BLOCKQUOTE = 13,

226

BODY = 14,

227

BR = 15,

228

BUTTON = 16,

229

CAPTION = 17,

230

CENTER = 18,

231

CODE = 19,

232

COL = 20,

233

COLGROUP = 21,

234

DD = 22,

235

DESC = 23,

236

DETAILS = 24,

237

DIALOG = 25,

238

DIR = 26,

239

DIV = 27,

240

DL = 28,

241

DT = 29,

242

EM = 30,

243

EMBED = 31,

244

FIELDSET = 32,

245

FIGCAPTION = 33,

246

FIGURE = 34,

247

FONT = 35,

248

FOOTER = 36,

249

FOREIGN_OBJECT = 37,

250

FORM = 38,

251

FRAME = 39,

252

FRAMESET = 40,

253

H1 = 41,

254

H2 = 42,

255

H3 = 43,

256

H4 = 44,

257

H5 = 45,

258

H6 = 46,

259

HEAD = 47,

260

HEADER = 48,

261

HGROUP = 49,

262

HR = 50,

263

HTML = 51,

264

I = 52,

265

IMG = 53,

266

IMAGE = 54,

267

INPUT = 55,

268

IFRAME = 56,

269

KEYGEN = 57,

270

LABEL = 58,

271

LI = 59,

272

LINK = 60,

273

LISTING = 61,

274

MAIN = 62,

275

MALIGNMARK = 63,

276

MARQUEE = 64,

277

MATH = 65,

278

MENU = 66,

279

META = 67,

280

MGLYPH = 68,

281

MI = 69,

282

MO = 70,

283

MN = 71,

284

MS = 72,

285

MTEXT = 73,

286

NAV = 74,

287

NOBR = 75,

288

NOFRAMES = 76,

289

NOEMBED = 77,

290

NOSCRIPT = 78,

291

OBJECT = 79,

292

OL = 80,

293

OPTGROUP = 81,

294

OPTION = 82,

295

P = 83,

296

PARAM = 84,

297

PICTURE = 85,

298

PLAINTEXT = 86,

299

PRE = 87,

300

RB = 88,

301

RP = 89,

302

RT = 90,

303

RTC = 91,

304

RUBY = 92,

305

S = 93,

306

SCRIPT = 94,

307

SEARCH = 95,

308

SECTION = 96,

309

SELECT = 97,

310

SOURCE = 98,

311

SMALL = 99,

312

SPAN = 100,

313

STRIKE = 101,

314

STRONG = 102,

315

STYLE = 103,

316

SUB = 104,

317

SUMMARY = 105,

318

SUP = 106,

319

TABLE = 107,

320

TBODY = 108,

321

TEMPLATE = 109,

322

TEXTAREA = 110,

323

TFOOT = 111,

324

TD = 112,

325

TH = 113,

326

THEAD = 114,

327

TITLE = 115,

328

TR = 116,

329

TRACK = 117,

330

TT = 118,

331

U = 119,

332

UL = 120,

333

SVG = 121,

334

VAR = 122,

335

WBR = 123,

336

XMP = 124

337

}

338

```

339

340

### Document Mode Constants

341

342

HTML document mode enumeration for quirks mode handling.

343

344

```typescript { .api }

345

/**

346

* Document mode constants for quirks mode handling

347

*/

348

enum DOCUMENT_MODE {

349

/** Standards mode (no quirks) */

350

NO_QUIRKS = 'no-quirks',

351

352

/** Quirks mode */

353

QUIRKS = 'quirks',

354

355

/** Limited quirks mode */

356

LIMITED_QUIRKS = 'limited-quirks'

357

}

358

```

359

360

### HTML Attributes

361

362

Common HTML attribute name constants.

363

364

```typescript { .api }

365

/**

366

* Common HTML attribute name constants

367

*/

368

enum ATTRS {

369

TYPE = 'type',

370

ACTION = 'action',

371

ENCODING = 'encoding',

372

PROMPT = 'prompt',

373

NAME = 'name',

374

COLOR = 'color',

375

FACE = 'face',

376

SIZE = 'size',

377

CLASS = 'class',

378

ID = 'id',

379

STYLE = 'style',

380

LANG = 'lang',

381

DIR = 'dir',

382

TITLE = 'title',

383

ALT = 'alt',

384

SRC = 'src',

385

HREF = 'href',

386

REL = 'rel',

387

MEDIA = 'media',

388

CHARSET = 'charset',

389

CONTENT = 'content',

390

HTTP_EQUIV = 'http-equiv',

391

VALUE = 'value',

392

CHECKED = 'checked',

393

SELECTED = 'selected',

394

DISABLED = 'disabled',

395

READONLY = 'readonly',

396

MULTIPLE = 'multiple',

397

HIDDEN = 'hidden',

398

REQUIRED = 'required',

399

AUTOFOCUS = 'autofocus',

400

PLACEHOLDER = 'placeholder',

401

PATTERN = 'pattern',

402

MIN = 'min',

403

MAX = 'max',

404

STEP = 'step',

405

MAXLENGTH = 'maxlength',

406

MINLENGTH = 'minlength',

407

COLS = 'cols',

408

ROWS = 'rows',

409

WRAP = 'wrap',

410

ACCEPT = 'accept',

411

ACCEPT_CHARSET = 'accept-charset',

412

ACCESSKEY = 'accesskey',

413

CONTENTEDITABLE = 'contenteditable',

414

CONTEXTMENU = 'contextmenu',

415

CONTROLS = 'controls',

416

COORDS = 'coords',

417

CROSSORIGIN = 'crossorigin',

418

DATA = 'data',

419

DATETIME = 'datetime',

420

DEFAULT = 'default',

421

DEFER = 'defer',

422

DRAGGABLE = 'draggable',

423

DROPZONE = 'dropzone',

424

ENCTYPE = 'enctype',

425

FOR = 'for',

426

FORM = 'form',

427

FORMACTION = 'formaction',

428

FORMENCTYPE = 'formenctype',

429

FORMMETHOD = 'formmethod',

430

FORMNOVALIDATE = 'formnovalidate',

431

FORMTARGET = 'formtarget',

432

FRAMEBORDER = 'frameborder',

433

HEADERS = 'headers',

434

HEIGHT = 'height',

435

HIGH = 'high',

436

HREFLANG = 'hreflang',

437

ICON = 'icon',

438

ISMAP = 'ismap',

439

ITEMID = 'itemid',

440

ITEMPROP = 'itemprop',

441

ITEMREF = 'itemref',

442

ITEMSCOPE = 'itemscope',

443

ITEMTYPE = 'itemtype',

444

KIND = 'kind',

445

LABEL = 'label',

446

LIST = 'list',

447

LOOP = 'loop',

448

LOW = 'low',

449

MANIFEST = 'manifest',

450

MARGINHEIGHT = 'marginheight',

451

MARGINWIDTH = 'marginwidth',

452

METHOD = 'method',

453

MUTED = 'muted',

454

NOVALIDATE = 'novalidate',

455

OPEN = 'open',

456

OPTIMUM = 'optimum',

457

PING = 'ping',

458

POSTER = 'poster',

459

PRELOAD = 'preload',

460

RADIOGROUP = 'radiogroup',

461

REVERSED = 'reversed',

462

ROLE = 'role',

463

SANDBOX = 'sandbox',

464

SCOPE = 'scope',

465

SCOPED = 'scoped',

466

SEAMLESS = 'seamless',

467

SHAPE = 'shape',

468

SIZES = 'sizes',

469

SPAN = 'span',

470

SPELLCHECK = 'spellcheck',

471

SRCDOC = 'srcdoc',

472

SRCLANG = 'srclang',

473

SRCSET = 'srcset',

474

START = 'start',

475

TABINDEX = 'tabindex',

476

TARGET = 'target',

477

TRANSLATE = 'translate',

478

USEMAP = 'usemap',

479

WIDTH = 'width'

480

}

481

```

482

483

### HTML Utility Functions

484

485

Utility functions for HTML processing and tag identification.

486

487

```typescript { .api }

488

/**

489

* Get numeric tag ID from tag name string

490

* @param tagName - HTML tag name

491

* @returns Corresponding TAG_ID or TAG_ID.UNKNOWN

492

*/

493

function getTagID(tagName: string): TAG_ID;

494

495

/**

496

* Check if a tag contains unescaped text content

497

* @param tagName - HTML tag name

498

* @param scriptingEnabled - Whether scripting is enabled

499

* @returns True if tag contains unescaped text

500

*/

501

function hasUnescapedText(tagName: string, scriptingEnabled: boolean): boolean;

502

```

503

504

**Usage Examples:**

505

506

```typescript

507

import { html } from "parse5";

508

509

// Get tag ID for efficient comparison

510

const divTagId = html.getTagID('div');

511

const spanTagId = html.getTagID('span');

512

513

if (divTagId === html.TAG_ID.DIV) {

514

console.log('Found div tag');

515

}

516

517

// Check if tag has unescaped text content

518

const hasUnescaped = html.hasUnescapedText('script', true);

519

console.log('Script has unescaped text:', hasUnescaped); // true

520

521

// Use tag name constants

522

function isHeadingTag(tagName: string): boolean {

523

return [

524

html.TAG_NAMES.H1,

525

html.TAG_NAMES.H2,

526

html.TAG_NAMES.H3,

527

html.TAG_NAMES.H4,

528

html.TAG_NAMES.H5,

529

html.TAG_NAMES.H6

530

].includes(tagName as any);

531

}

532

533

// Use attribute constants

534

function hasRequiredClass(element: Element): boolean {

535

const className = element.getAttribute(html.ATTRS.CLASS);

536

return className?.includes('required') ?? false;

537

}

538

539

// Use namespace constants

540

function createElement(tagName: string, namespace = html.NS.HTML): Element {

541

return document.createElementNS(namespace, tagName);

542

}

543

```

544

545

## HTML Specification Compliance Patterns

546

547

### Tag Validation

548

549

```typescript

550

import { html } from "parse5";

551

552

class HTMLValidator {

553

private voidElements = new Set([

554

html.TAG_ID.AREA, html.TAG_ID.BASE, html.TAG_ID.BR,

555

html.TAG_ID.COL, html.TAG_ID.EMBED, html.TAG_ID.HR,

556

html.TAG_ID.IMG, html.TAG_ID.INPUT, html.TAG_ID.LINK,

557

html.TAG_ID.META, html.TAG_ID.PARAM, html.TAG_ID.SOURCE,

558

html.TAG_ID.TRACK, html.TAG_ID.WBR

559

]);

560

561

isVoidElement(tagName: string): boolean {

562

const tagId = html.getTagID(tagName);

563

return this.voidElements.has(tagId);

564

}

565

566

isBlockElement(tagName: string): boolean {

567

const tagId = html.getTagID(tagName);

568

const blockElements = new Set([

569

html.TAG_ID.DIV, html.TAG_ID.P, html.TAG_ID.H1,

570

html.TAG_ID.H2, html.TAG_ID.H3, html.TAG_ID.H4,

571

html.TAG_ID.H5, html.TAG_ID.H6, html.TAG_ID.HEADER,

572

html.TAG_ID.FOOTER, html.TAG_ID.SECTION, html.TAG_ID.ARTICLE,

573

html.TAG_ID.ASIDE, html.TAG_ID.NAV, html.TAG_ID.MAIN

574

]);

575

return blockElements.has(tagId);

576

}

577

578

isInlineElement(tagName: string): boolean {

579

const tagId = html.getTagID(tagName);

580

const inlineElements = new Set([

581

html.TAG_ID.SPAN, html.TAG_ID.A, html.TAG_ID.STRONG,

582

html.TAG_ID.EM, html.TAG_ID.CODE, html.TAG_ID.B,

583

html.TAG_ID.I, html.TAG_ID.U, html.TAG_ID.S

584

]);

585

return inlineElements.has(tagId);

586

}

587

}

588

```

589

590

### Namespace-Aware Element Creation

591

592

```typescript

593

import { html } from "parse5";

594

595

class NamespaceAwareBuilder {

596

createElement(tagName: string, namespace?: string): Element {

597

// Determine namespace based on tag name if not provided

598

if (!namespace) {

599

if (tagName === html.TAG_NAMES.SVG) {

600

namespace = html.NS.SVG;

601

} else if (tagName === html.TAG_NAMES.MATH) {

602

namespace = html.NS.MATHML;

603

} else {

604

namespace = html.NS.HTML;

605

}

606

}

607

608

return document.createElementNS(namespace, tagName);

609

}

610

611

createSVGElement(tagName: string): Element {

612

return this.createElement(tagName, html.NS.SVG);

613

}

614

615

createMathMLElement(tagName: string): Element {

616

return this.createElement(tagName, html.NS.MATHML);

617

}

618

619

isForeignElement(element: Element): boolean {

620

return element.namespaceURI !== html.NS.HTML;

621

}

622

}

623

```

624

625

### Document Mode Detection

626

627

```typescript

628

import { html } from "parse5";

629

630

class DocumentModeDetector {

631

detectMode(doctype: string | null): html.DOCUMENT_MODE {

632

if (!doctype) {

633

return html.DOCUMENT_MODE.QUIRKS;

634

}

635

636

const doctypeLower = doctype.toLowerCase();

637

638

// HTML5 doctype

639

if (doctypeLower === '<!doctype html>') {

640

return html.DOCUMENT_MODE.NO_QUIRKS;

641

}

642

643

// Legacy doctypes that trigger quirks mode

644

const quirksPatterns = [

645

'html 4.01 transitional',

646

'html 4.01 frameset',

647

'xhtml 1.0 transitional',

648

'xhtml 1.0 frameset'

649

];

650

651

if (quirksPatterns.some(pattern => doctypeLower.includes(pattern))) {

652

return html.DOCUMENT_MODE.LIMITED_QUIRKS;

653

}

654

655

// Default to no-quirks for unrecognized doctypes

656

return html.DOCUMENT_MODE.NO_QUIRKS;

657

}

658

659

applyQuirksMode(mode: html.DOCUMENT_MODE): void {

660

switch (mode) {

661

case html.DOCUMENT_MODE.QUIRKS:

662

console.log('Applying full quirks mode');

663

break;

664

case html.DOCUMENT_MODE.LIMITED_QUIRKS:

665

console.log('Applying limited quirks mode');

666

break;

667

case html.DOCUMENT_MODE.NO_QUIRKS:

668

console.log('Applying standards mode');

669

break;

670

}

671

}

672

}

673

```

674

675

### Attribute Processing

676

677

```typescript

678

import { html } from "parse5";

679

680

class AttributeProcessor {

681

private booleanAttributes = new Set([

682

html.ATTRS.CHECKED, html.ATTRS.SELECTED, html.ATTRS.DISABLED,

683

html.ATTRS.READONLY, html.ATTRS.MULTIPLE, html.ATTRS.HIDDEN,

684

html.ATTRS.REQUIRED, html.ATTRS.AUTOFOCUS, html.ATTRS.DEFER,

685

html.ATTRS.ASYNC, html.ATTRS.CONTROLS, html.ATTRS.LOOP,

686

html.ATTRS.MUTED, html.ATTRS.OPEN, html.ATTRS.REVERSED

687

]);

688

689

isBooleanAttribute(name: string): boolean {

690

return this.booleanAttributes.has(name as any);

691

}

692

693

normalizeAttributeValue(name: string, value: string): string | boolean {

694

if (this.isBooleanAttribute(name)) {

695

// Boolean attributes: presence = true, absence = false

696

return value !== null;

697

}

698

699

// Standard attributes

700

return value;

701

}

702

703

validateAttribute(tagName: string, attrName: string, attrValue: string): boolean {

704

const tagId = html.getTagID(tagName);

705

706

// Example validation rules

707

switch (tagId) {

708

case html.TAG_ID.IMG:

709

if (attrName === html.ATTRS.SRC) {

710

return attrValue.length > 0;

711

}

712

if (attrName === html.ATTRS.ALT) {

713

return true; // Alt can be empty

714

}

715

break;

716

717

case html.TAG_ID.A:

718

if (attrName === html.ATTRS.HREF) {

719

return attrValue.length > 0;

720

}

721

break;

722

723

case html.TAG_ID.INPUT:

724

if (attrName === html.ATTRS.TYPE) {

725

const validTypes = ['text', 'password', 'email', 'number', 'checkbox', 'radio', 'submit', 'button'];

726

return validTypes.includes(attrValue);

727

}

728

break;

729

}

730

731

return true; // Default to valid

732

}

733

}

734

```