detect-is-it-html-or-xhtml4.1.0

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

Type Value Description
String or null 'html', 'xhtml' or null Identified 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 GitHub.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 emlint 4.7.0
Pluggable email template code linter
📦 html-crush 4.2.0
Minifies HTML/CSS: valid or broken, pure or mixed with other languages
📦 stristri 3.2.0
Extracts or deletes HTML, CSS, text and/or templating tags from string
📦 string-strip-html 8.3.0
Strips HTML tags from strings. No parser, accepts mixed sources
📦 detect-templating-language 2.1.0
Detects various templating languages present in string
📦 html-table-patcher 4.1.0
Visual helper to place templating code around table tags into correct places
📦 is-html-tag-opening 2.1.0
Does an HTML tag start at given position?