OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "content/browser/accessibility/dump_accessibility_tree_helper.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 | |
12 namespace content { | |
13 namespace { | |
14 const int kIndentSpaces = 4; | |
15 const char* kSkipString = "@NO_DUMP"; | |
16 } | |
17 | |
18 DumpAccessibilityTreeHelper::DumpAccessibilityTreeHelper() { | |
19 Initialize(); | |
20 } | |
21 | |
22 DumpAccessibilityTreeHelper::~DumpAccessibilityTreeHelper() { | |
23 } | |
24 | |
25 void DumpAccessibilityTreeHelper::DumpAccessibilityTree( | |
26 BrowserAccessibility* node, string16* contents) { | |
27 RecursiveDumpAccessibilityTree(node, contents, 0); | |
28 } | |
29 | |
30 void DumpAccessibilityTreeHelper::RecursiveDumpAccessibilityTree( | |
31 BrowserAccessibility* node, string16* contents, int indent) { | |
32 scoped_array<char> prefix(new char[indent + 1]); | |
33 for (int i = 0; i < indent; ++i) | |
34 prefix[i] = ' '; | |
35 prefix[indent] = '\0'; | |
36 | |
37 string16 line = ToString(node, prefix.get()); | |
38 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) | |
39 return; | |
40 | |
41 *contents += line; | |
42 for (size_t i = 0; i < node->children().size(); ++i) { | |
43 RecursiveDumpAccessibilityTree(node->children()[i], contents, | |
44 indent + kIndentSpaces); | |
45 } | |
46 } | |
47 | |
48 void DumpAccessibilityTreeHelper::SetFilters( | |
49 const std::vector<Filter>& filters) { | |
50 filters_ = filters; | |
51 } | |
52 | |
53 bool DumpAccessibilityTreeHelper::MatchesFilters( | |
54 const string16& text, bool default_result) { | |
55 std::vector<Filter>::const_iterator iter = filters_.begin(); | |
56 bool allow = default_result; | |
57 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { | |
58 if (MatchPattern(text, iter->match_str)) { | |
59 if (iter->type == Filter::ALLOW_EMPTY) | |
60 allow = true; | |
61 else if (iter->type == Filter::ALLOW) | |
62 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); | |
63 else | |
64 allow = false; | |
65 } | |
66 } | |
67 return allow; | |
68 } | |
69 | |
70 void DumpAccessibilityTreeHelper::StartLine() { | |
71 line_.clear(); | |
72 } | |
73 | |
74 void DumpAccessibilityTreeHelper::Add( | |
75 bool include_by_default, const string16& attr) { | |
76 if (attr.empty()) | |
77 return; | |
78 if (!MatchesFilters(attr, include_by_default)) | |
79 return; | |
80 if (!line_.empty()) | |
81 line_ += ASCIIToUTF16(" "); | |
82 line_ += attr; | |
83 } | |
84 | |
85 string16 DumpAccessibilityTreeHelper::FinishLine() { | |
86 return line_; | |
87 } | |
88 | |
89 } // namespace content | |
OLD | NEW |