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

§ Quick Take

import { strict as assert } from "assert";
import isAttrClosing from "dist/is-html-attribute-closing.esm";

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

§ Examples

§ Purpose

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

isAttrClosing(str, idxOfAttrOpening, isThisClosingIdx)

In other words, function which takes three arguments:

Input argumentKey value's typeObligatory?Description
strStringyesThe input string of zero or more characters
idxOfAttrOpeningNatural number or zeroyesIndex of an opening quote of an attribute
isThisClosingIdxNatural numberyesIndex 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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 codsen-tokenizer 2.17.7
HTML and CSS lexer aimed at code with fatal errors, accepts mixed coding languages
📦 codsen-parser 0.7.7
Parser aiming at broken or mixed code, especially HTML & CSS
📦 emlint 2.18.17
Pluggable email template code linter
📦 html-crush 2.0.9
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 string-strip-html 6.0.4
Strips HTML tags from strings. No parser, accepts mixed sources.
📦 detect-is-it-html-or-xhtml 3.10.0
Answers, is the string input string more an HTML or XHTML (or neither)
📦 html-table-patcher 2.0.12
Visual helper to place templating code around table tags into correct places