Web Inspector: Refactor searching in inspector, add test (fixed non regex search).
authorvsevik@chromium.org <vsevik@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Sep 2011 11:56:36 +0000 (11:56 +0000)
committervsevik@chromium.org <vsevik@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Sep 2011 11:56:36 +0000 (11:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=68930

Reviewed by Yury Semikhatsky.

Source/WebCore:

Search moved out from page agent.
Fixed search for non regex case which is not yet used.

Test: http/tests/inspector/search/search-in-resources.html

* CMakeLists.txt:
* GNUmakefile.list.am:
* WebCore.gypi:
* WebCore.pro:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:
* inspector/ContentSearchUtils.cpp: Added.
(WebCore::ContentSearchUtils::createSearchRegexSource):
(WebCore::ContentSearchUtils::createSearchRegex):
(WebCore::ContentSearchUtils::countRegularExpressionMatches):
* inspector/ContentSearchUtils.h: Added.
* inspector/InspectorPageAgent.cpp:
(WebCore::hasTextContent):
(WebCore::InspectorPageAgent::cachedResourceContent):
(WebCore::textContentForCachedResource):
(WebCore::buildObjectForSearchResult):
(WebCore::InspectorPageAgent::searchInResources):
* inspector/front-end/utilities.js:
():

LayoutTests:

* http/tests/inspector/search/resources/search.css: Added.
(div.searchTestUniqueString):
(div.searchTestUniqueString:hover):
* http/tests/inspector/search/resources/search.html: Added.
* http/tests/inspector/search/resources/search.js: Added.
(searchTestUniqueString):
(doSomething):
* http/tests/inspector/search/search-in-resources-expected.txt: Added.
* http/tests/inspector/search/search-in-resources.html: Added.
* http/tests/inspector/search/search-test.js: Added.
(initialize_SearchTest.InspectorTest.dumpSearchResults):
(initialize_SearchTest):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@96215 268f45cc-cd09-0410-ab3c-d52691b4dbfc

18 files changed:
LayoutTests/ChangeLog
LayoutTests/http/tests/inspector/search/resources/search.css [new file with mode: 0644]
LayoutTests/http/tests/inspector/search/resources/search.html [new file with mode: 0644]
LayoutTests/http/tests/inspector/search/resources/search.js [new file with mode: 0644]
LayoutTests/http/tests/inspector/search/search-in-resources-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/inspector/search/search-in-resources.html [new file with mode: 0644]
LayoutTests/http/tests/inspector/search/search-test.js [new file with mode: 0644]
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/GNUmakefile.list.am
Source/WebCore/WebCore.gypi
Source/WebCore/WebCore.pro
Source/WebCore/WebCore.vcproj/WebCore.vcproj
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/inspector/ContentSearchUtils.cpp [new file with mode: 0644]
Source/WebCore/inspector/ContentSearchUtils.h [new file with mode: 0644]
Source/WebCore/inspector/InspectorPageAgent.cpp
Source/WebCore/inspector/front-end/utilities.js

index f58a0ae..e91af53 100644 (file)
@@ -1,3 +1,23 @@
+2011-09-28  Vsevolod Vlasov  <vsevik@chromium.org>
+
+        Web Inspector: Refactor searching in inspector, add test (fixed non regex search).
+        https://bugs.webkit.org/show_bug.cgi?id=68930
+
+        Reviewed by Yury Semikhatsky.
+
+        * http/tests/inspector/search/resources/search.css: Added.
+        (div.searchTestUniqueString):
+        (div.searchTestUniqueString:hover):
+        * http/tests/inspector/search/resources/search.html: Added.
+        * http/tests/inspector/search/resources/search.js: Added.
+        (searchTestUniqueString):
+        (doSomething):
+        * http/tests/inspector/search/search-in-resources-expected.txt: Added.
+        * http/tests/inspector/search/search-in-resources.html: Added.
+        * http/tests/inspector/search/search-test.js: Added.
+        (initialize_SearchTest.InspectorTest.dumpSearchResults):
+        (initialize_SearchTest):
+
 2011-09-28  Kentaro Hara  <haraken@chromium.org>
 
         Implement a PopStateEvent constructor for V8
diff --git a/LayoutTests/http/tests/inspector/search/resources/search.css b/LayoutTests/http/tests/inspector/search/resources/search.css
new file mode 100644 (file)
index 0000000..0760c9b
--- /dev/null
@@ -0,0 +1,9 @@
+div.searchTestUniqueString {
+    background-color: red;
+}
+
+div.searchTestUniqueString:hover {
+    /* another searchTestUniqueString occurence */
+    background-color: red;
+}
+
diff --git a/LayoutTests/http/tests/inspector/search/resources/search.html b/LayoutTests/http/tests/inspector/search/resources/search.html
new file mode 100644 (file)
index 0000000..6ea888d
--- /dev/null
@@ -0,0 +1,13 @@
+<html>
+<head>
+<link href="search.css" rel="stylesheet" type="text/css">
+<script src="search.js"></script>
+<body>
+<div>searchTestUniqueString</div>
+<div>other text</div>
+<!-- searchTestUniqueString -->
+<div>some other text</div>
+<div id="searchTestUniqueString">div text</div>
+</body>
+</html>
+
diff --git a/LayoutTests/http/tests/inspector/search/resources/search.js b/LayoutTests/http/tests/inspector/search/resources/search.js
new file mode 100644 (file)
index 0000000..3593ee1
--- /dev/null
@@ -0,0 +1,13 @@
+function searchTestUniqueString()
+{
+    var variable = 0;
+    // searchTestUniqueString two occurences on the same line searchTestUniqueString
+    var variable2 = 0;
+}
+
+function doSomething()
+{
+    searchTestUniqueString();
+    // SEARCHTestUniqueString();
+}
+
diff --git a/LayoutTests/http/tests/inspector/search/search-in-resources-expected.txt b/LayoutTests/http/tests/inspector/search/search-in-resources-expected.txt
new file mode 100644 (file)
index 0000000..a2afec7
--- /dev/null
@@ -0,0 +1,28 @@
+Tests backend search in inspector.
+
+Bug 61205  
+Case insensitive, non regex:
+Search results: 
+url: http://127.0.0.1:8000/inspector/search/resources/search.css, matchesCount: 3
+url: http://127.0.0.1:8000/inspector/search/resources/search.js, matchesCount: 5
+url: http://127.0.0.1:8000/inspector/search/resources/search.html, matchesCount: 3
+
+Case sensitive, non regex:
+Search results: 
+url: http://127.0.0.1:8000/inspector/search/resources/search.css, matchesCount: 3
+url: http://127.0.0.1:8000/inspector/search/resources/search.js, matchesCount: 4
+url: http://127.0.0.1:8000/inspector/search/resources/search.html, matchesCount: 3
+
+Case insensitive, regex:
+Search results: 
+url: http://127.0.0.1:8000/inspector/search/resources/search.css, matchesCount: 3
+url: http://127.0.0.1:8000/inspector/search/resources/search.js, matchesCount: 5
+url: http://127.0.0.1:8000/inspector/search/resources/search.html, matchesCount: 3
+
+Case sensitive, regex:
+Search results: 
+url: http://127.0.0.1:8000/inspector/search/resources/search.css, matchesCount: 3
+url: http://127.0.0.1:8000/inspector/search/resources/search.js, matchesCount: 4
+url: http://127.0.0.1:8000/inspector/search/resources/search.html, matchesCount: 3
+
+
diff --git a/LayoutTests/http/tests/inspector/search/search-in-resources.html b/LayoutTests/http/tests/inspector/search/search-in-resources.html
new file mode 100644 (file)
index 0000000..c17b346
--- /dev/null
@@ -0,0 +1,54 @@
+<html>
+<head>
+<script src="../inspector-test.js"></script>
+<script src="../resource-tree/resource-tree-test.js"></script>
+<script src="search-test.js"></script>
+<script>
+function test()
+{
+    // This file should not match search query.
+    var text = "searchTest" + "UniqueString";
+    InspectorTest.runAfterResourcesAreFinished(["search.js", "search.css", "search.html"], step2);
+
+    function step2()
+    {
+        InspectorTest.addResult("Case insensitive, non regex:");
+        PageAgent.searchInResources(text, false, false, step3);
+    }
+
+    function step3(error, searchResults)
+    {
+        InspectorTest.dumpSearchResults(searchResults);
+        InspectorTest.addResult("Case sensitive, non regex:");
+        PageAgent.searchInResources(text, true, false, step4);
+    }
+
+    function step4(error, searchResults)
+    {
+        InspectorTest.dumpSearchResults(searchResults);
+        InspectorTest.addResult("Case insensitive, regex:");
+        PageAgent.searchInResources(text, false, true, step5);
+    }
+
+    function step5(error, searchResults)
+    {
+        InspectorTest.dumpSearchResults(searchResults);
+        InspectorTest.addResult("Case sensitive, regex:");
+        PageAgent.searchInResources(text, true, true, step6);
+    }
+
+    function step6(error, searchResults)
+    {
+        InspectorTest.dumpSearchResults(searchResults);
+        InspectorTest.completeTest();
+    }
+}
+</script>
+</head>
+<body>
+<p>Tests backend search in inspector.</p>
+<a href="https://bugs.webkit.org/show_bug.cgi?id=61205">Bug 61205</a>
+
+<iframe src="resources/search.html" onload="runTest()">
+</body>
+</html>
diff --git a/LayoutTests/http/tests/inspector/search/search-test.js b/LayoutTests/http/tests/inspector/search/search-test.js
new file mode 100644 (file)
index 0000000..9dd1975
--- /dev/null
@@ -0,0 +1,11 @@
+var initialize_SearchTest = function() {
+
+InspectorTest.dumpSearchResults = function(searchResults)
+{
+    InspectorTest.addResult("Search results: ");
+    for (var i = 0; i < searchResults.length; i++)
+        InspectorTest.addResult("url: " + searchResults[i].url + ", matchesCount: " + searchResults[i].matchesCount);
+    InspectorTest.addResult("");
+}
+
+};
index e1eee73..a3df571 100644 (file)
@@ -853,6 +853,7 @@ SET(WebCore_SOURCES
     html/shadow/TextControlInnerElements.cpp
 
     inspector/ConsoleMessage.cpp
+    inspector/ContentSearchUtils.cpp
     inspector/DOMNodeHighlighter.cpp
     inspector/IdentifiersFactory.cpp
     inspector/InjectedScript.cpp
index 05888ce..613b408 100644 (file)
@@ -1,3 +1,35 @@
+2011-09-28  Vsevolod Vlasov  <vsevik@chromium.org>
+
+        Web Inspector: Refactor searching in inspector, add test (fixed non regex search).
+        https://bugs.webkit.org/show_bug.cgi?id=68930
+
+        Reviewed by Yury Semikhatsky.
+
+        Search moved out from page agent.
+        Fixed search for non regex case which is not yet used.
+
+        Test: http/tests/inspector/search/search-in-resources.html
+
+        * CMakeLists.txt:
+        * GNUmakefile.list.am:
+        * WebCore.gypi:
+        * WebCore.pro:
+        * WebCore.vcproj/WebCore.vcproj:
+        * WebCore.xcodeproj/project.pbxproj:
+        * inspector/ContentSearchUtils.cpp: Added.
+        (WebCore::ContentSearchUtils::createSearchRegexSource):
+        (WebCore::ContentSearchUtils::createSearchRegex):
+        (WebCore::ContentSearchUtils::countRegularExpressionMatches):
+        * inspector/ContentSearchUtils.h: Added.
+        * inspector/InspectorPageAgent.cpp:
+        (WebCore::hasTextContent):
+        (WebCore::InspectorPageAgent::cachedResourceContent):
+        (WebCore::textContentForCachedResource):
+        (WebCore::buildObjectForSearchResult):
+        (WebCore::InspectorPageAgent::searchInResources):
+        * inspector/front-end/utilities.js:
+        ():
+
 2011-09-28  Kentaro Hara  <haraken@chromium.org>
 
         Implement a PopStateEvent constructor for V8
index 7436ecf..83929c3 100644 (file)
@@ -1975,6 +1975,8 @@ webcore_sources += \
        Source/WebCore/icu/unicode/uversion.h \
        Source/WebCore/inspector/ConsoleMessage.cpp \
        Source/WebCore/inspector/ConsoleMessage.h \
+       Source/WebCore/inspector/ContentSearchUtils.cpp \
+       Source/WebCore/inspector/ContentSearchUtils.h \
        Source/WebCore/inspector/DOMNodeHighlighter.cpp \
        Source/WebCore/inspector/DOMNodeHighlighter.h \
        Source/WebCore/inspector/IdentifiersFactory.cpp \
index bee98c3..04204ea 100644 (file)
             'icu/unicode/uversion.h',
             'inspector/ConsoleMessage.cpp',
             'inspector/ConsoleMessage.h',
+            'inspector/ContentSearchUtils.cpp',
+            'inspector/ContentSearchUtils.h',
             'inspector/DOMNodeHighlighter.cpp',
             'inspector/DOMNodeHighlighter.h',
             'inspector/IdentifiersFactory.cpp',
index 79ae403..71b89e9 100644 (file)
@@ -812,6 +812,7 @@ SOURCES += \
     html/shadow/SliderThumbElement.cpp \
     html/shadow/TextControlInnerElements.cpp \
     inspector/ConsoleMessage.cpp \
+    inspector/ContentSearchUtils.cpp \
     inspector/DOMNodeHighlighter.cpp \
     inspector/IdentifiersFactory.cpp \
     inspector/InjectedScript.cpp \
@@ -1804,6 +1805,7 @@ HEADERS += \
     html/track/WebVTTToken.h \
     html/track/WebVTTTokenizer.h \
     inspector/ConsoleMessage.h \
+    inspector/ContentSearchUtils.h \
     inspector/DOMNodeHighlighter.h \
     inspector/IdentifiersFactory.h \
     inspector/InjectedScript.h \
index a11b47e..5a9ddaf 100755 (executable)
                                >
                        </File>
                        <File
+                               RelativePath="..\inspector\ContentSearchUtils.cpp"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\inspector\ContentSearchUtils.h"
+                               >
+                       </File>
+                       <File
                                RelativePath="..\inspector\DOMNodeHighlighter.cpp"
                                >
                        </File>
index a3dab97..34ea754 100644 (file)
                5905ADC01302F3CE00F116DF /* XMLTreeViewer.h in Headers */ = {isa = PBXBuildFile; fileRef = 5905ADBE1302F3CE00F116DF /* XMLTreeViewer.h */; };
                590E1B4911E4EF4B0069F784 /* DeviceOrientation.h in Headers */ = {isa = PBXBuildFile; fileRef = 590E1B4811E4EF4B0069F784 /* DeviceOrientation.h */; settings = {ATTRIBUTES = (Private, ); }; };
                590E1B4B11E4EF700069F784 /* JSDeviceOrientationEventCustom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 590E1B4A11E4EF700069F784 /* JSDeviceOrientationEventCustom.cpp */; };
+               59102FBB14327D3B003C9D04 /* ContentSearchUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59102FB914327D3B003C9D04 /* ContentSearchUtils.cpp */; };
+               59102FBC14327D3B003C9D04 /* ContentSearchUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 59102FBA14327D3B003C9D04 /* ContentSearchUtils.h */; };
                5913A24113D49EBA00F5B05C /* IdentifiersFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5913A23F13D49EBA00F5B05C /* IdentifiersFactory.cpp */; };
                5913A24213D49EBA00F5B05C /* IdentifiersFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 5913A24013D49EBA00F5B05C /* IdentifiersFactory.h */; };
                59309A1111F4AE5800250603 /* DeviceOrientationClientMock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59309A1011F4AE5800250603 /* DeviceOrientationClientMock.cpp */; };
                5905ADBE1302F3CE00F116DF /* XMLTreeViewer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMLTreeViewer.h; sourceTree = "<group>"; };
                590E1B4811E4EF4B0069F784 /* DeviceOrientation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceOrientation.h; sourceTree = "<group>"; };
                590E1B4A11E4EF700069F784 /* JSDeviceOrientationEventCustom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSDeviceOrientationEventCustom.cpp; sourceTree = "<group>"; };
+               59102FB914327D3B003C9D04 /* ContentSearchUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContentSearchUtils.cpp; sourceTree = "<group>"; };
+               59102FBA14327D3B003C9D04 /* ContentSearchUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentSearchUtils.h; sourceTree = "<group>"; };
                5913A23F13D49EBA00F5B05C /* IdentifiersFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IdentifiersFactory.cpp; sourceTree = "<group>"; };
                5913A24013D49EBA00F5B05C /* IdentifiersFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IdentifiersFactory.h; sourceTree = "<group>"; };
                59309A1011F4AE5800250603 /* DeviceOrientationClientMock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DeviceOrientationClientMock.cpp; path = mock/DeviceOrientationClientMock.cpp; sourceTree = "<group>"; };
                                1C81B9590E97330800266E07 /* front-end */,
                                41F0618D0F5F069800A07EAC /* ConsoleMessage.cpp */,
                                41F0618C0F5F069800A07EAC /* ConsoleMessage.h */,
