All files / xml / _common.ts

99.53% Branches 210/211
100.00% Functions 8/8
98.92% Lines 276/279
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
 
 
 
 
 
 
x10
 
 
 
 
 
x10
 
 
 
 
 
x10
 
 
 
 
 
x10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x1397
x1397
 
 
 
 
 
 
 
x10
 
x60
x4
x4
x58
x57
x57
x1
x1
x57
x55
x60
 
 
 
 
 
 
 
x10
x19
x1
x1
 
x18
x19
x19
x19
x19
x2
x2
 
x19
x77
x77
x77
x77
x77
x77
x77
x77
x77
x1
x1
x77
x15
x19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x10
 
 
x72
x72
 
x8
x8
x8
x3
x3
x3
x3
x3
x3
x5
x5
x5
x5
x5
 
x64
x64
 
x64
x64
x64
x64
x64
x64
 
 
x72
x3
x3
 
 
x72
 
x112
x112
x112
x112
x112
x112
x862
x862
x112
 
 
 
 
 
 
 
x112
x66
x2
x2
x2
x2
x2
x64
x112
x22
x1
x1
x1
x1
x1
x22
x2
x2
x2
x2
x2
x19
x46
x16
x1
x1
x1
x1
x1
x15
x24
x24
x24
x24
x24
 
x6
x6
x6
x6
x8
x2
x2
x2
x2
x2
 
 
x112
x3
x3
 
 
x112
x1
x1
x1
x1
x1
x97
 
 
x112
x3
x3
 
 
x112
x1
x1
x1
x1
x1
x96
x112
x1
x1
x1
x1
x1
x95
 
 
x95
x112
x334
x334
x112
x1
x1
x1
x1
x1
x94
x94
 
 
x112
x60
x5
x5
x5
x5
x5
x5
x55
x112
x19
x4
x4
x4
x4
x4
x4
x15
x34
x15
x2
x2
x2
x2
x2
x13
x13
 
 
x112
 
 
x112
x54
x54
 
 
x112
x2
x2
x2
x2
x2
x112
 
x33
x33
x33
x72
x72
x72
x72
 
 
 
 
 
 
 
 
 
x10
x64
x64
 
 
 
 
 
 
x10
 
 
x10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x420
x333
x333
 
 
x409
x5
x5
 
 
x409
x2
x2
 
 
x409
x2
x2
 
x78
x420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x10
x10
 
x47
x47
x47
x47
 
 
 
x47
x1
x1
 
 
x47
x2
x2
 
 
x47
x1
x1
 
 
x47
x2
x2
 
 
x47
x1
x1
 
 
x47
x2
x2
 
 
x47
x1
x1
 
x37
x47
 
 
 
 
 
 
 
x10
x128
x128
x111
x111
x17
x17
x17
x17
x128
x128
x128
































































































































































































I





































































































































































































































































































// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

/**
 * Internal shared utilities for the XML module.
 *
 * @module
 */

import type { XmlName } from "./types.ts";

/**
 * Line ending normalization pattern per XML 1.0 §2.11.
 * Converts \r\n and standalone \r to \n.
 */
export const LINE_ENDING_REGEXP = /\r\n?/g;

/**
 * Whitespace-only test per XML 1.0 §2.3.
 * Uses explicit [ \t\r\n] instead of \s to match XML spec exactly:
 *   S ::= (#x20 | #x9 | #xD | #xA)+
 */
export const WHITESPACE_ONLY_REGEXP = /^[ \t\r\n]*$/;

/**
 * XML declaration version attribute pattern.
 * Matches both single and double quoted values.
 */
export const VERSION_REGEXP = /version\s*=\s*(?:"([^"]+)"|'([^']+)')/;

/**
 * XML declaration encoding attribute pattern.
 * Matches both single and double quoted values.
 */
export const ENCODING_REGEXP = /encoding\s*=\s*(?:"([^"]+)"|'([^']+)')/;

/**
 * XML declaration standalone attribute pattern.
 * Matches both single and double quoted values, restricted to "yes" or "no".
 */
export const STANDALONE_REGEXP = /standalone\s*=\s*(?:"(yes|no)"|'(yes|no)')/;

/**
 * Result of validating an XML declaration.
 */
