Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→detect-is-it-html-or-xhtmlnext

detect-is-it-html-or-xhtml6.2.0

Does the string resemble an HTML or XHTML (or neither)?

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — DETECTISITHTM…
  • API — VERSION
  • UNDER THE HO…
  • Changelog

Installation

Quick Take

Examples

  • Detect HTML from its doctype
  • Prefer HTML when self-closing and open single tags are tied
  • Return null when the source contains no decisive markup
  • Infer XHTML when most single tags are self-closing
  • Detect XHTML from its doctype

Purpose

It is a function which detects, does given string resemble XHTML or HTML. Practically, it affects closing slashes and singleton tags in the markup.

API — detectIsItHTMLOrXhtml()

The main function detectIsItHTMLOrXhtml() is imported like this:

It’s a function which takes one argument:

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

API — version

You can import version:

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

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab