From 0d5b34ca49e6a2880503fc3b26072b58932c8b1e Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Fri, 15 Jan 2016 19:53:47 +0100 Subject: [PATCH] Fix uncaught exception when loading some pages In frame scripts, `document.activeElement` may be null while the page is loading. --- extension/lib/commands-frame.coffee | 2 +- extension/lib/events-frame.coffee | 3 ++- extension/lib/utils.coffee | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/extension/lib/commands-frame.coffee b/extension/lib/commands-frame.coffee index bd483cb..e53015e 100644 --- a/extension/lib/commands-frame.coffee +++ b/extension/lib/commands-frame.coffee @@ -56,7 +56,7 @@ commands.go_to_root = ({vim}) -> commands.scroll = (args) -> {vim} = args - activeElement = utils.getActiveElement(vim.content) + return unless activeElement = utils.getActiveElement(vim.content) element = # If no element is focused on the page, the the active element is the # topmost ``, and blurring it is a no-op. If it is scrollable, it diff --git a/extension/lib/events-frame.coffee b/extension/lib/events-frame.coffee index f93c400..b7cdf66 100644 --- a/extension/lib/events-frame.coffee +++ b/extension/lib/events-frame.coffee @@ -168,7 +168,8 @@ class FrameEventManager ) sendFocusType = => - focusType = utils.getFocusType(utils.getActiveElement(@vim.content)) + return unless activeElement = utils.getActiveElement(@vim.content) + focusType = utils.getFocusType(activeElement) messageManager.send('focusType', focusType) @listen('focus', (event) => diff --git a/extension/lib/utils.coffee b/extension/lib/utils.coffee index 5e9a36e..bf7c58f 100644 --- a/extension/lib/utils.coffee +++ b/extension/lib/utils.coffee @@ -108,8 +108,13 @@ isTypingElement = (element) -> # Active/focused element helpers +# NOTE: In frame scripts, `document.activeElement` may be `null` when the page +# is loading. Therefore always check if anything was returned, such as: +# +# return unless activeElement = utils.getActiveElement(window) getActiveElement = (window) -> {activeElement} = window.document + return null unless activeElement # If the active element is a frame, recurse into it. The easiest way to detect # a frame that works both in browser UI and in web page content is to check # for the presence of `.contentWindow`. However, in non-multi-process @@ -128,7 +133,7 @@ blurActiveElement = (window) -> # focus to the `` of its containing frame, while blurring the top-most # frame gives focus to the top-most ``. This allows to blur fancy text # editors which use an `