4 * BookmarkletGen : converts readable Javascript code into a bookmarklet link
8 * - compresses code, not literal strings
10 * function someName( param ) { alert( "this is a string" ) }
12 * function%20someName(param){alert("this is a string")}
13 * - wraps code into a self invoking function
15 * This is basically a slightly enhanced PHP port of the excellent Bookmarklet Crunchinator
16 * http://ted.mielczarek.org/code/mozilla/bookmarklet.html
19 class BookmarkletGen {
21 private $literal_strings = array();
24 * Main function, calls all others
26 * @param string $code Javascript code to bookmarkletify
27 * @return string Bookmarklet link
29 public function crunch( $code ) {
30 $out = "(function() {\n" . $code . "\n})();";
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;
44 * PHP port of Javascript function encodeURIComponent
46 * From http://stackoverflow.com/a/1734255/36850
49 * @param string $str String to encode
50 * @return string Encoded string
53 private function encodeURIComponent( $str ) {
55 '%21'=>'!', '%2A'=>'*', '%28'=>'(', '%29'=>')',
58 return strtr( rawurlencode( $str ), $revert );
62 * Kill comment lines and blocks
64 * @param string $code Commented Javascript code
65 * @return string Commentless code
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
75 * Compress white space
77 * Remove some extraneous spaces and make the whole script a one liner
79 * @param string $code Javascript code
80 * @return string Compressed code
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 );
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 );
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 );
97 $code = implode( '', $code );
103 * Combine any consecutive strings
105 * In the case we have two consecutive quoted strings (eg: "hello" + "world"), save a couple more
106 * length and combine them
108 * @param string $code Javascript code
109 * @return string Javascript code
111 private function combine_strings( $code ) {
112 $code = preg_replace('/"\+"/m', "", $code);
113 $code = preg_replace("/'\+'/m", "", $code);
120 * Replace all literal strings (eg: "hello world") with a placeholder and collect them in an array
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
125 * @param string $code Javascript code
126 * @return string Javascript code with placeholders (eg "__1__") instead of literal strings
128 private function replace_strings( $code ) {
134 // Split script into individual lines.
135 $lines = explode("\n", $code);
136 $count = count( $lines );
137 for( $i = 0; $i < $count; $i++ ) {
141 while ($j < strlen( $lines[$i] ) ) {
142 $c = $lines[ $i ][ $j ];
144 // If not already in a string, look for the start of one.
146 if ($c == '"' || $c == "'") {
157 // Already in a string, look for end and copy characters.
159 if ($c == $quoteChar && !$escaped) {
161 $literal .= $quoteChar;
162 $return .= "__" . count( $this->literal_strings ) . "__";
163 $this->literal_strings[ count( $this->literal_strings ) ] = $literal;
165 else if ($c == "\\" && !$escaped) {
182 * Restore literal strings by replacing their placeholders with actual strings
184 * @param string $code Javascript code with placeholders
185 * @return string Javascript code with actual strings
187 private function restore_strings( $code ) {
188 foreach( $this->literal_strings as $i => $string ) {
189 $code = preg_replace( '/__' . $i . '__/', $string, $code, 1 );