mm_matchs 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/htmlformat.js CHANGED
@@ -1,400 +1,396 @@
1
- function style_html(html_source, indent_size, indent_character, max_char) {
2
- //Wrapper function to invoke all the necessary constructors and deal with the output.
3
-
4
- var Parser, multi_parser;
5
-
6
- function Parser() {
7
-
8
- this.pos = 0; //Parser position
9
- this.token = '';
10
- this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT
11
- this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values
12
- parent: 'parent1',
13
- parentcount: 1,
14
- parent1: ''
15
- };
16
- this.tag_type = '';
17
- this.token_text = this.last_token = this.last_text = this.token_type = '';
18
-
19
-
20
- this.Utils = { //Uilities made available to the various functions
21
- whitespace: "\n\r\t ".split(''),
22
- single_token: 'br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed'.split(','), //all the single tags for HTML
23
- extra_liners: 'head,body,/html'.split(','), //for tags that need a line of whitespace before them
24
- in_array: function(what, arr) {
25
- var len = arr.length;
26
- for (var i = 0; i < len; i++) {
27
- if (what === arr[i]) {
28
- return true;
29
- }
30
- }
31
- return false;
32
- }
33
- }
34
-
35
- this.get_content = function() { //function to capture regular content between tags
36
-
37
- var char = '';
38
- var content = [];
39
- var space = false; //if a space is needed
40
- while (this.input.charAt(this.pos) !== '<') {
41
- if (this.pos >= this.input.length) {
42
- return content.length ? content.join('') : ['', 'TK_EOF'];
43
- }
44
-
45
- char = this.input.charAt(this.pos);
46
- this.pos++;
47
- this.line_char_count++;
48
-
49
-
50
- if (this.Utils.in_array(char, this.Utils.whitespace)) {
51
- if (content.length) {
52
- space = true;
53
- }
54
- this.line_char_count--;
55
- continue; //don't want to insert unnecessary space
56
- } else if (space) {
57
- if (this.line_char_count >= this.max_char) { //insert a line when the max_char is reached
58
- content.push('\n');
59
- for (var i = 0; i < this.indent_level; i++) {
60
- content.push(this.indent_string);
61
- }
62
- this.line_char_count = 0;
63
- } else {
64
- content.push(' ');
65
- this.line_char_count++;
66
- }
67
- space = false;
68
- }
69
- content.push(char); //letter at-a-time (or string) inserted to an array
70
- }
71
- return content.length ? content.join('') : '';
72
- }
73
-
74
- this.get_script = function() { //get the full content of a script to pass to js_beautify
75
-
76
- var char = '';
77
- var content = [];
78
- var reg_match = this.regex.scriptEndTag;
79
- reg_match.lastIndex = this.pos;
80
- var reg_array = reg_match.exec(this.input);
81
- var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
82
- while (this.pos < end_script) { //get everything in between the script tags
83
- if (this.pos >= this.input.length) {
84
- return content.length ? content.join('') : ['', 'TK_EOF'];
85
- }
86
-
87
- char = this.input.charAt(this.pos);
88
- this.pos++;
89
-
90
-
91
- content.push(char);
92
- }
93
- return content.length ? content.join('') : ''; //we might not have any content at all
94
- }
95
-
96
- this.record_tag = function(tag) { //function to record a tag and its parent in this.tags Object
97
- if (this.tags[tag + 'count']) { //check for the existence of this tag type
98
- this.tags[tag + 'count']++;
99
- this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
100
- } else { //otherwise initialize this tag type
101
- this.tags[tag + 'count'] = 1;
102
- this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
103
- }
104
- 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)
105
- this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1')
106
- }
107
-
108
- this.retrieve_tag = function(tag) { //function to retrieve the opening tag to the corresponding closer
109
- if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it
110
- var temp_parent = this.tags.parent; //check to see if it's a closable tag.
111
- while (temp_parent) { //till we reach '' (the initial value);
112
- if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it
113
- break;
114
- }
115
- temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree
116
- }
117
- if (temp_parent) { //if we caught something
118
- this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly
119
- this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent
120
- }
121
- delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
122
- delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
123
- if (this.tags[tag + 'count'] == 1) {
124
- delete this.tags[tag + 'count'];
125
- } else {
126
- this.tags[tag + 'count']--;
127
- }
128
- }
129
- }
130
-
131
- this.get_tag = function() { //function to get a full tag and parse its type
132
- var char = '';
133
- var content = [];
134
- var space = false;
135
-
136
- do {
137
- if (this.pos >= this.input.length) {
138
- return content.length ? content.join('') : ['', 'TK_EOF'];
139
- }
140
-
141
- char = this.input.charAt(this.pos);
142
- this.pos++;
143
- this.line_char_count++;
144
-
145
- if (this.Utils.in_array(char, this.Utils.whitespace)) { //don't want to insert unnecessary space
146
- space = true;
147
- this.line_char_count--;
148
- continue;
149
- }
150
-
151
- if (char === "'" || char === '"') {
152
- if (!content[1] || content[1] !== '!') { //if we're in a comment strings don't get treated specially
153
- char += this.get_unformatted(char);
154
- space = true;
155
- }
156
- }
157
-
158
- if (char === '=') { //no space before =
159
- space = false;
160
- }
161
-
162
- if (content.length && content[content.length - 1] !== '=' && char !== '>' &&
1
+ function styleHtml(html_source, indent_size, indent_char, max_char) {
2
+ //Wrapper function to invoke all the necessary constructors and deal with the output.
3
+
4
+ var parser, multi_parser;
5
+
6
+ function parser() {
7
+
8
+ this.pos = 0; //Parser position
9
+ this.token = '';
10
+ this.current_mode = 'CONTENT'; //reflects the current Parser mode: TAG/CONTENT
11
+ this.tags = { //An object to hold tags, their position, and their parent-tags, initiated with default values
12
+ parent: 'parent1',
13
+ parentcount: 1,
14
+ parent1: ''
15
+ };
16
+ this.tag_type = '';
17
+ this.token_text = this.last_token = this.last_text = this.token_type = '';
18
+
19
+
20
+ this.Utils = { //Uilities made available to the various functions
21
+ whitespace: '\n\r\t '.split(''),
22
+ single_token: 'br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed'.split(','), //all the single tags for HTML
23
+ extra_liners: 'head,body,/html'.split(','), //for tags that need a line of whitespace before them
24
+ inArray: function (what, arr) {
25
+ var len = arr.length;
26
+ for (var i = 0; i < len; i++) {
27
+ if (what === arr[i]) {
28
+ return true;
29
+ }
30
+ }
31
+ return false;
32
+ }
33
+ };
34
+
35
+ this.getContent = function () { //function to capture regular content between tags
36
+
37
+ var char = '';
38
+ var content = [];
39
+ var space = false; //if a space is needed
40
+ while (this.input.charAt(this.pos) !== '<') {
41
+ if (this.pos >= this.input.length) {
42
+ return content.length ? content.join('') : ['', 'TK_EOF'];
43
+ }
44
+
45
+ char = this.input.charAt(this.pos);
46
+ this.pos++;
47
+ this.line_char_count++;
48
+
49
+
50
+ if (this.Utils.in_array(char, this.Utils.whitespace)) {
51
+ if (content.length) {
52
+ space = true;
53
+ }
54
+ this.line_char_count--;
55
+ continue; //don't want to insert unnecessary space
56
+ } else if (space) {
57
+ if (this.line_char_count >= this.max_char) { //insert a line when the max_char is reached
58
+ content.push('\n');
59
+ for (var i = 0; i < this.indent_level; i++) {
60
+ content.push(this.indent_string);
61
+ }
62
+ this.line_char_count = 0;
63
+ } else {
64
+ content.push(' ');
65
+ this.line_char_count++;
66
+ }
67
+ space = false;
68
+ }
69
+ content.push(char); //letter at-a-time (or string) inserted to an array
70
+ }
71
+ return content.length ? content.join('') : '';
72
+ };
73
+
74
+ this.getScript = function () { //get the full content of a script to pass to js_beautify
75
+
76
+ var char = '';
77
+ var content = [];
78
+ var reg_match = this.regex.scriptEndTag;
79
+ reg_match.lastIndex = this.pos;
80
+ var reg = reg_match.exec(this.input);
81
+ var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
82
+ while (this.pos < end_script) { //get everything in between the script tags
83
+ if (this.pos >= this.input.length) {
84
+ return content.length ? content.join('') : ['', 'TK_EOF'];
85
+ }
86
+
87
+ char = this.input.charAt(this.pos);
88
+ this.pos++;
89
+
90
+
91
+ content.push(char);
92
+ }
93
+ return content.length ? content.join('') : ''; //we might not have any content at all
94
+ };
95
+
96
+ this.recordTag = function (tag) { //function to record a tag and its parent in this.tags Object
97
+ if (this.tags[tag + 'count']) { //check for the existence of this tag type
98
+ this.tags[tag + 'count']++;
99
+ this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
100
+ } else { //otherwise initialize this tag type
101
+ this.tags[tag + 'count'] = 1;
102
+ this.tags[tag + this.tags[tag + 'count']] = this.indent_level; //and record the present indent level
103
+ }
104
+ 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)
105
+ this.tags.parent = tag + this.tags[tag + 'count']; //and make this the current parent (i.e. in the case of a div 'div1')
106
+ };
107
+
108
+ this.retrieveTag = function (tag) { //function to retrieve the opening tag to the corresponding closer
109
+ if (this.tags[tag + 'count']) { //if the openener is not in the Object we ignore it
110
+ var parent = this.tags.parent; //check to see if it's a closable tag.
111
+ while (temp_parent) { //till we reach '' (the initial value);
112
+ if (tag + this.tags[tag + 'count'] === temp_parent) { //if this is it use it
113
+ break;
114
+ }
115
+ temp_parent = this.tags[temp_parent + 'parent']; //otherwise keep on climbing up the DOM Tree
116
+ }
117
+ if (temp_parent) { //if we caught something
118
+ this.indent_level = this.tags[tag + this.tags[tag + 'count']]; //set the indent_level accordingly
119
+ this.tags.parent = this.tags[temp_parent + 'parent']; //and set the current parent
120
+ }
121
+ delete this.tags[tag + this.tags[tag + 'count'] + 'parent']; //delete the closed tags parent reference...
122
+ delete this.tags[tag + this.tags[tag + 'count']]; //...and the tag itself
123
+ if (this.tags[tag + 'count'] == 1) {
124
+ delete this.tags[tag + 'count'];
125
+ } else {
126
+ this.tags[tag + 'count']--;
127
+ }
128
+ }
129
+ };
130
+
131
+ this.getTag = function () { //function to get a full tag and parse its type
132
+ var char = '';
133
+ var content = [];
134
+ var space = false;
135
+
136
+ do {
137
+ if (this.pos >= this.input.length) {
138
+ return content.length ? content.join('') : ['', 'TK_EOF'];
139
+ }
140
+
141
+ char = this.input.charAt(this.pos);
142
+ this.pos++;
143
+ this.line_char_count++;
144
+
145
+ if (this.Utils.in_array(char, this.Utils.whitespace)) { //don't want to insert unnecessary space
146
+ space = true;
147
+ this.line_char_count--;
148
+ continue;
149
+ }
150
+
151
+ if (char === "'" || char === '"') {
152
+ if (!content[1] || content[1] !== '!') { //if we're in a comment strings don't get treated specially
153
+ char += this.getUnfo(char);
154
+ space = true;
155
+ }
156
+ }
157
+
158
+ if (char === '=') { //no space before =
159
+ space = false;
160
+ }
161
+
162
+ if (content.length && content[content.length - 1] !== '=' && char !== '>' &&
163
163
  space) { //no space after = or before >
164
- if (this.line_char_count >= this.max_char) {
165
- this.print_newline(false, content);
166
- this.line_char_count = 0;
167
- } else {
168
- content.push(' ');
169
- this.line_char_count++;
170
- }
171
- space = false;
172
- }
173
- content.push(char); //inserts character at-a-time (or string)
174
- } while (char !== '>');
175
-
176
- var tag_complete = content.join('');
177
- var tag_index;
178
- if (tag_complete.indexOf(' ') != -1) { //if there's whitespace, thats where the tag name ends
179
- tag_index = tag_complete.indexOf(' ');
180
- } else { //otherwise go with the tag ending
181
- tag_index = tag_complete.indexOf('>');
182
- }
183
- var tag_check = tag_complete.substring(1, tag_index).toLowerCase();
184
- if (tag_complete.charAt(tag_complete.length - 2) === '/' ||
164
+ if (this.line_char_count >= this.max_char) {
165
+ this.printNewline(false, content);
166
+ this.line_char_count = 0;
167
+ } else {
168
+ content.push(' ');
169
+ this.line_char_count++;
170
+ }
171
+ space = false;
172
+ }
173
+ content.push(char); //inserts character at-a-time (or string)
174
+ } while (char !== '>');
175
+
176
+ var tag_complete = content.join('');
177
+ var tag;
178
+ if (tag_complete.indexOf(' ') != -1) { //if there's whitespace, thats where the tag name ends
179
+ tag_index = tag_complete.indexOf(' ');
180
+ } else { //otherwise go with the tag ending
181
+ tag_index = tag_complete.indexOf('>');
182
+ }
183
+ var tag_check = tag_complete.substring(1, tag_index).toLowerCase();
184
+ if (tag_complete.charAt(tag_complete.length - 2) === '/' ||
185
185
  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 /)
186
- this.tag_type = 'SINGLE';
187
- } else if (tag_check === 'script') { //for later script handling
188
- this.record_tag(tag_check);
189
- this.tag_type = 'SCRIPT';
190
- } else if (tag_check === 'style') { //for future style handling (for now it justs uses get_content)
191
- this.record_tag(tag_check);
192
- this.tag_type = 'STYLE';
193
- } else if (tag_check.charAt(0) === '!') { //peek for <!-- comment
194
- if (tag_check.indexOf('[if') != -1) { //peek for <!--[if conditional comment
195
- if (tag_complete.indexOf('!IE') != -1) { //this type needs a closing --> so...
196
- var comment = this.get_unformatted(this.regex.commentEnd, tag_complete); //...delegate to get_unformatted
197
- content.push(comment);
198
- }
199
- this.tag_type = 'START';
200
- } else if (tag_check.indexOf('[endif') != -1) { //peek for <!--[endif end conditional comment
201
- this.tag_type = 'END';
202
- this.unindent();
203
- } else if (tag_check.indexOf('[cdata[') != -1) { //if it's a <[cdata[ comment...
204
- var comment = this.get_unformatted(this.regex.cdataEnd, tag_complete); //...delegate to get_unformatted function
205
- content.push(comment);
206
- this.tag_type = 'SINGLE'; //<![CDATA[ comments are treated like single tags
207
- } else {
208
- var comment = this.get_unformatted('-->', tag_complete);
209
- content.push(comment);
210
- this.tag_type = 'SINGLE';
211
- }
212
- } else {
213
- if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
214
- this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors
215
- this.tag_type = 'END';
216
- } else { //otherwise it's a start-tag
217
- this.record_tag(tag_check); //push it on the tag stack
218
- this.tag_type = 'START';
219
- }
220
- if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
221
- this.print_newline(true, this.output);
222
- }
223
- }
224
- return content.join(''); //returns fully formatted tag
225
- }
226
-
227
- this.get_unformatted = function(delimiter, orig_tag) { //function to return unformatted content in its entirety
228
-
229
- if (orig_tag && orig_tag.indexOf(delimiter) != -1) {
230
- return '';
231
- }
232
- var char = '';
233
- var content = '';
234
- var space = true;
235
- do {
236
-
237
-
238
- char = this.input.charAt(this.pos);
239
- this.pos++
240
-
241
- if (this.Utils.in_array(char, this.Utils.whitespace)) {
242
- if (!space) {
243
- this.line_char_count--;
244
- continue;
245
- }
246
- if (char === '\n' || char === '\r') {
247
- content += '\n';
248
- for (var i = 0; i < this.indent_level; i++) {
249
- content += this.indent_string;
250
- }
251
- space = false; //...and make sure other indentation is erased
252
- this.line_char_count = 0;
253
- continue;
254
- }
255
- }
256
- content += char;
257
- this.line_char_count++;
258
- space = true;
259
-
260
-
261
- } while (content.indexOf(delimiter) == -1);
262
- return content;
263
- }
264
-
265
- this.get_token = function() { //initial handler for token-retrieval
266
- var token;
267
-
268
- if (this.last_token === 'TK_TAG_SCRIPT') { //check if we need to format javascript
269
- var temp_token = this.get_script();
270
- if (typeof temp_token !== 'string') {
271
- return temp_token;
272
- }
273
- token = js_beautify(temp_token, this.indent_size, this.indent_character, this.indent_level); //call the JS Beautifier
274
- return [token, 'TK_CONTENT'];
275
- }
276
- if (this.current_mode === 'CONTENT') {
277
- token = this.get_content();
278
- if (typeof token !== 'string') {
279
- return token;
280
- } else {
281
- return [token, 'TK_CONTENT'];
282
- }
283
- }
284
-
285
- if (this.current_mode === 'TAG') {
286
- token = this.get_tag();
287
- if (typeof token !== 'string') {
288
- return token;
289
- } else {
290
- var tag_name_type = 'TK_TAG_' + this.tag_type;
291
- return [token, tag_name_type];
292
- }
293
- }
294
- }
295
-
296
- this.printer = function(js_source, indent_character, indent_size, max_char) { //handles input/output and some other printing functions
297
-
298
- this.input = js_source || ''; //gets the input for the Parser
299
- this.output = [];
300
- this.indent_character = indent_character || ' ';
301
- this.indent_string = '';
302
- this.indent_size = indent_size || 2;
303
- this.indent_level = 0;
304
- this.max_char = max_char || 7000; //maximum amount of characters per line
305
- this.line_char_count = 0; //count to see if max_char was exceeded
306
-
307
- for (var i = 0; i < this.indent_size; i++) {
308
- this.indent_string += this.indent_character;
309
- }
310
-
311
- this.print_newline = function(ignore, arr) {
312
- this.line_char_count = 0;
313
- if (!arr || !arr.length) {
314
- return;
315
- }
316
- if (!ignore) { //we might want the extra line
317
- while (this.Utils.in_array(arr[arr.length - 1], this.Utils.whitespace)) {
318
- arr.pop();
319
- }
320
- }
321
- arr.push('\n');
322
- for (var i = 0; i < this.indent_level; i++) {
323
- arr.push(this.indent_string);
324
- }
325
- }
326
-
327
-
328
- this.print_token = function(text) {
329
- this.output.push(text);
330
- }
331
-
332
- this.indent = function() {
333
- this.indent_level++;
334
- }
335
-
336
- this.unindent = function() {
337
- if (this.indent_level > 0) {
338
- this.indent_level--;
339
- }
340
- }
341
- }
342
- return this;
343
- }
344
-
345
- /*_____________________--------------------_____________________*/
346
-
347
-
348
-
349
- multi_parser = new Parser(); //wrapping functions Parser
350
- multi_parser.printer(html_source, indent_character, indent_size); //initialize starting values
351
-
352
-
353
-
354
- var f = true;
355
- while (true) {
356
- var t = multi_parser.get_token();
357
- multi_parser.token_text = t[0];
358
- multi_parser.token_type = t[1];
359
-
360
- if (multi_parser.token_type === 'TK_EOF') {
361
- break;
362
- }
363
-
364
- switch (multi_parser.token_type) {
365
- case 'TK_TAG_START':
366
- case 'TK_TAG_SCRIPT':
367
- case 'TK_TAG_STYLE':
368
- multi_parser.print_newline(false, multi_parser.output);
369
- multi_parser.print_token(multi_parser.token_text);
370
- multi_parser.indent();
371
- multi_parser.current_mode = 'CONTENT';
372
- break;
373
- case 'TK_TAG_END':
374
- if (f)
375
- multi_parser.print_newline(true, multi_parser.output);
376
- multi_parser.print_token(multi_parser.token_text);
377
- multi_parser.current_mode = 'CONTENT';
378
- f = true;
379
- break;
380
- case 'TK_TAG_SINGLE':
381
- multi_parser.print_newline(false, multi_parser.output);
382
- multi_parser.print_token(multi_parser.token_text);
383
- multi_parser.current_mode = 'CONTENT';
384
- break;
385
- case 'TK_CONTENT':
386
- if (multi_parser.token_text !== '') {
387
- f = false;
388
- //multi_parser.print_newline(false, multi_parser.output);
389
- multi_parser.print_token(multi_parser.token_text);
390
- }
391
- multi_parser.current_mode = 'TAG';
392
- break;
393
- }
394
- multi_parser.last_token = multi_parser.token_type;
395
- multi_parser.last_text = multi_parser.token_text;
396
- }
397
- return multi_parser.output.join('');
186
+ this.tag_type = 'SINGLE';
187
+ } else if (tag_check === 'script') { //for later script handling
188
+ this.recordTag(tag_check);
189
+ this.tag_type = 'SCRIPT';
190
+ } else if (tag_check === 'style') { //for future style handling (for now it justs uses get_content)
191
+ this.recordTag(tag_check);
192
+ this.tag_type = 'STYLE';
193
+ } else if (tag_check.charAt(0) === '!') { //peek for <!-- comment
194
+ if (tag_check.indexOf('[if') != -1) { //peek for <!--[if conditional comment
195
+ if (tag_complete.indexOf('!IE') != -1) { //this type needs a closing --> so...
196
+ var comment = this.getUnfo(this.regex.commentEnd, tag_complete); //...delegate to get_unformatted
197
+ content.push(comment);
198
+ }
199
+ this.tag_type = 'START';
200
+ } else if (tag_check.indexOf('[endif') != -1) { //peek for <!--[endif end conditional comment
201
+ this.tag_type = 'END';
202
+ this.unindent();
203
+ } else if (tag_check.indexOf('[cdata[') != -1) { //if it's a <[cdata[ comment...
204
+ var comment = this.getUnfo(this.regex.cdataEnd, tag_complete); //...delegate to get_unformatted function
205
+ content.push(comment);
206
+ this.tag_type = 'SINGLE'; //<![CDATA[ comments are treated like single tags
207
+ } else {
208
+ var comment = this.getUnfo('-->', tag_complete);
209
+ content.push(comment);
210
+ this.tag_type = 'SINGLE';
211
+ }
212
+ } else {
213
+ if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
214
+ this.retrieveTag(tag_check.substring(1)); //remove it and all ancestors
215
+ this.tag_type = 'END';
216
+ } else { //otherwise it's a start-tag
217
+ this.recordTag(tag_check); //push it on the tag stack
218
+ this.tag_type = 'START';
219
+ }
220
+ if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
221
+ this.printNewline(true, this.output);
222
+ }
223
+ }
224
+ return content.join(''); //returns fully formatted tag
225
+ };
226
+
227
+ this.getUnfo = function (delimiter, orig_tag) { //function to return unformatted content in its entirety
228
+
229
+ if (orig_tag && orig_tag.indexOf(delimiter) != -1) {
230
+ return '';
231
+ }
232
+ var char = '';
233
+ var content = '';
234
+ var space = true;
235
+ do {
236
+
237
+ char = this.input.charAt(this.pos);
238
+ this.pos++;
239
+
240
+ if (this.Utils.in_array(char, this.Utils.whitespace)) {
241
+ if (!space) {
242
+ this.line_char_count--;
243
+ continue;
244
+ }
245
+ if (char === '\n' || char === '\r') {
246
+ content += '\n';
247
+ for (var i = 0; i < this.indent_level; i++) {
248
+ content += this.indent_string;
249
+ }
250
+ space = false; //...and make sure other indentation is erased
251
+ this.line_char_count = 0;
252
+ continue;
253
+ }
254
+ }
255
+ content += char;
256
+ this.line_char_count++;
257
+ space = true;
258
+
259
+
260
+ } while (content.indexOf(delimiter) == -1);
261
+ return content;
262
+ };
263
+
264
+ this.getToken = function () { //initial handler for token-retrieval
265
+ var token;
266
+
267
+ if (this.last_token === 'TK_TAG_SCRIPT') { //check if we need to format javascript
268
+ var token = this.getScript();
269
+ if (typeof temp_token !== 'string') {
270
+ return temp_token;
271
+ }
272
+ token = js_beautify(temp_token, this.indent_size, this.indent_character, this.indent_level); //call the JS Beautifier
273
+ return [token, 'TK_CONTENT'];
274
+ }
275
+ if (this.current_mode === 'CONTENT') {
276
+ token = this.getContent();
277
+ if (typeof token !== 'string') {
278
+ return token;
279
+ } else {
280
+ return [token, 'TK_CONTENT'];
281
+ }
282
+ }
283
+
284
+ if (this.current_mode === 'TAG') {
285
+ token = this.getTag();
286
+ if (typeof token !== 'string') {
287
+ return token;
288
+ } else {
289
+ var tag_name_type = 'TK_TAG_' + this.tag_type;
290
+ return [token, tag_name_type];
291
+ }
292
+ }
293
+ };
294
+
295
+ this.printer = function (js_source, indent_char, indent_size, max_char) { //handles input/output and some other printing functions
296
+
297
+ this.input = js_source || ''; //gets the input for the Parser
298
+ this.output = [];
299
+ this.indent_character = indent_character || ' ';
300
+ this.indent_string = '';
301
+ this.indent_size = indent_size || 2;
302
+ this.indent_level = 0;
303
+ this.max_char = max_char || 7000; //maximum amount of characters per line
304
+ this.line_char_count = 0; //count to see if max_char was exceeded
305
+
306
+ for (var i = 0; i < this.indent_size; i++) {
307
+ this.indent_string += this.indent_character;
308
+ }
309
+
310
+ this.printNewline = function (ignore, arr) {
311
+ this.line_char_count = 0;
312
+ if (!arr || !arr.length) {
313
+ return;
314
+ }
315
+ if (!ignore) { //we might want the extra line
316
+ while (this.Utils.in_array(arr[arr.length - 1], this.Utils.whitespace)) {
317
+ arr.pop();
318
+ }
319
+ }
320
+ arr.push('\n');
321
+ for (var i = 0; i < this.indent_level; i++) {
322
+ arr.push(this.indent_string);
323
+ }
324
+ };
325
+
326
+
327
+ this.printToken = function (text) {
328
+ this.output.push(text);
329
+ };
330
+
331
+ this.indent = function () {
332
+ this.indent_level++;
333
+ };
334
+
335
+ this.unindent = function () {
336
+ if (this.indent_level > 0) {
337
+ this.indent_level--;
338
+ }
339
+ };
340
+ };
341
+ return this;
342
+ }
343
+
344
+ /*_____________________--------------------_____________________*/
345
+
346
+
347
+ multi_parser = new Parser(); //wrapping functions Parser
348
+ multi_parser.printer(html_source, indent_character, indent_size); //initialize starting values
349
+
350
+ var f = true;
351
+ while (true) {
352
+ var t = multi_parser.get_token();
353
+ multi_parser.token_text = t[0];
354
+ multi_parser.token_type = t[1];
355
+
356
+ if (multi_parser.token_type === 'TK_EOF') {
357
+ break;
358
+ }
359
+
360
+ switch (multi_parser.token_type) {
361
+ case 'TK_TAG_START':
362
+ case 'TK_TAG_SCRIPT':
363
+ case 'TK_TAG_STYLE':
364
+ multi_parser.print_newline(false, multi_parser.output);
365
+ multi_parser.print_token(multi_parser.token_text);
366
+ multi_parser.indent();
367
+ multi_parser.current_mode = 'CONTENT';
368
+ break;
369
+ case 'TK_TAG_END':
370
+ if (f)
371
+ multi_parser.print_newline(true, multi_parser.output);
372
+ multi_parser.print_token(multi_parser.token_text);
373
+ multi_parser.current_mode = 'CONTENT';
374
+ f = true;
375
+ break;
376
+ case 'TK_TAG_SINGLE':
377
+ multi_parser.print_newline(false, multi_parser.output);
378
+ multi_parser.print_token(multi_parser.token_text);
379
+ multi_parser.current_mode = 'CONTENT';
380
+ break;
381
+ case 'TK_CONTENT':
382
+ if (multi_parser.token_text !== '') {
383
+ f = false;
384
+ //multi_parser.print_newline(false, multi_parser.output);
385
+ multi_parser.print_token(multi_parser.token_text);
386
+ }
387
+ multi_parser.current_mode = 'TAG';
388
+ break;
389
+ }
390
+ multi_parser.last_token = multi_parser.token_type;
391
+ multi_parser.last_text = multi_parser.token_text;
392
+ }
393
+ return multi_parser.output.join('');
398
394
  }
399
395
 
400
396
  module.exports = style_html;