Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scripts/docs/generate-topics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,10 @@ function firstSentence(text) {
return (match ? match[1] : clean).trim();
}

// A Markdown table cell: backslashes first, so that an escaped pipe in the
// source does not turn into a cell boundary, then the pipes themselves.
function escapeCell(text) {
return text.replace(/\|/g, "\\|").replace(/\s+/g, " ").trim();
return text.replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\s+/g, " ").trim();
}

function renderPage(entry, language, content, sourceContent, pages, thrownBy) {
Expand Down Expand Up @@ -874,7 +876,7 @@ function localiseTables(english, translated, language, summaries) {

const localised = cells.slice();

localised[localised.length - 1] = ` ${summary.replace(/\|/g, "\\|")} `;
localised[localised.length - 1] = ` ${escapeCell(summary)} `;

return `|${localised.join("|")}|`;
})
Expand Down
5 changes: 1 addition & 4 deletions scripts/docs/sync-jsdoc-links.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ for (const file of listSources(SRC).sort()) {

// A link written before the casing was fixed is corrected in place rather
// than left beside its replacement.
const corrected = text.replace(
new RegExp(`${SITE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/${name}\\.html`, "g"),
page
);
const corrected = text.replaceAll(`${SITE}/${name}.html`, page);

const missing = [...wanted].filter(([, url]) => !corrected.includes(url));

Expand Down
42 changes: 30 additions & 12 deletions src/json/parseJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,29 @@ export function parseJson(value: string): object {
return JSON.parse(result);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err: unknown) {
/**
* Matches a complete double-quoted JSON string literal, escapes included.
* The tokenizer below turns every quoted string, single-quoted ones too,
* into this form.
*/
const quotedString = /^"(?:[^"\\]|\\.)*"$/;

/**
* Checks if a token is a valid key format (either quoted or unquoted alphanumeric).
*
* @param {string} token The token string to check.
* @returns {boolean} `true` if the token is a valid key format; otherwise, `false`.
*/
const isValidKey = (token: string): boolean =>
/^"[^"]*"$/.test(token) || /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(token);
quotedString.test(token) || /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(token);

/**
* Returns a key token as a JSON string literal.
*
* @param {string} token A token accepted by `isValidKey`.
* @returns {string} The token itself when it is already quoted; otherwise the token in double quotes.
*/
const toKey = (token: string): string => (quotedString.test(token) ? token : `"${token}"`);

/**
* Checks if a token is a valid value format (boolean, null, number, or quoted string).
Expand All @@ -59,8 +74,7 @@ export function parseJson(value: string): object {
token === "false" ||
token === "null" ||
/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(token) ||
/^"[^"]*"$/.test(token) ||
/^'[^']*'$/.test(token);
quotedString.test(token);

const tokens = [];

Expand All @@ -76,6 +90,12 @@ export function parseJson(value: string): object {
const char = result[i];

if (isEscaped) {
// `\'` is how a single-quoted string carries a quote; JSON has no such
// escape, and inside double quotes the quote needs none.
if (char === "'" && isString) {
currentToken = currentToken.slice(0, -1);
}

currentToken += char;
isEscaped = false;

Expand All @@ -97,7 +117,9 @@ export function parseJson(value: string): object {
tokens.push(currentToken);
currentToken = "";
} else {
currentToken += char;
// A double quote inside a single-quoted string has to be escaped
// once the string is re-quoted with double quotes.
currentToken += char === '"' ? '\\"' : char;
}
} else {
isString = true;
Expand Down Expand Up @@ -153,19 +175,15 @@ export function parseJson(value: string): object {
expectingKey = correctedTokens[correctedTokens.length - 2] === "}";
expectingValue = !expectingKey;
} else if (expectingKey && isValidKey(token)) {
correctedTokens.push(`"${token.replace(/"/g, "")}"`);
correctedTokens.push(toKey(token));
expectingKey = false;
} else if (expectingValue && isValidValue(token)) {
correctedTokens.push(
token.match(/^'[^']*'$/) ? `"${token.slice(1, -1).replace(/"/g, '\\"')}"` : token
);
correctedTokens.push(token);
expectingValue = false;
} else if (isValidKey(token)) {
correctedTokens.push(`"${token.replace(/"/g, "")}"`);
correctedTokens.push(toKey(token));
} else if (isValidValue(token)) {
correctedTokens.push(
token.match(/^'[^']*'$/) ? `"${token.slice(1, -1).replace(/"/g, '\\"')}"` : token
);
correctedTokens.push(token);
} else {
throw new Error(`Invalid token: ${token}`);
}
Expand Down
32 changes: 32 additions & 0 deletions test/json/parseJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ describe("parseJson", () => {
it("should accept unquoted keys", () => {
expect(parseJson("{a:1}")).toEqual({ a: 1 });
});

it("should keep double quotes inside a single-quoted value", () => {
expect(parseJson(`{'a':'say "hi"'}`)).toEqual({ a: 'say "hi"' });
});

it("should keep double quotes inside a single-quoted key", () => {
expect(parseJson(`{'say "hi"':1}`)).toEqual({ 'say "hi"': 1 });
});

it("should unescape an escaped single quote in a single-quoted value", () => {
expect(parseJson(String.raw`{'a':'it\'s'}`)).toEqual({ a: "it's" });
});

it("should keep backslash escapes in a single-quoted value", () => {
expect(parseJson(String.raw`{'a':'C:\\dir\n'}`)).toEqual({ a: "C:\\dir\n" });
});

it("should keep an escaped double quote in a double-quoted value", () => {
expect(parseJson(String.raw`{a:"x\"y"}`)).toEqual({ a: 'x"y' });
});

it("should keep a backslash before a double quote in a single-quoted value", () => {
expect(parseJson(String.raw`{'a':'x\\"y'}`)).toEqual({ a: 'x\\"y' });
});

it("should accept mixed quoted and unquoted keys", () => {
expect(parseJson(`{ "foo": "bar", baz: true, 'qux': null }`)).toEqual({
foo: "bar",
baz: true,
qux: null
});
});
});

describe("Input it rejects", () => {
Expand Down
Loading