Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTML.isArray to work across frames. #276

Merged
merged 5 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/blaze/builtins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Blaze._calculateCondition = function (cond) {
if (cond instanceof Array && cond.length === 0)
if (HTML.isArray(cond) && cond.length === 0)
cond = false;
return !! cond;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/html-tools/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ getContent = HTMLTools.Parse.getContent = function (scanner, shouldStopFunc) {
// as in `FOO.apply(null, content)`.
if (content == null)
content = [];
else if (! (content instanceof Array))
else if (! HTML.isArray(content))
content = [content];

items.push(HTML.getTag(tagName).apply(
Expand Down
25 changes: 17 additions & 8 deletions packages/htmljs/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ Raw.prototype.htmljsType = Raw.htmljsType = ['Raw'];


HTML.isArray = function (x) {
// could change this to use the more convoluted Object.prototype.toString
// approach that works when objects are passed between frames, but does
// it matter?
return (x instanceof Array);
return x instanceof Array || Array.isArray(x);
};

HTML.isConstructedObject = function (x) {
Expand All @@ -185,10 +182,22 @@ HTML.isConstructedObject = function (x) {
// if you assign to a prototype when setting up the class as in:
// `Foo = function () { ... }; Foo.prototype = { ... }`, then
// `(new Foo).constructor` is `Object`, not `Foo`).
return (x && (typeof x === 'object') &&
(x.constructor !== Object) &&
(typeof x.constructor === 'function') &&
(x instanceof x.constructor));
if(!x || (typeof x !== 'object')) return false;
// Is this a plain object?
let plain = false;
if(Object.getPrototypeOf(x) === null) {
plain = true;
} else {
let proto = x;
while(Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
plain = Object.getPrototypeOf(x) === proto;
}

return !plain &&
(typeof x.constructor === 'function') &&
(x instanceof x.constructor);
};

HTML.isNully = function (node) {
Expand Down