refactor: move encode HTML function into separate module (#4591)

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
Alexandr Garbuzov
2025-10-25 20:29:24 +02:00
committed by martin-mfg
co-authored by Alexandr
parent aef861ec08
commit 8c754e3aec
9 changed files with 30 additions and 24 deletions
+1 -1
View File
@@ -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";
+1 -1
View File
@@ -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,
+4 -1
View File
@@ -1,4 +1,7 @@
import { encodeHTML, flexLayout } from "./render.js";
// @ts-check
import { encodeHTML } from "./html.js";
import { flexLayout } from "./render.js";
class Card {
/**
+1 -1
View File
@@ -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.
+19
View File
@@ -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 };
-1
View File
@@ -9,7 +9,6 @@ export { retryer } from "./retryer.js";
export {
ERROR_CARD_LENGTH,
renderError,
encodeHTML,
flexLayout,
measureText,
} from "./render.js";
+1 -17
View File
@@ -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,
+1 -1
View File
@@ -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.
+2 -1
View File
@@ -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", () => {