Bug 1753131 - wait for focus before dispatching devicechange events r=jib
[gecko.git] / accessible / aom / AccessibleNode.cpp
blob2ecc58b5677fe4cadd21e16ebf7967ccb0722a65
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/. */
6 #include "AccessibleNode.h"
7 #include "mozilla/dom/AccessibleNodeBinding.h"
8 #include "mozilla/dom/BindingDeclarations.h"
9 #include "mozilla/dom/DOMStringList.h"
10 #include "mozilla/StaticPrefs_accessibility.h"
11 #include "nsContentUtils.h"
12 #include "nsISimpleEnumerator.h"
14 #include "AccAttributes.h"
15 #include "LocalAccessible-inl.h"
16 #include "nsAccessibilityService.h"
17 #include "DocAccessible.h"
19 #include "mozilla/dom/Document.h" // for inline nsINode::GetParentObject
20 #include "mozilla/dom/ToJSValue.h"
22 using namespace mozilla;
23 using namespace mozilla::a11y;
24 using namespace mozilla::dom;
26 bool AccessibleNode::IsAOMEnabled(JSContext* aCx, JSObject* /*unused*/) {
27 return nsContentUtils::IsSystemCaller(aCx) ||
28 StaticPrefs::accessibility_AOM_enabled();
31 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AccessibleNode, mRelationProperties,
32 mIntl, mDOMNode, mStates)
34 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AccessibleNode)
35 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
36 NS_INTERFACE_MAP_ENTRY(nsISupports)
37 NS_INTERFACE_MAP_END
39 NS_IMPL_CYCLE_COLLECTING_ADDREF(AccessibleNode)
40 NS_IMPL_CYCLE_COLLECTING_RELEASE(AccessibleNode)
42 AccessibleNode::AccessibleNode(nsINode* aNode)
43 : mDoubleProperties(3),
44 mIntProperties(3),
45 mUIntProperties(6),
46 mBooleanProperties(0),
47 mRelationProperties(3),
48 mStringProperties(16),
49 mDOMNode(aNode) {
50 nsAccessibilityService* accService = GetOrCreateAccService();
51 if (!accService) {
52 return;
55 DocAccessible* doc = accService->GetDocAccessible(mDOMNode->OwnerDoc());
56 if (doc) {
57 mIntl = doc->GetAccessible(mDOMNode);
61 AccessibleNode::~AccessibleNode() {}
63 /* virtual */
64 JSObject* AccessibleNode::WrapObject(JSContext* aCx,
65 JS::Handle<JSObject*> aGivenProto) {
66 return AccessibleNode_Binding::Wrap(aCx, this, aGivenProto);
69 /* virtual */
70 ParentObject AccessibleNode::GetParentObject() const {
71 return mDOMNode->GetParentObject();
74 void AccessibleNode::GetComputedRole(nsAString& aRole) {
75 if (mIntl) {
76 nsAccessibilityService* accService = GetOrCreateAccService();
77 if (accService) {
78 accService->GetStringRole(mIntl->Role(), aRole);
79 return;
83 aRole.AssignLiteral("unknown");
86 void AccessibleNode::GetStates(nsTArray<nsString>& aStates) {
87 nsAccessibilityService* accService = GetOrCreateAccService();
88 if (!mIntl || !accService) {
89 aStates.AppendElement(u"defunct"_ns);
90 return;
93 if (mStates) {
94 aStates = mStates->StringArray().Clone();
95 return;
98 mStates = accService->GetStringStates(mIntl->State());
99 aStates = mStates->StringArray().Clone();
102 void AccessibleNode::GetAttributes(nsTArray<nsString>& aAttributes) {
103 if (!mIntl) {
104 return;
107 RefPtr<AccAttributes> attrs = mIntl->Attributes();
109 for (auto iter : *attrs) {
110 aAttributes.AppendElement(nsAtomString(iter.Name()));
114 bool AccessibleNode::Is(const Sequence<nsString>& aFlavors) {
115 nsAccessibilityService* accService = GetOrCreateAccService();
116 if (!mIntl || !accService) {
117 for (const auto& flavor : aFlavors) {
118 if (!flavor.EqualsLiteral("unknown") &&
119 !flavor.EqualsLiteral("defunct")) {
120 return false;
123 return true;
126 nsAutoString role;
127 accService->GetStringRole(mIntl->Role(), role);
129 if (!mStates) {
130 mStates = accService->GetStringStates(mIntl->State());
133 for (const auto& flavor : aFlavors) {
134 if (!flavor.Equals(role) && !mStates->Contains(flavor)) {
135 return false;
138 return true;
141 bool AccessibleNode::Has(const Sequence<nsString>& aAttributes) {
142 if (!mIntl) {
143 return false;
145 RefPtr<AccAttributes> attrs = mIntl->Attributes();
146 for (const auto& attr : aAttributes) {
147 RefPtr<nsAtom> attrAtom = NS_Atomize(attr);
148 if (!attrs->HasAttribute(attrAtom)) {
149 return false;
152 return true;
155 void AccessibleNode::Get(JSContext* aCX, const nsAString& aAttribute,
156 JS::MutableHandle<JS::Value> aValue,
157 ErrorResult& aRv) {
158 if (!mIntl) {
159 aRv.ThrowInvalidStateError("No attributes available");
160 return;
163 RefPtr<nsAtom> attrAtom = NS_Atomize(aAttribute);
164 RefPtr<AccAttributes> attrs = mIntl->Attributes();
165 nsAutoString valueStr;
166 attrs->GetAttribute(attrAtom, valueStr);
167 if (!ToJSValue(aCX, valueStr, aValue)) {
168 aRv.NoteJSContextException(aCX);
169 return;
173 nsINode* AccessibleNode::GetDOMNode() { return mDOMNode; }