diff --git a/backend/src/cards/gist.js b/backend/src/cards/gist.js index d5f69cc0..5bc88ea8 100644 --- a/backend/src/cards/gist.js +++ b/backend/src/cards/gist.js @@ -1,7 +1,6 @@ // @ts-check import { - encodeHTML, measureText, flexLayout, iconWithLabel, @@ -10,6 +9,7 @@ import { import Card from "../common/Card.js"; import { getCardColors } from "../common/color.js"; import { kFormatter, wrapTextMultiline } from "../common/fmt.js"; +import { encodeHTML } from "../common/html.js"; import { icons } from "../common/icons.js"; import languageColors from "../common/languageColors.json" with { type: "json" }; import { parseEmojis } from "../common/ops.js"; diff --git a/backend/src/cards/repo.js b/backend/src/cards/repo.js index 29266917..aac1d353 100644 --- a/backend/src/cards/repo.js +++ b/backend/src/cards/repo.js @@ -3,12 +3,12 @@ import { Card } from "../common/Card.js"; import { getCardColors } from "../common/color.js"; import { kFormatter, wrapTextMultiline } from "../common/fmt.js"; +import { encodeHTML } from "../common/html.js"; import { I18n } from "../common/I18n.js"; import { icons } from "../common/icons.js"; import { clampValue, parseEmojis } from "../common/ops.js"; import { buildSearchFilter } from "../common/utils.js"; import { - encodeHTML, flexLayout, measureText, iconWithLabel, diff --git a/backend/src/common/Card.js b/backend/src/common/Card.js index 2c2c2e19..3e7a80e8 100644 --- a/backend/src/common/Card.js +++ b/backend/src/common/Card.js @@ -1,4 +1,7 @@ -import { encodeHTML, flexLayout } from "./render.js"; +// @ts-check + +import { encodeHTML } from "./html.js"; +import { flexLayout } from "./render.js"; class Card { /** diff --git a/backend/src/common/fmt.js b/backend/src/common/fmt.js index a292d002..5820b53e 100644 --- a/backend/src/common/fmt.js +++ b/backend/src/common/fmt.js @@ -1,7 +1,7 @@ // @ts-check import wrap from "word-wrap"; -import { encodeHTML } from "./render.js"; +import { encodeHTML } from "./html.js"; /** * Retrieves num with suffix k(thousands) precise to given decimal places. diff --git a/backend/src/common/html.js b/backend/src/common/html.js new file mode 100644 index 00000000..2b1db470 --- /dev/null +++ b/backend/src/common/html.js @@ -0,0 +1,19 @@ +// @ts-check + +/** + * Encode string as HTML. + * + * @see https://stackoverflow.com/a/48073476/10629172 + * + * @param {string} str String to encode. + * @returns {string} Encoded string. + */ +const encodeHTML = (str) => { + return str + .replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => { + return "&#" + i.charCodeAt(0) + ";"; + }) + .replace(/\u0008/gim, ""); +}; + +export { encodeHTML }; diff --git a/backend/src/common/index.js b/backend/src/common/index.js index e87f04a6..ccedfa9a 100644 --- a/backend/src/common/index.js +++ b/backend/src/common/index.js @@ -9,7 +9,6 @@ export { retryer } from "./retryer.js"; export { ERROR_CARD_LENGTH, renderError, - encodeHTML, flexLayout, measureText, } from "./render.js"; diff --git a/backend/src/common/render.js b/backend/src/common/render.js index a4b5573c..527e4533 100644 --- a/backend/src/common/render.js +++ b/backend/src/common/render.js @@ -2,6 +2,7 @@ import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js"; import { getCardColors } from "./color.js"; +import { encodeHTML } from "./html.js"; const OWNER_AFFILIATIONS = ["OWNER", "COLLABORATOR", "ORGANIZATION_MEMBER"]; @@ -90,22 +91,6 @@ const buildSearchFilter = (repos = [], owners = []) => { // Script parameters. const ERROR_CARD_LENGTH = 576.5; -/** - * Encode string as HTML. - * - * @see https://stackoverflow.com/a/48073476/10629172 - * - * @param {string} str String to encode. - * @returns {string} Encoded string. - */ -const encodeHTML = (str) => { - return str - .replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => { - return "&#" + i.charCodeAt(0) + ";"; - }) - .replace(/\u0008/gim, ""); -}; - const UPSTREAM_API_ERRORS = [ TRY_AGAIN_LATER, SECONDARY_ERROR_MESSAGES.MAX_RETRY, @@ -252,7 +237,6 @@ export { renderError, createLanguageNode, iconWithLabel, - encodeHTML, buildSearchFilter, flexLayout, OWNER_AFFILIATIONS, diff --git a/backend/src/translations.js b/backend/src/translations.js index 3b9ef1f5..5ba7e96e 100644 --- a/backend/src/translations.js +++ b/backend/src/translations.js @@ -1,6 +1,6 @@ // @ts-check -import { encodeHTML } from "./common/render.js"; +import { encodeHTML } from "./common/html.js"; /** * Retrieves stat card labels in the available locales. diff --git a/backend/tests/render.test.js b/backend/tests/render.test.js index 026bc9fc..935b7335 100644 --- a/backend/tests/render.test.js +++ b/backend/tests/render.test.js @@ -3,7 +3,8 @@ import { describe, expect, it } from "@jest/globals"; import { queryByTestId } from "@testing-library/dom"; import "@testing-library/jest-dom/jest-globals"; -import { encodeHTML, renderError } from "../src/common/render.js"; +import { renderError } from "../src/common/render.js"; +import { encodeHTML } from "../src/common/html.js"; describe("Test render.js", () => { it("should test encodeHTML", () => {