Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / ContentSearchUtils.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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
14  * distribution.
15  *
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.
27  */
28
29 #include "config.h"
30
31 #include "core/inspector/ContentSearchUtils.h"
32
33 #include "bindings/v8/ScriptRegexp.h"
34 #include "wtf/text/StringBuilder.h"
35
36 using namespace std;
37
38 namespace WebCore {
39 namespace ContentSearchUtils {
40
41 namespace {
42 // This should be kept the same as the one in front-end/utilities.js
43 static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
44 }
45
46 static String createSearchRegexSource(const String& text)
47 {
48     StringBuilder result;
49     String specials(regexSpecialCharacters);
50
51     for (unsigned i = 0; i < text.length(); i++) {
52         if (specials.find(text[i]) != kNotFound)
53             result.append("\\");
54         result.append(text[i]);
55     }
56
57     return result.toString();
58 }
59
60 static Vector<pair<int, String> > getScriptRegexpMatchesByLines(const ScriptRegexp* regex, const String& text)
61 {
62     Vector<pair<int, String> > result;
63     if (text.isEmpty())
64         return result;
65
66     OwnPtr<Vector<unsigned> > endings(lineEndings(text));
67     unsigned size = endings->size();
68     unsigned start = 0;
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);
74
75         int matchLength;
76         if (regex->match(line, 0, &matchLength) != -1)
77             result.append(pair<int, String>(lineNumber, line));
78
79         start = lineEnd + 1;
80     }
81     return result;
82 }
83
84 static PassRefPtr<TypeBuilder::Page::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
85 {
86     return TypeBuilder::Page::SearchMatch::create()
87         .setLineNumber(lineNumber)
88         .setLineContent(lineContent)
89         .release();
90 }
91
92 PassOwnPtr<ScriptRegexp> createSearchRegex(const String& query, bool caseSensitive, bool isRegex)
93 {
94     String regexSource = isRegex ? query : createSearchRegexSource(query);
95     return adoptPtr(new ScriptRegexp(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive));
96 }
97
98 PassRefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
99 {
100     RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > result = TypeBuilder::Array<TypeBuilder::Page::SearchMatch>::create();
101
102     OwnPtr<ScriptRegexp> regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex);
103     Vector<pair<int, String> > matches = getScriptRegexpMatchesByLines(regex.get(), text);
104
105     for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
106         result->addItem(buildObjectForSearchMatch(it->first, it->second));
107
108     return result;
109 }
110
111 static String findMagicComment(const String& content, const String& name, MagicCommentType commentType, bool* deprecated = 0)
112 {
113     ASSERT(name.find("=") == kNotFound);
114     if (deprecated)
115         *deprecated = false;
116
117     unsigned length = content.length();
118     unsigned nameLength = name.length();
119
120     size_t pos = length;
121     size_t equalSignPos = 0;
122     size_t closingCommentPos = 0;
123     while (true) {
124         pos = content.reverseFind(name, pos);
125         if (pos == kNotFound)
126             return String();
127
128         // Check for a /\/[\/*][@#][ \t]/ regexp (length of 4) before found name.
129         if (pos < 4)
130             return String();
131         pos -= 4;
132         if (content[pos] != '/')
133             continue;
134         if ((content[pos + 1] != '/' || commentType != JavaScriptMagicComment)
135             && (content[pos + 1] != '*' || commentType != CSSMagicComment))
136             continue;
137         if (content[pos + 2] != '#' && content[pos + 2] != '@')
138             continue;
139         if (content[pos + 3] != ' ' && content[pos + 3] != '\t')
140             continue;
141         equalSignPos = pos + 4 + nameLength;
142         if (equalSignPos < length && content[equalSignPos] != '=')
143             continue;
144         if (commentType == CSSMagicComment) {
145             closingCommentPos = content.find("*/", equalSignPos + 1);
146             if (closingCommentPos == kNotFound)
147                 return String();
148         }
149
150         break;
151     }
152
153     if (deprecated && content[pos + 2] == '@')
154         *deprecated = true;
155
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);
162
163     size_t newLine = match.find("\n");
164     if (newLine != kNotFound)
165         match = match.substring(0, newLine);
166     match = match.stripWhiteSpace();
167
168     String disallowedChars("\"' \t");
169     for (unsigned i = 0; i < match.length(); ++i) {
170         if (disallowedChars.find(match[i]) != kNotFound)
171             return "";
172     }
173
174     return match;
175 }
176
177 String findSourceURL(const String& content, MagicCommentType commentType, bool* deprecated)
178 {
179     return findMagicComment(content, "sourceURL", commentType, deprecated);
180 }
181
182 String findSourceMapURL(const String& content, MagicCommentType commentType, bool* deprecated)
183 {
184     return findMagicComment(content, "sourceMappingURL", commentType, deprecated);
185 }
186
187 } // namespace ContentSearchUtils
188 } // namespace WebCore
189