Changeset 96223 in webkit


Ignore:
Timestamp:
Sep 28, 2011 8:01:41 AM (13 years ago)
Author:
vsevik@chromium.org
Message:

Web Inspector: Add support for search in single resource to page agent.
https://bugs.webkit.org/show_bug.cgi?id=68998

Reviewed by Pavel Feldman.

Source/WebCore:

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

  • inspector/ContentSearchUtils.cpp:

(WebCore::ContentSearchUtils::getRegularExpressionMatchesByLines):

  • inspector/ContentSearchUtils.h:
  • inspector/Inspector.json:
  • inspector/InspectorPageAgent.cpp:

(WebCore::buildObjectForSearchMatch):
(WebCore::InspectorPageAgent::searchInResource):

  • inspector/InspectorPageAgent.h:
  • inspector/front-end/Resource.js:

(WebInspector.Resource.prototype.searchInContent):

  • inspector/front-end/SourceFile.js:

(WebInspector.ResourceContentProvider.prototype.requestContent):
(WebInspector.ResourceContentProvider.prototype.searchInContent):

  • inspector/front-end/UISourceCode.js:

(WebInspector.ContentProvider.prototype.requestContent):
(WebInspector.ContentProvider.prototype.searchInContent):

LayoutTests:

  • http/tests/inspector/search/search-in-resource-expected.txt: Added.
  • http/tests/inspector/search/search-in-resource.html: Added.
  • http/tests/inspector/search/search-in-resources.html:
  • http/tests/inspector/search/search-test.js:

(initialize_SearchTest.InspectorTest.dumpSearchResults):
(initialize_SearchTest.InspectorTest.dumpSearchMatches):
(initialize_SearchTest):

