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/v8/ScriptRegexp.h"
34 #include "wtf/text/StringBuilder.h"
39 namespace ContentSearchUtils {
42 // This should be kept the same as the one in front-end/utilities.js
43 static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
46 static String createSearchRegexSource(const String& text)
49 String specials(regexSpecialCharacters);
51 for (unsigned i = 0; i < text.length(); i++) {
52 if (specials.find(text[i]) != kNotFound)
54 result.append(text[i]);
57 return result.toString();
60 static Vector<pair<int, String> > getScriptRegexpMatchesByLines(const ScriptRegexp* regex, const String& text)
62 Vector<pair<int, String> > result;
66 OwnPtr<Vector<unsigned> > endings(lineEndings(text));
67 unsigned size = endings->size();
69 for (unsigned lineNumber = 0; lineNumber < size; ++lineNumber) {
70 unsigned lineEnd = endings->at(lineNumber);
71 String line = text.substring(start, lineEnd - start);
72 if (line.endsWith('\r'))
73 line = line.left(line.length() - 1);
76 if (regex->match(line, 0, &matchLength) != -1)
77 result.append(pair<int, String>(lineNumber, line));
84 static PassRefPtr<TypeBuilder::Page::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
86 return TypeBuilder::Page::SearchMatch::create()
87 .setLineNumber(lineNumber)
88 .setLineContent(lineContent)
92 PassOwnPtr<ScriptRegexp> createSearchRegex(const String& query, bool caseSensitive, bool isRegex)
94 String regexSource = isRegex ? query : createSearchRegexSource(query);
95 return adoptPtr(new ScriptRegexp(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive));
98 PassRefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
100 RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > result = TypeBuilder::Array<TypeBuilder::Page::SearchMatch>::create();
102 OwnPtr<ScriptRegexp> regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex);
103 Vector<pair<int, String> > matches = getScriptRegexpMatchesByLines(regex.get(), text);
105 for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
106 result->addItem(buildObjectForSearchMatch(it->first, it->second));
111 static String findMagicComment(const String& content, const String& name, MagicCommentType commentType, bool* deprecated = 0)
113 ASSERT(name.find("=") == kNotFound);
117 unsigned length = content.length();
118 unsigned nameLength = name.length();
121 size_t equalSignPos = 0;
122 size_t closingCommentPos = 0;
124 pos = content.reverseFind(name, pos);
125 if (pos == kNotFound)
128 // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name.
132 if (content[pos] != '/')
134 if ((content[pos + 1] != '/' || commentType != JavaScriptMagicComment)
135 && (content[pos + 1] != '*' || commentType != CSSMagicComment))
137 if (content[pos + 2] != '#' && content[pos + 2] != '@')
139 if (content[pos + 3] != ' ' && content[pos + 3] != '\t')
141 equalSignPos = pos + 4 + nameLength;
142 if (equalSignPos < length && content[equalSignPos] != '=')
144 if (commentType == CSSMagicComment) {
145 closingCommentPos = content.find("*/", equalSignPos + 1);
146 if (closingCommentPos == kNotFound)
153 if (deprecated && content[pos + 2] == '@')
156 ASSERT(equalSignPos);
157 ASSERT(commentType != CSSMagicComment || closingCommentPos);
158 size_t urlPos = equalSignPos + 1;
159 String match = commentType == CSSMagicComment
160 ? content.substring(urlPos, closingCommentPos - urlPos)
161 : content.substring(urlPos);
163 size_t newLine = match.find("\n");
164 if (newLine != kNotFound)
165 match = match.substring(0, newLine);
166 match = match.stripWhiteSpace();
168 String disallowedChars("\"' \t");
169 for (unsigned i = 0; i < match.length(); ++i) {
170 if (disallowedChars.find(match[i]) != kNotFound)
177 String findSourceURL(const String& content, MagicCommentType commentType, bool* deprecated)
179 return findMagicComment(content, "sourceURL", commentType, deprecated);
182 String findSourceMapURL(const String& content, MagicCommentType commentType, bool* deprecated)
184 return findMagicComment(content, "sourceMappingURL", commentType, deprecated);
187 } // namespace ContentSearchUtils
188 } // namespace WebCore