Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / workspace / SearchConfig.js
blobf00c5fdd9df9b5c95ee469f9538d6f273ec261bb
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.
5 /**
6 * @constructor
7 * @implements {WebInspector.ProjectSearchConfig}
8 * @param {string} query
9 * @param {boolean} ignoreCase
10 * @param {boolean} isRegex
12 WebInspector.SearchConfig = function(query, ignoreCase, isRegex)
14 this._query = query;
15 this._ignoreCase = ignoreCase;
16 this._isRegex = isRegex;
17 this._parse();
20 /** @typedef {!{regex: !RegExp, isNegative: boolean}} */
21 WebInspector.SearchConfig.RegexQuery;
23 /**
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 = {
33 /**
34 * @override
35 * @return {string}
37 query: function()
39 return this._query;
42 /**
43 * @override
44 * @return {boolean}
46 ignoreCase: function()
48 return this._ignoreCase;
51 /**
52 * @override
53 * @return {boolean}
55 isRegex: function()
57 return this._isRegex;
60 /**
61 * @return {{query: string, ignoreCase: boolean, isRegex: boolean}}
63 toPlainObject: function()
65 return { query: this.query(), ignoreCase: this.ignoreCase(), isRegex: this.isRegex() };
68 _parse: function()
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) || [];
81 /**
82 * @type {!Array.<!WebInspector.SearchConfig.QueryTerm>}
84 this._fileQueries = [];
86 /**
87 * @type {!Array.<string>}
89 this._queries = [];
91 for (var i = 0; i < queryParts.length; ++i) {
92 var queryPart = queryParts[i];
93 if (!queryPart)
94 continue;
95 var fileQuery = this._parseFileQuery(queryPart);
96 if (fileQuery) {
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 });
101 continue;
103 if (queryPart.startsWith("\"")) {
104 if (!queryPart.endsWith("\""))
105 continue;
106 this._queries.push(this._parseQuotedQuery(queryPart));
107 continue;
109 this._queries.push(this._parseUnquotedQuery(queryPart));
114 * @override
115 * @param {string} filePath
116 * @return {boolean}
118 filePathMatchesFileQuery: function(filePath)
120 if (!this._fileRegexQueries)
121 return true;
122 for (var i = 0; i < this._fileRegexQueries.length; ++i) {
123 if (!!filePath.match(this._fileRegexQueries[i].regex) === this._fileRegexQueries[i].isNegative)
124 return false;
126 return true;
130 * @override
131 * @return {!Array.<string>}
133 queries: function()
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)?:/);
155 if (!match)
156 return null;
157 var isNegative = !!match[1];
158 query = query.substr(match[0].length);
159 var result = "";
160 for (var i = 0; i < query.length; ++i) {
161 var char = query[i];
162 if (char === "*") {
163 result += ".*";
164 } else if (char === "\\") {
165 ++i;
166 var nextChar = query[i];
167 if (nextChar === " ")
168 result += " ";
169 } else {
170 if (String.regexSpecialCharacters().indexOf(query.charAt(i)) !== -1)
171 result += "\\";
172 result += query.charAt(i);
175 return new WebInspector.SearchConfig.QueryTerm(result, isNegative);
180 * @constructor
181 * @param {string} text
182 * @param {boolean} isNegative
184 WebInspector.SearchConfig.QueryTerm = function(text, isNegative)
186 this.text = text;
187 this.isNegative = isNegative;