☕️ Classic, reliable, trusted test framework for Node.js and the browser
Remove he dependency (#6129)
Josh Goldberg ✨9f85d7176925
diff --git a/lib/utils.cjs b/lib/utils.cjsindex c2129fec..dbfd97f9 100644--- a/lib/utils.cjs+++ b/lib/utils.cjs@@ -9,12 +9,37 @@* Module dependencies.*/var path = require("node:path");-var he = require("he");var pc = require("picocolors");var isUnicodeSupported = require("is-unicode-supported")();const MOCHA_ID_PROP_NAME = "__mocha_id__";+function htmlCodePointEscape(codePoint) {+ return "&#x" + codePoint.toString(16).toUpperCase() + ";";+}++function shouldEscapeHtmlChar(codePoint) {+ return (+ (codePoint >= 0x01 && codePoint <= 0x09) ||+ codePoint === 0x0b ||+ codePoint === 0x0c ||+ (codePoint >= 0x0e && codePoint <= 0x1f) ||+ codePoint === 0x22 ||+ codePoint === 0x26 ||+ codePoint === 0x27 ||+ codePoint === 0x3c ||+ codePoint === 0x3e ||+ codePoint === 0x60 ||+ codePoint === 0x7f ||+ codePoint === 0x81 ||+ codePoint === 0x8d ||+ codePoint === 0x8f ||+ codePoint === 0x90 ||+ codePoint === 0x9d ||+ codePoint >= 0xa0+ );+}+/*** Escape special characters in the given string of html.*@@ -23,7 +48,35 @@ const MOCHA_ID_PROP_NAME = "__mocha_id__";* @return {string}*/exports.escape = function (html) {- return he.encode(String(html), { useNamedReferences: false });+ var string = String(html);+ var escaped = "";++ for (var index = 0; index < string.length; index++) {+ var codePoint = string.charCodeAt(index);++ if (+ codePoint >= 0xd800 &&+ codePoint <= 0xdbff &&+ index + 1 < string.length+ ) {+ var low = string.charCodeAt(index + 1);++ if (low >= 0xdc00 && low <= 0xdfff) {+ // Decode a UTF-16 surrogate pair into a single Unicode code point.+ // See https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae+ codePoint = (codePoint - 0xd800) * 0x400 + low - 0xdc00 + 0x10000;+ escaped += htmlCodePointEscape(codePoint);+ index++;+ continue;+ }+ }++ escaped += shouldEscapeHtmlChar(codePoint)+ ? htmlCodePointEscape(codePoint)+ : string.charAt(index);+ }++ return escaped;};/**diff --git a/package-lock.json b/package-lock.jsonindex 17fc7e27..1dba73cf 100644--- a/package-lock.json+++ b/package-lock.json@@ -15,7 +15,6 @@"diff": "^9.0.0","find-up": "^5.0.0","glob": "^13.0.0",- "he": "^1.2.0","is-path-inside": "^3.0.3","is-unicode-supported": "^0.1.0","js-yaml": "^4.1.0",@@ -4798,14 +4797,6 @@"node": ">= 0.4"}},- "node_modules/he": {- "version": "1.2.0",- "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",- "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",- "bin": {- "he": "bin/he"- }- },"node_modules/hmac-drbg": {"version": "1.0.1","resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz",diff --git a/package.json b/package.jsonindex 6a4bc014..5be548d5 100644--- a/package.json+++ b/package.json@@ -93,7 +93,6 @@"diff": "^9.0.0","find-up": "^5.0.0","glob": "^13.0.0",- "he": "^1.2.0","is-path-inside": "^3.0.3","is-unicode-supported": "^0.1.0","js-yaml": "^4.1.0",diff --git a/test/unit/utils.spec.cjs b/test/unit/utils.spec.cjsindex 53e1ef10..7e8bc1c6 100644--- a/test/unit/utils.spec.cjs+++ b/test/unit/utils.spec.cjs@@ -2,6 +2,7 @@"use strict";var utils = require("../../lib/utils.cjs");+var pkg = require("../../package.json");var sinon = require("sinon");describe("lib/utils", function () {@@ -685,24 +686,170 @@ describe("lib/utils", function () {});describe("escape()", function () {- it("replaces the usual xml suspects", function () {- expect(utils.escape("<a<bc<d<"), "to be", "<a<bc<d<");- expect(utils.escape(">a>bc>d>"), "to be", ">a>bc>d>");- expect(utils.escape('"a"bc"d"'), "to be", ""a"bc"d"");- expect(utils.escape('<>"&'), "to be", "<>"&");+ [+ ["null", null, "null"],+ ["undefined", undefined, "undefined"],+ ["number", 42, "42"],+ ["boolean", false, "false"],+ ["object", { toString: () => "<tag>" }, "<tag>"],+ ].forEach(function ([label, input, expected]) {+ it(`coerces ${label} to a string before escaping`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ [+ [+ "alphanumeric",+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",+ ],+ ["safe punctuation", " !#$%()*+,-./:;=?@[\\]^_{|}~"],+ ].forEach(function ([label, input]) {+ it(`leaves safe ascii ${label} unchanged`, function () {+ expect(utils.escape(input), "to be", input);+ });+ });++ [+ ["less-than signs", "<a<bc<d<", "<a<bc<d<"],+ ["greater-than signs", ">a>bc>d>", ">a>bc>d>"],+ ["double quotes", '"a"bc"d"', ""a"bc"d""],+ ["ampersands", "&a&bc&d&", "&a&bc&d&"],+ ["existing entities", "&<", "&amp;&lt;"],+ [+ "all sensitive punctuation",+ "<>\"&'`",+ "<>"&'`",+ ],+ ].forEach(function ([label, input, expected]) {+ it(`replaces ${label} with uppercase hexadecimal references`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });- expect(utils.escape("&a&bc&d&"), "to be", "&a&bc&d&");- expect(utils.escape("&<"), "to be", "&amp;&lt;");+ [+ ["nul", "\x00", "\x00"],+ ["start of heading", "\x01", ""],+ ["start of text", "\x02", ""],+ ["end of text", "\x03", ""],+ ["end of transmission", "\x04", ""],+ ["enquiry", "\x05", ""],+ ["acknowledge", "\x06", ""],+ ["bell", "\x07", ""],+ ["backspace", "\x08", ""],+ ["tab", "\x09", "	"],+ ["line feed", "\x0A", "\n"],+ ["vertical tab", "\x0B", ""],+ ["form feed", "\x0C", ""],+ ["carriage return", "\x0D", "\r"],+ ["shift out", "\x0E", ""],+ ["shift in", "\x0F", ""],+ ["data link escape", "\x10", ""],+ ["device control one", "\x11", ""],+ ["device control two", "\x12", ""],+ ["device control three", "\x13", ""],+ ["device control four", "\x14", ""],+ ["negative acknowledge", "\x15", ""],+ ["synchronous idle", "\x16", ""],+ ["end transmission block", "\x17", ""],+ ["cancel", "\x18", ""],+ ["end of medium", "\x19", ""],+ ["substitute", "\x1A", ""],+ ["escape", "\x1B", ""],+ ["file separator", "\x1C", ""],+ ["group separator", "\x1D", ""],+ ["record separator", "\x1E", ""],+ ["unit separator", "\x1F", ""],+ ].forEach(function ([label, input, expected]) {+ it(`handles C0 control ${label}`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ [+ ["delete", "\x7F", ""],+ ["padding character", "\x80", "\x80"],+ ["high octet preset", "\x81", ""],+ ["partial line backward", "\x8C", "\x8C"],+ ["reverse line feed", "\x8D", ""],+ ["single shift two", "\x8E", "\x8E"],+ ["single shift three", "\x8F", ""],+ ["device control string", "\x90", ""],+ ["private use one", "\x91", "\x91"],+ ["string terminator", "\x9C", "\x9C"],+ ["operating system command", "\x9D", ""],+ ["privacy message", "\x9E", "\x9E"],+ ["application program command", "\x9F", "\x9F"],+ ].forEach(function ([label, input, expected]) {+ it(`handles C1 control ${label}`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ [+ ["copyright sign", "©", "©"],+ ["latin small e acute", "é", "é"],+ ["em dash", "—", "—"],+ ["CJK character", "中", "中"],+ ["replacement character", "\uFFFD", "�"],+ ["noncharacter FFFE", "\uFFFE", ""],+ ["noncharacter FFFF", "\uFFFF", ""],+ ].forEach(function ([label, input, expected]) {+ it(`replaces non-ascii unicode ${label}`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ [+ ["pile of poo", "💩", "💩"],+ ["grinning face", "😀", "😀"],+ ["tetragram", "𝌆", "𝌆"],+ ["CJK extension", "𠜎", "𠜎"],+ ].forEach(function ([label, input, expected]) {+ it(`replaces astral unicode ${label} as one code point`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ [+ ["lone high surrogate", "\uD800", "�"],+ ["lone low surrogate", "\uDC00", "�"],+ ["high surrogate between ascii", "a\uD800b", "a�b"],+ ["low surrogate between ascii", "a\uDC00b", "a�b"],+ ].forEach(function ([label, input, expected]) {+ it(`replaces ${label}`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ it("does not rely on he as a runtime dependency", function () {+ expect(pkg.dependencies, "not to have key", "he");+ });++ [+ ["ansi color escapes", "\x1B[32mfoo\x1B[0m", "[32mfoo[0m"],+ ["ascii whitespace controls", "\t\n\r", "	\n\r"],+ ["mixed null and C1 controls", "\0\x7F\x80\x81", "\0\x80"],+ ].forEach(function ([label, input, expected]) {+ it(`replaces ${label}`, function () {+ expect(utils.escape(input), "to be", expected);+ });+ });++ it("replaces unpaired surrogates without consuming neighboring characters", function () {+ expect(+ utils.escape("\uD800\uD800\uDC00\uDC00"),+ "to be",+ "�𐀀�",+ );});- it("replaces invalid xml characters", function () {+ it("escapes mixed reporter content in order", function () {expect(- utils.escape("\x1B[32mfoo\x1B[0m"),+ utils.escape("Suite <root> failed: 💩 & \x1B[31mred\x1B[0m"),"to be",- "[32mfoo[0m",+ "Suite <root> failed: 💩 & [31mred[0m",);- // Ensure we can handle non-trivial unicode characters as well- expect(utils.escape("💩"), "to be", "💩");});});