No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- Broken #1
- Broken #2
- Broken #3
- Return false for unusable source indexes
- Recover when spaces replace attribute equals signs
- Recover when an attribute value has no opening quote
- Distinguish quotes inside a value from its closing quote
- Detect the end of a single-quoted attribute value
- Ignore quotes that belong to templating code inside a value
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 — isAttrClosing()
The main function isAttrClosing() is imported like this:
It’s a function which takes three input arguments:
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | The input string of zero or more characters |
idxOfAttrOpeningType: Natural number or zero Obligatory: yes | |||
idxOfAttrOpening | Natural number or zero | yes | Index of an opening quote of an attribute |
isThisClosingIdxType: Natural number Obligatory: yes | |||
isThisClosingIdx | Natural number | yes | Index you ask this program to evaluate, is it a closing quote |
The function returns a boolean.
Attribute names and string indices
Following attribute names can contain underscores, periods, other permitted punctuation, and Unicode characters, including supplementary characters such as emoji. The helper uses these names to distinguish the current attribute’s closing quote from a later attribute’s quote.
Both indices are UTF-16 offsets, as returned by JavaScript’s indexOf().
A supplementary character occupies two code units; do not count it as one
when calculating an index.
const html = '<div foo="text" foo😀="next">';
isAttrClosing(html, 9, 14); // true: closes foo
isAttrClosing(html, 9, 27); // false: closes foo😀
When an equals sign is missing, the helper uses additional recovery heuristics. Punctuation inside a broken value is weaker evidence than a recognized attribute name. This function identifies likely boundaries; it does not validate an entire attribute name or HTML document.
If anything is wrong with the input arguments, the program returns false. It never throws.
API — version
You can import version: