Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "AccGroupInfo.h" |
michael@0 | 6 | #include "nsAccUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "Role.h" |
michael@0 | 9 | #include "States.h" |
michael@0 | 10 | |
michael@0 | 11 | using namespace mozilla::a11y; |
michael@0 | 12 | |
michael@0 | 13 | AccGroupInfo::AccGroupInfo(Accessible* aItem, role aRole) : |
michael@0 | 14 | mPosInSet(0), mSetSize(0), mParent(nullptr), mItem(aItem), mRole(aRole) |
michael@0 | 15 | { |
michael@0 | 16 | MOZ_COUNT_CTOR(AccGroupInfo); |
michael@0 | 17 | Update(); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | void |
michael@0 | 21 | AccGroupInfo::Update() |
michael@0 | 22 | { |
michael@0 | 23 | Accessible* parent = mItem->Parent(); |
michael@0 | 24 | if (!parent) |
michael@0 | 25 | return; |
michael@0 | 26 | |
michael@0 | 27 | int32_t indexInParent = mItem->IndexInParent(); |
michael@0 | 28 | uint32_t siblingCount = parent->ChildCount(); |
michael@0 | 29 | if (indexInParent == -1 || |
michael@0 | 30 | indexInParent >= static_cast<int32_t>(siblingCount)) { |
michael@0 | 31 | NS_ERROR("Wrong index in parent! Tree invalidation problem."); |
michael@0 | 32 | return; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | int32_t level = nsAccUtils::GetARIAOrDefaultLevel(mItem); |
michael@0 | 36 | |
michael@0 | 37 | // Compute position in set. |
michael@0 | 38 | mPosInSet = 1; |
michael@0 | 39 | for (int32_t idx = indexInParent - 1; idx >= 0 ; idx--) { |
michael@0 | 40 | Accessible* sibling = parent->GetChildAt(idx); |
michael@0 | 41 | roles::Role siblingRole = sibling->Role(); |
michael@0 | 42 | |
michael@0 | 43 | // If the sibling is separator then the group is ended. |
michael@0 | 44 | if (siblingRole == roles::SEPARATOR) |
michael@0 | 45 | break; |
michael@0 | 46 | |
michael@0 | 47 | // If sibling is not visible and hasn't the same base role. |
michael@0 | 48 | if (BaseRole(siblingRole) != mRole || sibling->State() & states::INVISIBLE) |
michael@0 | 49 | continue; |
michael@0 | 50 | |
michael@0 | 51 | // Check if it's hierarchical flatten structure, i.e. if the sibling |
michael@0 | 52 | // level is lesser than this one then group is ended, if the sibling level |
michael@0 | 53 | // is greater than this one then the group is split by some child elements |
michael@0 | 54 | // (group will be continued). |
michael@0 | 55 | int32_t siblingLevel = nsAccUtils::GetARIAOrDefaultLevel(sibling); |
michael@0 | 56 | if (siblingLevel < level) { |
michael@0 | 57 | mParent = sibling; |
michael@0 | 58 | break; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // Skip subset. |
michael@0 | 62 | if (siblingLevel > level) |
michael@0 | 63 | continue; |
michael@0 | 64 | |
michael@0 | 65 | // If the previous item in the group has calculated group information then |
michael@0 | 66 | // build group information for this item based on found one. |
michael@0 | 67 | if (sibling->mGroupInfo) { |
michael@0 | 68 | mPosInSet += sibling->mGroupInfo->mPosInSet; |
michael@0 | 69 | mParent = sibling->mGroupInfo->mParent; |
michael@0 | 70 | mSetSize = sibling->mGroupInfo->mSetSize; |
michael@0 | 71 | return; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | mPosInSet++; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // Compute set size. |
michael@0 | 78 | mSetSize = mPosInSet; |
michael@0 | 79 | |
michael@0 | 80 | for (uint32_t idx = indexInParent + 1; idx < siblingCount; idx++) { |
michael@0 | 81 | Accessible* sibling = parent->GetChildAt(idx); |
michael@0 | 82 | |
michael@0 | 83 | roles::Role siblingRole = sibling->Role(); |
michael@0 | 84 | |
michael@0 | 85 | // If the sibling is separator then the group is ended. |
michael@0 | 86 | if (siblingRole == roles::SEPARATOR) |
michael@0 | 87 | break; |
michael@0 | 88 | |
michael@0 | 89 | // If sibling is visible and has the same base role |
michael@0 | 90 | if (BaseRole(siblingRole) != mRole || sibling->State() & states::INVISIBLE) |
michael@0 | 91 | continue; |
michael@0 | 92 | |
michael@0 | 93 | // and check if it's hierarchical flatten structure. |
michael@0 | 94 | int32_t siblingLevel = nsAccUtils::GetARIAOrDefaultLevel(sibling); |
michael@0 | 95 | if (siblingLevel < level) |
michael@0 | 96 | break; |
michael@0 | 97 | |
michael@0 | 98 | // Skip subset. |
michael@0 | 99 | if (siblingLevel > level) |
michael@0 | 100 | continue; |
michael@0 | 101 | |
michael@0 | 102 | // If the next item in the group has calculated group information then |
michael@0 | 103 | // build group information for this item based on found one. |
michael@0 | 104 | if (sibling->mGroupInfo) { |
michael@0 | 105 | mParent = sibling->mGroupInfo->mParent; |
michael@0 | 106 | mSetSize = sibling->mGroupInfo->mSetSize; |
michael@0 | 107 | return; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | mSetSize++; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | if (mParent) |
michael@0 | 114 | return; |
michael@0 | 115 | |
michael@0 | 116 | roles::Role parentRole = parent->Role(); |
michael@0 | 117 | if (ShouldReportRelations(mRole, parentRole)) |
michael@0 | 118 | mParent = parent; |
michael@0 | 119 | |
michael@0 | 120 | // ARIA tree and list can be arranged by using ARIA groups to organize levels. |
michael@0 | 121 | if (parentRole != roles::GROUPING) |
michael@0 | 122 | return; |
michael@0 | 123 | |
michael@0 | 124 | // Way #1 for ARIA tree (not ARIA treegrid): previous sibling of a group is a |
michael@0 | 125 | // parent. In other words the parent of the tree item will be a group and |
michael@0 | 126 | // the previous tree item of the group is a conceptual parent of the tree |
michael@0 | 127 | // item. |
michael@0 | 128 | if (mRole == roles::OUTLINEITEM) { |
michael@0 | 129 | Accessible* parentPrevSibling = parent->PrevSibling(); |
michael@0 | 130 | if (parentPrevSibling && parentPrevSibling->Role() == mRole) { |
michael@0 | 131 | mParent = parentPrevSibling; |
michael@0 | 132 | return; |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | // Way #2 for ARIA list and tree: group is a child of an item. In other words |
michael@0 | 137 | // the parent of the item will be a group and containing item of the group is |
michael@0 | 138 | // a conceptual parent of the item. |
michael@0 | 139 | if (mRole == roles::LISTITEM || mRole == roles::OUTLINEITEM) { |
michael@0 | 140 | Accessible* grandParent = parent->Parent(); |
michael@0 | 141 | if (grandParent && grandParent->Role() == mRole) |
michael@0 | 142 | mParent = grandParent; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | Accessible* |
michael@0 | 147 | AccGroupInfo::FirstItemOf(Accessible* aContainer) |
michael@0 | 148 | { |
michael@0 | 149 | // ARIA tree can be arranged by ARIA groups case #1 (previous sibling of a |
michael@0 | 150 | // group is a parent) or by aria-level. |
michael@0 | 151 | a11y::role containerRole = aContainer->Role(); |
michael@0 | 152 | Accessible* item = aContainer->NextSibling(); |
michael@0 | 153 | if (item) { |
michael@0 | 154 | if (containerRole == roles::OUTLINEITEM && item->Role() == roles::GROUPING) |
michael@0 | 155 | item = item->FirstChild(); |
michael@0 | 156 | |
michael@0 | 157 | if (item) { |
michael@0 | 158 | AccGroupInfo* itemGroupInfo = item->GetGroupInfo(); |
michael@0 | 159 | if (itemGroupInfo && itemGroupInfo->ConceptualParent() == aContainer) |
michael@0 | 160 | return item; |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | // ARIA list and tree can be arranged by ARIA groups case #2 (group is |
michael@0 | 165 | // a child of an item). |
michael@0 | 166 | item = aContainer->LastChild(); |
michael@0 | 167 | if (!item) |
michael@0 | 168 | return nullptr; |
michael@0 | 169 | |
michael@0 | 170 | if (item->Role() == roles::GROUPING && |
michael@0 | 171 | (containerRole == roles::LISTITEM || containerRole == roles::OUTLINEITEM)) { |
michael@0 | 172 | item = item->FirstChild(); |
michael@0 | 173 | if (item) { |
michael@0 | 174 | AccGroupInfo* itemGroupInfo = item->GetGroupInfo(); |
michael@0 | 175 | if (itemGroupInfo && itemGroupInfo->ConceptualParent() == aContainer) |
michael@0 | 176 | return item; |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | // Otherwise, it can be a direct child if the container is a list or tree. |
michael@0 | 181 | item = aContainer->FirstChild(); |
michael@0 | 182 | if (ShouldReportRelations(item->Role(), containerRole)) |
michael@0 | 183 | return item; |
michael@0 | 184 | |
michael@0 | 185 | return nullptr; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | Accessible* |
michael@0 | 189 | AccGroupInfo::NextItemTo(Accessible* aItem) |
michael@0 | 190 | { |
michael@0 | 191 | AccGroupInfo* groupInfo = aItem->GetGroupInfo(); |
michael@0 | 192 | if (!groupInfo) |
michael@0 | 193 | return nullptr; |
michael@0 | 194 | |
michael@0 | 195 | // If the item in middle of the group then search next item in siblings. |
michael@0 | 196 | if (groupInfo->PosInSet() >= groupInfo->SetSize()) |
michael@0 | 197 | return nullptr; |
michael@0 | 198 | |
michael@0 | 199 | Accessible* parent = aItem->Parent(); |
michael@0 | 200 | uint32_t childCount = parent->ChildCount(); |
michael@0 | 201 | for (int32_t idx = aItem->IndexInParent() + 1; idx < childCount; idx++) { |
michael@0 | 202 | Accessible* nextItem = parent->GetChildAt(idx); |
michael@0 | 203 | AccGroupInfo* nextGroupInfo = nextItem->GetGroupInfo(); |
michael@0 | 204 | if (nextGroupInfo && |
michael@0 | 205 | nextGroupInfo->ConceptualParent() == groupInfo->ConceptualParent()) { |
michael@0 | 206 | return nextItem; |
michael@0 | 207 | } |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | NS_NOTREACHED("Item in the middle of the group but there's no next item!"); |
michael@0 | 211 | return nullptr; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | bool |
michael@0 | 215 | AccGroupInfo::ShouldReportRelations(role aRole, role aParentRole) |
michael@0 | 216 | { |
michael@0 | 217 | // We only want to report hierarchy-based node relations for items in tree or |
michael@0 | 218 | // list form. ARIA level/owns relations are always reported. |
michael@0 | 219 | if (aParentRole == roles::OUTLINE && aRole == roles::OUTLINEITEM) |
michael@0 | 220 | return true; |
michael@0 | 221 | if (aParentRole == roles::TREE_TABLE && aRole == roles::ROW) |
michael@0 | 222 | return true; |
michael@0 | 223 | if (aParentRole == roles::LIST && aRole == roles::LISTITEM) |
michael@0 | 224 | return true; |
michael@0 | 225 | |
michael@0 | 226 | return false; |
michael@0 | 227 | } |