2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @implements {WebInspector.ContentProvider}
34 * @param {!Array.<!WebInspector.Script>} scripts
36 WebInspector.ConcatenatedScriptsContentProvider = function(scripts)
38 this._scripts = scripts;
41 WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag = "<script>";
42 WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag = "</script>";
44 WebInspector.ConcatenatedScriptsContentProvider.prototype = {
46 * @return {!Array.<!WebInspector.Script>}
48 _sortedScripts: function()
50 if (this._sortedScriptsArray)
51 return this._sortedScriptsArray;
53 this._sortedScriptsArray = [];
55 var scripts = this._scripts.slice();
56 scripts.sort(function(x, y) { return x.lineOffset - y.lineOffset || x.columnOffset - y.columnOffset; });
58 var scriptOpenTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag.length;
59 var scriptCloseTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag.length;
61 this._sortedScriptsArray.push(scripts[0]);
62 for (var i = 1; i < scripts.length; ++i) {
63 var previousScript = this._sortedScriptsArray[this._sortedScriptsArray.length - 1];
65 var lineNumber = previousScript.endLine;
66 var columnNumber = previousScript.endColumn + scriptCloseTagLength + scriptOpenTagLength;
68 if (lineNumber < scripts[i].lineOffset || (lineNumber === scripts[i].lineOffset && columnNumber <= scripts[i].columnOffset))
69 this._sortedScriptsArray.push(scripts[i]);
71 return this._sortedScriptsArray;
77 contentURL: function()
83 * @return {!WebInspector.ResourceType}
85 contentType: function()
87 return WebInspector.resourceTypes.Document;
91 * @param {function(?string)} callback
93 requestContent: function(callback)
95 var scripts = this._sortedScripts();
99 * @param {?string} content
100 * @this {WebInspector.ConcatenatedScriptsContentProvider}
102 function didRequestSource(content)
104 sources.push(content);
105 if (sources.length == scripts.length)
106 callback(this._concatenateScriptsContent(scripts, sources));
108 for (var i = 0; i < scripts.length; ++i)
109 scripts[i].requestContent(didRequestSource.bind(this));
113 * @param {string} query
114 * @param {boolean} caseSensitive
115 * @param {boolean} isRegex
116 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
118 searchInContent: function(query, caseSensitive, isRegex, callback)
121 var scripts = this._sortedScripts();
122 var scriptsLeft = scripts.length;
124 function maybeCallback()
130 for (var i = 0; i < scripts.length; ++i)
131 result = result.concat(results[scripts[i].scriptId]);
136 * @param {!WebInspector.Script} script
137 * @param {!Array.<!PageAgent.SearchMatch>} searchMatches
139 function searchCallback(script, searchMatches)
141 results[script.scriptId] = [];
142 for (var i = 0; i < searchMatches.length; ++i) {
143 var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber + script.lineOffset, searchMatches[i].lineContent);
144 results[script.scriptId].push(searchMatch);
151 for (var i = 0; i < scripts.length; ++i)
152 scripts[i].searchInContent(query, caseSensitive, isRegex, searchCallback.bind(null, scripts[i]));
158 _concatenateScriptsContent: function(scripts, sources)
162 var columnNumber = 0;
164 var scriptOpenTag = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag;
165 var scriptCloseTag = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag;
166 for (var i = 0; i < scripts.length; ++i) {
167 // Fill the gap with whitespace characters.
168 for (var newLinesCount = scripts[i].lineOffset - lineNumber; newLinesCount > 0; --newLinesCount) {
172 for (var spacesCount = scripts[i].columnOffset - columnNumber - scriptOpenTag.length; spacesCount > 0; --spacesCount)
176 content += scriptOpenTag;
177 content += sources[i];
178 content += scriptCloseTag;
179 lineNumber = scripts[i].endLine;
180 columnNumber = scripts[i].endColumn + scriptCloseTag.length;
189 * @implements {WebInspector.ContentProvider}
190 * @param {string} sourceURL
191 * @param {!WebInspector.ResourceType} contentType
193 WebInspector.CompilerSourceMappingContentProvider = function(sourceURL, contentType)
195 this._sourceURL = sourceURL;
196 this._contentType = contentType;
199 WebInspector.CompilerSourceMappingContentProvider.prototype = {
203 contentURL: function()
205 return this._sourceURL;
209 * @return {!WebInspector.ResourceType}
211 contentType: function()
213 return this._contentType;
217 * @param {function(?string)} callback
219 requestContent: function(callback)
221 NetworkAgent.loadResourceForFrontend(WebInspector.resourceTreeModel.mainFrame.id, this._sourceURL, undefined, contentLoaded.bind(this));
224 * @param {?Protocol.Error} error
225 * @param {number} statusCode
226 * @param {!NetworkAgent.Headers} headers
227 * @param {string} content
228 * @this {WebInspector.CompilerSourceMappingContentProvider}
230 function contentLoaded(error, statusCode, headers, content)
232 if (error || statusCode >= 400) {
233 console.error("Could not load content for " + this._sourceURL + " : " + (error || ("HTTP status code: " + statusCode)));
243 * @param {string} query
244 * @param {boolean} caseSensitive
245 * @param {boolean} isRegex
246 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
248 searchInContent: function(query, caseSensitive, isRegex, callback)
250 this.requestContent(contentLoaded);
253 * @param {?string} content
255 function contentLoaded(content)
257 if (typeof content !== "string") {
262 callback(WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex));
269 * @implements {WebInspector.ContentProvider}
270 * @param {!WebInspector.ResourceType} contentType
271 * @param {string} content
273 WebInspector.StaticContentProvider = function(contentType, content)
275 this._content = content;
276 this._contentType = contentType;
279 WebInspector.StaticContentProvider.prototype = {
283 contentURL: function()
289 * @return {!WebInspector.ResourceType}
291 contentType: function()
293 return this._contentType;
297 * @param {function(?string)} callback
299 requestContent: function(callback)
301 callback(this._content);
305 * @param {string} query
306 * @param {boolean} caseSensitive
307 * @param {boolean} isRegex
308 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
310 searchInContent: function(query, caseSensitive, isRegex, callback)
313 * @this {WebInspector.StaticContentProvider}
315 function performSearch()
317 callback(WebInspector.ContentProvider.performSearchInContent(this._content, query, caseSensitive, isRegex));
320 // searchInContent should call back later.
321 window.setTimeout(performSearch.bind(this), 0);