1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | Function: xajaxCompressFile |
---|
5 | |
---|
6 | <xajax> will call this function internally to compress the javascript code for |
---|
7 | more efficient delivery. |
---|
8 | |
---|
9 | $sFile - (stirng): The file to be compressed. |
---|
10 | */ |
---|
11 | function xajaxCompressFile($sFile) |
---|
12 | { |
---|
13 | //remove windows cariage returns |
---|
14 | $sFile = str_replace("\r",'',$sFile); |
---|
15 | |
---|
16 | //array to store replaced literal strings |
---|
17 | $literal_strings = array(); |
---|
18 | |
---|
19 | //explode the string into lines |
---|
20 | $lines = explode("\n",$sFile); |
---|
21 | //loop through all the lines, building a new string at the same time as removing literal strings |
---|
22 | $clean = ''; |
---|
23 | $inComment = false; |
---|
24 | $literal = ''; |
---|
25 | $inQuote = false; |
---|
26 | $escaped = false; |
---|
27 | $quoteChar = ''; |
---|
28 | |
---|
29 | $iLen = count($lines); |
---|
30 | for($i=0; $i<$iLen; ++$i) |
---|
31 | { |
---|
32 | $line = $lines[$i]; |
---|
33 | $inNormalComment = false; |
---|
34 | |
---|
35 | //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string |
---|
36 | $jLen = strlen($line); |
---|
37 | for($j=0; $j<$jLen; ++$j) |
---|
38 | { |
---|
39 | $c = substr($line,$j,1); |
---|
40 | $d = substr($line,$j,2); |
---|
41 | |
---|
42 | //look for start of quote |
---|
43 | if(!$inQuote && !$inComment) |
---|
44 | { |
---|
45 | //is this character a quote or a comment |
---|
46 | if(($c=='"' || $c=="'") && !$inComment && !$inNormalComment) |
---|
47 | { |
---|
48 | $inQuote = true; |
---|
49 | $inComment = false; |
---|
50 | $escaped = false; |
---|
51 | $quoteChar = $c; |
---|
52 | $literal = $c; |
---|
53 | } |
---|
54 | else if($d=="/*" && !$inNormalComment) |
---|
55 | { |
---|
56 | $inQuote = false; |
---|
57 | $inComment = true; |
---|
58 | $escaped = false; |
---|
59 | $quoteChar = $d; |
---|
60 | $literal = $d; |
---|
61 | $j++; |
---|
62 | } |
---|
63 | else if($d=="//") //ignore string markers that are found inside comments |
---|
64 | { |
---|
65 | $inNormalComment = true; |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | if (!$inNormalComment) |
---|
70 | $clean .= $c; |
---|
71 | } |
---|
72 | } |
---|
73 | else //allready in a string so find end quote |
---|
74 | { |
---|
75 | if($c == $quoteChar && !$escaped && !$inComment) |
---|
76 | { |
---|
77 | $inQuote = false; |
---|
78 | $literal .= $c; |
---|
79 | |
---|
80 | //subsitute in a marker for the string |
---|
81 | $clean .= "___" . count($literal_strings) . "___"; |
---|
82 | |
---|
83 | //push the string onto our array |
---|
84 | array_push($literal_strings,$literal); |
---|
85 | |
---|
86 | } |
---|
87 | else if($inComment && $d=="*/") |
---|
88 | { |
---|
89 | $inComment = false; |
---|
90 | $literal .= $d; |
---|
91 | ++$j; |
---|
92 | } |
---|
93 | else if($c == "\\" && !$escaped) |
---|
94 | $escaped = true; |
---|
95 | else |
---|
96 | $escaped = false; |
---|
97 | |
---|
98 | $literal .= $c; |
---|
99 | } |
---|
100 | } |
---|
101 | if($inComment) $literal .= "\n"; |
---|
102 | $clean .= "\n"; |
---|
103 | } |
---|
104 | //explode the clean string into lines again |
---|
105 | $lines = explode("\n",$clean); |
---|
106 | |
---|
107 | //now process each line at a time |
---|
108 | $iLen = count($lines); |
---|
109 | for($i=0; $i<$iLen; ++$i) |
---|
110 | { |
---|
111 | $line = $lines[$i]; |
---|
112 | |
---|
113 | //remove comments |
---|
114 | $line = preg_replace("/\/\/(.*)/","",$line); |
---|
115 | |
---|
116 | //strip leading and trailing whitespace |
---|
117 | $line = trim($line); |
---|
118 | |
---|
119 | //remove all whitespace with a single space |
---|
120 | $line = preg_replace("/\s+/"," ",$line); |
---|
121 | |
---|
122 | //remove any whitespace that occurs after/before an operator |
---|
123 | $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line); |
---|
124 | |
---|
125 | $lines[$i] = $line; |
---|
126 | } |
---|
127 | |
---|
128 | //implode the lines |
---|
129 | $sFile = implode("\n",$lines); |
---|
130 | |
---|
131 | //make sure there is a max of 1 \n after each line |
---|
132 | $sFile = preg_replace("/[\n]+/","\n",$sFile); |
---|
133 | |
---|
134 | //strip out line breaks that immediately follow a semi-colon |
---|
135 | $sFile = preg_replace("/;\n/",";",$sFile); |
---|
136 | |
---|
137 | //curly brackets aren't on their own |
---|
138 | $sFile = preg_replace("/[\n]*\{[\n]*/","{",$sFile); |
---|
139 | |
---|
140 | //finally loop through and replace all the literal strings: |
---|
141 | $iLen = count($literal_strings); |
---|
142 | for($i=0; $i<$iLen; ++$i) |
---|
143 | $sFile = str_replace('___'.$i.'___',$literal_strings[$i],$sFile); |
---|
144 | |
---|
145 | return $sFile; |
---|
146 | } |
---|