aboutsummaryrefslogtreecommitdiff
path: root/www/waterfox/files/patch-bug712130
blob: aaa845735f4550c997ac0a9e7667055a55b57f6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
commit a5892fdff0c9
Author: decltype <mozilla@decltype.org>
Date:   Wed Jan 10 00:40:13 2018 +0100

    Bug 712130 - Add reftest to ensure the first autofocus elements gets picked. r=bz
    
    MozReview-Commit-ID: 6UAUbqwVhTm
    
    --HG--
    extra : rebase_source : 7ade8a42489bbf0be97894c18a38bd765a1da891
---
 layout/reftests/bugs/712130-1-ref.html | 5 +++++
 layout/reftests/bugs/712130-1.html     | 5 +++++
 layout/reftests/bugs/reftest.list      | 1 +
 3 files changed, 11 insertions(+)

diff --git layout/reftests/bugs/712130-1-ref.html layout/reftests/bugs/712130-1-ref.html
new file mode 100644
index 000000000000..6d07ca048250
--- /dev/null
+++ layout/reftests/bugs/712130-1-ref.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="stylesheet" href="nothing"><!-- Empty stylesheet to delay PresShell init -->
+<input type="text" autofocus="" onfocus="document.documentElement.removeAttribute('class')">
+<input type="text">
diff --git layout/reftests/bugs/712130-1.html layout/reftests/bugs/712130-1.html
new file mode 100644
index 000000000000..ffbc82309366
--- /dev/null
+++ layout/reftests/bugs/712130-1.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="stylesheet" href="nothing"><!-- Empty stylesheet to delay PresShell init -->
+<input type="text" autofocus="" onfocus="document.documentElement.removeAttribute('class')">
+<input type="text" autofocus="" onfocus="document.documentElement.removeAttribute('class')">
diff --git layout/reftests/bugs/reftest.list layout/reftests/bugs/reftest.list
index 764d3dbd5b3c..79e5df14fcff 100644
--- layout/reftests/bugs/reftest.list
+++ layout/reftests/bugs/reftest.list
@@ -1707,6 +1707,7 @@ needs-focus == 703186-1.html 703186-1-ref.html
 needs-focus == 703186-2.html 703186-2-ref.html
 needs-focus != 703186-1.html 703186-2.html
 == 711359-1.html 711359-1-ref.html
+fuzzy-if(skiaContent,1,3) needs-focus == 712130-1.html 712130-1-ref.html
 == 712849-1.html 712849-1-ref.html
 == 713856-static.html  713856-ref.html
 == 713856-dynamic.html 713856-ref.html

commit 5269d4159ca0
Author: decltype <mozilla@decltype.org>
Date:   Wed Jan 10 19:07:21 2018 +0100

    Bug 712130 - Add reftest in case autofocus element is seen after PresShell init. r=bz
    
    MozReview-Commit-ID: 75IKOqiqBhL
    
    --HG--
    extra : rebase_source : ec679c6f6b6c48aa88dd6c5fa6b6970e6cfa1314
---
 layout/reftests/bugs/712130-2-ref.html | 3 +++
 layout/reftests/bugs/712130-2.html     | 4 ++++
 layout/reftests/bugs/reftest.list      | 1 +
 3 files changed, 8 insertions(+)

