is-html-attribute-closing examples

Table of Contents

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

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

Broken #1

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

assert.equal(
  isAttrClosing(
    `<a href="z' click here</a>`,
    //       ^ ^
    //       | \
    //       |  \_________________________________
    //       |                                   |
    8, // known opening                          |
    10 // is this an attribute closing at index 10?
  ),
  true
);
// => yes, indeed a closing of an attribute (not counting
// the code is broken)

Broken #2

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

assert.equal(
  isAttrClosing(
    `<a b = = = "c" d = = = 'e'>`,
    //          ^ ^
    //          | |
    //          | L_______________________________
    //          |                                |
    11, // known opening                         |
    13 // is this an attribute closing at index 13?
  ),
  true
);
// => true - indeed a closing of an attribute (not counting
// the code is broken)

Broken #3

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

// Correct code:
// <img class="so-called" alt="!" border='10'/>

// Broken code:
const str = `<img class="so-called "alt !' border 10'/>`;
//                      ^
//                known opening at idx 11

assert.equal(isAttrClosing(str, 11, 22), true);
// => true - indeed a closing of an attribute

assert.equal(isAttrClosing(str, 11, 28), false);
// => not a closing of an attribute

assert.equal(isAttrClosing(str, 11, 39), false);
// => not a closing of an attribute