1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "HTMLFormControlAccessible.h"
7 
8 #include "LocalAccessible-inl.h"
9 #include "nsAccUtils.h"
10 #include "nsEventShell.h"
11 #include "nsTextEquivUtils.h"
12 #include "Relation.h"
13 #include "Role.h"
14 #include "States.h"
15 
16 #include "nsContentList.h"
17 #include "mozilla/dom/HTMLInputElement.h"
18 #include "mozilla/dom/HTMLTextAreaElement.h"
19 #include "nsIFormControl.h"
20 #include "nsITextControlFrame.h"
21 #include "nsNameSpaceManager.h"
22 #include "mozilla/dom/ScriptSettings.h"
23 
24 #include "mozilla/EditorBase.h"
25 #include "mozilla/EventStates.h"
26 #include "mozilla/FloatingPoint.h"
27 #include "mozilla/Preferences.h"
28 #include "mozilla/TextEditor.h"
29 
30 using namespace mozilla;
31 using namespace mozilla::dom;
32 using namespace mozilla::a11y;
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 // HTMLFormAccessible
36 ////////////////////////////////////////////////////////////////////////////////
37 
NativeRole() const38 role HTMLFormAccessible::NativeRole() const {
39   nsAutoString name;
40   const_cast<HTMLFormAccessible*>(this)->Name(name);
41   return name.IsEmpty() ? roles::FORM : roles::FORM_LANDMARK;
42 }
43 
LandmarkRole() const44 nsAtom* HTMLFormAccessible::LandmarkRole() const {
45   if (!HasOwnContent()) {
46     return nullptr;
47   }
48 
49   // Only return xml-roles "form" if the form has an accessible name.
50   nsAutoString name;
51   const_cast<HTMLFormAccessible*>(this)->Name(name);
52   return name.IsEmpty() ? HyperTextAccessibleWrap::LandmarkRole()
53                         : nsGkAtoms::form;
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 // HTMLRadioButtonAccessible
58 ////////////////////////////////////////////////////////////////////////////////
59 
NativeState() const60 uint64_t HTMLRadioButtonAccessible::NativeState() const {
61   uint64_t state = AccessibleWrap::NativeState();
62 
63   state |= states::CHECKABLE;
64 
65   HTMLInputElement* input = HTMLInputElement::FromNode(mContent);
66   if (input && input->Checked()) state |= states::CHECKED;
67 
68   return state;
69 }
70 
GetPositionAndSizeInternal(int32_t * aPosInSet,int32_t * aSetSize)71 void HTMLRadioButtonAccessible::GetPositionAndSizeInternal(int32_t* aPosInSet,
72                                                            int32_t* aSetSize) {
73   Unused << ComputeGroupAttributes(aPosInSet, aSetSize);
74 }
75 
ComputeGroupAttributes(int32_t * aPosInSet,int32_t * aSetSize) const76 Relation HTMLRadioButtonAccessible::ComputeGroupAttributes(
77     int32_t* aPosInSet, int32_t* aSetSize) const {
78   Relation rel = Relation();
79   int32_t namespaceId = mContent->NodeInfo()->NamespaceID();
80   nsAutoString tagName;
81   mContent->NodeInfo()->GetName(tagName);
82 
83   nsAutoString type;
84   mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
85   nsAutoString name;
86   mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::name, name);
87 
88   RefPtr<nsContentList> inputElms;
89 
90   nsCOMPtr<nsIFormControl> formControlNode(do_QueryInterface(mContent));
91   if (dom::Element* formElm = formControlNode->GetFormElement()) {
92     inputElms = NS_GetContentList(formElm, namespaceId, tagName);
93   } else {
94     inputElms = NS_GetContentList(mContent->OwnerDoc(), namespaceId, tagName);
95   }
96   NS_ENSURE_TRUE(inputElms, rel);
97 
98   uint32_t inputCount = inputElms->Length(false);
99 
100   // Compute posinset and setsize.
101   int32_t indexOf = 0;
102   int32_t count = 0;
103 
104   for (uint32_t index = 0; index < inputCount; index++) {
105     nsIContent* inputElm = inputElms->Item(index, false);
106     if (inputElm->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
107                                            type, eCaseMatters) &&
108         inputElm->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
109                                            name, eCaseMatters) &&
110         mDoc->HasAccessible(inputElm)) {
111       count++;
112       rel.AppendTarget(mDoc->GetAccessible(inputElm));
113       if (inputElm == mContent) indexOf = count;
114     }
115   }
116 
117   *aPosInSet = indexOf;
118   *aSetSize = count;
119   return rel;
120 }
121 
RelationByType(RelationType aType) const122 Relation HTMLRadioButtonAccessible::RelationByType(RelationType aType) const {
123   if (aType == RelationType::MEMBER_OF) {
124     int32_t unusedPos, unusedSetSize;
125     return ComputeGroupAttributes(&unusedPos, &unusedSetSize);
126   }
127 
128   return LocalAccessible::RelationByType(aType);
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 // HTMLButtonAccessible
133 ////////////////////////////////////////////////////////////////////////////////
134 
HTMLButtonAccessible(nsIContent * aContent,DocAccessible * aDoc)135 HTMLButtonAccessible::HTMLButtonAccessible(nsIContent* aContent,
136                                            DocAccessible* aDoc)
137     : HyperTextAccessibleWrap(aContent, aDoc) {
138   mGenericTypes |= eButton;
139 }
140 
ActionCount() const141 uint8_t HTMLButtonAccessible::ActionCount() const { return 1; }
142 
ActionNameAt(uint8_t aIndex,nsAString & aName)143 void HTMLButtonAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
144   if (aIndex == eAction_Click) aName.AssignLiteral("press");
145 }
146 
DoAction(uint8_t aIndex) const147 bool HTMLButtonAccessible::DoAction(uint8_t aIndex) const {
148   if (aIndex != eAction_Click) return false;
149 
150   DoCommand();
151   return true;
152 }
153 
State()154 uint64_t HTMLButtonAccessible::State() {
155   uint64_t state = HyperTextAccessibleWrap::State();
156   if (state == states::DEFUNCT) return state;
157 
158   // Inherit states from input@type="file" suitable for the button. Note,
159   // no special processing for unavailable state since inheritance is supplied
160   // other code paths.
161   if (mParent && mParent->IsHTMLFileInput()) {
162     uint64_t parentState = mParent->State();
163     state |= parentState & (states::BUSY | states::REQUIRED | states::HASPOPUP |
164                             states::INVALID);
165   }
166 
167   return state;
168 }
169 
NativeState() const170 uint64_t HTMLButtonAccessible::NativeState() const {
171   uint64_t state = HyperTextAccessibleWrap::NativeState();
172 
173   EventStates elmState = mContent->AsElement()->State();
174   if (elmState.HasState(NS_EVENT_STATE_DEFAULT)) state |= states::DEFAULT;
175 
176   return state;
177 }
178 
NativeRole() const179 role HTMLButtonAccessible::NativeRole() const { return roles::PUSHBUTTON; }
180 
NativeName(nsString & aName) const181 ENameValueFlag HTMLButtonAccessible::NativeName(nsString& aName) const {
182   // No need to check @value attribute for buttons since this attribute results
183   // in native anonymous text node and the name is calculated from subtree.
184   // The same magic works for @alt and @value attributes in case of type="image"
185   // element that has no valid @src (note if input@type="image" has an image
186   // then neither @alt nor @value attributes are used to generate a visual label
187   // and thus we need to obtain the accessible name directly from attribute
188   // value). Also the same algorithm works in case of default labels for
189   // type="submit"/"reset"/"image" elements.
190 
191   ENameValueFlag nameFlag = LocalAccessible::NativeName(aName);
192   if (!aName.IsEmpty() || !mContent->IsHTMLElement(nsGkAtoms::input) ||
193       !mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
194                                           nsGkAtoms::image, eCaseMatters)) {
195     return nameFlag;
196   }
197 
198   if (!mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::alt,
199                                       aName)) {
200     mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aName);
201   }
202 
203   aName.CompressWhitespace();
204   return eNameOK;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 // HTMLButtonAccessible: Widgets
209 
IsWidget() const210 bool HTMLButtonAccessible::IsWidget() const { return true; }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 // HTMLTextFieldAccessible
214 ////////////////////////////////////////////////////////////////////////////////
215 
HTMLTextFieldAccessible(nsIContent * aContent,DocAccessible * aDoc)216 HTMLTextFieldAccessible::HTMLTextFieldAccessible(nsIContent* aContent,
217                                                  DocAccessible* aDoc)
218     : HyperTextAccessibleWrap(aContent, aDoc) {
219   mType = mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
220                                              nsGkAtoms::password, eIgnoreCase)
221               ? eHTMLTextPasswordFieldType
222               : eHTMLTextFieldType;
223 }
224 
NativeRole() const225 role HTMLTextFieldAccessible::NativeRole() const {
226   if (mType == eHTMLTextPasswordFieldType) {
227     return roles::PASSWORD_TEXT;
228   }
229   if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::list_)) {
230     return roles::EDITCOMBOBOX;
231   }
232   return roles::ENTRY;
233 }
234 
NativeAttributes()235 already_AddRefed<AccAttributes> HTMLTextFieldAccessible::NativeAttributes() {
236   RefPtr<AccAttributes> attributes =
237       HyperTextAccessibleWrap::NativeAttributes();
238 
239   // Expose type for text input elements as it gives some useful context,
240   // especially for mobile.
241   nsAutoString type;
242   // In the case of this element being part of a binding, the binding's
243   // parent's type should have precedence. For example an input[type=number]
244   // has an embedded anonymous input[type=text] (along with spinner buttons).
245   // In that case, we would want to take the input type from the parent
246   // and not the anonymous content.
247   nsIContent* widgetElm = BindingOrWidgetParent();
248   if ((widgetElm && widgetElm->AsElement()->GetAttr(kNameSpaceID_None,
249                                                     nsGkAtoms::type, type)) ||
250       mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::type,
251                                      type)) {
252     attributes->SetAttribute(nsGkAtoms::textInputType, type);
253     if (!ARIARoleMap() && type.EqualsLiteral("search")) {
254       attributes->SetAttribute(nsGkAtoms::xmlroles, u"searchbox"_ns);
255     }
256   }
257 
258   // If this element  has the placeholder attribute set,
259   // and if that is not identical to the name, expose it as an object attribute.
260   nsAutoString placeholderText;
261   if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::placeholder,
262                                      placeholderText)) {
263     nsAutoString name;
264     const_cast<HTMLTextFieldAccessible*>(this)->Name(name);
265     if (!name.Equals(placeholderText)) {
266       attributes->SetAttribute(nsGkAtoms::placeholder, placeholderText);
267     }
268   }
269 
270   return attributes.forget();
271 }
272 
NativeName(nsString & aName) const273 ENameValueFlag HTMLTextFieldAccessible::NativeName(nsString& aName) const {
274   ENameValueFlag nameFlag = LocalAccessible::NativeName(aName);
275   if (!aName.IsEmpty()) return nameFlag;
276 
277   // If part of compound of XUL widget then grab a name from XUL widget element.
278   nsIContent* widgetElm = BindingOrWidgetParent();
279   if (widgetElm) XULElmName(mDoc, widgetElm, aName);
280 
281   if (!aName.IsEmpty()) return eNameOK;
282 
283   // text inputs and textareas might have useful placeholder text
284   mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::placeholder,
285                                  aName);
286   return eNameOK;
287 }
288 
Value(nsString & aValue) const289 void HTMLTextFieldAccessible::Value(nsString& aValue) const {
290   aValue.Truncate();
291   if (NativeState() & states::PROTECTED) {  // Don't return password text!
292     return;
293   }
294 
295   HTMLTextAreaElement* textArea = HTMLTextAreaElement::FromNode(mContent);
296   if (textArea) {
297     textArea->GetValue(aValue);
298     return;
299   }
300 
301   HTMLInputElement* input = HTMLInputElement::FromNode(mContent);
302   if (input) {
303     // Pass NonSystem as the caller type, to be safe.  We don't expect to have a
304     // file input here.
305     input->GetValue(aValue, CallerType::NonSystem);
306   }
307 }
308 
ApplyARIAState(uint64_t * aState) const309 void HTMLTextFieldAccessible::ApplyARIAState(uint64_t* aState) const {
310   HyperTextAccessibleWrap::ApplyARIAState(aState);
311   aria::MapToState(aria::eARIAAutoComplete, mContent->AsElement(), aState);
312 
313   // If part of compound of XUL widget then pick up ARIA stuff from XUL widget
314   // element.
315   nsIContent* widgetElm = BindingOrWidgetParent();
316   if (widgetElm) {
317     aria::MapToState(aria::eARIAAutoComplete, widgetElm->AsElement(), aState);
318   }
319 }
320 
NativeState() const321 uint64_t HTMLTextFieldAccessible::NativeState() const {
322   uint64_t state = HyperTextAccessibleWrap::NativeState();
323 
324   // Text fields are always editable, even if they are also read only or
325   // disabled.
326   state |= states::EDITABLE;
327 
328   // can be focusable, focused, protected. readonly, unavailable, selected
329   if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
330                                          nsGkAtoms::password, eIgnoreCase)) {
331     state |= states::PROTECTED;
332   }
333 
334   if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly)) {
335     state |= states::READONLY;
336   }
337 
338   // Is it an <input> or a <textarea> ?
339   HTMLInputElement* input = HTMLInputElement::FromNode(mContent);
340   state |= input && input->IsSingleLineTextControl() ? states::SINGLE_LINE
341                                                      : states::MULTI_LINE;
342 
343   if (state & (states::PROTECTED | states::MULTI_LINE | states::READONLY |
344                states::UNAVAILABLE)) {
345     return state;
346   }
347 
348   // Expose autocomplete states if this input is part of autocomplete widget.
349   LocalAccessible* widget = ContainerWidget();
350   if (widget && widget - IsAutoComplete()) {
351     state |= states::HASPOPUP | states::SUPPORTS_AUTOCOMPLETION;
352     return state;
353   }
354 
355   // Expose autocomplete state if it has associated autocomplete list.
356   if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::list_)) {
357     return state | states::SUPPORTS_AUTOCOMPLETION | states::HASPOPUP;
358   }
359 
360   // Ordinal XUL textboxes don't support autocomplete.
361   if (!BindingOrWidgetParent() &&
362       Preferences::GetBool("browser.formfill.enable")) {
363     // Check to see if autocompletion is allowed on this input. We don't expose
364     // it for password fields even though the entire password can be remembered
365     // for a page if the user asks it to be. However, the kind of autocomplete
366     // we're talking here is based on what the user types, where a popup of
367     // possible choices comes up.
368     nsAutoString autocomplete;
369     mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::autocomplete,
370                                    autocomplete);
371 
372     if (!autocomplete.LowerCaseEqualsLiteral("off")) {
373       Element* formElement = input->GetFormElement();
374       if (formElement) {
375         formElement->GetAttr(kNameSpaceID_None, nsGkAtoms::autocomplete,
376                              autocomplete);
377       }
378 
379       if (!formElement || !autocomplete.LowerCaseEqualsLiteral("off")) {
380         state |= states::SUPPORTS_AUTOCOMPLETION;
381       }
382     }
383   }
384 
385   return state;
386 }
387 
ActionCount() const388 uint8_t HTMLTextFieldAccessible::ActionCount() const { return 1; }
389 
ActionNameAt(uint8_t aIndex,nsAString & aName)390 void HTMLTextFieldAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
391   if (aIndex == eAction_Click) aName.AssignLiteral("activate");
392 }
393 
DoAction(uint8_t aIndex) const394 bool HTMLTextFieldAccessible::DoAction(uint8_t aIndex) const {
395   if (aIndex != 0) return false;
396 
397   if (FocusMgr()->IsFocused(this)) {
398     // This already has focus, so TakeFocus()will do nothing. However, the user
399     // might be activating this element because they dismissed a touch keyboard
400     // and want to bring it back.
401     DoCommand();
402   } else {
403     TakeFocus();
404   }
405   return true;
406 }
407 
GetEditor() const408 already_AddRefed<EditorBase> HTMLTextFieldAccessible::GetEditor() const {
409   RefPtr<TextControlElement> textControlElement =
410       TextControlElement::FromNodeOrNull(mContent);
411   if (!textControlElement) {
412     return nullptr;
413   }
414   RefPtr<TextEditor> textEditor = textControlElement->GetTextEditor();
415   return textEditor.forget();
416 }
417 
418 ////////////////////////////////////////////////////////////////////////////////
419 // HTMLTextFieldAccessible: Widgets
420 
IsWidget() const421 bool HTMLTextFieldAccessible::IsWidget() const { return true; }
422 
ContainerWidget() const423 LocalAccessible* HTMLTextFieldAccessible::ContainerWidget() const {
424   if (!mParent || mParent->Role() != roles::AUTOCOMPLETE) {
425     return nullptr;
426   }
427   return mParent;
428 }
429 
430 ////////////////////////////////////////////////////////////////////////////////
431 // HTMLFileInputAccessible
432 ////////////////////////////////////////////////////////////////////////////////
433 
HTMLFileInputAccessible(nsIContent * aContent,DocAccessible * aDoc)434 HTMLFileInputAccessible::HTMLFileInputAccessible(nsIContent* aContent,
435                                                  DocAccessible* aDoc)
436     : HyperTextAccessibleWrap(aContent, aDoc) {
437   mType = eHTMLFileInputType;
438 }
439 
NativeRole() const440 role HTMLFileInputAccessible::NativeRole() const {
441   // No specific role in AT APIs. We use GROUPING so that the label will be
442   // reported by screen readers when focus enters this control .
443   return roles::GROUPING;
444 }
445 
HandleAccEvent(AccEvent * aEvent)446 nsresult HTMLFileInputAccessible::HandleAccEvent(AccEvent* aEvent) {
447   nsresult rv = HyperTextAccessibleWrap::HandleAccEvent(aEvent);
448   NS_ENSURE_SUCCESS(rv, rv);
449 
450   // Redirect state change events for inherited states to child controls. Note,
451   // unavailable state is not redirected. That's a standard for unavailable
452   // state handling.
453   AccStateChangeEvent* event = downcast_accEvent(aEvent);
454   if (event && (event->GetState() == states::BUSY ||
455                 event->GetState() == states::REQUIRED ||
456                 event->GetState() == states::HASPOPUP ||
457                 event->GetState() == states::INVALID)) {
458     LocalAccessible* button = LocalChildAt(0);
459     if (button && button->Role() == roles::PUSHBUTTON) {
460       RefPtr<AccStateChangeEvent> childEvent = new AccStateChangeEvent(
461           button, event->GetState(), event->IsStateEnabled(),
462           event->FromUserInput());
463       nsEventShell::FireEvent(childEvent);
464     }
465   }
466 
467   return NS_OK;
468 }
469 
CurrentItem() const470 LocalAccessible* HTMLFileInputAccessible::CurrentItem() const {
471   // Allow aria-activedescendant to override.
472   if (LocalAccessible* item = HyperTextAccessibleWrap::CurrentItem()) {
473     return item;
474   }
475 
476   // The HTML file input itself gets DOM focus, not the button inside it.
477   // For a11y, we want the button to get focus.
478   LocalAccessible* button = LocalFirstChild();
479   if (!button) {
480     MOZ_ASSERT_UNREACHABLE("File input doesn't contain a button");
481     return nullptr;
482   }
483   MOZ_ASSERT(button->IsButton());
484   return button;
485 }
486 
487 ////////////////////////////////////////////////////////////////////////////////
488 // HTMLSpinnerAccessible
489 ////////////////////////////////////////////////////////////////////////////////
490 
NativeRole() const491 role HTMLSpinnerAccessible::NativeRole() const { return roles::SPINBUTTON; }
492 
Value(nsString & aValue) const493 void HTMLSpinnerAccessible::Value(nsString& aValue) const {
494   HTMLTextFieldAccessible::Value(aValue);
495   if (!aValue.IsEmpty()) return;
496 
497   // Pass NonSystem as the caller type, to be safe.  We don't expect to have a
498   // file input here.
499   HTMLInputElement::FromNode(mContent)->GetValue(aValue, CallerType::NonSystem);
500 }
501 
MaxValue() const502 double HTMLSpinnerAccessible::MaxValue() const {
503   double value = HTMLTextFieldAccessible::MaxValue();
504   if (!IsNaN(value)) return value;
505 
506   return HTMLInputElement::FromNode(mContent)->GetMaximum().toDouble();
507 }
508 
MinValue() const509 double HTMLSpinnerAccessible::MinValue() const {
510   double value = HTMLTextFieldAccessible::MinValue();
511   if (!IsNaN(value)) return value;
512 
513   return HTMLInputElement::FromNode(mContent)->GetMinimum().toDouble();
514 }
515 
Step() const516 double HTMLSpinnerAccessible::Step() const {
517   double value = HTMLTextFieldAccessible::Step();
518   if (!IsNaN(value)) return value;
519 
520   return HTMLInputElement::FromNode(mContent)->GetStep().toDouble();
521 }
522 
CurValue() const523 double HTMLSpinnerAccessible::CurValue() const {
524   double value = HTMLTextFieldAccessible::CurValue();
525   if (!IsNaN(value)) return value;
526 
527   return HTMLInputElement::FromNode(mContent)->GetValueAsDecimal().toDouble();
528 }
529 
SetCurValue(double aValue)530 bool HTMLSpinnerAccessible::SetCurValue(double aValue) {
531   ErrorResult er;
532   HTMLInputElement::FromNode(mContent)->SetValueAsNumber(aValue, er);
533   return !er.Failed();
534 }
535 
536 ////////////////////////////////////////////////////////////////////////////////
537 // HTMLRangeAccessible
538 ////////////////////////////////////////////////////////////////////////////////
539 
NativeRole() const540 role HTMLRangeAccessible::NativeRole() const { return roles::SLIDER; }
541 
IsWidget() const542 bool HTMLRangeAccessible::IsWidget() const { return true; }
543 
Value(nsString & aValue) const544 void HTMLRangeAccessible::Value(nsString& aValue) const {
545   LeafAccessible::Value(aValue);
546   if (!aValue.IsEmpty()) return;
547 
548   // Pass NonSystem as the caller type, to be safe.  We don't expect to have a
549   // file input here.
550   HTMLInputElement::FromNode(mContent)->GetValue(aValue, CallerType::NonSystem);
551 }
552 
MaxValue() const553 double HTMLRangeAccessible::MaxValue() const {
554   double value = LeafAccessible::MaxValue();
555   if (!IsNaN(value)) return value;
556 
557   return HTMLInputElement::FromNode(mContent)->GetMaximum().toDouble();
558 }
559 
MinValue() const560 double HTMLRangeAccessible::MinValue() const {
561   double value = LeafAccessible::MinValue();
562   if (!IsNaN(value)) return value;
563 
564   return HTMLInputElement::FromNode(mContent)->GetMinimum().toDouble();
565 }
566 
Step() const567 double HTMLRangeAccessible::Step() const {
568   double value = LeafAccessible::Step();
569   if (!IsNaN(value)) return value;
570 
571   return HTMLInputElement::FromNode(mContent)->GetStep().toDouble();
572 }
573 
CurValue() const574 double HTMLRangeAccessible::CurValue() const {
575   double value = LeafAccessible::CurValue();
576   if (!IsNaN(value)) return value;
577 
578   return HTMLInputElement::FromNode(mContent)->GetValueAsDecimal().toDouble();
579 }
580 
SetCurValue(double aValue)581 bool HTMLRangeAccessible::SetCurValue(double aValue) {
582   ErrorResult er;
583   HTMLInputElement::FromNode(mContent)->SetValueAsNumber(aValue, er);
584   return !er.Failed();
585 }
586 
587 ////////////////////////////////////////////////////////////////////////////////
588 // HTMLGroupboxAccessible
589 ////////////////////////////////////////////////////////////////////////////////
590 
HTMLGroupboxAccessible(nsIContent * aContent,DocAccessible * aDoc)591 HTMLGroupboxAccessible::HTMLGroupboxAccessible(nsIContent* aContent,
592                                                DocAccessible* aDoc)
593     : HyperTextAccessibleWrap(aContent, aDoc) {}
594 
NativeRole() const595 role HTMLGroupboxAccessible::NativeRole() const { return roles::GROUPING; }
596 
GetLegend() const597 nsIContent* HTMLGroupboxAccessible::GetLegend() const {
598   for (nsIContent* legendContent = mContent->GetFirstChild(); legendContent;
599        legendContent = legendContent->GetNextSibling()) {
600     if (legendContent->NodeInfo()->Equals(nsGkAtoms::legend,
601                                           mContent->GetNameSpaceID())) {
602       // Either XHTML namespace or no namespace
603       return legendContent;
604     }
605   }
606 
607   return nullptr;
608 }
609 
NativeName(nsString & aName) const610 ENameValueFlag HTMLGroupboxAccessible::NativeName(nsString& aName) const {
611   ENameValueFlag nameFlag = LocalAccessible::NativeName(aName);
612   if (!aName.IsEmpty()) return nameFlag;
613 
614   nsIContent* legendContent = GetLegend();
615   if (legendContent) {
616     nsTextEquivUtils::AppendTextEquivFromContent(this, legendContent, &aName);
617   }
618 
619   aName.CompressWhitespace();
620   return eNameOK;
621 }
622 
RelationByType(RelationType aType) const623 Relation HTMLGroupboxAccessible::RelationByType(RelationType aType) const {
624   Relation rel = HyperTextAccessibleWrap::RelationByType(aType);
625   // No override for label, so use <legend> for this <fieldset>
626   if (aType == RelationType::LABELLED_BY) rel.AppendTarget(mDoc, GetLegend());
627 
628   return rel;
629 }
630 
631 ////////////////////////////////////////////////////////////////////////////////
632 // HTMLLegendAccessible
633 ////////////////////////////////////////////////////////////////////////////////
634 
HTMLLegendAccessible(nsIContent * aContent,DocAccessible * aDoc)635 HTMLLegendAccessible::HTMLLegendAccessible(nsIContent* aContent,
636                                            DocAccessible* aDoc)
637     : HyperTextAccessibleWrap(aContent, aDoc) {}
638 
RelationByType(RelationType aType) const639 Relation HTMLLegendAccessible::RelationByType(RelationType aType) const {
640   Relation rel = HyperTextAccessibleWrap::RelationByType(aType);
641   if (aType != RelationType::LABEL_FOR) return rel;
642 
643   LocalAccessible* groupbox = LocalParent();
644   if (groupbox && groupbox->Role() == roles::GROUPING) {
645     rel.AppendTarget(groupbox);
646   }
647 
648   return rel;
649 }
650 
651 ////////////////////////////////////////////////////////////////////////////////
652 // HTMLFigureAccessible
653 ////////////////////////////////////////////////////////////////////////////////
654 
HTMLFigureAccessible(nsIContent * aContent,DocAccessible * aDoc)655 HTMLFigureAccessible::HTMLFigureAccessible(nsIContent* aContent,
656                                            DocAccessible* aDoc)
657     : HyperTextAccessibleWrap(aContent, aDoc) {}
658 
NativeName(nsString & aName) const659 ENameValueFlag HTMLFigureAccessible::NativeName(nsString& aName) const {
660   ENameValueFlag nameFlag = HyperTextAccessibleWrap::NativeName(aName);
661   if (!aName.IsEmpty()) return nameFlag;
662 
663   nsIContent* captionContent = Caption();
664   if (captionContent) {
665     nsTextEquivUtils::AppendTextEquivFromContent(this, captionContent, &aName);
666   }
667 
668   aName.CompressWhitespace();
669   return eNameOK;
670 }
671 
RelationByType(RelationType aType) const672 Relation HTMLFigureAccessible::RelationByType(RelationType aType) const {
673   Relation rel = HyperTextAccessibleWrap::RelationByType(aType);
674   if (aType == RelationType::LABELLED_BY) rel.AppendTarget(mDoc, Caption());
675 
676   return rel;
677 }
678 
Caption() const679 nsIContent* HTMLFigureAccessible::Caption() const {
680   for (nsIContent* childContent = mContent->GetFirstChild(); childContent;
681        childContent = childContent->GetNextSibling()) {
682     if (childContent->NodeInfo()->Equals(nsGkAtoms::figcaption,
683                                          mContent->GetNameSpaceID())) {
684       return childContent;
685     }
686   }
687 
688   return nullptr;
689 }
690 
691 ////////////////////////////////////////////////////////////////////////////////
692 // HTMLFigcaptionAccessible
693 ////////////////////////////////////////////////////////////////////////////////
694 
HTMLFigcaptionAccessible(nsIContent * aContent,DocAccessible * aDoc)695 HTMLFigcaptionAccessible::HTMLFigcaptionAccessible(nsIContent* aContent,
696                                                    DocAccessible* aDoc)
697     : HyperTextAccessibleWrap(aContent, aDoc) {}
698 
RelationByType(RelationType aType) const699 Relation HTMLFigcaptionAccessible::RelationByType(RelationType aType) const {
700   Relation rel = HyperTextAccessibleWrap::RelationByType(aType);
701   if (aType != RelationType::LABEL_FOR) return rel;
702 
703   LocalAccessible* figure = LocalParent();
704   if (figure && figure->GetContent()->NodeInfo()->Equals(
705                     nsGkAtoms::figure, mContent->GetNameSpaceID())) {
706     rel.AppendTarget(figure);
707   }
708 
709   return rel;
710 }
711 
712 ////////////////////////////////////////////////////////////////////////////////
713 // HTMLProgressAccessible
714 ////////////////////////////////////////////////////////////////////////////////
715 
NativeRole() const716 role HTMLProgressAccessible::NativeRole() const { return roles::PROGRESSBAR; }
717 
NativeState() const718 uint64_t HTMLProgressAccessible::NativeState() const {
719   uint64_t state = LeafAccessible::NativeState();
720 
721   // An undetermined progressbar (i.e. without a value) has a mixed state.
722   nsAutoString attrValue;
723   mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value,
724                                  attrValue);
725   if (attrValue.IsEmpty()) {
726     state |= states::MIXED;
727   }
728 
729   return state;
730 }
731 
IsWidget() const732 bool HTMLProgressAccessible::IsWidget() const { return true; }
733 
Value(nsString & aValue) const734 void HTMLProgressAccessible::Value(nsString& aValue) const {
735   LeafAccessible::Value(aValue);
736   if (!aValue.IsEmpty()) {
737     return;
738   }
739 
740   double maxValue = MaxValue();
741   if (IsNaN(maxValue) || maxValue == 0) {
742     return;
743   }
744 
745   double curValue = CurValue();
746   if (IsNaN(curValue)) {
747     return;
748   }
749 
750   // Treat the current value bigger than maximum as 100%.
751   double percentValue =
752       (curValue < maxValue) ? (curValue / maxValue) * 100 : 100;
753 
754   aValue.AppendFloat(percentValue);
755   aValue.Append('%');
756 }
757 
MaxValue() const758 double HTMLProgressAccessible::MaxValue() const {
759   double value = LeafAccessible::MaxValue();
760   if (!IsNaN(value)) {
761     return value;
762   }
763 
764   nsAutoString strValue;
765   if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::max,
766                                      strValue)) {
767     nsresult result = NS_OK;
768     value = strValue.ToDouble(&result);
769     if (NS_SUCCEEDED(result)) {
770       return value;
771     }
772   }
773 
774   return 1;
775 }
776 
MinValue() const777 double HTMLProgressAccessible::MinValue() const {
778   double value = LeafAccessible::MinValue();
779   return IsNaN(value) ? 0 : value;
780 }
781 
Step() const782 double HTMLProgressAccessible::Step() const {
783   double value = LeafAccessible::Step();
784   return IsNaN(value) ? 0 : value;
785 }
786 
CurValue() const787 double HTMLProgressAccessible::CurValue() const {
788   double value = LeafAccessible::CurValue();
789   if (!IsNaN(value)) {
790     return value;
791   }
792 
793   nsAutoString attrValue;
794   if (!mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value,
795                                       attrValue)) {
796     return UnspecifiedNaN<double>();
797   }
798 
799   nsresult error = NS_OK;
800   value = attrValue.ToDouble(&error);
801   return NS_FAILED(error) ? UnspecifiedNaN<double>() : value;
802 }
803 
SetCurValue(double aValue)804 bool HTMLProgressAccessible::SetCurValue(double aValue) {
805   return false;  // progress meters are readonly.
806 }
807 
808 ////////////////////////////////////////////////////////////////////////////////
809 // HTMLMeterAccessible
810 ////////////////////////////////////////////////////////////////////////////////
811 
NativeRole() const812 role HTMLMeterAccessible::NativeRole() const { return roles::METER; }
813 
IsWidget() const814 bool HTMLMeterAccessible::IsWidget() const { return true; }
815 
Value(nsString & aValue) const816 void HTMLMeterAccessible::Value(nsString& aValue) const {
817   LeafAccessible::Value(aValue);
818   if (!aValue.IsEmpty()) {
819     return;
820   }
821 
822   // If we did not get a value from the above LeafAccessible call,
823   // we should check to see if the meter has inner text.
824   // If it does, we'll use that as our value.
825   nsTextEquivUtils::AppendFromDOMChildren(mContent, &aValue);
826   aValue.CompressWhitespace();
827   if (!aValue.IsEmpty()) {
828     return;
829   }
830 
831   // If no inner text is found, use curValue
832   double curValue = CurValue();
833   if (IsNaN(curValue)) {
834     return;
835   }
836 
837   aValue.AppendFloat(curValue);
838 }
839 
MaxValue() const840 double HTMLMeterAccessible::MaxValue() const {
841   double max = LeafAccessible::MaxValue();
842   double min = MinValue();
843 
844   if (!IsNaN(max)) {
845     return max > min ? max : min;
846   }
847 
848   // If we didn't find a max value, check for the max attribute
849   nsAutoString strValue;
850   if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::max,
851                                      strValue)) {
852     nsresult result = NS_OK;
853     max = strValue.ToDouble(&result);
854     if (NS_SUCCEEDED(result)) {
855       return max > min ? max : min;
856     }
857   }
858 
859   return 1 > min ? 1 : min;
860 }
861 
MinValue() const862 double HTMLMeterAccessible::MinValue() const {
863   double min = LeafAccessible::MinValue();
864   if (!IsNaN(min)) {
865     return min;
866   }
867 
868   nsAutoString strValue;
869   if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::min,
870                                      strValue)) {
871     nsresult result = NS_OK;
872     min = strValue.ToDouble(&result);
873     if (NS_SUCCEEDED(result)) {
874       return min;
875     }
876   }
877 
878   return 0;
879 }
880 
CurValue() const881 double HTMLMeterAccessible::CurValue() const {
882   double value = LeafAccessible::CurValue();
883   double minValue = MinValue();
884 
885   if (IsNaN(value)) {
886     /* If we didn't find a value from the LeafAccessible call above, check
887      * for a value attribute */
888     nsAutoString attrValue;
889     if (!mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value,
890                                         attrValue)) {
891       return minValue;
892     }
893 
894     // If we find a value attribute, attempt to convert it to a double
895     nsresult error = NS_OK;
896     value = attrValue.ToDouble(&error);
897     if (NS_FAILED(error)) {
898       return minValue;
899     }
900   }
901 
902   /* If we end up with a defined value, verify it falls between
903    * our established min/max. Otherwise, snap it to the nearest boundary. */
904   double maxValue = MaxValue();
905   if (value > maxValue) {
906     value = maxValue;
907   } else if (value < minValue) {
908     value = minValue;
909   }
910 
911   return value;
912 }
913 
SetCurValue(double aValue)914 bool HTMLMeterAccessible::SetCurValue(double aValue) {
915   return false;  // meters are readonly.
916 }
917