WebCore:
authoradele@apple.com <adele@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 7 Mar 2008 01:30:28 +0000 (01:30 +0000)
committeradele@apple.com <adele@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 7 Mar 2008 01:30:28 +0000 (01:30 +0000)
        Reviewed by Darin.

        Fixes for for <rdar://problem/5785892> Implement activeElement attribute for HTMLDocument (HTML5)
        and <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)

        Tests: fast/dom/HTMLDocument/activeElement.html
               fast/dom/HTMLDocument/hasFocus.html

        * html/HTMLDocument.cpp:
        (WebCore::HTMLDocument::activeElement): Added. Returns the focused element, or the body element if nothing's focused.
        (WebCore::HTMLDocument::hasFocus): Added. Returns whether or not the document has focus (inclusive of sub-frames).
        * html/HTMLDocument.h: Added methods for activeElement and hasFocus.
        * html/HTMLDocument.idl: Added definitions for activeElement and hasFocus.

WebKitTools:

        Reviewed by Darin.

        Updated for testing <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)

        * DumpRenderTree/mac/UIDelegate.mm:
        (-[UIDelegate webViewFocus:]): Remove duplicate code that was also in LayoutTestController::setWindowIsKey
        (-[UIDelegate webViewUnfocus:]): Added.  In Safari, the default is to focus another window if there is one.
         To easily test cases where the window is unfocused, I made this just call setWindowIsKey(false).

LayoutTests:

        Reviewed by Darin.

        Tests for <rdar://problem/5785892> Implement activeElement attribute for HTMLDocument (HTML5)
        and <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)

        * fast/dom/HTMLDocument/activeElement-expected.txt: Added.
        * fast/dom/HTMLDocument/activeElement.html: Added.
        * fast/dom/HTMLDocument/hasFocus-expected.txt: Added.
        * fast/dom/HTMLDocument/hasFocus.html: Added.
        * fast/dom/HTMLDocument/resources/hasFocus-iframe.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@30866 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/HTMLDocument/activeElement-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/HTMLDocument/activeElement.html [new file with mode: 0644]
LayoutTests/fast/dom/HTMLDocument/hasFocus-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/HTMLDocument/hasFocus.html [new file with mode: 0644]
LayoutTests/fast/dom/HTMLDocument/resources/hasFocus-iframe.html [new file with mode: 0644]
WebCore/ChangeLog
WebCore/html/HTMLDocument.cpp
WebCore/html/HTMLDocument.h
WebCore/html/HTMLDocument.idl
WebKitTools/ChangeLog
WebKitTools/DumpRenderTree/mac/UIDelegate.mm

index 8ba2703..da2d2ed 100644 (file)
@@ -1,3 +1,16 @@
+2008-03-06  Adele Peterson  <adele@apple.com>
+
+        Reviewed by Darin.
+
+        Tests for <rdar://problem/5785892> Implement activeElement attribute for HTMLDocument (HTML5)
+        and <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)
+
+        * fast/dom/HTMLDocument/activeElement-expected.txt: Added.
+        * fast/dom/HTMLDocument/activeElement.html: Added.
+        * fast/dom/HTMLDocument/hasFocus-expected.txt: Added.
+        * fast/dom/HTMLDocument/hasFocus.html: Added.
+        * fast/dom/HTMLDocument/resources/hasFocus-iframe.html: Added.
+
 2008-03-06  Dan Bernstein  <mitz@apple.com>
 
         Reviewed by Darin Adler.
