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

prevOpen Source→charcode-is-valid-xml-name-characternext

charcode-is-valid-xml-name-character3.2.1

Does a given character belong to XML spec’s “Production 4 OR 4a” type (is acceptable for XML element’s name)

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — ISPRODUCTION…
  • API — ISPRODUCTION…
  • IN PRACTICE
  • Changelog

Installation

Quick Take

Examples

  • Allow a combining mark after the first character
  • Check characters against the XML NameChar production
  • Check characters against the XML NameStartChar production
  • Validate a character after the first character in an XML name
  • Validate a whole XML name

Purpose

This program exports two functions. First-one tells, is a given character string valid for the first character of the XML element name. Second-one tells, is the character string valid for the second character onwards (the requirements for latter are more lax).

Practically, both are used to detect where (X)HTML element names end.

This articleopens in a new tab contains an in-depth explanation of the spec terminology.

API — isProduction4()

The function isProduction4() is imported like this:

For legacy reasons, this function can also be imported by name validFirstChar().

It’s a function which takes one input argument:

It will return a Boolean, is input string character an acceptable as the first XML character or not.

XML spec production 4opens in a new tab for the first character of the XML element is strict than requirements for the subsequent characters.

const res1 = isProduction4("-"); // or use validFirstChar(), the same
console.log("res1 = " + res1);
// => 'res1 = false <---- minus is not allowed for first character

const res2 = isProduction4("z"); // or use validFirstChar(), the same
console.log("res2 = " + res2);
// => 'res2 = true

API — isProduction4a()

The function isProduction4a() is imported like this:

For legacy reasons, this function can also be imported by name validSecondCharOnwards().

It’s a function which takes one input argument:

XML spec production 4aopens in a new tab describes the requirements for the second XML element’s name character and onwards.

Pass any character (including astral-one) into function isProduction4a(), and it will respond with a Boolean, is it acceptable as second XML character and onwards (or not). Requirements are same as for the first character but a bit more permissive.

import { isProduction4, isProduction4a } from "charcode-is-valid-xml-name-character";

const res1 = isProduction4a("-"); // or use validSecondCharOnwards(), the same
console.log("res1 = " + res1);
// => 'res1 = true <---- minus is allowed for second character-onwards

const res2 = isProduction4a("z"); // or use validSecondCharOnwards(), the same
console.log("res2 = " + res2);
// => 'res2 = true

In practice

Let’s say you are iterating through string, character-by-character and it contains (X)HTML code source. This library will evaluate any given character and tell, is it a valid character for an element name. You use this library to detect where element names end.

In the most simple scenario:

<img class="">
    ^     ^
    1     2

Characters space (1) and = (2) in the example above mark the ending of the element names (img and class). OK, so spaces and equals signs are not allowed as element names and therefore mark their ending. Are there more of such characters? Oh yes. Quite a lot according to specopens in a new tab what warrants a proper library dedicated only for that purpose.

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