Repository navigation
SDK 4.13.3 Array.from polyfill from <sdk:now-ux-globals> still broken for array-likes under prototype.js
#148
Replies: 1 comment
|
Further testing confirms that the 4.13.3 This follow-up covers:
Setup. Chrome 152, on a UI Page built with SDK 4.13.3 ( That file reports version 1.7.3. Compared with upstream 1.7.3 by parsed code, it has four local changes: Side note: the local 1. The
2. Other built-ins prototype.js changes
Unaffected: 3a. Mitigation: minimal mode, for pages whose dependencies permit it The 4.13.3 UI Page plugin supports
Loaded without prototype.js, those two remaining scripts run without errors and leave every built-in native. A page has to be checked against everything this mode drops. Using REST with 3b. Mitigation: restore only what breaks modern code The principle is to leave prototype.js's documented APIs alone. A native is restored only where prototype.js's version breaks modern code and prototype.js loses nothing by the restore. That covers Before if (typeof Prototype === "undefined") {
window.__sdkNatives = { from: Array.from, find: Array.prototype.find, entries: Array.prototype.entries };
}After prototype.js, before the existing (function (n) {
function put(owner, key, value) {
Object.defineProperty(owner, key, { value: value, writable: true, configurable: true, enumerable: false });
}
if (n) {
// Array.from: native, except prototype.js's own objects (Hash, ObjectRange), which have toArray() but no iterator
put(Array, "from", function from(items) {
var args = arguments;
if (items != null && typeof items[Symbol.iterator] !== "function" && typeof items.toArray === "function") {
args = Array.prototype.slice.call(arguments);
args[0] = items.toArray();
}
return n.from.apply(this, args);
});
put(Array.prototype, "find", n.find); // prototype.js's version throws on array-likes
put(Array.prototype, "entries", n.entries); // prototype.js deletes it
delete window.__sdkNatives;
}
// el.scrollTo(x, y) and el.scrollTo(options) go native; $(el).scrollTo() keeps prototype.js's scroll-window-to-element
if (window.Element && Element.Methods && Element.Methods.scrollTo) {
var legacyScrollTo = Element.Methods.scrollTo;
Element.Methods.scrollTo = function (element) {
if (arguments.length > 1) {
Element.prototype.scrollTo.apply(element, Array.prototype.slice.call(arguments, 1));
return element;
}
return legacyScrollTo.apply(this, arguments);
};
Element.addMethods();
}
})(window.__sdkNatives);Checked, in isolated frames with the served
Scope. Both approaches only affect the document that contains the tag. A UI Page has its own built-ins even when it is shown in an iframe inside UI16 or a workspace, so classic pages and the surrounding frame are untouched. This is also why the change belongs in the SDK tag rather than in the platform's On a direct SDK page, that document holds only:
None of those scripts throws On a non-direct page, where the platform header has already loaded prototype.js, the guard leaves everything as it is today except Not tested:
If the shim is kept as a re-implementation instead, the minimum is to drop the fallback to Reproduce: paste into the console of any UI Page that uses <sdk:now-ux-globals>(function () {
var frame = document.createElement("iframe");
frame.style.display = "none";
document.body.appendChild(frame);
var clean = frame.contentWindow;
function box() {
var b = document.createElement("div"), inner = document.createElement("div");
b.style.cssText = "position:fixed;left:-9999px;top:0;width:60px;height:60px;overflow:auto";
inner.style.height = "600px";
b.appendChild(inner);
document.body.appendChild(b);
return b;
}
var cases = {
"Array.from({ length: 3 }, fn)": function (w) { return w.Array.from({ length: 3 }, function (_, i) { return i; }); },
"Array.from(new Set([1, 2]))": function (w) { return w.Array.from(new Set([1, 2])); },
"Array.from(null)": function (w) { return w.Array.from(null); },
"[7].entries()": function (w) { return w.Array.prototype.entries.call([7]).next().value; },
"find.call(arrayLike, fn)": function (w) { return w.Array.prototype.find.call({ 0: "a", 1: "b", length: 2 }, function (v) { return v === "b"; }); },
"find.call(HTMLCollection, fn)": function (w) { return w.Array.prototype.find.call(document.getElementsByTagName("div"), function () { return true; }) !== undefined; },
"reverse.call(arrayLike)": function (w) { var o = { 0: "a", 1: "b", length: 2 }; w.Array.prototype.reverse.call(o); return [o[0], o[1]]; },
"Object.values(Object.create({ x: 1 }))": function (w) { return w.Object.values(Object.create({ x: 1 })); },
"Object.values('ab').length": function (w) { return w.Object.values("ab").length; },
"el.scrollTo(0, 120)": function (w) { var b = box(); try { w.HTMLElement.prototype.scrollTo.call(b, 0, 120); return b.scrollTop; } finally { document.body.removeChild(b); } },
"for (k in [1]) count": function (w) { var n = 0, k; for (k in w.Array.of(1)) n++; return n; },
"[1, 2].map()": function (w) { return w.Array.prototype.map.call([1, 2]); }
};
function run(fn, w) { try { return JSON.stringify(fn(w)); } catch (e) { return "throws " + e.name; } }
var rows = [];
for (var name in cases) {
var page = run(cases[name], window), native = run(cases[name], clean);
rows.push({ call: name, page: page, native: native, same: page === native });
}
document.body.removeChild(frame);
console.table(rows);
})(); |
Uh oh!
There was an error while loading. Please reload this page.
SDK 4.13.3 adds an
Array.frompolyfill to every UI Page that uses<sdk:now-ux-globals>(full mode). It only uses its correct implementation for inputs that haveSymbol.iterator. Every other input goes tooriginalArrayFrom, and on a UI Page that is prototype.js 1.7.3's$A, which ignores the map function.Repro, in the console of a UI Page that uses
<sdk:now-ux-globals>:Impact: any library that uses the array-like form breaks. dnd-kit builds its IntersectionObserver thresholds with
Array.from({ length: 100 }, fn), getsNaN, and throws "The provided double value is non-finite", so dragging never starts. Our own page crashed the same way on an instance running 4.13.3.Suggested fix: send every non-null input to
specArrayFrom, which already handles array-likes. Also add the array-like case to thetestWorkscheck, which today only tests aSet.All reactions