@prairielearn/html 4.0.9 → 4.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
- package/src/index.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type HtmlValue = string | number | boolean | bigint | HtmlSafeString | un
|
|
|
8
8
|
export declare function html(strings: TemplateStringsArray, ...values: HtmlValue[]): HtmlSafeString;
|
|
9
9
|
/**
|
|
10
10
|
* Pre-escapes the rendered HTML. Useful for when you want to inline the HTML
|
|
11
|
-
* in something else, for instance in a `data-content` attribute for a Bootstrap
|
|
11
|
+
* in something else, for instance in a `data-bs-content` attribute for a Bootstrap
|
|
12
12
|
* popover.
|
|
13
13
|
*/
|
|
14
14
|
export declare function escapeHtml(html: HtmlSafeString): HtmlSafeString;
|
package/dist/index.js
CHANGED
|
@@ -60,7 +60,7 @@ export function html(strings, ...values) {
|
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* Pre-escapes the rendered HTML. Useful for when you want to inline the HTML
|
|
63
|
-
* in something else, for instance in a `data-content` attribute for a Bootstrap
|
|
63
|
+
* in something else, for instance in a `data-bs-content` attribute for a Bootstrap
|
|
64
64
|
* popover.
|
|
65
65
|
*/
|
|
66
66
|
export function escapeHtml(html) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,iBAAiB,GAA2B;IAChD,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,OAAO;CACb,CAAC;AACF,MAAM,UAAU,GAAG,UAAU,CAAC;A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": ''',\n};\nconst MATCH_HTML = /[&<>'\"]/g;\n\nfunction encodeCharacter(c: string) {\n return ENCODE_HTML_RULES[c] || c;\n}\n\n/**\n * Based on the `escapeXML` function from the `ejs` library.\n */\nfunction escapeHtmlRaw(value: string): string {\n return value == null ? '' : String(value).replace(MATCH_HTML, encodeCharacter);\n}\n\nfunction escapeValue(value: unknown): string {\n if (value instanceof HtmlSafeString) {\n // Already escaped!\n return value.toString();\n } else if (Array.isArray(value)) {\n return value.map((val) => escapeValue(val)).join('');\n } else if (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'bigint' ||\n typeof value === 'boolean'\n ) {\n return escapeHtmlRaw(String(value));\n } else if (value == null) {\n // undefined or null -- render nothing\n return '';\n } else if (typeof value === 'object') {\n throw new Error(`Cannot interpolate object in template: ${JSON.stringify(value)}`);\n } else {\n // There shouldn't be any other types\n throw new Error(\n `Unexpected type in template: ${typeof value} for value ${JSON.stringify(value)}`,\n );\n }\n}\n\n// Based on https://github.com/Janpot/escape-html-template-tag\nexport class HtmlSafeString {\n private readonly strings: readonly string[];\n private readonly values: unknown[];\n\n constructor(strings: readonly string[], values: unknown[]) {\n this.strings = strings;\n this.values = values;\n }\n\n toString(): string {\n return this.values.reduce<string>((acc, val, i) => {\n return acc + escapeValue(val) + this.strings[i + 1];\n }, this.strings[0]);\n }\n}\n\nexport type HtmlValue =\n | string\n | number\n | boolean\n | bigint\n | HtmlSafeString\n | undefined\n | null\n | HtmlValue[];\n\nexport function html(strings: TemplateStringsArray, ...values: HtmlValue[]): HtmlSafeString {\n return new HtmlSafeString(strings, values);\n}\n\n/**\n * Pre-escapes the rendered HTML. Useful for when you want to inline the HTML\n * in something else, for instance in a `data-content` attribute for a Bootstrap\n * popover.\n */\nexport function escapeHtml(html: HtmlSafeString): HtmlSafeString {\n return unsafeHtml(escapeHtmlRaw(html.toString()));\n}\n\n/**\n * Will render the provided value without any additional escaping. Use carefully\n * with user-provided data.\n *\n * @param value The value to render.\n * @returns An {@link HtmlSafeString} representing the provided value.\n */\nexport function unsafeHtml(value: string): HtmlSafeString {\n return new HtmlSafeString([value], []);\n}\n\n/**\n * Joins a list of HTML values with a separator.\n *\n * @param values The values to join.\n * @param separator The separator to use between values.\n */\nexport function joinHtml(values: HtmlValue[], separator: HtmlValue = ''): HtmlSafeString {\n return unsafeHtml(values.map(escapeValue).join(escapeValue(separator)));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,iBAAiB,GAA2B;IAChD,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,MAAM;IACX,GAAG,EAAE,OAAO;IACZ,GAAG,EAAE,OAAO;CACb,CAAC;AACF,MAAM,UAAU,GAAG,UAAU,CAAC;AAE9B,SAAS,eAAe,CAAC,CAAS;IAChC,OAAO,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC;AAED;;GAEG;AACH,SAAS,aAAa,CAAC,KAAa;IAClC,OAAO,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC;AACjF,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,IAAI,KAAK,YAAY,cAAc,EAAE,CAAC;QACpC,mBAAmB;QACnB,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC1B,CAAC;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACvD,CAAC;SAAM,IACL,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAA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OAAO,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,IAAI,cAAc,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAmB,EAAE,YAAuB,EAAE;IACrE,OAAO,UAAU,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AAC1E,CAAC","sourcesContent":["const ENCODE_HTML_RULES: Record<string, string> = {\n '&': '&',\n '<': '<',\n '>': '>',\n '\"': '"',\n \"'\": ''',\n};\nconst MATCH_HTML = /[&<>'\"]/g;\n\nfunction encodeCharacter(c: string) {\n return ENCODE_HTML_RULES[c] || c;\n}\n\n/**\n * Based on the `escapeXML` function from the `ejs` library.\n */\nfunction escapeHtmlRaw(value: string): string {\n return value == null ? '' : String(value).replace(MATCH_HTML, encodeCharacter);\n}\n\nfunction escapeValue(value: unknown): string {\n if (value instanceof HtmlSafeString) {\n // Already escaped!\n return value.toString();\n } else if (Array.isArray(value)) {\n return value.map((val) => escapeValue(val)).join('');\n } else if (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'bigint' ||\n typeof value === 'boolean'\n ) {\n return escapeHtmlRaw(String(value));\n } else if (value == null) {\n // undefined or null -- render nothing\n return '';\n } else if (typeof value === 'object') {\n throw new Error(`Cannot interpolate object in template: ${JSON.stringify(value)}`);\n } else {\n // There shouldn't be any other types\n throw new Error(\n `Unexpected type in template: ${typeof value} for value ${JSON.stringify(value)}`,\n );\n }\n}\n\n// Based on https://github.com/Janpot/escape-html-template-tag\nexport class HtmlSafeString {\n private readonly strings: readonly string[];\n private readonly values: unknown[];\n\n constructor(strings: readonly string[], values: unknown[]) {\n this.strings = strings;\n this.values = values;\n }\n\n toString(): string {\n return this.values.reduce<string>((acc, val, i) => {\n return acc + escapeValue(val) + this.strings[i + 1];\n }, this.strings[0]);\n }\n}\n\nexport type HtmlValue =\n | string\n | number\n | boolean\n | bigint\n | HtmlSafeString\n | undefined\n | null\n | HtmlValue[];\n\nexport function html(strings: TemplateStringsArray, ...values: HtmlValue[]): HtmlSafeString {\n return new HtmlSafeString(strings, values);\n}\n\n/**\n * Pre-escapes the rendered HTML. Useful for when you want to inline the HTML\n * in something else, for instance in a `data-bs-content` attribute for a Bootstrap\n * popover.\n */\nexport function escapeHtml(html: HtmlSafeString): HtmlSafeString {\n return unsafeHtml(escapeHtmlRaw(html.toString()));\n}\n\n/**\n * Will render the provided value without any additional escaping. Use carefully\n * with user-provided data.\n *\n * @param value The value to render.\n * @returns An {@link HtmlSafeString} representing the provided value.\n */\nexport function unsafeHtml(value: string): HtmlSafeString {\n return new HtmlSafeString([value], []);\n}\n\n/**\n * Joins a list of HTML values with a separator.\n *\n * @param values The values to join.\n * @param separator The separator to use between values.\n */\nexport function joinHtml(values: HtmlValue[], separator: HtmlValue = ''): HtmlSafeString {\n return unsafeHtml(values.map(escapeValue).join(escapeValue(separator)));\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/html",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.11",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"sideEffects": false,
|
|
7
5
|
"repository": {
|
|
8
6
|
"type": "git",
|
|
9
7
|
"url": "https://github.com/PrairieLearn/PrairieLearn.git",
|
|
10
8
|
"directory": "packages/html"
|
|
11
9
|
},
|
|
10
|
+
"main": "dist/index.js",
|
|
12
11
|
"scripts": {
|
|
13
12
|
"build": "tsc",
|
|
14
13
|
"dev": "tsc --watch --preserveWatchOutput",
|
|
@@ -16,13 +15,14 @@
|
|
|
16
15
|
},
|
|
17
16
|
"devDependencies": {
|
|
18
17
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
19
|
-
"@types/node": "^20.17.
|
|
18
|
+
"@types/node": "^20.17.28",
|
|
20
19
|
"c8": "^10.1.3",
|
|
21
|
-
"chai": "^5.
|
|
22
|
-
"mocha": "^
|
|
23
|
-
"tsx": "^4.19.
|
|
24
|
-
"typescript": "^5.
|
|
20
|
+
"chai": "^5.2.0",
|
|
21
|
+
"mocha": "^11.1.0",
|
|
22
|
+
"tsx": "^4.19.3",
|
|
23
|
+
"typescript": "^5.8.2"
|
|
25
24
|
},
|
|
25
|
+
"sideEffects": false,
|
|
26
26
|
"c8": {
|
|
27
27
|
"reporter": [
|
|
28
28
|
"html",
|
package/src/index.ts
CHANGED
|
@@ -77,7 +77,7 @@ export function html(strings: TemplateStringsArray, ...values: HtmlValue[]): Htm
|
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* Pre-escapes the rendered HTML. Useful for when you want to inline the HTML
|
|
80
|
-
* in something else, for instance in a `data-content` attribute for a Bootstrap
|
|
80
|
+
* in something else, for instance in a `data-bs-content` attribute for a Bootstrap
|
|
81
81
|
* popover.
|
|
82
82
|
*/
|
|
83
83
|
export function escapeHtml(html: HtmlSafeString): HtmlSafeString {
|