1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * @implements {WebInspector.ProjectSearchConfig}
8 * @param {string} query
9 * @param {boolean} ignoreCase
10 * @param {boolean} isRegex
12 WebInspector
.SearchConfig = function(query
, ignoreCase
, isRegex
)
15 this._ignoreCase
= ignoreCase
;
16 this._isRegex
= isRegex
;
20 /** @typedef {!{regex: !RegExp, isNegative: boolean}} */
21 WebInspector
.SearchConfig
.RegexQuery
;
24 * @param {{query: string, ignoreCase: boolean, isRegex: boolean}} object
25 * @return {!WebInspector.SearchConfig}
27 WebInspector
.SearchConfig
.fromPlainObject = function(object
)
29 return new WebInspector
.SearchConfig(object
.query
, object
.ignoreCase
, object
.isRegex
);
32 WebInspector
.SearchConfig
.prototype = {
46 ignoreCase: function()
48 return this._ignoreCase
;
61 * @return {{query: string, ignoreCase: boolean, isRegex: boolean}}
63 toPlainObject: function()
65 return { query
: this.query(), ignoreCase
: this.ignoreCase(), isRegex
: this.isRegex() };
70 var filePattern
= "-?f(ile)?:(([^\\\\ ]|\\\\.)+)"; // After file: prefix: any symbol except space and backslash or any symbol escaped with a backslash.
71 var quotedPattern
= "\"(([^\\\\\"]|\\\\.)+)\""; // Inside double quotes: any symbol except double quote and backslash or any symbol escaped with a backslash.
73 // A word is a sequence of any symbols except space and backslash or any symbols escaped with a backslash, that does not start with file:.
74 var unquotedWordPattern
= "((?!-?f(ile)?:)[^\\\\ ]|\\\\.)+";
75 var unquotedPattern
= unquotedWordPattern
+ "( +" + unquotedWordPattern
+ ")*"; // A word or several words separated by space(s).
77 var pattern
= "(" + filePattern
+ ")|(" + quotedPattern
+ ")|(" + unquotedPattern
+ ")";
78 var regexp
= new RegExp(pattern
, "g");
79 var queryParts
= this._query
.match(regexp
) || [];
82 * @type {!Array.<!WebInspector.SearchConfig.QueryTerm>}
84 this._fileQueries
= [];
87 * @type {!Array.<string>}
91 for (var i
= 0; i
< queryParts
.length
; ++i
) {
92 var queryPart
= queryParts
[i
];
95 var fileQuery
= this._parseFileQuery(queryPart
);
97 this._fileQueries
.push(fileQuery
);
98 /** @type {!Array.<!WebInspector.SearchConfig.RegexQuery>} */
99 this._fileRegexQueries
= this._fileRegexQueries
|| [];
100 this._fileRegexQueries
.push({ regex
: new RegExp(fileQuery
.text
, this.ignoreCase
? "i" : ""), isNegative
: fileQuery
.isNegative
});
103 if (queryPart
.startsWith("\"")) {
104 if (!queryPart
.endsWith("\""))
106 this._queries
.push(this._parseQuotedQuery(queryPart
));
109 this._queries
.push(this._parseUnquotedQuery(queryPart
));
115 * @param {string} filePath
118 filePathMatchesFileQuery: function(filePath
)
120 if (!this._fileRegexQueries
)
122 for (var i
= 0; i
< this._fileRegexQueries
.length
; ++i
) {
123 if (!!filePath
.match(this._fileRegexQueries
[i
].regex
) === this._fileRegexQueries
[i
].isNegative
)
131 * @return {!Array.<string>}
135 return this._queries
;
138 _parseUnquotedQuery: function(query
)
140 return query
.replace(/\\(.)/g, "$1");
143 _parseQuotedQuery: function(query
)
145 return query
.substring(1, query
.length
- 1).replace(/\\(.)/g, "$1");
149 * @param {string} query
150 * @return {?WebInspector.SearchConfig.QueryTerm}
152 _parseFileQuery: function(query
)
154 var match
= query
.match(/^(-)?f(ile)?:/);
157 var isNegative
= !!match
[1];
158 query
= query
.substr(match
[0].length
);
160 for (var i
= 0; i
< query
.length
; ++i
) {
164 } else if (char === "\\") {
166 var nextChar
= query
[i
];
167 if (nextChar
=== " ")
170 if (String
.regexSpecialCharacters().indexOf(query
.charAt(i
)) !== -1)
172 result
+= query
.charAt(i
);
175 return new WebInspector
.SearchConfig
.QueryTerm(result
, isNegative
);
181 * @param {string} text
182 * @param {boolean} isNegative
184 WebInspector
.SearchConfig
.QueryTerm = function(text
, isNegative
)
187 this.isNegative
= isNegative
;