diff --git layout/reftests/bugs/712130-2-ref.html layout/reftests/bugs/712130-2-ref.html
new file mode 100644
index 000000000000..919ac05704ae
--- /dev/null
+++ layout/reftests/bugs/712130-2-ref.html
@@ -0,0 +1,3 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<input type="text" autofocus="" onfocus="document.documentElement.removeAttribute('class')">
diff --git layout/reftests/bugs/712130-2.html layout/reftests/bugs/712130-2.html
new file mode 100644
index 000000000000..232bef484917
--- /dev/null
+++ layout/reftests/bugs/712130-2.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="stylesheet" href="nothing"><!-- Empty stylesheet to delay PresShell init -->
+<input type="text" autofocus="" onfocus="document.documentElement.removeAttribute('class')">
diff --git layout/reftests/bugs/reftest.list layout/reftests/bugs/reftest.list
index 79e5df14fcff..7978c54a5ea2 100644
--- layout/reftests/bugs/reftest.list
+++ layout/reftests/bugs/reftest.list
@@ -1708,6 +1708,7 @@ needs-focus == 703186-2.html 703186-2-ref.html
 needs-focus != 703186-1.html 703186-2.html
 == 711359-1.html 711359-1-ref.html
 fuzzy-if(skiaContent,1,3) needs-focus == 712130-1.html 712130-1-ref.html
+fuzzy-if(skiaContent,1,3) needs-focus == 712130-2.html 712130-2-ref.html
 == 712849-1.html 712849-1-ref.html
 == 713856-static.html  713856-ref.html
 == 713856-dynamic.html 713856-ref.html

commit dd080e9ebacf
Author: decltype <mozilla@decltype.org>
Date:   Mon Jan 8 22:35:00 2018 +0100

    Bug 712130 - Defer autofocus until after frame construction. r=bz
    
    The autofocus attribute on form elements forces layout in CheckIfFocusable.
    To avoid unpleasant FOUCs, defer autofocus processing until frames are
    constructed in PresShell::Initialize.
    
    Resolve the race between nsAutoFocusEvent running and page load by checking the
    readystate at time of event posting. Skip autofocus if the element moved to a
    different window in the meantime.
    
    MozReview-Commit-ID: 90jiJYJWmRg
    
    --HG--
    extra : rebase_source : f94b479075df3e37ec1a658d71596c03930bab92
---
 dom/base/nsDocument.cpp           | 128 ++++++++++++++++++++++++++++++++++++++
 dom/base/nsDocument.h             |   6 ++
 dom/base/nsFocusManager.cpp       |   2 +-
 dom/base/nsIDocument.h            |   3 +
 dom/html/nsGenericHTMLElement.cpp |  64 +------------------
 layout/base/PresShell.cpp         |   2 +
 6 files changed, 142 insertions(+), 63 deletions(-)

diff --git dom/base/nsDocument.cpp dom/base/nsDocument.cpp
index 4ecc537eb54e..3ed0e93222ec 100644
--- dom/base/nsDocument.cpp
+++ dom/base/nsDocument.cpp
@@ -1500,6 +1500,7 @@ nsDocument::nsDocument(const char* aContentType)
 #ifdef DEBUG
   , mWillReparent(false)
 #endif
+  , mAutoFocusFired(false)
 {
   SetContentTypeInternal(nsDependentCString(aContentType));
 
@@ -9444,6 +9445,133 @@ nsDocument::GetTemplateContentsOwner()
   return mTemplateContentsOwner;
 }
 
