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/README.md +125 -125
- package/eslint.config.js +106 -0
- package/html.js +220 -220
- package/htmlformat.js +391 -395
- package/index.js +340 -311
- package/package.json +4 -2
- package/regex.js +80 -80
- package/test.js +206 -206
- package/test_html.js +2284 -2284
- package/index copy.js +0 -897
package/htmlformat.js
CHANGED
|
@@ -1,400 +1,396 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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;
|