All files / src/generators/sitemap xml-utils.ts

100% Statements 1/1
100% Branches 0/0
100% Functions 1/1
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                              41779x              
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/Sitemap/XmlUtils
 * @description Tiny string-escaping helpers shared by the XML sitemap and
 * RSS-feed generators. Lifted out of `sitemap.ts` so the helpers can be
 * tested in isolation and so any future XML output (Atom, news-sitemap,
 * etc.) can reuse the same canonical escape function instead of growing a
 * fourth copy.
 *
 * The single function below escapes the **five predefined XML entities**
 * (`&`, `<`, `>`, `"`, `'`). The order matters — `&` MUST be escaped first
 * so that subsequent `&lt;`/`&gt;` etc. aren't double-encoded.
 */
 
/**
 * Escape a string for safe embedding inside an XML text node or attribute.
 *
 * @param str - Raw string
 * @returns XML-safe string with the five predefined entities escaped
 */
export function escapeXML(str: string): string {
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&apos;');
}