├── .gitignore ├── README.md ├── bower.json ├── demo └── index.php ├── doc ├── assets │ └── smiley.png ├── css │ ├── prettify.css │ └── style.css ├── index.html └── js │ └── beautify.js ├── form-autofill.jquery.json ├── index.html ├── jquery.formautofill.js ├── jquery.formautofill.min.js └── jquery.formautofill.sublime-project /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by http://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must ends with two \r. 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear on external disk 16 | .Spotlight-V100 17 | .Trashes 18 | 19 | 20 | ### Windows ### 21 | # Windows image file caches 22 | Thumbs.db 23 | ehthumbs.db 24 | 25 | # Folder config file 26 | Desktop.ini 27 | 28 | # Recycle Bin used on file shares 29 | $RECYCLE.BIN/ 30 | 31 | # Windows Installer files 32 | *.cab 33 | *.msi 34 | *.msm 35 | *.msp 36 | 37 | 38 | ### SublimeText ### 39 | # workspace files are user-specific 40 | *.sublime-workspace 41 | 42 | # project files should be checked into the repository, unless a significant 43 | # proportion of contributors will probably not be using SublimeText 44 | # *.sublime-project 45 | 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jQuery form autofill 2 | 3 | jQuery plugin to simply autofill an empty form with data. 4 | 5 | 6 | ## Documentation 7 | 8 | See complete documentation and demo 9 | 10 | 11 | ## Usage 12 | 13 | You have a __form__ 14 | 15 | ```html 16 |
17 | name 18 | email 19 | love jQuery 20 | yes 21 | no 22 |
23 | ``` 24 | 25 | And __data__ 26 | 27 | ```javascript 28 | var data = { 29 | "name": "John Doe", 30 | "email": "johndoe@mail.com", 31 | "lovejquery": "yes" 32 | } 33 | ``` 34 | 35 | __Autofill__ the form with data ? just do 36 | 37 | ```javascript 38 | $("#f").autofill( data ); 39 | ``` 40 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "form-autofill", 3 | "version": "0.1.1", 4 | "main": "jquery.formautofill.js", 5 | "ignore": [ 6 | "**/.*", 7 | "doc", 8 | "demo", 9 | "README.md", 10 | "form-autofill.jquery.json", 11 | "index.html", 12 | "*.sublime-project" 13 | ], 14 | "dependencies": { 15 | "jquery": ">=1.5" 16 | } 17 | } -------------------------------------------------------------------------------- /demo/index.php: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | jQuery form autofill demo by Creative Area 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 | 32 | 33 | 34 | 35 | 47 | 48 | 49 | 50 | 65 | 66 |
67 |
68 | 69 |

jQuery form autofill

70 | 71 | fill the form. submit ... form is autofilled by post data. 72 | 73 |
74 |

 

75 |

Demo

76 | 77 |
78 |
79 | 80 |
81 |
82 |
83 | 84 |
85 | 86 |
87 |
88 | 89 |
90 | 91 |
92 | 93 |
94 |
95 | 96 |
97 | 98 |
99 | 106 |
107 |
108 | 109 |
110 | 111 |
112 | 119 |
120 |
121 | 122 |
123 | 124 |
125 | 126 |
127 |
128 | 129 |
130 | 131 |
132 |
    133 |
  • 134 | 138 |
  • 139 |
140 |
141 |
142 | 143 |
144 | 145 |
146 |
    147 |
  • 148 | 152 |
  • 153 |
154 |
155 |
156 | 157 |
158 | 159 |
160 |
    161 |
  • 162 | 166 |
  • 167 |
  • 168 | 172 |
  • 173 |
  • 174 | 178 |
  • 179 |
  • 180 | 184 |
  • 185 |
186 |
187 |
188 | 189 |
190 | 191 |
192 |
    193 |
  • 194 | 198 |
  • 199 |
  • 200 | 204 |
  • 205 |
  • 206 | 210 |
  • 211 |
212 |
213 |
214 | 215 | 216 | 217 | 218 |
219 | 220 |
221 |
222 | 223 |
224 | php 225 |
<?php
226 | $post_json = json_encode($_POST);
227 | ?>
228 |

229 | php javascript 230 |
var postdata = <?= $post_json ?>;
231 | 232 |

233 | render javascript 234 |

235 | 						
236 | 						

237 | jQuery form autofill 238 |
$("#myform").autofill( postdata );
239 | 240 |

241 | one line 242 |
$("#myform").autofill( <?= json_encode($_POST) ?> );
243 |
244 |
245 |
246 | 247 |
248 | 249 | 252 | 253 |
254 | 255 | 256 | -------------------------------------------------------------------------------- /doc/assets/smiley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/creative-area/jQuery-form-autofill/f3640c12230d1e4653aaa4a8cc9d401645965782/doc/assets/smiley.png -------------------------------------------------------------------------------- /doc/css/prettify.css: -------------------------------------------------------------------------------- 1 | /* Pretty printing styles. Used with prettify.js. */ 2 | /* Vim sunburst theme by David Leibovic */ 3 | 4 | pre .str, code .str { color: #65B042; } /* string - green */ 5 | pre .kwd, code .kwd { color: #E28964; } /* keyword - dark pink */ 6 | pre .com, code .com { color: #AEAEAE; font-style: italic; } /* comment - gray */ 7 | pre .typ, code .typ { color: #89bdff; } /* type - light blue */ 8 | pre .lit, code .lit { color: #3387CC; } /* literal - blue */ 9 | pre .pun, code .pun { color: #fff; } /* punctuation - white */ 10 | pre .pln, code .pln { color: #fff; } /* plaintext - white */ 11 | pre .tag, code .tag { color: #89bdff; } /* html/xml tag - light blue */ 12 | pre .atn, code .atn { color: #E28964; } /* html/xml attribute name - dark pink (khaki #bdb76b)*/ 13 | pre .atv, code .atv { color: #65B042; } /* html/xml attribute value - green */ 14 | pre .dec, code .dec { color: #3387CC; } /* decimal - blue */ 15 | 16 | pre.prettyprint, code.prettyprint { 17 | background-color: #222; 18 | -moz-border-radius: 8px; 19 | -webkit-border-radius: 8px; 20 | -o-border-radius: 8px; 21 | -ms-border-radius: 8px; 22 | -khtml-border-radius: 8px; 23 | border-radius: 8px; 24 | } 25 | 26 | 27 | /*background: #E8EBEC url("images/pre-bg.gif") repeat 0 0;*/ 28 | /*padding: 18px 5px; */ 29 | /*border: 1px dotted #bbb;*/ 30 | 31 | pre.prettyprint { 32 | width: 95%; 33 | margin: 1em auto; 34 | padding: 1em; 35 | white-space: pre-wrap; 36 | 37 | font: normal 13px/18px Consolas, "Courier New", Courier, monospace; 38 | } 39 | 40 | 41 | /* Specify class=linenums on a pre to get line numbering */ 42 | ol.linenums { margin-top: 0; margin-bottom: 0; color: #AEAEAE; } /* IE indents via margin-left */ 43 | li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8 { list-style-type: none } 44 | /* Alternate shading for lines */ 45 | li.L1,li.L3,li.L5,li.L7,li.L9 { } 46 | 47 | @media print { 48 | pre .str, code .str { color: #060; } 49 | pre .kwd, code .kwd { color: #006; font-weight: bold; } 50 | pre .com, code .com { color: #600; font-style: italic; } 51 | pre .typ, code .typ { color: #404; font-weight: bold; } 52 | pre .lit, code .lit { color: #044; } 53 | pre .pun, code .pun { color: #440; } 54 | pre .pln, code .pln { color: #000; } 55 | pre .tag, code .tag { color: #006; font-weight: bold; } 56 | pre .atn, code .atn { color: #404; } 57 | pre .atv, code .atv { color: #060; } 58 | } 59 | -------------------------------------------------------------------------------- /doc/css/style.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | background-color: #eee; 3 | } 4 | body { 5 | padding-top: 40px; /* 40px to make the container go all the way to the bottom of the topbar */ 6 | } 7 | .container > footer p { 8 | text-align: center; /* center align it with the container */ 9 | } 10 | /* The white background content wrapper */ 11 | .content { 12 | background-color: #fff; 13 | padding: 20px; 14 | margin: 0 -20px; /* negative indent the amount of the padding to maintain the grid system */ 15 | -webkit-border-radius: 0 0 6px 6px; 16 | -moz-border-radius: 0 0 6px 6px; 17 | border-radius: 0 0 6px 6px; 18 | -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); 19 | -moz-box-shadow: 0 1px 2px rgba(0,0,0,.15); 20 | box-shadow: 0 1px 2px rgba(0,0,0,.15); 21 | } 22 | 23 | .resetlabel label { 24 | width: auto; 25 | padding-right: 5px; 26 | } 27 | .row div { 28 | margin-bottom: 5px; 29 | } 30 | 31 | h2 { 32 | padding-top: 40px; 33 | } 34 | 35 | /* download link span .label */ 36 | .label a { color:white; } 37 | 38 | .inputs-list-hack li { 39 | display: inline !important; 40 | padding-right: 5px !important; 41 | width: auto !important; 42 | } 43 | .inputs-list-hack li label { 44 | display: inline !important; 45 | } 46 | .smiley { 47 | padding-right: 30px; 48 | margin-left: 8px; 49 | background: url(../assets/smiley.png) no-repeat; 50 | } -------------------------------------------------------------------------------- /doc/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | jQuery form autofill by Creative Area 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 98 | 99 | 100 | 101 | 113 | 114 | 115 | 130 | 131 |
132 |
133 | 134 |

jQuery form autofill

135 | 136 | simply autofill an empty form with data. 137 | 138 |
139 |

Download

140 | 141 |

142 | jquery.formautofill.js 143 |   144 | jquery.formautofill.min.js 145 |
146 | 147 |
148 |
149 | 150 |
151 |

How to use it

152 | 153 |

154 |

you have a form

155 | 156 |
157 | <form id="f">
158 |     name <input type="text" name="name">
159 |     email <input type="text" name="email">
160 |     love jQuery
161 |     <input type="radio" name="lovejquery" value="yes"> yes
162 |     <input type="radio" name="lovejquery" value="no"> no
163 | </form>
164 | 
165 | 166 |

and data

167 | 168 |
169 | var data = {
170 |     "name": "John Doe",
171 |     "email": "johndoe@mail.com",
172 |     "lovejquery": "yes"
173 | }
174 | 
175 | 176 |

autofill the form with data ? just do

177 | 178 |
179 | $("#f").autofill( data );
180 | 
181 | 182 |
183 | 184 |
185 |
186 |

Try it !

187 |

188 |
189 | 190 |
191 |
192 |
193 | 194 |
195 |
196 |
197 | 198 |
199 | yes 200 |   201 | no 202 |
203 |
204 | 205 | 206 |
207 |

208 |
209 | 210 |
211 |
212 | 213 |
214 | 215 |

Optional parameters:

216 | 217 |

218 |

you can pass an optional object as second argument. available options with default values are

219 | 220 |
221 | var options = {
222 |     findbyname: true,
223 |     restrict: true
224 | }
225 | // autofill with options :
226 | $("#f").autofill( data, options );
227 | 
228 | 229 |

findbyname true : if true, find elements by name attribute. if false, find elements by id.

230 |

restrict true : if true, restrict the fields search in the node childs. if false, search in all the document.

231 | 232 |
233 | 234 |
235 |

Examples

236 |

237 |
238 | 239 |
240 |

findbyname false

241 | 242 |
243 |
244 |
245 | <form id="f_findbyname">
246 |     name <input type="text" name="name" id="name_id">
247 |     email <input type="text" name="email" id="email_id">
248 |     love jQuery
249 |     <input type="radio" name="lovejquery" id="lovejquery_yes"
250 |      value="yes"> yes
251 |     <input type="radio" name="lovejquery" id="lovejquery_no"
252 |      value="no"> no
253 |     <input type="button" class="btn" value="autofill by id">
254 | </form>
255 | 
256 | <script>
257 | var data = {
258 |     "name_id": "John Doe",
259 |     "email_id": "johndoe@mail.com",
260 |     // always use "name" to find radio or checkbox multiple
261 |     "lovejquery": "yes"
262 | }
263 | $("#f_findbyname .btn").bind("click", function() {
264 |     $("#f_findbyname").autofill(data, {
265 |         findbyname: false
266 |     });
267 | });
268 | </script>
269 | 
270 |
271 | 272 |
273 |

274 |
275 |
276 | 277 |
278 |
279 |
280 | 281 |
282 |
283 |
284 | 285 |
286 | yes 287 |   288 | no 289 |
290 |
291 | 292 | 293 |
294 |

295 |
296 |
297 |
298 | 299 |
300 |

 

301 |

restrict false , findbyname false

302 | 303 |
304 |
305 |
306 | <form id="f_restrict">
307 |     name <input type="text" name="name" id="name_not_restricted">
308 |     email <input type="text" name="email" id="email_not_restricted">
309 |     <input type="button" class="btn" value="autofill by id">
310 | </form>
311 | 
312 | <script>
313 | var data = {
314 |     "name_not_restricted": "John Doe",
315 |     "email_not_restricted": "johndoe@mail.com"
316 | }
317 | $("#f_restrict .btn").bind("click", function() {
318 |     $("body").autofill(data, {
319 |         findbyname: false,
320 |         restrict: false
321 |     });
322 | });
323 | </script>
324 | 
325 |
326 | 327 |
328 |

329 |
330 |
331 | 332 |
333 |
334 |
335 | 336 |
337 |
338 | 339 | 340 |
341 |

342 |
343 |
344 |
345 | 346 |
347 |

 

348 |

restrict false , findbyname true

349 | 350 |
351 |
352 |
353 | <form>
354 |     <legend>First form</legend>
355 |     name <input type="text" name="name_multiple">
356 |     email <input type="text" name="email_multiple">
357 | </form>
358 | <form>
359 |     <legend>Second form</legend>
360 |     name <input type="text" name="name_multiple">
361 |     email <input type="text" name="email_multiple">
362 |     <div id="f_restrict_only">
363 |         <input type="button" class="clickaction" value="autofill">
364 |     </div>
365 | </form>
366 | 
367 | <script>
368 | var data = {
369 |     "name_multiple": "John Doe",
370 |     "email_multiple": "johndoe@mail.com"
371 | }
372 | $("#f_restrict_only .clickaction").bind("click", function() {
373 |     $("body").autofill(data, {
374 |         restrict: false
375 |     });
376 | });
377 | </script>
378 | 
379 |
380 | 381 |
382 |

383 |
384 | First form 385 |
386 | 387 |
388 |
389 |
390 | 391 |
392 |
393 | 394 |
395 |
396 | Second form 397 |
398 | 399 |
400 |
401 |
402 | 403 |
404 |
405 |
406 | 407 | 408 |
409 |
410 |

411 |
412 |
413 |
414 | 415 |
416 |

 

417 |

With all html elements - my "unit test"

418 | 419 |
420 |
421 |

422 | 						
423 | 			   			
424 |
425 | 426 |
427 | 428 |
429 |
430 |
431 | 432 |
433 | 434 |
435 |
436 | 437 |
438 | 439 |
440 | 441 |
442 |
443 | 444 |
445 | 446 |
447 | 454 |
455 |
456 | 457 |
458 | 459 |
460 | 467 |
468 |
469 | 470 |
471 | 472 |
473 | 474 |
475 |
476 | 477 |
478 | 479 |
480 |
    481 |
  • 482 | 486 |
  • 487 |
488 |
489 |
490 | 491 |
492 | 493 |
494 |
    495 |
  • 496 | 500 |
  • 501 |
502 |
503 |
504 | 505 |
506 | 507 |
508 |
    509 |
  • 510 | 514 |
  • 515 |
  • 516 | 520 |
  • 521 |
  • 522 | 526 |
  • 527 |
  • 528 | 532 |
  • 533 |
534 |
535 |
536 | 537 |
538 | 539 |
540 |
    541 |
  • 542 | 546 |
  • 547 |
  • 548 | 552 |
  • 553 |
  • 554 | 558 |
  • 559 |
560 |
561 |
562 | 563 | 564 | 565 | 566 | 567 |
568 | 569 |
570 | 571 |
572 |
573 |
574 | 575 |
576 | 577 | 580 | 581 |
582 | 583 | 584 | -------------------------------------------------------------------------------- /doc/js/beautify.js: -------------------------------------------------------------------------------- 1 | /*jslint onevar: false, plusplus: false */ 2 | /* 3 | 4 | JS Beautifier 5 | --------------- 6 | 7 | 8 | Written by Einar Lielmanis, 9 | http://jsbeautifier.org/ 10 | 11 | Originally converted to javascript by Vital, 12 | "End braces on own line" added by Chris J. Shull, 13 | 14 | You are free to use this in any way you want, in case you find this useful or working for you. 15 | 16 | Usage: 17 | js_beautify(js_source_text); 18 | js_beautify(js_source_text, options); 19 | 20 | The options are: 21 | indent_size (default 4) — indentation size, 22 | indent_char (default space) — character to indent with, 23 | preserve_newlines (default true) — whether existing line breaks should be preserved, 24 | preserve_max_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk, 25 | 26 | jslint_happy (default false) — if true, then jslint-stricter mode is enforced. 27 | 28 | jslint_happy !jslint_happy 29 | --------------------------------- 30 | function () function() 31 | 32 | brace_style (default "collapse") - "collapse" | "expand" | "end-expand" 33 | 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. 34 | 35 | e.g 36 | 37 | js_beautify(js_source_text, { 38 | 'indent_size': 1, 39 | 'indent_char': '\t' 40 | }); 41 | 42 | 43 | */ 44 | 45 | 46 | 47 | function js_beautify(js_source_text, options) { 48 | 49 | var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string; 50 | var whitespace, wordchar, punct, parser_pos, line_starters, digits; 51 | var prefix, token_type, do_block_just_closed; 52 | var wanted_newline, just_added_newline, n_newlines; 53 | var preindent_string = ''; 54 | 55 | 56 | // Some interpreters have unexpected results with foo = baz || bar; 57 | options = options ? options : {}; 58 | 59 | var opt_brace_style; 60 | 61 | // compatibility 62 | if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) { 63 | options.jslint_happy = options.space_after_anon_function; 64 | } 65 | if (options.braces_on_own_line !== undefined) { //graceful handling of depricated option 66 | opt_brace_style = options.braces_on_own_line ? "expand" : "collapse"; 67 | } 68 | opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse"); 69 | 70 | 71 | var opt_indent_size = options.indent_size ? options.indent_size : 4; 72 | var opt_indent_char = options.indent_char ? options.indent_char : ' '; 73 | var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines; 74 | var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines; 75 | var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy; 76 | var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation; 77 | 78 | just_added_newline = false; 79 | 80 | // cache the source's length. 81 | var input_length = js_source_text.length; 82 | 83 | function trim_output(eat_newlines) { 84 | eat_newlines = typeof eat_newlines === 'undefined' ? false : eat_newlines; 85 | while (output.length && (output[output.length - 1] === ' ' 86 | || output[output.length - 1] === indent_string 87 | || output[output.length - 1] === preindent_string 88 | || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) { 89 | output.pop(); 90 | } 91 | } 92 | 93 | function trim(s) { 94 | return s.replace(/^\s\s*|\s\s*$/, ''); 95 | } 96 | 97 | function force_newline() 98 | { 99 | var old_keep_array_indentation = opt_keep_array_indentation; 100 | opt_keep_array_indentation = false; 101 | print_newline() 102 | opt_keep_array_indentation = old_keep_array_indentation; 103 | } 104 | 105 | function print_newline(ignore_repeated) { 106 | 107 | flags.eat_next_space = false; 108 | if (opt_keep_array_indentation && is_array(flags.mode)) { 109 | return; 110 | } 111 | 112 | ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated; 113 | 114 | flags.if_line = false; 115 | trim_output(); 116 | 117 | if (!output.length) { 118 | return; // no newline on start of file 119 | } 120 | 121 | if (output[output.length - 1] !== "\n" || !ignore_repeated) { 122 | just_added_newline = true; 123 | output.push("\n"); 124 | } 125 | if (preindent_string) { 126 | output.push(preindent_string); 127 | } 128 | for (var i = 0; i < flags.indentation_level; i += 1) { 129 | output.push(indent_string); 130 | } 131 | if (flags.var_line && flags.var_line_reindented) { 132 | output.push(indent_string); // skip space-stuffing, if indenting with a tab 133 | } 134 | } 135 | 136 | 137 | 138 | function print_single_space() { 139 | if (flags.eat_next_space) { 140 | flags.eat_next_space = false; 141 | return; 142 | } 143 | var last_output = ' '; 144 | if (output.length) { 145 | last_output = output[output.length - 1]; 146 | } 147 | if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space 148 | output.push(' '); 149 | } 150 | } 151 | 152 | 153 | function print_token() { 154 | just_added_newline = false; 155 | flags.eat_next_space = false; 156 | output.push(token_text); 157 | } 158 | 159 | function indent() { 160 | flags.indentation_level += 1; 161 | } 162 | 163 | 164 | function remove_indent() { 165 | if (output.length && output[output.length - 1] === indent_string) { 166 | output.pop(); 167 | } 168 | } 169 | 170 | function set_mode(mode) { 171 | if (flags) { 172 | flag_store.push(flags); 173 | } 174 | flags = { 175 | previous_mode: flags ? flags.mode : 'BLOCK', 176 | mode: mode, 177 | var_line: false, 178 | var_line_tainted: false, 179 | var_line_reindented: false, 180 | in_html_comment: false, 181 | if_line: false, 182 | in_case: false, 183 | eat_next_space: false, 184 | indentation_baseline: -1, 185 | indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0), 186 | ternary_depth: 0 187 | }; 188 | } 189 | 190 | function is_array(mode) { 191 | return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]'; 192 | } 193 | 194 | function is_expression(mode) { 195 | return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)'; 196 | } 197 | 198 | function restore_mode() { 199 | do_block_just_closed = flags.mode === 'DO_BLOCK'; 200 | if (flag_store.length > 0) { 201 | flags = flag_store.pop(); 202 | } 203 | } 204 | 205 | function all_lines_start_with(lines, c) { 206 | for (var i = 0; i < lines.length; i++) { 207 | if (trim(lines[i])[0] != c) { 208 | return false; 209 | } 210 | } 211 | return true; 212 | } 213 | 214 | function in_array(what, arr) { 215 | for (var i = 0; i < arr.length; i += 1) { 216 | if (arr[i] === what) { 217 | return true; 218 | } 219 | } 220 | return false; 221 | } 222 | 223 | function get_next_token() { 224 | n_newlines = 0; 225 | 226 | if (parser_pos >= input_length) { 227 | return ['', 'TK_EOF']; 228 | } 229 | 230 | wanted_newline = false; 231 | 232 | var c = input.charAt(parser_pos); 233 | parser_pos += 1; 234 | 235 | 236 | var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode); 237 | 238 | if (keep_whitespace) { 239 | 240 | // 241 | // slight mess to allow nice preservation of array indentation and reindent that correctly 242 | // first time when we get to the arrays: 243 | // var a = [ 244 | // ....'something' 245 | // we make note of whitespace_count = 4 into flags.indentation_baseline 246 | // so we know that 4 whitespaces in original source match indent_level of reindented source 247 | // 248 | // and afterwards, when we get to 249 | // 'something, 250 | // .......'something else' 251 | // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces 252 | // 253 | var whitespace_count = 0; 254 | 255 | while (in_array(c, whitespace)) { 256 | 257 | if (c === "\n") { 258 | trim_output(); 259 | output.push("\n"); 260 | just_added_newline = true; 261 | whitespace_count = 0; 262 | } else { 263 | if (c === '\t') { 264 | whitespace_count += 4; 265 | } else if (c === '\r') { 266 | // nothing 267 | } else { 268 | whitespace_count += 1; 269 | } 270 | } 271 | 272 | if (parser_pos >= input_length) { 273 | return ['', 'TK_EOF']; 274 | } 275 | 276 | c = input.charAt(parser_pos); 277 | parser_pos += 1; 278 | 279 | } 280 | if (flags.indentation_baseline === -1) { 281 | flags.indentation_baseline = whitespace_count; 282 | } 283 | 284 | if (just_added_newline) { 285 | var i; 286 | for (i = 0; i < flags.indentation_level + 1; i += 1) { 287 | output.push(indent_string); 288 | } 289 | if (flags.indentation_baseline !== -1) { 290 | for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) { 291 | output.push(' '); 292 | } 293 | } 294 | } 295 | 296 | } else { 297 | while (in_array(c, whitespace)) { 298 | 299 | if (c === "\n") { 300 | n_newlines += ( (opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1: 0: 1 ); 301 | } 302 | 303 | 304 | if (parser_pos >= input_length) { 305 | return ['', 'TK_EOF']; 306 | } 307 | 308 | c = input.charAt(parser_pos); 309 | parser_pos += 1; 310 | 311 | } 312 | 313 | if (opt_preserve_newlines) { 314 | if (n_newlines > 1) { 315 | for (i = 0; i < n_newlines; i += 1) { 316 | print_newline(i === 0); 317 | just_added_newline = true; 318 | } 319 | } 320 | } 321 | wanted_newline = n_newlines > 0; 322 | } 323 | 324 | 325 | if (in_array(c, wordchar)) { 326 | if (parser_pos < input_length) { 327 | while (in_array(input.charAt(parser_pos), wordchar)) { 328 | c += input.charAt(parser_pos); 329 | parser_pos += 1; 330 | if (parser_pos === input_length) { 331 | break; 332 | } 333 | } 334 | } 335 | 336 | // small and surprisingly unugly hack for 1E-10 representation 337 | if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) { 338 | 339 | var sign = input.charAt(parser_pos); 340 | parser_pos += 1; 341 | 342 | var t = get_next_token(parser_pos); 343 | c += sign + t[0]; 344 | return [c, 'TK_WORD']; 345 | } 346 | 347 | if (c === 'in') { // hack for 'in' operator 348 | return [c, 'TK_OPERATOR']; 349 | } 350 | if (wanted_newline && last_type !== 'TK_OPERATOR' 351 | && last_type !== 'TK_EQUALS' 352 | && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) { 353 | print_newline(); 354 | } 355 | return [c, 'TK_WORD']; 356 | } 357 | 358 | if (c === '(' || c === '[') { 359 | return [c, 'TK_START_EXPR']; 360 | } 361 | 362 | if (c === ')' || c === ']') { 363 | return [c, 'TK_END_EXPR']; 364 | } 365 | 366 | if (c === '{') { 367 | return [c, 'TK_START_BLOCK']; 368 | } 369 | 370 | if (c === '}') { 371 | return [c, 'TK_END_BLOCK']; 372 | } 373 | 374 | if (c === ';') { 375 | return [c, 'TK_SEMICOLON']; 376 | } 377 | 378 | if (c === '/') { 379 | var comment = ''; 380 | // peek for comment /* ... */ 381 | var inline_comment = true; 382 | if (input.charAt(parser_pos) === '*') { 383 | parser_pos += 1; 384 | if (parser_pos < input_length) { 385 | while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input_length) { 386 | c = input.charAt(parser_pos); 387 | comment += c; 388 | if (c === '\x0d' || c === '\x0a') { 389 | inline_comment = false; 390 | } 391 | parser_pos += 1; 392 | if (parser_pos >= input_length) { 393 | break; 394 | } 395 | } 396 | } 397 | parser_pos += 2; 398 | if (inline_comment) { 399 | return ['/*' + comment + '*/', 'TK_INLINE_COMMENT']; 400 | } else { 401 | return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT']; 402 | } 403 | } 404 | // peek for comment // ... 405 | if (input.charAt(parser_pos) === '/') { 406 | comment = c; 407 | while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') { 408 | comment += input.charAt(parser_pos); 409 | parser_pos += 1; 410 | if (parser_pos >= input_length) { 411 | break; 412 | } 413 | } 414 | parser_pos += 1; 415 | if (wanted_newline) { 416 | print_newline(); 417 | } 418 | return [comment, 'TK_COMMENT']; 419 | } 420 | 421 | } 422 | 423 | if (c === "'" || // string 424 | c === '"' || // string 425 | (c === '/' && 426 | ((last_type === 'TK_WORD' && in_array(last_text, ['return', 'do'])) || 427 | (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 428 | var sep = c; 429 | var esc = false; 430 | var resulting_string = c; 431 | 432 | if (parser_pos < input_length) { 433 | if (sep === '/') { 434 | // 435 | // handle regexp separately... 436 | // 437 | var in_char_class = false; 438 | while (esc || in_char_class || input.charAt(parser_pos) !== sep) { 439 | resulting_string += input.charAt(parser_pos); 440 | if (!esc) { 441 | esc = input.charAt(parser_pos) === '\\'; 442 | if (input.charAt(parser_pos) === '[') { 443 | in_char_class = true; 444 | } else if (input.charAt(parser_pos) === ']') { 445 | in_char_class = false; 446 | } 447 | } else { 448 | esc = false; 449 | } 450 | parser_pos += 1; 451 | if (parser_pos >= input_length) { 452 | // incomplete string/rexp when end-of-file reached. 453 | // bail out with what had been received so far. 454 | return [resulting_string, 'TK_STRING']; 455 | } 456 | } 457 | 458 | } else { 459 | // 460 | // and handle string also separately 461 | // 462 | while (esc || input.charAt(parser_pos) !== sep) { 463 | resulting_string += input.charAt(parser_pos); 464 | if (!esc) { 465 | esc = input.charAt(parser_pos) === '\\'; 466 | } else { 467 | esc = false; 468 | } 469 | parser_pos += 1; 470 | if (parser_pos >= input_length) { 471 | // incomplete string/rexp when end-of-file reached. 472 | // bail out with what had been received so far. 473 | return [resulting_string, 'TK_STRING']; 474 | } 475 | } 476 | } 477 | 478 | 479 | 480 | } 481 | 482 | parser_pos += 1; 483 | 484 | resulting_string += sep; 485 | 486 | if (sep === '/') { 487 | // regexps may have modifiers /regexp/MOD , so fetch those, too 488 | while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) { 489 | resulting_string += input.charAt(parser_pos); 490 | parser_pos += 1; 491 | } 492 | } 493 | return [resulting_string, 'TK_STRING']; 494 | } 495 | 496 | if (c === '#') { 497 | 498 | 499 | if (output.length === 0 && input.charAt(parser_pos) === '!') { 500 | // shebang 501 | resulting_string = c; 502 | while (parser_pos < input_length && c != '\n') { 503 | c = input.charAt(parser_pos); 504 | resulting_string += c; 505 | parser_pos += 1; 506 | } 507 | output.push(trim(resulting_string) + '\n'); 508 | print_newline(); 509 | return get_next_token(); 510 | } 511 | 512 | 513 | 514 | // Spidermonkey-specific sharp variables for circular references 515 | // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript 516 | // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935 517 | var sharp = '#'; 518 | if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) { 519 | do { 520 | c = input.charAt(parser_pos); 521 | sharp += c; 522 | parser_pos += 1; 523 | } while (parser_pos < input_length && c !== '#' && c !== '='); 524 | if (c === '#') { 525 | // 526 | } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') { 527 | sharp += '[]'; 528 | parser_pos += 2; 529 | } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') { 530 | sharp += '{}'; 531 | parser_pos += 2; 532 | } 533 | return [sharp, 'TK_WORD']; 534 | } 535 | } 536 | 537 | if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '') { 544 | flags.in_html_comment = false; 545 | parser_pos += 2; 546 | if (wanted_newline) { 547 | print_newline(); 548 | } 549 | return ['-->', 'TK_COMMENT']; 550 | } 551 | 552 | if (in_array(c, punct)) { 553 | while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) { 554 | c += input.charAt(parser_pos); 555 | parser_pos += 1; 556 | if (parser_pos >= input_length) { 557 | break; 558 | } 559 | } 560 | 561 | if (c === '=') { 562 | return [c, 'TK_EQUALS']; 563 | } else { 564 | return [c, 'TK_OPERATOR']; 565 | } 566 | } 567 | 568 | return [c, 'TK_UNKNOWN']; 569 | } 570 | 571 | //---------------------------------- 572 | indent_string = ''; 573 | while (opt_indent_size > 0) { 574 | indent_string += opt_indent_char; 575 | opt_indent_size -= 1; 576 | } 577 | 578 | while (js_source_text && (js_source_text[0] === ' ' || js_source_text[0] === '\t')) { 579 | preindent_string += js_source_text[0]; 580 | js_source_text = js_source_text.substring(1); 581 | } 582 | input = js_source_text; 583 | 584 | last_word = ''; // last 'TK_WORD' passed 585 | last_type = 'TK_START_EXPR'; // last token type 586 | last_text = ''; // last token text 587 | last_last_text = ''; // pre-last token text 588 | output = []; 589 | 590 | do_block_just_closed = false; 591 | 592 | whitespace = "\n\r\t ".split(''); 593 | wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split(''); 594 | digits = '0123456789'.split(''); 595 | 596 | punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' '); 597 | 598 | // words which should always start on new line. 599 | line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(','); 600 | 601 | // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'. 602 | // some formatting depends on that. 603 | flag_store = []; 604 | set_mode('BLOCK'); 605 | 606 | parser_pos = 0; 607 | while (true) { 608 | var t = get_next_token(parser_pos); 609 | token_text = t[0]; 610 | token_type = t[1]; 611 | if (token_type === 'TK_EOF') { 612 | break; 613 | } 614 | 615 | switch (token_type) { 616 | 617 | case 'TK_START_EXPR': 618 | 619 | if (token_text === '[') { 620 | 621 | if (last_type === 'TK_WORD' || last_text === ')') { 622 | // this is array index specifier, break immediately 623 | // a[x], fn()[x] 624 | if (in_array(last_text, line_starters)) { 625 | print_single_space(); 626 | } 627 | set_mode('(EXPRESSION)'); 628 | print_token(); 629 | break; 630 | } 631 | 632 | if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') { 633 | if (last_last_text === ']' && last_text === ',') { 634 | // ], [ goes to new line 635 | if (flags.mode === '[EXPRESSION]') { 636 | flags.mode = '[INDENTED-EXPRESSION]'; 637 | if (!opt_keep_array_indentation) { 638 | indent(); 639 | } 640 | } 641 | set_mode('[EXPRESSION]'); 642 | if (!opt_keep_array_indentation) { 643 | print_newline(); 644 | } 645 | } else if (last_text === '[') { 646 | if (flags.mode === '[EXPRESSION]') { 647 | flags.mode = '[INDENTED-EXPRESSION]'; 648 | if (!opt_keep_array_indentation) { 649 | indent(); 650 | } 651 | } 652 | set_mode('[EXPRESSION]'); 653 | 654 | if (!opt_keep_array_indentation) { 655 | print_newline(); 656 | } 657 | } else { 658 | set_mode('[EXPRESSION]'); 659 | } 660 | } else { 661 | set_mode('[EXPRESSION]'); 662 | } 663 | 664 | 665 | 666 | } else { 667 | set_mode('(EXPRESSION)'); 668 | } 669 | 670 | if (last_text === ';' || last_type === 'TK_START_BLOCK') { 671 | print_newline(); 672 | } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') { 673 | // do nothing on (( and )( and ][ and ]( and .( 674 | } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { 675 | print_single_space(); 676 | } else if (last_word === 'function' || last_word === 'typeof') { 677 | // function() vs function () 678 | if (opt_jslint_happy) { 679 | print_single_space(); 680 | } 681 | } else if (in_array(last_text, line_starters) || last_text === 'catch') { 682 | print_single_space(); 683 | } 684 | print_token(); 685 | 686 | break; 687 | 688 | case 'TK_END_EXPR': 689 | if (token_text === ']') { 690 | if (opt_keep_array_indentation) { 691 | if (last_text === '}') { 692 | // trim_output(); 693 | // print_newline(true); 694 | remove_indent(); 695 | print_token(); 696 | restore_mode(); 697 | break; 698 | } 699 | } else { 700 | if (flags.mode === '[INDENTED-EXPRESSION]') { 701 | if (last_text === ']') { 702 | restore_mode(); 703 | print_newline(); 704 | print_token(); 705 | break; 706 | } 707 | } 708 | } 709 | } 710 | restore_mode(); 711 | print_token(); 712 | break; 713 | 714 | case 'TK_START_BLOCK': 715 | 716 | if (last_word === 'do') { 717 | set_mode('DO_BLOCK'); 718 | } else { 719 | set_mode('BLOCK'); 720 | } 721 | if (opt_brace_style=="expand") { 722 | if (last_type !== 'TK_OPERATOR') { 723 | if (last_text === 'return' || last_text === '=') { 724 | print_single_space(); 725 | } else { 726 | print_newline(true); 727 | } 728 | } 729 | print_token(); 730 | indent(); 731 | } else { 732 | if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') { 733 | if (last_type === 'TK_START_BLOCK') { 734 | print_newline(); 735 | } else { 736 | print_single_space(); 737 | } 738 | } else { 739 | // if TK_OPERATOR or TK_START_EXPR 740 | if (is_array(flags.previous_mode) && last_text === ',') { 741 | if (last_last_text === '}') { 742 | // }, { in array context 743 | print_single_space(); 744 | } else { 745 | print_newline(); // [a, b, c, { 746 | } 747 | } 748 | } 749 | indent(); 750 | print_token(); 751 | } 752 | 753 | break; 754 | 755 | case 'TK_END_BLOCK': 756 | restore_mode(); 757 | if (opt_brace_style=="expand") { 758 | if (last_text !== '{') { 759 | print_newline(); 760 | } 761 | print_token(); 762 | } else { 763 | if (last_type === 'TK_START_BLOCK') { 764 | // nothing 765 | if (just_added_newline) { 766 | remove_indent(); 767 | } else { 768 | // {} 769 | trim_output(); 770 | } 771 | } else { 772 | if (is_array(flags.mode) && opt_keep_array_indentation) { 773 | // we REALLY need a newline here, but newliner would skip that 774 | opt_keep_array_indentation = false; 775 | print_newline(); 776 | opt_keep_array_indentation = true; 777 | 778 | } else { 779 | print_newline(); 780 | } 781 | } 782 | print_token(); 783 | } 784 | break; 785 | 786 | case 'TK_WORD': 787 | 788 | // no, it's not you. even I have problems understanding how this works 789 | // and what does what. 790 | if (do_block_just_closed) { 791 | // do {} ## while () 792 | print_single_space(); 793 | print_token(); 794 | print_single_space(); 795 | do_block_just_closed = false; 796 | break; 797 | } 798 | 799 | if (token_text === 'function') { 800 | if (flags.var_line) { 801 | flags.var_line_reindented = true; 802 | } 803 | if ((just_added_newline || last_text === ';') && last_text !== '{') { 804 | // make sure there is a nice clean space of at least one blank line 805 | // before a new function definition 806 | n_newlines = just_added_newline ? n_newlines : 0; 807 | if ( ! opt_preserve_newlines) { 808 | n_newlines = 1; 809 | } 810 | 811 | for (var i = 0; i < 2 - n_newlines; i++) { 812 | print_newline(false); 813 | } 814 | } 815 | } 816 | 817 | if (token_text === 'case' || token_text === 'default') { 818 | if (last_text === ':') { 819 | // switch cases following one another 820 | remove_indent(); 821 | } else { 822 | // case statement starts in the same line where switch 823 | flags.indentation_level--; 824 | print_newline(); 825 | flags.indentation_level++; 826 | } 827 | print_token(); 828 | flags.in_case = true; 829 | break; 830 | } 831 | 832 | prefix = 'NONE'; 833 | 834 | if (last_type === 'TK_END_BLOCK') { 835 | 836 | if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { 837 | prefix = 'NEWLINE'; 838 | } else { 839 | if (opt_brace_style=="expand" || opt_brace_style=="end-expand") { 840 | prefix = 'NEWLINE'; 841 | } else { 842 | prefix = 'SPACE'; 843 | print_single_space(); 844 | } 845 | } 846 | } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) { 847 | prefix = 'NEWLINE'; 848 | } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) { 849 | prefix = 'SPACE'; 850 | } else if (last_type === 'TK_STRING') { 851 | prefix = 'NEWLINE'; 852 | } else if (last_type === 'TK_WORD') { 853 | if (last_text === 'else') { 854 | // eat newlines between ...else *** some_op... 855 | // won't preserve extra newlines in this place (if any), but don't care that much 856 | trim_output(true); 857 | } 858 | prefix = 'SPACE'; 859 | } else if (last_type === 'TK_START_BLOCK') { 860 | prefix = 'NEWLINE'; 861 | } else if (last_type === 'TK_END_EXPR') { 862 | print_single_space(); 863 | prefix = 'NEWLINE'; 864 | } 865 | 866 | if (in_array(token_text, line_starters) && last_text !== ')') { 867 | if (last_text == 'else') { 868 | prefix = 'SPACE'; 869 | } else { 870 | prefix = 'NEWLINE'; 871 | } 872 | } 873 | 874 | if (flags.if_line && last_type === 'TK_END_EXPR') { 875 | flags.if_line = false; 876 | } 877 | if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { 878 | if (last_type !== 'TK_END_BLOCK' || opt_brace_style=="expand" || opt_brace_style=="end-expand") { 879 | print_newline(); 880 | } else { 881 | trim_output(true); 882 | print_single_space(); 883 | } 884 | } else if (prefix === 'NEWLINE') { 885 | if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') { 886 | // no need to force newline on 'function': (function 887 | // DONOTHING 888 | } else if (token_text === 'function' && last_text == 'new') { 889 | print_single_space(); 890 | } else if (last_text === 'return' || last_text === 'throw') { 891 | // no newline between 'return nnn' 892 | print_single_space(); 893 | } else if (last_type !== 'TK_END_EXPR') { 894 | if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') { 895 | // no need to force newline on 'var': for (var x = 0...) 896 | if (token_text === 'if' && last_word === 'else' && last_text !== '{') { 897 | // no newline for } else if { 898 | print_single_space(); 899 | } else { 900 | flags.var_line = false; 901 | flags.var_line_reindented = false; 902 | print_newline(); 903 | } 904 | } 905 | } else if (in_array(token_text, line_starters) && last_text != ')') { 906 | flags.var_line = false; 907 | flags.var_line_reindented = false; 908 | print_newline(); 909 | } 910 | } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') { 911 | print_newline(); // }, in lists get a newline treatment 912 | } else if (prefix === 'SPACE') { 913 | print_single_space(); 914 | } 915 | print_token(); 916 | last_word = token_text; 917 | 918 | if (token_text === 'var') { 919 | flags.var_line = true; 920 | flags.var_line_reindented = false; 921 | flags.var_line_tainted = false; 922 | } 923 | 924 | if (token_text === 'if') { 925 | flags.if_line = true; 926 | } 927 | if (token_text === 'else') { 928 | flags.if_line = false; 929 | } 930 | 931 | break; 932 | 933 | case 'TK_SEMICOLON': 934 | 935 | print_token(); 936 | flags.var_line = false; 937 | flags.var_line_reindented = false; 938 | if (flags.mode == 'OBJECT') { 939 | // OBJECT mode is weird and doesn't get reset too well. 940 | flags.mode = 'BLOCK'; 941 | } 942 | break; 943 | 944 | case 'TK_STRING': 945 | 946 | if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') { 947 | print_newline(); 948 | } else if (last_type === 'TK_WORD') { 949 | print_single_space(); 950 | } 951 | print_token(); 952 | break; 953 | 954 | case 'TK_EQUALS': 955 | if (flags.var_line) { 956 | // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done 957 | flags.var_line_tainted = true; 958 | } 959 | print_single_space(); 960 | print_token(); 961 | print_single_space(); 962 | break; 963 | 964 | case 'TK_OPERATOR': 965 | 966 | var space_before = true; 967 | var space_after = true; 968 | 969 | if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) { 970 | // do not break on comma, for(var a = 1, b = 2) 971 | flags.var_line_tainted = false; 972 | } 973 | 974 | if (flags.var_line) { 975 | if (token_text === ',') { 976 | if (flags.var_line_tainted) { 977 | print_token(); 978 | flags.var_line_reindented = true; 979 | flags.var_line_tainted = false; 980 | print_newline(); 981 | break; 982 | } else { 983 | flags.var_line_tainted = false; 984 | } 985 | // } else if (token_text === ':') { 986 | // hmm, when does this happen? tests don't catch this 987 | // flags.var_line = false; 988 | } 989 | } 990 | 991 | if (last_text === 'return' || last_text === 'throw') { 992 | // "return" had a special handling in TK_WORD. Now we need to return the favor 993 | print_single_space(); 994 | print_token(); 995 | break; 996 | } 997 | 998 | if (token_text === ':' && flags.in_case) { 999 | print_token(); // colon really asks for separate treatment 1000 | print_newline(); 1001 | flags.in_case = false; 1002 | break; 1003 | } 1004 | 1005 | if (token_text === '::') { 1006 | // no spaces around exotic namespacing syntax operator 1007 | print_token(); 1008 | break; 1009 | } 1010 | 1011 | if (token_text === ',') { 1012 | if (flags.var_line) { 1013 | if (flags.var_line_tainted) { 1014 | print_token(); 1015 | print_newline(); 1016 | flags.var_line_tainted = false; 1017 | } else { 1018 | print_token(); 1019 | print_single_space(); 1020 | } 1021 | } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") { 1022 | print_token(); 1023 | if (flags.mode === 'OBJECT' && last_text === '}') { 1024 | print_newline(); 1025 | } else { 1026 | print_single_space(); 1027 | } 1028 | } else { 1029 | if (flags.mode === 'OBJECT') { 1030 | print_token(); 1031 | print_newline(); 1032 | } else { 1033 | // EXPR or DO_BLOCK 1034 | print_token(); 1035 | print_single_space(); 1036 | } 1037 | } 1038 | break; 1039 | // } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS']) || in_array(last_text, line_starters) || in_array(last_text, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) { 1040 | } else 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)))) { 1041 | // unary operators (and binary +/- pretending to be unary) special cases 1042 | 1043 | space_before = false; 1044 | space_after = false; 1045 | 1046 | if (last_text === ';' && is_expression(flags.mode)) { 1047 | // for (;; ++i) 1048 | // ^^^ 1049 | space_before = true; 1050 | } 1051 | if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) { 1052 | space_before = true; 1053 | } 1054 | 1055 | if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) { 1056 | // { foo; --i } 1057 | // foo(); --bar; 1058 | print_newline(); 1059 | } 1060 | } else if (token_text === '.') { 1061 | // decimal digits or object.property 1062 | space_before = false; 1063 | 1064 | } else if (token_text === ':') { 1065 | if (flags.ternary_depth == 0) { 1066 | flags.mode = 'OBJECT'; 1067 | space_before = false; 1068 | } else { 1069 | flags.ternary_depth -= 1; 1070 | } 1071 | } else if (token_text === '?') { 1072 | flags.ternary_depth += 1; 1073 | } 1074 | if (space_before) { 1075 | print_single_space(); 1076 | } 1077 | 1078 | print_token(); 1079 | 1080 | if (space_after) { 1081 | print_single_space(); 1082 | } 1083 | 1084 | if (token_text === '!') { 1085 | // flags.eat_next_space = true; 1086 | } 1087 | 1088 | break; 1089 | 1090 | case 'TK_BLOCK_COMMENT': 1091 | 1092 | var lines = token_text.split(/\x0a|\x0d\x0a/); 1093 | 1094 | if (all_lines_start_with(lines.slice(1), '*')) { 1095 | // javadoc: reformat and reindent 1096 | print_newline(); 1097 | output.push(lines[0]); 1098 | for (i = 1; i < lines.length; i++) { 1099 | print_newline(); 1100 | output.push(' '); 1101 | output.push(trim(lines[i])); 1102 | } 1103 | 1104 | } else { 1105 | 1106 | // simple block comment: leave intact 1107 | if (lines.length > 1) { 1108 | // multiline comment block starts with a new line 1109 | print_newline(); 1110 | trim_output(); 1111 | } else { 1112 | // single-line /* comment */ stays where it is 1113 | print_single_space(); 1114 | 1115 | } 1116 | 1117 | for (i = 0; i < lines.length; i++) { 1118 | output.push(lines[i]); 1119 | output.push('\n'); 1120 | } 1121 | 1122 | } 1123 | print_newline(); 1124 | break; 1125 | 1126 | case 'TK_INLINE_COMMENT': 1127 | 1128 | print_single_space(); 1129 | print_token(); 1130 | if (is_expression(flags.mode)) { 1131 | print_single_space(); 1132 | } else { 1133 | force_newline(); 1134 | } 1135 | break; 1136 | 1137 | case 'TK_COMMENT': 1138 | 1139 | // print_newline(); 1140 | if (wanted_newline) { 1141 | print_newline(); 1142 | } else { 1143 | print_single_space(); 1144 | } 1145 | print_token(); 1146 | force_newline(); 1147 | break; 1148 | 1149 | case 'TK_UNKNOWN': 1150 | if (last_text === 'return' || last_text === 'throw') { 1151 | print_single_space(); 1152 | } 1153 | print_token(); 1154 | break; 1155 | } 1156 | 1157 | last_last_text = last_text; 1158 | last_type = token_type; 1159 | last_text = token_text; 1160 | } 1161 | 1162 | var sweet_code = preindent_string + output.join('').replace(/[\n ]+$/, ''); 1163 | return sweet_code; 1164 | 1165 | } 1166 | 1167 | // Add support for CommonJS. Just put this file somewhere on your require.paths 1168 | // and you will be able to `var js_beautify = require("beautify").js_beautify`. 1169 | if (typeof exports !== "undefined") 1170 | exports.js_beautify = js_beautify; 1171 | -------------------------------------------------------------------------------- /form-autofill.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "form-autofill", 3 | "version": "0.1.1", 4 | "title": "jQuery form auto fill", 5 | "author": { 6 | "name": "Creative Area", 7 | "url": "http://www.creative-area.net/" 8 | }, 9 | "licenses": [ 10 | { 11 | "type": "GPL-2.0", 12 | "url": "http://opensource.org/licenses/GPL-2.0" 13 | }, 14 | { 15 | "type": "MIT", 16 | "url": "http://opensource.org/licenses/MIT" 17 | } 18 | ], 19 | "dependencies": { 20 | "jquery": ">=1.5" 21 | }, 22 | "description": "Simply autofill an empty form with data.", 23 | "keywords": ["jquery","form","data","field","input","fill"], 24 | "homepage": "http://labs.creative-area.net/jquery.formautofill/", 25 | "docs": "http://labs.creative-area.net/jquery.formautofill/doc/", 26 | "demo": "http://labs.creative-area.net/jquery.formautofill/demo/" 27 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jQuery form autofill 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 32 | 33 | 34 | 35 | 36 | 37 | 40 | 41 | 42 | 43 |
44 | 45 | -------------------------------------------------------------------------------- /jquery.formautofill.js: -------------------------------------------------------------------------------- 1 | /** 2 | * form autofill (jQuery plugin) 3 | * Version: 0.1 4 | * Released: 2011-11-30 5 | * 6 | * Copyright (c) 2011 Creative Area 7 | * Dual licensed under the MIT or GPL Version 2 licenses. 8 | * 9 | * Require jQuery 10 | * http://jquery.com/ 11 | * Copyright 2011, John Resig 12 | * Dual licensed under the MIT or GPL Version 2 licenses. 13 | * http://jquery.org/license 14 | */ 15 | (function($){ 16 | $.fn.extend({ 17 | autofill: function(data, options) { 18 | var settings = { 19 | findbyname: true, 20 | restrict: true 21 | }, 22 | self = this; 23 | 24 | if ( options ) { 25 | $.extend( settings, options ); 26 | } 27 | 28 | return this.each(function() { 29 | $.each( data, function(k, v) { 30 | 31 | // switch case findbyname / findbyid 32 | 33 | var selector, elt; 34 | 35 | if ( settings.findbyname ) { // by name 36 | 37 | selector = '[name="'+k+'"]'; 38 | elt = ( settings.restrict ) ? self.find( selector ) : $( selector ); 39 | 40 | if ( elt.length == 1 ) { 41 | elt.val( ( elt.attr("type") == "checkbox" ) ? [v] : v ); 42 | } else if ( elt.length > 1 ) { 43 | // radio 44 | elt.val([v]); 45 | } else { 46 | selector = '[name="'+k+'[]"]'; 47 | elt = ( settings.restrict ) ? self.find( selector ) : $( selector ); 48 | elt.each(function(){ 49 | $(this).val(v); 50 | }); 51 | } 52 | 53 | } else { // by id 54 | 55 | selector = '#'+k; 56 | elt = ( settings.restrict ) ? self.find( selector ) : $( selector ); 57 | 58 | if ( elt.length == 1 ) { 59 | elt.val( ( elt.attr("type") == "checkbox" ) ? [v] : v ); 60 | } else { 61 | var radiofound = false; 62 | 63 | // radio 64 | elt = ( settings.restrict ) ? self.find( 'input:radio[name="'+k+'"]' ) : $( 'input:radio[name="'+k+'"]' ); 65 | elt.each(function(){ 66 | radiofound = true; 67 | if ( this.value == v ) { this.checked = true; } 68 | }); 69 | // multi checkbox 70 | if ( !radiofound ) { 71 | elt = ( settings.restrict ) ? self.find( 'input:checkbox[name="'+k+'[]"]' ) : $( 'input:checkbox[name="'+k+'[]"]' ); 72 | elt.each(function(){ 73 | $(this).val(v); 74 | }); 75 | } 76 | } 77 | } 78 | }); 79 | }); 80 | } 81 | }); 82 | })(jQuery); -------------------------------------------------------------------------------- /jquery.formautofill.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * form autofill (jQuery plugin) 3 | * Version: 0.1 4 | * Released: 2011-11-30 5 | * 6 | * Copyright (c) 2011 Creative Area 7 | * Dual licensed under the MIT or GPL Version 2 licenses. 8 | */ 9 | (function(b){b.fn.extend({autofill:function(h,f){var d={findbyname:!0,restrict:!0},g=this;f&&b.extend(d,f);return this.each(function(){b.each(h,function(e,c){var a;if(d.findbyname)a='[name="'+e+'"]',a=d.restrict?g.find(a):b(a),1==a.length?a.val("checkbox"==a.attr("type")?[c]:c):1