:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { describe, expect, it } from "vitest";
import { escapeXmlAttr, unescapeXmlAttr } from "./xml";
describe("escapeXmlAttr", () => {
it("escapes the five XML attribute metacharacters", () => {
expect(escapeXmlAttr(`&<>"'`)).toBe("&<>"'");
});
it("escapes ampersands before other entities so output is not double-escaped on reverse", () => {
expect(escapeXmlAttr("a & b")).toBe("a & b");
});
it("leaves plain text untouched", () => {
expect(escapeXmlAttr("hello world")).toBe("hello world");
});
});
describe("unescapeXmlAttr", () => {
it("reverses the five entities", () => {
expect(unescapeXmlAttr("&<>"'")).toBe(`&<>"'`);
});
});
describe("escape/unescape round-trip", () => {
it.each([
`&<>"'`,
`tag <a href="x">y</a>`,
"literal & entity",
"ampersands & < mixed > with \" quotes ' and more",
"plain",
])("round-trips %j", (input) => {
expect(unescapeXmlAttr(escapeXmlAttr(input))).toBe(input);
});
});