is-html-attribute-closing2.1.3
§ 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
assert.equal(
isAttrClosing(
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 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.
§ Changelog
See it in the monorepo , on GitHub.
§ Contributing
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
§ Licence
Copyright © 2010–2021 Roy Revelt and other contributors