From 1ff25424db5caccb6f836b482c36de114ddefd8e Mon Sep 17 00:00:00 2001 From: Louis-Dominique Dubeau Date: Mon, 24 Jun 2019 19:16:45 -0400 Subject: [PATCH] feat: add support for the RestrictedChar production --- src/xml/1.1/ed2.ts | 21 +++++++++++++++++++++ test/xmlchars.spec.ts | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/src/xml/1.1/ed2.ts b/src/xml/1.1/ed2.ts index a75d46d..4aa6cde 100644 --- a/src/xml/1.1/ed2.ts +++ b/src/xml/1.1/ed2.ts @@ -11,6 +11,9 @@ // export const CHAR = "\u0001-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}"; +export const RESTRICTED_CHAR = + "\u0001-\u0008\u000B\u000C\u000E-\u001F\u007F-\u0084\u0086-\u009F"; + export const S = " \t\r\n"; // tslint:disable-next-line:max-line-length @@ -25,6 +28,8 @@ export const NAME_CHAR = export const CHAR_RE = new RegExp(`^[${CHAR}]$`, "u"); +export const RESTRICTED_CHAR_RE = new RegExp(`^[${RESTRICTED_CHAR}]$`, "u"); + export const S_RE = new RegExp(`^[${S}]+$`, "u"); export const NAME_START_CHAR_RE = new RegExp(`^[${NAME_START_CHAR}]$`, "u"); @@ -60,6 +65,22 @@ export function isChar(c: number): boolean { (c >= 0x10000 && c <= 0x10FFFF); } +/** + * Determines whether a codepoint matches the ``RESTRICTED_CHAR`` production. + * + * @param c The code point. + * + * @returns ``true`` if the codepoint matches ``RESTRICTED_CHAR``. + */ +export function isRestrictedChar(c: number): boolean { + return (c >= 0x1 && c <= 0x8) || + c === 0xB || + c === 0xC || + (c >= 0xE && c <= 0x1F) || + (c >= 0x7F && c <= 0x84) || + (c >= 0x86 && c <= 0x9F); +} + /** * Determines whether a codepoint matches the ``S`` (space) production. * diff --git a/test/xmlchars.spec.ts b/test/xmlchars.spec.ts index d58d7b7..5752b9f 100644 --- a/test/xmlchars.spec.ts +++ b/test/xmlchars.spec.ts @@ -303,6 +303,9 @@ describe("xml/1.1", () => { matching: [one, x, poo, colon, space, tab, newline, cr, ideographic, combining, digit, extender], }, + RESTRICTED_CHAR_RE: { + matching: [one], + }, S_RE: { matching: [space, tab, newline, cr], }, @@ -336,6 +339,12 @@ describe("xml/1.1", () => { makeCodePointTestTests(xml_1_1_ed2.isChar, cases.CHAR_RE); }); + describe(".isRestrictedChar", () => { + // tslint:disable-next-line:mocha-no-side-effect-code + makeCodePointTestTests(xml_1_1_ed2.isRestrictedChar, + cases.RESTRICTED_CHAR_RE); + }); + describe(".isS", () => { // tslint:disable-next-line:mocha-no-side-effect-code makeCodePointTestTests(xml_1_1_ed2.isS, cases.S_RE);