From 58bc2eb55631a0d004fe7fafc75e6a621b0e8c8c Mon Sep 17 00:00:00 2001 From: Joe Portner <5295965+jportner@users.noreply.github.com> Date: Fri, 10 Mar 2023 00:03:22 -0500 Subject: [PATCH] Change approach to shadowing "toString" property for escapeXML --- lib/utils.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 6859abf1..4f0be041 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -99,9 +99,14 @@ exports.escapeXML = function (markup) { : String(markup) .replace(_MATCH_HTML, encode_char); }; -exports.escapeXML.toString = function () { + +// If the Object prototype is frozen, the "toString" property is non-writable. This means that any objects which inherit this property +// cannot have the property changed using an assignment. If using strict mode, attempting that will cause an error. If not using strict +// mode, attempting that will be silently ignored. +// However, we can still explicitly shadow the prototype's "toString" property by defining a new "toString" property on this object. +Object.defineProperty(exports.escapeXML, 'toString', function () { return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr; -}; +}); /** * Naive copy of properties from one object to another.