Reviewed by Richard.
[WebKit-https.git] / WebCore / kwq / KWQWidget.mm
index 7d6d16b8bf2e5f37a7387680ccfe285e896b687c..3b77a97c86c0635f7f9c41cf78d88f58a7f2f50d 100644 (file)
@@ -235,7 +235,19 @@ void QWidget::setFocus()
 
     KWQ_BLOCK_EXCEPTIONS;
     if ([view acceptsFirstResponder]) {
-        [KWQKHTMLPart::bridgeForWidget(this) makeFirstResponder:view];
+        WebCoreBridge *bridge = KWQKHTMLPart::bridgeForWidget(this);
+        NSResponder *oldFirstResponder = [bridge firstResponder];
+
+        [bridge makeFirstResponder:view];
+
+        // setting focus can actually cause a style change which might
+        // remove the view from its superview while it's being made
+        // first responder. This confuses AppKit so we must restore
+        // the old first responder.
+
+        if (![view superview]) {
+            [bridge makeFirstResponder:oldFirstResponder];
+        }
     }
     KWQ_UNBLOCK_EXCEPTIONS;
 }