+static already_AddRefed<nsPIDOMWindowOuter>
+FindTopWindowForElement(Element* element)
+{
+  nsIDocument* document = element->OwnerDoc();
+  if (!document) {
+    return nullptr;
+  }
+
+  nsCOMPtr<nsPIDOMWindowOuter> window = document->GetWindow();
+  if (!window) {
+    return nullptr;
+  }
+
+  // Trying to find the top window (equivalent to window.top).
+  if (nsCOMPtr<nsPIDOMWindowOuter> top = window->GetTop()) {
+    window = top.forget();
+  }
+  return window.forget();
+}
+
+/**
+ * nsAutoFocusEvent is used to dispatch a focus event for an
+ * nsGenericHTMLFormElement with the autofocus attribute enabled.
+ */
+class nsAutoFocusEvent : public Runnable
+{
+public:
+  explicit nsAutoFocusEvent(already_AddRefed<Element>&& aElement,
+                            already_AddRefed<nsPIDOMWindowOuter>&& aTopWindow)
+    : mozilla::Runnable("nsAutoFocusEvent")
+    , mElement(aElement)
+    , mTopWindow(aTopWindow)
+  {
+  }
+
+  NS_IMETHOD Run() override
+  {
+    nsCOMPtr<nsPIDOMWindowOuter> currentTopWindow =
+      FindTopWindowForElement(mElement);
+    if (currentTopWindow != mTopWindow) {
+      // The element's top window changed from when the event was queued.
+      // Don't take away focus from an unrelated window.
+      return NS_OK;
+    }
+
+    nsFocusManager* fm = nsFocusManager::GetFocusManager();
+    if (!fm) {
+      return NS_ERROR_NULL_POINTER;
+    }
+
+    nsIDocument* document = mElement->OwnerDoc();
+
+    // Don't steal focus from the user.
+    if (mTopWindow->GetFocusedNode()) {
+      return NS_OK;
+    }
+
+    // If something is focused in the same document, ignore autofocus.
+    if (!fm->GetFocusedContent() ||
+        fm->GetFocusedContent()->OwnerDoc() != document) {
+      mozilla::ErrorResult rv;
+      mElement->Focus(rv);
+      return rv.StealNSResult();
+    }
+
+    return NS_OK;
+  }
+private:
+  nsCOMPtr<Element> mElement;
+  nsCOMPtr<nsPIDOMWindowOuter> mTopWindow;
+};
+
+void
+nsDocument::SetAutoFocusElement(Element* aAutoFocusElement)
+{
+  if (mAutoFocusFired) {
+    // Too late.
+    return;
+  }
+
+  if (mAutoFocusElement) {
+    // The spec disallows multiple autofocus elements, so we consider only the
+    // first one to preserve the old behavior.
+    return;
+  }
+
+  mAutoFocusElement = do_GetWeakReference(aAutoFocusElement);
+  TriggerAutoFocus();
+}
+
+void
+nsDocument::TriggerAutoFocus()
+{
+  if (mAutoFocusFired) {
+    return;
+  }
+
+  if (!mPresShell || !mPresShell->DidInitialize()) {
+    // Delay autofocus until frames are constructed so that we don't thrash
+    // style and layout calculations.
+    return;
+  }
+
+  nsCOMPtr<Element> autoFocusElement = do_QueryReferent(mAutoFocusElement);
+  if (autoFocusElement && autoFocusElement->OwnerDoc() == this) {
+    mAutoFocusFired = true;
+
+    nsCOMPtr<nsPIDOMWindowOuter> topWindow =
+      FindTopWindowForElement(autoFocusElement);
+    if (!topWindow) {
+      return;
+    }
+
+    // NOTE: This may be removed in the future since the spec technically
+    // allows autofocus after load.
+    nsCOMPtr<nsIDocument> topDoc = topWindow->GetExtantDoc();
+    if (topDoc && topDoc->GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE) {
+      return;
+    }
+
+    nsCOMPtr<nsIRunnable> event =
+      new nsAutoFocusEvent(autoFocusElement.forget(), topWindow.forget());
+    nsresult rv = NS_DispatchToCurrentThread(event.forget());
+    NS_ENSURE_SUCCESS_VOID(rv);
+  }
+}
+
 void
 nsDocument::SetScrollToRef(nsIURI *aDocumentURI)
 {
diff --git dom/base/nsDocument.h dom/base/nsDocument.h
index a25c4b417861..d015aae0bc8f 100644
--- dom/base/nsDocument.h
+++ dom/base/nsDocument.h
@@ -804,6 +804,9 @@ public:
   // Only BlockOnload should call this!
   void AsyncBlockOnload();
 
+  virtual void SetAutoFocusElement(Element* aAutoFocusElement) override;
+  virtual void TriggerAutoFocus() override;
+
   virtual void SetScrollToRef(nsIURI *aDocumentURI) override;
   virtual void ScrollToRef() override;
   virtual void ResetScrolledToRefAlready() override;
@@ -1306,6 +1309,8 @@ private:
 
   RefPtr<nsContentList> mImageMaps;
 
+  nsWeakPtr mAutoFocusElement;
+
   nsCString mScrollToRef;
   uint8_t mScrolledToRefAlready : 1;
   uint8_t mChangeScrollPosWhenScrollingToRef : 1;
@@ -1431,6 +1436,9 @@ private:
 public:
   bool mWillReparent;
 #endif
+
+private:
+  bool mAutoFocusFired : 1;
 };
 
 class nsDocumentOnStack
