Chromium Code Reviews| Index: extensions/browser/file_highlighter.cc |
| diff --git a/extensions/browser/manifest_highlighter.cc b/extensions/browser/file_highlighter.cc |
| similarity index 63% |
| rename from extensions/browser/manifest_highlighter.cc |
| rename to extensions/browser/file_highlighter.cc |
| index 5afdc46e0818179006f77ffa253142a406f59bf1..d3edeca42cf7bb0eed41ef2f95df899de1700816 100644 |
| --- a/extensions/browser/manifest_highlighter.cc |
| +++ b/extensions/browser/file_highlighter.cc |
| @@ -2,14 +2,22 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "extensions/browser/file_highlighter.h" |
| + |
| #include <stack> |
| -#include "extensions/browser/manifest_highlighter.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/values.h" |
| namespace extensions { |
| namespace { |
| +// Keys for a highlighted dictionary. |
| +const char kBeforeHighlightKey[] = "beforeHighlight"; |
| +const char kHighlightKey[] = "highlight"; |
| +const char kAfterHighlightKey[] = "afterHighlight"; |
| + |
| // Increment |index| to the position of the next quote ('"') in |str|, skipping |
| // over any escaped quotes. If no next quote is found, |index| is set to |
| // std::string::npos. Assumes |index| currently points to a quote. |
| @@ -68,30 +76,52 @@ void ChunkIncrement(const std::string& str, size_t* index, size_t end) { |
| } // namespace |
| -ManifestHighlighter::ManifestHighlighter(const std::string& manifest, |
| - const std::string& key, |
| - const std::string& specific) |
| - : manifest_(manifest), |
| - start_(manifest_.find('{') + 1), |
| - end_(manifest_.rfind('}')) { |
| - Parse(key, specific); |
| +FileHighlighter::FileHighlighter(const std::string& contents) |
| + : contents_(contents), start_(0u), end_(contents_.size()) { |
| } |
| -ManifestHighlighter::~ManifestHighlighter() { |
| +FileHighlighter::~FileHighlighter() { |
| } |
| -std::string ManifestHighlighter::GetBeforeFeature() const { |
| - return manifest_.substr(0, start_); |
| +std::string FileHighlighter::GetBeforeFeature() const { |
| + return contents_.substr(0, start_); |
| +} |
| + |
| +std::string FileHighlighter::GetFeature() const { |
| + return contents_.substr(start_, end_ - start_); |
| +} |
| + |
| +std::string FileHighlighter::GetAfterFeature() const { |
| + return contents_.substr(end_); |
| +} |
| + |
| +void FileHighlighter::SetHighlightedRegions(base::DictionaryValue* dict) const { |
| + std::string before_feature = GetBeforeFeature(); |
| + if (!before_feature.empty()) |
| + dict->SetString(kBeforeHighlightKey, base::UTF8ToUTF16(before_feature)); |
| + |
| + std::string feature = GetFeature(); |
| + if (!feature.empty()) |
| + dict->SetString(kHighlightKey, base::UTF8ToUTF16(feature)); |
| + |
| + std::string after_feature = GetAfterFeature(); |
| + if (!after_feature.empty()) |
| + dict->SetString(kAfterHighlightKey, base::UTF8ToUTF16(after_feature)); |
| } |
| -std::string ManifestHighlighter::GetFeature() const { |
| - return manifest_.substr(start_, end_ - start_); |
| +ManifestHighlighter::ManifestHighlighter(const std::string& manifest, |
| + const std::string& key, |
| + const std::string& specific) |
| + : FileHighlighter(manifest) { |
| + start_ = contents_.find('{') + 1; |
| + end_ = contents_.rfind('}'); |
| + Parse(key, specific); |
| } |
| -std::string ManifestHighlighter::GetAfterFeature() const { |
| - return manifest_.substr(end_); |
| +ManifestHighlighter::~ManifestHighlighter() { |
| } |
| + |
| void ManifestHighlighter::Parse(const std::string& key, |
| const std::string& specific) { |
| // First, try to find the bounds of the full key. |
| @@ -103,7 +133,7 @@ void ManifestHighlighter::Parse(const std::string& key, |
| // We may have found trailing whitespace. Don't use base::TrimWhitespace, |
| // because we want to keep any whitespace we find - just not highlight it. |
| - size_t trim = manifest_.find_last_not_of(" \t\n\r", end_ - 1); |
| + size_t trim = contents_.find_last_not_of(" \t\n\r", end_ - 1); |
| if (trim < end_ && trim > start_) |
| end_ = trim + 1; |
| } else { |
| @@ -117,27 +147,27 @@ bool ManifestHighlighter::FindBounds(const std::string& feature, |
| bool enforce_at_top_level) { |
| char c = '\0'; |
| while (start_ < end_) { |
| - c = manifest_[start_]; |
| + c = contents_[start_]; |
| if (c == '"') { |
| // The feature may be quoted. |
| size_t quote_end = start_; |
| - QuoteIncrement(manifest_, "e_end); |
| - if (manifest_.substr(start_ + 1, quote_end - 1 - start_) == feature) { |
| + QuoteIncrement(contents_, "e_end); |
| + if (contents_.substr(start_ + 1, quote_end - 1 - start_) == feature) { |
| FindBoundsEnd(feature, quote_end + 1); |
| return true; |
| } else { |
| // If it's not the feature, then we can skip the quoted section. |
| start_ = quote_end + 1; |
| } |
| - } else if (manifest_.substr(start_, feature.size()) == feature) { |
| + } else if (contents_.substr(start_, feature.size()) == feature) { |
| FindBoundsEnd(feature, start_ + feature.size() + 1); |
| return true; |
| } else if (enforce_at_top_level && (c == '{' || c == '[')) { |
| // If we don't have to be at the top level, then we can skip any chunks |
| // we find. |
| - ChunkIncrement(manifest_, &start_, end_); |
| + ChunkIncrement(contents_, &start_, end_); |
| } else { |
| - CommentSafeIncrement(manifest_, &start_); |
| + CommentSafeIncrement(contents_, &start_); |
| } |
| } |
| return false; |
| @@ -147,7 +177,7 @@ void ManifestHighlighter::FindBoundsEnd(const std::string& feature, |
| size_t local_start) { |
| char c = '\0'; |
| while (local_start < end_) { |
| - c = manifest_[local_start]; |
| + c = contents_[local_start]; |
| // We're done when we find a terminating character (i.e., either a comma or |
| // an ending bracket. |
| if (c == ',' || c == '}' || c == ']') { |
| @@ -157,10 +187,31 @@ void ManifestHighlighter::FindBoundsEnd(const std::string& feature, |
| // We can skip any chunks we find, since we are looking for the end of the |
| // current feature, and don't want to go any deeper. |
| if (c == '"' || c == '{' || c == '[') |
| - ChunkIncrement(manifest_, &local_start, end_); |
| + ChunkIncrement(contents_, &local_start, end_); |
| else |
| - CommentSafeIncrement(manifest_, &local_start); |
| + CommentSafeIncrement(contents_, &local_start); |
| } |
| } |
| +SourceHighlighter::SourceHighlighter(const std::string& contents, |
| + size_t line_number) |
| + : FileHighlighter(contents) { |
| + Parse(line_number); |
| +} |
| + |
| +SourceHighlighter::~SourceHighlighter() { |
| +} |
| + |
| +void SourceHighlighter::Parse(size_t line_number) { |
| + for (size_t i = 1; i < line_number; ++i) |
| + 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
|
| + end_ = contents_.find('\n', start_); |
| + |
| + // If we went off the end of the string (i.e., the line number was invalid), |
| + // then move start and end to the end of the string, so that the highlighted |
| + // portion is empty. |
| + start_ = start_ == std::string::npos ? contents_.size() : start_; |
| + end_ = end_ == std::string::npos ? contents_.size() : end_; |
| +} |
| + |
| } // namespace extensions |