-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented DebuggerAgent.searchInContent. Implemented PageAgent.searchInResource. Added /lib/search.js to provide shared search functions. Added resource/script source caching. Fixed highlighting matches bug on minified-file lines.
- Loading branch information
Marco Minetti
committed
May 19, 2015
1 parent
642e801
commit 74036dd
Showing
6 changed files
with
340 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/*jshint browser:true, nonew:false*/ | ||
/*global WebInspector:true, InspectorFrontendHost:true, InspectorFrontendHostAPI:true*/ | ||
|
||
(function() { | ||
var createSearchRegex = function(query, caseSensitive, isRegex) | ||
{ | ||
var regexFlags = caseSensitive ? "g" : "gi"; | ||
var regexObject; | ||
|
||
if (isRegex) { | ||
try { | ||
regexObject = new RegExp('^.*?'+query+'.*?$|^.*?'+query+'.*?\n|\n.*?'+query+'.*?\n|\n.*?'+query+'.*?$', regexFlags); | ||
} catch (e) { | ||
// Silent catch. | ||
} | ||
} | ||
|
||
if (!regexObject) | ||
regexObject = createPlainTextSearchRegex(query, regexFlags); | ||
|
||
return regexObject; | ||
} | ||
|
||
var createPlainTextSearchRegex = function(query, flags) | ||
{ | ||
// This should be kept the same as the one in ContentSearchUtils.cpp. | ||
var regexSpecialCharacters = "^[]{}()\\.^$*+?|-,"; | ||
var regex = ""; | ||
for (var i = 0; i < query.length; ++i) { | ||
var c = query.charAt(i); | ||
if (regexSpecialCharacters.indexOf(c) != -1) | ||
regex += "\\"; | ||
regex += c; | ||
} | ||
return new RegExp('^.*?'+regex+'.*?$|^.*?'+regex+'.*?\n|\n.*?'+regex+'.*?\n|\n.*?'+regex+'.*?$', flags || ""); | ||
} | ||
|
||
|
||
|
||
WebInspector.ContentProvider.performSearchInContent = function(content, query, caseSensitive, isRegex) | ||
{ | ||
var regex = createSearchRegex(query, caseSensitive, isRegex); | ||
|
||
var result = []; | ||
var lastMatch; | ||
var isMinified = false; | ||
|
||
var firstNewLine = content.indexOf('\n'); | ||
if (content.length > 1024) { | ||
if (firstNewLine > 1024 || firstNewLine === -1) { | ||
isMinified = true; | ||
} | ||
} | ||
|
||
while(lastMatch=regex.exec(content)) { | ||
var lineContent = lastMatch[0]; | ||
var firstChar = lineContent.charCodeAt(0); | ||
var lastChar = lineContent.charCodeAt(lineContent.length-1); | ||
var lineMatchesBefore = content.substr(0,regex.lastIndex).match(/\n/g); | ||
if (lineMatchesBefore){ | ||
var i = lineMatchesBefore.length; | ||
if (lastChar !== 10){ | ||
++i; | ||
} else { | ||
lineContent = lineContent.substr(0,lineContent.length-1); | ||
} | ||
if (firstChar === 10){ | ||
lineContent = lineContent.substr(1); | ||
} | ||
if (isMinified === true && lineContent.length > 1024) { | ||
lineContent = ' ... (line too long)'; | ||
} | ||
result.push(new WebInspector.ContentProvider.SearchMatch(i, lineContent)); | ||
} | ||
} | ||
return result; | ||
} | ||
})() | ||
|
||
WebInspector.FileBasedSearchResultsPane.FileTreeElement.prototype._appendSearchMatches = function(fromIndex, toIndex) | ||
{ | ||
var searchResult = this._searchResult; | ||
var uiSourceCode = searchResult.uiSourceCode; | ||
var searchMatches = searchResult.searchMatches; | ||
|
||
var queries = this._searchConfig.queries(); | ||
var regexes = []; | ||
for (var i = 0; i < queries.length; ++i) | ||
regexes.push(createSearchRegex(queries[i], !this._searchConfig.ignoreCase(), this._searchConfig.isRegex())); | ||
|
||
for (var i = fromIndex; i < toIndex; ++i) { | ||
var lineNumber = searchMatches[i].lineNumber; | ||
var lineContent = searchMatches[i].lineContent; | ||
var matchRanges = []; | ||
for (var j = 0; j < regexes.length; ++j) | ||
matchRanges = matchRanges.concat(this._regexMatchRanges(lineContent, regexes[j])); | ||
|
||
var anchor; | ||
if (!matchRanges[0]){ | ||
matchRanges[0] = new WebInspector.SourceRange(0,0); | ||
anchor = this._createAnchor(uiSourceCode, lineNumber, matchRanges[0].offset); | ||
} else { | ||
anchor = this._createAnchor(uiSourceCode, lineNumber, matchRanges[0].offset); | ||
} | ||
|
||
var numberString = numberToStringWithSpacesPadding(lineNumber + 1, 4); | ||
var lineNumberSpan = createElement("span"); | ||
lineNumberSpan.classList.add("search-match-line-number"); | ||
lineNumberSpan.textContent = numberString; | ||
anchor.appendChild(lineNumberSpan); | ||
|
||
var contentSpan = this._createContentSpan(lineContent, matchRanges); | ||
anchor.appendChild(contentSpan); | ||
|
||
var searchMatchElement = new TreeElement(""); | ||
searchMatchElement.selectable = false; | ||
this.appendChild(searchMatchElement); | ||
searchMatchElement.listItemElement.className = "search-match source-code"; | ||
searchMatchElement.listItemElement.appendChild(anchor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"dependencies": [ | ||
"sources", | ||
"console" | ||
], | ||
"scripts": [ | ||
"SearchOverrides.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// code extracted and adapted from ../front-end/platform/utilities.js | ||
// _performSearchInContent is based on ../front-end/platform/utilities.js implementation | ||
// performSearchInContent uses regexp to gain performance | ||
|
||
|
||
var findAll = function(content, string) | ||
{ | ||
var matches = []; | ||
var i = content.indexOf(string); | ||
while (i !== -1) { | ||
matches.push(i); | ||
i = content.indexOf(string, i + string.length); | ||
} | ||
return matches; | ||
} | ||
|
||
var getlineEndings = function(content) | ||
{ | ||
var _lineEndings = findAll(content, "\n"); | ||
_lineEndings.push(content.length); | ||
return _lineEndings; | ||
} | ||
|
||
var getlineAt = function(content, lineEndings, lineNumber) { | ||
var lineStart = lineNumber > 0 ? lineEndings[lineNumber - 1] + 1 : 0; | ||
var lineEnd = lineEndings[lineNumber]; | ||
var lineContent = content.substring(lineStart, lineEnd); | ||
if (lineContent.length > 0 && lineContent.charAt(lineContent.length - 1) === "\r") | ||
lineContent = lineContent.substring(0, lineContent.length - 1); | ||
return lineContent; | ||
} | ||
|
||
var _createSearchRegex = function(query, caseSensitive, isRegex) | ||
{ | ||
var regexFlags = caseSensitive ? "g" : "gi"; | ||
var regexObject; | ||
|
||
if (isRegex) { | ||
try { | ||
regexObject = new RegExp(query, regexFlags); | ||
} catch (e) { | ||
// Silent catch. | ||
} | ||
} | ||
|
||
if (!regexObject) | ||
regexObject = _createPlainTextSearchRegex(query, regexFlags); | ||
|
||
return regexObject; | ||
} | ||
|
||
var _createPlainTextSearchRegex = function(query, flags) | ||
{ | ||
// This should be kept the same as the one in ContentSearchUtils.cpp. | ||
var regexSpecialCharacters = "^[]{}()\\.^$*+?|-,"; | ||
var regex = ""; | ||
for (var i = 0; i < query.length; ++i) { | ||
var c = query.charAt(i); | ||
if (regexSpecialCharacters.indexOf(c) != -1) | ||
regex += "\\"; | ||
regex += c; | ||
} | ||
return new RegExp(regex, flags || ""); | ||
} | ||
|
||
exports._performSearchInContent = function(content, query, caseSensitive, isRegex) | ||
{ | ||
var regex = _createSearchRegex(query, caseSensitive, isRegex); | ||
|
||
var result = []; | ||
var lineEndings = getlineEndings(content); | ||
var lineCount = lineEndings.length; | ||
for (var i = 0; i < lineCount; ++i) { | ||
var lineContent = getlineAt(content, i, lineEndings); | ||
regex.lastIndex = 0; | ||
if (regex.exec(lineContent)) | ||
result.push(new SearchMatch(i, lineContent)); | ||
} | ||
return result; | ||
} | ||
|
||
var SearchMatch = function(lineNumber, lineContent) { | ||
this.lineNumber = lineNumber; | ||
this.lineContent = lineContent; | ||
} | ||
|
||
var createSearchRegex = function(query, caseSensitive, isRegex) | ||
{ | ||
var regexFlags = caseSensitive ? "g" : "gi"; | ||
var regexObject; | ||
|
||
if (isRegex) { | ||
try { | ||
regexObject = new RegExp('^.*?'+query+'.*?$|^.*?'+query+'.*?\n|\n.*?'+query+'.*?\n|\n.*?'+query+'.*?$', regexFlags); | ||
} catch (e) { | ||
// Silent catch. | ||
} | ||
} | ||
|
||
if (!regexObject) | ||
regexObject = createPlainTextSearchRegex(query, regexFlags); | ||
|
||
return regexObject; | ||
} | ||
|
||
var createPlainTextSearchRegex = function(query, flags) | ||
{ | ||
// This should be kept the same as the one in ContentSearchUtils.cpp. | ||
var regexSpecialCharacters = "^[]{}()\\.^$*+?|-,"; | ||
var regex = ""; | ||
for (var i = 0; i < query.length; ++i) { | ||
var c = query.charAt(i); | ||
if (regexSpecialCharacters.indexOf(c) != -1) | ||
regex += "\\"; | ||
regex += c; | ||
} | ||
return new RegExp('^.*?'+regex+'.*?$|^.*?'+regex+'.*?\n|\n.*?'+regex+'.*?\n|\n.*?'+regex+'.*?$', flags || ""); | ||
} | ||
|
||
|
||
|
||
exports.performSearchInContent = function(content, query, caseSensitive, isRegex) | ||
{ | ||
var regex = createSearchRegex(query, caseSensitive, isRegex); | ||
|
||
var result = []; | ||
var lastMatch; | ||
var isMinified = false; | ||
|
||
var firstNewLine = content.indexOf('\n'); | ||
if (content.length > 1024) { | ||
if (firstNewLine > 1024 || firstNewLine === -1) { | ||
isMinified = true; | ||
} | ||
} | ||
|
||
while(lastMatch=regex.exec(content)) { | ||
var lineContent = lastMatch[0]; | ||
var firstChar = lineContent.charCodeAt(0); | ||
var lastChar = lineContent.charCodeAt(lineContent.length-1); | ||
var lineMatchesBefore = content.substr(0,regex.lastIndex).match(/\n/g); | ||
if (lineMatchesBefore){ | ||
var i = lineMatchesBefore.length; | ||
if (lastChar !== 10){ | ||
++i; | ||
} else { | ||
lineContent = lineContent.substr(0,lineContent.length-1); | ||
} | ||
if (firstChar === 10){ | ||
lineContent = lineContent.substr(1); | ||
} | ||
if (isMinified === true && lineContent.length > 1024) { | ||
lineContent = ' ... (line too long)'; | ||
} | ||
result.push(new SearchMatch(i, lineContent)); | ||
} | ||
} | ||
return result; | ||
} |