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

backOpen Source→detect-is-it-html-or-xhtml

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

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

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • RETIREMENT
  • PURPOSE
  • API — DETECTISITHTM…
  • API — VERSION
  • UNDER THE HO…
  • Changelog

This package is retired. The published package, documentation, examples, and changelog remain available.

Retirement

This package has been retired and is no longer maintained.

The API documentation below is archived. Archived examples, the changelog, and the published package on npmopens in a new tab remain available.

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.

Permalink to changelogChangelog

Open Changelog
↑ back to top
back 

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