source: trunk/workflow/inc/report/xajax/xajaxCompress.php @ 7655

Revision 7655, 5.0 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

Line 
1<?php
2/**
3 * xajaxCompress.php :: function to compress Javascript
4 *
5 * xajax version 0.2.4
6 * copyright (c) 2005 by Jared White & J. Max Wilson
7 * http://www.xajaxproject.org
8 *
9 * xajax is an open source PHP class library for easily creating powerful
10 * PHP-driven, web-based Ajax Applications. Using xajax, you can asynchronously
11 * call PHP functions and update the content of your your webpage without
12 * reloading the page.
13 *
14 * xajax is released under the terms of the LGPL license
15 * http://www.gnu.org/copyleft/lesser.html#SEC3
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30 *
31 * @package xajax
32 * @version $Id$
33 * @copyright Copyright (c) 2005-2006  by Jared White & J. Max Wilson
34 * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License
35 */
36
37/**
38 * Compresses the Javascript code for more efficient delivery.
39 * (used internally)
40 *
41 * @param string contains the Javascript code to compress
42 */
43function xajaxCompressJavascript($sJS)
44{
45        //remove windows cariage returns
46        $sJS = str_replace("\r","",$sJS);
47       
48        //array to store replaced literal strings
49        $literal_strings = array();
50       
51        //explode the string into lines
52        $lines = explode("\n",$sJS);
53        //loop through all the lines, building a new string at the same time as removing literal strings
54        $clean = "";
55        $inComment = false;
56        $literal = "";
57        $inQuote = false;
58        $escaped = false;
59        $quoteChar = "";
60    $lines_count = count($lines);
61
62        for($i=0;$i<$lines_count;++$i)
63        {
64                $line = $lines[$i];
65                $inNormalComment = false;
66       
67                //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
68                for($j=0;$j<strlen($line);++$j)
69                {
70                        $c = substr($line,$j,1);
71                        $d = substr($line,$j,2);
72       
73                        //look for start of quote
74                        if(!$inQuote && !$inComment)
75                        {
76                                //is this character a quote or a comment
77                                if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
78                                {
79                                        $inQuote = true;
80                                        $inComment = false;
81                                        $escaped = false;
82                                        $quoteChar = $c;
83                                        $literal = $c;
84                                }
85                                else if($d=="/*" && !$inNormalComment)
86                                {
87                                        $inQuote = false;
88                                        $inComment = true;
89                                        $escaped = false;
90                                        $quoteChar = $d;
91                                        $literal = $d; 
92                                        ++$j;
93                                }
94                                else if($d=="//") //ignore string markers that are found inside comments
95                                {
96                                        $inNormalComment = true;
97                                        $clean .= $c;
98                                }
99                                else
100                                {
101                                        $clean .= $c;
102                                }
103                        }
104                        else //allready in a string so find end quote
105                        {
106                                if($c == $quoteChar && !$escaped && !$inComment)
107                                {
108                                        $inQuote = false;
109                                        $literal .= $c;
110       
111                                        //subsitute in a marker for the string
112                                        $clean .= "___" . count($literal_strings) . "___";
113       
114                                        //push the string onto our array
115                                        array_push($literal_strings,$literal);
116       
117                                }
118                                else if($inComment && $d=="*/")
119                                {
120                                        $inComment = false;
121                                        $literal .= $d;
122       
123                                        //subsitute in a marker for the string
124                                        $clean .= "___" . count($literal_strings) . "___";
125       
126                                        //push the string onto our array
127                                        array_push($literal_strings,$literal);
128       
129                                        ++$j;
130                                }
131                                else if($c == "\\" && !$escaped)
132                                        $escaped = true;
133                                else
134                                        $escaped = false;
135       
136                                $literal .= $c;
137                        }
138                }
139                if($inComment) $literal .= "\n";
140                $clean .= "\n";
141        }
142        //explode the clean string into lines again
143        $lines = explode("\n",$clean);
144       
145        //now process each line at a time
146    $lines_count = count($lines);
147        for($i=0;$i<$lines_count;++$i)
148        {
149                $line = $lines[$i];
150       
151                //remove comments
152                $line = preg_replace("/\/\/(.*)/","",$line);
153       
154                //strip leading and trailing whitespace
155                $line = trim($line);
156       
157                //remove all whitespace with a single space
158                $line = preg_replace("/\s+/"," ",$line);
159       
160                //remove any whitespace that occurs after/before an operator
161                $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
162       
163                $lines[$i] = $line;
164        }
165       
166        //implode the lines
167        $sJS = implode("\n",$lines);
168       
169        //make sure there is a max of 1 \n after each line
170        $sJS = preg_replace("/[\n]+/","\n",$sJS);
171       
172        //strip out line breaks that immediately follow a semi-colon
173        $sJS = preg_replace("/;\n/",";",$sJS);
174       
175        //curly brackets aren't on their own
176        $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
177       
178        //finally loop through and replace all the literal strings:
179    $literal_strings_count = count($literal_strings);
180        for($i=0;$i<$literal_strings_count;++$i)
181                $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
182       
183        return $sJS;
184}
185?>
Note: See TracBrowser for help on using the repository browser.