Skip to content

Commit b240a06

Browse files
refactor: move encode HTML function into separate module (anuraghazra#4591)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
1 parent af1d8ca commit b240a06

9 files changed

Lines changed: 30 additions & 24 deletions

File tree

src/cards/gist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-check
22

33
import {
4-
encodeHTML,
54
measureText,
65
flexLayout,
76
iconWithLabel,
@@ -10,6 +9,7 @@ import {
109
import Card from "../common/Card.js";
1110
import { getCardColors } from "../common/color.js";
1211
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
12+
import { encodeHTML } from "../common/html.js";
1313
import { icons } from "../common/icons.js";
1414
import { parseEmojis } from "../common/ops.js";
1515

src/cards/repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import { Card } from "../common/Card.js";
44
import { getCardColors } from "../common/color.js";
55
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
6+
import { encodeHTML } from "../common/html.js";
67
import { I18n } from "../common/I18n.js";
78
import { icons } from "../common/icons.js";
89
import { clampValue, parseEmojis } from "../common/ops.js";
910
import {
10-
encodeHTML,
1111
flexLayout,
1212
measureText,
1313
iconWithLabel,

src/common/Card.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { encodeHTML, flexLayout } from "./render.js";
1+
// @ts-check
2+
3+
import { encodeHTML } from "./html.js";
4+
import { flexLayout } from "./render.js";
25

36
class Card {
47
/**

src/common/fmt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
import wrap from "word-wrap";
4-
import { encodeHTML } from "./render.js";
4+
import { encodeHTML } from "./html.js";
55

66
/**
77
* Retrieves num with suffix k(thousands) precise to given decimal places.

src/common/html.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
/**
4+
* Encode string as HTML.
5+
*
6+
* @see https://stackoverflow.com/a/48073476/10629172
7+
*
8+
* @param {string} str String to encode.
9+
* @returns {string} Encoded string.
10+
*/
11+
const encodeHTML = (str) => {
12+
return str
13+
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
14+
return "&#" + i.charCodeAt(0) + ";";
15+
})
16+
.replace(/\u0008/gim, "");
17+
};
18+
19+
export { encodeHTML };

src/common/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export { retryer } from "./retryer.js";
99
export {
1010
ERROR_CARD_LENGTH,
1111
renderError,
12-
encodeHTML,
1312
flexLayout,
1413
measureText,
1514
} from "./render.js";

src/common/render.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
44
import { getCardColors } from "./color.js";
5+
import { encodeHTML } from "./html.js";
56

67
/**
78
* Auto layout utility, allows us to layout things vertically or horizontally with
@@ -76,22 +77,6 @@ const iconWithLabel = (icon, label, testid, iconSize) => {
7677
// Script parameters.
7778
const ERROR_CARD_LENGTH = 576.5;
7879

79-
/**
80-
* Encode string as HTML.
81-
*
82-
* @see https://stackoverflow.com/a/48073476/10629172
83-
*
84-
* @param {string} str String to encode.
85-
* @returns {string} Encoded string.
86-
*/
87-
const encodeHTML = (str) => {
88-
return str
89-
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
90-
return "&#" + i.charCodeAt(0) + ";";
91-
})
92-
.replace(/\u0008/gim, "");
93-
};
94-
9580
const UPSTREAM_API_ERRORS = [
9681
TRY_AGAIN_LATER,
9782
SECONDARY_ERROR_MESSAGES.MAX_RETRY,
@@ -207,7 +192,6 @@ export {
207192
renderError,
208193
createLanguageNode,
209194
iconWithLabel,
210-
encodeHTML,
211195
flexLayout,
212196
measureText,
213197
};

src/translations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
import { encodeHTML } from "./common/render.js";
3+
import { encodeHTML } from "./common/html.js";
44

55
/**
66
* Retrieves stat card labels in the available locales.

tests/render.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import { describe, expect, it } from "@jest/globals";
44
import { queryByTestId } from "@testing-library/dom";
55
import "@testing-library/jest-dom/jest-globals";
6-
import { encodeHTML, renderError } from "../src/common/render.js";
6+
import { renderError } from "../src/common/render.js";
7+
import { encodeHTML } from "../src/common/html.js";
78

89
describe("Test render.js", () => {
910
it("should test encodeHTML", () => {

0 commit comments

Comments
 (0)