summaryrefslogtreecommitdiffstats
path: root/accessible/generic/BaseAccessibles.cpp
blob: 8f1f6286a447c7ac8997bd2c88f4f519b77210b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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 "BaseAccessibles.h"

#include "Accessible-inl.h"
#include "HyperTextAccessibleWrap.h"
#include "nsAccessibilityService.h"
#include "nsAccUtils.h"
#include "nsCoreUtils.h"
#include "Role.h"
#include "States.h"
#include "nsIURI.h"

using namespace mozilla::a11y;

////////////////////////////////////////////////////////////////////////////////
// LeafAccessible
////////////////////////////////////////////////////////////////////////////////

LeafAccessible::LeafAccessible(nsIContent* aContent, DocAccessible* aDoc)
    : AccessibleWrap(aContent, aDoc) {
  mStateFlags |= eNoKidsFromDOM;
}

////////////////////////////////////////////////////////////////////////////////
// LeafAccessible: Accessible public

Accessible* LeafAccessible::ChildAtPoint(int32_t aX, int32_t aY,
                                         EWhichChildAtPoint aWhichChild) {
  // Don't walk into leaf accessibles.
  return this;
}

bool LeafAccessible::InsertChildAt(uint32_t aIndex, Accessible* aChild) {
  MOZ_ASSERT_UNREACHABLE("InsertChildAt called on leaf accessible!");
  return false;
}

bool LeafAccessible::RemoveChild(Accessible* aChild) {
  MOZ_ASSERT_UNREACHABLE("RemoveChild called on leaf accessible!");
  return false;
}

bool LeafAccessible::IsAcceptableChild(nsIContent* aEl) const {
  // No children for leaf accessible.
  return false;
}

////////////////////////////////////////////////////////////////////////////////
// LinkableAccessible
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// LinkableAccessible. nsIAccessible

void LinkableAccessible::TakeFocus() const {
  if (const Accessible* actionAcc = ActionWalk()) {
    actionAcc->TakeFocus();
  } else {
    AccessibleWrap::TakeFocus();
  }
}

uint64_t LinkableAccessible::NativeLinkState() const {
  bool isLink;
  const Accessible* actionAcc = ActionWalk(&isLink);
  if (isLink) {
    return states::LINKED | (actionAcc->LinkState() & states::TRAVERSED);
  }

  return 0;
}

void LinkableAccessible::Value(nsString& aValue) const {
  aValue.Truncate();

  Accessible::Value(aValue);
  if (!aValue.IsEmpty()) {
    return;
  }

  bool isLink;
  const Accessible* actionAcc = ActionWalk(&isLink);
  if (isLink) {
    actionAcc->Value(aValue);
  }
}

uint8_t LinkableAccessible::ActionCount() const {
  bool isLink, isOnclick, isLabelWithControl;
  ActionWalk(&isLink, &isOnclick, &isLabelWithControl);
  return (isLink || isOnclick || isLabelWithControl) ? 1 : 0;
}

const Accessible* LinkableAccessible::ActionWalk(
    bool* aIsLink, bool* aIsOnclick, bool* aIsLabelWithControl) const {
  if (aIsOnclick) {
    *aIsOnclick = false;
  }
  if (aIsLink) {
    *aIsLink = false;
  }
  if (aIsLabelWithControl) {
    *aIsLabelWithControl = false;
  }

  if (nsCoreUtils::HasClickListener(mContent)) {
    if (aIsOnclick) {
      *aIsOnclick = true;
    }
    return nullptr;
  }

  // XXX: The logic looks broken since the click listener may be registered
  // on non accessible node in parent chain but this node is skipped when tree
  // is traversed.
  const Accessible* walkUpAcc = this;
  while ((walkUpAcc = walkUpAcc->Parent()) && !walkUpAcc->IsDoc()) {
    if (walkUpAcc->LinkState() & states::LINKED) {
      if (aIsLink) {
        *aIsLink = true;
      }
      return walkUpAcc;
    }

    if (nsCoreUtils::HasClickListener(walkUpAcc->GetContent())) {
      if (aIsOnclick) {
        *aIsOnclick = true;
      }
      return walkUpAcc;
    }

    if (nsCoreUtils::IsLabelWithControl(walkUpAcc->GetContent())) {
      if (aIsLabelWithControl) {
        *aIsLabelWithControl = true;
      }
      return walkUpAcc;
    }
  }
  return nullptr;
}

void LinkableAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) {
  aName.Truncate();

  // Action 0 (default action): Jump to link
  if (aIndex == eAction_Jump) {
    bool isOnclick, isLink, isLabelWithControl;
    ActionWalk(&isLink, &isOnclick, &isLabelWithControl);
    if (isLink) {
      aName.AssignLiteral("jump");
    } else if (isOnclick || isLabelWithControl) {
      aName.AssignLiteral("click");
    }
  }
}

bool LinkableAccessible::DoAction(uint8_t aIndex) const {
  if (aIndex != eAction_Jump) {
    return false;
  }

  if (const Accessible* actionAcc = ActionWalk()) {
    return actionAcc->DoAction(aIndex);
  }

  return AccessibleWrap::DoAction(aIndex);
}

KeyBinding LinkableAccessible::AccessKey() const {
  if (const Accessible* actionAcc =
          const_cast<LinkableAccessible*>(this)->ActionWalk()) {
    return actionAcc->AccessKey();
  }

  return Accessible::AccessKey();
}

////////////////////////////////////////////////////////////////////////////////
// LinkableAccessible: HyperLinkAccessible

already_AddRefed<nsIURI> LinkableAccessible::AnchorURIAt(
    uint32_t aAnchorIndex) const {
  bool isLink;
  const Accessible* actionAcc = ActionWalk(&isLink);
  if (isLink) {
    NS_ASSERTION(actionAcc->IsLink(), "HyperLink isn't implemented.");

    if (actionAcc->IsLink()) {
      return actionAcc->AnchorURIAt(aAnchorIndex);
    }
  }

  return nullptr;
}

////////////////////////////////////////////////////////////////////////////////
// DummyAccessible
////////////////////////////////////////////////////////////////////////////////

uint64_t DummyAccessible::NativeState() const { return 0; }
uint64_t DummyAccessible::NativeInteractiveState() const { return 0; }

uint64_t DummyAccessible::NativeLinkState() const { return 0; }

bool DummyAccessible::NativelyUnavailable() const { return false; }

void DummyAccessible::ApplyARIAState(uint64_t* aState) const {}