initial import
[vuplus_webkit] / Source / WebCore / inspector / front-end / ScriptFormatterWorker.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 onmessage = function(event) {
32     var result = {};
33     if (event.data.mimeType === "text/html") {
34         var formatter = new HTMLScriptFormatter();
35         result = formatter.format(event.data.content);
36     } else {
37         result.mapping = { original: [0], formatted: [0] };
38         result.content = formatScript(event.data.content, result.mapping, 0, 0);
39     }
40     postMessage(result);
41 };
42
43 function formatScript(content, mapping, offset, formattedOffset)
44 {
45     var formattedContent;
46     try {
47         var tokenizer = new Tokenizer(content);
48         var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset);
49         var formatter = new JavaScriptFormatter(tokenizer, builder);
50         formatter.format();
51         formattedContent = builder.content();
52     } catch (e) {
53         formattedContent = content;
54     }
55     return formattedContent;
56 }
57
58 WebInspector = {};
59 importScripts("SourceTokenizer.js");
60 importScripts("SourceHTMLTokenizer.js");
61
62 HTMLScriptFormatter = function()
63 {
64     WebInspector.SourceHTMLTokenizer.call(this);
65 }
66
67 HTMLScriptFormatter.prototype = {
68     format: function(content)
69     {
70         this.line = content;
71         this._content = content;
72         this._formattedContent = "";
73         this._mapping = { original: [0], formatted: [0] };
74         this._position = 0;
75
76         var cursor = 0;
77         while (cursor < this._content.length)
78             cursor = this.nextToken(cursor);
79
80         this._formattedContent += this._content.substring(this._position);
81         return { content: this._formattedContent, mapping: this._mapping };
82     },
83
84     scriptStarted: function(cursor)
85     {
86         this._formattedContent += this._content.substring(this._position, cursor);
87         this._formattedContent += "\n";
88         this._position = cursor;
89     },
90
91     scriptEnded: function(cursor)
92     {
93         if (cursor === this._position)
94             return;
95
96         var scriptContent = this._content.substring(this._position, cursor);
97         this._mapping.original.push(this._position);
98         this._mapping.formatted.push(this._formattedContent.length);
99         var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length);
100
101         this._formattedContent += formattedScriptContent;
102         this._position = cursor;
103     },
104
105     styleSheetStarted: function(cursor)
106     {
107     },
108
109     styleSheetEnded: function(cursor)
110     {
111     }
112 }
113
114 HTMLScriptFormatter.prototype.__proto__ = WebInspector.SourceHTMLTokenizer.prototype;
115
116 function require()
117 {
118     return parse;
119 }
120
121 var exports = {};
122 importScripts("UglifyJS/parse-js.js");
123 var parse = exports;
124
125 importScripts("JavaScriptFormatter.js");