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 "core/platform/text/RegularExpression.h" 34 35 using namespace std; 36 37 namespace WebCore { 38 namespace ContentSearchUtils { 39 40 namespace { 41 // This should be kept the same as the one in front-end/utilities.js 42 static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|"; 43 } 44 45 static String createSearchRegexSource(const String& text) 46 { 47 String result; 48 String specials(regexSpecialCharacters); 49 50 for (unsigned i = 0; i < text.length(); i++) { 51 if (specials.find(text[i]) != notFound) 52 result.append("\\"); 53 result.append(text[i]); 54 } 55 56 return result; 57 } 58 59 static Vector<pair<int, String> > getRegularExpressionMatchesByLines(const RegularExpression* regex, const String& text) 60 { 61 Vector<pair<int, String> > result; 62 if (text.isEmpty()) 63 return result; 64 65 OwnPtr<Vector<unsigned> > endings(lineEndings(text)); 66 unsigned size = endings->size(); 67 unsigned start = 0; 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); 73 74 int matchLength; 75 if (regex->match(line, 0, &matchLength) != -1) 76 result.append(pair<int, String>(lineNumber, line)); 77 78 start = lineEnd + 1; 79 } 80 return result; 81 } 82 83 static PassRefPtr<TypeBuilder::Page::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent) 84 { 85 return TypeBuilder::Page::SearchMatch::create() 86 .setLineNumber(lineNumber) 87 .setLineContent(lineContent) 88 .release(); 89 } 90 91 PassOwnPtr<RegularExpression> createSearchRegex(const String& query, bool caseSensitive, bool isRegex) 92 { 93 String regexSource = isRegex ? query : createSearchRegexSource(query); 94 return adoptPtr(new RegularExpression(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive)); 95 } 96 97 int countRegularExpressionMatches(const RegularExpression* regex, const String& content) 98 { 99 if (content.isEmpty()) 100 return 0; 101 102 int result = 0; 103 int position; 104 unsigned start = 0; 105 int matchLength; 106 while ((position = regex->match(content, start, &matchLength)) != -1) { 107 if (start >= content.length()) 108 break; 109 if (matchLength > 0) 110 ++result; 111 start = position + 1; 112 } 113 return result; 114 } 115 116 PassRefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex) 117 { 118 RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > result = TypeBuilder::Array<TypeBuilder::Page::SearchMatch>::create(); 119 120 OwnPtr<RegularExpression> regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex); 121 Vector<pair<int, String> > matches = getRegularExpressionMatchesByLines(regex.get(), text); 122 123 for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it) 124 result->addItem(buildObjectForSearchMatch(it->first, it->second)); 125 126 return result; 127 } 128 129 static String findMagicComment(const String& content, const String& name, MagicCommentType commentType, bool* deprecated = 0) 130 { 131 ASSERT(name.find("=") == notFound); 132 if (deprecated) 133 *deprecated = false; 134 String pattern; 135 String deprecatedPattern; 136 switch (commentType) { 137 case JavaScriptMagicComment: 138 pattern = "//#[\040\t]" + createSearchRegexSource(name) + "=[\040\t]*([^\\s\'\"]*)[\040\t]*$"; 139 deprecatedPattern = "//@[\040\t]" + createSearchRegexSource(name) + "=[\040\t]*([^\\s\'\"]*)[\040\t]*$"; 140 break; 141 case CSSMagicComment: 142 pattern = "/\\*#[\040\t]" + createSearchRegexSource(name) + "=[\040\t]*([^\\s]*)[\040\t]*\\*/[\040\t]*$"; 143 deprecatedPattern = "/\\*@[\040\t]" + createSearchRegexSource(name) + "=[\040\t]*([^\\s]*)[\040\t]*\\*/[\040\t]*$"; 144 break; 145 default: 146 ASSERT_NOT_REACHED(); 147 return String(); 148 } 149 RegularExpression regex(pattern, TextCaseSensitive, MultilineEnabled); 150 RegularExpression deprecatedRegex(deprecatedPattern, TextCaseSensitive, MultilineEnabled); 151 152 int matchLength; 153 int offset = regex.match(content, 0, &matchLength); 154 if (offset == -1) { 155 offset = deprecatedRegex.match(content, 0, &matchLength); 156 if (offset != -1 && deprecated) 157 *deprecated = true; 158 } 159 if (offset == -1) 160 return String(); 161 162 String match = content.substring(offset, matchLength); 163 size_t separator = match.find("="); 164 ASSERT(separator != notFound); 165 match = match.substring(separator + 1); 166 167 switch (commentType) { 168 case JavaScriptMagicComment: 169 return match.stripWhiteSpace(); 170 case CSSMagicComment: { 171 size_t lastStarIndex = match.reverseFind('*'); 172 ASSERT(lastStarIndex != notFound); 173 return match.substring(0, lastStarIndex).stripWhiteSpace(); 174 } 175 default: 176 ASSERT_NOT_REACHED(); 177 return String(); 178 } 179 } 180 181 String findSourceURL(const String& content, MagicCommentType commentType, bool* deprecated) 182 { 183 return findMagicComment(content, "sourceURL", commentType, deprecated); 184 } 185 186 String findSourceMapURL(const String& content, MagicCommentType commentType, bool* deprecated) 187 { 188 return findMagicComment(content, "sourceMappingURL", commentType, deprecated); 189 } 190 191 } // namespace ContentSearchUtils 192 } // namespace WebCore 193 194