export interface XmlDeclarationValidationResult {
  valid: true;
  version: string;
  encoding?: string;
  standalone?: "yes" | "no";
}

/**
 * Error result of validating an XML declaration.
 */
export interface XmlDeclarationValidationError {
  valid: false;
  error: string;
}

/**
 * XML whitespace characters per §2.3.
 */
function isXmlWhitespace(code: number): boolean {
  return code === 0x20 || code === 0x09 || code === 0x0A || code === 0x0D;
}

/**
 * Validates a VersionNum per XML 1.0 §2.8.
 * VersionNum ::= '1.' [0-9]+
 *
 * @returns true if valid, false otherwise
 */
function isValidVersionNum(version: string): boolean {
  // Must be "1." followed by one or more digits
  if (version.length < 3 || !version.startsWith("1.")) {
    return false;
  }
  for (let i = 2; i < version.length; i++) {
    const code = version.charCodeAt(i);
    if (code < 0x30 || code > 0x39) { // 0-9
      return false;
    }
  }
  return true;
}

/**
 * Validates an EncName per XML 1.0 §4.3.3.
 * EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
 *
 * @returns true if valid, false otherwise
 */
function isValidEncName(encoding: string): boolean {
  if (encoding.length === 0) {
    return false;
  }
  // First character must be a letter
  const first = encoding.charCodeAt(0);
  if (
    !(first >= 0x41 && first <= 0x5A) && // A-Z
    !(first >= 0x61 && first <= 0x7A) // a-z
  ) {
    return false;
  }
  // Rest can be letter, digit, '.', '_', '-'
  for (let i = 1; i < encoding.length; i++) {
    const code = encoding.charCodeAt(i);
    if (
      !(code >= 0x41 && code <= 0x5A) && // A-Z
      !(code >= 0x61 && code <= 0x7A) && // a-z
      !(code >= 0x30 && code <= 0x39) && // 0-9
      code !== 0x2E && // .
      code !== 0x5F && // _
      code !== 0x2D // -
    ) {
      return false;
    }
  }
  return true;
}

/**
 * Validates and parses an XML declaration content string.
 *
 * XML 1.0 §2.8 XMLDecl:
 *   XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
 *   VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
 *   EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )
 *   SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
 *   Eq ::= S? '=' S?
 *
 * This function validates:
 * - 'version' is required and must be first
 * - Attributes must have whitespace before them (after previous value/version)
 * - No duplicate attributes
 * - No unknown attributes
 * - Quote characters must match
 * - 'encoding' must come before 'standalone' if both present
 *
 * @param content The content between <?xml and ?> (whitespace trimmed by caller)
 * @returns Validation result with parsed values or error message
 */
