Is a character on a given index a closing of an HTML attribute?

Quick Take

import { strict as assert } from "assert";
import { isAttrClosing } from "is-html-attribute-closing";

const str = `<a href="zzz" target="_blank" style="color: black;">`;

// <a href="zzz" target="_blank" ...
//                      ^
//                  index 21

// <a href="zzz" target="_blank" ...
//                             ^
//                         index 28

    str, // reference string
    21, // known opening (or in absence of a quote, a start of a value)
    28 // we ask, is this a closing on the attribute?
  true // the answer



This program detects, is a character at a given index in a given string being a closing of an attribute. In healthy code, that's normally a double quotes character.

This program is aimed at fixing seriously broken HTML code — missing closing quotes, mismatching closing quotes, swapped quotes and unencoded "content" quotes as a part of attribute's value.

It's driving the codsen-tokenizer which in turn powers codsen-parser which in turn powers emlint.

For healthy HTML code, however, finding the closing double quotes is a trivial task.

API - Input


In other words, function which takes three arguments:

Input argument Key value's type Obligatory? Description
str String yes The input string of zero or more characters
idxOfAttrOpening Natural number or zero yes Index of an opening quote of an attribute
isThisClosingIdx Natural number yes Index we ask program to evaluate, is it a closing quote

This program does not throw. It just returns false.

If anything is wrong with the input arguments, the program returns false. It never throws. That's because it's to be used inside other programs. Idea is, proper algorithms that will use this program will "care" only about the truthy case: does the given quote pass as a closing-one. Crappy input arguments yields false, happy days, consuming algorithms continue whatever dodgy journeys they have been making.

We don't throw errors in this program.

API - Output

Boolean, true or false. Erroneous input arguments will yield false as well.


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
📦 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
📦 detect-is-it-html-or-xhtml 5.0.1
Answers, is the string input string more an HTML or XHTML (or neither)