Location:
trunk
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r96221 r96223  
     12011-09-28  Vsevolod Vlasov  <vsevik@chromium.org>
     2
     3        Web Inspector: Add support for search in single resource to page agent.
     4        https://bugs.webkit.org/show_bug.cgi?id=68998
     5
     6        Reviewed by Pavel Feldman.
     7
     8        * http/tests/inspector/search/search-in-resource-expected.txt: Added.
     9        * http/tests/inspector/search/search-in-resource.html: Added.
     10        * http/tests/inspector/search/search-in-resources.html:
     11        * http/tests/inspector/search/search-test.js:
     12        (initialize_SearchTest.InspectorTest.dumpSearchResults):
     13        (initialize_SearchTest.InspectorTest.dumpSearchMatches):
     14        (initialize_SearchTest):
     15
    1162011-09-28  Oliver Varga  <voliver@inf.u-szeged.hu>
    217
  • trunk/LayoutTests/http/tests/inspector/search/search-in-resources.html

    r96215 r96223  
    99    // This file should not match search query.
    1010    var text = "searchTest" + "UniqueString";
    11     InspectorTest.runAfterResourcesAreFinished(["search.js", "search.css", "search.html"], step2);
    1211
    13     function step2()
     12    InspectorTest.addResult("Case insensitive, non regex:");
     13    PageAgent.searchInResources(text, false, false, step2);
     14
     15    function step2(error, searchResults)
    1416    {
    15         InspectorTest.addResult("Case insensitive, non regex:");
    16         PageAgent.searchInResources(text, false, false, step3);
     17        InspectorTest.dumpSearchResults(searchResults);
     18        InspectorTest.addResult("Case sensitive, non regex:");
     19        PageAgent.searchInResources(text, true, false, step3);
    1720    }
    1821
     
    2023    {
    2124        InspectorTest.dumpSearchResults(searchResults);
    22         InspectorTest.addResult("Case sensitive, non regex:");
    23         PageAgent.searchInResources(text, true, false, step4);
     25        InspectorTest.addResult("Case insensitive, regex:");
     26        PageAgent.searchInResources(text, false, true, step4);
    2427    }
    2528
     
    2730    {
    2831        InspectorTest.dumpSearchResults(searchResults);
    29         InspectorTest.addResult("Case insensitive, regex:");
    30         PageAgent.searchInResources(text, false, true, step5);
     32        InspectorTest.addResult("Case sensitive, regex:");
     33        PageAgent.searchInResources(text, true, true, step5);
    3134    }
    3235
    3336    function step5(error, searchResults)
    34     {
    35         InspectorTest.dumpSearchResults(searchResults);
    36         InspectorTest.addResult("Case sensitive, regex:");
    37         PageAgent.searchInResources(text, true, true, step6);
    38     }
    39 
    40     function step6(error, searchResults)
    4137    {
    4238        InspectorTest.dumpSearchResults(searchResults);
  • trunk/LayoutTests/http/tests/inspector/search/search-test.js

    r96215 r96223  
    77        InspectorTest.addResult("url: " + searchResults[i].url + ", matchesCount: " + searchResults[i].matchesCount);
    88    InspectorTest.addResult("");
    9 }
     9};
     10
     11InspectorTest.dumpSearchMatches = function(searchMatches)
     12{
     13    InspectorTest.addResult("Search matches: ");
     14    for (var i = 0; i < searchMatches.length; i++)
     15        InspectorTest.addResult("lineNumber: " + searchMatches[i].lineNumber + ", line: '" + searchMatches[i].lineContent + "'");
     16    InspectorTest.addResult("");
     17};
    1018
    1119};
  • trunk/Source/WebCore/ChangeLog

    r96222 r96223  
     12011-09-28  Vsevolod Vlasov  <vsevik@chromium.org>
     2
     3        Web Inspector: Add support for search in single resource to page agent.
     4        https://bugs.webkit.org/show_bug.cgi?id=68998
     5
     6        Reviewed by Pavel Feldman.
     7
     8        Test: http/tests/inspector/search/search-in-resource.html
     9
     10        * inspector/ContentSearchUtils.cpp:
     11        (WebCore::ContentSearchUtils::getRegularExpressionMatchesByLines):
     12        * inspector/ContentSearchUtils.h:
     13        * inspector/Inspector.json:
     14        * inspector/InspectorPageAgent.cpp:
     15        (WebCore::buildObjectForSearchMatch):
     16        (WebCore::InspectorPageAgent::searchInResource):
     17        * inspector/InspectorPageAgent.h:
     18        * inspector/front-end/Resource.js:
     19        (WebInspector.Resource.prototype.searchInContent):
     20        * inspector/front-end/SourceFile.js:
     21        (WebInspector.ResourceContentProvider.prototype.requestContent):
     22        (WebInspector.ResourceContentProvider.prototype.searchInContent):
     23        * inspector/front-end/UISourceCode.js:
     24        (WebInspector.ContentProvider.prototype.requestContent):
     25        (WebInspector.ContentProvider.prototype.searchInContent):
     26
    1272011-09-28  Ilya Tikhonovsky  <loislo@chromium.org>
    228
  • trunk/Source/WebCore/inspector/ContentSearchUtils.cpp

    r96215 r96223  
    3232#if ENABLE(INSPECTOR)
    3333
     34#include "InspectorValues.h"
    3435#include "RegularExpression.h"
     36
     37using namespace std;
    3538
    3639namespace WebCore {
     
    5356        result.append(characters[i]);
    5457    }
     58
     59    return result;
     60}
     61
     62static Vector<pair<int, String> > getRegularExpressionMatchesByLines(const RegularExpression& regex, const String& text)
     63{
     64    Vector<pair<int, String> > result;
     65
     66    int lineNumber = 0;
     67    unsigned start = 0;
     68    while (start < text.length()) {
     69        size_t lineEnd = text.find('\n', start);
     70        if (lineEnd == notFound)
     71            lineEnd = text.length();
     72        else
     73            lineEnd++;
     74
     75        String line = text.substring(start, lineEnd - start);
     76        if (line.endsWith("\r\n"))
     77            line = line.left(line.length() - 2);
     78        if (line.endsWith("\n"))
     79            line = line.left(line.length() - 1);
     80
     81        int matchLength;
     82        if (regex.match(line, 0, &matchLength) != -1)
     83            result.append(pair<int, String>(lineNumber, line));
     84
     85        start = lineEnd;
     86        lineNumber++;
     87    }
     88    return result;
     89}
     90
     91static PassRefPtr<InspectorObject> buildObjectForSearchMatch(int lineNumber, String lineContent)
     92{
     93    RefPtr<InspectorObject> result = InspectorObject::create();
     94    result->setNumber("lineNumber", lineNumber);
     95    result->setString("lineContent", lineContent);
    5596
    5697    return result;
     
    79120}
    80121
     122PassRefPtr<InspectorArray> searchInTextByLines(const String& query, const String& text)
     123{
     124    RefPtr<InspectorArray> result = InspectorArray::create();
     125
     126    RegularExpression regex = ContentSearchUtils::createSearchRegex(query, false, false);
     127    Vector<pair<int, String> > matches = getRegularExpressionMatchesByLines(regex, text);
     128
     129    for (Vector<pair<int, String> >::const_iterator it = matches.begin(); it != matches.end(); ++it)
     130        result->pushValue(buildObjectForSearchMatch(it->first, it->second));
     131
     132    return result;
     133}
     134
    81135} // namespace ContentSearchUtils
    82136} // namespace WebCore
  • trunk/Source/WebCore/inspector/ContentSearchUtils.h

    r96215 r96223  
    3232#include "PlatformString.h"
    3333
     34#include <wtf/Vector.h>
     35
    3436namespace WebCore {
    3537
     38class InspectorArray;
    3639class RegularExpression;
    3740
     
    4043RegularExpression createSearchRegex(const String& text, bool caseSensitive, bool isRegex);
    4144int countRegularExpressionMatches(const RegularExpression&, const String&);
     45PassRefPtr<InspectorArray> searchInTextByLines(const String& query, const String& text);
    4246
    4347} // namespace ContentSearchUtils
  • trunk/Source/WebCore/inspector/Inspector.json

    r96106 r96223  
    103103            },
    104104            {
     105                "id": "SearchMatch",
     106                "type": "object",
     107                "description": "Search match for resource.",
     108                "properties": [
     109                    { "name": "lineNumber", "type": "number", "description": "Line number in resource content." },
     110                    { "name": "lineContent", "type": "string", "description": "Line with match content." }
     111                ],
     112                "hidden": true
     113            },
     114            {
    105115                "id": "SearchResult",
    106116                "type": "object",
     
    186196                    { "name": "content", "type": "string", "description": "Resource content." },
    187197                    { "name": "base64Encoded", "type": "boolean", "description": "True, if content was served as base64." }
     198                ],
     199                "hidden": true
     200            },
     201            {
     202                "name": "searchInResource",
     203                "description": "Searches for given string in resource content.",
     204                "parameters": [
     205                    { "name": "frameId", "type": "string", "description": "Frame id for resource to search in." },
     206                    { "name": "url", "type": "string", "description": "URL of the resource to search in." },
     207                    { "name": "query", "type": "string", "description": "String to search for."  }
     208                ],
     209                "returns": [
     210                    { "name": "result", "type": "array", "items": { "$ref": "SearchMatch" }, "description": "List of search matches." }
    188211                ],
    189212                "hidden": true
  • trunk/Source/WebCore/inspector/InspectorPageAgent.cpp

    r96215 r96223  
    6868#include <wtf/Vector.h>
    6969
     70using namespace std;
     71
    7072namespace WebCore {
    7173
     
    483485}
    484486
     487void InspectorPageAgent::searchInResource(ErrorString*, const String& frameId, const String& url, const String& query, RefPtr<InspectorArray>* object)
     488{
     489    RefPtr<InspectorArray> result = InspectorArray::create();
     490
     491    Frame* frame = frameForId(frameId);
     492    KURL kurl(ParsedURLString, url);
     493
     494    FrameLoader* frameLoader = frame ? frame->loader() : 0;
     495    DocumentLoader* loader = frameLoader ? frameLoader->documentLoader() : 0;
     496    if (!loader) {
     497        *object = result;
     498        return;
     499    }
     500
     501    String content;
     502    bool success = false;
     503    if (equalIgnoringFragmentIdentifier(kurl, loader->url()))
     504        success = mainResourceContent(frame, false, &content);
     505
     506    if (!success) {
     507        CachedResource* resource = cachedResource(frame, kurl);
     508        success = textContentForCachedResource(resource, &content);
     509    }
     510
     511    if (!success) {
     512        *object = result;
     513        return;
     514    }
     515
     516    result = ContentSearchUtils::searchInTextByLines(query, content);
     517    *object = result;
     518}
     519
    485520static PassRefPtr<InspectorObject> buildObjectForSearchResult(const String& frameId, const String& url, int matchesCount)
    486521{
  • trunk/Source/WebCore/inspector/InspectorPageAgent.h

    r95901 r96223  
    9898    void getResourceTree(ErrorString*, RefPtr<InspectorObject>*);
    9999    void getResourceContent(ErrorString*, const String& frameId, const String& url, String* content, bool* base64Encoded);
     100    void searchInResource(ErrorString*, const String& frameId, const String& url, const String&, RefPtr<InspectorArray>*);
    100101    void searchInResources(ErrorString*, const String&, const bool* const caseSensitive, const bool* const isRegex, RefPtr<InspectorArray>*);
    101102
  • trunk/Source/WebCore/inspector/front-end/Resource.js

    r94754 r96223  
    868868    },
    869869
     870    searchInContent: function(query, callback)
     871    {
     872        PageAgent.searchInResource(query, callback);
     873    },
     874
    870875    populateImageSource: function(image)
    871876    {
  • trunk/Source/WebCore/inspector/front-end/SourceFile.js

    r96097 r96223  
    359359        }
    360360        this._resource.requestContent(didRequestContent.bind(this));
    361     }
     361    },
     362   
     363    searchInContent: function(query, callback)
     364    {
     365        this._resource.searchInContent(query, callback);
     366    }   
    362367}
    363368
  • trunk/Source/WebCore/inspector/front-end/UISourceCode.js

    r93969 r96223  
    9191WebInspector.ContentProvider = function() { }
    9292WebInspector.ContentProvider.prototype = {
    93     requestContent: function(callback) { }
     93    requestContent: function(callback) { },
     94    searchInContent: function(query, callback) { }
    9495}
Note: See TracChangeset for help on using the changeset viewer.