Last Comment Bug 840187 - Autofocus overrides anchors
: Autofocus overrides anchors
Status: NEW
: testcase
Product: Core
Classification: Components
Component: Document Navigation (show other bugs)
: Trunk
: All All
: -- normal (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
:
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-02-11 11:35 PST by Ahmed
Modified: 2014-12-28 06:12 PST (History)
6 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
Test case where autofocus for the input box will override the anchors. (152.63 KB, text/html)
2013-02-14 13:39 PST, Ahmed
no flags Details
Test case where autofocus was removed from the input box. Now anchors work. (152.62 KB, text/html)
2013-02-14 13:42 PST, Ahmed
no flags Details

Description Ahmed 2013-02-11 11:35:57 PST
User Agent: Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0
Build ID: 20130104151925

Steps to reproduce:

Having an input box with autofocus in the beginning of the pages, and then having an anchor(s) later in the page. 


Actual results:

Linking to an anchor inside the page from an external page would fail to jump to the anchor. Clicking again after the page loads will jump to the anchor.


Expected results:

The browser should jump to the anchor even if the autofocus exists. IE and Chrome does the right behavior.

Removing the autofocus gets firefox to jump to the anchor directly.
Comment 1 Loic 2013-02-11 12:51:15 PST
Please, attach a minimal testcase (online if you want), so we could test.
Comment 2 Phoenix 2013-02-12 12:49:15 PST
Dupe for Bug 668213 (which proposed to dupe with bug 633821)?
Comment 3 Ahmed 2013-02-14 13:39:20 PST
Created attachment 714072 [details]
Test case where autofocus for the input box will override the anchors.
Comment 4 Ahmed 2013-02-14 13:42:12 PST
Created attachment 714074 [details]
Test case where autofocus was removed from the input box. Now anchors work.

Try to reach an example anchor #Q using this file and using the other file with autofocus. The first time to reach this anchor will fail if there is an autofocus, if you try AGAIN while the page is LOADED. The browser will jump.
Comment 5 [:Aleksej] 2013-02-14 23:55:41 PST
Works for me with 
2013-02-14-03-28-50-mozilla-central-firefox-21.0a1.en-US.linux-x86_64
firefox-18.0.1.ru.linux64
Comment 6 Rodney Rehm 2014-12-28 03:56:58 PST
I can confirm the behavior in Firefox 34.0 (Mac OS X 10.8.5) and Firefox Nightly 37.0a1 (2014-12-20). My test is available at http://medialize.github.io/ally.js/tests/browser-bugs/blink-autofocus.html#foo

I can also add that Blink (not WebKit) has the same issue: https://code.google.com/p/chromium/issues/detail?id=382901
Comment 7 Rodney Rehm 2014-12-28 05:17:30 PST
When both, autofocus and fragment-anchor are present, the following event sequence is played:

init (synchronous script at end of document)
  target: null
  activeElement: BODY

focus event
  event.target: #document

focus event
  event.target: INPUT

readystatechange event
  readyState: interactive
  target: null
  activeElement: INPUT

DOMContentLoaded
  target: DIV
  activeElement: INPUT

readystatechange event
  radyState: complete
  target: DIV
  activeElement: INPUT

load event
  target: DIV
  activeElement: INPUT

The fragment's target (`:target`) is only accessible after DOMContentLoaded, the autofocus element receives focus *before* the document enters interactive state.

Disabling the autofocus by adding the following script to the end of the document has will cause the <input> to not have that attribute anymore, but is still focused right away:

<script>document.querySelector('[autofocus]').removeAttribute('autofocus');</script>

There is no way to *prevent* the <input> from receiving focus, you can only blur it, and move the document back to the :target (using Element.scrollIntoView [1]) after the fact.

[1] https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView
Comment 8 Rodney Rehm 2014-12-28 06:12:00 PST
> Disabling the autofocus by adding the following script to the end of the document has will
> cause the <input> to not have that attribute anymore, but is still focused right away:
> <script>document.querySelector('[autofocus]').removeAttribute('autofocus');</script>

While that holds true, removing the [autofocus] element will, of course, issue a blur event, making <body> the activeElement and fragment target remains in the viewport.

After pressing <kbd>Command R</kbd> to reload the document, scroll position moves to the beginning, as if the [autofocus] element was moved into viewport. This does not happen with <kbd>Command Shift R</kbd>.

Note You need to log in before you can comment on or make changes to this bug.