Installation
Quick Take
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 article 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 4 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 4a 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 we know spaces and equals’ are not allowed as element names and therefore mark their ending. Are there more of such characters? Oh yes. Quite a lot according to spec what warrants a proper library dedicated only for that purpose.