luca 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (310) hide show
  1. data/CHANGELOG +49 -0
  2. data/Gemfile +8 -1
  3. data/Gemfile.lock +97 -53
  4. data/Guardfile +3 -25
  5. data/README.md +5 -16
  6. data/ROADMAP +15 -9
  7. data/Rakefile +24 -75
  8. data/app.rb +10 -42
  9. data/app/assets/javascripts/luca/basic.coffee +1 -1
  10. data/app/assets/javascripts/luca/components/application.coffee +187 -104
  11. data/app/assets/javascripts/luca/components/collection_view.coffee +115 -51
  12. data/app/assets/javascripts/luca/components/controller.coffee +87 -10
  13. data/app/assets/javascripts/luca/components/fields/base.coffee +74 -13
  14. data/app/assets/javascripts/luca/components/fields/button_field.coffee +60 -13
  15. data/app/assets/javascripts/luca/components/fields/checkbox_array.coffee +12 -7
  16. data/app/assets/javascripts/luca/components/fields/select_field.coffee +82 -23
  17. data/app/assets/javascripts/luca/components/fields/text_area_field.coffee +25 -10
  18. data/app/assets/javascripts/luca/components/fields/text_field.coffee +9 -3
  19. data/app/assets/javascripts/luca/components/form_view.coffee +105 -33
  20. data/app/assets/javascripts/luca/components/grid_layout_view.coffee +42 -0
  21. data/app/assets/javascripts/luca/components/index.coffee +6 -0
  22. data/app/assets/javascripts/luca/components/nav_bar.coffee +60 -6
  23. data/app/assets/javascripts/luca/components/page.coffee +70 -0
  24. data/app/assets/javascripts/luca/components/simple_collection_view.coffee +10 -0
  25. data/app/assets/javascripts/luca/components/table_view.coffee +7 -3
  26. data/app/assets/javascripts/luca/components/table_view_scrollable.coffee +23 -0
  27. data/app/assets/javascripts/luca/concerns/collection_event_bindings.coffee +4 -1
  28. data/app/assets/javascripts/luca/concerns/development_tool_helpers.coffee +23 -14
  29. data/app/assets/javascripts/luca/concerns/dom_helpers.coffee +2 -2
  30. data/app/assets/javascripts/luca/concerns/filterable.coffee +8 -11
  31. data/app/assets/javascripts/luca/concerns/form_model_bindings.coffee +20 -0
  32. data/app/assets/javascripts/luca/concerns/modal_view.coffee +40 -15
  33. data/app/assets/javascripts/luca/concerns/query_collection_bindings.coffee +7 -1
  34. data/app/assets/javascripts/luca/concerns/state_model.coffee +40 -26
  35. data/app/assets/javascripts/luca/concerns/templating.coffee +3 -1
  36. data/app/assets/javascripts/luca/config.coffee +5 -0
  37. data/app/assets/javascripts/luca/containers/card_view.coffee +87 -52
  38. data/app/assets/javascripts/luca/containers/container.coffee +305 -108
  39. data/app/assets/javascripts/luca/containers/modal_view.coffee +9 -9
  40. data/app/assets/javascripts/luca/containers/page_controller.coffee +25 -0
  41. data/app/assets/javascripts/luca/containers/panel_toolbar.coffee +5 -6
  42. data/app/assets/javascripts/luca/containers/tab_view.coffee +19 -10
  43. data/app/assets/javascripts/luca/containers/viewport.coffee +12 -16
  44. data/app/assets/javascripts/luca/core/collection.coffee +19 -5
  45. data/app/assets/javascripts/luca/core/events.coffee +5 -5
  46. data/app/assets/javascripts/luca/core/model.coffee +1 -1
  47. data/app/assets/javascripts/luca/core/panel.coffee +18 -6
  48. data/app/assets/javascripts/luca/core/registry/component_definition.coffee +2 -1
  49. data/app/assets/javascripts/luca/core/registry/meta_data.coffee +2 -0
  50. data/app/assets/javascripts/luca/core/registry/registry.coffee +14 -11
  51. data/app/assets/javascripts/luca/core/templates.coffee +5 -1
  52. data/app/assets/javascripts/luca/core/view.coffee +200 -47
  53. data/app/assets/javascripts/luca/dependencies.coffee +2 -0
  54. data/app/assets/javascripts/luca/development/code_sync_manager.coffee +173 -0
  55. data/app/assets/javascripts/luca/development/component.coffee +76 -0
  56. data/app/assets/javascripts/luca/development/components.coffee +57 -0
  57. data/app/assets/javascripts/luca/development/console.coffee +1 -1
  58. data/app/assets/javascripts/luca/development/index.coffee +4 -1
  59. data/app/assets/javascripts/luca/framework.coffee +7 -3
  60. data/app/assets/javascripts/luca/index.coffee +2 -1
  61. data/app/assets/javascripts/luca/managers/collection_manager.coffee +2 -3
  62. data/app/assets/javascripts/luca/managers/index.coffee +1 -1
  63. data/app/assets/javascripts/luca/managers/socket_manager.coffee +31 -8
  64. data/app/assets/javascripts/luca/templates/components/nav_bar.jst.ejs +16 -1
  65. data/app/assets/javascripts/luca/templates/containers/tab_view.jst.ejs +1 -1
  66. data/app/assets/javascripts/luca/util/index.coffee +1 -0
  67. data/app/assets/javascripts/luca/util/keybindings.coffee +24 -0
  68. data/app/assets/javascripts/luca/util/logging.coffee +15 -0
  69. data/app/assets/javascripts/luca/util/luca.coffee +9 -1
  70. data/app/assets/stylesheets/luca/components/table_view.scss +85 -0
  71. data/app/assets/stylesheets/luca/components/viewport.scss +0 -4
  72. data/app/assets/stylesheets/luca/containers/container.scss +8 -0
  73. data/app/assets/stylesheets/luca/index.css +2 -2
  74. data/bin/luca +14 -0
  75. data/config.ru +1 -2
  76. data/docs/framework.json +1 -0
  77. data/docs/luca-framework-documentation.js +1 -0
  78. data/docs/{application.md → old/application.md} +0 -0
  79. data/docs/{collection.md → old/collection.md} +0 -0
  80. data/docs/{collection_manager.md → old/collection_manager.md} +0 -0
  81. data/docs/{container_philosophy.md → old/container_philosophy.md} +0 -0
  82. data/docs/{event_binding_helpers.md → old/event_binding_helpers.md} +0 -0
  83. data/docs/{method_caching_and_computed_properties.md → old/method_caching_and_computed_properties.md} +0 -0
  84. data/docs/{view.md → old/view.md} +0 -0
  85. data/lib/generators/luca/application/templates/javascripts/dependencies.coffee +2 -5
  86. data/lib/guard/luca.rb +84 -0
  87. data/lib/luca.rb +25 -1
  88. data/lib/luca/asset_compiler.rb +117 -0
  89. data/lib/luca/cli.rb +68 -0
  90. data/lib/luca/cli/generate.rb +37 -0
  91. data/lib/luca/cli/server.rb +20 -0
  92. data/lib/luca/cli/sync.rb +40 -0
  93. data/lib/luca/cli/watch.rb +16 -0
  94. data/lib/luca/collection.rb +64 -0
  95. data/lib/luca/collection/endpoint.rb +38 -0
  96. data/lib/luca/collection/file_backend.rb +121 -0
  97. data/lib/luca/collection/redis_backend.rb +153 -0
  98. data/lib/luca/compiled_asset.rb +61 -0
  99. data/lib/luca/component_definition.rb +356 -0
  100. data/lib/luca/luca_application.rb +258 -0
  101. data/lib/luca/project.rb +73 -0
  102. data/lib/luca/project_harness.rb +96 -0
  103. data/lib/luca/rails.rb +5 -3
  104. data/lib/luca/rails/engine.rb +8 -0
  105. data/lib/luca/rails/version.rb +1 -2
  106. data/lib/luca/server.rb +7 -0
  107. data/lib/luca/stylesheet.rb +35 -0
  108. data/lib/luca/template.rb +2 -0
  109. data/lib/luca/template_asset.rb +64 -0
  110. data/lib/luca/version.rb +3 -0
  111. data/lib/luca/watcher.rb +72 -0
  112. data/lib/railties/luca/tasks.rake +7 -0
  113. data/site/.bundle/config +2 -0
  114. data/site/.gitignore +5 -0
  115. data/site/.rvmrc +1 -0
  116. data/site/CHANGELOG.md +41 -0
  117. data/site/DOCS.md +41 -0
  118. data/site/Gemfile +8 -0
  119. data/site/Gemfile.lock +134 -0
  120. data/site/LICENSE.md +19 -0
  121. data/site/config.rb +84 -0
  122. data/site/helpers/site_helpers.rb +20 -0
  123. data/site/html5bp-docs/README.md +38 -0
  124. data/site/html5bp-docs/contribute.md +104 -0
  125. data/site/html5bp-docs/crossdomain.md +21 -0
  126. data/site/html5bp-docs/css.md +135 -0
  127. data/site/html5bp-docs/extend.md +507 -0
  128. data/site/html5bp-docs/faq.md +77 -0
  129. data/site/html5bp-docs/htaccess.md +323 -0
  130. data/site/html5bp-docs/html.md +170 -0
  131. data/site/html5bp-docs/js.md +31 -0
  132. data/site/html5bp-docs/misc.md +25 -0
  133. data/site/html5bp-docs/usage.md +109 -0
  134. data/site/readme.md +47 -0
  135. data/site/source/.htaccess +540 -0
  136. data/site/source/404.html +157 -0
  137. data/site/source/app/assets/javascripts/dependencies.js.coffee +6 -0
  138. data/site/source/app/assets/javascripts/docs-docs.js +1 -0
  139. data/site/source/app/assets/javascripts/docs/application.coffee +64 -0
  140. data/site/source/app/assets/javascripts/docs/collections/docs_documentation.coffee +17 -0
  141. data/site/source/app/assets/javascripts/docs/collections/github_repositories.coffee +7 -0
  142. data/site/source/app/assets/javascripts/docs/collections/index.coffee +1 -0
  143. data/site/source/app/assets/javascripts/docs/collections/luca_documentation.coffee +17 -0
  144. data/site/source/app/assets/javascripts/docs/collections/public_gists.coffee +4 -0
  145. data/site/source/app/assets/javascripts/docs/config.coffee +5 -0
  146. data/site/source/app/assets/javascripts/docs/index.coffee +12 -0
  147. data/site/source/app/assets/javascripts/docs/lib/router.coffee +3 -0
  148. data/{spec/components/application_spec.coffee → site/source/app/assets/javascripts/docs/lib/util.coffee} +0 -0
  149. data/site/source/app/assets/javascripts/docs/models/component.coffee +99 -0
  150. data/site/source/app/assets/javascripts/docs/models/github_repository.coffee +3 -0
  151. data/site/source/app/assets/javascripts/docs/models/index.coffee +1 -0
  152. data/site/source/app/assets/javascripts/docs/templates/component_documentation.jst.ejs +55 -0
  153. data/site/source/app/assets/javascripts/docs/templates/examples_browser/overview.jst.ejs +4 -0
  154. data/site/source/app/assets/javascripts/docs/templates/examples_browser/selector.jst.ejs +11 -0
  155. data/site/source/app/assets/javascripts/docs/templates/github_repository.jst.ejs +4 -0
  156. data/site/source/app/assets/javascripts/docs/templates/layouts/main.jst.ejs +4 -0
  157. data/site/source/app/assets/javascripts/docs/templates/left_navigation.jst.ejs +5 -0
  158. data/site/source/app/assets/javascripts/docs/templates/pages/getting_started.jst.ejs +78 -0
  159. data/site/source/app/assets/javascripts/docs/templates/pages/home.jst.ejs +57 -0
  160. data/site/source/app/assets/javascripts/docs/views/components/code_editor.coffee +45 -0
  161. data/{spec/components/collection_loader_view_spec.coffee → site/source/app/assets/javascripts/docs/views/components/code_editor/index.coffee} +0 -0
  162. data/site/source/app/assets/javascripts/docs/views/components/component_documentation.coffee +72 -0
  163. data/site/source/app/assets/javascripts/docs/views/index.coffee +3 -0
  164. data/site/source/app/assets/javascripts/docs/views/pages/browse_source.coffee +46 -0
  165. data/site/source/app/assets/javascripts/docs/views/pages/browse_source/details.coffee +37 -0
  166. data/site/source/app/assets/javascripts/docs/views/pages/browse_source/list.coffee +31 -0
  167. data/site/source/app/assets/javascripts/docs/views/pages/component_editor.coffee +10 -0
  168. data/site/source/app/assets/javascripts/docs/views/pages/examples_browser.coffee +102 -0
  169. data/site/source/app/assets/javascripts/docs/views/pages/examples_browser/docs.coffee +12 -0
  170. data/site/source/app/assets/javascripts/docs/views/pages/examples_browser/source.coffee +13 -0
  171. data/site/source/app/assets/javascripts/docs/views/pages/home.coffee +10 -0
  172. data/site/source/app/assets/javascripts/docs/views/views/api_browser/index.coffee +43 -0
  173. data/site/source/app/assets/javascripts/docs/views/views/collection_view_examples/grid_layout_view_example.coffee +14 -0
  174. data/site/source/app/assets/javascripts/docs/views/views/collection_view_examples/table_view_example.coffee +39 -0
  175. data/site/source/app/assets/javascripts/docs/views/views/form_view_examples/basic_example.coffee +38 -0
  176. data/site/source/app/assets/javascripts/docs/views/views/form_view_examples/complex_layout.coffee +110 -0
  177. data/site/source/app/assets/javascripts/docs/views/views/top_navigation.coffee +6 -0
  178. data/site/source/app/assets/javascripts/luca-docs.js +1 -0
  179. data/site/source/app/assets/javascripts/luca-framework-documentation.js +1 -0
  180. data/site/source/app/assets/javascripts/site.js.coffee +4 -0
  181. data/site/source/app/assets/javascripts/vendor/codemirror.js +4786 -0
  182. data/site/source/app/assets/javascripts/vendor/coffeescript.js +346 -0
  183. data/site/source/app/assets/javascripts/vendor/css.js +465 -0
  184. data/site/source/app/assets/javascripts/vendor/htmlmixed.js +84 -0
  185. data/site/source/app/assets/javascripts/vendor/javascript.js +422 -0
  186. data/site/source/app/assets/javascripts/vendor/js-beautify.js +1353 -0
  187. data/site/source/app/assets/javascripts/vendor/modernizr-2.6.1.min.js +4 -0
  188. data/site/source/app/assets/javascripts/vendor/vim.js +2511 -0
  189. data/site/source/app/assets/stylesheets/docs/api-browser.css.scss +5 -0
  190. data/site/source/app/assets/stylesheets/docs/application.css.scss +35 -0
  191. data/site/source/app/assets/stylesheets/docs/browse-source.css.scss +5 -0
  192. data/site/source/app/assets/stylesheets/docs/scrollable-table.css.scss +5 -0
  193. data/site/source/app/assets/stylesheets/site.css.scss +2 -0
  194. data/site/source/app/assets/stylesheets/vendor/codemirror.css +240 -0
  195. data/site/source/app/assets/stylesheets/vendor/prettify-tomorrow-night-bright.css +160 -0
  196. data/site/source/app/assets/stylesheets/vendor/twilight.css +26 -0
  197. data/site/source/crossdomain.xml +15 -0
  198. data/site/source/documentation.html.haml +1 -0
  199. data/site/source/favicon_base.png +0 -0
  200. data/site/source/humans.txt +15 -0
  201. data/site/source/images/background.png +0 -0
  202. data/site/source/images/middleman.png +0 -0
  203. data/site/source/index.html.haml +1 -0
  204. data/site/source/layouts/layout.haml +55 -0
  205. data/site/source/readme.md +63 -0
  206. data/site/source/robots.txt +3 -0
  207. data/spec/{components/grid_view_spec.coffee → javascripts/components/application_spec.coffee} +0 -0
  208. data/spec/{components/pagination_control_spec.coffee → javascripts/components/collection_loader_view_spec.coffee} +0 -0
  209. data/spec/{components → javascripts/components}/collection_view_spec.coffee +1 -1
  210. data/spec/{components → javascripts/components}/controller_spec.coffee +0 -0
  211. data/spec/{components → javascripts/components}/fields/checkbox_array_spec.coffee +0 -0
  212. data/spec/javascripts/components/form_view_spec.coffee +162 -0
  213. data/spec/{components/record_manager_spec.coffee → javascripts/components/grid_view_spec.coffee} +0 -0
  214. data/spec/{components → javascripts/components}/multi_collection_view_spec.coffee +0 -0
  215. data/spec/{components/template_spec.coffee → javascripts/components/pagination_control_spec.coffee} +0 -0
  216. data/spec/{concerns/paginatable_spec.coffee → javascripts/components/record_manager_spec.coffee} +0 -0
  217. data/spec/{components → javascripts/components}/table_view_spec.coffee +0 -0
  218. data/spec/{containers/modal_view_spec.coffee → javascripts/components/template_spec.coffee} +0 -0
  219. data/spec/{concerns → javascripts/concerns}/collection_event_bindings_spec.coffee +0 -0
  220. data/spec/{concerns → javascripts/concerns}/dom_helpers_spec.coffee +0 -0
  221. data/spec/{concerns → javascripts/concerns}/filterable_spec.coffee +0 -0
  222. data/spec/{concerns → javascripts/concerns}/model_presenter_spec.coffee +0 -0
  223. data/spec/{containers/panel_view_spec.coffee → javascripts/concerns/paginatable_spec.coffee} +0 -0
  224. data/spec/{concerns → javascripts/concerns}/state_model_spec.coffee +5 -0
  225. data/spec/javascripts/containers/card_view_spec.coffee +108 -0
  226. data/spec/{containers/tab_view_spec.coffee → javascripts/containers/modal_view_spec.coffee} +0 -0
  227. data/spec/{containers/viewport_spec.coffee → javascripts/containers/panel_view_spec.coffee} +0 -0
  228. data/spec/{core/observer_spec.coffee → javascripts/containers/tab_view_spec.coffee} +0 -0
  229. data/spec/{managers/socket_manager_spec.coffee → javascripts/containers/viewport_spec.coffee} +0 -0
  230. data/spec/{core → javascripts/core}/collection_spec.coffee +1 -1
  231. data/spec/{core → javascripts/core}/concerns_spec.coffee +0 -0
  232. data/spec/{core → javascripts/core}/container_spec.coffee +0 -0
  233. data/spec/{core → javascripts/core}/define_spec.coffee +0 -0
  234. data/spec/{core → javascripts/core}/events_spec.coffee +0 -0
  235. data/spec/{core → javascripts/core}/field_spec.coffee +0 -0
  236. data/spec/{core → javascripts/core}/framework_spec.coffee +0 -0
  237. data/spec/{core → javascripts/core}/model_spec.coffee +0 -0
  238. data/spec/javascripts/core/observer_spec.coffee +0 -0
  239. data/spec/{core → javascripts/core}/util_spec.coffee +0 -0
  240. data/spec/{core → javascripts/core}/view_spec.coffee +51 -39
  241. data/spec/{dependencies → javascripts/dependencies}/index.coffee +0 -0
  242. data/spec/{dependencies → javascripts/dependencies}/jasmine-html.js +0 -0
  243. data/spec/{dependencies → javascripts/dependencies}/jasmine.js +0 -0
  244. data/spec/{dependencies → javascripts/dependencies}/sinon.js +0 -0
  245. data/spec/{helper.coffee → javascripts/helper.coffee} +0 -0
  246. data/spec/{managers → javascripts/managers}/collection_manager_spec.coffee +0 -0
  247. data/spec/javascripts/managers/socket_manager_spec.coffee +0 -0
  248. data/spec/lib/component_definition_spec.rb +63 -0
  249. data/spec/lib/input_compiler_spec.rb +9 -0
  250. data/spec/lib/luca_application_spec.rb +30 -0
  251. data/spec/support/fixtures/application.coffee +45 -0
  252. data/spec/support/fixtures/component.coffee +34 -0
  253. data/tutorials/component-definitions.md +0 -0
  254. data/tutorials/component-definitions/01_intro.md +0 -0
  255. data/tutorials/component-driven-design.md +140 -0
  256. data/tutorials/structure-of-a-project.md +63 -0
  257. data/vendor/assets/javascripts/backbone-min.js +37 -33
  258. data/vendor/assets/javascripts/backbone-query.min.js +1 -1
  259. data/vendor/assets/javascripts/hogan.js +707 -0
  260. data/vendor/assets/javascripts/jquery.js +5 -4
  261. data/vendor/assets/javascripts/keymaster.min.js +4 -0
  262. data/vendor/assets/javascripts/luca-dependencies.min.js +8 -0
  263. data/vendor/assets/javascripts/luca-development.min.js +1 -0
  264. data/vendor/assets/javascripts/luca-spec.js +6 -6
  265. data/vendor/assets/javascripts/luca-ui.js +7386 -0
  266. data/vendor/assets/javascripts/luca-ui.min.js +5 -0
  267. data/vendor/assets/javascripts/luca.full.min.js +12 -0
  268. data/vendor/assets/javascripts/luca.min.js +5 -0
  269. data/vendor/assets/javascripts/underscore-min.js +1 -5
  270. data/vendor/assets/javascripts/underscore-string.min.js +1 -1
  271. data/vendor/assets/stylesheets/luca-components.css +202 -0
  272. data/vendor/assets/stylesheets/luca-development.css +23 -0
  273. data/vendor/assets/stylesheets/luca-ui.css +198 -0
  274. metadata +324 -94
  275. data/app/assets/javascripts/luca/components/base_toolbar.coffee +0 -17
  276. data/app/assets/javascripts/luca/components/form_button_toolbar.coffee +0 -28
  277. data/app/assets/javascripts/luca/components/grid_view.coffee +0 -269
  278. data/app/assets/javascripts/luca/components/page_controller.coffee +0 -7
  279. data/app/assets/javascripts/luca/components/template.coffee +0 -5
  280. data/app/assets/javascripts/luca/components/toolbar_dialog.coffee +0 -25
  281. data/lib/luca/code_browser.rb +0 -55
  282. data/lib/luca/command_line.rb +0 -69
  283. data/lib/luca/component_documentation.rb +0 -72
  284. data/site/assets/bootstrap.min.js +0 -7
  285. data/site/assets/dependencies.js +0 -94
  286. data/site/assets/glyphicons-halflings-white.png +0 -0
  287. data/site/assets/glyphicons-halflings.png +0 -0
  288. data/site/assets/luca-ui-bootstrap.css +0 -1331
  289. data/site/assets/luca-ui-bootstrap.js +0 -9
  290. data/site/assets/luca-ui-development-tools.css +0 -234
  291. data/site/assets/luca-ui-development-tools.js +0 -18561
  292. data/site/assets/luca-ui-development-tools.min.js +0 -15
  293. data/site/assets/luca-ui-full.min.js +0 -8
  294. data/site/assets/luca-ui.min.js +0 -4
  295. data/site/assets/sandbox.css +0 -62
  296. data/site/assets/sandbox.js +0 -469
  297. data/site/docs/application.html +0 -41
  298. data/site/docs/caching.html +0 -43
  299. data/site/docs/collection.html +0 -75
  300. data/site/docs/collection_manager.html +0 -71
  301. data/site/docs/containers.html +0 -118
  302. data/site/docs/events.html +0 -153
  303. data/site/docs/view.html +0 -128
  304. data/site/img/glyphicons-halflings-white.png +0 -0
  305. data/site/img/glyphicons-halflings.png +0 -0
  306. data/site/index.html +0 -20
  307. data/site/source-map.js +0 -1
  308. data/spec/components/form_view_spec.coffee +0 -84
  309. data/spec/containers/card_view_spec.coffee +0 -50
  310. data/spec/luca-spec.coffee +0 -9
