Bug 601030 (1/2) - Don't let a frame to steal focus from another frame with autofocus. r=smaug a=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 17 Dec 2010 09:47:58 -0800
changeset 59450 14ca1420a81d0e7571acf2600c9c76d1068e0172
parent 59449 0ae68f7446c014ecd951156c70f1ddb197e66ee4
child 59451 dc8dea3241427c47d6dff6e51f2db02240e3f430
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewerssmaug, sicking
bugs601030
milestone2.0b9pre
Bug 601030 (1/2) - Don't let a frame to steal focus from another frame with autofocus. r=smaug a=sicking
content/html/content/src/nsGenericHTMLElement.cpp
content/html/content/test/Makefile.in
content/html/content/test/test_bug546995-2.html
content/html/content/test/test_bug601030.html
layout/reftests/bugs/557087-1.html
layout/reftests/bugs/557087-2.html
--- a/content/html/content/src/nsGenericHTMLElement.cpp
+++ b/content/html/content/src/nsGenericHTMLElement.cpp
@@ -195,16 +195,24 @@ public:
 
     nsIDocument* document = mElement->GetOwnerDoc();
     if (!document) {
       return NS_OK;
     }
 
     // Do not autofocus if an sub-window is focused.
     nsPIDOMWindow* window = document->GetWindow();
+
+    // Trying to found the top window (equivalent to window.top).
+    nsCOMPtr<nsIDOMWindow> top;
+    window->GetTop(getter_AddRefs(top));
+    if (top) {
+      window = static_cast<nsPIDOMWindow*>(top.get());
+    }
+
     if (window && window->GetFocusedNode()) {
       return NS_OK;
     }
 
     // If something is focused in the same document, ignore autofocus.
     if (!fm->GetFocusedContent() ||
         fm->GetFocusedContent()->GetOwnerDoc() != document) {
       return mElement->Focus();
--- a/content/html/content/test/Makefile.in
+++ b/content/html/content/test/Makefile.in
@@ -247,12 +247,13 @@ include $(topsrcdir)/config/rules.mk
 		test_bug605124-1.html \
 		test_bug605124-2.html \
 		test_bug605125-1.html \
 		test_bug605125-2.html \
 		test_bug612730.html \
 		test_bug613722.html \
 		test_bug613979.html \
 		test_bug615833.html \
+		test_bug601030.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
--- a/content/html/content/test/test_bug546995-2.html
+++ b/content/html/content/test/test_bug546995-2.html
@@ -7,46 +7,48 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 546995</title>
   <script type="application/javascript" src="/MochiKit/packed.js"></script>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=546995">Mozilla Bug 546995</a>
 <p id="display"></p>
-<iframe id="iframe"></iframe>
 <div id='content'>
 </div>
 <pre id="test">
 <script type="application/javascript;version=1.8">
 
 /** Test for Bug 546995 **/
 
 SimpleTest.waitForExplicitFinish();
 
 var gGen = runTests();
 addLoadEvent(function() { gGen.next(); });
 
 var gElement = "";
 
 function runTests()
 {
-  var iframe = document.getElementById("iframe");
-  var iframeCw = iframe.contentWindow;
-
   // TODO: keygen should be added when correctly implemented, see bug 101019.
   var elements = ["input", "textarea", "select", "button"];
 
   for each(element in elements) {
+    var iframe = document.createElement("iframe");
+    document.getElementById('content').appendChild(iframe);
+    var iframeCw = iframe.contentWindow;
+
     gElement = element;
     iframeCw.location = "file_bug546995.html";
     yield;
     is(iframe.contentDocument.activeElement,
        iframe.contentDocument.getElementById('a'),
        "The first inserted element with autofocus should be focused");
+
+    document.getElementById('content').removeChild(iframe);
   }
 
   // Now we want to focus the body element.
   document.activeElement.blur();
   is(document.activeElement, document.body,
      "Body should be the active element");
 
   // Adding elements with autofocus.
new file mode 100644
--- /dev/null
+++ b/content/html/content/test/test_bug601030.html
@@ -0,0 +1,43 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=601030
+-->
+<head>
+  <title>Test for Bug 601030</title>
+  <script type="application/javascript" src="/MochiKit/packed.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=601030">Mozilla Bug 601030</a>
+<p id="display"></p>
+<div id="content">
+  <iframe src="data:text/html,<input autofocus>"></iframe>
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 601030 **/
+
+SimpleTest.waitForExplicitFinish();
+addLoadEvent(function() {
+  var f = document.createElement("iframe");
+
+  f.addEventListener("load", function() {
+    f.removeEventListener("load", arguments.callee, false);
+    SimpleTest.executeSoon(function() {
+      isnot(document.activeElement, f,
+            "autofocus should not work when another frame is inserted in the document");
+      SimpleTest.finish();
+    });
+  }, false);
+
+  f.src = "data:text/html,<input autofocus>";
+  document.getElementById('content').appendChild(f);
+});
+
+</script>
+</pre>
+</body>
+</html>