OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 * distribution. | 13 * distribution. |
14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 * | 17 * |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
| 30 importScripts("utilities.js"); |
| 31 importScripts("cm/headlesscodemirror.js"); |
| 32 importScripts("cm/css.js"); |
| 33 importScripts("cm/javascript.js"); |
| 34 importScripts("cm/xml.js"); |
| 35 importScripts("cm/htmlmixed.js"); |
| 36 WebInspector = {}; |
| 37 importScripts("CodeMirrorUtils.js"); |
30 | 38 |
31 onmessage = function(event) { | 39 onmessage = function(event) { |
32 if (!event.data.method) | 40 if (!event.data.method) |
33 return; | 41 return; |
34 | 42 |
35 self[event.data.method](event.data.params); | 43 self[event.data.method](event.data.params); |
36 }; | 44 }; |
37 | 45 |
38 function format(params) | 46 function format(params) |
39 { | 47 { |
(...skipping 30 matching lines...) Expand all Loading... |
70 var outlineChunk = []; | 78 var outlineChunk = []; |
71 var previousIdentifier = null; | 79 var previousIdentifier = null; |
72 var previousToken = null; | 80 var previousToken = null; |
73 var previousTokenType = null; | 81 var previousTokenType = null; |
74 var currentChunk = 1; | 82 var currentChunk = 1; |
75 var processedChunkCharacters = 0; | 83 var processedChunkCharacters = 0; |
76 var addedFunction = false; | 84 var addedFunction = false; |
77 var isReadingArguments = false; | 85 var isReadingArguments = false; |
78 var argumentsText = ""; | 86 var argumentsText = ""; |
79 var currentFunction = null; | 87 var currentFunction = null; |
80 var scriptTokenizer = new WebInspector.SourceJavaScriptTokenizer(); | 88 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/javascrip
t"); |
81 scriptTokenizer.condition = scriptTokenizer.createInitialCondition(); | |
82 | |
83 for (var i = 0; i < lines.length; ++i) { | 89 for (var i = 0; i < lines.length; ++i) { |
84 var line = lines[i]; | 90 var line = lines[i]; |
85 var column = 0; | 91 function processToken(tokenValue, tokenType, column, newColumn) |
86 scriptTokenizer.line = line; | 92 { |
87 do { | 93 tokenType = tokenType ? WebInspector.CodeMirrorUtils.convertTokenTyp
e(tokenType) : null; |
88 var newColumn = scriptTokenizer.nextToken(column); | |
89 var tokenType = scriptTokenizer.tokenType; | |
90 var tokenValue = line.substring(column, newColumn); | |
91 if (tokenType === "javascript-ident") { | 94 if (tokenType === "javascript-ident") { |
92 previousIdentifier = tokenValue; | 95 previousIdentifier = tokenValue; |
93 if (tokenValue && previousToken === "function") { | 96 if (tokenValue && previousToken === "function") { |
94 // A named function: "function f...". | 97 // A named function: "function f...". |
95 currentFunction = { line: i, column: column, name: tokenValu
e }; | 98 currentFunction = { line: i, column: column, name: tokenValu
e }; |
96 addedFunction = true; | 99 addedFunction = true; |
97 previousIdentifier = null; | 100 previousIdentifier = null; |
98 } | 101 } |
99 } else if (tokenType === "javascript-keyword") { | 102 } else if (tokenType === "javascript-keyword") { |
100 if (tokenValue === "function") { | 103 if (tokenValue === "function") { |
(...skipping 19 matching lines...) Expand all Loading... |
120 argumentsText = ""; | 123 argumentsText = ""; |
121 outlineChunk.push(currentFunction); | 124 outlineChunk.push(currentFunction); |
122 } | 125 } |
123 | 126 |
124 if (tokenValue.trim().length) { | 127 if (tokenValue.trim().length) { |
125 // Skip whitespace tokens. | 128 // Skip whitespace tokens. |
126 previousToken = tokenValue; | 129 previousToken = tokenValue; |
127 previousTokenType = tokenType; | 130 previousTokenType = tokenType; |
128 } | 131 } |
129 processedChunkCharacters += newColumn - column; | 132 processedChunkCharacters += newColumn - column; |
130 column = newColumn; | |
131 | 133 |
132 if (processedChunkCharacters >= chunkSize) { | 134 if (processedChunkCharacters >= chunkSize) { |
133 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur
rentChunk++ }); | 135 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur
rentChunk++ }); |
134 outlineChunk = []; | 136 outlineChunk = []; |
135 processedChunkCharacters = 0; | 137 processedChunkCharacters = 0; |
136 } | 138 } |
137 } while (column < line.length); | 139 } |
| 140 tokenizer(line, processToken); |
138 } | 141 } |
139 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); | 142 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); |
140 } | 143 } |
141 | 144 |
142 function formatScript(content, mapping, offset, formattedOffset, indentString) | 145 function formatScript(content, mapping, offset, formattedOffset, indentString) |
143 { | 146 { |
144 var formattedContent; | 147 var formattedContent; |
145 try { | 148 try { |
146 var tokenizer = new Tokenizer(content); | 149 var tokenizer = new Tokenizer(content); |
147 var builder = new FormattedContentBuilder(tokenizer.content(), mapping,
offset, formattedOffset, indentString); | 150 var builder = new FormattedContentBuilder(tokenizer.content(), mapping,
offset, formattedOffset, indentString); |
148 var formatter = new JavaScriptFormatter(tokenizer, builder); | 151 var formatter = new JavaScriptFormatter(tokenizer, builder); |
149 formatter.format(); | 152 formatter.format(); |
150 formattedContent = builder.content(); | 153 formattedContent = builder.content(); |
151 } catch (e) { | 154 } catch (e) { |
152 formattedContent = content; | 155 formattedContent = content; |
153 } | 156 } |
154 return formattedContent; | 157 return formattedContent; |
155 } | 158 } |
156 | 159 |
157 WebInspector = {}; | |
158 | |
159 Array.prototype.keySet = function() | 160 Array.prototype.keySet = function() |
160 { | 161 { |
161 var keys = {}; | 162 var keys = {}; |
162 for (var i = 0; i < this.length; ++i) | 163 for (var i = 0; i < this.length; ++i) |
163 keys[this[i]] = true; | 164 keys[this[i]] = true; |
164 return keys; | 165 return keys; |
165 }; | 166 }; |
166 | 167 |
167 importScripts("SourceTokenizer.js"); | |
168 importScripts("SourceHTMLTokenizer.js"); | |
169 importScripts("SourceJavaScriptTokenizer.js"); | |
170 | |
171 HTMLScriptFormatter = function(indentString) | 168 HTMLScriptFormatter = function(indentString) |
172 { | 169 { |
173 WebInspector.SourceHTMLTokenizer.call(this); | |
174 this._indentString = indentString; | 170 this._indentString = indentString; |
175 } | 171 } |
176 | 172 |
177 HTMLScriptFormatter.prototype = { | 173 HTMLScriptFormatter.prototype = { |
178 format: function(content) | 174 format: function(content) |
179 { | 175 { |
180 this.line = content; | 176 this.line = content; |
181 this._content = content; | 177 this._content = content; |
182 this._formattedContent = ""; | 178 this._formattedContent = ""; |
183 this._mapping = { original: [0], formatted: [0] }; | 179 this._mapping = { original: [0], formatted: [0] }; |
184 this._position = 0; | 180 this._position = 0; |
185 | 181 |
186 var cursor = 0; | 182 var scriptOpened = false; |
187 while (cursor < this._content.length) | 183 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html"
); |
188 cursor = this.nextToken(cursor); | 184 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { |
| 185 if (tokenValue === "<script" && tokenType === "xml-tag") { |
| 186 scriptOpened = true; |
| 187 } else if (scriptOpened && tokenValue === ">" && tokenType === "xml-
tag") { |
| 188 scriptOpened = false; |
| 189 this.scriptStarted(tokenEnd); |
| 190 } else if (tokenValue === "</script" && tokenType === "xml-tag") { |
| 191 this.scriptEnded(tokenStart); |
| 192 } |
| 193 } |
| 194 tokenizer(content, processToken.bind(this)); |
189 | 195 |
190 this._formattedContent += this._content.substring(this._position); | 196 this._formattedContent += this._content.substring(this._position); |
191 return { content: this._formattedContent, mapping: this._mapping }; | 197 return { content: this._formattedContent, mapping: this._mapping }; |
192 }, | 198 }, |
193 | 199 |
194 scriptStarted: function(cursor) | 200 scriptStarted: function(cursor) |
195 { | 201 { |
196 this._formattedContent += this._content.substring(this._position, cursor
); | 202 this._formattedContent += this._content.substring(this._position, cursor
); |
197 this._formattedContent += "\n"; | 203 this._formattedContent += "\n"; |
198 this._position = cursor; | 204 this._position = cursor; |
199 }, | 205 }, |
200 | 206 |
201 scriptEnded: function(cursor) | 207 scriptEnded: function(cursor) |
202 { | 208 { |
203 if (cursor === this._position) | 209 if (cursor === this._position) |
204 return; | 210 return; |
205 | 211 |
206 var scriptContent = this._content.substring(this._position, cursor); | 212 var scriptContent = this._content.substring(this._position, cursor); |
207 this._mapping.original.push(this._position); | 213 this._mapping.original.push(this._position); |
208 this._mapping.formatted.push(this._formattedContent.length); | 214 this._mapping.formatted.push(this._formattedContent.length); |
209 var formattedScriptContent = formatScript(scriptContent, this._mapping,
this._position, this._formattedContent.length, this._indentString); | 215 var formattedScriptContent = formatScript(scriptContent, this._mapping,
this._position, this._formattedContent.length, this._indentString); |
210 | 216 |
211 this._formattedContent += formattedScriptContent; | 217 this._formattedContent += formattedScriptContent; |
212 this._position = cursor; | 218 this._position = cursor; |
213 }, | 219 }, |
214 | |
215 styleSheetStarted: function(cursor) | |
216 { | |
217 }, | |
218 | |
219 styleSheetEnded: function(cursor) | |
220 { | |
221 }, | |
222 | |
223 __proto__: WebInspector.SourceHTMLTokenizer.prototype | |
224 } | 220 } |
225 | 221 |
226 function require() | 222 function require() |
227 { | 223 { |
228 return parse; | 224 return parse; |
229 } | 225 } |
230 | 226 |
231 var exports = {}; | 227 var exports = {}; |
232 importScripts("UglifyJS/parse-js.js"); | 228 importScripts("UglifyJS/parse-js.js"); |
233 var parse = exports; | 229 var parse = exports; |
234 | 230 |
235 importScripts("JavaScriptFormatter.js"); | 231 importScripts("JavaScriptFormatter.js"); |
OLD | NEW |