detect-is-it-html-or-xhtml3.10.1

Answers, is the string input string more an HTML or XHTML (or neither)

§ Quick Take

import { strict as assert } from "assert";
import detectIsItHTMLOrXhtml from "detect-is-it-html-or-xhtml";

assert.equal(
  detectIsItHTMLOrXhtml(
    `<img src="some.jpg" width="zzz" height="zzz" border="0" style="display:block;" alt="zzz"/>`
  ),
  "xhtml"
);

§ Purpose

It is a function which detects, does given string resemble XHTML or HTML. The format, XHTML or HTML, affects closing slashes and singleton tags.

§ API - Input

detectIsItHTMLOrXhtml(str)

In other words, it's a function which takes one input argument, a string.

If the input is not String type, this package will throw an error. If the input is missing completely, it will return null.

§ API - Output

TypeValueDescription
String or null'html', 'xhtml' or nullIdentified type of your input

§ Under the hood

The algorithm is the following:

  1. Look for doctype. If recognised, Bob's your uncle, here's your answer.
  2. IF there's no doctype or it's messed up beyond recognition, DO scan all singleton tags (<img>, <br> and <hr>) and see which type the majority is (closed or not closed).
  3. In a rare case when there is an equal amount of both closed and unclosed tags, lean for html.
  4. If (there are no tags in the input) OR (there are no doctype tags and no singleton tags), return null.

§ Changelog

See it in the monorepo opens in a new tab, on GitLab.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 emlint 2.19.4
Pluggable email template code linter
📦 html-crush 2.0.10
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 string-strip-html 6.3.0
Strips HTML tags from strings. No parser, accepts mixed sources
📦 html-table-patcher 2.0.16
Visual helper to place templating code around table tags into correct places
📦 is-html-tag-opening 1.8.4
Is given opening bracket a beginning of a tag?
📦 is-html-attribute-closing 1.3.1
Is a character on a given index a closing of an HTML attribute?
📦 is-language-code 1.0.13
Is given string a language code (as per IANA)