Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: extensions/browser/file_highlighter.cc

Issue 23624002: Add UI for RuntimeErrors in the ErrorConsole (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dc_ec_merge
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/file_highlighter.h"
6
5 #include <stack> 7 #include <stack>
6 8
7 #include "extensions/browser/manifest_highlighter.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
8 11
9 namespace extensions { 12 namespace extensions {
10 13
11 namespace { 14 namespace {
12 15
16 // Keys for a highlighted dictionary.
17 const char kBeforeHighlightKey[] = "beforeHighlight";
18 const char kHighlightKey[] = "highlight";
19 const char kAfterHighlightKey[] = "afterHighlight";
20
13 // Increment |index| to the position of the next quote ('"') in |str|, skipping 21 // Increment |index| to the position of the next quote ('"') in |str|, skipping
14 // over any escaped quotes. If no next quote is found, |index| is set to 22 // over any escaped quotes. If no next quote is found, |index| is set to
15 // std::string::npos. Assumes |index| currently points to a quote. 23 // std::string::npos. Assumes |index| currently points to a quote.
16 void QuoteIncrement(const std::string& str, size_t* index) { 24 void QuoteIncrement(const std::string& str, size_t* index) {
17 size_t i = *index + 1; // Skip over the first quote. 25 size_t i = *index + 1; // Skip over the first quote.
18 bool found = false; 26 bool found = false;
19 while (!found && i < str.size()) { 27 while (!found && i < str.size()) {
20 if (str[i] == '\\') 28 if (str[i] == '\\')
21 i += 2; // if we find an escaped character, skip it. 29 i += 2; // if we find an escaped character, skip it.
22 else if (str[i] == '"') 30 else if (str[i] == '"')
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 stack.push('}'); 69 stack.push('}');
62 else if (!stack.empty() && c == stack.top()) 70 else if (!stack.empty() && c == stack.top())
63 stack.pop(); 71 stack.pop();
64 CommentSafeIncrement(str, index); 72 CommentSafeIncrement(str, index);
65 c = str[*index]; 73 c = str[*index];
66 } while (!stack.empty() && *index < end); 74 } while (!stack.empty() && *index < end);
67 } 75 }
68 76
69 } // namespace 77 } // namespace
70 78
79 FileHighlighter::FileHighlighter(const std::string& contents)
80 : contents_(contents), start_(0u), end_(contents_.size()) {
81 }
82
83 FileHighlighter::~FileHighlighter() {
84 }
85
86 std::string FileHighlighter::GetBeforeFeature() const {
87 return contents_.substr(0, start_);
88 }
89
90 std::string FileHighlighter::GetFeature() const {
91 return contents_.substr(start_, end_ - start_);
92 }
93
94 std::string FileHighlighter::GetAfterFeature() const {
95 return contents_.substr(end_);
96 }
97
98 void FileHighlighter::SetHighlightedRegions(base::DictionaryValue* dict) const {
99 std::string before_feature = GetBeforeFeature();
100 if (!before_feature.empty())
101 dict->SetString(kBeforeHighlightKey, base::UTF8ToUTF16(before_feature));
102
103 std::string feature = GetFeature();
104 if (!feature.empty())
105 dict->SetString(kHighlightKey, base::UTF8ToUTF16(feature));
106
107 std::string after_feature = GetAfterFeature();
108 if (!after_feature.empty())
109 dict->SetString(kAfterHighlightKey, base::UTF8ToUTF16(after_feature));
110 }
111
71 ManifestHighlighter::ManifestHighlighter(const std::string& manifest, 112 ManifestHighlighter::ManifestHighlighter(const std::string& manifest,
72 const std::string& key, 113 const std::string& key,
73 const std::string& specific) 114 const std::string& specific)
74 : manifest_(manifest), 115 : FileHighlighter(manifest) {
75 start_(manifest_.find('{') + 1), 116 start_ = contents_.find('{') + 1;
76 end_(manifest_.rfind('}')) { 117 end_ = contents_.rfind('}');
77 Parse(key, specific); 118 Parse(key, specific);
78 } 119 }
79 120
80 ManifestHighlighter::~ManifestHighlighter() { 121 ManifestHighlighter::~ManifestHighlighter() {
81 } 122 }
82 123
83 std::string ManifestHighlighter::GetBeforeFeature() const {
84 return manifest_.substr(0, start_);
85 }
86
87 std::string ManifestHighlighter::GetFeature() const {
88 return manifest_.substr(start_, end_ - start_);
89 }
90
91 std::string ManifestHighlighter::GetAfterFeature() const {
92 return manifest_.substr(end_);
93 }
94 124
95 void ManifestHighlighter::Parse(const std::string& key, 125 void ManifestHighlighter::Parse(const std::string& key,
96 const std::string& specific) { 126 const std::string& specific) {
97 // First, try to find the bounds of the full key. 127 // First, try to find the bounds of the full key.
98 if (FindBounds(key, true) /* enforce at top level */ ) { 128 if (FindBounds(key, true) /* enforce at top level */ ) {
99 // If we succeed, and we have a specific location, find the bounds of the 129 // If we succeed, and we have a specific location, find the bounds of the
100 // specific. 130 // specific.
101 if (!specific.empty()) 131 if (!specific.empty())
102 FindBounds(specific, false /* don't enforce at top level */ ); 132 FindBounds(specific, false /* don't enforce at top level */ );
103 133
104 // We may have found trailing whitespace. Don't use base::TrimWhitespace, 134 // We may have found trailing whitespace. Don't use base::TrimWhitespace,
105 // because we want to keep any whitespace we find - just not highlight it. 135 // because we want to keep any whitespace we find - just not highlight it.
106 size_t trim = manifest_.find_last_not_of(" \t\n\r", end_ - 1); 136 size_t trim = contents_.find_last_not_of(" \t\n\r", end_ - 1);
107 if (trim < end_ && trim > start_) 137 if (trim < end_ && trim > start_)
108 end_ = trim + 1; 138 end_ = trim + 1;
109 } else { 139 } else {
110 // If we fail, then we set start to end so that the highlighted portion is 140 // If we fail, then we set start to end so that the highlighted portion is
111 // empty. 141 // empty.
112 start_ = end_; 142 start_ = end_;
113 } 143 }
114 } 144 }
115 145
116 bool ManifestHighlighter::FindBounds(const std::string& feature, 146 bool ManifestHighlighter::FindBounds(const std::string& feature,
117 bool enforce_at_top_level) { 147 bool enforce_at_top_level) {
118 char c = '\0'; 148 char c = '\0';
119 while (start_ < end_) { 149 while (start_ < end_) {
120 c = manifest_[start_]; 150 c = contents_[start_];
121 if (c == '"') { 151 if (c == '"') {
122 // The feature may be quoted. 152 // The feature may be quoted.
123 size_t quote_end = start_; 153 size_t quote_end = start_;
124 QuoteIncrement(manifest_, &quote_end); 154 QuoteIncrement(contents_, &quote_end);
125 if (manifest_.substr(start_ + 1, quote_end - 1 - start_) == feature) { 155 if (contents_.substr(start_ + 1, quote_end - 1 - start_) == feature) {
126 FindBoundsEnd(feature, quote_end + 1); 156 FindBoundsEnd(feature, quote_end + 1);
127 return true; 157 return true;
128 } else { 158 } else {
129 // If it's not the feature, then we can skip the quoted section. 159 // If it's not the feature, then we can skip the quoted section.
130 start_ = quote_end + 1; 160 start_ = quote_end + 1;
131 } 161 }
132 } else if (manifest_.substr(start_, feature.size()) == feature) { 162 } else if (contents_.substr(start_, feature.size()) == feature) {
133 FindBoundsEnd(feature, start_ + feature.size() + 1); 163 FindBoundsEnd(feature, start_ + feature.size() + 1);
134 return true; 164 return true;
135 } else if (enforce_at_top_level && (c == '{' || c == '[')) { 165 } else if (enforce_at_top_level && (c == '{' || c == '[')) {
136 // If we don't have to be at the top level, then we can skip any chunks 166 // If we don't have to be at the top level, then we can skip any chunks
137 // we find. 167 // we find.
138 ChunkIncrement(manifest_, &start_, end_); 168 ChunkIncrement(contents_, &start_, end_);
139 } else { 169 } else {
140 CommentSafeIncrement(manifest_, &start_); 170 CommentSafeIncrement(contents_, &start_);
141 } 171 }
142 } 172 }
143 return false; 173 return false;
144 } 174 }
145 175
146 void ManifestHighlighter::FindBoundsEnd(const std::string& feature, 176 void ManifestHighlighter::FindBoundsEnd(const std::string& feature,
147 size_t local_start) { 177 size_t local_start) {
148 char c = '\0'; 178 char c = '\0';
149 while (local_start < end_) { 179 while (local_start < end_) {
150 c = manifest_[local_start]; 180 c = contents_[local_start];
151 // We're done when we find a terminating character (i.e., either a comma or 181 // We're done when we find a terminating character (i.e., either a comma or
152 // an ending bracket. 182 // an ending bracket.
153 if (c == ',' || c == '}' || c == ']') { 183 if (c == ',' || c == '}' || c == ']') {
154 end_ = local_start; 184 end_ = local_start;
155 return; 185 return;
156 } 186 }
157 // We can skip any chunks we find, since we are looking for the end of the 187 // We can skip any chunks we find, since we are looking for the end of the
158 // current feature, and don't want to go any deeper. 188 // current feature, and don't want to go any deeper.
159 if (c == '"' || c == '{' || c == '[') 189 if (c == '"' || c == '{' || c == '[')
160 ChunkIncrement(manifest_, &local_start, end_); 190 ChunkIncrement(contents_, &local_start, end_);
161 else 191 else
162 CommentSafeIncrement(manifest_, &local_start); 192 CommentSafeIncrement(contents_, &local_start);
163 } 193 }
164 } 194 }
165 195
196 SourceHighlighter::SourceHighlighter(const std::string& contents,
197 size_t line_number)
198 : FileHighlighter(contents) {
199 Parse(line_number);
200 }
201
202 SourceHighlighter::~SourceHighlighter() {
203 }
204
205 void SourceHighlighter::Parse(size_t line_number) {
206 for (size_t i = 1; i < line_number; ++i)
207 start_ = contents_.find('\n', start_) + 1;
Finnur 2013/09/09 09:15:56 The +1 makes the check for npos on line 213 invali
Devlin 2013/09/09 16:56:03 Whoops! It'll actually work, given valid input (t
208 end_ = contents_.find('\n', start_);
209
210 // If we went off the end of the string (i.e., the line number was invalid),
211 // then move start and end to the end of the string, so that the highlighted
212 // portion is empty.
213 start_ = start_ == std::string::npos ? contents_.size() : start_;
214 end_ = end_ == std::string::npos ? contents_.size() : end_;
215 }
216
166 } // namespace extensions 217 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698