All files / scripts/rss escape.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                                          448x              
/**
 * @module Infrastructure/Rss/Escape
 * @category Intelligence Operations / Supporting Infrastructure
 * @name XML escaper for RSS payloads
 *
 * @description
 * Pure-string XML escaper used by every `<item>` field in `rss.xml`.
 * Preserves valid pre-encoded entities (`&amp;`, `&#39;`, `&lt;`, etc.)
 * by only escaping `&` when it is not already part of an entity.
 *
 * Round-6 split: extracted from `scripts/generate-rss.ts`.
 *
 * @author Hack23 AB (Infrastructure Team)
 * @license Apache-2.0
 */
 
/**
 * Escape XML special characters while preserving valid pre-encoded
 * entities. Used for both attribute values and text content.
 */
export function escapeXml(text: string): string {
  return text
    .replace(/&(?!(?:#\d+|#x[0-9a-fA-F]+|[a-zA-Z]+);)/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&apos;');
}