]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/BookmarkletGen/BookmarkletGen.php
Update ozh/BookmarkletGen
[Github/YOURLS.git] / includes / BookmarkletGen / BookmarkletGen.php
1 <?php
2
3 /**
4  * BookmarkletGen : converts readable Javascript code into a bookmarklet link
5  *
6  * Features :
7  * - removes comments
8  * - compresses code, not literal strings
9  *   Example:
10  *   function someName( param ) { alert( "this is a string" ) }
11  *   will return:
12  *   function%20someName(param){alert("this is a string")}
13  * - wraps code into a self invoking function
14  *
15  * This is basically a slightly enhanced PHP port of the excellent Bookmarklet Crunchinator
16  * http://ted.mielczarek.org/code/mozilla/bookmarklet.html
17  *
18  */
19 class BookmarkletGen {
20
21     private $literal_strings = array();
22     
23     /**
24      * Main function, calls all others
25      *
26      * @param  string $code  Javascript code to bookmarkletify
27      * @return string        Bookmarklet link
28      */
29     public function crunch( $code ) {
30         $out = "(function() {\n" . $code . "\n})();";
31
32         $out = $this->replace_strings( $out );
33         $out = $this->kill_comments( $out );
34         $out = $this->compress_white_space( $out );
35         $out = $this->combine_strings( $out );
36         $out = $this->restore_strings( $out );
37         $out = $this->encodeURIComponent( $out );
38         $out = 'javascript:' . $out;
39
40         return $out;
41     }
42     
43     /**
44      * PHP port of Javascript function encodeURIComponent
45      *
46      * From http://stackoverflow.com/a/1734255/36850
47      *
48      * @since
49      * @param  string $str  String to encode
50      * @return string       Encoded string
51      */
52     // 
53     private function encodeURIComponent( $str ) {
54         $revert = array(
55             '%21'=>'!', '%2A'=>'*', '%28'=>'(', '%29'=>')',
56         );
57     
58         return strtr( rawurlencode( $str ), $revert );
59     }
60
61     /**
62      * Kill comment lines and blocks
63      *
64      * @param  string $code  Commented Javascript code
65      * @return string        Commentless code
66      */
67     private function kill_comments( $code ) {
68         $code = preg_replace( '!\s*//.+$!m', '', $code );
69         $code = preg_replace( '!/\*.+?\*/!sm', '', $code ); // s modifier: dot matches new lines
70         
71         return $code;
72     }
73     
74     /**
75      * Compress white space
76      *
77      * Remove some extraneous spaces and make the whole script a one liner
78      *
79      * @param  string $code  Javascript code
80      * @return string        Compressed code
81      */
82     private function compress_white_space( $code ) {
83         // Tabs to space, no more than 1 consecutive space
84         $code = preg_replace( '!\t!m', ' ', $code );
85         $code = preg_replace( '![ ]{2,}!m', ' ', $code );
86         
87         // Remove uneccessary white space around operators, braces and brackets.
88         // \xHH sequence is: !%&()*+,-/:;<=>?[]\{|}~
89         $code = preg_replace( '/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/m', "$1", $code );
90         $code = preg_replace( '/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/m', "$1", $code );
91         
92         // Split on each line, trim leading/trailing white space, kill empty lines, combine everything in one line
93         $code = preg_split( '/\r\n|\r|\n/', $code );
94         foreach( $code as $i => $line ) {
95             $code[ $i ] = trim( $line );
96         }
97         $code = implode( '', $code );
98
99         return $code;
100     }
101     
102     /**
103      * Combine any consecutive strings
104      *
105      * In the case we have two consecutive quoted strings (eg: "hello" + "world"), save a couple more
106      * length and combine them
107      *
108      * @param  string $code  Javascript code
109      * @return string        Javascript code
110      */
111     private function combine_strings( $code ) {
112         $code = preg_replace('/"\+"/m', "", $code);
113         $code = preg_replace("/'\+'/m", "", $code);
114
115         return $code;
116     }
117
118     
119     /**
120      * Replace all literal strings (eg: "hello world") with a placeholder and collect them in an array
121      *
122      * The idea is that strings cannot be trimmed or white-space optimized: take them out first before uglifying
123      * the code, then we'll reinject them back in later
124      *
125      * @param  string $code  Javascript code
126      * @return string        Javascript code with placeholders (eg "__1__") instead of literal strings
127      */
128     private function replace_strings( $code ) {
129         $return    = "";
130         $literal   = "";
131         $quoteChar = "";
132         $escaped   = false;
133
134         // Split script into individual lines.
135         $lines = explode("\n", $code);
136         $count = count( $lines );
137         for( $i = 0; $i < $count; $i++ ) {
138
139             $j = 0;
140             $inQuote = false;
141             while ($j < strlen( $lines[$i] ) ) {
142                 $c = $lines[ $i ][ $j ];
143
144                 // If not already in a string, look for the start of one.
145                 if (!$inQuote) {
146                     if ($c == '"' || $c == "'") {
147                         $inQuote = true;
148                         $escaped = false;
149                         $quoteChar = $c;
150                         $literal = $c;
151                     }
152                     else {
153                         $return .= $c;
154                     }
155                 }
156
157                 // Already in a string, look for end and copy characters.
158                 else {
159                     if ($c == $quoteChar && !$escaped) {
160                         $inQuote = false;
161                         $literal .= $quoteChar;
162                         $return .= "__" . count( $this->literal_strings ) . "__";
163                         $this->literal_strings[ count( $this->literal_strings ) ] = $literal;
164                     }
165                     else if ($c == "\\" && !$escaped) {
166                         $escaped = true;
167                     }
168                     else {
169                         $escaped = false;
170                     }
171                     $literal .= $c;
172                 }
173                 $j++;
174             }
175             $return .= "\n";
176         }
177
178         return $return;
179     }
180     
181     /**
182      * Restore literal strings by replacing their placeholders with actual strings
183      *
184      * @param  string $code  Javascript code with placeholders
185      * @return string        Javascript code with actual strings
186      */
187     private function restore_strings( $code ) {
188         foreach( $this->literal_strings as $i => $string ) {
189             $code = preg_replace( '/__' . $i . '__/', $string, $code, 1 );
190         }
191         
192         return $code;
193     }
194
195 }