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.TargetAwareObject}
29 * @implements {WebInspector.ContentProvider}
30 * @param {!WebInspector.Target} target
31 * @param {string} scriptId
32 * @param {string} sourceURL
33 * @param {number} startLine
34 * @param {number} startColumn
35 * @param {number} endLine
36 * @param {number} endColumn
37 * @param {boolean} isContentScript
38 * @param {string=} sourceMapURL
39 * @param {boolean=} hasSourceURL
41 WebInspector.Script = function(target, scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript, sourceMapURL, hasSourceURL)
43 WebInspector.TargetAwareObject.call(this, target);
44 this.scriptId = scriptId;
45 this.sourceURL = sourceURL;
46 this.lineOffset = startLine;
47 this.columnOffset = startColumn;
48 this.endLine = endLine;
49 this.endColumn = endColumn;
50 this._isContentScript = isContentScript;
51 this.sourceMapURL = sourceMapURL;
52 this.hasSourceURL = hasSourceURL;
53 /** @type {!Set.<!WebInspector.Script.Location>} */
54 this._locations = new Set();
55 /** @type {!Array.<!WebInspector.SourceMapping>} */
56 this._sourceMappings = [];
59 WebInspector.Script.Events = {
60 ScriptEdited: "ScriptEdited",
63 WebInspector.Script.snippetSourceURLPrefix = "snippets:///";
65 WebInspector.Script.sourceURLRegex = /\n[\040\t]*\/\/[@#]\ssourceURL=\s*(\S*?)\s*$/mg;
68 * @param {string} source
71 WebInspector.Script._trimSourceURLComment = function(source)
73 return source.replace(WebInspector.Script.sourceURLRegex, "");
77 WebInspector.Script.prototype = {
81 isContentScript: function()
83 return this._isContentScript;
89 contentURL: function()
91 return this.sourceURL;
95 * @return {!WebInspector.ResourceType}
97 contentType: function()
99 return WebInspector.resourceTypes.Script;
103 * @param {function(?string)} callback
105 requestContent: function(callback)
108 callback(this._source);
113 * @this {WebInspector.Script}
114 * @param {?Protocol.Error} error
115 * @param {string} source
117 function didGetScriptSource(error, source)
119 this._source = WebInspector.Script._trimSourceURLComment(error ? "" : source);
120 callback(this._source);
123 // Script failed to parse.
124 DebuggerAgent.getScriptSource(this.scriptId, didGetScriptSource.bind(this));
130 * @param {string} query
131 * @param {boolean} caseSensitive
132 * @param {boolean} isRegex
133 * @param {function(!Array.<!PageAgent.SearchMatch>)} callback
135 searchInContent: function(query, caseSensitive, isRegex, callback)
138 * @param {?Protocol.Error} error
139 * @param {!Array.<!PageAgent.SearchMatch>} searchMatches
141 function innerCallback(error, searchMatches)
144 console.error(error);
146 for (var i = 0; i < searchMatches.length; ++i) {
147 var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber, searchMatches[i].lineContent);
148 result.push(searchMatch);
150 callback(result || []);
154 // Script failed to parse.
155 DebuggerAgent.searchInContent(this.scriptId, query, caseSensitive, isRegex, innerCallback);
161 * @param {string} source
164 _appendSourceURLCommentIfNeeded: function(source)
166 if (!this.hasSourceURL)
168 return source + "\n //# sourceURL=" + this.sourceURL;
172 * @param {string} newSource
173 * @param {function(?Protocol.Error, !DebuggerAgent.SetScriptSourceError=, !Array.<!DebuggerAgent.CallFrame>=, !DebuggerAgent.StackTrace=, boolean=)} callback
175 editSource: function(newSource, callback)
178 * @this {WebInspector.Script}
179 * @param {?Protocol.Error} error
180 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
181 * @param {!Array.<!DebuggerAgent.CallFrame>=} callFrames
182 * @param {!Object=} debugData
183 * @param {!DebuggerAgent.StackTrace=} asyncStackTrace
185 function didEditScriptSource(error, errorData, callFrames, debugData, asyncStackTrace)
187 // FIXME: support debugData.stack_update_needs_step_in flag by calling WebInspector.debugger_model.callStackModified
189 this._source = newSource;
190 var needsStepIn = !!debugData && debugData["stack_update_needs_step_in"] === true;
191 callback(error, errorData, callFrames, asyncStackTrace, needsStepIn);
193 this.dispatchEventToListeners(WebInspector.Script.Events.ScriptEdited, newSource);
196 newSource = WebInspector.Script._trimSourceURLComment(newSource);
197 // We append correct sourceURL to script for consistency only. It's not actually needed for things to work correctly.
198 newSource = this._appendSourceURLCommentIfNeeded(newSource);
201 DebuggerAgent.setScriptSource(this.scriptId, newSource, undefined, didEditScriptSource.bind(this));
203 callback("Script failed to parse");
209 isInlineScript: function()
211 var startsAtZero = !this.lineOffset && !this.columnOffset;
212 return !!this.sourceURL && !startsAtZero;
218 isAnonymousScript: function()
220 return !this.sourceURL;
226 isSnippet: function()
228 return !!this.sourceURL && this.sourceURL.startsWith(WebInspector.Script.snippetSourceURLPrefix);
234 isFramework: function()
236 if (!WebInspector.experimentsSettings.frameworksDebuggingSupport.isEnabled())
238 if (!WebInspector.settings.skipStackFramesSwitch.get())
240 var regex = WebInspector.settings.skipStackFramesPattern.asRegExp();
241 return regex ? regex.test(this.sourceURL) : false;
245 * @param {number} lineNumber
246 * @param {number=} columnNumber
247 * @return {!WebInspector.UILocation}
249 rawLocationToUILocation: function(lineNumber, columnNumber)
252 var rawLocation = new WebInspector.DebuggerModel.Location(this.target(), this.scriptId, lineNumber, columnNumber || 0);
253 for (var i = this._sourceMappings.length - 1; !uiLocation && i >= 0; --i)
254 uiLocation = this._sourceMappings[i].rawLocationToUILocation(rawLocation);
255 console.assert(uiLocation, "Script raw location can not be mapped to any ui location.");
256 return /** @type {!WebInspector.UILocation} */ (uiLocation);
260 * @param {!WebInspector.SourceMapping} sourceMapping
262 pushSourceMapping: function(sourceMapping)
264 this._sourceMappings.push(sourceMapping);
265 this.updateLocations();
269 * @return {!WebInspector.SourceMapping}
271 popSourceMapping: function()
273 var sourceMapping = this._sourceMappings.pop();
274 this.updateLocations();
275 return sourceMapping;
278 updateLocations: function()
280 var items = this._locations.items();
281 for (var i = 0; i < items.length; ++i)
286 * @param {!WebInspector.DebuggerModel.Location} rawLocation
287 * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
288 * @return {!WebInspector.Script.Location}
290 createLiveLocation: function(rawLocation, updateDelegate)
292 console.assert(rawLocation.scriptId === this.scriptId);
293 var location = new WebInspector.Script.Location(this, rawLocation, updateDelegate);
294 this._locations.add(location);
299 __proto__: WebInspector.TargetAwareObject.prototype
304 * @extends {WebInspector.LiveLocation}
305 * @param {!WebInspector.Script} script
306 * @param {!WebInspector.DebuggerModel.Location} rawLocation
307 * @param {function(!WebInspector.UILocation):(boolean|undefined)} updateDelegate
309 WebInspector.Script.Location = function(script, rawLocation, updateDelegate)
311 WebInspector.LiveLocation.call(this, rawLocation, updateDelegate);
312 this._script = script;
315 WebInspector.Script.Location.prototype = {
317 * @return {!WebInspector.UILocation}
319 uiLocation: function()
321 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (this.rawLocation());
322 return this._script.rawLocationToUILocation(debuggerModelLocation.lineNumber, debuggerModelLocation.columnNumber);
327 WebInspector.LiveLocation.prototype.dispose.call(this);
328 this._script._locations.remove(this);
331 __proto__: WebInspector.LiveLocation.prototype