diff --git dom/base/nsFocusManager.cpp dom/base/nsFocusManager.cpp
index dfec30f7da13..a8faa46fff3c 100644
--- dom/base/nsFocusManager.cpp
+++ dom/base/nsFocusManager.cpp
@@ -1603,7 +1603,7 @@ nsFocusManager::CheckIfFocusable(nsIContent* aContent, uint32_t aFlags)
   }
 
   // Make sure that our frames are up to date while ensuring the presshell is
-  // also initialized in case we come from an autofocus event.
+  // also initialized in case we come from a script calling focus() early.
   mEventHandlingNeedsFlush = false;
   doc->FlushPendingNotifications(FlushType::EnsurePresShellInitAndFrames);
 
diff --git dom/base/nsIDocument.h dom/base/nsIDocument.h
index 53ef3746ffc7..98d5b0d2ed15 100644
--- dom/base/nsIDocument.h
+++ dom/base/nsIDocument.h
@@ -2637,6 +2637,9 @@ public:
 
   virtual nsISupports* GetCurrentContentSink() = 0;
 
+  virtual void SetAutoFocusElement(Element* aAutoFocusElement) = 0;
+  virtual void TriggerAutoFocus() = 0;
+
   virtual void SetScrollToRef(nsIURI *aDocumentURI) = 0;
   virtual void ScrollToRef() = 0;
   virtual void ResetScrolledToRefAlready() = 0;
diff --git dom/html/nsGenericHTMLElement.cpp dom/html/nsGenericHTMLElement.cpp
index 334c80ace636..775ec887ba2c 100644
--- dom/html/nsGenericHTMLElement.cpp
+++ dom/html/nsGenericHTMLElement.cpp
@@ -111,64 +111,6 @@
 using namespace mozilla;
 using namespace mozilla::dom;
 
-/**
- * nsAutoFocusEvent is used to dispatch a focus event when a
- * nsGenericHTMLFormElement is binded to the tree with the autofocus attribute
- * enabled.
- */
-class nsAutoFocusEvent : public Runnable
-{
-public:
-  explicit nsAutoFocusEvent(nsGenericHTMLFormElement* aElement)
-    : mozilla::Runnable("nsAutoFocusEvent")
-    , mElement(aElement)
-  {
-  }
-
-  NS_IMETHOD Run() override {
-    nsFocusManager* fm = nsFocusManager::GetFocusManager();
-    if (!fm) {
-      return NS_ERROR_NULL_POINTER;
-    }
-
-    nsIDocument* document = mElement->OwnerDoc();
-
-    nsPIDOMWindowOuter* window = document->GetWindow();
-    if (!window) {
-      return NS_OK;
-    }
-
-    // Trying to found the top window (equivalent to window.top).
-    if (nsCOMPtr<nsPIDOMWindowOuter> top = window->GetTop()) {
-      window = top;
-    }
-
-    if (window->GetFocusedNode()) {
-      return NS_OK;
-    }
-
-    nsCOMPtr<nsIDocument> topDoc = window->GetExtantDoc();
-    if (topDoc && topDoc->GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE) {
-      return NS_OK;
-    }
-
-    // If something is focused in the same document, ignore autofocus.
-    if (!fm->GetFocusedContent() ||
-        fm->GetFocusedContent()->OwnerDoc() != document) {
-      mozilla::ErrorResult rv;
-      mElement->Focus(rv);
-      return rv.StealNSResult();
-    }
-
-    return NS_OK;
-  }
-private:
-  // NOTE: nsGenericHTMLFormElement is saved as a nsGenericHTMLElement
-  // because AddRef/Release are ambiguous with nsGenericHTMLFormElement
-  // and Focus() is declared (and defined) in nsGenericHTMLElement class.
-  RefPtr<nsGenericHTMLElement> mElement;
-};
-
 NS_IMPL_ADDREF_INHERITED(nsGenericHTMLElement, nsGenericHTMLElementBase)
 NS_IMPL_RELEASE_INHERITED(nsGenericHTMLElement, nsGenericHTMLElementBase)
 
