tizen beta release
[profile/ivi/webkit-efl.git] / debian / tmp / usr / share / ewebkit-0 / webinspector / ContentProviders.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 /**
32  * @constructor
33  * @implements {WebInspector.ContentProvider}
34  */
35 WebInspector.ScriptContentProvider = function(script)
36 {
37     this._mimeType = "text/javascript";
38     this._script = script;
39 };
40
41 WebInspector.ScriptContentProvider.prototype = {
42     /**
43      * @param {function(string,string)} callback
44      */
45     requestContent: function(callback)
46     {
47         function didRequestSource(source)
48         {
49             callback(this._mimeType, source);
50         }
51         this._script.requestSource(didRequestSource.bind(this));
52     },
53
54     /**
55      * @param {string} query
56      * @param {boolean} caseSensitive
57      * @param {boolean} isRegex
58      * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
59      */
60     searchInContent: function(query, caseSensitive, isRegex, callback)
61     {
62         this._script.searchInContent(query, caseSensitive, isRegex, callback);
63     }
64 }
65
66 WebInspector.ScriptContentProvider.prototype.__proto__ = WebInspector.ContentProvider.prototype;
67
68 /**
69  * @constructor
70  * @implements {WebInspector.ContentProvider}
71  */
72 WebInspector.ConcatenatedScriptsContentProvider = function(scripts)
73 {
74     this._mimeType = "text/html";
75     this._scripts = scripts;
76 };
77
78 WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag = "<script>";
79 WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag = "</script>";
80
81 WebInspector.ConcatenatedScriptsContentProvider.prototype = {
82     /**
83      * @return {Array.<WebInspector.Script>}
84      */
85     _sortedScripts: function()
86     {
87         if (this._sortedScriptsArray)
88             return this._sortedScriptsArray;
89
90         this._sortedScriptsArray = [];
91         
92         var scripts = this._scripts.slice();
93         scripts.sort(function(x, y) { return x.lineOffset - y.lineOffset || x.columnOffset - y.columnOffset; });
94         
95         var scriptOpenTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag.length;
96         var scriptCloseTagLength = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag.length;
97         
98         this._sortedScriptsArray.push(scripts[0]);
99         for (var i = 1; i < scripts.length; ++i) {
100             var previousScript = this._sortedScriptsArray[this._sortedScriptsArray.length - 1];
101             
102             var lineNumber = previousScript.endLine;
103             var columnNumber = previousScript.endColumn + scriptCloseTagLength + scriptOpenTagLength;
104             
105             if (lineNumber < scripts[i].lineOffset || (lineNumber === scripts[i].lineOffset && columnNumber <= scripts[i].columnOffset))
106                 this._sortedScriptsArray.push(scripts[i]);
107         }
108         return this._sortedScriptsArray;
109     },
110
111     /**
112      * @param {function(string,string)} callback
113      */
114     requestContent: function(callback)
115     {
116         var scripts = this._sortedScripts();
117         var sources = [];
118         function didRequestSource(source)
119         {
120             sources.push(source);
121             if (sources.length == scripts.length)
122                 callback(this._mimeType, this._concatenateScriptsContent(scripts, sources));
123         }
124         for (var i = 0; i < scripts.length; ++i)
125             scripts[i].requestSource(didRequestSource.bind(this));
126     },
127
128     /**
129      * @param {string} query
130      * @param {boolean} caseSensitive
131      * @param {boolean} isRegex
132      * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
133      */
134     searchInContent: function(query, caseSensitive, isRegex, callback)
135     {
136         var results = {};
137         var scripts = this._sortedScripts();
138         var scriptsLeft = scripts.length;
139
140         function maybeCallback()
141         {
142             if (scriptsLeft)
143                 return;
144
145             var result = [];
146             for (var i = 0; i < scripts.length; ++i)
147                 result = result.concat(results[scripts[i].scriptId]);
148             callback(result);
149         }
150
151         /**
152          * @param {WebInspector.Script} script
153          * @param {Array.<PageAgent.SearchMatch>} searchMatches
154          */
155         function searchCallback(script, searchMatches)
156         {
157             results[script.scriptId] = [];
158             for (var i = 0; i < searchMatches.length; ++i) {
159                 var searchMatch = new WebInspector.ContentProvider.SearchMatch(searchMatches[i].lineNumber + script.lineOffset, searchMatches[i].lineContent);
160                 results[script.scriptId].push(searchMatch);
161             }
162             scriptsLeft--;
163             maybeCallback.call(this);
164         }
165
166         maybeCallback();
167         for (var i = 0; i < scripts.length; ++i)
168             scripts[i].searchInContent(query, caseSensitive, isRegex, searchCallback.bind(this, scripts[i]));
169     },
170
171     /**
172      * @return {string}
173      */
174     _concatenateScriptsContent: function(scripts, sources)
175     {
176         var content = "";
177         var lineNumber = 0;
178         var columnNumber = 0;
179
180         var scriptOpenTag = WebInspector.ConcatenatedScriptsContentProvider.scriptOpenTag;
181         var scriptCloseTag = WebInspector.ConcatenatedScriptsContentProvider.scriptCloseTag;
182         for (var i = 0; i < scripts.length; ++i) {
183             // Fill the gap with whitespace characters.
184             for (var newLinesCount = scripts[i].lineOffset - lineNumber; newLinesCount > 0; --newLinesCount) {
185                 columnNumber = 0;
186                 content += "\n";
187             }
188             for (var spacesCount = scripts[i].columnOffset - columnNumber - scriptOpenTag.length; spacesCount > 0; --spacesCount)
189                 content += " ";
190
191             // Add script tag.
192             content += scriptOpenTag;
193             content += sources[i];
194             content += scriptCloseTag;
195             lineNumber = scripts[i].endLine;
196             columnNumber = scripts[i].endColumn + scriptCloseTag.length;
197         }
198
199         return content;
200     }
201 }
202
203 WebInspector.ConcatenatedScriptsContentProvider.prototype.__proto__ = WebInspector.ContentProvider.prototype;
204
205 /**
206  * @constructor
207  * @implements {WebInspector.ContentProvider}
208  */
209 WebInspector.ResourceContentProvider = function(resource)
210 {
211     this._mimeType = resource.type === WebInspector.Resource.Type.Script ? "text/javascript" : "text/html";
212     this._resource = resource;
213 };
214
215 WebInspector.ResourceContentProvider.prototype = {
216     /**
217      * @param {function(string,string)} callback
218      */
219     requestContent: function(callback)
220     {
221         function didRequestContent(content)
222         {
223             callback(this._mimeType, content);
224         }
225         this._resource.requestContent(didRequestContent.bind(this));
226     },
227
228     /**
229      * @param {string} query
230      * @param {boolean} caseSensitive
231      * @param {boolean} isRegex
232      * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
233      */
234     searchInContent: function(query, caseSensitive, isRegex, callback)
235     {
236         this._resource.searchInContent(query, caseSensitive, isRegex, callback);
237     }
238 }
239
240 WebInspector.ResourceContentProvider.prototype.__proto__ = WebInspector.ContentProvider.prototype;
241
242 /**
243  * @constructor
244  * @implements {WebInspector.ContentProvider}
245  */
246 WebInspector.CompilerSourceMappingContentProvider = function(sourceURL, compilerSourceMapping)
247 {
248     this._mimeType = "text/javascript";
249     this._sourceURL = sourceURL;
250     this._compilerSourceMapping = compilerSourceMapping;
251 };
252
253 WebInspector.CompilerSourceMappingContentProvider.prototype = {
254     /**
255      * @param {function(string,string)} callback
256      */
257     requestContent: function(callback)
258     {
259         var sourceCode = this._compilerSourceMapping.loadSourceCode(this._sourceURL);
260         callback(this._mimeType, sourceCode);
261     },
262
263     /**
264      * @param {string} query
265      * @param {boolean} caseSensitive
266      * @param {boolean} isRegex
267      * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
268      */
269     searchInContent: function(query, caseSensitive, isRegex, callback)
270     {
271         callback([]);
272     }
273 }
274
275 WebInspector.CompilerSourceMappingContentProvider.prototype.__proto__ = WebInspector.ContentProvider.prototype;
276
277 /**
278  * @constructor
279  * @implements {WebInspector.ContentProvider}
280  */
281 WebInspector.StaticContentProvider = function(mimeType, content)
282 {
283     this._mimeType = mimeType;
284     this._content = content;
285 };
286
287 WebInspector.StaticContentProvider.prototype = {
288     /**
289      * @param {function(string,string)} callback
290      */
291     requestContent: function(callback)
292     {
293         callback(this._mimeType, this._content);
294     },
295
296     /**
297      * @param {string} query
298      * @param {boolean} caseSensitive
299      * @param {boolean} isRegex
300      * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
301      */
302     searchInContent: function(query, caseSensitive, isRegex, callback)
303     {
304         function performSearch()
305         {
306             var regex = createSearchRegex(query, caseSensitive, isRegex);
307             
308             var result = [];
309             var lineEndings = this._content.lineEndings();
310             for (var i = 0; i < lineEndings.length; ++i) {
311                 var lineStart = i > 0 ? lineEndings[i - 1] + 1 : 0;
312                 var lineEnd = lineEndings[i];
313                 var lineContent = this._content.substring(lineStart, lineEnd);
314                 if (lineContent.length > 0 && lineContent.charAt(lineContent.length - 1) === "\r")
315                     lineContent = lineContent.substring(0, lineContent.length - 1)
316                 
317                 if (regex.exec(lineContent))
318                     result.push(new WebInspector.ContentProvider.SearchMatch(i, lineContent));
319             }
320             callback(result);
321         }
322
323         // searchInContent should call back later.
324         window.setTimeout(performSearch.bind(this), 0);
325     }
326 }
327
328 WebInspector.StaticContentProvider.prototype.__proto__ = WebInspector.ContentProvider.prototype;