Bug 1325160 - implement AOM attributes, r=smaug
authorAlexander Surkov <surkov.alexander@gmail.com>
Fri, 23 Dec 2016 14:36:55 -0500
changeset 453581 a0c9f0827fd67d2f7fe701aed1f511886202a161
parent 453580 ec3d424e593ebce4b99c1857cbf74350245c67b6
child 453582 d2c65588f41f5e6c370a6bbb5b727006599ee8c8
push id39711
push userdmitchell@mozilla.com
push dateFri, 23 Dec 2016 21:59:47 +0000
reviewerssmaug
bugs1325160
milestone53.0a1
Bug 1325160 - implement AOM attributes, r=smaug
accessible/aom/AccessibleNode.cpp
accessible/aom/AccessibleNode.h
accessible/tests/mochitest/aom/test_general.html
dom/webidl/AccessibleNode.webidl
--- a/accessible/aom/AccessibleNode.cpp
+++ b/accessible/aom/AccessibleNode.cpp
@@ -3,16 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AccessibleNode.h"
 #include "mozilla/dom/AccessibleNodeBinding.h"
 #include "mozilla/dom/BindingDeclarations.h"
 #include "mozilla/dom/DOMStringList.h"
 #include "nsIPersistentProperties2.h"
+#include "nsISimpleEnumerator.h"
 
 #include "Accessible-inl.h"
 #include "nsAccessibilityService.h"
 #include "DocAccessible.h"
 
 using namespace mozilla;
 using namespace mozilla::a11y;
 using namespace mozilla::dom;
@@ -72,16 +73,41 @@ AccessibleNode::GetStates(nsTArray<nsStr
     }
     aStates = mStates->StringArray();
     return;
   }
 
   aStates.AppendElement(NS_LITERAL_STRING("defunct"));
 }
 
+void
+AccessibleNode::GetAttributes(nsTArray<nsString>& aAttributes)
+{
+  if (!mIntl) {
+    return;
+  }
+
+  nsCOMPtr<nsIPersistentProperties> attrs = mIntl->Attributes();
+
+  nsCOMPtr<nsISimpleEnumerator> props;
+  attrs->Enumerate(getter_AddRefs(props));
+
+  bool hasMore = false;
+  while (NS_SUCCEEDED(props->HasMoreElements(&hasMore)) && hasMore) {
+    nsCOMPtr<nsISupports> supp;
+    props->GetNext(getter_AddRefs(supp));
+
+    nsCOMPtr<nsIPropertyElement> prop(do_QueryInterface(supp));
+
+    nsAutoCString attr;
+    prop->GetKey(attr);
+    aAttributes.AppendElement(NS_ConvertUTF8toUTF16(attr));
+  }
+}
+
 bool
 AccessibleNode::Is(const Sequence<nsString>& aFlavors)
 {
   if (!mIntl) {
     for (const auto& flavor : aFlavors) {
       if (!flavor.EqualsLiteral("unknown") && !flavor.EqualsLiteral("defunct")) {
         return false;
       }
--- a/accessible/aom/AccessibleNode.h
+++ b/accessible/aom/AccessibleNode.h
@@ -33,16 +33,17 @@ public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS;
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AccessibleNode);
 
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override final;
   virtual dom::ParentObject GetParentObject() const final;
 
   void GetRole(nsAString& aRole);
   void GetStates(nsTArray<nsString>& aStates);
+  void GetAttributes(nsTArray<nsString>& aAttributes);
   nsINode* GetDOMNode();
 
   bool Is(const Sequence<nsString>& aFlavors);
   bool Has(const Sequence<nsString>& aAttributes);
   void Get(JSContext* cx, const nsAString& aAttribute,
            JS::MutableHandle<JS::Value> aValue,
            ErrorResult& aRv);
 
--- a/accessible/tests/mochitest/aom/test_general.html
+++ b/accessible/tests/mochitest/aom/test_general.html
@@ -81,12 +81,27 @@
        'correct role and state on an accessible node');
 
     is(anode.get('explicit-name'), 'true',
        'correct object attribute value on an accessible node');
 
     ok(anode.has('explicit-name'),
        'object attributes are present');
 
+    var attrs = [ 'explicit-name' ];
+    if (anode.attributes.length > 1) {
+      attrs = [
+        'margin-left', 'text-align', 'text-indent', 'margin-right',
+        'tag', 'margin-top', 'margin-bottom', 'display',
+        'explicit-name'
+      ];
+    }
+
+    is(anode.attributes.length, attrs.length, 'correct number of attributes');
+    for (var i = 0; i < attrs.length; i++) {
+      is(anode.attributes[i], attrs[i],
+         `${attrs[i]} attribute is expected at ${i}th index`);
+    }
+
     finish();
   }
   </script>
 </head>
--- a/dom/webidl/AccessibleNode.webidl
+++ b/dom/webidl/AccessibleNode.webidl
@@ -4,15 +4,17 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
 [Pref="accessibility.AOM.enabled"]
 interface AccessibleNode {
   readonly attribute DOMString role;
   [Frozen, Cached, Pure]
   readonly attribute sequence<DOMString> states;
+  [Frozen, Cached, Pure]
+  readonly attribute sequence<DOMString> attributes;
   readonly attribute Node? DOMNode;
 
   boolean is(DOMString... states);
   boolean has(DOMString... attributes);
   [Throws]
   any get(DOMString attribute);
 };