export function validateXmlDeclaration(
  content: string,
): XmlDeclarationValidationResult | XmlDeclarationValidationError {
  // Fast-path: version is required and must be first, quick check before full parse
  const trimmed = content.trimStart();
  if (!trimmed.startsWith("version")) {
    // Check for common errors: case variants
    if (
      trimmed.startsWith("VERSION") || trimmed.startsWith("Version")
    ) {
      const name = trimmed.slice(0, 7);
      return {
        valid: false,
        error: `'${name}' must be lowercase in XML declaration`,
      };
    }
    return {
      valid: false,
      error: "Missing required 'version' attribute in XML declaration",
    };
  }

  let pos = 0;
  const len = content.length;

  let version: string | undefined;
  let encoding: string | undefined;
  let standalone: "yes" | "no" | undefined;
  let foundVersion = false;
  let foundEncoding = false;
  let foundStandalone = false;

  // Skip leading whitespace
  while (pos < len && isXmlWhitespace(content.charCodeAt(pos))) {
    pos++;
  }

  // Parse attributes
  while (pos < len) {
    // Read attribute name
    const nameStart = pos;
    while (
      pos < len &&
      !isXmlWhitespace(content.charCodeAt(pos)) &&
      content.charCodeAt(pos) !== 0x3D // =
    ) {
      pos++;
    }
    const name = content.slice(nameStart, pos);

    if (name === "") {
      // Trailing whitespace only
      break;
    }

    // Check for valid attribute names and order
    if (name === "version") {
      if (foundVersion) {
        return {
          valid: false,
          error: "Duplicate 'version' attribute in XML declaration",
        };
      }
      foundVersion = true;
    } else if (name === "encoding") {
      if (foundEncoding) {
        return {
          valid: false,
          error: "Duplicate 'encoding' attribute in XML declaration",
        };
      }
      if (foundStandalone) {
        return {
          valid: false,
          error: "'encoding' must come before 'standalone' in XML declaration",
        };
      }
      foundEncoding = true;
    } else if (name === "standalone") {
      if (foundStandalone) {
        return {
          valid: false,
          error: "Duplicate 'standalone' attribute in XML declaration",
        };
      }
      foundStandalone = true;
    } else if (
      name === "VERSION" || name === "Version" ||
      name === "ENCODING" || name === "Encoding" ||
      name === "STANDALONE" || name === "Standalone"
    ) {
      // Case-sensitive check - these must be lowercase
      return {
        valid: false,
        error: `'${name}' must be lowercase in XML declaration`,
      };
    } else {
      return {
        valid: false,
        error: `Unknown attribute '${name}' in XML declaration`,
      };
    }

    // Skip whitespace before =
    while (pos < len && isXmlWhitespace(content.charCodeAt(pos))) {
      pos++;
    }

    // Expect =
    if (pos >= len || content.charCodeAt(pos) !== 0x3D) {
      return {
        valid: false,
        error: `Expected '=' after '${name}' in XML declaration`,
      };
    }
    pos++; // Skip =

    // Skip whitespace after =
    while (pos < len && isXmlWhitespace(content.charCodeAt(pos))) {
      pos++;
    }

    // Expect quote
    if (pos >= len) {
      return {
        valid: false,
        error: `Expected quoted value for '${name}' in XML declaration`,
      };
    }
    const quote = content.charCodeAt(pos);
    if (quote !== 0x22 && quote !== 0x27) { // " or '
      return {
        valid: false,
        error: `Expected quote for '${name}' value in XML declaration`,
      };
    }
    pos++; // Skip opening quote

    // Read value until matching quote
    const valueStart = pos;
    while (pos < len && content.charCodeAt(pos) !== quote) {
      pos++;
    }
    if (pos >= len) {
      return {
        valid: false,
        error: `Unterminated ${name} value in XML declaration`,
      };
    }
    const value = content.slice(valueStart, pos);
    pos++; // Skip closing quote

    // Validate and store value
    if (name === "version") {
      if (!isValidVersionNum(value)) {
        return {
          valid: false,
          error:
            `Invalid version '${value}': must match '1.' followed by digits`,
        };
      }
      version = value;
    } else if (name === "encoding") {
      if (!isValidEncName(value)) {
        return {
          valid: false,
          error:
            `Invalid encoding name '${value}': must start with a letter and contain only letters, digits, '.', '_', '-'`,
        };
      }
      encoding = value;
    } else if (name === "standalone") {
      if (value !== "yes" && value !== "no") {
        return {
          valid: false,
          error: `Invalid standalone value '${value}': must be 'yes' or 'no'`,
        };
      }
      standalone = value;
    }

    // Check for required whitespace before next attribute
    const hadWhitespace = pos < len && isXmlWhitespace(content.charCodeAt(pos));

    // Skip whitespace
    while (pos < len && isXmlWhitespace(content.charCodeAt(pos))) {
      pos++;
    }

    // If there's more content, whitespace was required
    if (pos < len && !hadWhitespace) {
      return {
        valid: false,
        error: "Missing whitespace between attributes in XML declaration",
      };
    }
  }

  return {
    valid: true,
    version: version!,
    ...(encoding !== undefined && { encoding }),
    ...(standalone !== undefined && { standalone }),
  };
}

/**
 * Checks if a PI target is a case-variant of "xml" (which is reserved).
 *
 * XML 1.0 §2.6: "The target names 'XML', 'xml', and so on are reserved for
 * standardization in this or future versions of this specification."
 *
 * @returns true if the target is reserved (any case variant of "xml")
 */
export function isReservedPiTarget(target: string): boolean {
  return target.length === 3 && target.toLowerCase() === "xml";
}

// =============================================================================
// NAMESPACE VALIDATION (XML Namespaces 1.0)
// =============================================================================

