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
)
39 NS_IMPL_CYCLE_COLLECTING_ADDREF(AccessibleNode
)
40 NS_IMPL_CYCLE_COLLECTING_RELEASE(AccessibleNode
)
42 AccessibleNode::AccessibleNode(nsINode
* aNode
)
43 : mDoubleProperties(3),
46 mBooleanProperties(0),
47 mRelationProperties(3),
48 mStringProperties(16),
50 nsAccessibilityService
* accService
= GetOrCreateAccService();
55 DocAccessible
* doc
= accService
->GetDocAccessible(mDOMNode
->OwnerDoc());
57 mIntl
= doc
->GetAccessible(mDOMNode
);
61 AccessibleNode::~AccessibleNode() {}
64 JSObject
* AccessibleNode::WrapObject(JSContext
* aCx
,
65 JS::Handle
<JSObject
*> aGivenProto
) {
66 return AccessibleNode_Binding::Wrap(aCx
, this, aGivenProto
);
70 ParentObject
AccessibleNode::GetParentObject() const {
71 return mDOMNode
->GetParentObject();
74 void AccessibleNode::GetComputedRole(nsAString
& aRole
) {
76 nsAccessibilityService
* accService
= GetOrCreateAccService();
78 accService
->GetStringRole(mIntl
->Role(), aRole
);
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
);
94 aStates
= mStates
->StringArray().Clone();
98 mStates
= accService
->GetStringStates(mIntl
->State());
99 aStates
= mStates
->StringArray().Clone();
102 void AccessibleNode::GetAttributes(nsTArray
<nsString
>& aAttributes
) {
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")) {
127 accService
->GetStringRole(mIntl
->Role(), role
);
130 mStates
= accService
->GetStringStates(mIntl
->State());
133 for (const auto& flavor
: aFlavors
) {
134 if (!flavor
.Equals(role
) && !mStates
->Contains(flavor
)) {
141 bool AccessibleNode::Has(const Sequence
<nsString
>& aAttributes
) {
145 RefPtr
<AccAttributes
> attrs
= mIntl
->Attributes();
146 for (const auto& attr
: aAttributes
) {
147 RefPtr
<nsAtom
> attrAtom
= NS_Atomize(attr
);
148 if (!attrs
->HasAttribute(attrAtom
)) {
155 void AccessibleNode::Get(JSContext
* aCX
, const nsAString
& aAttribute
,
156 JS::MutableHandle
<JS::Value
> aValue
,
159 aRv
.ThrowInvalidStateError("No attributes available");
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
);
173 nsINode
* AccessibleNode::GetDOMNode() { return mDOMNode
; }