+                               59102FB914327D3B003C9D04 /* ContentSearchUtils.cpp */,
+                               59102FBA14327D3B003C9D04 /* ContentSearchUtils.h */,
                                4F1442261339FD6200E0D6F8 /* DOMNodeHighlighter.cpp */,
                                4F1442271339FD6200E0D6F8 /* DOMNodeHighlighter.h */,
                                5913A23F13D49EBA00F5B05C /* IdentifiersFactory.cpp */,
                                1A927FD41416A15B003A83C8 /* nptypes.h in Headers */,
                                CECCFC3B141973D5002A0AC1 /* DecodeEscapeSequences.h in Headers */,
                                0FE71406142170B800DB33BA /* ScrollbarThemeMock.h in Headers */,
+                               59102FBC14327D3B003C9D04 /* ContentSearchUtils.h in Headers */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                                A85F22091430377D007CC884 /* JSPopStateEventCustom.cpp in Sources */,
                                319AE063142D6B24006563A1 /* StyleFilterData.cpp in Sources */,
                                319AE069142D78DD006563A1 /* FilterOperations.cpp in Sources */,
+                               59102FBB14327D3B003C9D04 /* ContentSearchUtils.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/Source/WebCore/inspector/ContentSearchUtils.cpp b/Source/WebCore/inspector/ContentSearchUtils.cpp
new file mode 100644 (file)
index 0000000..4cc22ab
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2011 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
+ * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "ContentSearchUtils.h"
+
+#if ENABLE(INSPECTOR)
+
+#include "RegularExpression.h"
+
+namespace WebCore {
+namespace ContentSearchUtils {
+
+namespace {
+// This should be kept the same as the one in front-end/utilities.js
+static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
+}
+
+static String createSearchRegexSource(const String& text)
+{
+    String result;
+    const UChar* characters = text.characters();
+    String specials(regexSpecialCharacters);
+
+    for (unsigned i = 0; i < text.length(); i++) {
+        if (specials.find(characters[i]) != notFound)
+            result.append("\\");
+        result.append(characters[i]);
+    }
+
+    return result;
+}
+
+RegularExpression createSearchRegex(const String& text, bool caseSensitive, bool isRegex)
+{
+    String regexSource = isRegex ? text : createSearchRegexSource(text);
+    return RegularExpression(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive);
+}
+
+int countRegularExpressionMatches(const RegularExpression& regex, const String& content)
+{
+    int result = 0;
+    int position;
+    unsigned start = 0;
+    int matchLength;
+    while ((position = regex.match(content, start, &matchLength)) != -1) {
+        if (start >= content.length())
+            break;
+        if (matchLength > 0)
+            ++result;
+        start = position + 1;
+    }
+    return result;
+}
+
+} // namespace ContentSearchUtils
+} // namespace WebCore
+
+#endif // ENABLE(INSPECTOR)
diff --git a/Source/WebCore/inspector/ContentSearchUtils.h b/Source/WebCore/inspector/ContentSearchUtils.h
new file mode 100644 (file)
index 0000000..6515450
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2011 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
+ * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ContentSearchUtils_h
+#define ContentSearchUtils_h
+
+#include "PlatformString.h"
+
+namespace WebCore {
+
+class RegularExpression;
+
+namespace ContentSearchUtils {
+
+RegularExpression createSearchRegex(const String& text, bool caseSensitive, bool isRegex);
+int countRegularExpressionMatches(const RegularExpression&, const String&);
+
+} // namespace ContentSearchUtils
+} // namespace WebCore
+
+#endif // !defined(ContentSearchUtils_h)
index 9792d0c..9e9aed6 100644 (file)
@@ -39,6 +39,7 @@
 #include "CachedResource.h"
 #include "CachedResourceLoader.h"
 #include "CachedScript.h"
+#include "ContentSearchUtils.h"
 #include "Cookie.h"
 #include "CookieJar.h"
 #include "Document.h"
 
 namespace WebCore {
 
-namespace {
-// This should be kept the same as the one in front-end/utilities.js
-static const char regexSpecialCharacters[] = "[](){}+-*.,?\\^$|";
-}
-
 namespace PageAgentState {
 static const char pageAgentEnabled[] = "resourceAgentEnabled";
 }
@@ -115,6 +111,12 @@ static bool prepareCachedResourceBuffer(CachedResource* cachedResource, bool* ha
     return true;
 }
 
+static bool hasTextContent(CachedResource* cachedResource)
+{
+    InspectorPageAgent::ResourceType type = InspectorPageAgent::cachedResourceType(*cachedResource);
+    return type == InspectorPageAgent::StylesheetResource || type == InspectorPageAgent::ScriptResource;
+}
+
 bool InspectorPageAgent::cachedResourceContent(CachedResource* cachedResource, String* result, bool* base64Encoded)
 {
     bool hasZeroSize;
@@ -122,9 +124,7 @@ bool InspectorPageAgent::cachedResourceContent(CachedResource* cachedResource, S
     if (!prepared)
         return false;
 
-    ResourceType type = cachedResourceType(*cachedResource);
-    *base64Encoded = type != StylesheetResource && type != ScriptResource;
-
+    *base64Encoded = !hasTextContent(cachedResource);
     if (*base64Encoded) {
         RefPtr<SharedBuffer> buffer = hasZeroSize ? SharedBuffer::create() : cachedResource->data();
 
@@ -469,38 +469,20 @@ void InspectorPageAgent::getResourceContent(ErrorString* errorString, const Stri
     resourceContent(errorString, frame, KURL(ParsedURLString, url), content, base64Encoded);
 }
 
-static String createSearchRegexSource(const String& text)
-{
-    String result;
-    const UChar* characters = text.characters();
-    String specials(regexSpecialCharacters);
-
-    for (unsigned i = 0; i < text.length(); i++) {
-        if (specials.find(characters[i]))
-            result.append("\\");
-        result.append(characters[i]);
-    }
-
-    return result;
-}
-
-static int countRegularExpressionMatches(const RegularExpression& regex, const String& content)
+static bool textContentForCachedResource(CachedResource* cachedResource, String* result)
 {
-    int result = 0;
-    int position;
-    unsigned start = 0;
-    int matchLength;
-    while ((position = regex.match(content, start, &matchLength)) != -1) {
-        if (start >= content.length())
-            break;
-        if (matchLength > 0)
-            ++result;
-        start = position + 1;
+    if (hasTextContent(cachedResource)) {
+        String content;
+        bool base64Encoded;
+        if (InspectorPageAgent::cachedResourceContent(cachedResource, result, &base64Encoded)) {
+            ASSERT(!base64Encoded);
+            return true;
+        }
     }
-    return result;
+    return false;
 }
 
-static PassRefPtr<InspectorObject> buildObjectForSearchMatch(const String& frameId, const String& url, int matchesCount)
+static PassRefPtr<InspectorObject> buildObjectForSearchResult(const String& frameId, const String& url, int matchesCount)
 {
     RefPtr<InspectorObject> result = InspectorObject::create();
     result->setString("frameId", frameId);
@@ -515,35 +497,24 @@ void InspectorPageAgent::searchInResources(ErrorString*, const String& text, con
     RefPtr<InspectorArray> result = InspectorArray::create();
 
     bool isRegex = optionalIsRegex ? *optionalIsRegex : false;
-    String regexSource = isRegex ? text : createSearchRegexSource(text);
-
     bool caseSensitive = optionalCaseSensitive ? *optionalCaseSensitive : false;
-    RegularExpression regex(regexSource, caseSensitive ? TextCaseSensitive : TextCaseInsensitive);
+    RegularExpression regex = ContentSearchUtils::createSearchRegex(text, caseSensitive, isRegex);
 
     for (Frame* frame = m_page->mainFrame(); frame; frame = frame->tree()->traverseNext(m_page->mainFrame())) {
         String content;
-        bool base64Encoded;
         Vector<CachedResource*> allResources = cachedResourcesForFrame(frame);
         for (Vector<CachedResource*>::const_iterator it = allResources.begin(); it != allResources.end(); ++it) {
             CachedResource* cachedResource = *it;
-            switch (InspectorPageAgent::cachedResourceType(*cachedResource)) {
-            case InspectorPageAgent::StylesheetResource:
-            case InspectorPageAgent::ScriptResource:
-                if (cachedResourceContent(cachedResource, &content, &base64Encoded)) {
-                    ASSERT(!base64Encoded);
-                    int matchesCount = countRegularExpressionMatches(regex, content);
-                    if (matchesCount)
-                        result->pushValue(buildObjectForSearchMatch(frameId(frame), cachedResource->url(), matchesCount));
-                }
-                break;
-            default:
-                break;
+            if (textContentForCachedResource(cachedResource, &content)) {
+                int matchesCount = ContentSearchUtils::countRegularExpressionMatches(regex, content);
+                if (matchesCount)
+                    result->pushValue(buildObjectForSearchResult(frameId(frame), cachedResource->url(), matchesCount));
             }
         }
         if (mainResourceContent(frame, false, &content)) {
-            int matchesCount = countRegularExpressionMatches(regex, content);
+            int matchesCount = ContentSearchUtils::countRegularExpressionMatches(regex, content);
             if (matchesCount)
-                result->pushValue(buildObjectForSearchMatch(frameId(frame), frame->document()->url(), matchesCount));
+                result->pushValue(buildObjectForSearchResult(frameId(frame), frame->document()->url(), matchesCount));
         }
     }
 
index 9679067..c14f438 100644 (file)
@@ -961,7 +961,7 @@ function revertDomChanges(domChanges)
 
 function createSearchRegex(query, extraFlags)
 {
-    // This should be kept the same as the one in InspectorPageAgent.cpp.
+    // This should be kept the same as the one in ContentSearchUtils.cpp.
     var regexSpecialCharacters = "[](){}+-*.,?\\^$|";
     var regex = "";
     for (var i = 0; i < query.length; ++i) {