Skip to content
Snippets Groups Projects
Commit 8e8cf7ec authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

XWIKI-22377: Prototype.js can still break the standard Array.from method

* Prevent Prototype.js from overriding Array.from, second try
parent 917d730e
No related branches found
No related tags found
No related merge requests found
......@@ -18,12 +18,25 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
(function () {
// Code executed before Prototype.js is loaded.
// Restore the original code, since Prototype.js is overriding the Array.from method. See
// https://github.com/prototypejs/prototype/issues/338.
const originalArrayFrom = Array.from;
setTimeout(() => {
// Code executed after Prototype.js is loaded.
Array.from = originalArrayFrom;
}, 0);
// We expect this code to be loaded right before Prototype.js code.
// Prevent Prototype.js from overriding the Array.from function.
// See https://github.com/prototypejs/prototype/issues/338.
let arrayFrom = Array.from;
Object.defineProperty(Array, "from", {
get: () => arrayFrom,
set: function(newArrayFrom) {
if (window.Prototype && newArrayFrom === window.$A) {
// Prototype.js tries to override the Array.from function.
// We delete the defined property because we don't want to to prevent anyone from overriding Array.from, we just
// want to prevent Prototype.js from doing it because we know that Prototype's implementation is outdated and
// causes new code to fail.
delete this.from;
this.from = arrayFrom;
} else {
// Allow others to override Array.from (their need may be valid, we don't know).
arrayFrom = newArrayFrom;
}
}
});
})();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment