• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29WebInspector.SourceView = function(resource)
30{
31    // Set the sourceFrame first since WebInspector.ResourceView will set headersVisible
32    // and our override of headersVisible needs the sourceFrame.
33    this.sourceFrame = new WebInspector.SourceFrame(null, this._addBreakpoint.bind(this));
34
35    WebInspector.ResourceView.call(this, resource);
36
37    resource.addEventListener("finished", this._resourceLoadingFinished, this);
38
39    this.element.addStyleClass("source");
40
41    this._frameNeedsSetup = true;
42
43    this.contentElement.appendChild(this.sourceFrame.element);
44
45    var gutterElement = document.createElement("div");
46    gutterElement.className = "webkit-line-gutter-backdrop";
47    this.element.appendChild(gutterElement);
48}
49
50WebInspector.SourceView.prototype = {
51    set headersVisible(x)
52    {
53        if (x === this._headersVisible)
54            return;
55
56        var superSetter = WebInspector.ResourceView.prototype.__lookupSetter__("headersVisible");
57        if (superSetter)
58            superSetter.call(this, x);
59
60        this.sourceFrame.autoSizesToFitContentHeight = x;
61    },
62
63    show: function(parentElement)
64    {
65        WebInspector.ResourceView.prototype.show.call(this, parentElement);
66        this.setupSourceFrameIfNeeded();
67    },
68
69    hide: function()
70    {
71        WebInspector.View.prototype.hide.call(this);
72        this._currentSearchResultIndex = -1;
73    },
74
75    resize: function()
76    {
77        if (this.sourceFrame.autoSizesToFitContentHeight)
78            this.sourceFrame.sizeToFitContentHeight();
79    },
80
81    detach: function()
82    {
83        WebInspector.ResourceView.prototype.detach.call(this);
84
85        // FIXME: We need to mark the frame for setup on detach because the frame DOM is cleared
86        // when it is removed from the document. Is this a bug?
87        this._frameNeedsSetup = true;
88        this._sourceFrameSetup = false;
89    },
90
91    setupSourceFrameIfNeeded: function()
92    {
93        if (!this._frameNeedsSetup)
94            return;
95
96        this.attach();
97
98        delete this._frameNeedsSetup;
99        this.sourceFrame.addEventListener("content loaded", this._contentLoaded, this);
100        InspectorController.addResourceSourceToFrame(this.resource.identifier, this.sourceFrame.element);
101    },
102
103    _contentLoaded: function()
104    {
105        delete this._frameNeedsSetup;
106        this.sourceFrame.removeEventListener("content loaded", this._contentLoaded, this);
107
108        if (this.resource.type === WebInspector.Resource.Type.Script
109            || this.resource.mimeType === 'application/json'
110            || this.resource.mimeType === 'application/javascript'
111            || /\.js(on)?$/.test(this.resource.lastPathComponent) ) {
112            this.sourceFrame.addEventListener("syntax highlighting complete", this._syntaxHighlightingComplete, this);
113            this.sourceFrame.syntaxHighlightJavascript();
114        } else
115            this._sourceFrameSetupFinished();
116    },
117
118    _resourceLoadingFinished: function(event)
119    {
120        this._frameNeedsSetup = true;
121        this._sourceFrameSetup = false;
122        if (this.visible)
123            this.setupSourceFrameIfNeeded();
124        this.resource.removeEventListener("finished", this._resourceLoadingFinished, this);
125    },
126
127    _addBreakpoint: function(line)
128    {
129        var sourceID = null;
130        var closestStartingLine = 0;
131        var scripts = this.resource.scripts;
132        for (var i = 0; i < scripts.length; ++i) {
133            var script = scripts[i];
134            if (script.startingLine <= line && script.startingLine >= closestStartingLine) {
135                closestStartingLine = script.startingLine;
136                sourceID = script.sourceID;
137            }
138        }
139
140        if (WebInspector.panels.scripts) {
141            var breakpoint = new WebInspector.Breakpoint(this.resource.url, line, sourceID);
142            WebInspector.panels.scripts.addBreakpoint(breakpoint);
143        }
144    },
145
146    // The rest of the methods in this prototype need to be generic enough to work with a ScriptView.
147    // The ScriptView prototype pulls these methods into it's prototype to avoid duplicate code.
148
149    searchCanceled: function()
150    {
151        this._currentSearchResultIndex = -1;
152        this._searchResults = [];
153        delete this._delayedFindSearchMatches;
154    },
155
156    performSearch: function(query, finishedCallback)
157    {
158        // Call searchCanceled since it will reset everything we need before doing a new search.
159        this.searchCanceled();
160
161        var lineQueryRegex = /(^|\s)(?:#|line:\s*)(\d+)(\s|$)/i;
162        var lineQueryMatch = query.match(lineQueryRegex);
163        if (lineQueryMatch) {
164            var lineToSearch = parseInt(lineQueryMatch[2]);
165
166            // If there was a space before and after the line query part, replace with a space.
167            // Otherwise replace with an empty string to eat the prefix or postfix space.
168            var lineQueryReplacement = (lineQueryMatch[1] && lineQueryMatch[3] ? " " : "");
169            var filterlessQuery = query.replace(lineQueryRegex, lineQueryReplacement);
170        }
171
172        this._searchFinishedCallback = finishedCallback;
173
174        function findSearchMatches(query, finishedCallback)
175        {
176            if (isNaN(lineToSearch)) {
177                // Search the whole document since there was no line to search.
178                this._searchResults = (InspectorController.search(this.sourceFrame.element.contentDocument, query) || []);
179            } else {
180                var sourceRow = this.sourceFrame.sourceRow(lineToSearch);
181                if (sourceRow) {
182                    if (filterlessQuery) {
183                        // There is still a query string, so search for that string in the line.
184                        this._searchResults = (InspectorController.search(sourceRow, filterlessQuery) || []);
185                    } else {
186                        // Match the whole line, since there was no remaining query string to match.
187                        var rowRange = this.sourceFrame.element.contentDocument.createRange();
188                        rowRange.selectNodeContents(sourceRow);
189                        this._searchResults = [rowRange];
190                    }
191                }
192
193                // Attempt to search for the whole query, just incase it matches a color like "#333".
194                var wholeQueryMatches = InspectorController.search(this.sourceFrame.element.contentDocument, query);
195                if (wholeQueryMatches)
196                    this._searchResults = this._searchResults.concat(wholeQueryMatches);
197            }
198
199            if (this._searchResults)
200                finishedCallback(this, this._searchResults.length);
201        }
202
203        if (!this._sourceFrameSetup) {
204            // The search is performed in _sourceFrameSetupFinished by calling _delayedFindSearchMatches.
205            this._delayedFindSearchMatches = findSearchMatches.bind(this, query, finishedCallback);
206            this.setupSourceFrameIfNeeded();
207            return;
208        }
209
210        findSearchMatches.call(this, query, finishedCallback);
211    },
212
213    jumpToFirstSearchResult: function()
214    {
215        if (!this._searchResults || !this._searchResults.length)
216            return;
217        this._currentSearchResultIndex = 0;
218        this._jumpToSearchResult(this._currentSearchResultIndex);
219    },
220
221    jumpToLastSearchResult: function()
222    {
223        if (!this._searchResults || !this._searchResults.length)
224            return;
225        this._currentSearchResultIndex = (this._searchResults.length - 1);
226        this._jumpToSearchResult(this._currentSearchResultIndex);
227    },
228
229    jumpToNextSearchResult: function()
230    {
231        if (!this._searchResults || !this._searchResults.length)
232            return;
233        if (++this._currentSearchResultIndex >= this._searchResults.length)
234            this._currentSearchResultIndex = 0;
235        this._jumpToSearchResult(this._currentSearchResultIndex);
236    },
237
238    jumpToPreviousSearchResult: function()
239    {
240        if (!this._searchResults || !this._searchResults.length)
241            return;
242        if (--this._currentSearchResultIndex < 0)
243            this._currentSearchResultIndex = (this._searchResults.length - 1);
244        this._jumpToSearchResult(this._currentSearchResultIndex);
245    },
246
247    showingFirstSearchResult: function()
248    {
249        return (this._currentSearchResultIndex === 0);
250    },
251
252    showingLastSearchResult: function()
253    {
254        return (this._searchResults && this._currentSearchResultIndex === (this._searchResults.length - 1));
255    },
256
257    revealLine: function(lineNumber)
258    {
259        this.setupSourceFrameIfNeeded();
260        this.sourceFrame.revealLine(lineNumber);
261    },
262
263    highlightLine: function(lineNumber)
264    {
265        this.setupSourceFrameIfNeeded();
266        this.sourceFrame.highlightLine(lineNumber);
267    },
268
269    addMessage: function(msg)
270    {
271        this.sourceFrame.addMessage(msg);
272    },
273
274    clearMessages: function()
275    {
276        this.sourceFrame.clearMessages();
277    },
278
279    _jumpToSearchResult: function(index)
280    {
281        var foundRange = this._searchResults[index];
282        if (!foundRange)
283            return;
284
285        var selection = this.sourceFrame.element.contentWindow.getSelection();
286        selection.removeAllRanges();
287        selection.addRange(foundRange);
288
289        if (foundRange.startContainer.scrollIntoViewIfNeeded)
290            foundRange.startContainer.scrollIntoViewIfNeeded(true);
291        else if (foundRange.startContainer.parentNode)
292            foundRange.startContainer.parentNode.scrollIntoViewIfNeeded(true);
293    },
294
295    _sourceFrameSetupFinished: function()
296    {
297        this._sourceFrameSetup = true;
298        if (this._delayedFindSearchMatches) {
299            this._delayedFindSearchMatches();
300            delete this._delayedFindSearchMatches;
301        }
302    },
303
304    _syntaxHighlightingComplete: function(event)
305    {
306        this._sourceFrameSetupFinished();
307        this.sourceFrame.removeEventListener("syntax highlighting complete", null, this);
308    }
309}
310
311WebInspector.SourceView.prototype.__proto__ = WebInspector.ResourceView.prototype;
312