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

§ Examples

§ Purpose

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 argumentKey value's typeObligatory?Description
strStringyesThe input string of zero or more characters
idxNatural number or zeroyesIndex of an opening bracket (<)
optsfalsy or Plain ObjectnoOptional Options Object

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

§ Optional Options Object

Options Object's keyType of its valueDefaultDescription
allowCustomTagNamesbooleanfalseProgram 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.
skipOpeningBracketbooleanfalseBy 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.

§ Changelog

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

§ Contributing

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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

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