is-html-tag-opening examples

Table of Contents

Quick Take

import { strict as assert } from "assert";
import { isOpening } from "is-html-tag-opening";

const text = `<span>a < b<span>`;

// opening span tag's opening
assert.equal(isOpening(text, 0), true);

// unencoded bracket between a and b
assert.equal(isOpening(text, 8), false);

// closing span tag's opening
assert.equal(isOpening(text, 11), true);

opts.allowCustomTagNames

import { strict as assert } from "assert";
import { isOpening } from "is-html-tag-opening";

// recognised tag
assert.equal(isOpening(`abc <div>`, 4), true);
//                          ^

// unrecognised tag
assert.equal(isOpening(`abc <zzz>`, 4), false);
//                          ^

// but with opts.allowCustomTagNames result is "true"
assert.equal(
  isOpening(`abc <zzz>`, 4, {
    allowCustomTagNames: true,
  }),
  true
);

opts.skipOpeningBracket

import { strict as assert } from "assert";
import { isOpening } from "is-html-tag-opening";

// missing opening bracket:
assert.equal(
  isOpening(`<a>div class="w-64 h-3">`, 3),
  false
);
//                         ^
// result is instant "false" because `idx` has to be
// on an opening bracket (besides checking for tag name
// being present and recognised and attributes being present)

// but with opts.skipOpeningBracket we remove the requirement
// that "idx" has to be on a bracket - algorithm detects valid
// tag name and the attribute that follows and recognise it's a
// tag starting:
assert.equal(
  isOpening(`<a>div class="w-64 h-3">`, 3, {
    skipOpeningBracket: true,
  }),
  true
);