2 * Copyright (C) 2008 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * @extends {WebInspector.Object}
29 * @implements {WebInspector.ContentProvider}
30 * @param {string} scriptId
31 * @param {string} sourceURL
32 * @param {number} startLine
33 * @param {number} startColumn
34 * @param {number} endLine
35 * @param {number} endColumn
36 * @param {boolean} isContentScript
37 * @param {string=} sourceMapURL
38 * @param {boolean=} hasSourceURL
40 WebInspector.Script = function(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript, sourceMapURL, hasSourceURL)
42 this.scriptId = scriptId;
43 this.sourceURL = sourceURL;
44 this.lineOffset = startLine;
45 this.columnOffset = startColumn;
46 this.endLine = endLine;
47 this.endColumn = endColumn;
48 this.isContentScript = isContentScript;
49 this.sourceMapURL = sourceMapURL;
50 this.hasSourceURL = hasSourceURL;
51 /** @type {!Set.<!WebInspector.Script.Location>} */
52 this._locations = new Set();
53 /** @type {!Array.<!WebInspector.SourceMapping>} */
54 this._sourceMappings = [];
57 WebInspector.Script.Events = {
58 ScriptEdited: "ScriptEdited",
61 WebInspector.Script.snippetSourceURLPrefix = "snippets:///";
64 * @param {string} source
67 WebInspector.Script._trimSourceURLComment = function(source)
69 var sourceURLRegex = /\n[\040\t]*\/\/[@#]\ssourceURL=\s*(\S*?)\s*$/mg;
70 return source.replace(sourceURLRegex, "");
74 WebInspector.Script.prototype = {
78 contentURL: function()
80 return this.sourceURL;
84 * @return {!WebInspector.ResourceType}
86 contentType: function()
88 return WebInspector.resourceTypes.Script;
92 * @param {function(?string)} callback
94 requestContent: function(callback)
97 callback(this._source);
102 * @this {WebInspector.Script}
103 * @param {?Protocol.Error} error
104 * @param {string} source
106 function didGetScriptSource(error, source)
108 this._source = WebInspector.Script._trimSourceURLComment(error ? "" : source);
109 callback(this._source);
112 // Script failed to parse.
113 DebuggerAgent.getScriptSource(this.scriptId, didGetScriptSource.bind(this));
119 * @param {string} query
120 * @param {boolean} caseSensitive
121 * @param {boolean} isRegex
122 * @param {function(!Array.<!PageAgent.SearchMatch>)} callback
124 searchInContent: function(query, caseSensitive, isRegex, callback)
127 * @param {?Protocol.Error} error
128 * @param {!Array.<!PageAgent.SearchMatch>} searchMatches
130 function innerCallback(error, searchMatches)
133 console.error(error);
135 for (var i = 0; i < searchMatches.length; ++i) {
136 var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber, searchMatches[i].lineContent);
137 result.push(searchMatch);
139 callback(result || []);
143 // Script failed to parse.
144 DebuggerAgent.searchInContent(this.scriptId, query, caseSensitive, isRegex, innerCallback);
150 * @param {string} source
153 _appendSourceURLCommentIfNeeded: function(source)
155 if (!this.hasSourceURL)
157 return source + "\n //# sourceURL=" + this.sourceURL;
161 * @param {string} newSource
162 * @param {function(?Protocol.Error, !DebuggerAgent.SetScriptSourceError=, !Array.<!DebuggerAgent.CallFrame>=, !DebuggerAgent.StackTrace=, boolean=)} callback
164 editSource: function(newSource, callback)
167 * @this {WebInspector.Script}
168 * @param {?Protocol.Error} error
169 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
170 * @param {!Array.<!DebuggerAgent.CallFrame>=} callFrames
171 * @param {!Object=} debugData
172 * @param {!DebuggerAgent.StackTrace=} asyncStackTrace
174 function didEditScriptSource(error, errorData, callFrames, debugData, asyncStackTrace)
176 // FIXME: support debugData.stack_update_needs_step_in flag by calling WebInspector.debugger_model.callStackModified
178 this._source = newSource;
179 var needsStepIn = !!debugData && debugData["stack_update_needs_step_in"] === true;
180 callback(error, errorData, callFrames, asyncStackTrace, needsStepIn);
182 this.dispatchEventToListeners(WebInspector.Script.Events.ScriptEdited, newSource);
185 newSource = WebInspector.Script._trimSourceURLComment(newSource);
186 // We append correct sourceURL to script for consistency only. It's not actually needed for things to work correctly.
187 newSource = this._appendSourceURLCommentIfNeeded(newSource);
190 DebuggerAgent.setScriptSource(this.scriptId, newSource, undefined, didEditScriptSource.bind(this));
192 callback("Script failed to parse");
198 isInlineScript: function()
200 var startsAtZero = !this.lineOffset && !this.columnOffset;
201 return !!this.sourceURL && !startsAtZero;
207 isAnonymousScript: function()
209 return !this.sourceURL;
215 isSnippet: function()
217 return !!this.sourceURL && this.sourceURL.startsWith(WebInspector.Script.snippetSourceURLPrefix);
221 * @param {number} lineNumber
222 * @param {number=} columnNumber
223 * @return {!WebInspector.UILocation}
225 rawLocationToUILocation: function(lineNumber, columnNumber)
228 var rawLocation = new WebInspector.DebuggerModel.Location(this.scriptId, lineNumber, columnNumber || 0);
229 for (var i = this._sourceMappings.length - 1; !uiLocation && i >= 0; --i)
230 uiLocation = this._sourceMappings[i].rawLocationToUILocation(rawLocation);
231 console.assert(uiLocation, "Script raw location can not be mapped to any ui location.");
232 return /** @type {!WebInspector.UILocation} */ (uiLocation);
236 * @param {!WebInspector.SourceMapping} sourceMapping
238 pushSourceMapping: function(sourceMapping)
240 this._sourceMappings.push(sourceMapping);
241 this.updateLocations();
245 * @return {!WebInspector.SourceMapping}
247 popSourceMapping: function()
249 var sourceMapping = this._sourceMappings.pop();
250 this.updateLocations();
251 return sourceMapping;
254 updateLocations: function()
256 var items = this._locations.items();
257 for (var i = 0; i < items.length; ++i)
262 * @param {!WebInspector.DebuggerModel.Location} rawLocation
263 * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
264 * @return {!WebInspector.Script.Location}
266 createLiveLocation: function(rawLocation, updateDelegate)
268 console.assert(rawLocation.scriptId === this.scriptId);
269 var location = new WebInspector.Script.Location(this, rawLocation, updateDelegate);
270 this._locations.add(location);
275 __proto__: WebInspector.Object.prototype
280 * @extends {WebInspector.LiveLocation}
281 * @param {!WebInspector.Script} script
282 * @param {!WebInspector.DebuggerModel.Location} rawLocation
283 * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
285 WebInspector.Script.Location = function(script, rawLocation, updateDelegate)
287 WebInspector.LiveLocation.call(this, rawLocation, updateDelegate);
288 this._script = script;
291 WebInspector.Script.Location.prototype = {
293 * @return {!WebInspector.UILocation}
295 uiLocation: function()
297 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (this.rawLocation());
298 return this._script.rawLocationToUILocation(debuggerModelLocation.lineNumber, debuggerModelLocation.columnNumber);
303 WebInspector.LiveLocation.prototype.dispose.call(this);
304 this._script._locations.remove(this);
307 __proto__: WebInspector.LiveLocation.prototype