wysiwyg-rails 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -1
  3. data/app/assets/javascripts/froala_editor.min.js +5 -5
  4. data/app/assets/javascripts/froala_editor_ie8.min.js +5 -0
  5. data/app/assets/javascripts/langs/ar.js +88 -0
  6. data/app/assets/javascripts/langs/cs.js +88 -0
  7. data/app/assets/javascripts/langs/da.js +88 -0
  8. data/app/assets/javascripts/langs/de.js +88 -0
  9. data/app/assets/javascripts/langs/en_ca.js +88 -0
  10. data/app/assets/javascripts/langs/en_gb.js +88 -0
  11. data/app/assets/javascripts/langs/en_us.js +88 -0
  12. data/app/assets/javascripts/langs/es.js +88 -0
  13. data/app/assets/javascripts/langs/fa.js +88 -0
  14. data/app/assets/javascripts/langs/fi.js +88 -0
  15. data/app/assets/javascripts/langs/fr.js +88 -0
  16. data/app/assets/javascripts/langs/hu.js +88 -0
  17. data/app/assets/javascripts/langs/id.js +88 -0
  18. data/app/assets/javascripts/langs/it.js +88 -0
  19. data/app/assets/javascripts/langs/ja.js +88 -0
  20. data/app/assets/javascripts/langs/ko.js +88 -0
  21. data/app/assets/javascripts/langs/nb.js +88 -0
  22. data/app/assets/javascripts/langs/nl.js +88 -0
  23. data/app/assets/javascripts/langs/pl.js +88 -0
  24. data/app/assets/javascripts/langs/pt_br.js +88 -0
  25. data/app/assets/javascripts/langs/pt_pt.js +88 -0
  26. data/app/assets/javascripts/langs/ro.js +88 -0
  27. data/app/assets/javascripts/langs/ru.js +88 -0
  28. data/app/assets/javascripts/langs/sv.js +88 -0
  29. data/app/assets/javascripts/langs/th.js +88 -0
  30. data/app/assets/javascripts/langs/tr.js +88 -0
  31. data/app/assets/javascripts/langs/zh_cn.js +88 -0
  32. data/app/assets/javascripts/langs/zh_tw.js +88 -0
  33. data/app/assets/javascripts/libs/beautify/beautify-css.js +384 -0
  34. data/app/assets/javascripts/libs/beautify/beautify-html.js +840 -0
  35. data/app/assets/javascripts/libs/beautify/beautify.js +1737 -0
  36. data/app/assets/javascripts/plugins/block_styles.min.js +5 -0
  37. data/app/assets/javascripts/plugins/colors.min.js +5 -0
  38. data/app/assets/javascripts/plugins/fonts/font_family.min.js +5 -0
  39. data/app/assets/javascripts/plugins/fonts/font_size.min.js +5 -0
  40. data/app/assets/javascripts/plugins/fonts/fonts.min.js +5 -0
  41. data/app/assets/javascripts/plugins/media_manager.min.js +5 -0
  42. data/app/assets/javascripts/plugins/tables.min.js +5 -0
  43. data/app/assets/javascripts/plugins/video.min.js +5 -0
  44. data/app/assets/stylesheets/froala_editor.min.css +2 -2
  45. data/app/assets/stylesheets/themes/dark.min.css +4 -0
  46. data/lib/wysiwyg-rails/version.rb +1 -1
  47. metadata +46 -5
