diff --git a/mozilla/content/base/src/nsGenericElement.cpp b/mozilla/content/base/src/nsGenericElement.cpp index 9036eedda14..357900afb0c 100644 --- a/mozilla/content/base/src/nsGenericElement.cpp +++ b/mozilla/content/base/src/nsGenericElement.cpp @@ -96,6 +96,9 @@ #include "nsIServiceManager.h" #include "nsIDOMEventListener.h" +#include "nsIWebNavigation.h" +#include "nsIBaseWindow.h" + #include "jsapi.h" #include "nsIDOMXPathEvaluator.h" @@ -2422,6 +2425,34 @@ nsGenericElement::SetFocus(nsPresContext* aPresContext) } } +// static +PRBool +nsGenericElement::ShouldFocus(nsIContent *aContent) +{ + PRBool visible = PR_TRUE; + + // Figure out if we're focusing an element in an inactive (hidden) + // tab (whose docshell is not visible), if so, drop this focus + // request on the floor + + nsIDocument *document = aContent->GetDocument(); + + if (document) { + nsIScriptGlobalObject *sgo = document->GetScriptGlobalObject(); + + if (sgo) { + nsCOMPtr webNav(do_GetInterface(sgo)); + nsCOMPtr baseWin(do_QueryInterface(webNav)); + + if (baseWin) { + baseWin->GetVisibility(&visible); + } + } + } + + return visible; +} + nsIContent* nsGenericElement::GetBindingParent() const { diff --git a/mozilla/content/base/src/nsGenericElement.h b/mozilla/content/base/src/nsGenericElement.h index 135b7a86a55..0a47fe343c1 100644 --- a/mozilla/content/base/src/nsGenericElement.h +++ b/mozilla/content/base/src/nsGenericElement.h @@ -641,6 +641,8 @@ public: static PRBool HasMutationListeners(nsIContent* aContent, PRUint32 aType); + static PRBool ShouldFocus(nsIContent *aContent); + static nsresult InitHashes(); static PLDHashTable sEventListenerManagersHash; diff --git a/mozilla/content/html/content/src/nsHTMLAnchorElement.cpp b/mozilla/content/html/content/src/nsHTMLAnchorElement.cpp index 99a190a4dcd..d7b24a2f5b8 100644 --- a/mozilla/content/html/content/src/nsHTMLAnchorElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLAnchorElement.cpp @@ -200,7 +200,9 @@ nsHTMLAnchorElement::Blur() NS_IMETHODIMP nsHTMLAnchorElement::Focus() { - SetElementFocus(PR_TRUE); + if (ShouldFocus(this)) { + SetElementFocus(PR_TRUE); + } return NS_OK; } diff --git a/mozilla/content/html/content/src/nsHTMLButtonElement.cpp b/mozilla/content/html/content/src/nsHTMLButtonElement.cpp index a8e7b4ceb71..8e960635cf7 100644 --- a/mozilla/content/html/content/src/nsHTMLButtonElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLButtonElement.cpp @@ -184,7 +184,9 @@ nsHTMLButtonElement::Blur() NS_IMETHODIMP nsHTMLButtonElement::Focus() { - SetElementFocus(PR_TRUE); + if (ShouldFocus(this)) { + SetElementFocus(PR_TRUE); + } return NS_OK; } diff --git a/mozilla/content/html/content/src/nsHTMLInputElement.cpp b/mozilla/content/html/content/src/nsHTMLInputElement.cpp index c3f2def5967..3b702e14a20 100644 --- a/mozilla/content/html/content/src/nsHTMLInputElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLInputElement.cpp @@ -1037,7 +1037,9 @@ nsHTMLInputElement::Blur() NS_IMETHODIMP nsHTMLInputElement::Focus() { - SetElementFocus(PR_TRUE); + if (ShouldFocus(this)) { + SetElementFocus(PR_TRUE); + } return NS_OK; } diff --git a/mozilla/content/html/content/src/nsHTMLSelectElement.cpp b/mozilla/content/html/content/src/nsHTMLSelectElement.cpp index 1af46ad223d..29ec62ddad7 100644 --- a/mozilla/content/html/content/src/nsHTMLSelectElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLSelectElement.cpp @@ -1568,7 +1568,9 @@ nsHTMLSelectElement::Blur() NS_IMETHODIMP nsHTMLSelectElement::Focus() { - SetElementFocus(PR_TRUE); + if (ShouldFocus(this)) { + SetElementFocus(PR_TRUE); + } return NS_OK; } diff --git a/mozilla/content/html/content/src/nsHTMLTextAreaElement.cpp b/mozilla/content/html/content/src/nsHTMLTextAreaElement.cpp index ac635a01380..e5b5298fac0 100644 --- a/mozilla/content/html/content/src/nsHTMLTextAreaElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLTextAreaElement.cpp @@ -225,7 +225,9 @@ nsHTMLTextAreaElement::Blur() NS_IMETHODIMP nsHTMLTextAreaElement::Focus() { - SetElementFocus(PR_TRUE); + if (ShouldFocus(this)) { + SetElementFocus(PR_TRUE); + } return NS_OK; } diff --git a/mozilla/content/xul/content/src/nsXULElement.cpp b/mozilla/content/xul/content/src/nsXULElement.cpp index add25e84434..be4597f6d1f 100644 --- a/mozilla/content/xul/content/src/nsXULElement.cpp +++ b/mozilla/content/xul/content/src/nsXULElement.cpp @@ -3749,6 +3749,10 @@ nsXULElement::IsAncestor(nsIDOMNode* aParentNode, nsIDOMNode* aChildNode) NS_IMETHODIMP nsXULElement::Focus() { + if (!nsGenericElement::ShouldFocus(this)) { + return NS_OK; + } + // What kind of crazy tries to focus an element without a doc? if (!mDocument) return NS_OK;