From f9bde050b496724f8f6e4033c3b56ebb4e8bed6a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 9 Jan 2015 15:00:29 -0800 Subject: [PATCH] Handle TextEditorElement::focus() while parent is being attached Fixes atom/autocomplete#61 --- spec/text-editor-element-spec.coffee | 16 ++++++++++++++++ src/text-editor-element.coffee | 8 +++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spec/text-editor-element-spec.coffee b/spec/text-editor-element-spec.coffee index 35e13fb17..f29a8f91b 100644 --- a/spec/text-editor-element-spec.coffee +++ b/spec/text-editor-element-spec.coffee @@ -98,6 +98,22 @@ describe "TextEditorElement", -> document.body.focus() expect(blurCalled).toBe true + describe "when focused while a parent node is being attached to the DOM", -> + class ElementThatFocusesChild extends HTMLDivElement + attachedCallback: -> + @firstChild.focus() + + document.registerElement("element-that-focuses-child", + prototype: ElementThatFocusesChild.prototype + ) + + it "proxies the focus event to the hidden input", -> + element = new TextEditorElement + parentElement = document.createElement("element-that-focuses-child") + parentElement.appendChild(element) + jasmineContent.appendChild(parentElement) + expect(element.shadowRoot.activeElement).toBe element.shadowRoot.querySelector('input') + describe "when the themes finish loading", -> [themeReloadCallback, initialThemeLoadComplete, element] = [] diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index 07da37c40..3cc52642a 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -63,7 +63,8 @@ class TextEditorElement extends HTMLElement @buildModel() unless @getModel()? @mountComponent() unless @component?.isMounted() @component.checkForVisibilityChange() - @focus() if @focusOnAttach + if this is document.activeElement + @focused() @emitter.emit("did-attach") detachedCallback: -> @@ -128,10 +129,7 @@ class TextEditorElement extends HTMLElement @component = null focused: -> - if @component? - @component.focused() - else - @focusOnAttach = true + @component?.focused() blurred: (event) -> unless @useShadowDOM