Quick Take

import { strict as assert } from "assert";
import { isOpening } from "is-html-tag-opening";

const text = `<span>a < b<span>`;

// opening span tag's opening
assert.equal(isOpening(text, 0), true);

// unencoded bracket between a and b
assert.equal(isOpening(text, 8), false);

// closing span tag's opening
assert.equal(isOpening(text, 11), true);



Detect, is an opening bracket (<) a tag opening? Otherwise, it might be an un-encoded text.

API - Input

isOpening(str, idx)

In other words, function which takes two arguments:

Input argument Key value's type Obligatory? Description
str String yes The input string of zero or more characters
idx Natural number or zero yes Index of an opening bracket (<)
opts falsy or Plain Object no Optional Options Object

If supplied input arguments are of any other types, an error will be thrown.

Optional Options Object

Options Object's key Type of its value Default Description
allowCustomTagNames boolean false Program is aware of all known HTML tag names and by default will exclude what it can't recognise. You can turn off that behaviour and make it rely on bracket/attribute patterns only.
skipOpeningBracket boolean false By default, algorithm expects that idx is on a bracket <. If you enable it, you can start from idx at the first tag's character.

API - Output

It returns a boolean.

In Practice

This program allows us to tackle any raw unencoded brackets in HTML code. It will drive codsen-tokenizer which in turn will drive codsen-parser which in turn will drive emlint.


See it in the monorepo opens in a new tab, on GitHub.


To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 codsen-tokenizer 6.0.1
HTML and CSS lexer aimed at code with fatal errors, accepts mixed coding languages
📦 codsen-parser 0.12.1
Parser aiming at broken or mixed code, especially HTML & CSS
📦 emlint 5.0.1
Pluggable email template code linter
📦 is-html-attribute-closing 3.0.1
Is a character on a given index a closing of an HTML attribute?
📦 html-crush 5.0.1
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 stristri 4.0.1
Extracts or deletes HTML, CSS, text and/or templating tags from string
📦 string-strip-html 9.0.1
Strips HTML tags from strings. No parser, accepts mixed sources