diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index 0132b4fc94c8..ddfb5bb084f8 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -27,6 +27,8 @@ #include "nsContentUtils.h" #include "nsGlobalWindow.h" +#include "mozilla/dom/BindingUtils.h" +#include "mozilla/dom/Element.h" #include "mozilla/dom/ScriptSettings.h" using namespace mozilla::dom; @@ -313,6 +315,27 @@ nsJSUtils::EvaluateString(JSContext* aCx, options, &unused, aOffThreadToken); } +/* static */ +bool +nsJSUtils::GetScopeChainForElement(JSContext* aCx, + mozilla::dom::Element* aElement, + JS::AutoObjectVector& aScopeChain) +{ + for (nsINode* cur = aElement; cur; cur = cur->GetScopeChainParent()) { + JS::RootedValue val(aCx); + if (!WrapNewBindingObject(aCx, cur, &val)) { + return false; + } + + if (!aScopeChain.append(&val.toObject())) { + return false; + } + } + + return true; +} + + // // nsDOMJSUtils.h // diff --git a/dom/base/nsJSUtils.h b/dom/base/nsJSUtils.h index d46a5a5b09dd..3442487229ff 100644 --- a/dom/base/nsJSUtils.h +++ b/dom/base/nsJSUtils.h @@ -25,6 +25,7 @@ class nsIScriptGlobalObject; namespace mozilla { namespace dom { class AutoJSAPI; +class Element; } } @@ -119,6 +120,11 @@ public: JS::CompileOptions &aCompileOptions, void **aOffThreadToken = nullptr); + // Returns false if an exception got thrown on aCx. Passing a null + // aElement is allowed; that wil produce an empty aScopeChain. + static bool GetScopeChainForElement(JSContext* aCx, + mozilla::dom::Element* aElement, + JS::AutoObjectVector& aScopeChain); }; class MOZ_STACK_CLASS AutoDontReportUncaught {