@@ -0,0 +1,840 @@
1
+ /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
2
+ /*
3
+
4
+ The MIT License (MIT)
5
+
6
+ Copyright (c) 2007-2013 Einar Lielmanis and contributors.
7
+
8
+ Permission is hereby granted, free of charge, to any person
9
+ obtaining a copy of this software and associated documentation files
10
+ (the "Software"), to deal in the Software without restriction,
11
+ including without limitation the rights to use, copy, modify, merge,
12
+ publish, distribute, sublicense, and/or sell copies of the Software,
13
+ and to permit persons to whom the Software is furnished to do so,
14
+ subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+
29
+ Style HTML
30
+ ---------------
31
+
32
+ Written by Nochum Sossonko, (nsossonko@hotmail.com)
33
+
34
+ Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
35
+ http://jsbeautifier.org/
36
+
37
+ Usage:
38
+ style_html(html_source);
39
+
40
+ style_html(html_source, options);
41
+
42
+ The options are:
43
+ indent_inner_html (default false) — indent <head> and <body> sections,
44
+ indent_size (default 4) — indentation size,
45
+ indent_char (default space) — character to indent with,
46
+ wrap_line_length (default 250) - maximum amount of characters per line (0 = disable)
47
+ brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
48
+ 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.
49
+ unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
50
+ indent_scripts (default normal) - "keep"|"separate"|"normal"
51
+ preserve_newlines (default true) - whether existing line breaks before elements should be preserved
52
+ Only works before elements, not inside tags or for text.
53
+ max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk
54
+ indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}}
55
+
56
+ e.g.
57
+
58
+ style_html(html_source, {
59
+ 'indent_inner_html': false,
60
+ 'indent_size': 2,
61
+ 'indent_char': ' ',
62
+ 'wrap_line_length': 78,
63
+ 'brace_style': 'expand',
64
+ 'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u'],
65
+ 'preserve_newlines': true,
66
+ 'max_preserve_newlines': 5,
67
+ 'indent_handlebars': false
68
+ });
69
+ */
70
+
71
+ (function() {
72
+
73
+ function trim(s) {
74
+ return s.replace(/^\s+|\s+$/g, '');
75
+ }
76
+
77
+ function ltrim(s) {
78
+ return s.replace(/^\s+/g, '');
79
+ }
80
+
81
+ function style_html(html_source, options, js_beautify, css_beautify) {
82
+ //Wrapper function to invoke all the necessary constructors and deal with the output.
83
+
84
+ var multi_parser,
85
+ indent_inner_html,
86
+ indent_size,
87
+ indent_character,
88
+ wrap_line_length,
89
+ brace_style,
90
+ unformatted,
91
+ preserve_newlines,
92
+ max_preserve_newlines,
93
+ indent_handlebars;
94
+
95
+ options = options || {};
96
+
97
+ // backwards compatibility to 1.3.4
98
+ if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) &&
99
+ (options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) {
100
+ options.wrap_line_length = options.max_char;
101
+ }
102
+
103
+ indent_inner_html = options.indent_inner_html || true;
104
+ indent_size = parseInt(options.indent_size || 1, 10);
105
+ indent_character = options.indent_char || '\t';
106
+ brace_style = options.brace_style || 'collapse';
107
+ wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 100000, 10);
108
+ unformatted = options.unformatted || ['a', 'span', 'bdo', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'q', 'sub', 'sup', 'tt', 'i', 'b', 'big', 'small', 'u', 's', 'strike', 'font', 'ins', 'del', 'pre', 'address', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
109
+ preserve_newlines = options.preserve_newlines || true;
110
+ max_preserve_newlines = preserve_newlines ? parseInt(options.max_preserve_newlines || 32786, 10) : 0;
111
+ indent_handlebars = options.indent_handlebars || true;
112
+
113
+ function Parser() {
114
+
115
+ this.pos = 0; //Parser position
116
+ this.token = '';
117
+ this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT
118
+ this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values
119
+ parent: 'parent1',
120
+ parentcount: 1,
121
+ parent1: ''
122
+ };
123
+ this.tag_type = '';
124
+ this.token_text = this.last_token = this.last_text = this.token_type = '';
125
+ this.newlines = 0;
126
+ this.indent_content = indent_inner_html;
127
+
128
+ this.Utils = { //Uilities made available to the various functions
129
+ whitespace: "\n\r\t ".split(''),
130
+ single_token: 'br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed,?php,?,?='.split(','), //all the single tags for HTML
131
+ extra_liners: 'head,body,/html'.split(','), //for tags that need a line of whitespace before them
132
+ in_array: function(what, arr) {
133
+ for (var i = 0; i < arr.length; i++) {
134
+ if (what === arr[i]) {
135
+ return true;
136
+ }
137
+ }
138
+ return false;
139
+ }
140
+ };
141
+
142
+ this.traverse_whitespace = function() {
143
+ var input_char = '';
144
+
145
+ input_char = this.input.charAt(this.pos);
146
+ if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
147
+ this.newlines = 0;
148
+ while (this.Utils.in_array(input_char, this.Utils.whitespace)) {
149
+ if (preserve_newlines && input_char === '\n' && this.newlines <= max_preserve_newlines) {
150
+ this.newlines += 1;
151
+ }
152
+
153
+ this.pos++;
154
+ input_char = this.input.charAt(this.pos);
155
+ }
156
+ return true;
157
+ }
158
+ return false;
159
+ };
160
+
161
+ this.get_content = function() { //function to capture regular content between tags
162
+
163
+ var input_char = '',
164
+ content = [],
165
+ space = false; //if a space is needed
166
+
167
+ while (this.input.charAt(this.pos) !== '<') {
168
+ if (this.pos >= this.input.length) {
169
+ return content.length ? content.join('') : ['', 'TK_EOF'];
170
+ }
171
+
172
+ if (this.traverse_whitespace()) {
173
+ if (content.length) {
174
+ space = true;
175
+ }
176
+ continue; //don't want to insert unnecessary space
177
+ }
178
+
179
+ if (indent_handlebars) {
180
+ // Handlebars parsing is complicated.
181
+ // {{#foo}} and {{/foo}} are formatted tags.
182
+ // {{something}} should get treated as content, except:
183
+ // {{else}} specifically behaves like {{#if}} and {{/if}}
184
+ var peek3 = this.input.substr(this.pos, 3);
185
+ if (peek3 === '{{#' || peek3 === '{{/') {
186
+ // These are tags and not content.
187
+ break;
188
+ } else if (this.input.substr(this.pos, 2) === '{{') {
189
+ if (this.get_tag(true) === '{{else}}') {
190
+ break;
191
+ }
192
+ }
193
+ }
194
+
195
+ input_char = this.input.charAt(this.pos);
196
+ this.pos++;
197
+
198
+ if (space) {
199
+ if (this.line_char_count >= this.wrap_line_length) { //insert a line when the wrap_line_length is reached
200
+ this.print_newline(false, content);
201
+ this.print_indentation(content);
202
+ } else {
203
+ this.line_char_count++;
204
+ content.push(' ');
205
+ }
206
+ space = false;
207
+ }
208
+ this.line_char_count++;
209
+ content.push(input_char); //letter at-a-time (or string) inserted to an array
210
+ }
211
+ return content.length ? content.join('') : '';
212
+ };
213
+
214
+ this.get_contents_to = function(name) { //get the full content of a script or style to pass to js_beautify
215
+ if (this.pos === this.input.length) {
216
+ return ['', 'TK_EOF'];
217
+ }
218
+ var input_char = '';
219
+ var content = '';
220
+ var reg_match = new RegExp('</' + name + '\\s*>', 'igm');
221
+ reg_match.lastIndex = this.pos;
222
+ var reg_array = reg_match.exec(this.input);
223
+ var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
224
+ if (this.pos < end_script) { //get everything in between the script tags
225
+ content = this.input.substring(this.pos, end_script);
226
+ this.pos = end_script;
227
+ }
228
+ return content;
229
+ };
230
+
231
+ this.record_tag = function(tag) { //function to record a tag and its parent in this.tags Object
232
+ if (this.tags[tag + 'count']) { //check for the existence of this tag type
233
+ this.tags[tag + 'count']++;
234
+ this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
235
+ } else { //otherwise initialize this tag type
236
+ this.tags[tag + 'count'] = 1;
237
+ this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
238
+ }
239
+ this.tags[tag + this.tags[tag + 'count'] + 'parent'] = this.tags.parent; //set the parent (i.e. in the case of a div this.tags.div1parent)
240
+ this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1')
241
+ };
242
+
243
+ this.retrieve_tag = function(tag) { //function to retrieve the opening tag to the corresponding closer
244
+ if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it
245
+ var temp_parent = this.tags.parent; //check to see if it's a closable tag.
246
+ while (temp_parent) { //till we reach '' (the initial value);
247
+ if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it
248
+ break;
249
+ }
250
+ temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree
251
+ }
252
+ if (temp_parent) { //if we caught something
253
+ this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly
254
+ this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent
255
+ }
256
+ delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
257
+ delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
258
+ if (this.tags[tag + 'count'] === 1) {
259
+ delete this.tags[tag + 'count'];
260
+ } else {
261
+ this.tags[tag + 'count']--;
262
+ }
263
+ }
264
+ };
265
+
266
+ this.indent_to_tag = function(tag) {
267
+ // Match the indentation level to the last use of this tag, but don't remove it.
268
+ if (!this.tags[tag + 'count']) {
269
+ return;
270
+ }
271
+ var temp_parent = this.tags.parent;
272
+ while (temp_parent) {
273
+ if (tag + this.tags[tag + 'count'] === temp_parent) {
274
+ break;
275
+ }
276
+ temp_parent = this.tags[temp_parent + 'parent'];
277
+ }
278
+ if (temp_parent) {
279
+ this.indent_level = this.tags[tag + this.tags[tag + 'count']];
280
+ }
281
+ };
282
+
283
+ this.get_tag = function(peek) { //function to get a full tag and parse its type
284
+ var input_char = '',
285
+ content = [],
286
+ comment = '',
287
+ space = false,
288
+ tag_start, tag_end,
289
+ tag_start_char,
290
+ orig_pos = this.pos,
291
+ orig_line_char_count = this.line_char_count;
292
+
293
+ peek = peek !== undefined ? peek : false;
294
+
295
+ do {
296
+ if (this.pos >= this.input.length) {
297
+ if (peek) {
298
+ this.pos = orig_pos;
299
+ this.line_char_count = orig_line_char_count;
300
+ }
301
+ return content.length ? content.join('') : ['', 'TK_EOF'];
302
+ }
303
+
304
+ input_char = this.input.charAt(this.pos);
305
+ this.pos++;
306
+
307
+ if (this.Utils.in_array(input_char, this.Utils.whitespace)) { //don't want to insert unnecessary space
308
+ space = true;
309
+ continue;
310
+ }
311
+
312
+ if (input_char === "'" || input_char === '"') {
313
+ input_char += this.get_unformatted(input_char);
314
+ space = true;
315
+
316
+ }
317
+
318
+ if (input_char === '=') { //no space before =
319
+ space = false;
320
+ }
321
+
322
+ if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) {
323
+ //no space after = or before >
324
+ if (this.line_char_count >= this.wrap_line_length) {
325
+ this.print_newline(false, content);
326
+ this.print_indentation(content);
327
+ } else {
328
+ content.push(' ');
329
+ this.line_char_count++;
330
+ }
331
+ space = false;
332
+ }
333
+
334
+ if (indent_handlebars && tag_start_char === '<') {
335
+ // When inside an angle-bracket tag, put spaces around
336
+ // handlebars not inside of strings.
337
+ if ((input_char + this.input.charAt(this.pos)) === '{{') {
338
+ input_char += this.get_unformatted('}}');
339
+ if (content.length && content[content.length - 1] !== ' ' && content[content.length - 1] !== '<') {
340
+ input_char = ' ' + input_char;
341
+ }
342
+ space = true;
343
+ }
344
+ }
345
+
346
+ if (input_char === '<' && !tag_start_char) {
347
+ tag_start = this.pos - 1;
348
+ tag_start_char = '<';
349
+ }
350
+
351
+ if (indent_handlebars && !tag_start_char) {
352
+ if (content.length >= 2 && content[content.length - 1] === '{' && content[content.length - 2] == '{') {
353
+ if (input_char === '#' || input_char === '/') {
354
+ tag_start = this.pos - 3;
355
+ } else {
356
+ tag_start = this.pos - 2;
357
+ }
358
+ tag_start_char = '{';
359
+ }
360
+ }
361
+
362
+ this.line_char_count++;
363
+ content.push(input_char); //inserts character at-a-time (or string)
364
+
365
+ if (content[1] && content[1] === '!') { //if we're in a comment, do something special
366
+ // We treat all comments as literals, even more than preformatted tags
367
+ // we just look for the appropriate close tag
368
+ content = [this.get_comment(tag_start)];
369
+ break;
370
+ }
371
+
372
+ if (indent_handlebars && tag_start_char === '{' && content.length > 2 && content[content.length - 2] === '}' && content[content.length - 1] === '}') {
373
+ break;
374
+ }
375
+ } while (input_char !== '>');
376
+
377
+ var tag_complete = content.join('');
378
+ var tag_index;
379
+ var tag_offset;
380
+
381
+ if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends
382
+ tag_index = tag_complete.indexOf(' ');
383
+ } else if (tag_complete[0] === '{') {
384
+ tag_index = tag_complete.indexOf('}');
385
+ } else { //otherwise go with the tag ending
386
+ tag_index = tag_complete.indexOf('>');
387
+ }
388
+ if (tag_complete[0] === '<' || !indent_handlebars) {
389
+ tag_offset = 1;
390
+ } else {
391
+ tag_offset = tag_complete[2] === '#' ? 3 : 2;
392
+ }
393
+ var tag_check = tag_complete.substring(tag_offset, tag_index).toLowerCase();
394
+ if (tag_complete.charAt(tag_complete.length - 2) === '/' ||
395
+ this.Utils.in_array(tag_check, this.Utils.single_token)) { //if this tag name is a single tag type (either in the list or has a closing /)
396
+ if (!peek) {
397
+ this.tag_type = 'SINGLE';
398
+ }
399
+ } else if (indent_handlebars && tag_complete[0] === '{' && tag_check === 'else') {
400
+ if (!peek) {
401
+ this.indent_to_tag('if');
402
+ this.tag_type = 'HANDLEBARS_ELSE';
403
+ this.indent_content = true;
404
+ this.traverse_whitespace();
405
+ }
406
+ } else if (tag_check === 'script') { //for later script handling
407
+ if (!peek) {
408
+ this.record_tag(tag_check);
409
+ this.tag_type = 'SCRIPT';
410
+ }
411
+ } else if (tag_check === 'style') { //for future style handling (for now it justs uses get_content)
412
+ if (!peek) {
413
+ this.record_tag(tag_check);
414
+ this.tag_type = 'STYLE';
415
+ }
416
+ } else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags
417
+ comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function
418
+ content.push(comment);
419
+ // Preserve collapsed whitespace either before or after this tag.
420
+ if (tag_start > 0 && this.Utils.in_array(this.input.charAt(tag_start - 1), this.Utils.whitespace)) {
421
+ content.splice(0, 0, this.input.charAt(tag_start - 1));
422
+ }
423
+ tag_end = this.pos - 1;
424
+ if (this.Utils.in_array(this.input.charAt(tag_end + 1), this.Utils.whitespace)) {
425
+ content.push(this.input.charAt(tag_end + 1));
426
+ }
427
+ this.tag_type = 'SINGLE';
428
+ } else if (tag_check.charAt(0) === '!') { //peek for <! comment
429
+ // for comments content is already correct.
430
+ if (!peek) {
431
+ this.tag_type = 'SINGLE';
432
+ this.traverse_whitespace();
433
+ }
434
+ } else if (!peek) {
435
+ if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
436
+ this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors
437
+ this.tag_type = 'END';
438
+ this.traverse_whitespace();
439
+ } else { //otherwise it's a start-tag
440
+ this.record_tag(tag_check); //push it on the tag stack
441
+ if (tag_check.toLowerCase() !== 'html') {
442
+ this.indent_content = true;
443
+ }
444
+ this.tag_type = 'START';
445
+
446
+ // Allow preserving of newlines after a start tag
447
+ this.traverse_whitespace();
448
+ }
449
+ if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
450
+ this.print_newline(false, this.output);
451
+ if (this.output.length && this.output[this.output.length - 2] !== '\n') {
452
+ this.print_newline(true, this.output);
453
+ }
454
+ }
455
+ }
456
+
457
+ if (peek) {
458
+ this.pos = orig_pos;
459
+ this.line_char_count = orig_line_char_count;
460
+ }
461
+
462
+ return content.join(''); //returns fully formatted tag
463
+ };
464
+
465
+ this.get_comment = function(start_pos) { //function to return comment content in its entirety
466
+ // this is will have very poor perf, but will work for now.
467
+ var comment = '',
468
+ delimiter = '>',
469
+ matched = false;
470
+
471
+ this.pos = start_pos;
472
+ input_char = this.input.charAt(this.pos);
473
+ this.pos++;
474
+
475
+ while (this.pos <= this.input.length) {
476
+ comment += input_char;
477
+
478
+ // only need to check for the delimiter if the last chars match
479
+ if (comment[comment.length - 1] === delimiter[delimiter.length - 1] &&
480
+ comment.indexOf(delimiter) !== -1) {
481
+ break;
482
+ }
483
+
484
+ // only need to search for custom delimiter for the first few characters
485
+ if (!matched && comment.length < 10) {
486
+ if (comment.indexOf('<![if') === 0) { //peek for <![if conditional comment
487
+ delimiter = '<![endif]>';
488
+ matched = true;
489
+ } else if (comment.indexOf('<![cdata[') === 0) { //if it's a <[cdata[ comment...
490
+ delimiter = ']]>';
491
+ matched = true;
492
+ } else if (comment.indexOf('<![') === 0) { // some other ![ comment? ...
493
+ delimiter = ']>';
494
+ matched = true;
495
+ } else if (comment.indexOf('<!--') === 0) { // <!-- comment ...
496
+ delimiter = '-->';
497
+ matched = true;
498
+ }
499
+ }
500
+
501
+ input_char = this.input.charAt(this.pos);
502
+ this.pos++;
503
+ }
504
+
505
+ return comment;
506
+ };
507
+
508
+ this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety
509
+
510
+ if (orig_tag && orig_tag.toLowerCase().indexOf(delimiter) !== -1) {
511
+ return '';
512
+ }
513
+ var input_char = '';
514
+ var content = '';
515
+ var min_index = 0;
516
+ var space = true;
517
+ do {
518
+
519
+ if (this.pos >= this.input.length) {
520
+ return content;
521
+ }
522
+
523
+ input_char = this.input.charAt(this.pos);
524
+ this.pos++;
525
+
526
+ if (this.Utils.in_array(input_char, this.Utils.whitespace)) {
527
+ if (!space) {
528
+ this.line_char_count--;
529
+ continue;
530
+ }
531
+ if (input_char === '\n' || input_char === '\r') {
532
+ content += '\n';
533
+ /* Don't change tab indention for unformatted blocks. If using code for html editing, this will greatly affect <pre> tags if they are specified in the 'unformatted array'
534
+ for (var i=0; i<this.indent_level; i++) {
535
+ content += this.indent_string;
536
+ }
537
+ space = false; //...and make sure other indentation is erased
538
+ */
539
+ this.line_char_count = 0;
540
+ continue;
541
+ }
542
+ }
543
+ content += input_char;
544
+ this.line_char_count++;
545
+ space = true;
546
+
547
+ if (indent_handlebars && input_char === '{' && content.length && content[content.length - 2] === '{') {
548
+ // Handlebars expressions in strings should also be unformatted.
549
+ content += this.get_unformatted('}}');
550
+ // These expressions are opaque. Ignore delimiters found in them.
551
+ min_index = content.length;
552
+ }
553
+ } while (content.toLowerCase().indexOf(delimiter, min_index) === -1);
554
+ return content;
555
+ };
556
+
557
+ this.get_token = function() { //initial handler for token-retrieval
558
+ var token;
559
+
560
+ if (this.last_token === 'TK_TAG_SCRIPT' || this.last_token === 'TK_TAG_STYLE') { //check if we need to format javascript
561
+ var type = this.last_token.substr(7);
562
+ token = this.get_contents_to(type);
563
+ if (typeof token !== 'string') {
564
+ return token;
565
+ }
566
+ return [token, 'TK_' + type];
567
+ }
568
+ if (this.current_mode === 'CONTENT') {
569
+ token = this.get_content();
570
+ if (typeof token !== 'string') {
571
+ return token;
572
+ } else {
573
+ return [token, 'TK_CONTENT'];
574
+ }
575
+ }
576
+
577
+ if (this.current_mode === 'TAG') {
578
+ token = this.get_tag();
579
+ if (typeof token !== 'string') {
580
+ return token;
581
+ } else {
582
+ var tag_name_type = 'TK_TAG_' + this.tag_type;
583
+ return [token, tag_name_type];
584
+ }
585
+ }
586
+ };
587
+
588
+ this.get_full_indent = function(level) {
589
+ level = this.indent_level + level || 0;
590
+ if (level < 1) {
591
+ return '';
592
+ }
593
+
594
+ return Array(level + 1).join(this.indent_string);
595
+ };
596
+
597
+ this.is_unformatted = function(tag_check, unformatted) {
598
+ //is this an HTML5 block-level link?
599
+ if (!this.Utils.in_array(tag_check, unformatted)) {
600
+ return false;
601
+ }
602
+
603
+ if (tag_check.toLowerCase() !== 'a' || !this.Utils.in_array('a', unformatted)) {
604
+ return true;
605
+ }
606
+
607
+ //at this point we have an tag; is its first child something we want to remain
608
+ //unformatted?
609
+ var next_tag = this.get_tag(true /* peek. */ );
610
+
611
+ // test next_tag to see if it is just html tag (no external content)
612
+ var tag = (next_tag || "").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/);
613
+
614
+ // if next_tag comes back but is not an isolated tag, then
615
+ // let's treat the 'a' tag as having content
616
+ // and respect the unformatted option
617
+ if (!tag || this.Utils.in_array(tag, unformatted)) {
618
+ return true;
619
+ } else {
620
+ return false;
621
+ }
622
+ };
623
+
624
+ this.printer = function(js_source, indent_character, indent_size, wrap_line_length, brace_style) { //handles input/output and some other printing functions
625
+
626
+ this.input = js_source || ''; //gets the input for the Parser
627
+ this.output = [];
628
+ this.indent_character = indent_character;
629
+ this.indent_string = '';
630
+ this.indent_size = indent_size;
631
+ this.brace_style = brace_style;
632
+ this.indent_level = 0;
633
+ this.wrap_line_length = wrap_line_length;
634
+ this.line_char_count = 0; //count to see if wrap_line_length was exceeded
635
+
636
+ for (var i = 0; i < this.indent_size; i++) {
637
+ this.indent_string += this.indent_character;
638
+ }
639
+
640
+ this.print_newline = function(force, arr) {
641
+ this.line_char_count = 0;
642
+ if (!arr || !arr.length) {
643
+ return;
644
+ }
645
+ if (force || (arr[arr.length - 1] !== '\n')) { //we might want the extra line
646
+ arr.push('\n');
647
+ }
648
+ };
649
+
650
+ this.print_indentation = function(arr) {
651
+ for (var i = 0; i < this.indent_level; i++) {
652
+ arr.push(this.indent_string);
653
+ this.line_char_count += this.indent_string.length;
654
+ }
655
+ };
656
+
657
+ this.print_token = function(text) {
658
+ if (text || text !== '') {
659
+ if (this.output.length && this.output[this.output.length - 1] === '\n') {
660
+ this.print_indentation(this.output);
661
+ text = ltrim(text);
662
+ }
663
+ }
664
+ this.print_token_raw(text);
665
+ };
666
+
667
+ this.print_token_raw = function(text) {
668
+ if (text && text !== '') {
669
+ if (text.length > 1 && text[text.length - 1] === '\n') {
670
+ // unformatted tags can grab newlines as their last character
671
+ this.output.push(text.slice(0, -1));
672
+ this.print_newline(false, this.output);
673
+ } else {
674
+ this.output.push(text);
675
+ }
676
+ }
677
+
678
+ for (var n = 0; n < this.newlines; n++) {
679
+ this.print_newline(n > 0, this.output);
680
+ }
681
+ this.newlines = 0;
682
+ };
683
+
684
+ this.indent = function() {
685
+ this.indent_level++;
686
+ };
687
+
688
+ this.unindent = function() {
689
+ if (this.indent_level > 0) {
690
+ this.indent_level--;
691
+ }
692
+ };
693
+ };
694
+ return this;
695
+ }
696
+
697
+ /*_____________________--------------------_____________________*/
698
+
699
+ multi_parser = new Parser(); //wrapping functions Parser
700
+ multi_parser.printer(html_source, indent_character, indent_size, wrap_line_length, brace_style); //initialize starting values
701
+
702
+ while (true) {
703
+ var t = multi_parser.get_token();
704
+ multi_parser.token_text = t[0];
705
+ multi_parser.token_type = t[1];
706
+
707
+ if (multi_parser.token_type === 'TK_EOF') {
708
+ break;
709
+ }
710
+
711
+ switch (multi_parser.token_type) {
712
+ case 'TK_TAG_START':
713
+ multi_parser.print_newline(false, multi_parser.output);
714
+ multi_parser.print_token(multi_parser.token_text);
715
+ if (multi_parser.indent_content) {
716
+ multi_parser.indent();
717
+ multi_parser.indent_content = false;
718
+ }
719
+ multi_parser.current_mode = 'CONTENT';
720
+ break;
721
+ case 'TK_TAG_STYLE':
722
+ case 'TK_TAG_SCRIPT':
723
+ multi_parser.print_newline(false, multi_parser.output);
724
+ multi_parser.print_token(multi_parser.token_text);
725
+ multi_parser.current_mode = 'CONTENT';
726
+ break;
727
+ case 'TK_TAG_END':
728
+ //Print new line only if the tag has no content and has child
729
+ if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') {
730
+ var tag_name = multi_parser.token_text.match(/\w+/)[0];
731
+ var tag_extracted_from_last_output = null;
732
+ if (multi_parser.output.length) {
733
+ tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length - 1].match(/(?:<|{{#)\s*(\w+)/);
734
+ }
735
+ if (tag_extracted_from_last_output === null ||
736
+ tag_extracted_from_last_output[1] !== tag_name) {
737
+ multi_parser.print_newline(false, multi_parser.output);
738
+ }
739
+ }
740
+ multi_parser.print_token(multi_parser.token_text);
741
+ multi_parser.current_mode = 'CONTENT';
742
+ break;
743
+ case 'TK_TAG_SINGLE':
744
+ // Don't add a newline before elements that should remain unformatted.
745
+ var tag_check = multi_parser.token_text.match(/^\s*<([a-z]+)/i);
746
+ if (!tag_check || !multi_parser.Utils.in_array(tag_check[1], unformatted)) {
747
+ multi_parser.print_newline(false, multi_parser.output);
748
+ }
749
+ multi_parser.print_token(multi_parser.token_text);
750
+ multi_parser.current_mode = 'CONTENT';
751
+ break;
752
+ case 'TK_TAG_HANDLEBARS_ELSE':
753
+ multi_parser.print_token(multi_parser.token_text);
754
+ if (multi_parser.indent_content) {
755
+ multi_parser.indent();
756
+ multi_parser.indent_content = false;
757
+ }
758
+ multi_parser.current_mode = 'CONTENT';
759
+ break;
760
+ case 'TK_CONTENT':
761
+ multi_parser.print_token(multi_parser.token_text);
762
+ multi_parser.current_mode = 'TAG';
763
+ break;
764
+ case 'TK_STYLE':
765
+ case 'TK_SCRIPT':
766
+ if (multi_parser.token_text !== '') {
767
+ multi_parser.print_newline(false, multi_parser.output);
768
+ var text = multi_parser.token_text,
769
+ _beautifier,
770
+ script_indent_level = 1;
771
+ if (multi_parser.token_type === 'TK_SCRIPT') {
772
+ _beautifier = typeof js_beautify === 'function' && js_beautify;
773
+ } else if (multi_parser.token_type === 'TK_STYLE') {
774
+ _beautifier = typeof css_beautify === 'function' && css_beautify;
775
+ }
776
+
777
+ if (options.indent_scripts === "keep") {
778
+ script_indent_level = 0;
779
+ } else if (options.indent_scripts === "separate") {
780
+ script_indent_level = -multi_parser.indent_level;
781
+ }
782
+
783
+ var indentation = multi_parser.get_full_indent(script_indent_level);
784
+ if (_beautifier) {
785
+ // call the Beautifier if avaliable
786
+ text = _beautifier(text.replace(/^\s*/, indentation), options);
787
+ } else {
788
+ // simply indent the string otherwise
789
+ var white = text.match(/^\s*/)[0];
790
+ var _level = white.match(/[^\n\r]*$/)[0].split(multi_parser.indent_string).length - 1;
791
+ var reindent = multi_parser.get_full_indent(script_indent_level - _level);
792
+ text = text.replace(/^\s*/, indentation)
793
+ .replace(/\r\n|\r|\n/g, '\n' + reindent)
794
+ .replace(/\s+$/, '');
795
+ }
796
+ if (text) {
797
+ multi_parser.print_token_raw(indentation + trim(text));
798
+ multi_parser.print_newline(false, multi_parser.output);
799
+ }
800
+ }
801
+ multi_parser.current_mode = 'TAG';
802
+ break;
803
+ }
804
+ multi_parser.last_token = multi_parser.token_type;
805
+ multi_parser.last_text = multi_parser.token_text;
806
+ }
807
+ return multi_parser.output.join('');
808
+ }
809
+
810
+ if (typeof define === "function" && define.amd) {
811
+ // Add support for require.js
812
+ define(["./beautify", "./beautify-css"], function(js_beautify, css_beautify) {
813
+ return {
814
+ html_beautify: function(html_source, options) {
815
+ return style_html(html_source, options, js_beautify, css_beautify);
816
+ }
817
+ };
818
+ });
819
+ } else if (typeof exports !== "undefined") {
820
+ // Add support for CommonJS. Just put this file somewhere on your require.paths
821
+ // and you will be able to `var html_beautify = require("beautify").html_beautify`.
822
+ var js_beautify = require('./beautify.js').js_beautify;
823
+ var css_beautify = require('./beautify-css.js').css_beautify;
824
+
825
+ exports.html_beautify = function(html_source, options) {
826
+ return style_html(html_source, options, js_beautify, css_beautify);
827
+ };
828
+ } else if (typeof window !== "undefined") {
829
+ // If we're running a web page and don't have either of the above, add our one global
830
+ window.html_beautify = function(html_source, options) {
831
+ return style_html(html_source, options, window.js_beautify, window.css_beautify);
832
+ };
833
+ } else if (typeof global !== "undefined") {
834
+ // If we don't even have window, try global.
835
+ global.html_beautify = function(html_source, options) {
836
+ return style_html(html_source, options, global.js_beautify, global.css_beautify);
837
+ };
838
+ }
839
+
840
+ }());