├── .project
└── WebContent
├── WebInspector.js
├── background.js
├── beautify.js
├── bootstrap.js
├── content.js
├── cssbeautify.js
├── icon_128.png
├── icon_16.png
├── icon_48.png
├── manifest.json
├── options.html
├── options.js
├── worker-WebInspector.js
├── worker-beautify.js
└── worker-cssbeautify.js
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | PrettyPrint
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.wst.jsdt.core.javascriptValidator
10 |
11 |
12 |
13 |
14 | org.eclipse.wst.common.project.facet.core.builder
15 |
16 |
17 |
18 |
19 | org.eclipse.wst.validation.validationbuilder
20 |
21 |
22 |
23 |
24 |
25 | org.eclipse.wst.common.project.facet.core.nature
26 | org.eclipse.wst.common.modulecore.ModuleCoreNature
27 | org.eclipse.wst.jsdt.core.jsNature
28 |
29 |
30 |
--------------------------------------------------------------------------------
/WebContent/background.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | var menuId;
22 |
23 | function getOptions() {
24 | return localStorage.options ? JSON.parse(localStorage.options) : {
25 | auto_indentation : true,
26 | braces_on_own_line : false,
27 | indent_size : 4,
28 | indent_char : " ",
29 | preserve_newlines : true,
30 | space_after_anon_function : false,
31 | keep_array_indentation : false,
32 | css_auto_indentation : true,
33 | css_indent_size : 4,
34 | css_indent_char : " ",
35 | css_braces_on_own_line : false,
36 | use_contextmenu : false
37 | };
38 | }
39 |
40 | function resetOptions() {
41 | delete localStorage.options;
42 | }
43 |
44 | function setOptions(options) {
45 | localStorage.options = JSON.stringify(options);
46 | refreshContextMenu(options);
47 | }
48 |
49 | function refreshContextMenu(options) {
50 | if (options.use_contextmenu && !menuId)
51 | menuId = chrome.contextMenus.create({
52 | title : "pretty print",
53 | onclick : function(info, tab) {
54 | if (tab.url.indexOf("file:") != 0)
55 | chrome.tabs.executeScript(tab.id, {
56 | file : "content.js"
57 | });
58 | }
59 | });
60 | if (!options.use_contextmenu && menuId)
61 | menuId = chrome.contextMenus.remove(menuId);
62 | }
63 |
64 | chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
65 | var workerBeautifyJS, workerBeautifyCSS, workerWebInspector;
66 | if (request.getOptions) {
67 | chrome.tabs.sendMessage(sender.tab.id, {
68 | options : getOptions()
69 | });
70 | if (!getOptions().use_contextmenu && sender.tab.url.indexOf("file:") != 0)
71 | chrome.tabs.executeScript(sender.tab.id, {
72 | file : "content.js"
73 | });
74 | }
75 | if (request.beautifyJS) {
76 | workerBeautifyJS = new Worker("worker-beautify.js");
77 | workerBeautifyJS.addEventListener("message", function(event) {
78 | chrome.tabs.sendMessage(sender.tab.id, {
79 | content : event.data
80 | });
81 | workerBeautifyJS.terminate();
82 | }, false);
83 | workerBeautifyJS.postMessage({
84 | text : request.text,
85 | options : getOptions()
86 | });
87 | }
88 | if (request.beautifyCSS) {
89 | workerBeautifyCSS = new Worker("worker-cssbeautify.js");
90 | workerBeautifyCSS.addEventListener("message", function(event) {
91 | chrome.tabs.sendMessage(sender.tab.id, {
92 | content : event.data
93 | });
94 | workerBeautifyCSS.terminate();
95 | }, false);
96 | workerBeautifyCSS.postMessage({
97 | text : request.text,
98 | options : getOptions()
99 | });
100 | }
101 | if (request.syntaxHighlight) {
102 | workerWebInspector = new Worker("worker-WebInspector.js");
103 | workerWebInspector.addEventListener("message", function(event) {
104 | chrome.tabs.sendMessage(sender.tab.id, {
105 | content : event.data.text,
106 | linesLength : event.data.linesLength
107 | });
108 | workerWebInspector.terminate();
109 | }, false);
110 | workerWebInspector.postMessage({
111 | text : request.text,
112 | type : request.type
113 | });
114 | }
115 | return true;
116 | });
117 |
118 | refreshContextMenu(getOptions());
119 |
--------------------------------------------------------------------------------
/WebContent/beautify.js:
--------------------------------------------------------------------------------
1 | /*jslint onevar: false, plusplus: false */
2 | /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
3 | /*
4 |
5 | JS Beautifier
6 | ---------------
7 |
8 |
9 | Written by Einar Lielmanis,
10 | http://jsbeautifier.org/
11 |
12 | Originally converted to javascript by Vital,
13 | "End braces on own line" added by Chris J. Shull,
14 |
15 | You are free to use this in any way you want, in case you find this useful or working for you.
16 |
17 | Usage:
18 | js_beautify(js_source_text);
19 | js_beautify(js_source_text, options);
20 |
21 | The options are:
22 | indent_size (default 4) - indentation size,
23 | indent_char (default space) - character to indent with,
24 | preserve_newlines (default true) - whether existing line breaks should be preserved,
25 | max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk,
26 |
27 | jslint_happy (default false) - if true, then jslint-stricter mode is enforced.
28 |
29 | jslint_happy !jslint_happy
30 | ---------------------------------
31 | function () function()
32 |
33 | brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "expand-strict"
34 | put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
35 |
36 | expand-strict: put brace on own line even in such cases:
37 |
38 | var a =
39 | {
40 | a: 5,
41 | b: 6
42 | }
43 | This mode may break your scripts - e.g "return { a: 1 }" will be broken into two lines, so beware.
44 |
45 | space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)",
46 |
47 | unescape_strings (default false) - should printable characters in strings encoded in \xNN notation be unescaped, "example" vs "\x65\x78\x61\x6d\x70\x6c\x65"
48 |
49 | e.g
50 |
51 | js_beautify(js_source_text, {
52 | 'indent_size': 1,
53 | 'indent_char': '\t'
54 | });
55 |
56 |
57 | */
58 |
59 |
60 |
61 | function js_beautify(js_source_text, options) {
62 |
63 | var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
64 | var whitespace, wordchar, punct, parser_pos, line_starters, digits;
65 | var prefix, token_type, do_block_just_closed;
66 | var wanted_newline, just_added_newline, n_newlines;
67 | var preindent_string = '';
68 |
69 |
70 | // Some interpreters have unexpected results with foo = baz || bar;
71 | options = options ? options : {};
72 |
73 | var opt_brace_style;
74 |
75 | // compatibility
76 | if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) {
77 | options.jslint_happy = options.space_after_anon_function;
78 | }
79 | if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
80 | opt_brace_style = options.braces_on_own_line ? "expand" : "collapse";
81 | }
82 | opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");
83 |
84 |
85 | var opt_indent_size = options.indent_size ? options.indent_size : 4,
86 | opt_indent_char = options.indent_char ? options.indent_char : ' ',
87 | opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines,
88 | opt_break_chained_methods = typeof options.break_chained_methods === 'undefined' ? false : options.break_chained_methods,
89 | opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines,
90 | opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy,
91 | opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation,
92 | opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional,
93 | opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;
94 |
95 | just_added_newline = false;
96 |
97 | // cache the source's length.
98 | var input_length = js_source_text.length;
99 |
100 | function trim_output(eat_newlines) {
101 | eat_newlines = typeof eat_newlines === 'undefined' ? false : eat_newlines;
102 | while (output.length && (output[output.length - 1] === ' '
103 | || output[output.length - 1] === indent_string
104 | || output[output.length - 1] === preindent_string
105 | || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) {
106 | output.pop();
107 | }
108 | }
109 |
110 | function trim(s) {
111 | return s.replace(/^\s\s*|\s\s*$/, '');
112 | }
113 |
114 | // we could use just string.split, but
115 | // IE doesn't like returning empty strings
116 | function split_newlines(s) {
117 | //return s.split(/\x0d\x0a|\x0a/);
118 |
119 | s = s.replace(/\x0d/g, '');
120 | var out = [],
121 | idx = s.indexOf("\n");
122 | while (idx !== -1) {
123 | out.push(s.substring(0, idx));
124 | s = s.substring(idx + 1);
125 | idx = s.indexOf("\n");
126 | }
127 | if (s.length) {
128 | out.push(s);
129 | }
130 | return out;
131 | }
132 |
133 | function force_newline() {
134 | var old_keep_array_indentation = opt_keep_array_indentation;
135 | opt_keep_array_indentation = false;
136 | print_newline();
137 | opt_keep_array_indentation = old_keep_array_indentation;
138 | }
139 |
140 | function print_newline(ignore_repeated, reset_statement_flags) {
141 |
142 | flags.eat_next_space = false;
143 | if (opt_keep_array_indentation && is_array(flags.mode)) {
144 | return;
145 | }
146 |
147 | ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated;
148 | reset_statement_flags = typeof reset_statement_flags === 'undefined' ? true : reset_statement_flags;
149 |
150 | if (reset_statement_flags) {
151 | flags.if_line = false;
152 | flags.chain_extra_indentation = 0;
153 | }
154 |
155 | trim_output();
156 |
157 | if (!output.length) {
158 | return; // no newline on start of file
159 | }
160 |
161 | if (output[output.length - 1] !== "\n" || !ignore_repeated) {
162 | just_added_newline = true;
163 | output.push("\n");
164 | }
165 | if (preindent_string) {
166 | output.push(preindent_string);
167 | }
168 | for (var i = 0; i < flags.indentation_level + flags.chain_extra_indentation; i += 1) {
169 | output.push(indent_string);
170 | }
171 | if (flags.var_line && flags.var_line_reindented) {
172 | output.push(indent_string); // skip space-stuffing, if indenting with a tab
173 | }
174 | }
175 |
176 |
177 |
178 | function print_single_space() {
179 |
180 | if (last_type === 'TK_COMMENT') {
181 | return print_newline();
182 | }
183 | if (flags.eat_next_space) {
184 | flags.eat_next_space = false;
185 | return;
186 | }
187 | var last_output = ' ';
188 | if (output.length) {
189 | last_output = output[output.length - 1];
190 | }
191 | if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
192 | output.push(' ');
193 | }
194 | }
195 |
196 |
197 | function print_token() {
198 | just_added_newline = false;
199 | flags.eat_next_space = false;
200 | output.push(token_text);
201 | }
202 |
203 | function indent() {
204 | flags.indentation_level += 1;
205 | }
206 |
207 |
208 | function remove_indent() {
209 | if (output.length && output[output.length - 1] === indent_string) {
210 | output.pop();
211 | }
212 | }
213 |
214 | function set_mode(mode) {
215 | if (flags) {
216 | flag_store.push(flags);
217 | }
218 | flags = {
219 | previous_mode: flags ? flags.mode : 'BLOCK',
220 | mode: mode,
221 | var_line: false,
222 | var_line_tainted: false,
223 | var_line_reindented: false,
224 | in_html_comment: false,
225 | if_line: false,
226 | chain_extra_indentation: 0,
227 | in_case_statement: false, // switch(..){ INSIDE HERE }
228 | in_case: false, // we're on the exact line with "case 0:"
229 | case_body: false, // the indented case-action block
230 | eat_next_space: false,
231 | indentation_baseline: -1,
232 | indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
233 | ternary_depth: 0
234 | };
235 | }
236 |
237 | function is_array(mode) {
238 | return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
239 | }
240 |
241 | function is_expression(mode) {
242 | return in_array(mode, ['[EXPRESSION]', '(EXPRESSION)', '(FOR-EXPRESSION)', '(COND-EXPRESSION)']);
243 | }
244 |
245 | function restore_mode() {
246 | do_block_just_closed = flags.mode === 'DO_BLOCK';
247 | if (flag_store.length > 0) {
248 | var mode = flags.mode;
249 | flags = flag_store.pop();
250 | flags.previous_mode = mode;
251 | }
252 | }
253 |
254 | function all_lines_start_with(lines, c) {
255 | for (var i = 0; i < lines.length; i++) {
256 | var line = trim(lines[i]);
257 | if (line.charAt(0) !== c) {
258 | return false;
259 | }
260 | }
261 | return true;
262 | }
263 |
264 | function is_special_word(word) {
265 | return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']);
266 | }
267 |
268 | function in_array(what, arr) {
269 | for (var i = 0; i < arr.length; i += 1) {
270 | if (arr[i] === what) {
271 | return true;
272 | }
273 | }
274 | return false;
275 | }
276 |
277 | function look_up(exclude) {
278 | var local_pos = parser_pos;
279 | var c = input.charAt(local_pos);
280 | while (in_array(c, whitespace) && c !== exclude) {
281 | local_pos++;
282 | if (local_pos >= input_length) {
283 | return 0;
284 | }
285 | c = input.charAt(local_pos);
286 | }
287 | return c;
288 | }
289 |
290 | function get_next_token() {
291 | var i;
292 | var resulting_string;
293 |
294 | n_newlines = 0;
295 |
296 | if (parser_pos >= input_length) {
297 | return ['', 'TK_EOF'];
298 | }
299 |
300 | wanted_newline = false;
301 |
302 | var c = input.charAt(parser_pos);
303 | parser_pos += 1;
304 |
305 |
306 | var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
307 |
308 | if (keep_whitespace) {
309 |
310 | //
311 | // slight mess to allow nice preservation of array indentation and reindent that correctly
312 | // first time when we get to the arrays:
313 | // var a = [
314 | // ....'something'
315 | // we make note of whitespace_count = 4 into flags.indentation_baseline
316 | // so we know that 4 whitespaces in original source match indent_level of reindented source
317 | //
318 | // and afterwards, when we get to
319 | // 'something,
320 | // .......'something else'
321 | // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
322 | //
323 | var whitespace_count = 0;
324 |
325 | while (in_array(c, whitespace)) {
326 |
327 | if (c === "\n") {
328 | trim_output();
329 | output.push("\n");
330 | just_added_newline = true;
331 | whitespace_count = 0;
332 | } else {
333 | if (c === '\t') {
334 | whitespace_count += 4;
335 | } else if (c === '\r') {
336 | // nothing
337 | } else {
338 | whitespace_count += 1;
339 | }
340 | }
341 |
342 | if (parser_pos >= input_length) {
343 | return ['', 'TK_EOF'];
344 | }
345 |
346 | c = input.charAt(parser_pos);
347 | parser_pos += 1;
348 |
349 | }
350 | if (flags.indentation_baseline === -1) {
351 | flags.indentation_baseline = whitespace_count;
352 | }
353 |
354 | if (just_added_newline) {
355 | for (i = 0; i < flags.indentation_level + 1; i += 1) {
356 | output.push(indent_string);
357 | }
358 | if (flags.indentation_baseline !== -1) {
359 | for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) {
360 | output.push(' ');
361 | }
362 | }
363 | }
364 |
365 | } else {
366 | while (in_array(c, whitespace)) {
367 |
368 | if (c === "\n") {
369 | n_newlines += ((opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1 : 0 : 1);
370 | }
371 |
372 |
373 | if (parser_pos >= input_length) {
374 | return ['', 'TK_EOF'];
375 | }
376 |
377 | c = input.charAt(parser_pos);
378 | parser_pos += 1;
379 |
380 | }
381 |
382 | if (opt_preserve_newlines) {
383 | if (n_newlines > 1) {
384 | for (i = 0; i < n_newlines; i += 1) {
385 | print_newline(i === 0);
386 | just_added_newline = true;
387 | }
388 | }
389 | }
390 | wanted_newline = n_newlines > 0;
391 | }
392 |
393 |
394 | if (in_array(c, wordchar)) {
395 | if (parser_pos < input_length) {
396 | while (in_array(input.charAt(parser_pos), wordchar)) {
397 | c += input.charAt(parser_pos);
398 | parser_pos += 1;
399 | if (parser_pos === input_length) {
400 | break;
401 | }
402 | }
403 | }
404 |
405 | // small and surprisingly unugly hack for 1E-10 representation
406 | if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
407 |
408 | var sign = input.charAt(parser_pos);
409 | parser_pos += 1;
410 |
411 | var t = get_next_token();
412 | c += sign + t[0];
413 | return [c, 'TK_WORD'];
414 | }
415 |
416 | if (c === 'in') { // hack for 'in' operator
417 | return [c, 'TK_OPERATOR'];
418 | }
419 | if (wanted_newline && last_type !== 'TK_OPERATOR'
420 | && last_type !== 'TK_EQUALS'
421 | && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) {
422 | print_newline();
423 | }
424 | return [c, 'TK_WORD'];
425 | }
426 |
427 | if (c === '(' || c === '[') {
428 | return [c, 'TK_START_EXPR'];
429 | }
430 |
431 | if (c === ')' || c === ']') {
432 | return [c, 'TK_END_EXPR'];
433 | }
434 |
435 | if (c === '{') {
436 | return [c, 'TK_START_BLOCK'];
437 | }
438 |
439 | if (c === '}') {
440 | return [c, 'TK_END_BLOCK'];
441 | }
442 |
443 | if (c === ';') {
444 | return [c, 'TK_SEMICOLON'];
445 | }
446 |
447 | if (c === '/') {
448 | var comment = '';
449 | // peek for comment /* ... */
450 | var inline_comment = true;
451 | if (input.charAt(parser_pos) === '*') {
452 | parser_pos += 1;
453 | if (parser_pos < input_length) {
454 | while (parser_pos < input_length &&
455 | ! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/')) {
456 | c = input.charAt(parser_pos);
457 | comment += c;
458 | if (c === "\n" || c === "\r") {
459 | inline_comment = false;
460 | }
461 | parser_pos += 1;
462 | if (parser_pos >= input_length) {
463 | break;
464 | }
465 | }
466 | }
467 | parser_pos += 2;
468 | if (inline_comment && n_newlines === 0) {
469 | return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
470 | } else {
471 | return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
472 | }
473 | }
474 | // peek for comment // ...
475 | if (input.charAt(parser_pos) === '/') {
476 | comment = c;
477 | while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') {
478 | comment += input.charAt(parser_pos);
479 | parser_pos += 1;
480 | if (parser_pos >= input_length) {
481 | break;
482 | }
483 | }
484 | if (wanted_newline) {
485 | print_newline();
486 | }
487 | return [comment, 'TK_COMMENT'];
488 | }
489 |
490 | }
491 |
492 | if (c === "'" || // string
493 | c === '"' || // string
494 | (c === '/' &&
495 | ((last_type === 'TK_WORD' && is_special_word(last_text)) ||
496 | (last_text === ')' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) ||
497 | (last_type === 'TK_COMMA' || last_type === 'TK_COMMENT' || last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
498 | var sep = c;
499 | var esc = false;
500 | var esc1 = 0;
501 | var esc2 = 0;
502 | resulting_string = c;
503 |
504 | if (parser_pos < input_length) {
505 | if (sep === '/') {
506 | //
507 | // handle regexp separately...
508 | //
509 | var in_char_class = false;
510 | while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
511 | resulting_string += input.charAt(parser_pos);
512 | if (!esc) {
513 | esc = input.charAt(parser_pos) === '\\';
514 | if (input.charAt(parser_pos) === '[') {
515 | in_char_class = true;
516 | } else if (input.charAt(parser_pos) === ']') {
517 | in_char_class = false;
518 | }
519 | } else {
520 | esc = false;
521 | }
522 | parser_pos += 1;
523 | if (parser_pos >= input_length) {
524 | // incomplete string/rexp when end-of-file reached.
525 | // bail out with what had been received so far.
526 | return [resulting_string, 'TK_STRING'];
527 | }
528 | }
529 |
530 | } else {
531 | //
532 | // and handle string also separately
533 | //
534 | while (esc || input.charAt(parser_pos) !== sep) {
535 | resulting_string += input.charAt(parser_pos);
536 | if (esc1 && esc1 >= esc2) {
537 | esc1 = parseInt(resulting_string.substr(-esc2), 16);
538 | if (esc1 && esc1 >= 0x20 && esc1 <= 0x7e) {
539 | esc1 = String.fromCharCode(esc1);
540 | resulting_string = resulting_string.substr(0, resulting_string.length - esc2 - 2) + (((esc1 === sep) || (esc1 === '\\')) ? '\\' : '') + esc1;
541 | }
542 | esc1 = 0;
543 | }
544 | if (esc1) {
545 | esc1++;
546 | } else if (!esc) {
547 | esc = input.charAt(parser_pos) === '\\';
548 | } else {
549 | esc = false;
550 | if (opt_unescape_strings) {
551 | if (input.charAt(parser_pos) === 'x') {
552 | esc1++;
553 | esc2 = 2;
554 | } else if (input.charAt(parser_pos) === 'u') {
555 | esc1++;
556 | esc2 = 4;
557 | }
558 | }
559 | }
560 | parser_pos += 1;
561 | if (parser_pos >= input_length) {
562 | // incomplete string/rexp when end-of-file reached.
563 | // bail out with what had been received so far.
564 | return [resulting_string, 'TK_STRING'];
565 | }
566 | }
567 | }
568 |
569 |
570 |
571 | }
572 |
573 | parser_pos += 1;
574 |
575 | resulting_string += sep;
576 |
577 | if (sep === '/') {
578 | // regexps may have modifiers /regexp/MOD , so fetch those, too
579 | while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
580 | resulting_string += input.charAt(parser_pos);
581 | parser_pos += 1;
582 | }
583 | }
584 | return [resulting_string, 'TK_STRING'];
585 | }
586 |
587 | if (c === '#') {
588 |
589 |
590 | if (output.length === 0 && input.charAt(parser_pos) === '!') {
591 | // shebang
592 | resulting_string = c;
593 | while (parser_pos < input_length && c !== '\n') {
594 | c = input.charAt(parser_pos);
595 | resulting_string += c;
596 | parser_pos += 1;
597 | }
598 | output.push(trim(resulting_string) + '\n');
599 | print_newline();
600 | return get_next_token();
601 | }
602 |
603 |
604 |
605 | // Spidermonkey-specific sharp variables for circular references
606 | // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
607 | // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
608 | var sharp = '#';
609 | if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
610 | do {
611 | c = input.charAt(parser_pos);
612 | sharp += c;
613 | parser_pos += 1;
614 | } while (parser_pos < input_length && c !== '#' && c !== '=');
615 | if (c === '#') {
616 | //
617 | } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
618 | sharp += '[]';
619 | parser_pos += 2;
620 | } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
621 | sharp += '{}';
622 | parser_pos += 2;
623 | }
624 | return [sharp, 'TK_WORD'];
625 | }
626 | }
627 |
628 | if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '') {
640 | flags.in_html_comment = false;
641 | parser_pos += 2;
642 | if (wanted_newline) {
643 | print_newline();
644 | }
645 | return ['-->', 'TK_COMMENT'];
646 | }
647 |
648 | if (c === '.') {
649 | return [c, 'TK_DOT'];
650 | }
651 |
652 | if (in_array(c, punct)) {
653 | while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
654 | c += input.charAt(parser_pos);
655 | parser_pos += 1;
656 | if (parser_pos >= input_length) {
657 | break;
658 | }
659 | }
660 |
661 | if (c === ',') {
662 | return [c, 'TK_COMMA'];
663 | } else if (c === '=') {
664 | return [c, 'TK_EQUALS'];
665 | } else {
666 | return [c, 'TK_OPERATOR'];
667 | }
668 | }
669 |
670 | return [c, 'TK_UNKNOWN'];
671 | }
672 |
673 | //----------------------------------
674 | indent_string = '';
675 | while (opt_indent_size > 0) {
676 | indent_string += opt_indent_char;
677 | opt_indent_size -= 1;
678 | }
679 |
680 | while (js_source_text && (js_source_text.charAt(0) === ' ' || js_source_text.charAt(0) === '\t')) {
681 | preindent_string += js_source_text.charAt(0);
682 | js_source_text = js_source_text.substring(1);
683 | }
684 | input = js_source_text;
685 |
686 | last_word = ''; // last 'TK_WORD' passed
687 | last_type = 'TK_START_EXPR'; // last token type
688 | last_text = ''; // last token text
689 | last_last_text = ''; // pre-last token text
690 | output = [];
691 |
692 | do_block_just_closed = false;
693 |
694 | whitespace = "\n\r\t ".split('');
695 | wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
696 | digits = '0123456789'.split('');
697 |
698 | punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::';
699 | punct += ' <%= <% %> = ?>'; // try to be a good boy and try not to break the markup language identifiers
700 | punct = punct.split(' ');
701 |
702 | // words which should always start on new line.
703 | line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
704 |
705 | // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
706 | // some formatting depends on that.
707 | flag_store = [];
708 | set_mode('BLOCK');
709 |
710 | parser_pos = 0;
711 | while (true) {
712 | var t = get_next_token();
713 | token_text = t[0];
714 | token_type = t[1];
715 | if (token_type === 'TK_EOF') {
716 | break;
717 | }
718 |
719 | switch (token_type) {
720 |
721 | case 'TK_START_EXPR':
722 |
723 | if (token_text === '[') {
724 |
725 | if (last_type === 'TK_WORD' || last_text === ')') {
726 | // this is array index specifier, break immediately
727 | // a[x], fn()[x]
728 | if (in_array(last_text, line_starters)) {
729 | print_single_space();
730 | }
731 | set_mode('(EXPRESSION)');
732 | print_token();
733 | break;
734 | }
735 |
736 | if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
737 | if (last_last_text === ']' && last_text === ',') {
738 | // ], [ goes to new line
739 | if (flags.mode === '[EXPRESSION]') {
740 | flags.mode = '[INDENTED-EXPRESSION]';
741 | if (!opt_keep_array_indentation) {
742 | indent();
743 | }
744 | }
745 | set_mode('[EXPRESSION]');
746 | if (!opt_keep_array_indentation) {
747 | print_newline();
748 | }
749 | } else if (last_text === '[') {
750 | if (flags.mode === '[EXPRESSION]') {
751 | flags.mode = '[INDENTED-EXPRESSION]';
752 | if (!opt_keep_array_indentation) {
753 | indent();
754 | }
755 | }
756 | set_mode('[EXPRESSION]');
757 |
758 | if (!opt_keep_array_indentation) {
759 | print_newline();
760 | }
761 | } else {
762 | set_mode('[EXPRESSION]');
763 | }
764 | } else {
765 | set_mode('[EXPRESSION]');
766 | }
767 |
768 |
769 |
770 | } else {
771 | if (last_word === 'for') {
772 | set_mode('(FOR-EXPRESSION)');
773 | } else if (in_array(last_word, ['if', 'while'])) {
774 | set_mode('(COND-EXPRESSION)');
775 | } else {
776 | set_mode('(EXPRESSION)');
777 | }
778 | }
779 |
780 | if (last_text === ';' || last_type === 'TK_START_BLOCK') {
781 | print_newline();
782 | } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') {
783 | if (wanted_newline) {
784 | print_newline();
785 | }
786 | // do nothing on (( and )( and ][ and ]( and .(
787 | } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
788 | print_single_space();
789 | } else if (last_word === 'function' || last_word === 'typeof') {
790 | // function() vs function ()
791 | if (opt_jslint_happy) {
792 | print_single_space();
793 | }
794 | } else if (in_array(last_text, line_starters) || last_text === 'catch') {
795 | if (opt_space_before_conditional) {
796 | print_single_space();
797 | }
798 | }
799 | print_token();
800 |
801 | break;
802 |
803 | case 'TK_DOT':
804 |
805 | if (is_special_word(last_text)) {
806 | print_single_space();
807 | } else if (last_text === ')') {
808 | if (opt_break_chained_methods || wanted_newline) {
809 | flags.chain_extra_indentation = 1;
810 | print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
811 | }
812 | }
813 |
814 | print_token();
815 | break;
816 |
817 | case 'TK_END_EXPR':
818 | if (token_text === ']') {
819 | if (opt_keep_array_indentation) {
820 | if (last_text === '}') {
821 | // trim_output();
822 | // print_newline(true);
823 | remove_indent();
824 | print_token();
825 | restore_mode();
826 | break;
827 | }
828 | } else {
829 | if (flags.mode === '[INDENTED-EXPRESSION]') {
830 | if (last_text === ']') {
831 | restore_mode();
832 | print_newline();
833 | print_token();
834 | break;
835 | }
836 | }
837 | }
838 | }
839 | restore_mode();
840 | print_token();
841 | break;
842 |
843 | case 'TK_START_BLOCK':
844 |
845 | if (last_word === 'do') {
846 | set_mode('DO_BLOCK');
847 | } else {
848 | set_mode('BLOCK');
849 | }
850 | if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") {
851 | var empty_braces = false;
852 | if (opt_brace_style === "expand-strict") {
853 | empty_braces = (look_up() === '}');
854 | if (!empty_braces) {
855 | print_newline(true);
856 | }
857 | } else {
858 | if (last_type !== 'TK_OPERATOR') {
859 | if (last_text === '=' || (is_special_word(last_text) && last_text !== 'else')) {
860 | print_single_space();
861 | } else {
862 | print_newline(true);
863 | }
864 | }
865 | }
866 | print_token();
867 | if (!empty_braces) {
868 | indent();
869 | }
870 | } else {
871 | if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
872 | if (last_type === 'TK_START_BLOCK') {
873 | print_newline();
874 | } else {
875 | print_single_space();
876 | }
877 | } else {
878 | // if TK_OPERATOR or TK_START_EXPR
879 | if (is_array(flags.previous_mode) && last_text === ',') {
880 | if (last_last_text === '}') {
881 | // }, { in array context
882 | print_single_space();
883 | } else {
884 | print_newline(); // [a, b, c, {
885 | }
886 | }
887 | }
888 | indent();
889 | print_token();
890 | }
891 |
892 | break;
893 |
894 | case 'TK_END_BLOCK':
895 | restore_mode();
896 | if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") {
897 | if (last_text !== '{') {
898 | print_newline();
899 | }
900 | print_token();
901 | } else {
902 | if (last_type === 'TK_START_BLOCK') {
903 | // nothing
904 | if (just_added_newline) {
905 | remove_indent();
906 | } else {
907 | // {}
908 | trim_output();
909 | }
910 | } else {
911 | if (is_array(flags.mode) && opt_keep_array_indentation) {
912 | // we REALLY need a newline here, but newliner would skip that
913 | opt_keep_array_indentation = false;
914 | print_newline();
915 | opt_keep_array_indentation = true;
916 |
917 | } else {
918 | print_newline();
919 | }
920 | }
921 | print_token();
922 | }
923 | break;
924 |
925 | case 'TK_WORD':
926 |
927 | // no, it's not you. even I have problems understanding how this works
928 | // and what does what.
929 | if (do_block_just_closed) {
930 | // do {} ## while ()
931 | print_single_space();
932 | print_token();
933 | print_single_space();
934 | do_block_just_closed = false;
935 | break;
936 | }
937 |
938 | prefix = 'NONE';
939 |
940 | if (token_text === 'function') {
941 | if (flags.var_line && last_type !== 'TK_EQUALS' ) {
942 | flags.var_line_reindented = true;
943 | }
944 | if ((just_added_newline || last_text === ';') && last_text !== '{'
945 | && last_type !== 'TK_BLOCK_COMMENT' && last_type !== 'TK_COMMENT') {
946 | // make sure there is a nice clean space of at least one blank line
947 | // before a new function definition
948 | n_newlines = just_added_newline ? n_newlines : 0;
949 | if (!opt_preserve_newlines) {
950 | n_newlines = 1;
951 | }
952 |
953 | for (var i = 0; i < 2 - n_newlines; i++) {
954 | print_newline(false);
955 | }
956 | }
957 | if (last_type === 'TK_WORD') {
958 | if (last_text === 'get' || last_text === 'set' || last_text === 'new' || last_text === 'return') {
959 | print_single_space();
960 | } else {
961 | print_newline();
962 | }
963 | } else if (last_type === 'TK_OPERATOR' || last_text === '=') {
964 | // foo = function
965 | print_single_space();
966 | } else if (is_expression(flags.mode)) {
967 | //ää print nothing
968 | } else {
969 | print_newline();
970 | }
971 |
972 | print_token();
973 | last_word = token_text;
974 | break;
975 | }
976 |
977 | if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) {
978 | print_newline();
979 | if (flags.case_body) {
980 | // switch cases following one another
981 | flags.indentation_level--;
982 | flags.case_body = false;
983 | remove_indent();
984 | }
985 | print_token();
986 | flags.in_case = true;
987 | flags.in_case_statement = true;
988 | break;
989 | }
990 |
991 | if (last_type === 'TK_END_BLOCK') {
992 |
993 | if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
994 | prefix = 'NEWLINE';
995 | } else {
996 | if (opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") {
997 | prefix = 'NEWLINE';
998 | } else {
999 | prefix = 'SPACE';
1000 | print_single_space();
1001 | }
1002 | }
1003 | } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
1004 | prefix = 'NEWLINE';
1005 | } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
1006 | prefix = 'SPACE';
1007 | } else if (last_type === 'TK_STRING') {
1008 | prefix = 'NEWLINE';
1009 | } else if (last_type === 'TK_WORD') {
1010 | if (last_text === 'else') {
1011 | // eat newlines between ...else *** some_op...
1012 | // won't preserve extra newlines in this place (if any), but don't care that much
1013 | trim_output(true);
1014 | }
1015 | prefix = 'SPACE';
1016 | } else if (last_type === 'TK_START_BLOCK') {
1017 | prefix = 'NEWLINE';
1018 | } else if (last_type === 'TK_END_EXPR') {
1019 | print_single_space();
1020 | prefix = 'NEWLINE';
1021 | }
1022 |
1023 | if (in_array(token_text, line_starters) && last_text !== ')') {
1024 | if (last_text === 'else') {
1025 | prefix = 'SPACE';
1026 | } else {
1027 | prefix = 'NEWLINE';
1028 | }
1029 |
1030 | }
1031 |
1032 | if (flags.if_line && last_type === 'TK_END_EXPR') {
1033 | flags.if_line = false;
1034 | }
1035 | if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
1036 | if (last_type !== 'TK_END_BLOCK' || opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") {
1037 | print_newline();
1038 | } else {
1039 | trim_output(true);
1040 | print_single_space();
1041 | }
1042 | } else if (prefix === 'NEWLINE') {
1043 | if (is_special_word(last_text)) {
1044 | // no newline between 'return nnn'
1045 | print_single_space();
1046 | } else if (last_type !== 'TK_END_EXPR') {
1047 | if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
1048 | // no need to force newline on 'var': for (var x = 0...)
1049 | if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
1050 | // no newline for } else if {
1051 | print_single_space();
1052 | } else {
1053 | flags.var_line = false;
1054 | flags.var_line_reindented = false;
1055 | print_newline();
1056 | }
1057 | }
1058 | } else if (in_array(token_text, line_starters) && last_text !== ')') {
1059 | flags.var_line = false;
1060 | flags.var_line_reindented = false;
1061 | print_newline();
1062 | }
1063 | } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') {
1064 | print_newline(); // }, in lists get a newline treatment
1065 | } else if (prefix === 'SPACE') {
1066 | print_single_space();
1067 | }
1068 | print_token();
1069 | last_word = token_text;
1070 |
1071 | if (token_text === 'var') {
1072 | flags.var_line = true;
1073 | flags.var_line_reindented = false;
1074 | flags.var_line_tainted = false;
1075 | }
1076 |
1077 | if (token_text === 'if') {
1078 | flags.if_line = true;
1079 | }
1080 | if (token_text === 'else') {
1081 | flags.if_line = false;
1082 | }
1083 |
1084 | break;
1085 |
1086 | case 'TK_SEMICOLON':
1087 |
1088 | print_token();
1089 | flags.var_line = false;
1090 | flags.var_line_reindented = false;
1091 | if (flags.mode === 'OBJECT') {
1092 | // OBJECT mode is weird and doesn't get reset too well.
1093 | flags.mode = 'BLOCK';
1094 | }
1095 | break;
1096 |
1097 | case 'TK_STRING':
1098 |
1099 | if (last_type === 'TK_END_EXPR' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) {
1100 | print_single_space();
1101 | } else if (last_type === 'TK_COMMENT' || last_type === 'TK_STRING' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') {
1102 | print_newline();
1103 | } else if (last_type === 'TK_WORD') {
1104 | print_single_space();
1105 | }
1106 | print_token();
1107 | break;
1108 |
1109 | case 'TK_EQUALS':
1110 | if (flags.var_line) {
1111 | // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
1112 | flags.var_line_tainted = true;
1113 | }
1114 | print_single_space();
1115 | print_token();
1116 | print_single_space();
1117 | break;
1118 |
1119 | case 'TK_COMMA':
1120 | if (flags.var_line) {
1121 | if (is_expression(flags.mode) || last_type === 'TK_END_BLOCK' ) {
1122 | // do not break on comma, for(var a = 1, b = 2)
1123 | flags.var_line_tainted = false;
1124 | }
1125 | if (flags.var_line_tainted) {
1126 | print_token();
1127 | flags.var_line_reindented = true;
1128 | flags.var_line_tainted = false;
1129 | print_newline();
1130 | break;
1131 | } else {
1132 | flags.var_line_tainted = false;
1133 | }
1134 |
1135 | print_token();
1136 | print_single_space();
1137 | break;
1138 | }
1139 |
1140 | if (last_type === 'TK_COMMENT') {
1141 | print_newline();
1142 | }
1143 |
1144 | if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
1145 | print_token();
1146 | if (flags.mode === 'OBJECT' && last_text === '}') {
1147 | print_newline();
1148 | } else {
1149 | print_single_space();
1150 | }
1151 | } else {
1152 | if (flags.mode === 'OBJECT') {
1153 | print_token();
1154 | print_newline();
1155 | } else {
1156 | // EXPR or DO_BLOCK
1157 | print_token();
1158 | print_single_space();
1159 | }
1160 | }
1161 | break;
1162 |
1163 |
1164 | case 'TK_OPERATOR':
1165 |
1166 | var space_before = true;
1167 | var space_after = true;
1168 | if (is_special_word(last_text)) {
1169 | // "return" had a special handling in TK_WORD. Now we need to return the favor
1170 | print_single_space();
1171 | print_token();
1172 | break;
1173 | }
1174 |
1175 | // hack for actionscript's import .*;
1176 | if (token_text === '*' && last_type === 'TK_DOT' && !last_last_text.match(/^\d+$/)) {
1177 | print_token();
1178 | break;
1179 | }
1180 |
1181 | if (token_text === ':' && flags.in_case) {
1182 | flags.case_body = true;
1183 | indent();
1184 | print_token();
1185 | print_newline();
1186 | flags.in_case = false;
1187 | break;
1188 | }
1189 |
1190 | if (token_text === '::') {
1191 | // no spaces around exotic namespacing syntax operator
1192 | print_token();
1193 | break;
1194 | }
1195 |
1196 | if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(last_text, line_starters)))) {
1197 | // unary operators (and binary +/- pretending to be unary) special cases
1198 |
1199 | space_before = false;
1200 | space_after = false;
1201 |
1202 | if (last_text === ';' && is_expression(flags.mode)) {
1203 | // for (;; ++i)
1204 | // ^^^
1205 | space_before = true;
1206 | }
1207 | if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) {
1208 | space_before = true;
1209 | }
1210 |
1211 | if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) {
1212 | // { foo; --i }
1213 | // foo(); --bar;
1214 | print_newline();
1215 | }
1216 | } else if (token_text === ':') {
1217 | if (flags.ternary_depth === 0) {
1218 | if (flags.mode === 'BLOCK') {
1219 | flags.mode = 'OBJECT';
1220 | }
1221 | space_before = false;
1222 | } else {
1223 | flags.ternary_depth -= 1;
1224 | }
1225 | } else if (token_text === '?') {
1226 | flags.ternary_depth += 1;
1227 | }
1228 | if (space_before) {
1229 | print_single_space();
1230 | }
1231 |
1232 | print_token();
1233 |
1234 | if (space_after) {
1235 | print_single_space();
1236 | }
1237 |
1238 | break;
1239 |
1240 | case 'TK_BLOCK_COMMENT':
1241 |
1242 | var lines = split_newlines(token_text);
1243 | var j; // iterator for this case
1244 |
1245 | if (all_lines_start_with(lines.slice(1), '*')) {
1246 | // javadoc: reformat and reindent
1247 | print_newline();
1248 | output.push(lines[0]);
1249 | for (j = 1; j < lines.length; j++) {
1250 | print_newline();
1251 | output.push(' ');
1252 | output.push(trim(lines[j]));
1253 | }
1254 |
1255 | } else {
1256 |
1257 | // simple block comment: leave intact
1258 | if (lines.length > 1) {
1259 | // multiline comment block starts with a new line
1260 | print_newline();
1261 | } else {
1262 | // single-line /* comment */ stays where it is
1263 | if (last_type === 'TK_END_BLOCK') {
1264 | print_newline();
1265 | } else {
1266 | print_single_space();
1267 | }
1268 |
1269 | }
1270 |
1271 | for (j = 0; j < lines.length; j++) {
1272 | output.push(lines[j]);
1273 | output.push("\n");
1274 | }
1275 |
1276 | }
1277 | if (look_up('\n') !== '\n') {
1278 | print_newline();
1279 | }
1280 | break;
1281 |
1282 | case 'TK_INLINE_COMMENT':
1283 | print_single_space();
1284 | print_token();
1285 | if (is_expression(flags.mode)) {
1286 | print_single_space();
1287 | } else {
1288 | force_newline();
1289 | }
1290 | break;
1291 |
1292 | case 'TK_COMMENT':
1293 |
1294 | if (last_text === ',' && !wanted_newline) {
1295 | trim_output(true);
1296 | }
1297 | if (last_type !== 'TK_COMMENT') {
1298 | if (wanted_newline) {
1299 | print_newline();
1300 | } else {
1301 | print_single_space();
1302 | }
1303 | }
1304 | print_token();
1305 | print_newline();
1306 | break;
1307 |
1308 | case 'TK_UNKNOWN':
1309 | print_token();
1310 | break;
1311 | }
1312 |
1313 | last_last_text = last_text;
1314 | last_type = token_type;
1315 | last_text = token_text;
1316 | }
1317 |
1318 | var sweet_code = preindent_string + output.join('').replace(/[\r\n ]+$/, '');
1319 | return sweet_code;
1320 |
1321 | }
1322 |
1323 | // Add support for CommonJS. Just put this file somewhere on your require.paths
1324 | // and you will be able to `var js_beautify = require("beautify").js_beautify`.
1325 | if (typeof exports !== "undefined") {
1326 | exports.js_beautify = js_beautify;
1327 | }
1328 |
--------------------------------------------------------------------------------
/WebContent/bootstrap.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | var options;
22 |
23 | if (document.location.pathname.substr(-4) == ".css" || document.location.pathname.substr(-3) == ".js") {
24 | chrome.extension.onMessage.addListener(function(response) {
25 | if (response.options) {
26 | options = response.options;
27 | if (document.location.protocol == "file:")
28 | process();
29 | }
30 | });
31 | chrome.extension.sendMessage({
32 | getOptions : true
33 | });
34 | }
35 |
--------------------------------------------------------------------------------
/WebContent/content.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | var cssContent = '.webkit-css-comment { color: rgb(0, 116, 0); } .webkit-css-url, .webkit-css-color, .webkit-css-string, .webkit-css-keyword { color: rgb(7, 144, 154); } .webkit-css-number { color: rgb(50, 0, 255); } .webkit-css-property, .webkit-css-at-rule { color: rgb(200, 0, 0); } .webkit-css-selector { color: black; } .webkit-css-important { color: rgb(200, 0, 180); } .webkit-javascript-comment { color: rgb(0, 116, 0); } .webkit-javascript-keyword { color: rgb(170, 13, 145); } .webkit-javascript-number { color: rgb(28, 0, 207); } .webkit-javascript-string, .webkit-javascript-regexp { color: rgb(196, 26, 22); } .webkit-javascript-ident { color: black; } .text-editor-lines { border: 0; -webkit-border-horizontal-spacing: 0; -webkit-border-vertical-spacing: 0; -webkit-user-select: text; } .webkit-line-number { color: rgb(128, 128, 128); background-color: rgb(240, 240, 240); border-right: 1px solid rgb(187, 187, 187); text-align: right; word-break: normal; -webkit-user-select: none; padding-right: 4px; padding-left: 6px; } .webkit-line-number-inner { margin-right: 4px; } .webkit-line-number-outer { margin-right: -4px; margin-left: -4px; border-color: transparent; border-style: solid; border-width: 0 0 0px 2px; vertical-align: top; } body { font-family: monospace; white-space: pre; margin: 0px; } .viewer-line-numbers { float: left; -webkit-user-select: none; } .viewer-content { display: inline-table; padding-left: 5px; }';
22 |
23 | function beautifyJS(text, callback) {
24 | function onMessage(response) {
25 | chrome.extension.onMessage.removeListener(onMessage);
26 | callback(response.content);
27 | }
28 | chrome.extension.onMessage.addListener(onMessage);
29 | chrome.extension.sendMessage({
30 | beautifyJS : true,
31 | text : text
32 | });
33 | }
34 |
35 | function beautifyCSS(text, callback) {
36 | function onMessage(response) {
37 | chrome.extension.onMessage.removeListener(onMessage);
38 | callback(response.content);
39 | }
40 | chrome.extension.onMessage.addListener(onMessage);
41 | chrome.extension.sendMessage({
42 | beautifyCSS : true,
43 | text : text
44 | });
45 | }
46 |
47 | function displayHighlightedText(text, type, node) {
48 | function onMessage(response) {
49 | chrome.extension.onMessage.removeListener(onMessage);
50 | node.innerHTML = response.content;
51 | document.body.appendChild(createViewer(node, response.linesLength));
52 | }
53 | chrome.extension.onMessage.addListener(onMessage);
54 | chrome.extension.sendMessage({
55 | syntaxHighlight : true,
56 | type : type,
57 | text : text
58 | });
59 | }
60 |
61 | function createViewer(newNode, count) {
62 | var _linesContainerElement, i, element, _lineNumberElement, innerSpan, anchor, _linesElement, contentElement, outerSpan;
63 |
64 | document.body.innerHTML = '';
65 |
66 | _linesContainerElement = document.createElement("table");
67 | _linesContainerElement.className = "text-editor-lines";
68 | _linesContainerElement.setAttribute("cellspacing", 0);
69 | _linesContainerElement.setAttribute("cellpadding", 0);
70 | for (i = 0; i < count; i++) {
71 | element = document.createElement("tr");
72 | _lineNumberElement = document.createElement("td");
73 | _lineNumberElement.className = "webkit-line-number";
74 | element.appendChild(_lineNumberElement);
75 | anchor = document.createElement("a");
76 | anchor.name = i + 1;
77 | innerSpan = document.createElement("span");
78 | innerSpan.className = "webkit-line-number-inner";
79 | innerSpan.textContent = i + 1;
80 | outerSpan = document.createElement("div");
81 | outerSpan.className = "webkit-line-number-outer";
82 | outerSpan.appendChild(anchor);
83 | outerSpan.appendChild(innerSpan);
84 | _lineNumberElement.appendChild(outerSpan);
85 | _linesContainerElement.appendChild(element);
86 | }
87 |
88 | _linesElement = document.createElement("div");
89 | _linesElement.className = "viewer-line-numbers";
90 | _linesElement.appendChild(_linesContainerElement);
91 | document.body.appendChild(_linesElement);
92 |
93 | contentElement = document.createElement("div");
94 | contentElement.className = "viewer-content";
95 | contentElement.appendChild(newNode);
96 | return contentElement;
97 | }
98 |
99 | function injectViewer(contentType, text, pathname) {
100 | var container = document.createElement("pre");
101 | if (pathname.substr(-3) == ".js"
102 | && (!contentType || (contentType.indexOf("text/plain") != -1 || contentType.indexOf("text/javascript") != -1
103 | || contentType.indexOf("application/javascript") != -1 || contentType.indexOf("application/x-javascript") != -1))) {
104 | try {
105 | new Function(text);
106 | } catch (e) {
107 | return;
108 | }
109 | if (options.auto_indentation)
110 | beautifyJS(text, function(beautifiedText) {
111 | displayHighlightedText(beautifiedText, "text/javascript", container);
112 | });
113 | else
114 | displayHighlightedText(text, "text/javascript", container);
115 | } else if (pathname.substr(-4) == ".css" && (!contentType || (contentType.indexOf("text/css") != -1 || contentType.indexOf("text/plain") != -1))) {
116 | if (options.css_auto_indentation)
117 | beautifyCSS(text, function(parsedText) {
118 | displayHighlightedText(parsedText, "text/css", container);
119 | });
120 | else
121 | displayHighlightedText(text, "text/css", container);
122 | }
123 | }
124 |
125 | function process() {
126 | var xhr, pre;
127 | if (document.location.protocol == "file:") {
128 | pre = document.querySelector("pre");
129 | if (pre && pre.textContent)
130 | injectViewer(null, pre.textContent, document.location.pathname);
131 | } else if (document.body.childNodes[0] && document.body.childNodes[0].className != "webkit-line-gutter-backdrop") {
132 | xhr = new XMLHttpRequest();
133 | xhr.onreadystatechange = function() {
134 | if (xhr.readyState == 4)
135 | injectViewer(xhr.getResponseHeader("Content-Type"), xhr.responseText, document.location.pathname);
136 | };
137 | xhr.open("GET", document.location.href, true);
138 | xhr.send(null);
139 | }
140 | }
141 |
142 | chrome.extension.onRequest.addListener(process);
143 | if (document.location.protocol != "file:")
144 | process();
145 |
--------------------------------------------------------------------------------
/WebContent/cssbeautify.js:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2012 Sencha Inc.
3 | Copyright (C) 2011 Sencha Inc.
4 |
5 | Author: Ariya Hidayat.
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in
15 | all copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | THE SOFTWARE.
24 | */
25 |
26 | /*jslint continue: true, indent: 4 */
27 | /*global exports:true, module:true, window:true */
28 |
29 | function cssbeautify(style, opt) {
30 |
31 | var options, index = 0, length = style.length, blocks, formatted = '', ch, ch2, str, state, State, depth, quote, comment, openbracesuffix = true, autosemicolon = false, trimRight;
32 |
33 | options = arguments.length > 1 ? opt : {};
34 | if (typeof options.indent === 'undefined') {
35 | options.indent = ' ';
36 | }
37 | if (typeof options.openbrace === 'string') {
38 | openbracesuffix = (options.openbrace === 'end-of-line');
39 | }
40 | if (typeof options.autosemicolon === 'boolean') {
41 | autosemicolon = options.autosemicolon;
42 | }
43 |
44 | function isWhitespace(c) {
45 | return (c === ' ') || (c === '\n') || (c === '\t') || (c === '\r') || (c === '\f');
46 | }
47 |
48 | function isQuote(c) {
49 | return (c === '\'') || (c === '"');
50 | }
51 |
52 | // FIXME: handle Unicode characters
53 | function isName(c) {
54 | return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || '-_*.:#'.indexOf(c) >= 0;
55 | }
56 |
57 | function appendIndent() {
58 | var i;
59 | for (i = depth; i > 0; i -= 1) {
60 | formatted += options.indent;
61 | }
62 | }
63 |
64 | function openBlock() {
65 | formatted = trimRight(formatted);
66 | if (openbracesuffix) {
67 | formatted += ' {';
68 | } else {
69 | formatted += '\n';
70 | appendIndent();
71 | formatted += '{';
72 | }
73 | if (ch2 !== '\n') {
74 | formatted += '\n';
75 | }
76 | depth += 1;
77 | }
78 |
79 | function closeBlock() {
80 | depth -= 1;
81 | formatted = trimRight(formatted);
82 |
83 | if (autosemicolon) {
84 | if (formatted.charAt(formatted.length - 1) !== ';') {
85 | formatted += ';';
86 | }
87 | }
88 |
89 | formatted += '\n';
90 | appendIndent();
91 | formatted += '}';
92 | blocks.push(formatted);
93 | formatted = '';
94 | }
95 |
96 | if (String.prototype.trimRight) {
97 | trimRight = function(s) {
98 | return s.trimRight();
99 | };
100 | } else {
101 | // old Internet Explorer
102 | trimRight = function(s) {
103 | return s.replace(/\s+$/, '');
104 | };
105 | }
106 |
107 | State = {
108 | Start : 0,
109 | AtRule : 1,
110 | Block : 2,
111 | Selector : 3,
112 | Ruleset : 4,
113 | Property : 5,
114 | Separator : 6,
115 | Expression : 7,
116 | URL : 8
117 | };
118 |
119 | depth = 0;
120 | state = State.Start;
121 | comment = false;
122 | blocks = [];
123 |
124 | // We want to deal with LF (\n) only
125 | style = style.replace(/\r\n/g, '\n');
126 |
127 | while (index < length) {
128 | ch = style.charAt(index);
129 | ch2 = style.charAt(index + 1);
130 | index += 1;
131 |
132 | // Inside a string literal?
133 | if (isQuote(quote)) {
134 | formatted += ch;
135 | if (ch === quote) {
136 | quote = null;
137 | }
138 | if (ch === '\\' && ch2 === quote) {
139 | // Don't treat escaped character as the closing quote
140 | formatted += ch2;
141 | index += 1;
142 | }
143 | continue;
144 | }
145 |
146 | // Starting a string literal?
147 | if (isQuote(ch)) {
148 | formatted += ch;
149 | quote = ch;
150 | continue;
151 | }
152 |
153 | // Comment
154 | if (comment) {
155 | formatted += ch;
156 | if (ch === '*' && ch2 === '/') {
157 | comment = false;
158 | formatted += ch2;
159 | index += 1;
160 | }
161 | continue;
162 | } else {
163 | if (ch === '/' && ch2 === '*') {
164 | comment = true;
165 | formatted += ch;
166 | formatted += ch2;
167 | index += 1;
168 | continue;
169 | }
170 | }
171 |
172 | if (state === State.Start) {
173 |
174 | if (blocks.length === 0) {
175 | if (isWhitespace(ch) && formatted.length === 0) {
176 | continue;
177 | }
178 | }
179 |
180 | // Copy white spaces and control characters
181 | if (ch <= ' ' || ch.charCodeAt(0) >= 128) {
182 | state = State.Start;
183 | formatted += ch;
184 | continue;
185 | }
186 |
187 | // Selector or at-rule
188 | if (isName(ch) || (ch === '@')) {
189 |
190 | // Clear trailing whitespaces and linefeeds.
191 | str = trimRight(formatted);
192 |
193 | if (str.length === 0) {
194 | // If we have empty string after removing all the trailing
195 | // spaces, that means we are right after a block.
196 | // Ensure a blank line as the separator.
197 | if (blocks.length > 0) {
198 | formatted = '\n\n';
199 | }
200 | } else {
201 | // After finishing a ruleset or directive statement,
202 | // there should be one blank line.
203 | if (str.charAt(str.length - 1) === '}' || str.charAt(str.length - 1) === ';') {
204 |
205 | formatted = str + '\n\n';
206 | } else {
207 | // After block comment, keep all the linefeeds but
208 | // start from the first column (remove whitespaces prefix).
209 | while (true) {
210 | ch2 = formatted.charAt(formatted.length - 1);
211 | if (ch2 !== ' ' && ch2.charCodeAt(0) !== 9) {
212 | break;
213 | }
214 | formatted = formatted.substr(0, formatted.length - 1);
215 | }
216 | }
217 | }
218 | formatted += ch;
219 | state = (ch === '@') ? State.AtRule : State.Selector;
220 | continue;
221 | }
222 | }
223 |
224 | if (state === State.AtRule) {
225 |
226 | // ';' terminates a statement.
227 | if (ch === ';') {
228 | formatted += ch;
229 | state = State.Start;
230 | continue;
231 | }
232 |
233 | // '{' starts a block
234 | if (ch === '{') {
235 | openBlock();
236 | state = State.Block;
237 | continue;
238 | }
239 |
240 | formatted += ch;
241 | continue;
242 | }
243 |
244 | if (state === State.Block) {
245 |
246 | // Selector
247 | if (isName(ch)) {
248 |
249 | // Clear trailing whitespaces and linefeeds.
250 | str = trimRight(formatted);
251 |
252 | if (str.length === 0) {
253 | // If we have empty string after removing all the trailing
254 | // spaces, that means we are right after a block.
255 | // Ensure a blank line as the separator.
256 | if (blocks.length > 0) {
257 | formatted = '\n\n';
258 | }
259 | } else {
260 | // Insert blank line if necessary.
261 | if (str.charAt(str.length - 1) === '}') {
262 | formatted = str + '\n\n';
263 | } else {
264 | // After block comment, keep all the linefeeds but
265 | // start from the first column (remove whitespaces prefix).
266 | while (true) {
267 | ch2 = formatted.charAt(formatted.length - 1);
268 | if (ch2 !== ' ' && ch2.charCodeAt(0) !== 9) {
269 | break;
270 | }
271 | formatted = formatted.substr(0, formatted.length - 1);
272 | }
273 | }
274 | }
275 |
276 | appendIndent();
277 | formatted += ch;
278 | state = State.Selector;
279 | continue;
280 | }
281 |
282 | // '}' resets the state.
283 | if (ch === '}') {
284 | closeBlock();
285 | state = State.Start;
286 | continue;
287 | }
288 |
289 | formatted += ch;
290 | continue;
291 | }
292 |
293 | if (state === State.Selector) {
294 |
295 | // '{' starts the ruleset.
296 | if (ch === '{') {
297 | openBlock();
298 | state = State.Ruleset;
299 | continue;
300 | }
301 |
302 | // '}' resets the state.
303 | if (ch === '}') {
304 | closeBlock();
305 | state = State.Start;
306 | continue;
307 | }
308 |
309 | formatted += ch;
310 | continue;
311 | }
312 |
313 | if (state === State.Ruleset) {
314 |
315 | // '}' finishes the ruleset.
316 | if (ch === '}') {
317 | closeBlock();
318 | state = State.Start;
319 | if (depth > 0) {
320 | state = State.Block;
321 | }
322 | continue;
323 | }
324 |
325 | // Make sure there is no blank line or trailing spaces inbetween
326 | if (ch === '\n') {
327 | formatted = trimRight(formatted);
328 | formatted += '\n';
329 | continue;
330 | }
331 |
332 | // property name
333 | if (!isWhitespace(ch)) {
334 | formatted = trimRight(formatted);
335 | formatted += '\n';
336 | appendIndent();
337 | formatted += ch;
338 | state = State.Property;
339 | continue;
340 | }
341 | formatted += ch;
342 | continue;
343 | }
344 |
345 | if (state === State.Property) {
346 |
347 | // ':' concludes the property.
348 | if (ch === ':') {
349 | formatted = trimRight(formatted);
350 | formatted += ': ';
351 | state = State.Expression;
352 | if (isWhitespace(ch2)) {
353 | state = State.Separator;
354 | }
355 | continue;
356 | }
357 |
358 | // '}' finishes the ruleset.
359 | if (ch === '}') {
360 | closeBlock();
361 | state = State.Start;
362 | if (depth > 0) {
363 | state = State.Block;
364 | }
365 | continue;
366 | }
367 |
368 | formatted += ch;
369 | continue;
370 | }
371 |
372 | if (state === State.Separator) {
373 |
374 | // Non-whitespace starts the expression.
375 | if (!isWhitespace(ch)) {
376 | formatted += ch;
377 | state = State.Expression;
378 | continue;
379 | }
380 |
381 | // Anticipate string literal.
382 | if (isQuote(ch2)) {
383 | state = State.Expression;
384 | }
385 |
386 | continue;
387 | }
388 |
389 | if (state === State.Expression) {
390 |
391 | // '}' finishes the ruleset.
392 | if (ch === '}') {
393 | closeBlock();
394 | state = State.Start;
395 | if (depth > 0) {
396 | state = State.Block;
397 | }
398 | continue;
399 | }
400 |
401 | // ';' completes the declaration.
402 | if (ch === ';') {
403 | formatted = trimRight(formatted);
404 | formatted += ';\n';
405 | state = State.Ruleset;
406 | continue;
407 | }
408 |
409 | formatted += ch;
410 |
411 | if (ch === '(') {
412 | if (formatted.charAt(formatted.length - 2) === 'l' && formatted.charAt(formatted.length - 3) === 'r'
413 | && formatted.charAt(formatted.length - 4) === 'u') {
414 |
415 | // URL starts with '(' and closes with ')'.
416 | state = State.URL;
417 | continue;
418 | }
419 | }
420 |
421 | continue;
422 | }
423 |
424 | if (state === State.URL) {
425 |
426 | // ')' finishes the URL (only if it is not escaped).
427 | if (ch === ')' && formatted.charAt(formatted.length - 1 !== '\\')) {
428 | formatted += ch;
429 | state = State.Expression;
430 | continue;
431 | }
432 | }
433 |
434 | // The default action is to copy the character (to prevent
435 | // infinite loop).
436 | formatted += ch;
437 | }
438 |
439 | formatted = blocks.join('') + formatted;
440 |
441 | return formatted;
442 | }
443 |
--------------------------------------------------------------------------------
/WebContent/icon_128.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gildas-lormeau/PrettyPrint/7cdcf5eeea5759b680aef3ee49f03c8e45e33ea8/WebContent/icon_128.png
--------------------------------------------------------------------------------
/WebContent/icon_16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gildas-lormeau/PrettyPrint/7cdcf5eeea5759b680aef3ee49f03c8e45e33ea8/WebContent/icon_16.png
--------------------------------------------------------------------------------
/WebContent/icon_48.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gildas-lormeau/PrettyPrint/7cdcf5eeea5759b680aef3ee49f03c8e45e33ea8/WebContent/icon_48.png
--------------------------------------------------------------------------------
/WebContent/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "PrettyPrint",
3 | "icons": {
4 | "16": "icon_16.png",
5 | "48": "icon_48.png",
6 | "128": "icon_128.png"
7 | },
8 | "version": "0.0.25",
9 | "description": "JavaScript and CSS formatter/syntax highlighter",
10 | "content_scripts": [{
11 | "matches": [ "http://*/*", "https://*/*", "ftp://*/*" ],
12 | "js": [ "bootstrap.js" ],
13 | "run_at" : "document_end"
14 | }, {
15 | "matches": [ "file:///*" ],
16 | "js": [ "content.js", "bootstrap.js" ],
17 | "run_at" : "document_end"
18 | }],
19 | "background": {
20 | "scripts": [
21 | "background.js"
22 | ]
23 | },
24 | "options_page": "options.html",
25 | "permissions": [ "contextMenus", "tabs", "http://*/", "https://*/", "ftp://*/" ],
26 | "manifest_version": 2
27 | }
--------------------------------------------------------------------------------
/WebContent/options.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | PrettyPrint options
5 |
6 |
43 |
44 |
45 |
46 |
PrettyPrint options
47 |
48 |
General
49 |
50 |
51 |
52 |
53 |
54 |
JS formatter
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
CSS formatter
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
--------------------------------------------------------------------------------
/WebContent/options.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | var options, use_contextmenu, auto_indentation, css_auto_indentation, css_indent_char, css_indent_size, braces_on_own_line, indent_size, indent_char, preserve_newlines, space_after_anon_function, keep_array_indentation, css_braces_on_own_line, bgPage = chrome.extension.getBackgroundPage();
22 |
23 | function initForm() {
24 | options = bgPage.getOptions();
25 | use_contextmenu.checked = options.use_contextmenu;
26 | auto_indentation.checked = options.auto_indentation;
27 | braces_on_own_line.checked = options.braces_on_own_line;
28 | indent_size.value = options.indent_size;
29 | indent_char.value = options.indent_char;
30 | preserve_newlines.checked = options.preserve_newlines;
31 | space_after_anon_function.checked = options.space_after_anon_function;
32 | keep_array_indentation.checked = options.keep_array_indentation;
33 | css_auto_indentation.checked = options.css_auto_indentation;
34 | css_indent_char.value = options.css_indent_char;
35 | css_indent_size.value = options.css_indent_size;
36 | css_braces_on_own_line.checked = options.css_braces_on_own_line;
37 | }
38 |
39 | function load() {
40 | use_contextmenu = document.getElementById("use_contextmenu");
41 | auto_indentation = document.getElementById("auto_indentation");
42 | braces_on_own_line = document.getElementById("braces_on_own_line");
43 | indent_size = document.getElementById("indent_size");
44 | indent_char = document.getElementById("indent_char");
45 | preserve_newlines = document.getElementById("preserve_newlines");
46 | space_after_anon_function = document.getElementById("space_after_anon_function");
47 | keep_array_indentation = document.getElementById("keep_array_indentation");
48 | css_auto_indentation = document.getElementById("css_auto_indentation");
49 | css_indent_char = document.getElementById("css_indent_char");
50 | css_indent_size = document.getElementById("css_indent_size");
51 | css_braces_on_own_line = document.getElementById("css_braces_on_own_line");
52 | initForm();
53 |
54 | document.getElementById("reset").onclick = function() {
55 | bgPage.resetOptions();
56 | initForm();
57 | };
58 |
59 | document.getElementById("save").onclick = function() {
60 | options.use_contextmenu = use_contextmenu.checked;
61 | options.auto_indentation = auto_indentation.checked;
62 | options.braces_on_own_line = braces_on_own_line.checked;
63 | options.indent_size = indent_size.value;
64 | options.indent_char = indent_char.value;
65 | options.preserve_newlines = preserve_newlines.checked;
66 | options.space_after_anon_function = space_after_anon_function.checked;
67 | options.keep_array_indentation = keep_array_indentation.checked;
68 | options.css_auto_indentation = css_auto_indentation.checked;
69 | options.css_indent_char = css_indent_char.value;
70 | options.css_indent_size = css_indent_size.value;
71 | options.css_braces_on_own_line = css_braces_on_own_line.checked;
72 | bgPage.setOptions(options);
73 | };
74 | };
75 |
76 | addEventListener("load", load, false);
--------------------------------------------------------------------------------
/WebContent/worker-WebInspector.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | importScripts('WebInspector.js');
22 | addEventListener("message", function(event) {
23 | postMessage(new WebInspector.DOMSyntaxHighlighter(event.data.type).syntaxHighlightNode(event.data.text));
24 | }, false);
25 |
--------------------------------------------------------------------------------
/WebContent/worker-beautify.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | importScripts('beautify.js');
22 | addEventListener("message", function(event) {
23 | postMessage(js_beautify(event.data.text, event.data.options));
24 | }, false);
--------------------------------------------------------------------------------
/WebContent/worker-cssbeautify.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Gildas Lormeau
3 | * contact : gildas.lormeau gmail.com
4 | *
5 | * This file is part of PrettyPrint.
6 | *
7 | * PrettyPrint is free software: you can redistribute it and/or modify
8 | * it under the terms of the GNU Lesser General Public License as published by
9 | * the Free Software Foundation, either version 3 of the License, or
10 | * (at your option) any later version.
11 | *
12 | * PrettyPrint is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | * GNU Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public License
18 | * along with PrettyPrint. If not, see .
19 | */
20 |
21 | importScripts('cssbeautify.js');
22 |
23 | addEventListener("message", function(event) {
24 | var options = event.data.options, i, indent = "";
25 | for (i = 0; i < options.css_indent_size; i++)
26 | indent += options.css_indent_char;
27 | postMessage(cssbeautify(event.data.text, {
28 | indent : indent,
29 | openbrace : options.css_braces_on_own_line ? "separate-line" : "end-of-line"
30 | }));
31 | }, false);
32 |
--------------------------------------------------------------------------------