Is given opening bracket a beginning of a tag?

§ Quick Take

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

const isOpening = require("is-html-tag-opening");
const text = `<span>a < b<span>`;
console.log(is(text, 0)); // opening span tag's opening
// => true
console.log(is(text, 8)); // bracket between a and b
// => false
console.log(is(text, 11)); // closing span tag's opening
// => true

§ API - Input

isOpening(str, idx)

In other words, function which takes two arguments:

Input argumentKey value's typeObligatory?Description
strStringyesThe input string of zero or more characters
idxNatural number or zeroyesIndex of an opening bracket (<)

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

§ API - Output

Boolean, true or false.

§ Purpose

This program tries to tackle as much broken code cases as possible. It recognises various cases of unclosed or broken tags. For example:

< br/>
< br / >
a < b
let's say that a < b and c > d.
<a b="ccc"<d>

This program is also aware of all known HTML tags and will use that information in its algorithm.

But that comes at perf cost. In theory, same program but without broken code support would be String.indexOf("<"). If the source can't and won't ever contain unencoded raw brackets, if the source bracket pairs will always be closed, every opening bracket < IS a tag opening!

§ Licence

MITopens in a new tab

Copyright © 2015–2020 Roy Revelt and other contributors

Related packages:

📦 emlint 2.18.15
Pluggable email template code linter
📦 html-crush 2.0.8
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 string-strip-html 6.0.3
Strips HTML tags from strings. No parser, accepts mixed sources.
📦 detect-is-it-html-or-xhtml 3.9.61
Answers, is the string input string more an HTML or XHTML (or neither)
📦 html-table-patcher 2.0.11
Visual helper to place templating code around table tags into correct places
📦 is-html-attribute-closing 1.2.7
Is a character on a given index a closing of an HTML attribute?
📦 is-language-code 1.0.11
Is given string a language code (as per IANA)