diff --git a/LayoutTests/fast/dom/HTMLDocument/activeElement-expected.txt b/LayoutTests/fast/dom/HTMLDocument/activeElement-expected.txt
new file mode 100644 (file)
index 0000000..12c25ca
--- /dev/null
@@ -0,0 +1,6 @@
+[object HTMLBodyElement]
+[object HTMLInputElement] id: one
+[object HTMLInputElement] id: two
+[object HTMLBodyElement]
+
diff --git a/LayoutTests/fast/dom/HTMLDocument/activeElement.html b/LayoutTests/fast/dom/HTMLDocument/activeElement.html
new file mode 100644 (file)
index 0000000..0ea6c6e
--- /dev/null
@@ -0,0 +1,27 @@
+<html>
+    <head>
+        <script>
+            function test()
+            {
+                if (window.layoutTestController)
+                    layoutTestController.dumpAsText();
+                log(document.activeElement);
+                document.getElementById('one').focus();
+                log(document.activeElement + " id: " + document.activeElement.id);
+                document.getElementById('two').focus();
+                log(document.activeElement + " id: " + document.activeElement.id);
+                document.getElementById('two').blur();
+                log(document.activeElement);
+            }
+            function log(msg)
+            {
+                document.getElementById("console").appendChild(document.createTextNode(msg + "\n"));
+            }
+        </script>
+    </head>
+    <body onload="test()">
+        <input id="one"><input id="two">
+        <br>
+        <pre id="console"></pre>
+    </body>
+</html>
diff --git a/LayoutTests/fast/dom/HTMLDocument/hasFocus-expected.txt b/LayoutTests/fast/dom/HTMLDocument/hasFocus-expected.txt
new file mode 100644 (file)
index 0000000..9531873
--- /dev/null
@@ -0,0 +1,13 @@
+   
+Focus the text field in the frame
+Main document.hasFocus() should be true : PASS
+Sub-frame document.hasFocus() should be true : PASS
+
+Focus the text field in the main document
+Main document.hasFocus() should be true : PASS
+Sub-frame document.hasFocus() should be false : PASS
+
+Blur the window
+Main document.hasFocus() should be false : PASS
+Sub-frame document.hasFocus() should be false : PASS
+
diff --git a/LayoutTests/fast/dom/HTMLDocument/hasFocus.html b/LayoutTests/fast/dom/HTMLDocument/hasFocus.html
new file mode 100644 (file)
index 0000000..2bed1ae
--- /dev/null
@@ -0,0 +1,75 @@
+<html>
+    <head>
+        <script>
+            var subFrameDocument;
+            function test()
+            {
+                if (window.layoutTestController) {
+                    layoutTestController.dumpAsText();
+                    layoutTestController.waitUntilDone();
+                } else {
+                    /*
+                     * Opening a separate window is only necessary for testing in Safari.
+                     * In Safari, window.blur() only blurs the current window if there's another window to focus.
+                     * In DumpRenderTree, it will just cause the window to lose focus regardless.
+                     * The popup blocker needs to be off to pass this test in Safari.
+                     */
+                    window.open("about:blank", "test");
+                    window.focus();
+                }
+                subFrameDocument = document.getElementById('subFrame').contentDocument;
+                
+                log("Focus the text field in the frame");
+                subFrameDocument.getElementById('two').focus();
+                checkFocus(true, true);
+                
+                log("\nFocus the text field in the main document");
+                document.getElementById('one').focus();
+                checkFocus(true, false);
+                
+                log("\nBlur the window");
+                window.blur();
+                checkFocus(false, false);
+                
+                setTimeout(doneHandler, 1);
+            }
+            function checkFocus(expectedMain, expectedSub)
+            {
+                var mainFocus = document.hasFocus();
+                var msg = "Main document.hasFocus() should be " + expectedMain;
+                if (mainFocus != expectedMain)
+                    msg = msg + " : FAIL";
+                else
+                    msg = msg + " : PASS";
+                
+                log(msg);
+
+                var subFocus = subFrameDocument.hasFocus();
+                msg = "Sub-frame document.hasFocus() should be " + expectedSub;
+                if (subFocus != expectedSub)
+                    msg = msg + " : FAIL";
+                else
+                    msg = msg + " : PASS";
+
+                log(msg);
+            }
+            
+            function doneHandler()
+            {
+                if (window.layoutTestController)
+                    layoutTestController.notifyDone();
+            }
+            function log(msg)
+            {
+                document.getElementById("console").appendChild(document.createTextNode(msg + "\n"));
+            }
+        </script>
+    </head>
+    <body onload="test()">
+        <input id="one">
+        <iframe id="subFrame" src="resources/hasFocus-iframe.html"></iframe>
+        <br>
+        <pre id="console"></pre>
+    </body>
+</html>
+
diff --git a/LayoutTests/fast/dom/HTMLDocument/resources/hasFocus-iframe.html b/LayoutTests/fast/dom/HTMLDocument/resources/hasFocus-iframe.html
new file mode 100644 (file)
index 0000000..0c479f0
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+    <body>
+        <input id="two">
+    </body>
+</html>
index 4aabe54..0d06269 100644 (file)
@@ -1,3 +1,19 @@
+2008-03-06  Adele Peterson  <adele@apple.com>
+
+        Reviewed by Darin.
+
+        Fixes for for <rdar://problem/5785892> Implement activeElement attribute for HTMLDocument (HTML5)
+        and <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)
+
+        Tests: fast/dom/HTMLDocument/activeElement.html
+               fast/dom/HTMLDocument/hasFocus.html
+
+        * html/HTMLDocument.cpp:
+        (WebCore::HTMLDocument::activeElement): Added. Returns the focused element, or the body element if nothing's focused.
+        (WebCore::HTMLDocument::hasFocus): Added. Returns whether or not the document has focus (inclusive of sub-frames).
+        * html/HTMLDocument.h: Added methods for activeElement and hasFocus.
+        * html/HTMLDocument.idl: Added definitions for activeElement and hasFocus.
+
 2008-03-06  Dan Bernstein  <mitz@apple.com>
 
         Reviewed by Darin Adler.
