|
| 1 | +// Copyright 2018-2026 the Deno authors. MIT license. |
| 2 | +// This module is browser compatible. |
| 3 | + |
| 4 | +/** |
| 5 | + * Internal module for XML entity encoding and decoding. |
| 6 | + * |
| 7 | + * @module |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * The five predefined XML entities per XML 1.0 §4.6. |
| 12 | + * Using const assertion for precise typing. |
| 13 | + */ |
| 14 | +const NAMED_ENTITIES = { |
| 15 | + lt: "<", |
| 16 | + gt: ">", |
| 17 | + amp: "&", |
| 18 | + apos: "'", |
| 19 | + quot: '"', |
| 20 | +} as const; |
| 21 | + |
| 22 | +/** |
| 23 | + * Reverse mapping for encoding special characters. |
| 24 | + */ |
| 25 | +const CHAR_TO_ENTITY = { |
| 26 | + "<": "<", |
| 27 | + ">": ">", |
| 28 | + "&": "&", |
| 29 | + "'": "'", |
| 30 | + '"': """, |
| 31 | +} as const; |
| 32 | + |
| 33 | +/** |
| 34 | + * Extended mapping for attribute value encoding (includes whitespace). |
| 35 | + */ |
| 36 | +const ATTR_CHAR_MAP: Record<string, string> = { |
| 37 | + "<": "<", |
| 38 | + ">": ">", |
| 39 | + "&": "&", |
| 40 | + "'": "'", |
| 41 | + '"': """, |
| 42 | + "\t": "	", |
| 43 | + "\n": " ", |
| 44 | + "\r": " ", |
| 45 | +}; |
| 46 | + |
| 47 | +// Hoisted regex patterns for performance |
| 48 | +const ENTITY_RE = /&([a-zA-Z]+|#[0-9]+|#x[0-9a-fA-F]+);/g; |
| 49 | +const SPECIAL_CHARS_RE = /[<>&'"]/g; |
| 50 | +const ATTR_ENCODE_RE = /[<>&'"\t\n\r]/g; |
| 51 | + |
| 52 | +/** |
| 53 | + * Pattern to detect bare `&` not followed by a valid reference. |
| 54 | + * Valid references are: &name; or &#digits; or &#xhexdigits; |
| 55 | + */ |
| 56 | +const BARE_AMPERSAND_RE = /&(?![a-zA-Z][a-zA-Z0-9]*;|#[0-9]+;|#x[0-9a-fA-F]+;)/; |
| 57 | + |
| 58 | +/** |
| 59 | + * Checks if a code point is a valid XML 1.0 Char per §2.2. |
| 60 | + * |
| 61 | + * Per the specification: |
| 62 | + * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] |
| 63 | + * |
| 64 | + * This excludes: |
| 65 | + * - NULL (#x0) |
| 66 | + * - Control characters #x1-#x8, #xB-#xC, #xE-#x1F |
| 67 | + * - Surrogate pairs #xD800-#xDFFF (handled separately) |
| 68 | + * - Non-characters #xFFFE-#xFFFF |
| 69 | + * |
| 70 | + * @see {@link https://www.w3.org/TR/xml/#charsets | XML 1.0 §2.2 Characters} |
| 71 | + */ |
| 72 | +function isValidXmlChar(codePoint: number): boolean { |
| 73 | + return ( |
| 74 | + codePoint === 0x9 || |
| 75 | + codePoint === 0xA || |
| 76 | + codePoint === 0xD || |
| 77 | + (codePoint >= 0x20 && codePoint <= 0xD7FF) || |
| 78 | + (codePoint >= 0xE000 && codePoint <= 0xFFFD) || |
| 79 | + (codePoint >= 0x10000 && codePoint <= 0x10FFFF) |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Options for entity decoding. |
| 85 | + */ |
| 86 | +export interface DecodeEntityOptions { |
| 87 | + /** |
| 88 | + * If true, throws an error on invalid bare `&` characters. |
| 89 | + * Per XML 1.0 §3.1, `&` must be escaped as `&` unless it starts |
| 90 | + * a valid entity or character reference. |
| 91 | + * |
| 92 | + * @default false |
| 93 | + */ |
| 94 | + readonly strict?: boolean; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Decodes XML entities in a string. |
| 99 | + * |
| 100 | + * Handles the five predefined entities (§4.6) and numeric character |
| 101 | + * references (§4.1) per the XML 1.0 specification. |
| 102 | + * |
| 103 | + * @param text The text containing XML entities to decode. |
| 104 | + * @param options Decoding options. |
| 105 | + * @returns The text with entities decoded. |
| 106 | + */ |
| 107 | +export function decodeEntities( |
| 108 | + text: string, |
| 109 | + options?: DecodeEntityOptions, |
| 110 | +): string { |
| 111 | + // Fast path: no ampersand means no entities to decode |
| 112 | + if (!text.includes("&")) return text; |
| 113 | + |
| 114 | + if (options?.strict) { |
| 115 | + const match = BARE_AMPERSAND_RE.exec(text); |
| 116 | + if (match) { |
| 117 | + throw new Error( |
| 118 | + `Invalid bare '&' at position ${match.index}: ` + |
| 119 | + `entity references must be &name; or &#num; or &#xHex;`, |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return text.replace(ENTITY_RE, (match, entity: string) => { |
| 125 | + if (entity.startsWith("#x")) { |
| 126 | + // Hexadecimal character reference |
| 127 | + const codePoint = parseInt(entity.slice(2), 16); |
| 128 | + // Invalid per XML 1.0 §4.1 WFC: Legal Character - must match Char production |
| 129 | + if (!isValidXmlChar(codePoint)) { |
| 130 | + return match; |
| 131 | + } |
| 132 | + return String.fromCodePoint(codePoint); |
| 133 | + } |
| 134 | + if (entity.startsWith("#")) { |
| 135 | + // Decimal character reference |
| 136 | + const codePoint = parseInt(entity.slice(1), 10); |
| 137 | + // Invalid per XML 1.0 §4.1 WFC: Legal Character - must match Char production |
| 138 | + if (!isValidXmlChar(codePoint)) { |
| 139 | + return match; |
| 140 | + } |
| 141 | + return String.fromCodePoint(codePoint); |
| 142 | + } |
| 143 | + // Named entity |
| 144 | + if (entity in NAMED_ENTITIES) { |
| 145 | + return NAMED_ENTITIES[entity as keyof typeof NAMED_ENTITIES]; |
| 146 | + } |
| 147 | + // Unknown entity - return as-is |
| 148 | + return match; |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Encodes special characters as XML entities. |
| 154 | + * |
| 155 | + * @param text The text to encode. |
| 156 | + * @returns The text with special characters encoded as entities. |
| 157 | + */ |
| 158 | +export function encodeEntities(text: string): string { |
| 159 | + // Fast path: no special characters means nothing to encode |
| 160 | + if (!/[<>&'"]/.test(text)) return text; |
| 161 | + return text.replace( |
| 162 | + SPECIAL_CHARS_RE, |
| 163 | + (char) => CHAR_TO_ENTITY[char as keyof typeof CHAR_TO_ENTITY], |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Encodes special characters for use in XML attribute values. |
| 169 | + * Encodes whitespace characters that would be normalized per XML 1.0 §3.3.3. |
| 170 | + * |
| 171 | + * @param value The attribute value to encode. |
| 172 | + * @returns The encoded attribute value. |
| 173 | + */ |
| 174 | +export function encodeAttributeValue(value: string): string { |
| 175 | + // Fast path: no special characters means nothing to encode |
| 176 | + if (!/[<>&'"\t\n\r]/.test(value)) return value; |
| 177 | + return value.replace(ATTR_ENCODE_RE, (c) => ATTR_CHAR_MAP[c]!); |
| 178 | +} |
0 commit comments