Bug 778654 - Implement and move tabIndex functions to Element class to avoid duplicate work.r=peterv,heycam

MozReview-Commit-ID: uetkJztNcn

--HG--
extra : transplant_source : U%E7va%AE%E6%FE%BDN.%28%AA%8F%19%1D%FF%26%E6%B7t
This commit is contained in:
Daosheng Mu 2016-05-24 12:22:17 +08:00
parent cf5845e98a
commit 8083dbd7bd
10 changed files with 107 additions and 74 deletions

View File

@ -291,6 +291,55 @@ Element::UpdateEditableState(bool aNotify)
}
}
int32_t
Element::TabIndex()
{
const nsAttrValue* attrVal = mAttrsAndChildren.GetAttr(nsGkAtoms::tabindex);
if (attrVal && attrVal->Type() == nsAttrValue::eInteger) {
return attrVal->GetIntegerValue();
}
return TabIndexDefault();
}
void
Element::Focus(mozilla::ErrorResult& aError)
{
nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(this);
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm && domElement) {
aError = fm->SetFocus(domElement, 0);
}
}
void
Element::SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError)
{
nsAutoString value;
value.AppendInt(aTabIndex);
SetAttr(nsGkAtoms::tabindex, value, aError);
}
void
Element::Blur(mozilla::ErrorResult& aError)
{
if (!ShouldBlur(this)) {
return;
}
nsIDocument* doc = GetComposedDoc();
if (!doc) {
return;
}
nsPIDOMWindowOuter* win = doc->GetWindow();
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (win && fm) {
aError = fm->ClearFocus(win);
}
}
EventStates
Element::StyleStateFromLocks() const
{

View File

@ -193,6 +193,31 @@ public:
*/
void UpdateLinkState(EventStates aState);
virtual int32_t TabIndexDefault()
{
return -1;
}
/**
* Get tabIndex of this element. If not found, return TabIndexDefault.
*/
int32_t TabIndex();
/**
* Set tabIndex value to this element.
*/
void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError);
/**
* Make focus on this element.
*/
virtual void Focus(mozilla::ErrorResult& aError);
/**
* Show blur and clear focus.
*/
virtual void Blur(mozilla::ErrorResult& aError);
/**
* The style state of this element. This is the real state of the element
* with any style locks applied for pseudo-class inspecting.

View File

@ -2582,34 +2582,6 @@ nsGenericHTMLFormElement::IsLabelable() const
//----------------------------------------------------------------------
void
nsGenericHTMLElement::Blur(mozilla::ErrorResult& aError)
{
if (!ShouldBlur(this)) {
return;
}
nsIDocument* doc = GetComposedDoc();
if (!doc) {
return;
}
nsPIDOMWindowOuter* win = doc->GetWindow();
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (win && fm) {
aError = fm->ClearFocus(win);
}
}
void
nsGenericHTMLElement::Focus(ErrorResult& aError)
{
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
aError = fm->SetFocus(this, 0);
}
}
void
nsGenericHTMLElement::Click()
{

View File

@ -51,6 +51,8 @@ class nsGenericHTMLElement : public nsGenericHTMLElementBase,
public nsIDOMHTMLElement
{
public:
using Element::SetTabIndex;
using Element::Focus;
explicit nsGenericHTMLElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsGenericHTMLElementBase(aNodeInfo)
{
@ -103,20 +105,6 @@ public:
SetHTMLBoolAttr(nsGkAtoms::hidden, aHidden, aError);
}
virtual void Click();
virtual int32_t TabIndexDefault()
{
return -1;
}
int32_t TabIndex()
{
return GetIntAttr(nsGkAtoms::tabindex, TabIndexDefault());
}
void SetTabIndex(int32_t aTabIndex, mozilla::ErrorResult& aError)
{
SetHTMLIntAttr(nsGkAtoms::tabindex, aTabIndex, aError);
}
virtual void Focus(mozilla::ErrorResult& aError);
virtual void Blur(mozilla::ErrorResult& aError);
void GetAccessKey(nsString& aAccessKey)
{
GetHTMLAttr(nsGkAtoms::accesskey, aAccessKey);

View File

@ -194,6 +194,9 @@ SVGAElement::IsFocusableInternal(int32_t *aTabIndex, bool aWithMouse)
}
return true;
}
if (nsSVGElement::IsFocusableInternal(aTabIndex, aWithMouse)) {
return true;
}
if (aTabIndex) {
*aTabIndex = -1;

View File

@ -610,6 +610,9 @@ nsSVGElement::ParseAttribute(int32_t aNamespaceID,
didSetResult = true;
}
foundMatch = true;
} else if (aAttribute == nsGkAtoms::tabindex) {
didSetResult = aResult.ParseIntValue(aValue);
foundMatch = true;
}
}
@ -1120,6 +1123,19 @@ nsSVGElement::ClassName()
return mClassAttribute.ToDOMAnimatedString(this);
}
bool
nsSVGElement::IsFocusableInternal(int32_t* aTabIndex, bool)
{
int32_t index = TabIndex();
if (index == -1) {
return false;
}
*aTabIndex = index;
return true;
}
//------------------------------------------------------------------------
// Helper class: MappedAttrParser, for parsing values of mapped attributes

View File

@ -317,6 +317,8 @@ public:
mozilla::dom::SVGSVGElement* GetOwnerSVGElement();
nsSVGElement* GetViewportElement();
already_AddRefed<mozilla::dom::SVGAnimatedString> ClassName();
virtual bool IsFocusableInternal(int32_t* aTabIndex, bool aWithMouse) override;
protected:
virtual JSObject* WrapNode(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;

View File

@ -21,9 +21,14 @@ interface SVGElement : Element {
readonly attribute SVGSVGElement? ownerSVGElement;
readonly attribute SVGElement? viewportElement;
attribute EventHandler oncopy;
attribute EventHandler oncut;
attribute EventHandler onpaste;
attribute EventHandler oncopy;
attribute EventHandler oncut;
attribute EventHandler onpaste;
[SetterThrows, Pure]
attribute long tabIndex;
[Throws] void focus();
[Throws] void blur();
};
SVGElement implements GlobalEventHandlers;

View File

@ -1697,16 +1697,6 @@ nsXULElement::Focus()
return rv.StealNSResult();
}
void
nsXULElement::Focus(ErrorResult& rv)
{
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
nsCOMPtr<nsIDOMElement> elem = do_QueryObject(this);
if (fm) {
rv = fm->SetFocus(this, 0);
}
}
NS_IMETHODIMP
nsXULElement::Blur()
{
@ -1715,23 +1705,6 @@ nsXULElement::Blur()
return rv.StealNSResult();
}
void
nsXULElement::Blur(ErrorResult& rv)
{
if (!ShouldBlur(this))
return;
nsIDocument* doc = GetComposedDoc();
if (!doc)
return;
nsPIDOMWindowOuter* win = doc->GetWindow();
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
if (win && fm) {
rv = fm->ClearFocus(win);
}
}
NS_IMETHODIMP
nsXULElement::Click()
{

View File

@ -341,6 +341,8 @@ class nsXULElement final : public nsStyledElement,
public nsIDOMXULElement
{
public:
using Element::Blur;
using Element::Focus;
explicit nsXULElement(already_AddRefed<mozilla::dom::NodeInfo> aNodeInfo);
static nsresult
@ -561,8 +563,6 @@ public:
already_AddRefed<nsIRDFResource> GetResource(mozilla::ErrorResult& rv);
nsIControllers* GetControllers(mozilla::ErrorResult& rv);
already_AddRefed<mozilla::dom::BoxObject> GetBoxObject(mozilla::ErrorResult& rv);
void Focus(mozilla::ErrorResult& rv);
void Blur(mozilla::ErrorResult& rv);
void Click(mozilla::ErrorResult& rv);
// The XPCOM DoCommand never fails, so it's OK for us.
already_AddRefed<nsINodeList>