@@ -1882,10 +1824,8 @@ nsGenericHTMLFormElement::BindToTree(nsIDocument* aDocument,
   // the document should not be already loaded and the "browser.autofocus"
   // preference should be 'true'.
   if (IsAutofocusable() && HasAttr(kNameSpaceID_None, nsGkAtoms::autofocus) &&
-      nsContentUtils::AutoFocusEnabled()) {
-    nsCOMPtr<nsIRunnable> event = new nsAutoFocusEvent(this);
-    rv = NS_DispatchToCurrentThread(event);
-    NS_ENSURE_SUCCESS(rv, rv);
+      nsContentUtils::AutoFocusEnabled() && aDocument) {
+    aDocument->SetAutoFocusElement(this);
   }
 
   // If @form is set, the element *has* to be in a document, otherwise it
diff --git layout/base/PresShell.cpp layout/base/PresShell.cpp
index 0c94f8516271..f851f9c7df2c 100644
--- layout/base/PresShell.cpp
+++ layout/base/PresShell.cpp
@@ -1817,6 +1817,8 @@ PresShell::Initialize(nscoord aWidth, nscoord aHeight)
     NS_ENSURE_STATE(!mHaveShutDown);
   }
 
+  mDocument->TriggerAutoFocus();
+
   NS_ASSERTION(rootFrame, "How did that happen?");
 
   // Note: when the frame was created above it had the NS_FRAME_IS_DIRTY bit

commit 0c31aff36b45
Author: decltype <mozilla@decltype.org>
Date:   Fri Jan 12 00:17:58 2018 +0100

    Bug 712130 - Simplify focus stealing prevention for autofocus. r=bz
    
    Remove redundant check already implied by earlier condition: If the focus
    manager's focused content is in our document, all parent windows including the
    root window must have their focused node set to non-null.
    
    MozReview-Commit-ID: CryGb6mfczK
    
    --HG--
    extra : rebase_source : ed5c452f68b76a656512026b5d162d8782083c2c
---
 dom/base/nsDocument.cpp | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git dom/base/nsDocument.cpp dom/base/nsDocument.cpp
index 3ed0e93222ec..18b81abe599a 100644
--- dom/base/nsDocument.cpp
+++ dom/base/nsDocument.cpp
@@ -9490,27 +9490,14 @@ public:
       return NS_OK;
     }
 
-    nsFocusManager* fm = nsFocusManager::GetFocusManager();
-    if (!fm) {
-      return NS_ERROR_NULL_POINTER;
-    }
-
-    nsIDocument* document = mElement->OwnerDoc();
-
     // Don't steal focus from the user.
     if (mTopWindow->GetFocusedNode()) {
       return NS_OK;
     }
 
-    // If something is focused in the same document, ignore autofocus.
-    if (!fm->GetFocusedContent() ||
-        fm->GetFocusedContent()->OwnerDoc() != document) {
-      mozilla::ErrorResult rv;
-      mElement->Focus(rv);
-      return rv.StealNSResult();
-    }
-
-    return NS_OK;
+    mozilla::ErrorResult rv;
+    mElement->Focus(rv);
+    return rv.StealNSResult();
   }
 private:
   nsCOMPtr<Element> mElement;