From 5ae354718c4f2046fdcc29186d492b8e7e98ad27 Mon Sep 17 00:00:00 2001 From: xyh Date: Sat, 20 Mar 2021 00:38:43 +0800 Subject: [PATCH] fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405 Co-authored-by: Yuehan Xu --- src/utils/common.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/common.ts b/src/utils/common.ts index 16f94303..58de2b6f 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -36,11 +36,20 @@ export function isDraftable(value: any): boolean { ) } +const objectCtorString = Object.prototype.constructor.toString() /*#__PURE__*/ export function isPlainObject(value: any): boolean { if (!value || typeof value !== "object") return false const proto = Object.getPrototypeOf(value) - return !proto || proto === Object.prototype + if (proto === null) { + return true + } + const Ctor = + Object.hasOwnProperty.call(proto, "constructor") && proto.constructor + return ( + typeof Ctor == "function" && + Function.toString.call(Ctor) === objectCtorString + ) } /** Get the underlying object that is represented by the given draft */