| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stack> | |
| 6 | |
| 7 #include "extensions/browser/manifest_highlighter.h" | |
| 8 | |
| 9 namespace extensions { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // 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 | |
| 15 // std::string::npos. Assumes |index| currently points to a quote. | |
| 16 void QuoteIncrement(const std::string& str, size_t* index) { | |
| 17 size_t i = *index + 1; // Skip over the first quote. | |
| 18 bool found = false; | |
| 19 while (!found && i < str.size()) { | |
| 20 if (str[i] == '\\') | |
| 21 i += 2; // if we find an escaped character, skip it. | |
| 22 else if (str[i] == '"') | |
| 23 found = true; | |
| 24 else | |
| 25 ++i; | |
| 26 } | |
| 27 *index = found ? i : std::string::npos; | |
| 28 } | |
| 29 | |
| 30 // Increment |index| by one if the next character is not a comment. Increment | |
| 31 // index until the end of the comment if it is a comment. | |
| 32 void CommentSafeIncrement(const std::string& str, size_t* index) { | |
| 33 size_t i = *index; | |
| 34 if (str[i] == '/' && i + 1 < str.size()) { | |
| 35 // Eat a single-line comment. | |
| 36 if (str[i + 1] == '/') { | |
| 37 i += 2; // Eat the '//'. | |
| 38 while (i < str.size() && str[i] != '\n' && str[i] != '\r') | |
| 39 ++i; | |
| 40 } else if (str[i + 1] == '*') { // Eat a multi-line comment. | |
| 41 i += 3; // Advance to the first possible comment end. | |
| 42 while (i < str.size() && !(str[i - 1] == '*' && str[i] == '/')) | |
| 43 ++i; | |
| 44 } | |
| 45 } | |
| 46 *index = i + 1; | |
| 47 } | |
| 48 | |
| 49 // Increment index until the end of the current "chunk"; a "chunk" is a JSON- | |
| 50 // style list, object, or string literal, without exceeding |end|. Assumes | |
| 51 // |index| currently points to a chunk's starting character ('{', '[', or '"'). | |
| 52 void ChunkIncrement(const std::string& str, size_t* index, size_t end) { | |
| 53 char c = str[*index]; | |
| 54 std::stack<char> stack; | |
| 55 do { | |
| 56 if (c == '"') | |
| 57 QuoteIncrement(str, index); | |
| 58 else if (c == '[') | |
| 59 stack.push(']'); | |
| 60 else if (c == '{') | |
| 61 stack.push('}'); | |
| 62 else if (!stack.empty() && c == stack.top()) | |
| 63 stack.pop(); | |
| 64 CommentSafeIncrement(str, index); | |
| 65 c = str[*index]; | |
| 66 } while (!stack.empty() && *index < end); | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 ManifestHighlighter::ManifestHighlighter(const std::string& manifest, | |
| 72 const std::string& key, | |
| 73 const std::string& specific) | |
| 74 : manifest_(manifest), | |
| 75 start_(manifest_.find('{') + 1), | |
| 76 end_(manifest_.rfind('}')) { | |
| 77 Parse(key, specific); | |
| 78 } | |
| 79 | |
| 80 ManifestHighlighter::~ManifestHighlighter() { | |
| 81 } | |
| 82 | |
| 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 | |
| 95 void ManifestHighlighter::Parse(const std::string& key, | |
| 96 const std::string& specific) { | |
| 97 // First, try to find the bounds of the full key. | |
| 98 if (FindBounds(key, true) /* enforce at top level */ ) { | |
| 99 // If we succeed, and we have a specific location, find the bounds of the | |
| 100 // specific. | |
| 101 if (!specific.empty()) | |
| 102 FindBounds(specific, false /* don't enforce at top level */ ); | |
| 103 | |
| 104 // 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. | |
| 106 size_t trim = manifest_.find_last_not_of(" \t\n\r", end_ - 1); | |
| 107 if (trim < end_ && trim > start_) | |
| 108 end_ = trim + 1; | |
| 109 } else { | |
| 110 // If we fail, then we set start to end so that the highlighted portion is | |
| 111 // empty. | |
| 112 start_ = end_; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 bool ManifestHighlighter::FindBounds(const std::string& feature, | |
| 117 bool enforce_at_top_level) { | |
| 118 char c = '\0'; | |
| 119 while (start_ < end_) { | |
| 120 c = manifest_[start_]; | |
| 121 if (c == '"') { | |
| 122 // The feature may be quoted. | |
| 123 size_t quote_end = start_; | |
| 124 QuoteIncrement(manifest_, "e_end); | |
| 125 if (manifest_.substr(start_ + 1, quote_end - 1 - start_) == feature) { | |
| 126 FindBoundsEnd(feature, quote_end + 1); | |
| 127 return true; | |
| 128 } else { | |
| 129 // If it's not the feature, then we can skip the quoted section. | |
| 130 start_ = quote_end + 1; | |
| 131 } | |
| 132 } else if (manifest_.substr(start_, feature.size()) == feature) { | |
| 133 FindBoundsEnd(feature, start_ + feature.size() + 1); | |
| 134 return true; | |
| 135 } 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 | |
| 137 // we find. | |
| 138 ChunkIncrement(manifest_, &start_, end_); | |
| 139 } else { | |
| 140 CommentSafeIncrement(manifest_, &start_); | |
| 141 } | |
| 142 } | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 void ManifestHighlighter::FindBoundsEnd(const std::string& feature, | |
| 147 size_t local_start) { | |
| 148 char c = '\0'; | |
| 149 while (local_start < end_) { | |
| 150 c = manifest_[local_start]; | |
| 151 // We're done when we find a terminating character (i.e., either a comma or | |
| 152 // an ending bracket. | |
| 153 if (c == ',' || c == '}' || c == ']') { | |
| 154 end_ = local_start; | |
| 155 return; | |
| 156 } | |
| 157 // 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. | |
| 159 if (c == '"' || c == '{' || c == '[') | |
| 160 ChunkIncrement(manifest_, &local_start, end_); | |
| 161 else | |
| 162 CommentSafeIncrement(manifest_, &local_start); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 } // namespace extensions | |
| OLD | NEW |