// Direct coverage for `xml.ts` — the escaping every sitemap, feed and OpenSearch document in this // package is built out of. Untested until now despite being the one place an unescaped `&` or a // `]]>` inside CDATA turns a valid document into one a crawler rejects. import { describe, expect, test } from 'bun:test'; import { escapeHtml } from '@ultimat3/core'; import { absoluteUrl, attributes, cdata, escapeXml, xmlElement } from './xml'; describe('escapeXml', () => { test('escapes all five special characters', () => { expect(escapeXml('&')).toBe('&'); expect(escapeXml('<')).toBe('<'); expect(escapeXml('>')).toBe('>'); expect(escapeXml('"')).toBe('"'); // `'`, a numeric reference: the same output is well-formed XML and valid HTML. expect(escapeXml("'")).toBe('''); }); test('leaves other characters untouched', () => { expect(escapeXml('hello world 123')).toBe('hello world 123'); }); test('a string with no special chars is returned unchanged', () => { const value = 'no special characters here'; expect(escapeXml(value)).toBe(value); }); test('encodes each special character independently, no double-escaping', () => { // if `&` were escaped first and the result re-scanned, the `&` inside `&` would be // re-escaped into `&amp;` — assert the exact one-pass output instead. expect(escapeXml('')).toBe( '<script>alert("x")&'y'</script>', ); }); test('all five special characters in sequence encode in order', () => { expect(escapeXml(`&<>"'`)).toBe('&<>"''); }); test("is core's character table, so seo and every HTML writer escape one set", () => { const value = `it's "quoted" & `; expect(escapeXml(value)).toBe(escapeHtml(value)); }); }); // XML 1.0 has no way to write these AT ALL — not raw, and not as ``, which is illegal too // (`Char` excludes them, and a numeric reference is a `Char`). So the only thing an emitter can do // with one is drop it. One byte of a scraped title, a paste out of a Word document or a `\x00` a // database column happily stored makes the WHOLE feed not well-formed, and a feed reader answers // that with "invalid XML" rather than with the item it could not parse. describe('a character XML 1.0 cannot represent', () => { const CONTROL = String.fromCharCode(1); const NUL = String.fromCharCode(0); test('escapeXml drops it instead of emitting it verbatim', () => { expect(escapeXml(`a${CONTROL}b`)).toBe('ab'); expect(escapeXml(`a${NUL}b`)).toBe('ab'); // The three C0 characters XML 1.0 DOES allow are not touched: a description holds newlines. expect(escapeXml('a\tb\nc\rd')).toBe('a\tb\nc\rd'); // The two non-characters at the end of the BMP are illegal for the same reason. const nonCharacters = `a${String.fromCharCode(0xfffe)}b${String.fromCharCode(0xffff)}c`; expect(escapeXml(nonCharacters)).toBe('abc'); // A character above the BMP is a surrogate PAIR and perfectly legal — dropping half of one // would be worse than the byte this rule exists for. expect(escapeXml('a👍b')).toBe('a👍b'); }); test('an attribute drops it too, so an attribute cannot break the document either', () => { expect(attributes({ href: `x${NUL}y` })).toBe(' href="xy"'); }); test('xmlElement inherits it, which is what a is built out of', () => { expect(xmlElement('title', `Q${CONTROL}1 results`)).toBe('<title>Q1 results'); }); test('cdata inherits it as well — CDATA suspends markup, never the character rule', () => { expect(cdata(`

a${CONTROL}b

`)).toBe('ab

]]>'); // And the escape it already had is untouched. expect(cdata('a]]>b')).toBe('b]]>'); }); }); describe('xmlElement', () => { test('wraps text in ...', () => { expect(xmlElement('title', 'hello')).toBe('hello'); }); test('escapes the text via escapeXml, not just wraps it', () => { expect(xmlElement('title', 'Fish & Chips ')).toBe( 'Fish & Chips <best>', ); }); test('apostrophes are escaped in element text too', () => { expect(xmlElement('title', "cook's tour")).toBe('cook's tour'); }); }); describe('cdata', () => { test('wraps a value in ', () => { expect(cdata('hello world')).toBe(''); }); test('splits a literal ]]> terminator so it cannot prematurely close the section', () => { expect(cdata(']]>')).toBe(']]>'); }); test('splits a ]]> terminator embedded within surrounding content', () => { const value = 'before]]>after'; const result = cdata(value); expect(result).toBe('after]]>'); // the section is well-formed: it opens exactly once and, per the standard splitting // technique, the embedded `]]>` only ever appears immediately followed by a fresh // `')).toBe(true); const reopens = result.split(']]>').length - 1; const opens = result.split(' { const value = 'a]]>b]]>c'; const result = cdata(value); expect(result).toBe('b]]]]>c]]>'); }); }); describe('attributes', () => { test('an empty object renders as an empty string', () => { expect(attributes({})).toBe(''); }); test('multiple entries render space-prefixed and joined, in insertion order', () => { expect(attributes({ href: '/a', title: 'A' })).toBe(' href="/a" title="A"'); }); test('values are escaped with the one table: apostrophes and quotes both', () => { expect(attributes({ title: `it's "quoted"` })).toBe(` title="it's "quoted""`); }); }); describe('absoluteUrl', () => { test('an http:// path is returned verbatim, ignoring baseUrl', () => { expect(absoluteUrl('https://example.com', 'http://other.com/x')).toBe('http://other.com/x'); }); test('an https:// path is returned verbatim, ignoring baseUrl', () => { expect(absoluteUrl('https://example.com', 'https://other.com/x')).toBe('https://other.com/x'); }); test('a non-http(s) absolute-looking path is NOT treated as passthrough', () => { expect(absoluteUrl('https://example.com', 'ftp://x')).toBe('https://example.com/ftp://x'); }); test('joins base and path with exactly one slash: neither has a slash', () => { expect(absoluteUrl('https://example.com', 'about')).toBe('https://example.com/about'); }); test('joins base and path with exactly one slash: base has a trailing slash', () => { expect(absoluteUrl('https://example.com/', 'about')).toBe('https://example.com/about'); }); test('joins base and path with exactly one slash: path has a leading slash', () => { expect(absoluteUrl('https://example.com', '/about')).toBe('https://example.com/about'); }); test('joins base and path with exactly one slash: both have a slash', () => { expect(absoluteUrl('https://example.com/', '/about')).toBe('https://example.com/about'); }); test('multiple internal slashes on the path collapse to one join point', () => { expect(absoluteUrl('https://example.com', '//foo')).toBe('https://example.com/foo'); }); // `/blog/` and `/blog` are different resources. This builds every `` and every canonical, // so stripping the slash the author wrote made a trailing-slash site publish URLs that redirect // — and made `assertCanonical` compare a canonical against a path it had just rewritten. test('a trailing slash the path declares is kept', () => { expect(absoluteUrl('https://example.com', 'about/')).toBe('https://example.com/about/'); expect(absoluteUrl('https://example.com/', '/blog/')).toBe('https://example.com/blog/'); expect(absoluteUrl('https://example.com', '/blog')).toBe('https://example.com/blog'); }); test('only the bare-root join collapses back to the base', () => { expect(absoluteUrl('https://example.com', '')).toBe('https://example.com'); expect(absoluteUrl('https://example.com/', '/')).toBe('https://example.com'); }); });