/** The reserved XML namespace URI. */
export const XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";

/** The reserved xmlns namespace URI. */
export const XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";

/**
 * Validates QName syntax per XML Namespaces 1.0.
 *
 * A QName must be either:
 * - An NCName (no colons)
 * - A prefix:local pair where both prefix and local are valid NCNames
 *
 * This validates:
 * - No colon at start (:foo is invalid)
 * - No colon at end (foo: is invalid)
 * - No multiple colons (a:b:c is invalid)
 *
 * @param colonIndex The index of the first colon, or -1 if none.
 * @returns Error message if invalid, null if valid.
 */
export function validateQName(name: string, colonIndex: number): string | null {
  if (colonIndex === -1) {
    return null; // No colon means valid NCName (already validated by name char rules)
  }

  // Colon at start (:foo) - invalid
  if (colonIndex === 0) {
    return "QName cannot start with ':'";
  }

  // Colon at end (foo:) - invalid
  if (colonIndex === name.length - 1) {
    return "QName cannot end with ':'";
  }

  // Multiple colons (a:b:c) - invalid
  if (name.indexOf(":", colonIndex + 1) !== -1) {
    return "QName cannot contain multiple ':'";
  }

  return null; // Valid QName
}

/**
 * Validates a namespace binding (xmlns or xmlns:prefix attribute).
 *
 * Per XML Namespaces 1.0:
 * - xmlns:prefix="" (prefix unbinding) is NOT allowed in NS 1.0
 * - xmlns:xmlns is NOT allowed (cannot declare xmlns prefix)
 * - xmlns:xml must bind to the correct XML namespace
 * - Only xml prefix can bind to the XML namespace
 * - Nothing can bind to the xmlns namespace
 * - Default namespace cannot be xml or xmlns namespace
 *
 * @returns Error message if invalid, null if valid.
 */
export function validateNamespaceBinding(
  attrName: string,
  attrValue: string,
): string | null {
  const colonIndex = attrName.indexOf(":");
  const isDefaultNs = attrName === "xmlns";
  const prefix = isDefaultNs ? "" : attrName.slice(colonIndex + 1);
  const uri = attrValue;

  // xmlns: with empty local part is caught by QName validation
  // But check for empty prefix after xmlns:
  if (colonIndex !== -1 && prefix === "") {
    return "Cannot have empty namespace prefix after 'xmlns:'";
  }

  // Prefix unbinding not allowed in NS 1.0 (xmlns:foo="")
  if (!isDefaultNs && uri === "") {
    return "Cannot unbind namespace prefix (empty URI) in Namespaces 1.0";
  }

  // Cannot declare xmlns prefix at all
  if (prefix === "xmlns") {
    return "Cannot declare the 'xmlns' prefix";
  }

  // xml prefix must use correct URI
  if (prefix === "xml" && uri !== XML_NAMESPACE) {
    return `Cannot bind 'xml' prefix to '${uri}': must use '${XML_NAMESPACE}'`;
  }

  // Only xml prefix can use xml namespace
  if (uri === XML_NAMESPACE && prefix !== "xml" && prefix !== "") {
    return `Cannot bind prefix '${prefix}' to '${XML_NAMESPACE}': only the 'xml' prefix may use this namespace`;
  }

  // Nothing can bind to xmlns namespace (includes default namespace case)
  if (uri === XMLNS_NAMESPACE) {
    return `Cannot bind any prefix to '${XMLNS_NAMESPACE}'`;
  }

  // Default namespace cannot be xml namespace
  if (isDefaultNs && uri === XML_NAMESPACE) {
    return `Cannot use '${XML_NAMESPACE}' as the default namespace`;
  }

  return null; // Valid binding
}

/**
 * Parses a qualified XML name into its prefix and local parts.
 *
 * @param uri Optional resolved namespace URI for prefixed names.
 * @returns An XmlName object with raw, local, optional prefix, and optional uri.
 */
export function parseName(name: string, uri?: string): XmlName {
  const colonIndex = name.indexOf(":");
  if (colonIndex === -1) {
    return { raw: name, local: name };
  }
  return {
    raw: name,
    prefix: name.slice(0, colonIndex),
    local: name.slice(colonIndex + 1),
    ...(uri !== undefined && { uri }),
  };
}