Skip to content

Commit 58bc2eb

Browse files
committed
Change approach to shadowing "toString" property for escapeXML
1 parent f818bce commit 58bc2eb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,14 @@ exports.escapeXML = function (markup) {
9999
: String(markup)
100100
.replace(_MATCH_HTML, encode_char);
101101
};
102-
exports.escapeXML.toString = function () {
102+
103+
// If the Object prototype is frozen, the "toString" property is non-writable. This means that any objects which inherit this property
104+
// cannot have the property changed using an assignment. If using strict mode, attempting that will cause an error. If not using strict
105+
// mode, attempting that will be silently ignored.
106+
// However, we can still explicitly shadow the prototype's "toString" property by defining a new "toString" property on this object.
107+
Object.defineProperty(exports.escapeXML, 'toString', function () {
103108
return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
104-
};
109+
});
105110

106111
/**
107112
* Naive copy of properties from one object to another.

0 commit comments

Comments
 (0)