forked from prettydiff/prettydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup_beauty.js
2177 lines (2096 loc) · 116 KB
/
markup_beauty.js
1
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*prettydiff.com api.topcoms:true,api.insize:4,api.inchar:" " */
/*global markupmin, js_beautify, cleanCSS, markup_summary*/
/*
This code may be used internally to Travelocity without limitation,
exclusion, or restriction. If this code is used externally the
following comment must be included everywhere this code is used.
*/
/***********************************************************************
This is written by Austin Cheney on 7 May 2009. Anybody may use this
code without permission so long as this comment exists verbatim in each
instance of its use.
http://www.travelocity.com/
http://mailmarkup.org/
**********************************************************************/
/*
This application serves to beautify markup languages. The intent of
this application is to be language independant so long as the language
is tag based using "<" as the tag opening delimiter and ">" as the tag
ending delimiter. The input does not have to be well formed or valid
by any means so long as it can be determined where a tag begins and
where a tag ends. Additionally, the code also supports nested tags,
such as JSTL tags <c:out value="<em>text</em>"/>.
The only HTML specific area inferred by this application is that the
contents of "script" tags are presumed to be JavaScript and the
contents of "style" tags are presumed to be CSS.
Since this code is entirely language independant it does not make
assumptions on vocabulary. This means singleton tags, tags that occur
in a singular use opposed to tags that exist as a pair with one to
serve as an opening and the other to serve as the closing tag, are only
identified if they end with "/>". Otherwise singleton tags are
presumed to be opening tags will indent following tags as such.
This code was created for three reasons:
1) Create an application that is more open and friendly to
customization.
2) Provide an application that recognizes many various tag grammars
The default recognized tag types are:
* SGML comments "<!-- x -->"
* SSI tags "<!--#command -->"
* ASP tags "<% x %>"
* PHP tags "<?php x ?>" - Please note that php tags must begin with
"<?php" and not "<?" to prevent collision with XML parsing
statements. No support is provided for tags that start with
only "<?", and so they are considered to be just another start
tag.
* XML parsing commands "<?xml x ?>"
* SGML parsing commands "<!command x>"
* start tags "<div>"
* closing tags "</div>"
* singleton tags "<link/>" or "<link />"
!! It should be noted that at this time the contents of ASP and PHP
tags are completely preserved. The only impact is that the opening
line of these tags is indented exactly like a singleton tag up to the
first line break.
3) This provides beautification that does not insert extra whitespace
into the default whitespace parsed, tokenized, output. This is
accomplished by defining content as four different types:
* content - This type does not begin or end with whitespace
* mixed_start - This type begins but not ends with whitespace
* mixed_end - This type ends but not begins with whitespace
* mixed_both - This type ends and begins with whitespace
External Requirements:
1) cleanCSS function - This stand alone application is the
beautification engine used for the contents of "style" tags
2) js_beautify function - This stand alone application is the
beautification engine used for the contents of "script" tags
3) markupmin - This application is the minifier for markup langauges.
This is the first executed instruction so that we can start fresh
without any unneeded trash that may provide interference.
3a) markupmin requires the jsmin function for its standard
minification, but that portion of markupmin is not used by
markup_beauty
Options:
Options are properties of a single object literal named "args".
1) args.source - This is the source content to parse.
2) args.insize - This is the number of characters that make up a single
indentation. The default value is 4.
3) args.inchar - This is the character used for indentation. The
default character is a space.
4) args.mode - This argument accepts a value of 'beautify' or 'diff'.
The value 'beautify' informs the markupmin function to preserve markup
comments and not to minify CSS or JavaScript. The value 'diff'
informs markupmin to not minify CSS or JavaScript and does not
preserve markup comments.
5) args.comments - This determines whether comments should be indented
in accordance with the markup or flush to the left. The acceptable
values are 'noindent' or 'indent'. 'noindent' is the default value.
6) args.style - This determines whether the contents of script or
style tags should be indented starting from the opening script or
style tag or if this code should start indentation independent of the
markup. Acceptable values are true or false. The default value is
false.
7) args.html - If this argument is supplied the boolean value true,
and not string "true", then tags with certain tag names are converted
to singleton types after the type assignment is performed. This
correction occurs regardless of syntax.
8) args.content
9) args.force_indent - If this argument is supplied the boolean value
true then all parts of the source code are always indented without
regard for white space tokens.
summary
Variable summary is used in an unassigned anonymous function at the
very bottom of markup_beauty, but is not scoped by markup_beauty. It
is intended for use as closure to markup_beauty, because the variable
name markup_summary is intended for use outside of markup_beauty, but
it requires access to the variables and arrays of markup_beauty.
Markup Summary is a small HTML table and some conditional warning
statements to prodive statistics for analysis of processed markup and
to alter users if to possibly flaws in the input that may likely
interefere with processing of beautification.
-----------------------------------------------------------------------
*/
var markup_beauty = function (args) {
"use strict";
var tab = "",
token = [],
build = [],
cinfo = [],
level = [],
inner = [],
sum = [],
x = args.source,
//This function only normalizes input to the application
start = (function () {
if (!args.source || typeof args.source !== "string") {
args.source = "";
}
if (args.insize === undefined || isNaN(args.insize)) {
args.insize = 4;
}
if (typeof args.inchar !== "string" || args.inchar.length < 1) {
args.inchar = " ";
}
if (!args.mode || args.mode !== "diff") {
args.mode = "beautify";
}
if (!args.comments || args.comments !== "indent") {
args.comments = "noindent";
}
if (!args.style || args.style !== "indent") {
args.style = "noindent";
}
if (typeof args.html !== "boolean") {
args.html = false;
}
if (typeof args.content !== "boolean") {
args.content = false;
}
if (typeof args.force_indent !== "boolean") {
args.force_indent = false;
}
}()),
//cdatafix temporarily transforms angle brackets in cdata
//declarations to prevent contamination. This mutation is
//corrected at the top of the code_type function.
cdatafix = (function () {
var a = function (y) {
y = y.replace(/</g, "\nprettydiffcdatas");
return y;
},
b = function (y) {
y = y.replace(/>/g, "\nprettydiffcdatae");
return y;
};
x = x.replace(/\/+<!\[+[A-Z]+\[+/g, a).replace(/\/+\]+>/g, b);
}()),
//innerset Function is the logic that intelligently
//identifies the angle brackets nested inside quotes and
//converts them to square brackets to prevent interference
//of beautification. This is the logic that allows JSP
//beautification to occur.
innerset = (function () {
var a,
b,
e,
f,
g,
j,
l,
m,
n,
o,
p,
q = [">"],
r = 0,
h = -1,
i = 0,
k = -1,
c = args.source.length,
d = [];
for (a = 0; a < c; a += 1) {
//if PHP, ASP, script, or style found then pass over
if (x.substr(a, 7).toLowerCase() === "<script") {
for (b = a + 7; b < c; b += 1) {
if (x.charAt(b) + x.charAt(b + 1) + x.charAt(b + 2).toLowerCase() + x.charAt(b + 3).toLowerCase() + x.charAt(b + 4).toLowerCase() + x.charAt(b + 5).toLowerCase() + x.charAt(b + 6).toLowerCase() + x.charAt(b + 7).toLowerCase() + x.charAt(b + 8) === "</script>") {
//h counts the index of the future build
//array
if (/></.test(x.substr(a, b))) {
h += 2;
} else {
h += 3;
}
a = b + 8;
break;
}
}
} else if (x.substr(a, 6).toLowerCase() === "<style") {
for (b = a + 6; b < c; b += 1) {
if (x.charAt(b) + x.charAt(b + 1) + x.charAt(b + 2).toLowerCase() + x.charAt(b + 3).toLowerCase() + x.charAt(b + 4).toLowerCase() + x.charAt(b + 5).toLowerCase() + x.charAt(b + 6).toLowerCase() + x.charAt(b + 7) === "</style>") {
//h counts the index of the future build
//array
if (/></.test(x.substr(a, b))) {
h += 2;
} else {
h += 3;
}
a = b + 7;
break;
}
}
} else if (x.substr(a, 5) === "<?php") {
for (b = a + 5; b < c; b += 1) {
if (x.charAt(b - 1) === "?" && x.charAt(b) === ">") {
a = b;
h += 1;
break;
}
}
} else if (x.charAt(a) === "<" && x.charAt(a + 1) === "%") {
for (b = a + 2; b < c; b += 1) {
if (x.charAt(b - 1) === "%" && x.charAt(b) === ">") {
a = b;
h += 1;
break;
}
}
//This section identifies SGML tags and the
//location of internally contained angle
//brackets.
} else if (x.charAt(a) === "<" && x.charAt(a + 1) === "!" && /[A-Z]|\[/.test(x.charAt(a + 2))) {
for (b = a + 3; b < c; b += 1) {
if (x.charAt(b) === ">" && q[q.length - 1] === ">" && q.length === 1) {
h += 1;
if (r !== 0) {
d.push([a, b, h, a]);
}
r = 0;
a = b;
q = [">"];
break;
} else if (x.charAt(b) === "<") {
q.push(">");
r = b;
} else if (x.charAt(b) === ">" && q.length > 1) {
q.pop();
r = b;
} else if (x.charAt(b) === "[") {
q.push("]");
} else if (x.charAt(b) === "]") {
q.pop();
} else if (x.charAt(b) === "\"") {
if (q[q.length - 1] === "\"") {
q.pop();
} else {
q.push("\"");
}
} else if (x.charAt(b) === "'") {
if (q[q.length - 1] === "'") {
q.pop();
} else {
q.push("'");
}
}
}
//Don't even bother with empty qutoes: "" or ''
} else if (x.charAt(a) === x.charAt(a + 1) && (x.charAt(a) === "\"" || x.charAt(a) === "'")) {
a += 1;
} else if (x.charAt(a - 1) === "=" && (x.charAt(a) === "\"" || x.charAt(a) === "'")) {
//This first bit with the "m" and "o" variables
//instructs the principle loop of innerset to
//ignore quote characters that fall outside of
//tags.
o = -1;
for (m = a - 1; m > 0; m -= 1) {
if ((x.charAt(m) === "\"" && x.charAt(a) === "\"") || (x.charAt(m) === "'" && x.charAt(a) === "'") || x.charAt(m) === "<") {
break;
} else if (x.charAt(m) === ">") {
o = m;
break;
}
}
if (o === -1) {
//n is reset to be used as a switch.
n = 0;
for (b = a + 1; b < c; b += 1) {
//Ignore closing quotes if they reside
//inside a script, style, ASP, or PHP
//block.
if (x.substr(b, 7).toLowerCase() === "<script") {
for (p = b + 7; p < c; p += 1) {
if (x.charAt(p) + x.charAt(p + 1) + x.charAt(p + 2).toLowerCase() + x.charAt(p + 3).toLowerCase() + x.charAt(p + 4).toLowerCase() + x.charAt(p + 5).toLowerCase() + x.charAt(p + 6).toLowerCase() + x.charAt(p + 7).toLowerCase() + x.charAt(p + 8) === "</script>") {
b = p + 8;
break;
}
}
} else if (x.substr(b, 6).toLowerCase() === "<style") {
for (p = b + 6; p < c; p += 1) {
if (x.charAt(p) + x.charAt(p + 1) + x.charAt(p + 2).toLowerCase() + x.charAt(p + 3).toLowerCase() + x.charAt(p + 4).toLowerCase() + x.charAt(p + 5).toLowerCase() + x.charAt(p + 6).toLowerCase() + x.charAt(p + 7) === "</style>") {
b = p + 7;
break;
}
}
} else if (x.substr(b, 5) === "<?php") {
for (p = b + 5; p < c; p += 1) {
if (x.charAt(p - 1) === "?" && x.charAt(p) === ">") {
b = p;
break;
}
}
} else if (x.charAt(b) === "<" && x.charAt(b + 1) === "%") {
for (p = b + 5; p < c; p += 1) {
if (x.charAt(p - 1) === "%" && x.charAt(p) === ">") {
b = p;
break;
}
}
} else if (x.charAt(b) === ">" || x.charAt(b) === "<") {
//There is no reason to push every
//set of quotes into the "d" array
//if those quotes do not contain
//angle brackets. This is a switch
//to test for such.
n = 1;
} else if ((x.charAt(b - 1) !== "\\" && ((x.charAt(a) === "\"" && x.charAt(b) === "\"") || (x.charAt(a) === "'" && x.charAt(b) === "'"))) || b === c - 1) {
//The "l" variable is used as an
//on/off switch to allow content,
//but not sequentially. Tags with
//quotes following content with
//quotes need to be decremented to
//correct an inflated count
if (k !== h && l === 1) {
l = 0;
h -= 1;
k -= 1;
} else if (k === h) {
for (e = i + 1; e > a; e += 1) {
if (!/\s/.test(x.charAt(e))) {
break;
}
}
j = e;
//This condition is for
//nonsequential content pieces
if (i < a && l !== 1) {
l = 1;
h += 1;
k += 1;
}
}
//a = index of opening quotes from a
// quote pair
//b = index of closing quotes from a
// quote pair
//h = tag and content count
//j = the index where tag "h" starts
if (n === 1) {
d.push([a, b, h, j]);
}
a = b;
break;
}
}
}
} else if (x.charAt(a) === "<") {
//If a HTML/XML comment is encountered then skip
if (x.charAt(a + 1) === "!" && x.charAt(a + 2) === "-" && x.charAt(a + 3) === "-") {
for (b = a + 4; b < x.length; b += 1) {
if (x.charAt(b) === "-" && x.charAt(b + 1) === "-" && x.charAt(b + 2) === ">") {
break;
}
}
h += 1;
a = b + 2;
//If not a HTML/XML comment increase the tag
//count
} else {
h += 1;
j = a;
}
} else if (x.charAt(a + 1) === "<" && x.charAt(a) !== ">") {
//Acount for content outside of tags
for (b = a; b > 0; b -= 1) {
if (!/\s/.test(x.charAt(b)) && x.charAt(b) !== ">") {
h += 1;
k += 1;
j = a;
break;
} else if (x.charAt(b) === ">") {
if (h !== k) {
k += 1;
i = a;
}
break;
}
}
//Count for the closing of tags
} else if (x.charAt(a) === ">") {
k += 1;
i = a;
}
}
c = d.length;
x = x.split("");
//Code hand off must occur between quote discovery and
//tag count. Hand off must allow for discovery to be
//repacked into numbers relevant to postcomputation and
//not to input. This hand off produces the "inner"
//array for consumption by the innerfix array.
for (a = 0; a < c; a += 1) {
i = d[a][0] + 1;
f = d[a][1];
g = d[a][2];
j = d[a][3];
//This loop converts quotes angle brackets to square
//brackets and simultaneously builds out the "inner"
//arrry. The inner array contains the reference
//locations of the converted angle brackets so the
//program can put the angle brackets back after
//JavaScript and CSS are beautified.
for (e = i; e < f; e += 1) {
//h is the character index of a converted angle
//bracket in a given tag
h = 0;
if (x[e] === "<") {
x[e] = "[";
for (b = e; b > j; b -= 1) {
h += 1;
if (/\s/.test(x[b])) {
for (k = b - 1; k > j; k -= 1) {
if (!/\s/.test(x[k])) {
//This condition accounts
//for white space
//normalization around equal
//characters that is
//supplied by markupmin,
//otherwise h is incremented
//for runs of white space
//characters prior to
//accounting for
//tokenization.
if (x[k] !== "=") {
h += 1;
} else if (/\s/.test(x[k - 1])) {
h -= 1;
}
b = k;
break;
}
}
}
}
if (/\s/.test(x[i])) {
h -= 1;
}
inner.push(["<", h, g]);
} else if (x[e] === ">") {
x[e] = "]";
for (b = e; b > j; b -= 1) {
h += 1;
if (/\s/.test(x[b])) {
for (k = b - 1; k > j; k -= 1) {
if (!/\s/.test(x[k])) {
if (x[k] !== "=") {
h += 1;
} else if (/\s/.test(x[k - 1])) {
h -= 1;
}
b = k;
break;
}
}
}
}
if (/\s/.test(x[i])) {
h -= 1;
}
inner.push([">", h, g]);
}
}
}
//x must be joined back into a string so that it can
//pass through the markupmin function.
x = x.join("");
}()),
//This function builds full tags and content into an array
//named build and names the tag types or content into
//elements of a separate array that I have named token.
elements = (function () {
var q,
a,
loop,
i,
Z,
//This function looks for the end of a designated
//tag and then returns the entire tag as a single
//output.
b = function (end) {
var c = i,
d,
e,
f = "",
z = end.charAt(end.length - 2),
y = end.split("").reverse(),
//The first loop looks for the end position
//of a tag. The second loop verifies the
//first loop did not stop too early.
g = function () {
for (c; c < loop; c += 1) {
if (z !== "-" && z !== "?" && z !== "%" && x[c] === ">") {
break;
} else if (x[c - 1] + x[c] === z + ">") {
break;
}
}
Z = y.length;
for (d = 0; d < Z; d += 1) {
if (x[c - d] !== y[d]) {
e = false;
c += 1;
break;
}
e = true;
}
};
g();
//This is a check to ensure the entire intended
//tag was actually captured and stopped because
//of an early ">" character. This check is
//necessary for comments, ASP, and PHP tags.
if (e !== true) {
do {
g();
} while (e !== true);
}
if (e === true) {
//Concat the characters from the now known
//end point to build the tag.
Z = c + 1;
for (d = i; d < Z; d += 1) {
f = f + x[d];
}
}
//This logic provides the ability for later
//logic to determine if a singleton tag must be
//treated as content or as an element with
//indentation.
if (x[i - 2] === ">" && x[i - 1] === " ") {
f = " " + f;
}
i = c - 1;
return f;
},
//This function builds content into isolated usable
//content units. This is for content while, which
//include external code, elements() is for tags.
cgather = function (z) {
var c,
d = "",
e;
q = "";
for (c = i; c < loop; c += 1) {
//Verifies if the end script tag is quoted
if (q === "" && x[c - 1] !== "\\") {
if (x[c] === "/" && x[c + 1] && x[c + 1] === "/") {
q = "//";
} else if (x[c] === "/" && x[c + 1] && x[c + 1] === "*") {
q = "/*";
} else if (x[c] === "'" || x[c] === "\"" || x[c] === "/") {
//It is necessary to determine if
//a forward slash character is
//division or the opening of a
//regular expression. If division
//then ignore and move on. I am
//presuming division is only when
//the forward slash follows a
//closing container or word
//character.
if (x[c] === "/") {
for (e = c - 1; e > 0; e -= 1) {
if (!/\s/.test(x[e])) {
break;
}
}
if (x[e] === ")" || x[e] === "]" || x[e] === "}" || /\w/.test(x[e])) {
q = "";
} else {
q = "/";
}
} else {
q = x[c];
}
}
} else if (x[c - 1] !== "\\" && ((q === "'" && x[c] === "'") || (q === "\"" && x[c] === "\"") || (q === "/" && x[c] === "/") || (q === "//" && (x[c] === "\n" || (x[c - 4] && x[c - 4] === "/" && x[c - 3] === "/" && x[c - 2] === "-" && x[c - 1] === "-" && x[c] === ">"))) || (q === "/*" && x[c - 1] === "*" && x[c] === "/"))) {
q = "";
}
if (((z === "script" && q === "") || z === "style") && x[c] === "<" && x[c + 1] === "/" && x[c + 2].toLowerCase() === "s") {
if (z === "script" && (x[c + 3].toLowerCase() === "c" && x[c + 4].toLowerCase() === "r" && x[c + 5].toLowerCase() === "i" && x[c + 6].toLowerCase() === "p" && x[c + 7].toLowerCase() === "t")) {
break;
} else if (z === "style" && (x[c + 3].toLowerCase() === "t" && x[c + 4].toLowerCase() === "y" && x[c + 5].toLowerCase() === "l" && x[c + 6].toLowerCase() === "e")) {
break;
}
} else if (z === "other" && x[c] === "<") {
break;
} else {
d = d + x[c];
}
}
i = c - 1;
if (args.content) {
if (d.charAt(0) === " " && d.charAt(d.length - 1) === " ") {
d = " text ";
} else if (d.charAt(0) === " ") {
d = " text";
} else if (d.charAt(d.length - 1) === " ") {
d = "text ";
} else {
d = "text";
}
}
return d;
},
//The type_define function sorts markup code into
//various designated types. The build array holds
//particular code while the token array holds the
//designation for that code. The argument supplied
//to the "b" function is the syntax ending for a
//given tag type. I have designed these types but
//others can be added:
// * SGML and XML tag comments
// * SSI Apache instructions
// * SGML declarations, such as the HTML Doctype
// * XML processing declarations
// * PHP tags - These tags must be opened with
// <?php and not <?.
// * SCRIPT tags for html
// * STYLE tags for html
// * ASP tags
// * ending tags of a tag pair
// * singelton tags, such as
// <br/>, <meta/>, <link/>
// * starting tags of a tag pair
//
// !Tags starting with only <? are not considered,
//so by default these are treated as a start tag.
//Use the correct PHP tags!!!!
// !Singelton tags must end with "/>" or they will
//also be regarded as start tags. This code is
//vocabulary independent and I do not read minds.
type_define = (function () {
var a;
//Source data is minified before it is beautified.
x = markupmin(x, args.mode, args.html).split("");
loop = x.length;
for (i = 0; i < loop; i += 1) {
if (x[i] === "<" && x[i + 1] === "!" && x[i + 2] === "-" && x[i + 3] === "-" && x[i + 4] !== "#" && token[token.length - 1] !== "T_script" && token[token.length - 1] !== "T_style") {
build.push(b("-->"));
token.push("T_comment");
} else if (x[i] === "<" && x[i + 1] === "!" && x[i + 2] === "-" && x[i + 3] === "-" && x[i + 4] === "#") {
build.push(b("-->"));
token.push("T_ssi");
} else if (x[i] === "<" && x[i + 1] === "!" && x[i + 2] !== "-" && token[token.length - 1] !== "T_script") {
build.push(b(">"));
token.push("T_sgml");
} else if (x[i] === "<" && x[i + 1] === "?" && x[i + 2].toLowerCase() === "x" && x[i + 3].toLowerCase() === "m" && x[i + 4].toLowerCase() === "l") {
build.push(b("?>"));
token.push("T_xml");
} else if (x[i] === "<" && x[i + 1] === "?" && x[i + 2].toLowerCase() === "p" && x[i + 3].toLowerCase() === "h" && x[i + 4].toLowerCase() === "p") {
build.push(b("?>"));
token.push("T_php");
} else if (x[i] === "<" && x[i + 1].toLowerCase() === "s" && x[i + 2].toLowerCase() === "c" && x[i + 3].toLowerCase() === "r" && x[i + 4].toLowerCase() === "i" && x[i + 5].toLowerCase() === "p" && x[i + 6].toLowerCase() === "t") {
build.push(b(">"));
//contents of a script tag are
//JavaScript if value of type attribute
//is:
//* not present
//text/javascript
//application/javascript
//application/x-javascript
//text/ecmascript
//application/ecmascript
a = build[build.length - 1].toLowerCase().replace(/'/g, "\"");
if (a.charAt(a.length - 2) === "/") {
token.push("T_singleton");
} else if (a.indexOf(" type=\"") === -1 || a.indexOf(" type=\"text/javascript\"") !== -1 || a.indexOf(" type=\"application/javascript\"") !== -1 || a.indexOf(" type=\"application/x-javascript\"") !== -1 || a.indexOf(" type=\"text/ecmascript\"") !== -1 || a.indexOf(" type=\"application/ecmascript\"") !== -1) {
token.push("T_script");
} else {
token.push("T_tag_start");
}
} else if (x[i] === "<" && x[i + 1].toLowerCase() === "s" && x[i + 2].toLowerCase() === "t" && x[i + 3].toLowerCase() === "y" && x[i + 4].toLowerCase() === "l" && x[i + 5].toLowerCase() === "e") {
build.push(b(">"));
//contents of a style tag are CSS if
//value of type attribute is:
//* not present
//text/css
a = build[build.length - 1].toLowerCase().replace(/'/g, "\"");
if (a.indexOf(" type=\"") === -1 || a.indexOf(" type=\"text/css\"") !== -1) {
token.push("T_style");
} else {
token.push("T_tag_start");
}
} else if (x[i] === "<" && x[i + 1] === "%") {
build.push(b("%>"));
token.push("T_asp");
} else if (x[i] === "<" && x[i + 1] === "/") {
build.push(b(">"));
token.push("T_tag_end");
} else if (x[i] === "<" && token[token.length - 1] !== "T_script" && token[token.length - 1] !== "T_style" && (x[i + 1] !== "!" || x[i + 1] !== "?" || x[i + 1] !== "/" || x[i + 1] !== "%")) {
for (a = i; a < loop; a += 1) {
if (x[a] !== "?" && x[a] !== "%") {
if (x[a] === "/" && x[a + 1] === ">") {
build.push(b("/>"));
token.push("T_singleton");
break;
} else if (x[a + 1] === ">") {
build.push(b(">"));
token.push("T_tag_start");
break;
}
}
}
} else if (x[i - 1] === ">" && (x[i] !== "<" || (x[i] !== " " && x[i + 1] !== "<"))) {
if (token[token.length - 1] === "T_script") {
build.push(cgather("script"));
token.push("T_content");
} else if (token[token.length - 1] === "T_style") {
build.push(cgather("style"));
token.push("T_content");
} else if (x[i - 1] + x[i] + x[i + 1] !== "> <") {
build.push(cgather("other"));
token.push("T_content");
}
}
}
}());
}()),
//This function provides structual relevant descriptions for
//content and groups tags into categories.
code_type = (function () {
var i,
Z = token.length;
for (i = 0; i < Z; i += 1) {
build[i] = build[i].replace(/\s*prettydiffcdatas/g, "<").replace(/\s*prettydiffcdatae/g, ">");
if (token[i] === "T_sgml" || token[i] === "T_xml") {
cinfo.push("parse");
} else if (token[i] === "T_asp" || token[i] === "T_php" || token[i] === "T_ssi") {
cinfo.push("singleton");
} else if (token[i] === "T_comment") {
cinfo.push("comment");
} else if ((token[i] === "T_content" && build[i] !== " ") && token[i - 1] === "T_script") {
cinfo.push("external");
} else if (token[i] === "T_content" && token[i - 1] === "T_style") {
cinfo.push("external");
} else if (token[i] === "T_content" && build[i].charAt(0) === " " && build[i].charAt(build[i].length - 1) === " ") {
cinfo.push("mixed_both");
} else if (token[i] === "T_content" && build[i].charAt(0) === " " && build[i].charAt(build[i].length - 1) !== " ") {
cinfo.push("mixed_start");
} else if (token[i] === "T_content" && build[i].charAt(0) !== " " && build[i].charAt(build[i].length - 1) === " ") {
cinfo.push("mixed_end");
} else if (token[i] === "T_content") {
cinfo.push("content");
} else if (token[i] === "T_tag_start") {
cinfo.push("start");
} else if (token[i] === "T_style") {
build[i] = build[i].replace(/\s+/g, " ");
cinfo.push("start");
} else if (token[i] === "T_script") {
build[i] = build[i].replace(/\s+/g, " ");
cinfo.push("start");
} else if (token[i] === "T_singleton") {
cinfo.push("singleton");
} else if (token[i] === "T_tag_end") {
cinfo.push("end");
}
}
//summary is a replica of the build array prior to any
//beautification for use in the markup_summary function
sum = sum.concat(build);
}()),
//The tag_check function creates the tab stops from the
//values supplied by the indent_character and indent_size
//arguments. If no values are supplied or are supplied
//improperly a reasonable default is created.
tab_check = (function () {
var a,
b = args.insize,
c = args.inchar;
for (a = 0; a < b; a += 1) {
tab += c;
}
return tab;
}()),
//this function cheats the structure and looks at tag names
cheat = (function () {
if (!args.html) {
return;
}
var a,
b,
i,
loop = cinfo.length;
for (i = 0; i < loop; i += 1) {
if (cinfo[i] === "start") {
a = build[i].indexOf(" ");
if (build[i].length === 3) {
b = build[i].charAt(1).toLowerCase();
} else if (a === -1) {
b = build[i].slice(1, cinfo[i].length - 2).toLowerCase();
} else if (a === 0) {
b = build[i].slice(1, build[i].length);
a = b.indexOf(" ");
b = b.slice(1, a).toLowerCase();
} else {
b = build[i].slice(1, a).toLowerCase();
}
if (b === "br" || b === "meta" || b === "link" || b === "img" || b === "hr" || b === "base" || b === "basefont" || b === "area" || b === "col" || b === "frame" || b === "input" || b === "param") {
cinfo[i] = "singleton";
token[i] = "T_singleton";
}
}
}
}()),
//This function sets the tab levels for the code. It is set
//to use the cinfo array definitions but could be rewritten
//to use the token array.
tab_level = (function () {
var i,
loop = cinfo.length,
a,
//This function looks back to the most previous
//indented tag that is not an end tag and returns a
//count based upon the number between the current
//tag and this previous indented tag. If the
//argument has a value of "start" then indentation
//is increased by 1.
c = function (x) {
var k,
m = 0;
if (x === "start") {
m += 1;
}
for (k = i - 1; k > -1; k -= 1) {
if (cinfo[k] === "start" && level[k] === "x") {
m += 1;
} else if (cinfo[k] === "end") {
m -= 1;
} else if (cinfo[k] === "start" && level[k] !== "x") {
return level.push(level[k] + m);
} else if (k === 0) {
if (cinfo[k] !== "start") {
return level.push(0);
} else if (cinfo[i] === "mixed_start" || cinfo[i] === "content" || (cinfo[i] === "singleton" && build[i].charAt(0) !== " ")) {
return level.push("x");
} else {
return level.push(1);
}
}
}
},
//This function is used by end tags to determine
//indentation.
e = function () {
var yy = 1,
//This is executed if the previous start is
//not indented.
z = function (y) {
for (y; y > 0; y -= 1) {
if (level[y] !== "x") {
return level.push(level[y] + 1);
}
}
},
//If prior item is an end tag or content
//ending with space black box voodoo magic
//must occur.
w = function () {
var k,
q,
y,
//This function finds the prior
//existing indented start tag. This
//start tag may not be the current
//end tag's matching pair. If the
//tag prior to this indented start
//tag is not an end tag this
//function escapes and later logic
//is used.
u = function () {
//t() is executed if the prior
//indented start tag exists
//directly after an end tag.
//This function is necessary to
//determine if indentation must
//be subtracted from the prior
//indented start tag.
var t = function () {
var s,
l = 0;
//Finds the prior start
//tag followed by a
//start tag where both
//have indentation.
//This creates a frame
//of reference for
//performing reflexive
//calculation.
for (s = i - 1; s > 0; s -= 1) {
if ((cinfo[s] === "start" && cinfo[s + 1] === "start" && level[s] === level[s + 1] - 1) || (cinfo[s] === "start" && cinfo[s - 1] !== "start" && level[s] === level[s - 1])) {
break;
}
}
//Incrementor is
//increased if indented
//content found followed
//by unindented end tag
//by looping up from the
//frame of reference.
for (k = s + 1; k < i; k += 1) {
if (cinfo[k] === "mixed_start" && cinfo[k + 1] === "end") {
l += 1;
}
}
//If prior logic fails
//and frame of reference
//follows an indented
//end tag the
//incrementor is
//increased.
if (cinfo[s - 1] === "end" && level[s - 1] !== "x" && l === 0) {
l += 1;
}
//All prior logic can
//fail and so a