index eb9eb4d..c27a090 100644 (file)
 #include "DocumentLoader.h"
 #include "DocumentType.h"
 #include "ExceptionCode.h"
+#include "FocusController.h"
 #include "Frame.h"
 #include "FrameLoader.h"
+#include "FrameTree.h"
 #include "FrameView.h"
 #include "HTMLBodyElement.h"
 #include "HTMLElement.h"
@@ -140,6 +142,25 @@ String HTMLDocument::compatMode() const
     return inCompatMode() ? "BackCompat" : "CSS1Compat";
 }
 
+Element* HTMLDocument::activeElement()
+{
+    if (Node* node = focusedNode())
+        if (node->isElementNode())
+            return static_cast<Element*>(node);
+    return body();
+}
+
+bool HTMLDocument::hasFocus()
+{
+    if (!page()->focusController()->isActive())
+        return false;
+    if (Frame* focusedFrame = page()->focusController()->focusedFrame()) {
+        if (focusedFrame->tree()->isDescendantOf(frame()))
+            return true;
+    }
+    return false;
+}
+
 String HTMLDocument::bgColor()
 {
     HTMLElement* b = body();
index 567d121..bed2667 100644 (file)
@@ -49,6 +49,9 @@ public:
 
     String compatMode() const;
 
+    Element* activeElement();
+    bool hasFocus();
+
     String bgColor();
     void setBgColor(const String&);
     String fgColor();
index c0aabfe..b50e4b0 100644 (file)
@@ -54,6 +54,9 @@ module html {
                  attribute [ConvertNullToNullString] DOMString designMode;
         readonly attribute DOMString compatMode;
 
+        readonly attribute Element activeElement;
+        boolean hasFocus();
+
         // Deprecated attributes
                  attribute [ConvertNullToNullString] DOMString bgColor;
                  attribute [ConvertNullToNullString] DOMString fgColor;
index 5ccf076..cf59664 100644 (file)
@@ -1,3 +1,14 @@
+2008-03-06  Adele Peterson  <adele@apple.com>
+
+        Reviewed by Darin.
+
+        Updated for testing <rdar://problem/5785895> Implement hasFocus() for HTMLDocument (HTML5)
+
+        * DumpRenderTree/mac/UIDelegate.mm:
+        (-[UIDelegate webViewFocus:]): Remove duplicate code that was also in LayoutTestController::setWindowIsKey
+        (-[UIDelegate webViewUnfocus:]): Added.  In Safari, the default is to focus another window if there is one.
+         To easily test cases where the window is unfocused, I made this just call setWindowIsKey(false).
+
 2008-03-06  Matt Lilek  <webkit@mattlilek.com>
 
         Reviewed by Adam Roben.
index 9c440a7..cf4d342 100644 (file)
@@ -98,9 +98,11 @@ DumpRenderTreeDraggingInfo *draggingInfo = nil;
 - (void)webViewFocus:(WebView *)webView
 {
     layoutTestController->setWindowIsKey(true);
-    NSView *documentView = [[mainFrame frameView] documentView];
-    if ([documentView isKindOfClass:[WebHTMLView class]])
-        [(WebHTMLView *)documentView _updateFocusedAndActiveState];
+}
+
+- (void)webViewUnfocus:(WebView *)webView
+{
+    layoutTestController->setWindowIsKey(false);
 }
 
 - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request