Package Exports
- charcode-is-valid-xml-name-character
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (charcode-is-valid-xml-name-character) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
charcode-is-valid-xml-name-character
Does a given character belong to XML spec's "Production 4 OR 4a" type (is acceptable for XML element's name)
Table of Contents
Install
npm i charcode-is-valid-xml-name-character
// consume via a CommonJS require:
const {
isProduction4,
isProduction4a
} = require("charcode-is-valid-xml-name-character");
// or as an ES Module:
import {
isProduction4,
isProduction4a
} from "charcode-is-valid-xml-name-character";
Here's what you'll get:
Type | Key in package.json |
Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/charcode-is-valid-xml-name-character.cjs.js |
2 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . |
module |
dist/charcode-is-valid-xml-name-character.esm.js |
2 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in |
browser |
dist/charcode-is-valid-xml-name-character.umd.js |
20 KB |
What is does
It returns a Boolean, is the given character the Production 4a of XML spec, or in human terms, a possible ending character of an XML element.
This library is used to detect where (X)HTML element names end.
This article contains an in-depth explanation of the spec terminology: https://www.xml.com/pub/a/2001/07/25/namingparts.html — it helped me to get up to speed on this subject.
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.
API
Two functions - one to check requirements for first character, another to check requirements for second character and onwards. Both functions return a Boolean.
Function's name | Purpose | |
---|---|---|
isProduction4 |
To tell, is this character suitable to be the first character | |
validFirstChar |
||
isProduction4a |
To tell, is this character suitable to be the second character and onwards | |
validSecondCharOnwards |
isProduction4()
/ validFirstChar()
- requirements for 1st char
XML spec production 4 - the requirements for the first character of the XML element. It's more strict than requirements for the subsequent characters, see production 4a below.
Pass any character (including astral-one) into function isProduction4()
, and it will respond with a Boolean, is it acceptable as first XML character (or not).
const {
isProduction4,
validFirstChar
// isProduction4a,
} = require("charcode-is-valid-xml-name-character");
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
It consumes a single character (can be any Unicode character, including astral-one, comprising two surrogates). Returns Boolean - is it acceptable as the first character in XML element's name.
isProduction4a()
/ validSecondCharOnwards()
- requirements for 2nd char onwards
XML spec production 4a - the requirements for the second character onwards in XML element's name.
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.
const {
// isProduction4,
isProduction4a
} = require("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
It consumes a single character (can be any Unicode character, including astral-one, comprising two surrogates). Returns Boolean - is it acceptable as the second or subsequent character in XML element's name.
Contributing
If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.
If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.
If you would like to add or change some features, fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt