ContentSearchUtils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "config.h"
  29. #if ENABLE(INSPECTOR)
  30. #include "ContentSearchUtils.h"
  31. #include "InspectorValues.h"
  32. #include "RegularExpression.h"
  33. #include <wtf/BumpPointerAllocator.h>
  34. #include <wtf/StdLibExtras.h>
  35. #include <yarr/Yarr.h>
  36. using namespace std;
  37. namespace WebCore {
  38. namespace ContentSearchUtils {
  39. namespace {
  40. // This should be kept the same as the one in front-end/utilities.js
  41. static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
  42. }
  43. static String createSearchRegexSource(const String& text)
  44. {
  45. String result;
  46. const UChar* characters = text.characters();
  47. String specials(regexSpecialCharacters);
  48. for (unsigned i = 0; i < text.length(); i++) {
  49. if (specials.find(characters[i]) != notFound)
  50. result.append("\\");
  51. result.append(characters[i]);
  52. }
  53. return result;
  54. }
  55. static inline size_t sizetExtractor(const size_t* value)
  56. {
  57. return *value;
  58. }
  59. TextPosition textPositionFromOffset(size_t offset, const Vector<size_t>& lineEndings)
  60. {
  61. const size_t* foundLineEnding = approximateBinarySearch<size_t, size_t>(lineEndings, lineEndings.size(), offset, sizetExtractor);
  62. size_t lineIndex = foundLineEnding - &lineEndings.at(0);
  63. if (offset > *foundLineEnding)
  64. ++lineIndex;
  65. size_t lineStartOffset = lineIndex > 0 ? lineEndings.at(lineIndex - 1) + 1 : 0;
  66. size_t column = offset - lineStartOffset;
  67. return TextPosition(OrdinalNumber::fromZeroBasedInt(lineIndex), OrdinalNumber::fromZeroBasedInt(column));
  68. }
  69. static Vector<pair<int, String> > getRegularExpressionMatchesByLines(const RegularExpression& regex, const String& text)
  70. {
  71. Vector<pair<int, String> > result;
  72. if (text.isEmpty())
  73. return result;
  74. OwnPtr<Vector<size_t> > endings(lineEndings(text));
  75. size_t size = endings->size();
  76. unsigned start = 0;
  77. for (size_t lineNumber = 0; lineNumber < size; ++lineNumber) {
  78. size_t lineEnd = endings->at(lineNumber);
  79. String line = text.substring(start, lineEnd - start);
  80. if (line.endsWith('\r'))
  81. line = line.left(line.length() - 1);
  82. int matchLength;
  83. if (regex.match(line, 0, &matchLength) != -1)
  84. result.append(pair<int, String>(lineNumber, line));
  85. start = lineEnd + 1;
  86. }
  87. return result;
  88. }
  89. PassOwnPtr<Vector<size_t> > lineEndings(const String& text)
  90. {
  91. OwnPtr<Vector<size_t> > result(adoptPtr(new Vector<size_t>()));
  92. unsigned start = 0;
  93. while (start < text.length()) {
  94. size_t lineEnd = text.find('\n', start);
  95. if (lineEnd == notFound)
  96. break;
  97. result->append(lineEnd);
  98. start = lineEnd + 1;
  99. }
  100. result->append(text.length());
  101. return result.release();
  102. }
  103. static PassRefPtr<TypeBuilder::Page::SearchMatch> buildObjectForSearchMatch(int lineNumber, const String& lineContent)
  104. {
  105. return TypeBuilder::Page::SearchMatch::create()
  106. .setLineNumber(lineNumber)
  107. .setLineContent(lineContent)
  108. .release();
  109. }
  110. RegularExpression createSearchRegex(const String& query, bool caseSensitive, bool isRegex)
  111. {
  112. String regexSource = isRegex ? query : createSearchRegexSource(query);
  113. return RegularExpression(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive);
  114. }
  115. int countRegularExpressionMatches(const RegularExpression& regex, const String& content)
  116. {
  117. if (content.isEmpty())
  118. return 0;
  119. int result = 0;
  120. int position;
  121. unsigned start = 0;
  122. int matchLength;
  123. while ((position = regex.match(content, start, &matchLength)) != -1) {
  124. if (start >= content.length())
  125. break;
  126. if (matchLength > 0)
  127. ++result;
  128. start = position + 1;
  129. }
  130. return result;
  131. }
  132. PassRefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > searchInTextByLines(const String& text, const String& query, const bool caseSensitive, const bool isRegex)
  133. {
  134. RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> > result = TypeBuilder::Array<TypeBuilder::Page::SearchMatch>::create();
  135. RegularExpression regex = ContentSearchUtils::createSearchRegex(query, caseSensitive, isRegex);
  136. Vector<pair<int, String> > matches = getRegularExpressionMatchesByLines(regex, text);
  137. for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
  138. result->addItem(buildObjectForSearchMatch(it->first, it->second));
  139. return result;
  140. }
  141. static String scriptCommentPattern(const String& name)
  142. {
  143. // "//# <name>=<value>" and deprecated "//@"
  144. return "//[#@][\040\t]" + name + "=[\040\t]*([^\\s\'\"]*)[\040\t]*$";
  145. }
  146. static String stylesheetCommentPattern(const String& name)
  147. {
  148. // "/*# <name>=<value> */" and deprecated "/*@"
  149. return "/\\*[#@][\040\t]" + name + "=[\040\t]*([^\\s\'\"]*)[\040\t]*\\*/";
  150. }
  151. static String findMagicComment(const String& content, const String& patternString)
  152. {
  153. const char* error = 0;
  154. JSC::Yarr::YarrPattern pattern(patternString, false, true, &error);
  155. ASSERT(!error);
  156. BumpPointerAllocator regexAllocator;
  157. OwnPtr<JSC::Yarr::BytecodePattern> bytecodePattern = JSC::Yarr::byteCompile(pattern, &regexAllocator);
  158. ASSERT(bytecodePattern);
  159. ASSERT(pattern.m_numSubpatterns == 1);
  160. Vector<int, 4> matches;
  161. matches.resize(4);
  162. unsigned result = JSC::Yarr::interpret(bytecodePattern.get(), content, 0, reinterpret_cast<unsigned*>(matches.data()));
  163. if (result == JSC::Yarr::offsetNoMatch)
  164. return String();
  165. ASSERT(matches[2] > 0 && matches[3] > 0);
  166. return content.substring(matches[2], matches[3] - matches[2]);
  167. }
  168. String findScriptSourceURL(const String& content)
  169. {
  170. return findMagicComment(content, scriptCommentPattern("sourceURL"));
  171. }
  172. String findScriptSourceMapURL(const String& content)
  173. {
  174. return findMagicComment(content, scriptCommentPattern("sourceMappingURL"));
  175. }
  176. String findStylesheetSourceMapURL(const String& content)
  177. {
  178. return findMagicComment(content, stylesheetCommentPattern("sourceMappingURL"));
  179. }
  180. } // namespace ContentSearchUtils
  181. } // namespace WebCore
  182. #endif // ENABLE(INSPECTOR)