import { expect, it } from "vitest";
import { omitPrototypeKeys, parseXmlPropertyList } from "./propertyListKeys.js";
const plist = (body: string) =>
`${body}`;
it.each([
"__proto__",
"",
"__proto__",
"__proto__",
"__proto__",
"__proto__",
"",
])("omits and counts a dictionary keyed %s", (key) => {
const parsed = parseXmlPropertyList(
plist(
`CFBundleExecutableApp${key}pollutedlist${key}xkept1`,
),
);
expect(parsed).toEqual({
value: { CFBundleExecutable: "App", list: [{ kept: 1 }] },
omittedPrototypeKeys: 2,
});
expect(Object.getPrototypeOf(parsed.value)).toBe(Object.prototype);
});
it("keeps __proto__ text in strings, comments, and similar keys", () => {
const text = plist(
" __proto__ __proto__]]>__rea_prototype_key____proto__",
);
expect(parseXmlPropertyList(text)).toEqual({
value: {
" __proto__ ": "__proto__",
__rea_prototype_key__: "__proto__",
},
omittedPrototypeKeys: 0,
});
});
it("keeps an entity-encoded key that decodes to the placeholder", () => {
const text = plist(
"__rea_prototype_key__kept__proto__x",
);
expect(parseXmlPropertyList(text)).toEqual({
value: { __rea_prototype_key__: "kept" },
omittedPrototypeKeys: 1,
});
});
it("keeps entity-like text beyond Unicode in comments and CDATA", () => {
const text = plist(
"raw__proto__x",
);
expect(parseXmlPropertyList(text)).toEqual({
value: { raw: "" },
omittedPrototypeKeys: 1,
});
});
it("omits own __proto__ entries that JSON decoding creates", () => {
const decoded: unknown = JSON.parse(
'{"__proto__":{"x":1},"a":[{"__proto__":2,"b":3}]}',
);
expect(omitPrototypeKeys(decoded)).toEqual({
value: { a: [{ b: 3 }] },
omittedPrototypeKeys: 2,
});
});
it("preserves the pinned decoder's first-child key semantics", () => {
const text = plist(
"__prokept__proto__omitted",
);
expect(parseXmlPropertyList(text)).toEqual({
value: { __pro: "kept" },
omittedPrototypeKeys: 1,
});
});
it("preserves OpenStep dictionary and array autodetection", () => {
expect(
parseXmlPropertyList(
" { CFBundleExecutable = App; values = (one, two); __rea_prototype_key__ = kept; }",
),
).toEqual({
value: {
CFBundleExecutable: "App",
values: ["one", "two"],
__rea_prototype_key__: "kept",
},
omittedPrototypeKeys: 0,
});
expect(parseXmlPropertyList("(one, two)")).toEqual({
value: ["one", "two"],
omittedPrototypeKeys: 0,
});
});
it.each(["\n", "\r\n", "\r", "\u0085", "\u2028", "\u2029"])(
"preserves unrelated source values around %j line endings",
(newline) => {
const text = plist(
`${newline}kept
keya
b
c
d${newline}__proto__omitted`,
);
expect(parseXmlPropertyList(text)).toEqual({
value: { "kept\rkey": "a\rb\u0085c\u2028d" },
omittedPrototypeKeys: 1,
});
},
);
it("locates sparse omitted keys across newline-dense source", () => {
const lines = "\n".repeat(64 * 1024);
const text = plist(
`${lines}__proto__omittednested${lines}__proto__omittedkept`,
);
expect(parseXmlPropertyList(text)).toEqual({
value: { nested: { kept: true } },
omittedPrototypeKeys: 2,
});
});