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