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

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
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 25 matching lines...) Expand all
36 WebInspector = {}; 36 WebInspector = {};
37 importScripts("CodeMirrorUtils.js"); 37 importScripts("CodeMirrorUtils.js");
38 38
39 onmessage = function(event) { 39 onmessage = function(event) {
40 if (!event.data.method) 40 if (!event.data.method)
41 return; 41 return;
42 42
43 self[event.data.method](event.data.params); 43 self[event.data.method](event.data.params);
44 }; 44 };
45 45
46 /**
47 * @param {Object} params
48 */
46 function format(params) 49 function format(params)
47 { 50 {
48 // Default to a 4-space indent. 51 // Default to a 4-space indent.
49 var indentString = params.indentString || " "; 52 var indentString = params.indentString || " ";
50 var result = {}; 53 var result = {};
51 54
52 if (params.mimeType === "text/html") { 55 if (params.mimeType === "text/html") {
53 var formatter = new HTMLScriptFormatter(indentString); 56 var formatter = new HTMLScriptFormatter(indentString);
54 result = formatter.format(params.content); 57 result = formatter.format(params.content);
58 } else if (params.mimeType === "text/css") {
59 result.mapping = { original: [0], formatted: [0] };
60 result.content = formatCSS(params.content, result.mapping, 0, 0, indentS tring);
55 } else { 61 } else {
56 result.mapping = { original: [0], formatted: [0] }; 62 result.mapping = { original: [0], formatted: [0] };
57 result.content = formatScript(params.content, result.mapping, 0, 0, inde ntString); 63 result.content = formatScript(params.content, result.mapping, 0, 0, inde ntString);
58 } 64 }
59 postMessage(result); 65 postMessage(result);
60 } 66 }
61 67
68 /**
69 * @param {number} totalLength
70 * @param {number} chunkSize
71 */
62 function getChunkCount(totalLength, chunkSize) 72 function getChunkCount(totalLength, chunkSize)
63 { 73 {
64 if (totalLength <= chunkSize) 74 if (totalLength <= chunkSize)
65 return 1; 75 return 1;
66 76
67 var remainder = totalLength % chunkSize; 77 var remainder = totalLength % chunkSize;
68 var partialLength = totalLength - remainder; 78 var partialLength = totalLength - remainder;
69 return (partialLength / chunkSize) + (remainder ? 1 : 0); 79 return (partialLength / chunkSize) + (remainder ? 1 : 0);
70 } 80 }
71 81
82 /**
83 * @param {Object} params
84 */
72 function outline(params) 85 function outline(params)
73 { 86 {
74 const chunkSize = 100000; // characters per data chunk 87 const chunkSize = 100000; // characters per data chunk
75 const totalLength = params.content.length; 88 const totalLength = params.content.length;
76 const lines = params.content.split("\n"); 89 const lines = params.content.split("\n");
77 const chunkCount = getChunkCount(totalLength, chunkSize); 90 const chunkCount = getChunkCount(totalLength, chunkSize);
78 var outlineChunk = []; 91 var outlineChunk = [];
79 var previousIdentifier = null; 92 var previousIdentifier = null;
80 var previousToken = null; 93 var previousToken = null;
81 var previousTokenType = null; 94 var previousTokenType = null;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ }); 148 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ });
136 outlineChunk = []; 149 outlineChunk = [];
137 processedChunkCharacters = 0; 150 processedChunkCharacters = 0;
138 } 151 }
139 } 152 }
140 tokenizer(line, processToken); 153 tokenizer(line, processToken);
141 } 154 }
142 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); 155 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount });
143 } 156 }
144 157
158 /**
159 * @param {string} content
160 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping
161 * @param {number} offset
162 * @param {number} formattedOffset
163 * @param {string} indentString
164 * @return {string}
165 */
145 function formatScript(content, mapping, offset, formattedOffset, indentString) 166 function formatScript(content, mapping, offset, formattedOffset, indentString)
146 { 167 {
147 var formattedContent; 168 var formattedContent;
148 try { 169 try {
149 var tokenizer = new Tokenizer(content); 170 var tokenizer = new Tokenizer(content);
150 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString); 171 var builder = new JavaScriptFormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString);
151 var formatter = new JavaScriptFormatter(tokenizer, builder); 172 var formatter = new JavaScriptFormatter(tokenizer, builder);
152 formatter.format(); 173 formatter.format();
153 formattedContent = builder.content(); 174 formattedContent = builder.content();
154 } catch (e) { 175 } catch (e) {
155 formattedContent = content; 176 formattedContent = content;
156 } 177 }
157 return formattedContent; 178 return formattedContent;
158 } 179 }
159 180
181 /**
182 * @param {string} content
183 * @param {{original: Array.<number>, formatted: Array.<number>}} mapping
184 * @param {number} offset
185 * @param {number} formattedOffset
186 * @param {string} indentString
187 * @return {string}
188 */
189 function formatCSS(content, mapping, offset, formattedOffset, indentString)
pfeldman 2013/09/30 15:46:06 Should not be global
190 {
191 var formattedContent;
192 try {
193 var builder = new CSSFormattedContentBuilder(content, mapping, offset, f ormattedOffset, indentString);
194 var formatter = new CSSFormatter(content, builder);
195 formatter.format();
196 formattedContent = builder.content();
197 } catch (e) {
198 formattedContent = content;
199 }
200 return formattedContent;
201 }
202
160 Array.prototype.keySet = function() 203 Array.prototype.keySet = function()
161 { 204 {
162 var keys = {}; 205 var keys = {};
163 for (var i = 0; i < this.length; ++i) 206 for (var i = 0; i < this.length; ++i)
164 keys[this[i]] = true; 207 keys[this[i]] = true;
165 return keys; 208 return keys;
166 }; 209 };
167 210
211 /**
212 * @param {string} indentString
213 */
168 HTMLScriptFormatter = function(indentString) 214 HTMLScriptFormatter = function(indentString)
vsevik 2013/09/30 16:12:13 HTMLFormatter?
169 { 215 {
170 this._indentString = indentString; 216 this._indentString = indentString;
171 } 217 }
172 218
173 HTMLScriptFormatter.prototype = { 219 HTMLScriptFormatter.prototype = {
220 /**
221 * @param {string} content
222 */
174 format: function(content) 223 format: function(content)
175 { 224 {
176 this.line = content; 225 this.line = content;
177 this._content = content; 226 this._content = content;
178 this._formattedContent = ""; 227 this._formattedContent = "";
179 this._mapping = { original: [0], formatted: [0] }; 228 this._mapping = { original: [0], formatted: [0] };
180 this._position = 0; 229 this._position = 0;
181 230
182 var scriptOpened = false; 231 var scriptOpened = false;
232 var styleOpened = false;
183 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html" ); 233 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html" );
184 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { 234 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) {
185 if (tokenValue.toLowerCase() === "<script" && tokenType === "xml-tag ") { 235 if (tokenType !== "xml-tag")
236 return;
237 if (tokenValue.toLowerCase() === "<script") {
186 scriptOpened = true; 238 scriptOpened = true;
187 } else if (scriptOpened && tokenValue === ">" && tokenType === "xml- tag") { 239 } else if (scriptOpened && tokenValue === ">") {
188 scriptOpened = false; 240 scriptOpened = false;
189 this._scriptStarted(tokenEnd); 241 this._scriptStarted(tokenEnd);
190 } else if (tokenValue.toLowerCase() === "</script" && tokenType === "xml-tag") { 242 } else if (tokenValue.toLowerCase() === "</script") {
191 this._scriptEnded(tokenStart); 243 this._scriptEnded(tokenStart);
244 } else if (tokenValue.toLowerCase() === "<style") {
245 styleOpened = true;
246 } else if (styleOpened && tokenValue === ">") {
247 styleOpened = false;
248 this._styleStarted(tokenEnd);
249 } else if (tokenValue.toLowerCase() === "</style") {
250 this._styleEnded(tokenStart);
192 } 251 }
193 } 252 }
194 tokenizer(content, processToken.bind(this)); 253 tokenizer(content, processToken.bind(this));
195 254
196 this._formattedContent += this._content.substring(this._position); 255 this._formattedContent += this._content.substring(this._position);
197 return { content: this._formattedContent, mapping: this._mapping }; 256 return { content: this._formattedContent, mapping: this._mapping };
198 }, 257 },
199 258
259 /**
260 * @param {number} cursor
261 */
200 _scriptStarted: function(cursor) 262 _scriptStarted: function(cursor)
201 { 263 {
264 this._handleSubFormatterStart(cursor);
265 },
266
267 /**
268 * @param {number} cursor
269 */
270 _scriptEnded: function(cursor)
271 {
272 this._handleSubFormatterEnd(formatScript, cursor);
273 },
274
275 /**
276 * @param {number} cursor
277 */
278 _styleStarted: function(cursor)
279 {
280 this._handleSubFormatterStart(cursor);
281 },
282
283 /**
284 * @param {number} cursor
285 */
286 _styleEnded: function(cursor)
287 {
288 this._handleSubFormatterEnd(formatCSS, cursor);
289 },
290
291 /**
292 * @param {number} cursor
293 */
294 _handleSubFormatterStart: function(cursor)
295 {
202 this._formattedContent += this._content.substring(this._position, cursor ); 296 this._formattedContent += this._content.substring(this._position, cursor );
203 this._formattedContent += "\n"; 297 this._formattedContent += "\n";
204 this._position = cursor; 298 this._position = cursor;
205 }, 299 },
206 300
207 _scriptEnded: function(cursor) 301 /**
302 * @param {function(string, Array.<number>, number, number, string)} formatF unction
303 * @param {number} cursor
304 */
305 _handleSubFormatterEnd: function(formatFunction, cursor)
208 { 306 {
209 if (cursor === this._position) 307 if (cursor === this._position)
210 return; 308 return;
211 309
212 var scriptContent = this._content.substring(this._position, cursor); 310 var scriptContent = this._content.substring(this._position, cursor);
213 this._mapping.original.push(this._position); 311 this._mapping.original.push(this._position);
214 this._mapping.formatted.push(this._formattedContent.length); 312 this._mapping.formatted.push(this._formattedContent.length);
215 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString); 313 var formattedScriptContent = formatFunction(scriptContent, this._mapping , this._position, this._formattedContent.length, this._indentString);
216 314
217 this._formattedContent += formattedScriptContent; 315 this._formattedContent += formattedScriptContent;
218 this._position = cursor; 316 this._position = cursor;
219 }, 317 }
220 } 318 }
221 319
222 function require() 320 function require()
223 { 321 {
224 return parse; 322 return parse;
225 } 323 }
226 324
227 var exports = {}; 325 var exports = {};
228 importScripts("UglifyJS/parse-js.js"); 326 importScripts("UglifyJS/parse-js.js");
229 var parse = exports; 327 var parse = exports;
230 328
231 importScripts("JavaScriptFormatter.js"); 329 importScripts("JavaScriptFormatter.js");
330 importScripts("CSSFormatter.js");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698