parsing blog successfully and requesting youtube
This commit is contained in:
21
node_modules/xml-naming/LICENSE
generated
vendored
Normal file
21
node_modules/xml-naming/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Natural Intelligence
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
287
node_modules/xml-naming/README.md
generated
vendored
Normal file
287
node_modules/xml-naming/README.md
generated
vendored
Normal file
@@ -0,0 +1,287 @@
|
||||
# xml-naming
|
||||
[](https://npm-compare.com/xml-naming)
|
||||
[](https://www.npmjs.com/package/xml-naming)
|
||||
[](https://github.com/NaturalIntelligence/xml-naming)
|
||||
|
||||
Validates XML name productions as defined in the [XML 1.0](https://www.w3.org/TR/xml/) and [XML 1.1](https://www.w3.org/TR/xml11/) specifications.
|
||||
|
||||
Covers all five productions:
|
||||
|
||||
| Production | Description | Colon | Digit/hyphen start |
|
||||
|---|---|---|---|
|
||||
| `Name` | General XML name | ✅ | ❌ |
|
||||
| `NCName` | Non-Colonized name | ❌ | ❌ |
|
||||
| `QName` | Namespace-qualified name (`prefix:local`) | ✅ (one only) | ❌ |
|
||||
| `NMToken` | Name token (relaxed start) | ✅ | ✅ |
|
||||
| `NMTokens` | Whitespace-separated NMToken list | ✅ | ✅ |
|
||||
|
||||
Used internally by [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser), [fast-xml-validator](https://github.com/NaturalIntelligence/fast-xml-validator), [@nodable\flexible-xml-parser](https://github.com/nodable/flexible-xml-parser) and [fast-svg-parser](https://github.com/amitguptagwl/fast-svg-parser).
|
||||
|
||||
---
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install xml-naming
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
### Boolean validators
|
||||
|
||||
```js
|
||||
import { name, ncName, qName, nmToken, nmTokens } from 'xml-naming';
|
||||
|
||||
// Name — colon allowed anywhere, used for DOCTYPE entity names
|
||||
name('foo') // true
|
||||
name('a:b:c') // true ← multiple colons fine for Name
|
||||
name('1foo') // false ← digit start invalid
|
||||
|
||||
// NCName — no colon, used for SVG id attributes, namespace prefixes
|
||||
ncName('my-id') // true
|
||||
ncName('xlink:href') // false ← colon not allowed
|
||||
|
||||
// QName — exactly one colon as prefix separator, used for element/attribute names
|
||||
qName('svg:circle') // true
|
||||
qName('foo') // true ← unprefixed QName is valid
|
||||
qName('a:b:c') // false ← only one colon allowed
|
||||
qName(':foo') // false ← cannot start with colon
|
||||
|
||||
// NMToken — any NameChar at start, used for DTD NMTOKEN attributes
|
||||
nmToken('123') // true ← digit start is fine
|
||||
nmToken('-bar') // true
|
||||
nmToken('foo bar') // false ← space not allowed
|
||||
|
||||
// NMTokens — whitespace-separated NMToken list
|
||||
nmTokens('tok1 tok2 -foo 123') // true
|
||||
```
|
||||
|
||||
### XML version option
|
||||
|
||||
All validators accept an optional `{ xmlVersion }` option:
|
||||
|
||||
```js
|
||||
import { name } from 'xml-naming';
|
||||
|
||||
name('\u0085', { xmlVersion: '1.0' }) // false — NEL (Next Line), not in 1.0 ranges
|
||||
name('\u0085', { xmlVersion: '1.1' }) // true — explicitly allowed in 1.1
|
||||
|
||||
name('\uD800\uDC00', { xmlVersion: '1.0' }) // false
|
||||
name('\uD800\uDC00', { xmlVersion: '1.1' }) // true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### ASCII-only fast path
|
||||
|
||||
All validators, `validate`, `validateAll`, and `sanitize` also accept `{ asciiOnly: true }`.
|
||||
When set, matching is restricted to the ASCII subset of the NameStartChar/NameChar
|
||||
productions and skips unicode-aware regex matching entirely — no `\u00C0-\uFFFD`-style
|
||||
ranges, and (for XML 1.1) no `/u` regex flag. Unicode-aware regexes are measurably slower
|
||||
than plain ASCII matching in JS engines, so this is a real performance win when you know
|
||||
your input is ASCII-only, which is the common case for HTML/SVG ids and most XML tags.
|
||||
|
||||
**This is opt-in and defaults to `false`** for backward compatibility: turning it on
|
||||
changes behavior, since it rejects legitimate non-ASCII XML names that would otherwise be
|
||||
valid. Only enable it when you control the input and know it's ASCII (e.g. internal
|
||||
identifiers, machine-generated names), not for validating arbitrary user- or
|
||||
externally-supplied XML/SVG content.
|
||||
|
||||
```js
|
||||
import { name, sanitize } from 'xml-naming';
|
||||
|
||||
name('café', { asciiOnly: true }) // false — 'é' is not ASCII, even though it's
|
||||
name('café') // true a valid XML 1.0/1.1 NameChar
|
||||
|
||||
sanitize('café', 'name', { asciiOnly: true }) // 'caf_' — non-ASCII replaced too
|
||||
sanitize('café', 'name') // 'café' — left untouched by default
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Memoized validator (`createValidator`)
|
||||
|
||||
Real documents tend to reuse a small vocabulary of tag/attribute names across many
|
||||
siblings (`id`, `class`, `href`, ... repeated across hundreds of elements). Calling the
|
||||
plain boolean validators re-runs the regex on every call, even for names seen before.
|
||||
|
||||
`createValidator(production, opts)` returns a memoized validator function: `xmlVersion`
|
||||
and `asciiOnly` are fixed at creation time (so the regex is resolved once, not per call),
|
||||
and repeated inputs after the first are served from an internal cache instead of
|
||||
re-matching the regex.
|
||||
|
||||
```js
|
||||
import { createValidator } from 'xml-naming';
|
||||
|
||||
const isQName = createValidator('qName', { xmlVersion: '1.0' });
|
||||
|
||||
isQName('sku'); // false → regex test (cache miss), result cached
|
||||
isQName('sku'); // false → cache hit, no regex run
|
||||
```
|
||||
|
||||
Use one instance per document/parse (or reuse across a session — your choice), rather
|
||||
than creating one per call:
|
||||
|
||||
```js
|
||||
// e.g. inside a parser, once per parse call:
|
||||
const isValidTag = createValidator('qName', { asciiOnly: true });
|
||||
|
||||
for (const tagName of tagNames) {
|
||||
if (!isValidTag(tagName)) throw new Error(`Invalid tag name: ${tagName}`);
|
||||
}
|
||||
```
|
||||
|
||||
Because the validator is a plain function, this loop already gets short-circuit
|
||||
behaviour (via `break`/`throw` on first failure) and zero extra allocation on the happy
|
||||
path — no separate "bulk" API is needed for that.
|
||||
|
||||
**Cache bound:** the internal cache is capped by `maxCacheSize` (default `2048`). Once
|
||||
the cap is reached, new distinct strings are still validated correctly, they're just no
|
||||
longer cached — existing cached entries keep being served. This keeps memory bounded
|
||||
even against high-cardinality or adversarial input (e.g. externally-supplied names that
|
||||
never repeat), without the cost of a full LRU or the perf cliff of reset-and-refill.
|
||||
|
||||
Call `.reset()` on the returned function to clear the cache manually, e.g. between
|
||||
unrelated parse calls if you're reusing one validator instance across a long-running
|
||||
process:
|
||||
|
||||
```js
|
||||
isQName.reset();
|
||||
```
|
||||
|
||||
The cache is private to each `createValidator()` instance — there's no shared/global
|
||||
cache, so unrelated callers never interfere with each other.
|
||||
|
||||
---
|
||||
|
||||
### Diagnostic validation
|
||||
|
||||
```js
|
||||
import { validate } from 'xml-naming';
|
||||
|
||||
validate('svg:circle', 'qName')
|
||||
// { valid: true, production: 'qName', input: 'svg:circle' }
|
||||
|
||||
validate('1foo', 'ncName')
|
||||
// {
|
||||
// valid: false,
|
||||
// production: 'ncName',
|
||||
// input: '1foo',
|
||||
// reason: 'First character "1" is not a valid NameStartChar',
|
||||
// position: 0
|
||||
// }
|
||||
|
||||
validate('foo:bar', 'ncName')
|
||||
// {
|
||||
// valid: false,
|
||||
// production: 'ncName',
|
||||
// input: 'foo:bar',
|
||||
// reason: 'Colon is not allowed in NCName',
|
||||
// position: 3
|
||||
// }
|
||||
|
||||
validate('a:b:c', 'qName')
|
||||
// {
|
||||
// valid: false,
|
||||
// production: 'qName',
|
||||
// input: 'a:b:c',
|
||||
// reason: 'QName can have at most one colon',
|
||||
// position: 3
|
||||
// }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Batch validation
|
||||
|
||||
```js
|
||||
import { validateAll } from 'xml-naming';
|
||||
|
||||
validateAll(['svg', 'circle', '123bad', 'xlink:href'], 'ncName')
|
||||
// [
|
||||
// { valid: true, production: 'ncName', input: 'svg' },
|
||||
// { valid: true, production: 'ncName', input: 'circle' },
|
||||
// { valid: false, production: 'ncName', input: '123bad', reason: '...', position: 0 },
|
||||
// { valid: false, production: 'ncName', input: 'xlink:href',reason: '...', position: 5 }
|
||||
// ]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Sanitize / auto-fix
|
||||
|
||||
Useful when generating XML/SVG programmatically from user-supplied strings:
|
||||
|
||||
```js
|
||||
import { sanitize } from 'xml-naming';
|
||||
|
||||
sanitize('123abc', 'ncName') // '_123abc' ← digit start fixed
|
||||
sanitize('my element','name') // 'my_element' ← space replaced
|
||||
sanitize('foo:bar', 'ncName') // 'foobar' ← colon stripped
|
||||
sanitize('hello!', 'name') // 'hello_' ← illegal char replaced
|
||||
|
||||
// Custom replacement character
|
||||
sanitize('my element', 'name', { replacement: '-' }) // 'my-element'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Which production should I use?
|
||||
|
||||
| Context | Production |
|
||||
|---|---|
|
||||
| XML element/attribute names (namespace-aware) | `qName` |
|
||||
| SVG `id` attribute values | `ncName` |
|
||||
| Namespace prefix alone | `ncName` |
|
||||
| DOCTYPE `<!ENTITY name ...>` | `name` |
|
||||
| DOCTYPE `<!NOTATION name ...>` | `name` |
|
||||
| DTD `NMTOKEN` attribute values | `nmToken` |
|
||||
| DTD `NMTOKENS` attribute values | `nmTokens` |
|
||||
|
||||
> **Note:** DOCTYPE entity and notation names must use `Name`, not `QName`. Colons carry no namespace meaning in the DTD subset.
|
||||
|
||||
---
|
||||
|
||||
## API
|
||||
|
||||
### `name(str, opts?)` → `boolean`
|
||||
### `ncName(str, opts?)` → `boolean`
|
||||
### `qName(str, opts?)` → `boolean`
|
||||
### `nmToken(str, opts?)` → `boolean`
|
||||
### `nmTokens(str, opts?)` → `boolean`
|
||||
|
||||
`opts`:
|
||||
- `xmlVersion`: `'1.0'` (default) | `'1.1'`
|
||||
- `asciiOnly`: boolean (default `false`) — ASCII-only fast path, see above
|
||||
|
||||
### `createValidator(production, opts?)` → memoized `(str) => boolean`, with `.reset()`
|
||||
|
||||
`opts`:
|
||||
- `xmlVersion`: `'1.0'` (default) | `'1.1'`
|
||||
- `asciiOnly`: boolean (default `false`)
|
||||
- `maxCacheSize`: number (default `2048`) — cache stops accepting new entries once reached; existing entries keep serving hits
|
||||
|
||||
### `validate(str, production, opts?)` → `ValidationResult`
|
||||
|
||||
`production`: `'name'` | `'ncName'` | `'qName'` | `'nmToken'` | `'nmTokens'`
|
||||
|
||||
`opts`: same as boolean validators (`xmlVersion`, `asciiOnly`)
|
||||
|
||||
### `validateAll(strings[], production, opts?)` → `ValidationResult[]`
|
||||
|
||||
`opts`: same as `validate`
|
||||
|
||||
### `sanitize(str, production?, opts?)` → `string`
|
||||
|
||||
`opts`:
|
||||
- `xmlVersion`: `'1.0'` | `'1.1'`
|
||||
- `replacement`: string (default `'_'`)
|
||||
- `asciiOnly`: boolean (default `false`) — also replaces non-ASCII characters, not just XML-illegal ones
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
56
node_modules/xml-naming/package.json
generated
vendored
Normal file
56
node_modules/xml-naming/package.json
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "xml-naming",
|
||||
"version": "0.3.0",
|
||||
"description": "Validates XML name productions — Name, NCName, QName, NMToken, NMTokens — for XML 1.0 and 1.1",
|
||||
"type": "module",
|
||||
"main": "./src/index.js",
|
||||
"types": "./src/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./src/index.js",
|
||||
"types": "./src/index.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"README.md"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "jasmine specs/*spec.js",
|
||||
"perf": "node specs/perf.js"
|
||||
},
|
||||
"keywords": [
|
||||
"fast",
|
||||
"xml",
|
||||
"xml-name",
|
||||
"ncname",
|
||||
"qname",
|
||||
"nmtoken",
|
||||
"xml-validation",
|
||||
"xml-namespace",
|
||||
"name-validator",
|
||||
"xml-1.0",
|
||||
"xml-1.1"
|
||||
],
|
||||
"author": "Amit Gupta (https://solothought.com)",
|
||||
"license": "MIT",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/NaturalIntelligence"
|
||||
}
|
||||
],
|
||||
"devDependencies": {
|
||||
"jasmine": "^5.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/NaturalIntelligence/xml-naming"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/NaturalIntelligence/xml-naming/issues"
|
||||
}
|
||||
}
|
||||
108
node_modules/xml-naming/src/index.d.ts
generated
vendored
Normal file
108
node_modules/xml-naming/src/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
export interface ValidationOptions {
|
||||
/** XML specification version to validate against. Defaults to '1.0'. */
|
||||
xmlVersion?: '1.0' | '1.1';
|
||||
/**
|
||||
* Restrict matching to the ASCII subset of the NameStartChar/NameChar
|
||||
* productions and skip unicode-aware regex matching entirely. Faster,
|
||||
* especially for XML 1.1 (which otherwise requires the `/u` regex flag),
|
||||
* but rejects legitimate non-ASCII XML names. Off by default for
|
||||
* backward compatibility — opt in only when inputs are known to be ASCII.
|
||||
* Defaults to false.
|
||||
*/
|
||||
asciiOnly?: boolean;
|
||||
}
|
||||
|
||||
export interface SanitizeOptions extends ValidationOptions {
|
||||
/** Character used to replace invalid characters. Defaults to '_'. */
|
||||
replacement?: string;
|
||||
}
|
||||
|
||||
export type Production = 'name' | 'ncName' | 'qName' | 'nmToken' | 'nmTokens';
|
||||
|
||||
export interface ValidationResult {
|
||||
valid: boolean;
|
||||
production: Production;
|
||||
input: string;
|
||||
/** Present only when valid is false. */
|
||||
reason?: string;
|
||||
/** Index of the first offending character. Present only when valid is false. */
|
||||
position?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid XML Name.
|
||||
* Colons are permitted anywhere (Name production).
|
||||
* Used for: DOCTYPE entity names, notation names, DTD element declarations.
|
||||
*/
|
||||
export function name(str: string, opts?: ValidationOptions): boolean;
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NCName (Non-Colonized Name).
|
||||
* Colons are not permitted.
|
||||
* Used for: namespace prefixes, local names, SVG id attributes.
|
||||
*/
|
||||
export function ncName(str: string, opts?: ValidationOptions): boolean;
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid QName (Qualified Name).
|
||||
* Allows exactly one colon as a prefix separator: prefix:localName.
|
||||
* Used for: element and attribute names in namespace-aware XML/SVG.
|
||||
*/
|
||||
export function qName(str: string, opts?: ValidationOptions): boolean;
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NMToken.
|
||||
* Same character set as Name but no restriction on the first character.
|
||||
* Used for: DTD NMTOKEN attribute values.
|
||||
*/
|
||||
export function nmToken(str: string, opts?: ValidationOptions): boolean;
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NMTokens value.
|
||||
* A whitespace-separated list of NMToken values.
|
||||
* Used for: DTD NMTOKENS attribute values.
|
||||
*/
|
||||
export function nmTokens(str: string, opts?: ValidationOptions): boolean;
|
||||
|
||||
export interface CreateValidatorOptions extends ValidationOptions {
|
||||
/**
|
||||
* Max number of distinct strings to cache. Once reached, new strings are
|
||||
* still validated correctly but are no longer cached; existing cached
|
||||
* entries keep being served. Defaults to 2048.
|
||||
*/
|
||||
maxCacheSize?: number;
|
||||
}
|
||||
|
||||
export interface MemoizedValidator {
|
||||
(str: string): boolean;
|
||||
/** Clears the internal cache. */
|
||||
reset(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a memoized boolean validator function for a single production.
|
||||
* `opts` (xmlVersion, asciiOnly) are fixed at creation time. Repeated calls
|
||||
* with the same string after the first are served from an internal cache
|
||||
* instead of re-running the regex — useful when validating a small, reused
|
||||
* vocabulary of names (e.g. tag/attribute names across many siblings in a
|
||||
* document) many times.
|
||||
*/
|
||||
export function createValidator(production: Production, opts?: CreateValidatorOptions): MemoizedValidator;
|
||||
|
||||
/**
|
||||
* Validates a string against a named XML production and returns a detailed result.
|
||||
*/
|
||||
export function validate(str: string, production: Production, opts?: ValidationOptions): ValidationResult;
|
||||
|
||||
/**
|
||||
* Validates an array of strings against a named XML production.
|
||||
*/
|
||||
export function validateAll(strings: string[], production: Production, opts?: ValidationOptions): ValidationResult[];
|
||||
|
||||
/**
|
||||
* Transforms an invalid string into the nearest valid XML name for the given production.
|
||||
* - Strips or replaces illegal characters
|
||||
* - Fixes invalid start characters by prepending the replacement character
|
||||
* - Removes colons for NCName production
|
||||
*/
|
||||
export function sanitize(str: string, production?: Production, opts?: SanitizeOptions): string;
|
||||
382
node_modules/xml-naming/src/index.js
generated
vendored
Normal file
382
node_modules/xml-naming/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* xml-naming
|
||||
* Validates XML Name productions as defined in the XML 1.0 and 1.1 specifications.
|
||||
* Covers: Name, NCName, QName, NMToken, NMTokens
|
||||
*
|
||||
* XML 1.0 spec: https://www.w3.org/TR/xml/#NT-Name
|
||||
* XML 1.1 spec: https://www.w3.org/TR/xml11/#NT-NameStartChar
|
||||
* XML NS spec: https://www.w3.org/TR/xml-names/#NT-NCName
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Character class strings — XML 1.0
|
||||
//
|
||||
// NameStartChar ::= ":" | [A-Z] | "_" | [a-z]
|
||||
// | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF]
|
||||
// | [#x370-#x37D] | [#x37F-#x1FFF] <- split to exclude #x0487
|
||||
// | [#x200C-#x200D]
|
||||
// | [#x2070-#x218F] | [#x2C00-#x2FEF]
|
||||
// | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD]
|
||||
//
|
||||
// NameChar ::= NameStartChar | "-" | "." | [0-9]
|
||||
// | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
|
||||
//
|
||||
// Note: \u0487 (Combining Cyrillic Millions Sign) was added in Unicode 4.0,
|
||||
// after XML 1.0 was defined against Unicode 2.0. It falls inside the range
|
||||
// \u037F-\u1FFF but must be excluded. We split that range into
|
||||
// \u037F-\u0486 and \u0488-\u1FFF to exclude it explicitly.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const nameStartChar10 =
|
||||
':A-Za-z_' +
|
||||
'\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF' +
|
||||
'\u0370-\u037D' +
|
||||
'\u037F-\u0486\u0488-\u1FFF' + // split to exclude \u0487
|
||||
'\u200C-\u200D' +
|
||||
'\u2070-\u218F' +
|
||||
'\u2C00-\u2FEF' +
|
||||
'\u3001-\uD7FF' +
|
||||
'\uF900-\uFDCF' +
|
||||
'\uFDF0-\uFFFD';
|
||||
|
||||
const nameChar10 =
|
||||
nameStartChar10 +
|
||||
'\\-\\.\\d' +
|
||||
'\u00B7' +
|
||||
'\u0300-\u036F' +
|
||||
'\u203F-\u2040';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Character class strings — XML 1.1
|
||||
//
|
||||
// Differences from XML 1.0:
|
||||
//
|
||||
// NameStartChar:
|
||||
// 1.0 has split ranges: \u00C0-\u00D6, \u00D8-\u00F6, \u00F8-\u02FF
|
||||
// 1.1 merges them into: \u00C0-\u02FF
|
||||
// (\u00D7 x and \u00F7 / are division symbols, excluded in both versions)
|
||||
//
|
||||
// 1.0 tops out at \uFFFD (BMP only)
|
||||
// 1.1 adds \u{10000}-\u{EFFFF} (supplementary planes)
|
||||
// These require the /u flag on the RegExp — see buildRegexes below.
|
||||
//
|
||||
// NameChar:
|
||||
// 1.1 adds \u0487 (Combining Cyrillic Millions Sign, added in Unicode 4.0)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const nameStartChar11 =
|
||||
':A-Za-z_' +
|
||||
'\u00C0-\u02FF' + // merged — 1.0 had three split ranges here
|
||||
'\u0370-\u037D' +
|
||||
'\u037F-\u0486\u0488-\u1FFF' + // split to exclude \u0487 (combining mark, never a NameStartChar)
|
||||
'\u200C-\u200D' +
|
||||
'\u2070-\u218F' +
|
||||
'\u2C00-\u2FEF' +
|
||||
'\u3001-\uD7FF' +
|
||||
'\uF900-\uFDCF' +
|
||||
'\uFDF0-\uFFFD' +
|
||||
'\u{10000}-\u{EFFFF}'; // supplementary planes — REQUIRES /u flag on RegExp
|
||||
|
||||
const nameChar11 =
|
||||
nameStartChar11 +
|
||||
'\\-\\.\\d' +
|
||||
'\u00B7' +
|
||||
'\u0300-\u036F' +
|
||||
'\u0487' + // Combining Cyrillic Millions Sign — valid in 1.1, not 1.0
|
||||
'\u203F-\u2040';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Regex builders
|
||||
//
|
||||
// XML 1.0 regexes: no flags — BMP only, standard JS regex behaviour.
|
||||
// XML 1.1 regexes: /u flag — required for \u{10000}-\u{EFFFF} to match actual
|
||||
// supplementary code points rather than lone surrogates (which are illegal XML).
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const buildRegexes = (startChar, char, flags = '') => {
|
||||
const ncStart = startChar.replace(':', '');
|
||||
const ncChar = char.replace(':', '');
|
||||
const ncNamePat = `[${ncStart}][${ncChar}]*`;
|
||||
|
||||
return {
|
||||
name: new RegExp(`^[${startChar}][${char}]*$`, flags),
|
||||
ncName: new RegExp(`^${ncNamePat}$`, flags),
|
||||
qName: new RegExp(`^${ncNamePat}(?::${ncNamePat})?$`, flags),
|
||||
nmToken: new RegExp(`^[${char}]+$`, flags),
|
||||
nmTokens: new RegExp(`^[${char}]+(?:\\s+[${char}]+)*$`, flags),
|
||||
};
|
||||
};
|
||||
|
||||
const regexes10 = buildRegexes(nameStartChar10, nameChar10); // no /u — BMP only
|
||||
const regexes11 = buildRegexes(nameStartChar11, nameChar11, 'u'); // /u — enables \u{10000}-\u{EFFFF}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ASCII-only fast path (opt-in, off by default)
|
||||
//
|
||||
// The XML 1.0 vs 1.1 NameStartChar/NameChar productions differ *only* in
|
||||
// their non-ASCII ranges (merged vs split Latin-1 ranges, \u0487, and
|
||||
// supplementary planes). Restricted to ASCII, both versions collapse to the
|
||||
// same character classes, so a single regex pair covers both xmlVersion
|
||||
// values — no /u flag needed.
|
||||
//
|
||||
// Rationale: unicode-aware regexes (the /u flag, required for XML 1.1's
|
||||
// supplementary-plane range) are measurably slower in V8 than plain
|
||||
// non-unicode regexes on the same input, even when the input is pure ASCII.
|
||||
// For the common case — HTML/SVG ids, XML tags — names are ASCII, so callers
|
||||
// who know this can opt in to skip the unicode-aware matching path entirely.
|
||||
// This is a real but *conditional* win: mainly for XML 1.1 input (avoids /u),
|
||||
// or at scale where the larger unicode character classes add engine
|
||||
// overhead. It also changes behaviour (rejects legitimate non-ASCII XML
|
||||
// 1.0/1.1 names), so it must never be silently enabled — hence off by
|
||||
// default.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const nameStartCharAscii = ':A-Za-z_';
|
||||
const nameCharAscii = nameStartCharAscii + '\\-\\.\\d';
|
||||
|
||||
const regexesAscii = buildRegexes(nameStartCharAscii, nameCharAscii); // no /u — ASCII only
|
||||
|
||||
const getRegexes = (xmlVersion = '1.0', asciiOnly = false) => {
|
||||
if (asciiOnly) return regexesAscii;
|
||||
return xmlVersion === '1.1' ? regexes11 : regexes10;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Boolean validators
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid XML Name.
|
||||
* Colons are allowed anywhere (Name production).
|
||||
* Used for: DOCTYPE entity names, notation names, DTD element declarations.
|
||||
*
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: skip unicode-aware matching, ASCII names only (default false).
|
||||
*/
|
||||
export const name = (str, { xmlVersion = '1.0', asciiOnly = false } = {}) =>
|
||||
getRegexes(xmlVersion, asciiOnly).name.test(str);
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NCName (Non-Colonized Name).
|
||||
* Colons are not permitted.
|
||||
* Used for: namespace prefixes, local names, SVG id attributes.
|
||||
*
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: skip unicode-aware matching, ASCII names only (default false).
|
||||
*/
|
||||
export const ncName = (str, { xmlVersion = '1.0', asciiOnly = false } = {}) =>
|
||||
getRegexes(xmlVersion, asciiOnly).ncName.test(str);
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid QName (Qualified Name).
|
||||
* Allows exactly one colon as a prefix separator: prefix:localName.
|
||||
* Used for: element and attribute names in namespace-aware XML/SVG.
|
||||
*
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: skip unicode-aware matching, ASCII names only (default false).
|
||||
*/
|
||||
export const qName = (str, { xmlVersion = '1.0', asciiOnly = false } = {}) =>
|
||||
getRegexes(xmlVersion, asciiOnly).qName.test(str);
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NMToken.
|
||||
* Like Name but no restriction on the first character.
|
||||
* Used for: DTD NMTOKEN attribute values.
|
||||
*
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: skip unicode-aware matching, ASCII names only (default false).
|
||||
*/
|
||||
export const nmToken = (str, { xmlVersion = '1.0', asciiOnly = false } = {}) =>
|
||||
getRegexes(xmlVersion, asciiOnly).nmToken.test(str);
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid NMTokens value.
|
||||
* A whitespace-separated list of NMToken values.
|
||||
* Used for: DTD NMTOKENS attribute values.
|
||||
*
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: skip unicode-aware matching, ASCII names only (default false).
|
||||
*/
|
||||
export const nmTokens = (str, { xmlVersion = '1.0', asciiOnly = false } = {}) =>
|
||||
getRegexes(xmlVersion, asciiOnly).nmTokens.test(str);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Memoized validator factory
|
||||
//
|
||||
// Real documents reuse a small vocabulary of tag/attribute names across many
|
||||
// siblings (e.g. `id`, `class`, `href` repeated across hundreds of elements).
|
||||
// The plain boolean validators above re-run the regex on every call
|
||||
// regardless of repeats. `createValidator` returns a closure with a private
|
||||
// string -> boolean cache, so repeated names after the first become O(1)
|
||||
// lookups instead of regex tests.
|
||||
//
|
||||
// - opts (xmlVersion, asciiOnly) are fixed at creation time, so the regex is
|
||||
// resolved once, not on every call.
|
||||
// - The cache is private to the returned closure — no shared/global state,
|
||||
// no cross-caller pollution.
|
||||
// - `maxCacheSize` bounds memory: once the cache reaches this many entries,
|
||||
// it stops accepting new ones (existing entries keep serving hits; new
|
||||
// misses just fall through to the regex, uncached). This avoids unbounded
|
||||
// growth against adversarial/high-cardinality input (e.g. validating
|
||||
// attacker-supplied names with no repeats) without the cost/complexity of
|
||||
// a full LRU, and without the perf cliff of reset-and-refill thrashing.
|
||||
// - Call `.reset()` on the returned function to clear the cache manually
|
||||
// (e.g. between unrelated parse calls).
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const PRODUCTIONS = ['name', 'ncName', 'qName', 'nmToken', 'nmTokens'];
|
||||
|
||||
/**
|
||||
* Returns a memoized boolean validator function for a single production,
|
||||
* with opts fixed at creation time.
|
||||
*
|
||||
* @param {'name'|'ncName'|'qName'|'nmToken'|'nmTokens'} production
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean, maxCacheSize?: number }} [opts]
|
||||
* maxCacheSize: max number of distinct strings to cache (default 2048).
|
||||
* Once reached, new strings are validated but not cached; existing cached
|
||||
* entries keep being served.
|
||||
* @returns {((str: string) => boolean) & { reset: () => void }}
|
||||
*/
|
||||
export const createValidator = (production, { xmlVersion = '1.0', asciiOnly = false, maxCacheSize = 2048 } = {}) => {
|
||||
if (!PRODUCTIONS.includes(production)) {
|
||||
throw new TypeError(
|
||||
`Unknown production "${production}". Must be one of: ${PRODUCTIONS.join(', ')}`
|
||||
);
|
||||
}
|
||||
|
||||
const regex = getRegexes(xmlVersion, asciiOnly)[production];
|
||||
let cache = new Map();
|
||||
|
||||
const validator = (str) => {
|
||||
const cached = cache.get(str);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
const result = regex.test(str);
|
||||
if (cache.size < maxCacheSize) cache.set(str, result);
|
||||
return result;
|
||||
};
|
||||
|
||||
validator.reset = () => { cache = new Map(); };
|
||||
|
||||
return validator;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Diagnostic validator
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Validates a string against a named production and returns a detailed result.
|
||||
*
|
||||
* @param {string} str
|
||||
* @param {'name'|'ncName'|'qName'|'nmToken'|'nmTokens'} production
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* @returns {{ valid: boolean, production: string, input: string, reason?: string, position?: number }}
|
||||
*/
|
||||
export const validate = (str, production, { xmlVersion = '1.0', asciiOnly = false } = {}) => {
|
||||
if (!PRODUCTIONS.includes(production)) {
|
||||
throw new TypeError(
|
||||
`Unknown production "${production}". Must be one of: ${PRODUCTIONS.join(', ')}`
|
||||
);
|
||||
}
|
||||
|
||||
const validators = { name, ncName, qName, nmToken, nmTokens };
|
||||
const isValid = validators[production](str, { xmlVersion, asciiOnly });
|
||||
|
||||
if (isValid) return { valid: true, production, input: str };
|
||||
|
||||
let reason = 'Does not match the production rules';
|
||||
let position;
|
||||
|
||||
// Diagnostic fallback char checks must mirror the same character set the
|
||||
// boolean validator above used, or the reported reason/position could
|
||||
// contradict the `valid: false` result (e.g. flagging a char as illegal
|
||||
// that the unicode-aware check would have accepted).
|
||||
const startCharPattern = asciiOnly ? /^[:A-Za-z_]/ : /^[:A-Za-z_\u00C0-\uFFFD]/;
|
||||
const namePattern = asciiOnly ? /[\w\-\\.:]/ : /[\w\-\\.:\u00B7\u00C0-\uFFFD]/;
|
||||
|
||||
if (str.length === 0) {
|
||||
reason = 'Input is empty';
|
||||
} else if (production === 'ncName' && str.includes(':')) {
|
||||
position = str.indexOf(':');
|
||||
reason = 'Colon is not allowed in NCName';
|
||||
} else if (production === 'qName' && str.startsWith(':')) {
|
||||
reason = 'QName cannot start with a colon';
|
||||
position = 0;
|
||||
} else if (production === 'qName' && str.endsWith(':')) {
|
||||
reason = 'QName cannot end with a colon';
|
||||
position = str.length - 1;
|
||||
} else if (production === 'qName' && (str.match(/:/g) || []).length > 1) {
|
||||
reason = 'QName can have at most one colon';
|
||||
position = str.lastIndexOf(':');
|
||||
} else if (
|
||||
['name', 'ncName', 'qName'].includes(production) &&
|
||||
!startCharPattern.test(str[0])
|
||||
) {
|
||||
reason = `First character "${str[0]}" is not a valid NameStartChar`;
|
||||
position = 0;
|
||||
} else {
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (!namePattern.test(str[i])) {
|
||||
reason = `Character "${str[i]}" at position ${i} is not a valid NameChar`;
|
||||
position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: false, production, input: str, reason, position };
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Batch validator
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Validates an array of strings against a named production.
|
||||
*
|
||||
* @param {string[]} strings
|
||||
* @param {'name'|'ncName'|'qName'|'nmToken'|'nmTokens'} production
|
||||
* @param {{ xmlVersion?: '1.0'|'1.1', asciiOnly?: boolean }} [opts]
|
||||
* @returns {Array<{ valid: boolean, production: string, input: string, reason?: string, position?: number }>}
|
||||
*/
|
||||
export const validateAll = (strings, production, opts = {}) =>
|
||||
strings.map(str => validate(str, production, opts));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sanitizer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Transforms an invalid string into the nearest valid XML name for the given production.
|
||||
*
|
||||
* @param {string} str
|
||||
* @param {'name'|'ncName'|'qName'|'nmToken'|'nmTokens'} production
|
||||
* @param {{ replacement?: string, asciiOnly?: boolean }} [opts]
|
||||
* asciiOnly: also replace any non-ASCII character, not just XML-illegal
|
||||
* ones (default false).
|
||||
* @returns {string}
|
||||
*/
|
||||
export const sanitize = (str, production = 'name', { replacement = '_', asciiOnly = false } = {}) => {
|
||||
if (!str) return replacement;
|
||||
|
||||
let result = str;
|
||||
|
||||
// Strip colons for NCName
|
||||
if (production === 'ncName') {
|
||||
result = result.replace(/:/g, '');
|
||||
}
|
||||
|
||||
// Replace illegal characters
|
||||
const allowedCharPattern = asciiOnly ? /[^\w\-\.:]/g : /[^\w\-\.:\u00B7\u00C0-\uFFFD]/g;
|
||||
result = result.replace(allowedCharPattern, replacement);
|
||||
|
||||
// Fix invalid start character for Name / NCName / QName
|
||||
if (production !== 'nmToken' && production !== 'nmTokens') {
|
||||
if (/^[\-\.\d]/.test(result)) {
|
||||
result = replacement + result;
|
||||
}
|
||||
}
|
||||
|
||||
return result || replacement;
|
||||
};
|
||||
Reference in New Issue
Block a user