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

    `<img src="some.jpg" width="zzz" height="zzz" border="0" style="display:block;" alt="zzz"/>`

§ 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


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

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 Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 html-crush 3.0.3
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 stristri 2.0.0
Extracts or deletes HTML, CSS, text and/or templating tags from string
📦 string-strip-html 7.0.3
Strips HTML tags from strings. No parser, accepts mixed sources
📦 detect-templating-language 1.1.1
Detects various templating languages present in string
📦 html-table-patcher 3.0.5
Visual helper to place templating code around table tags into correct places
📦 is-html-tag-opening 1.9.0
Is given opening bracket a beginning of a tag?