OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/dom/AccessibleNodeList.h" |
| 6 |
| 7 #include "core/dom/AccessibleNode.h" |
| 8 |
| 9 namespace blink { |
| 10 |
| 11 // The spec doesn't give a limit, but there's no reason to allow relations |
| 12 // between an arbitrarily large number of other accessible nodes. |
| 13 static const unsigned kMaxItems = 65536; |
| 14 |
| 15 // static |
| 16 AccessibleNodeList* AccessibleNodeList::Create( |
| 17 const HeapVector<Member<AccessibleNode>>& nodes) { |
| 18 AccessibleNodeList* result = new AccessibleNodeList(); |
| 19 result->nodes_ = nodes; |
| 20 return result; |
| 21 } |
| 22 |
| 23 AccessibleNodeList::AccessibleNodeList() { |
| 24 DCHECK(RuntimeEnabledFeatures::AccessibilityObjectModelEnabled()); |
| 25 } |
| 26 |
| 27 AccessibleNodeList::~AccessibleNodeList() {} |
| 28 |
| 29 void AccessibleNodeList::AddOwner(AOMRelationListProperty property, |
| 30 AccessibleNode* node) { |
| 31 owners_.push_back(std::make_pair(property, node)); |
| 32 } |
| 33 |
| 34 void AccessibleNodeList::RemoveOwner(AOMRelationListProperty property, |
| 35 AccessibleNode* node) { |
| 36 for (size_t i = 0; i < owners_.size(); ++i) { |
| 37 auto& item = owners_[i]; |
| 38 if (item.first == property && item.second == node) { |
| 39 owners_.erase(i); |
| 40 return; |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 AccessibleNode* AccessibleNodeList::item(unsigned offset) const { |
| 46 if (offset < nodes_.size()) |
| 47 return nodes_[offset]; |
| 48 return nullptr; |
| 49 } |
| 50 |
| 51 void AccessibleNodeList::add(AccessibleNode* node, AccessibleNode* before) { |
| 52 if (nodes_.size() == kMaxItems) |
| 53 return; |
| 54 |
| 55 unsigned index = nodes_.size(); |
| 56 if (before) { |
| 57 for (index = 0; index < nodes_.size(); ++index) { |
| 58 if (nodes_[index] == before) |
| 59 break; |
| 60 } |
| 61 if (index == nodes_.size()) |
| 62 return; |
| 63 } |
| 64 |
| 65 nodes_.insert(index, node); |
| 66 } |
| 67 |
| 68 void AccessibleNodeList::remove(int index) { |
| 69 if (index >= 0 && index < static_cast<int>(nodes_.size())) |
| 70 nodes_.erase(index); |
| 71 } |
| 72 |
| 73 bool AccessibleNodeList::AnonymousIndexedSetter(unsigned index, |
| 74 AccessibleNode* node, |
| 75 ExceptionState& state) { |
| 76 if (!node) { |
| 77 remove(index); |
| 78 return true; |
| 79 } |
| 80 if (index >= kMaxItems) |
| 81 return false; |
| 82 if (index >= nodes_.size()) { |
| 83 unsigned old_size = nodes_.size(); |
| 84 nodes_.resize(index + 1); |
| 85 for (unsigned i = old_size; i < nodes_.size(); ++i) |
| 86 nodes_[i] = nullptr; |
| 87 } |
| 88 nodes_[index] = node; |
| 89 return true; |
| 90 } |
| 91 |
| 92 unsigned AccessibleNodeList::length() const { |
| 93 return nodes_.size(); |
| 94 } |
| 95 |
| 96 void AccessibleNodeList::setLength(unsigned new_length) { |
| 97 if (new_length >= kMaxItems) |
| 98 return; |
| 99 nodes_.resize(new_length); |
| 100 } |
| 101 |
| 102 void AccessibleNodeList::NotifyChanged() { |
| 103 for (auto& owner : owners_) |
| 104 owner.second->OnRelationListChanged(owner.first); |
| 105 } |
| 106 |
| 107 DEFINE_TRACE(AccessibleNodeList) { |
| 108 visitor->Trace(nodes_); |
| 109 visitor->Trace(owners_); |
| 110 } |
| 111 |
| 112 } // namespace blink |
OLD | NEW |