2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/inspector/ContentSearchUtils.h"
33 #include "bindings/core/v8/ScriptRegexp.h"
34 #include "wtf/Vector.h"
35 #include "wtf/text/StringBuilder.h"
38 namespace ContentSearchUtils {
41 // This should be kept the same as the one in front-end/utilities.js
42 static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
45 static String createSearchRegexSource(const String& text)
48 String specials(regexSpecialCharacters);
50 for (unsigned i = 0; i < text.length(); i++) {
51 if (specials.find(text[i]) != kNotFound)
53 result.append(text[i]);
56 return result.toString();
59 static Vector<pair<int, String> > getScriptRegexpMatchesByLines(const ScriptRegexp* regex, const String& text)
61 Vector<pair<int, String> > result;
65 OwnPtr<Vector<unsigned> > endings(lineEndings(text));
66 unsigned size = endings->size();
68 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) {
69 unsigned lineEnd = endings->at(lineNumber);
70 String line = text.substring(start, lineEnd - start);
71 if (line.endsWith('\r'))
72 line = line.left(line.length() - 1);
75 if (regex->match(line, 0, &matchLength) != -1)
76 result.append(pair<int, String>(lineNumber, line));
83 static PassRefPtr<TypeBuilder::Page::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
85 return TypeBuilder::Page::SearchMatch::create()
86 .setLineNumber(lineNumber)
87 .setLineContent(lineContent)
91 PassOwnPtr<ScriptRegexp> createSearchRegex(const String& query, bool caseSensitive, bool isRegex)
93 String regexSource = isRegex ? query : createSearchRegexSource(query);
94 return adoptPtr(new ScriptRegexp(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive));
97 PassRefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
99 RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > result = TypeBuilder::Array<TypeBuilder::Page::SearchMatch>::create();
101 OwnPtr<ScriptRegexp> regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex);
102 Vector<pair<int, String> > matches = getScriptRegexpMatchesByLines(regex.get(), text);
104 for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
105 result->addItem(buildObjectForSearchMatch(it->first, it->second));
110 static String findMagicComment(const String& content, const String& name, MagicCommentType commentType, bool* deprecated = 0)
112 ASSERT(name.find("=") == kNotFound);
116 unsigned length = content.length();
117 unsigned nameLength = name.length();
120 size_t equalSignPos = 0;
121 size_t closingCommentPos = 0;
123 pos = content.reverseFind(name, pos);
124 if (pos == kNotFound)
127 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name.
131 if (content[pos] != '/')
133 if ((content[pos + 1] != '/' || commentType != JavaScriptMagicComment)
134 && (content[pos + 1] != '*' || commentType != CSSMagicComment))
136 if (content[pos + 2] != '#' && content[pos + 2] != '@')
138 if (content[pos + 3] != ' ' && content[pos + 3] != '\t')
140 equalSignPos = pos + 4 + nameLength;
141 if (equalSignPos < length && content[equalSignPos] != '=')
143 if (commentType == CSSMagicComment) {
144 closingCommentPos = content.find("*/", equalSignPos + 1);
145 if (closingCommentPos == kNotFound)
152 if (deprecated && content[pos + 2] == '@')
155 ASSERT(equalSignPos);
156 ASSERT(commentType != CSSMagicComment || closingCommentPos);
157 size_t urlPos = equalSignPos + 1;
158 String match = commentType == CSSMagicComment
159 ? content.substring(urlPos, closingCommentPos - urlPos)
160 : content.substring(urlPos);
162 size_t newLine = match.find("\n");
163 if (newLine != kNotFound)
164 match = match.substring(0, newLine);
165 match = match.stripWhiteSpace();
167 String disallowedChars("\"' \t");
168 for (unsigned i = 0; i < match.length(); ++i) {
169 if (disallowedChars.find(match[i]) != kNotFound)
176 String findSourceURL(const String& content, MagicCommentType commentType, bool* deprecated)
178 return findMagicComment(content, "sourceURL", commentType, deprecated);
181 String findSourceMapURL(const String& content, MagicCommentType commentType, bool* deprecated)
183 return findMagicComment(content, "sourceMappingURL", commentType, deprecated);
186 } // namespace ContentSearchUtils