@@ -0,0 +1,1353 @@
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, <einar@jsbeautifier.org>
10
+ http://jsbeautifier.org/
11
+
12
+ Originally converted to javascript by Vital, <vital76@gmail.com>
13
+ "End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
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
+ var last_output = ' ';
181
+
182
+ if (flags.eat_next_space) {
183
+ flags.eat_next_space = false;
184
+ } else if (last_type === 'TK_COMMENT') {
185
+ print_newline();
186
+ } else {
187
+ if (output.length) {
188
+ last_output = output[output.length - 1];
189
+ }
190
+ if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
191
+ output.push(' ');
192
+ }
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_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
232
+ ternary_depth: 0
233
+ };
234
+ }
235
+
236
+ function is_array(mode) {
237
+ return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
238
+ }
239
+
240
+ function is_expression(mode) {
241
+ return in_array(mode, ['[EXPRESSION]', '(EXPRESSION)', '(FOR-EXPRESSION)', '(COND-EXPRESSION)']);
242
+ }
243
+
244
+ function restore_mode() {
245
+ do_block_just_closed = flags.mode === 'DO_BLOCK';
246
+ if (flag_store.length > 0) {
247
+ var mode = flags.mode;
248
+ flags = flag_store.pop();
249
+ flags.previous_mode = mode;
250
+ }
251
+ }
252
+
253
+ function all_lines_start_with(lines, c) {
254
+ for (var i = 0; i < lines.length; i++) {
255
+ var line = trim(lines[i]);
256
+ if (line.charAt(0) !== c) {
257
+ return false;
258
+ }
259
+ }
260
+ return true;
261
+ }
262
+
263
+ function is_special_word(word) {
264
+ return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']);
265
+ }
266
+
267
+ function in_array(what, arr) {
268
+ for (var i = 0; i < arr.length; i += 1) {
269
+ if (arr[i] === what) {
270
+ return true;
271
+ }
272
+ }
273
+ return false;
274
+ }
275
+
276
+ function unescape_string(s) {
277
+ var esc = false,
278
+ out = '',
279
+ pos = 0,
280
+ s_hex = '',
281
+ escaped = 0,
282
+ c;
283
+
284
+ while (esc || pos < s.length) {
285
+
286
+ c = s.charAt(pos);
287
+ pos++;
288
+
289
+ if (esc) {
290
+ esc = false;
291
+ if (c === 'x') {
292
+ // simple hex-escape \x24
293
+ s_hex = s.substr(pos, 2);
294
+ pos += 2;
295
+ } else if (c === 'u') {
296
+ // unicode-escape, \u2134
297
+ s_hex = s.substr(pos, 4);
298
+ pos += 4;
299
+ } else {
300
+ // some common escape, e.g \n
301
+ out += '\\' + c;
302
+ continue;
303
+ }
304
+ if ( ! s_hex.match(/^[0123456789abcdefABCDEF]+$/)) {
305
+ // some weird escaping, bail out,
306
+ // leaving whole string intact
307
+ return s;
308
+ }
309
+
310
+ escaped = parseInt(s_hex, 16);
311
+
312
+ if (escaped >= 0x00 && escaped < 0x20) {
313
+ // leave 0x00...0x1f escaped
314
+ if (c === 'x') {
315
+ out += '\\x' + s_hex;
316
+ } else {
317
+ out += '\\u' + s_hex;
318
+ }
319
+ continue;
320
+ } else if (escaped == 0x22 || escaped === 0x27 || escaped == 0x5c) {
321
+ // single-quote, apostrophe, backslash - escape these
322
+ out += '\\' + String.fromCharCode(escaped);
323
+ } else if (c === 'x' && escaped > 0x7e && escaped <= 0xff) {
324
+ // we bail out on \x7f..\xff,
325
+ // leaving whole string escaped,
326
+ // as it's probably completely binary
327
+ return s;
328
+ } else {
329
+ out += String.fromCharCode(escaped);
330
+ }
331
+ } else if (c == '\\') {
332
+ esc = true;
333
+ } else {
334
+ out += c;
335
+ }
336
+ }
337
+ return out;
338
+ }
339
+
340
+ function look_up(exclude) {
341
+ var local_pos = parser_pos;
342
+ var c = input.charAt(local_pos);
343
+ while (in_array(c, whitespace) && c !== exclude) {
344
+ local_pos++;
345
+ if (local_pos >= input_length) {
346
+ return 0;
347
+ }
348
+ c = input.charAt(local_pos);
349
+ }
350
+ return c;
351
+ }
352
+
353
+ function get_next_token() {
354
+ var i;
355
+
356
+ n_newlines = 0;
357
+
358
+ if (parser_pos >= input_length) {
359
+ return ['', 'TK_EOF'];
360
+ }
361
+
362
+ wanted_newline = false;
363
+
364
+ var c = input.charAt(parser_pos);
365
+ parser_pos += 1;
366
+
367
+
368
+ var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
369
+
370
+ if (keep_whitespace) {
371
+
372
+ var whitespace_count = 0;
373
+
374
+ while (in_array(c, whitespace)) {
375
+
376
+ if (c === "\n") {
377
+ trim_output();
378
+ output.push("\n");
379
+ just_added_newline = true;
380
+ whitespace_count = 0;
381
+ } else {
382
+ if (just_added_newline) {
383
+ if (c === indent_string) {
384
+ output.push(indent_string);
385
+ } else {
386
+ if (c !== '\r') {
387
+ output.push(' ');
388
+ }
389
+ }
390
+ }
391
+ }
392
+
393
+ if (parser_pos >= input_length) {
394
+ return ['', 'TK_EOF'];
395
+ }
396
+
397
+ c = input.charAt(parser_pos);
398
+ parser_pos += 1;
399
+
400
+ }
401
+
402
+ } else {
403
+ while (in_array(c, whitespace)) {
404
+
405
+ if (c === "\n") {
406
+ n_newlines += ((opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1 : 0 : 1);
407
+ }
408
+
409
+
410
+ if (parser_pos >= input_length) {
411
+ return ['', 'TK_EOF'];
412
+ }
413
+
414
+ c = input.charAt(parser_pos);
415
+ parser_pos += 1;
416
+
417
+ }
418
+
419
+ if (opt_preserve_newlines) {
420
+ if (n_newlines > 1) {
421
+ for (i = 0; i < n_newlines; i += 1) {
422
+ print_newline(i === 0);
423
+ just_added_newline = true;
424
+ }
425
+ }
426
+ }
427
+ wanted_newline = n_newlines > 0;
428
+ }
429
+
430
+
431
+ if (in_array(c, wordchar)) {
432
+ if (parser_pos < input_length) {
433
+ while (in_array(input.charAt(parser_pos), wordchar)) {
434
+ c += input.charAt(parser_pos);
435
+ parser_pos += 1;
436
+ if (parser_pos === input_length) {
437
+ break;
438
+ }
439
+ }
440
+ }
441
+
442
+ // small and surprisingly unugly hack for 1E-10 representation
443
+ if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
444
+
445
+ var sign = input.charAt(parser_pos);
446
+ parser_pos += 1;
447
+
448
+ var t = get_next_token();
449
+ c += sign + t[0];
450
+ return [c, 'TK_WORD'];
451
+ }
452
+
453
+ if (c === 'in') { // hack for 'in' operator
454
+ return [c, 'TK_OPERATOR'];
455
+ }
456
+ if (wanted_newline && last_type !== 'TK_OPERATOR'
457
+ && last_type !== 'TK_EQUALS'
458
+ && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) {
459
+ print_newline();
460
+ }
461
+ return [c, 'TK_WORD'];
462
+ }
463
+
464
+ if (c === '(' || c === '[') {
465
+ return [c, 'TK_START_EXPR'];
466
+ }
467
+
468
+ if (c === ')' || c === ']') {
469
+ return [c, 'TK_END_EXPR'];
470
+ }
471
+
472
+ if (c === '{') {
473
+ return [c, 'TK_START_BLOCK'];
474
+ }
475
+
476
+ if (c === '}') {
477
+ return [c, 'TK_END_BLOCK'];
478
+ }
479
+
480
+ if (c === ';') {
481
+ return [c, 'TK_SEMICOLON'];
482
+ }
483
+
484
+ if (c === '/') {
485
+ var comment = '';
486
+ // peek for comment /* ... */
487
+ var inline_comment = true;
488
+ if (input.charAt(parser_pos) === '*') {
489
+ parser_pos += 1;
490
+ if (parser_pos < input_length) {
491
+ while (parser_pos < input_length &&
492
+ ! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/')) {
493
+ c = input.charAt(parser_pos);
494
+ comment += c;
495
+ if (c === "\n" || c === "\r") {
496
+ inline_comment = false;
497
+ }
498
+ parser_pos += 1;
499
+ if (parser_pos >= input_length) {
500
+ break;
501
+ }
502
+ }
503
+ }
504
+ parser_pos += 2;
505
+ if (inline_comment && n_newlines === 0) {
506
+ return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
507
+ } else {
508
+ return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
509
+ }
510
+ }
511
+ // peek for comment // ...
512
+ if (input.charAt(parser_pos) === '/') {
513
+ comment = c;
514
+ while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') {
515
+ comment += input.charAt(parser_pos);
516
+ parser_pos += 1;
517
+ if (parser_pos >= input_length) {
518
+ break;
519
+ }
520
+ }
521
+ if (wanted_newline) {
522
+ print_newline();
523
+ }
524
+ return [comment, 'TK_COMMENT'];
525
+ }
526
+
527
+ }
528
+
529
+ if (c === "'" || // string
530
+ c === '"' || // string
531
+ (c === '/' &&
532
+ ((last_type === 'TK_WORD' && is_special_word(last_text)) ||
533
+ (last_text === ')' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) ||
534
+ (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
535
+ var sep = c,
536
+ esc = false,
537
+ has_char_escapes = false,
538
+ resulting_string = c;
539
+
540
+ if (parser_pos < input_length) {
541
+ if (sep === '/') {
542
+ //
543
+ // handle regexp separately...
544
+ //
545
+ var in_char_class = false;
546
+ while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
547
+ resulting_string += input.charAt(parser_pos);
548
+ if (!esc) {
549
+ esc = input.charAt(parser_pos) === '\\';
550
+ if (input.charAt(parser_pos) === '[') {
551
+ in_char_class = true;
552
+ } else if (input.charAt(parser_pos) === ']') {
553
+ in_char_class = false;
554
+ }
555
+ } else {
556
+ esc = false;
557
+ }
558
+ parser_pos += 1;
559
+ if (parser_pos >= input_length) {
560
+ // incomplete string/rexp when end-of-file reached.
561
+ // bail out with what had been received so far.
562
+ return [resulting_string, 'TK_STRING'];
563
+ }
564
+ }
565
+
566
+ } else {
567
+ //
568
+ // and handle string also separately
569
+ //
570
+ while (esc || input.charAt(parser_pos) !== sep) {
571
+ resulting_string += input.charAt(parser_pos);
572
+ if (esc) {
573
+ if (input.charAt(parser_pos) === 'x' || input.charAt(parser_pos) === 'u') {
574
+ has_char_escapes = true;
575
+ }
576
+ esc = false;
577
+ } else {
578
+ esc = input.charAt(parser_pos) === '\\';
579
+ }
580
+ parser_pos += 1;
581
+ if (parser_pos >= input_length) {
582
+ // incomplete string/rexp when end-of-file reached.
583
+ // bail out with what had been received so far.
584
+ return [resulting_string, 'TK_STRING'];
585
+ }
586
+ }
587
+
588
+ }
589
+ }
590
+
591
+ parser_pos += 1;
592
+ resulting_string += sep;
593
+
594
+ if (has_char_escapes && opt_unescape_strings) {
595
+ resulting_string = unescape_string(resulting_string);
596
+ }
597
+
598
+ if (sep === '/') {
599
+ // regexps may have modifiers /regexp/MOD , so fetch those, too
600
+ while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
601
+ resulting_string += input.charAt(parser_pos);
602
+ parser_pos += 1;
603
+ }
604
+ }
605
+ return [resulting_string, 'TK_STRING'];
606
+ }
607
+
608
+ if (c === '#') {
609
+
610
+
611
+ if (output.length === 0 && input.charAt(parser_pos) === '!') {
612
+ // shebang
613
+ resulting_string = c;
614
+ while (parser_pos < input_length && c !== '\n') {
615
+ c = input.charAt(parser_pos);
616
+ resulting_string += c;
617
+ parser_pos += 1;
618
+ }
619
+ output.push(trim(resulting_string) + '\n');
620
+ print_newline();
621
+ return get_next_token();
622
+ }
623
+
624
+
625
+
626
+ // Spidermonkey-specific sharp variables for circular references
627
+ // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
628
+ // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
629
+ var sharp = '#';
630
+ if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
631
+ do {
632
+ c = input.charAt(parser_pos);
633
+ sharp += c;
634
+ parser_pos += 1;
635
+ } while (parser_pos < input_length && c !== '#' && c !== '=');
636
+ if (c === '#') {
637
+ //
638
+ } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
639
+ sharp += '[]';
640
+ parser_pos += 2;
641
+ } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
642
+ sharp += '{}';
643
+ parser_pos += 2;
644
+ }
645
+ return [sharp, 'TK_WORD'];
646
+ }
647
+ }
648
+
649
+ if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
650
+ parser_pos += 3;
651
+ c = '<!--';
652
+ while (input.charAt(parser_pos) !== '\n' && parser_pos < input_length) {
653
+ c += input.charAt(parser_pos);
654
+ parser_pos++;
655
+ }
656
+ flags.in_html_comment = true;
657
+ return [c, 'TK_COMMENT'];
658
+ }
659
+
660
+ if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
661
+ flags.in_html_comment = false;
662
+ parser_pos += 2;
663
+ if (wanted_newline) {
664
+ print_newline();
665
+ }
666
+ return ['-->', 'TK_COMMENT'];
667
+ }
668
+
669
+ if (c === '.') {
670
+ return [c, 'TK_DOT'];
671
+ }
672
+
673
+ if (in_array(c, punct)) {
674
+ while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
675
+ c += input.charAt(parser_pos);
676
+ parser_pos += 1;
677
+ if (parser_pos >= input_length) {
678
+ break;
679
+ }
680
+ }
681
+
682
+ if (c === ',') {
683
+ return [c, 'TK_COMMA'];
684
+ } else if (c === '=') {
685
+ return [c, 'TK_EQUALS'];
686
+ } else {
687
+ return [c, 'TK_OPERATOR'];
688
+ }
689
+ }
690
+
691
+ return [c, 'TK_UNKNOWN'];
692
+ }
693
+
694
+ //----------------------------------
695
+ indent_string = '';
696
+ while (opt_indent_size > 0) {
697
+ indent_string += opt_indent_char;
698
+ opt_indent_size -= 1;
699
+ }
700
+
701
+ while (js_source_text && (js_source_text.charAt(0) === ' ' || js_source_text.charAt(0) === '\t')) {
702
+ preindent_string += js_source_text.charAt(0);
703
+ js_source_text = js_source_text.substring(1);
704
+ }
705
+ input = js_source_text;
706
+
707
+ last_word = ''; // last 'TK_WORD' passed
708
+ last_type = 'TK_START_EXPR'; // last token type
709
+ last_text = ''; // last token text
710
+ last_last_text = ''; // pre-last token text
711
+ output = [];
712
+
713
+ do_block_just_closed = false;
714
+
715
+ whitespace = "\n\r\t ".split('');
716
+ wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
717
+ digits = '0123456789'.split('');
718
+
719
+ punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::';
720
+ punct += ' <%= <% %> <?= <? ?>'; // try to be a good boy and try not to break the markup language identifiers
721
+ punct = punct.split(' ');
722
+
723
+ // words which should always start on new line.
724
+ line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
725
+
726
+ // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'.
727
+ // some formatting depends on that.
728
+ flag_store = [];
729
+ set_mode('BLOCK');
730
+
731
+ parser_pos = 0;
732
+ while (true) {
733
+ var t = get_next_token();
734
+ token_text = t[0];
735
+ token_type = t[1];
736
+ if (token_type === 'TK_EOF') {
737
+ break;
738
+ }
739
+
740
+ switch (token_type) {
741
+
742
+ case 'TK_START_EXPR':
743
+
744
+ if (token_text === '[') {
745
+
746
+ if (last_type === 'TK_WORD' || last_text === ')') {
747
+ // this is array index specifier, break immediately
748
+ // a[x], fn()[x]
749
+ if (in_array(last_text, line_starters)) {
750
+ print_single_space();
751
+ }
752
+ set_mode('(EXPRESSION)');
753
+ print_token();
754
+ break;
755
+ }
756
+
757
+ if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') {
758
+ if (last_last_text === ']' && last_text === ',') {
759
+ // ], [ goes to new line
760
+ if (flags.mode === '[EXPRESSION]') {
761
+ flags.mode = '[INDENTED-EXPRESSION]';
762
+ if (!opt_keep_array_indentation) {
763
+ indent();
764
+ }
765
+ }
766
+ set_mode('[EXPRESSION]');
767
+ if (!opt_keep_array_indentation) {
768
+ print_newline();
769
+ }
770
+ } else if (last_text === '[') {
771
+ if (flags.mode === '[EXPRESSION]') {
772
+ flags.mode = '[INDENTED-EXPRESSION]';
773
+ if (!opt_keep_array_indentation) {
774
+ indent();
775
+ }
776
+ }
777
+ set_mode('[EXPRESSION]');
778
+
779
+ if (!opt_keep_array_indentation) {
780
+ print_newline();
781
+ }
782
+ } else {
783
+ set_mode('[EXPRESSION]');
784
+ }
785
+ } else {
786
+ set_mode('[EXPRESSION]');
787
+ }
788
+
789
+
790
+
791
+ } else {
792
+ if (last_word === 'for') {
793
+ set_mode('(FOR-EXPRESSION)');
794
+ } else if (in_array(last_word, ['if', 'while'])) {
795
+ set_mode('(COND-EXPRESSION)');
796
+ } else {
797
+ set_mode('(EXPRESSION)');
798
+ }
799
+ }
800
+
801
+ if (last_text === ';' || last_type === 'TK_START_BLOCK') {
802
+ print_newline();
803
+ } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') {
804
+ if (wanted_newline) {
805
+ print_newline();
806
+ }
807
+ // do nothing on (( and )( and ][ and ]( and .(
808
+ } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
809
+ print_single_space();
810
+ } else if (last_word === 'function' || last_word === 'typeof') {
811
+ // function() vs function ()
812
+ if (opt_jslint_happy) {
813
+ print_single_space();
814
+ }
815
+ } else if (in_array(last_text, line_starters) || last_text === 'catch') {
816
+ if (opt_space_before_conditional) {
817
+ print_single_space();
818
+ }
819
+ }
820
+ print_token();
821
+
822
+ break;
823
+
824
+ case 'TK_DOT':
825
+
826
+ if (is_special_word(last_text)) {
827
+ print_single_space();
828
+ } else if (last_text === ')') {
829
+ if (opt_break_chained_methods || wanted_newline) {
830
+ flags.chain_extra_indentation = 1;
831
+ print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
832
+ }
833
+ }
834
+
835
+ print_token();
836
+ break;
837
+
838
+ case 'TK_END_EXPR':
839
+ if (token_text === ']') {
840
+ if (opt_keep_array_indentation) {
841
+ if (last_text === '}') {
842
+ // trim_output();
843
+ // print_newline(true);
844
+ remove_indent();
845
+ print_token();
846
+ restore_mode();
847
+ break;
848
+ }
849
+ } else {
850
+ if (flags.mode === '[INDENTED-EXPRESSION]') {
851
+ if (last_text === ']') {
852
+ restore_mode();
853
+ print_newline();
854
+ print_token();
855
+ break;
856
+ }
857
+ }
858
+ }
859
+ }
860
+ restore_mode();
861
+ print_token();
862
+ break;
863
+
864
+ case 'TK_START_BLOCK':
865
+
866
+ if (last_word === 'do') {
867
+ set_mode('DO_BLOCK');
868
+ } else {
869
+ set_mode('BLOCK');
870
+ }
871
+ if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") {
872
+ var empty_braces = false;
873
+ if (opt_brace_style === "expand-strict") {
874
+ empty_braces = (look_up() === '}');
875
+ if (!empty_braces) {
876
+ print_newline(true);
877
+ }
878
+ } else {
879
+ if (last_type !== 'TK_OPERATOR') {
880
+ if (last_text === '=' || (is_special_word(last_text) && last_text !== 'else')) {
881
+ print_single_space();
882
+ } else {
883
+ print_newline(true);
884
+ }
885
+ }
886
+ }
887
+ print_token();
888
+ if (!empty_braces) {
889
+ indent();
890
+ }
891
+ } else {
892
+ if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
893
+ if (last_type === 'TK_START_BLOCK') {
894
+ print_newline();
895
+ } else {
896
+ print_single_space();
897
+ }
898
+ } else {
899
+ // if TK_OPERATOR or TK_START_EXPR
900
+ if (is_array(flags.previous_mode) && last_text === ',') {
901
+ if (last_last_text === '}') {
902
+ // }, { in array context
903
+ print_single_space();
904
+ } else {
905
+ print_newline(); // [a, b, c, {
906
+ }
907
+ }
908
+ }
909
+ indent();
910
+ print_token();
911
+ }
912
+
913
+ break;
914
+
915
+ case 'TK_END_BLOCK':
916
+ restore_mode();
917
+ if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") {
918
+ if (last_text !== '{') {
919
+ print_newline();
920
+ }
921
+ print_token();
922
+ } else {
923
+ if (last_type === 'TK_START_BLOCK') {
924
+ // nothing
925
+ if (just_added_newline) {
926
+ remove_indent();
927
+ } else {
928
+ // {}
929
+ trim_output();
930
+ }
931
+ } else {
932
+ if (is_array(flags.mode) && opt_keep_array_indentation) {
933
+ // we REALLY need a newline here, but newliner would skip that
934
+ opt_keep_array_indentation = false;
935
+ print_newline();
936
+ opt_keep_array_indentation = true;
937
+
938
+ } else {
939
+ print_newline();
940
+ }
941
+ }
942
+ print_token();
943
+ }
944
+ break;
945
+
946
+ case 'TK_WORD':
947
+
948
+ // no, it's not you. even I have problems understanding how this works
949
+ // and what does what.
950
+ if (do_block_just_closed) {
951
+ // do {} ## while ()
952
+ print_single_space();
953
+ print_token();
954
+ print_single_space();
955
+ do_block_just_closed = false;
956
+ break;
957
+ }
958
+
959
+ prefix = 'NONE';
960
+
961
+ if (token_text === 'function') {
962
+ if (flags.var_line && last_type !== 'TK_EQUALS' ) {
963
+ flags.var_line_reindented = true;
964
+ }
965
+ if ((just_added_newline || last_text === ';') && last_text !== '{'
966
+ && last_type !== 'TK_BLOCK_COMMENT' && last_type !== 'TK_COMMENT') {
967
+ // make sure there is a nice clean space of at least one blank line
968
+ // before a new function definition
969
+ n_newlines = just_added_newline ? n_newlines : 0;
970
+ if (!opt_preserve_newlines) {
971
+ n_newlines = 1;
972
+ }
973
+
974
+ for (var i = 0; i < 2 - n_newlines; i++) {
975
+ print_newline(false);
976
+ }
977
+ }
978
+ if (last_type === 'TK_WORD') {
979
+ if (last_text === 'get' || last_text === 'set' || last_text === 'new' || last_text === 'return') {
980
+ print_single_space();
981
+ } else {
982
+ print_newline();
983
+ }
984
+ } else if (last_type === 'TK_OPERATOR' || last_text === '=') {
985
+ // foo = function
986
+ print_single_space();
987
+ } else if (is_expression(flags.mode)) {
988
+ // print nothing
989
+ } else {
990
+ print_newline();
991
+ }
992
+
993
+ print_token();
994
+ last_word = token_text;
995
+ break;
996
+ }
997
+
998
+ if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) {
999
+ print_newline();
1000
+ if (flags.case_body) {
1001
+ // switch cases following one another
1002
+ flags.indentation_level--;
1003
+ flags.case_body = false;
1004
+ remove_indent();
1005
+ }
1006
+ print_token();
1007
+ flags.in_case = true;
1008
+ flags.in_case_statement = true;
1009
+ break;
1010
+ }
1011
+
1012
+ if (last_type === 'TK_END_BLOCK') {
1013
+
1014
+ if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
1015
+ prefix = 'NEWLINE';
1016
+ } else {
1017
+ if (opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") {
1018
+ prefix = 'NEWLINE';
1019
+ } else {
1020
+ prefix = 'SPACE';
1021
+ print_single_space();
1022
+ }
1023
+ }
1024
+ } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
1025
+ prefix = 'NEWLINE';
1026
+ } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
1027
+ prefix = 'SPACE';
1028
+ } else if (last_type === 'TK_STRING') {
1029
+ prefix = 'NEWLINE';
1030
+ } else if (last_type === 'TK_WORD') {
1031
+ if (last_text === 'else') {
1032
+ // eat newlines between ...else *** some_op...
1033
+ // won't preserve extra newlines in this place (if any), but don't care that much
1034
+ trim_output(true);
1035
+ }
1036
+ prefix = 'SPACE';
1037
+ } else if (last_type === 'TK_START_BLOCK') {
1038
+ prefix = 'NEWLINE';
1039
+ } else if (last_type === 'TK_END_EXPR') {
1040
+ print_single_space();
1041
+ prefix = 'NEWLINE';
1042
+ }
1043
+
1044
+ if (in_array(token_text, line_starters) && last_text !== ')') {
1045
+ if (last_text === 'else') {
1046
+ prefix = 'SPACE';
1047
+ } else {
1048
+ prefix = 'NEWLINE';
1049
+ }
1050
+
1051
+ }
1052
+
1053
+ if (flags.if_line && last_type === 'TK_END_EXPR') {
1054
+ flags.if_line = false;
1055
+ }
1056
+ if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
1057
+ if (last_type !== 'TK_END_BLOCK' || opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") {
1058
+ print_newline();
1059
+ } else {
1060
+ trim_output(true);
1061
+ print_single_space();
1062
+ }
1063
+ } else if (prefix === 'NEWLINE') {
1064
+ if (is_special_word(last_text)) {
1065
+ // no newline between 'return nnn'
1066
+ print_single_space();
1067
+ } else if (last_type !== 'TK_END_EXPR') {
1068
+ if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
1069
+ // no need to force newline on 'var': for (var x = 0...)
1070
+ if (token_text === 'if' && last_word === 'else' && last_text !== '{') {
1071
+ // no newline for } else if {
1072
+ print_single_space();
1073
+ } else {
1074
+ flags.var_line = false;
1075
+ flags.var_line_reindented = false;
1076
+ print_newline();
1077
+ }
1078
+ }
1079
+ } else if (in_array(token_text, line_starters) && last_text !== ')') {
1080
+ flags.var_line = false;
1081
+ flags.var_line_reindented = false;
1082
+ print_newline();
1083
+ }
1084
+ } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') {
1085
+ print_newline(); // }, in lists get a newline treatment
1086
+ } else if (prefix === 'SPACE') {
1087
+ print_single_space();
1088
+ }
1089
+ print_token();
1090
+ last_word = token_text;
1091
+
1092
+ if (token_text === 'var') {
1093
+ flags.var_line = true;
1094
+ flags.var_line_reindented = false;
1095
+ flags.var_line_tainted = false;
1096
+ }
1097
+
1098
+ if (token_text === 'if') {
1099
+ flags.if_line = true;
1100
+ }
1101
+ if (token_text === 'else') {
1102
+ flags.if_line = false;
1103
+ }
1104
+
1105
+ break;
1106
+
1107
+ case 'TK_SEMICOLON':
1108
+
1109
+ print_token();
1110
+ flags.var_line = false;
1111
+ flags.var_line_reindented = false;
1112
+ if (flags.mode === 'OBJECT') {
1113
+ // OBJECT mode is weird and doesn't get reset too well.
1114
+ flags.mode = 'BLOCK';
1115
+ }
1116
+ break;
1117
+
1118
+ case 'TK_STRING':
1119
+
1120
+ if (last_type === 'TK_END_EXPR' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) {
1121
+ print_single_space();
1122
+ } else if (last_type === 'TK_WORD') {
1123
+ print_single_space();
1124
+ } else if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') {
1125
+ if (opt_preserve_newlines && wanted_newline && flags.mode !== 'OBJECT') {
1126
+ print_newline();
1127
+ output.push(indent_string);
1128
+ }
1129
+ } else {
1130
+ print_newline();
1131
+ }
1132
+ print_token();
1133
+ break;
1134
+
1135
+ case 'TK_EQUALS':
1136
+ if (flags.var_line) {
1137
+ // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
1138
+ flags.var_line_tainted = true;
1139
+ }
1140
+ print_single_space();
1141
+ print_token();
1142
+ print_single_space();
1143
+ break;
1144
+
1145
+ case 'TK_COMMA':
1146
+ if (flags.var_line) {
1147
+ if (is_expression(flags.mode) || last_type === 'TK_END_BLOCK' ) {
1148
+ // do not break on comma, for(var a = 1, b = 2)
1149
+ flags.var_line_tainted = false;
1150
+ }
1151
+ if (flags.var_line_tainted) {
1152
+ print_token();
1153
+ flags.var_line_reindented = true;
1154
+ flags.var_line_tainted = false;
1155
+ print_newline();
1156
+ break;
1157
+ } else {
1158
+ flags.var_line_tainted = false;
1159
+ }
1160
+
1161
+ print_token();
1162
+ print_single_space();
1163
+ break;
1164
+ }
1165
+
1166
+ if (last_type === 'TK_COMMENT') {
1167
+ print_newline();
1168
+ }
1169
+
1170
+ if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
1171
+ print_token();
1172
+ if (flags.mode === 'OBJECT' && last_text === '}') {
1173
+ print_newline();
1174
+ } else {
1175
+ print_single_space();
1176
+ }
1177
+ } else {
1178
+ if (flags.mode === 'OBJECT') {
1179
+ print_token();
1180
+ print_newline();
1181
+ } else {
1182
+ // EXPR or DO_BLOCK
1183
+ print_token();
1184
+ print_single_space();
1185
+ }
1186
+ }
1187
+ break;
1188
+
1189
+
1190
+ case 'TK_OPERATOR':
1191
+
1192
+ var space_before = true;
1193
+ var space_after = true;
1194
+ if (is_special_word(last_text)) {
1195
+ // "return" had a special handling in TK_WORD. Now we need to return the favor
1196
+ print_single_space();
1197
+ print_token();
1198
+ break;
1199
+ }
1200
+
1201
+ // hack for actionscript's import .*;
1202
+ if (token_text === '*' && last_type === 'TK_DOT' && !last_last_text.match(/^\d+$/)) {
1203
+ print_token();
1204
+ break;
1205
+ }
1206
+
1207
+ if (token_text === ':' && flags.in_case) {
1208
+ flags.case_body = true;
1209
+ indent();
1210
+ print_token();
1211
+ print_newline();
1212
+ flags.in_case = false;
1213
+ break;
1214
+ }
1215
+
1216
+ if (token_text === '::') {
1217
+ // no spaces around exotic namespacing syntax operator
1218
+ print_token();
1219
+ break;
1220
+ }
1221
+
1222
+ 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) || last_text == ','))) {
1223
+ // unary operators (and binary +/- pretending to be unary) special cases
1224
+
1225
+ space_before = false;
1226
+ space_after = false;
1227
+
1228
+ if (last_text === ';' && is_expression(flags.mode)) {
1229
+ // for (;; ++i)
1230
+ // ^^^
1231
+ space_before = true;
1232
+ }
1233
+ if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) {
1234
+ space_before = true;
1235
+ }
1236
+
1237
+ if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) {
1238
+ // { foo; --i }
1239
+ // foo(); --bar;
1240
+ print_newline();
1241
+ }
1242
+ } else if (token_text === ':') {
1243
+ if (flags.ternary_depth === 0) {
1244
+ if (flags.mode === 'BLOCK') {
1245
+ flags.mode = 'OBJECT';
1246
+ }
1247
+ space_before = false;
1248
+ } else {
1249
+ flags.ternary_depth -= 1;
1250
+ }
1251
+ } else if (token_text === '?') {
1252
+ flags.ternary_depth += 1;
1253
+ }
1254
+ if (space_before) {
1255
+ print_single_space();
1256
+ }
1257
+
1258
+ print_token();
1259
+
1260
+ if (space_after) {
1261
+ print_single_space();
1262
+ }
1263
+
1264
+ break;
1265
+
1266
+ case 'TK_BLOCK_COMMENT':
1267
+
1268
+ var lines = split_newlines(token_text);
1269
+ var j; // iterator for this case
1270
+
1271
+ if (all_lines_start_with(lines.slice(1), '*')) {
1272
+ // javadoc: reformat and reindent
1273
+ print_newline();
1274
+ output.push(lines[0]);
1275
+ for (j = 1; j < lines.length; j++) {
1276
+ print_newline();
1277
+ output.push(' ');
1278
+ output.push(trim(lines[j]));
1279
+ }
1280
+
1281
+ } else {
1282
+
1283
+ // simple block comment: leave intact
1284
+ if (lines.length > 1) {
1285
+ // multiline comment block starts with a new line
1286
+ print_newline();
1287
+ } else {
1288
+ // single-line /* comment */ stays where it is
1289
+ if (last_type === 'TK_END_BLOCK') {
1290
+ print_newline();
1291
+ } else {
1292
+ print_single_space();
1293
+ }
1294
+
1295
+ }
1296
+
1297
+ for (j = 0; j < lines.length; j++) {
1298
+ output.push(lines[j]);
1299
+ output.push("\n");
1300
+ }
1301
+
1302
+ }
1303
+ if (look_up('\n') !== '\n') {
1304
+ print_newline();
1305
+ }
1306
+ break;
1307
+
1308
+ case 'TK_INLINE_COMMENT':
1309
+ print_single_space();
1310
+ print_token();
1311
+ if (is_expression(flags.mode)) {
1312
+ print_single_space();
1313
+ } else {
1314
+ force_newline();
1315
+ }
1316
+ break;
1317
+
1318
+ case 'TK_COMMENT':
1319
+
1320
+ if (last_text === ',' && !wanted_newline) {
1321
+ trim_output(true);
1322
+ }
1323
+ if (last_type !== 'TK_COMMENT') {
1324
+ if (wanted_newline) {
1325
+ print_newline();
1326
+ } else {
1327
+ print_single_space();
1328
+ }
1329
+ }
1330
+ print_token();
1331
+ print_newline();
1332
+ break;
1333
+
1334
+ case 'TK_UNKNOWN':
1335
+ print_token();
1336
+ break;
1337
+ }
1338
+
1339
+ last_last_text = last_text;
1340
+ last_type = token_type;
1341
+ last_text = token_text;
1342
+ }
1343
+
1344
+ var sweet_code = preindent_string + output.join('').replace(/[\r\n ]+$/, '');
1345
+ return sweet_code;
1346
+
1347
+ }
1348
+
1349
+ // Add support for CommonJS. Just put this file somewhere on your require.paths
1350
+ // and you will be able to `var js_beautify = require("beautify").js_beautify`.
1351
+ if (typeof exports !== "undefined") {
1352
+ exports.js_beautify = js_beautify;
1353
+ }