Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: Source/devtools/front_end/ScriptFormatterWorker.js

Issue 18347003: DevTools: Implement CSS pretty-printing (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Comments addressed Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/devtools/front_end/ScriptFormatter.js ('k') | Source/devtools/front_end/externs.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 16 matching lines...) Expand all
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"); 30 importScripts("utilities.js");
31 importScripts("cm/headlesscodemirror.js"); 31 importScripts("cm/headlesscodemirror.js");
32 importScripts("cm/css.js"); 32 importScripts("cm/css.js");
33 importScripts("cm/javascript.js"); 33 importScripts("cm/javascript.js");
34 importScripts("cm/xml.js"); 34 importScripts("cm/xml.js");
35 importScripts("cm/htmlmixed.js"); 35 importScripts("cm/htmlmixed.js");
36 WebInspector = {}; 36 WebInspector = {};
37 FormatterWorker = {};
37 importScripts("CodeMirrorUtils.js"); 38 importScripts("CodeMirrorUtils.js");
38 39
39 onmessage = function(event) { 40 var onmessage = function(event) {
40 if (!event.data.method) 41 if (!event.data.method)
41 return; 42 return;
42 43
43 self[event.data.method](event.data.params); 44 FormatterWorker[event.data.method](event.data.params);
44 }; 45 };
45 46
46 function format(params) 47 /**
48 * @param {Object} params
49 */
50 FormatterWorker.format = function(params)
47 { 51 {
48 // Default to a 4-space indent. 52 // Default to a 4-space indent.
49 var indentString = params.indentString || " "; 53 var indentString = params.indentString || " ";
50 var result = {}; 54 var result = {};
51 55
52 if (params.mimeType === "text/html") { 56 if (params.mimeType === "text/html") {
53 var formatter = new HTMLScriptFormatter(indentString); 57 var formatter = new FormatterWorker.HTMLFormatter(indentString);
54 result = formatter.format(params.content); 58 result = formatter.format(params.content);
59 } else if (params.mimeType === "text/css") {
60 result.mapping = { original: [0], formatted: [0] };
61 result.content = FormatterWorker._formatCSS(params.content, result.mappi ng, 0, 0, indentString);
55 } else { 62 } else {
56 result.mapping = { original: [0], formatted: [0] }; 63 result.mapping = { original: [0], formatted: [0] };
57 result.content = formatScript(params.content, result.mapping, 0, 0, inde ntString); 64 result.content = FormatterWorker._formatScript(params.content, result.ma pping, 0, 0, indentString);
58 } 65 }
59 postMessage(result); 66 postMessage(result);
60 } 67 }
61 68
62 function getChunkCount(totalLength, chunkSize) 69 /**
70 * @param {number} totalLength
71 * @param {number} chunkSize
72 */
73 FormatterWorker._chunkCount = function(totalLength, chunkSize)
63 { 74 {
64 if (totalLength <= chunkSize) 75 if (totalLength <= chunkSize)
65 return 1; 76 return 1;
66 77
67 var remainder = totalLength % chunkSize; 78 var remainder = totalLength % chunkSize;
68 var partialLength = totalLength - remainder; 79 var partialLength = totalLength - remainder;
69 return (partialLength / chunkSize) + (remainder ? 1 : 0); 80 return (partialLength / chunkSize) + (remainder ? 1 : 0);
70 } 81 }
71 82
72 function outline(params) 83 /**
84 * @param {Object} params
85 */
86 FormatterWorker.outline = function(params)
73 { 87 {
74 const chunkSize = 100000; // characters per data chunk 88 const chunkSize = 100000; // characters per data chunk
75 const totalLength = params.content.length; 89 const totalLength = params.content.length;
76 const lines = params.content.split("\n"); 90 const lines = params.content.split("\n");
77 const chunkCount = getChunkCount(totalLength, chunkSize); 91 const chunkCount = FormatterWorker._chunkCount(totalLength, chunkSize);
78 var outlineChunk = []; 92 var outlineChunk = [];
79 var previousIdentifier = null; 93 var previousIdentifier = null;
80 var previousToken = null; 94 var previousToken = null;
81 var previousTokenType = null; 95 var previousTokenType = null;
82 var currentChunk = 1; 96 var currentChunk = 1;
83 var processedChunkCharacters = 0; 97 var processedChunkCharacters = 0;
84 var addedFunction = false; 98 var addedFunction = false;
85 var isReadingArguments = false; 99 var isReadingArguments = false;
86 var argumentsText = ""; 100 var argumentsText = "";
87 var currentFunction = null; 101 var currentFunction = null;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ }); 149 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ });
136 outlineChunk = []; 150 outlineChunk = [];
137 processedChunkCharacters = 0; 151 processedChunkCharacters = 0;
138 } 152 }
139 } 153 }
140 tokenizer(line, processToken); 154 tokenizer(line, processToken);
141 } 155 }
142 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); 156 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount });
143 } 157 }
144 158
145 function formatScript(content, mapping, offset, formattedOffset, indentString) 159 /**
160 * @param {string} content
161 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping
162 * @param {number} offset
163 * @param {number} formattedOffset
164 * @param {string} indentString
165 * @return {string}
166 */
167 FormatterWorker._formatScript = function(content, mapping, offset, formattedOffs et, indentString)
146 { 168 {
147 var formattedContent; 169 var formattedContent;
148 try { 170 try {
149 var tokenizer = new Tokenizer(content); 171 var tokenizer = new FormatterWorker.JavaScriptTokenizer(content);
150 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString); 172 var builder = new FormatterWorker.JavaScriptFormattedContentBuilder(toke nizer.content(), mapping, offset, formattedOffset, indentString);
151 var formatter = new JavaScriptFormatter(tokenizer, builder); 173 var formatter = new FormatterWorker.JavaScriptFormatter(tokenizer, build er);
152 formatter.format(); 174 formatter.format();
153 formattedContent = builder.content(); 175 formattedContent = builder.content();
154 } catch (e) { 176 } catch (e) {
155 formattedContent = content; 177 formattedContent = content;
156 } 178 }
157 return formattedContent; 179 return formattedContent;
158 } 180 }
159 181
160 Array.prototype.keySet = function() 182 /**
183 * @param {string} content
184 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping
185 * @param {number} offset
186 * @param {number} formattedOffset
187 * @param {string} indentString
188 * @return {string}
189 */
190 FormatterWorker._formatCSS = function(content, mapping, offset, formattedOffset, indentString)
161 { 191 {
162 var keys = {}; 192 var formattedContent;
163 for (var i = 0; i < this.length; ++i) 193 try {
164 keys[this[i]] = true; 194 var builder = new FormatterWorker.CSSFormattedContentBuilder(content, ma pping, offset, formattedOffset, indentString);
165 return keys; 195 var formatter = new FormatterWorker.CSSFormatter(content, builder);
166 }; 196 formatter.format();
197 formattedContent = builder.content();
198 } catch (e) {
199 formattedContent = content;
200 }
201 return formattedContent;
202 }
167 203
168 HTMLScriptFormatter = function(indentString) 204 /**
205 * @constructor
206 * @param {string} indentString
207 */
208 FormatterWorker.HTMLFormatter = function(indentString)
169 { 209 {
170 this._indentString = indentString; 210 this._indentString = indentString;
171 } 211 }
172 212
173 HTMLScriptFormatter.prototype = { 213 FormatterWorker.HTMLFormatter.prototype = {
214 /**
215 * @param {string} content
216 */
174 format: function(content) 217 format: function(content)
175 { 218 {
176 this.line = content; 219 this.line = content;
177 this._content = content; 220 this._content = content;
178 this._formattedContent = ""; 221 this._formattedContent = "";
179 this._mapping = { original: [0], formatted: [0] }; 222 this._mapping = { original: [0], formatted: [0] };
180 this._position = 0; 223 this._position = 0;
181 224
182 var scriptOpened = false; 225 var scriptOpened = false;
226 var styleOpened = false;
183 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html" ); 227 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html" );
184 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { 228 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) {
185 if (tokenValue.toLowerCase() === "<script" && tokenType === "xml-tag ") { 229 if (tokenType !== "xml-tag")
230 return;
231 if (tokenValue.toLowerCase() === "<script") {
186 scriptOpened = true; 232 scriptOpened = true;
187 } else if (scriptOpened && tokenValue === ">" && tokenType === "xml- tag") { 233 } else if (scriptOpened && tokenValue === ">") {
188 scriptOpened = false; 234 scriptOpened = false;
189 this._scriptStarted(tokenEnd); 235 this._scriptStarted(tokenEnd);
190 } else if (tokenValue.toLowerCase() === "</script" && tokenType === "xml-tag") { 236 } else if (tokenValue.toLowerCase() === "</script") {
191 this._scriptEnded(tokenStart); 237 this._scriptEnded(tokenStart);
238 } else if (tokenValue.toLowerCase() === "<style") {
239 styleOpened = true;
240 } else if (styleOpened && tokenValue === ">") {
241 styleOpened = false;
242 this._styleStarted(tokenEnd);
243 } else if (tokenValue.toLowerCase() === "</style") {
244 this._styleEnded(tokenStart);
192 } 245 }
193 } 246 }
194 tokenizer(content, processToken.bind(this)); 247 tokenizer(content, processToken.bind(this));
195 248
196 this._formattedContent += this._content.substring(this._position); 249 this._formattedContent += this._content.substring(this._position);
197 return { content: this._formattedContent, mapping: this._mapping }; 250 return { content: this._formattedContent, mapping: this._mapping };
198 }, 251 },
199 252
253 /**
254 * @param {number} cursor
255 */
200 _scriptStarted: function(cursor) 256 _scriptStarted: function(cursor)
201 { 257 {
258 this._handleSubFormatterStart(cursor);
259 },
260
261 /**
262 * @param {number} cursor
263 */
264 _scriptEnded: function(cursor)
265 {
266 this._handleSubFormatterEnd(FormatterWorker._formatScript, cursor);
267 },
268
269 /**
270 * @param {number} cursor
271 */
272 _styleStarted: function(cursor)
273 {
274 this._handleSubFormatterStart(cursor);
275 },
276
277 /**
278 * @param {number} cursor
279 */
280 _styleEnded: function(cursor)
281 {
282 this._handleSubFormatterEnd(FormatterWorker._formatCSS, cursor);
283 },
284
285 /**
286 * @param {number} cursor
287 */
288 _handleSubFormatterStart: function(cursor)
289 {
202 this._formattedContent += this._content.substring(this._position, cursor ); 290 this._formattedContent += this._content.substring(this._position, cursor );
203 this._formattedContent += "\n"; 291 this._formattedContent += "\n";
204 this._position = cursor; 292 this._position = cursor;
205 }, 293 },
206 294
207 _scriptEnded: function(cursor) 295 /**
296 * @param {function(string, {formatted: Array.<number>, original: Array.<num ber>}, number, number, string)} formatFunction
297 * @param {number} cursor
298 */
299 _handleSubFormatterEnd: function(formatFunction, cursor)
208 { 300 {
209 if (cursor === this._position) 301 if (cursor === this._position)
210 return; 302 return;
211 303
212 var scriptContent = this._content.substring(this._position, cursor); 304 var scriptContent = this._content.substring(this._position, cursor);
213 this._mapping.original.push(this._position); 305 this._mapping.original.push(this._position);
214 this._mapping.formatted.push(this._formattedContent.length); 306 this._mapping.formatted.push(this._formattedContent.length);
215 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString); 307 var formattedScriptContent = formatFunction(scriptContent, this._mapping , this._position, this._formattedContent.length, this._indentString);
216 308
217 this._formattedContent += formattedScriptContent; 309 this._formattedContent += formattedScriptContent;
218 this._position = cursor; 310 this._position = cursor;
219 }, 311 }
220 } 312 }
221 313
314 Array.prototype.keySet = function()
315 {
316 var keys = {};
317 for (var i = 0; i < this.length; ++i)
318 keys[this[i]] = true;
319 return keys;
320 };
321
222 function require() 322 function require()
223 { 323 {
224 return parse; 324 return parse;
225 } 325 }
226 326
227 var exports = {}; 327 /**
328 * @type {{tokenizer}}
329 */
330 var exports = { tokenizer: null };
228 importScripts("UglifyJS/parse-js.js"); 331 importScripts("UglifyJS/parse-js.js");
229 var parse = exports; 332 var parse = exports;
230 333
231 importScripts("JavaScriptFormatter.js"); 334 importScripts("JavaScriptFormatter.js");
335 importScripts("CSSFormatter.js");
OLDNEW
« no previous file with comments | « Source/devtools/front_end/ScriptFormatter.js ('k') | Source/devtools/front_end/externs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698