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();
23 private function __construct__() {}
26 * Main function, calls all others
28 * @param string $code Javascript code to bookmarkletify
29 * @return string Bookmarklet link
31 public function crunch( $code ) {
32 $out = $code = "(function() {\n" . $code . "\n})();";
34 $out = $this->replace_strings( $out );
35 $out = $this->kill_comments( $out );
36 $out = $this->compress_white_space( $out );
37 $out = $this->combine_strings( $out );
38 $out = $this->restore_strings( $out );
39 $out = $this->encodeURIComponent( $out );
40 $out = 'javascript:' . $out;
46 * PHP port of Javascript function encodeURIComponent
48 * From http://stackoverflow.com/a/1734255/36850
51 * @param string $str String to encode
52 * @return string Encoded string
55 private function encodeURIComponent( $str ) {
57 '%21'=>'!', '%2A'=>'*', '%28'=>'(', '%29'=>')',
60 return strtr( rawurlencode( $str ), $revert );
64 * Kill comment lines and blocks
66 * @param string $code Commented Javascript code
67 * @return string Commentless code
69 private function kill_comments( $code ) {
70 $code = preg_replace( '!\s*//.+$!m', '', $code );
71 $code = preg_replace( '!/\*.+?\*/!sm', '', $code ); // s modifier: dot matches new lines
77 * Compress white space
79 * Remove some extraneous spaces and make the whole script a one liner
81 * @param string $code Javascript code
82 * @return string Compressed code
84 private function compress_white_space( $code ) {
85 // Tabs to space, no more than 1 consecutive space
86 $code = preg_replace( '!\t!m', ' ', $code );
87 $code = preg_replace( '![ ]{2,}!m', ' ', $code );
89 // Remove uneccessary white space around operators, braces and brackets.
90 // \xHH sequence is: !%&()*+,-/:;<=>?[]\{|}~
91 $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 );
92 $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 );
94 // Split on each line, trim leading/trailing white space, kill empty lines, combine everything in one line
95 $code = preg_split( '/\r\n|\r|\n/', $code );
96 foreach( $code as $i => $line ) {
97 $code[ $i ] = trim( $line );
99 $code = implode( '', $code );
105 * Combine any consecutive strings
107 * In the case we have two consecutive quoted strings (eg: "hello" + "world"), save a couple more
108 * length and combine them
110 * @param string $code Javascript code
111 * @return string Javascript code
113 private function combine_strings( $code ) {
114 $code = preg_replace('/"\+"/m', "", $code);
115 $code = preg_replace("/'\+'/m", "", $code);
122 * Replace all literal strings (eg: "hello world") with a placeholder and collect them in an array
124 * The idea is that strings cannot be trimmed or white-space optimized: take them out first before uglifying
125 * the code, then we'll reinject them back in later
127 * @param string $code Javascript code
128 * @return string Javascript code with placeholders (eg "__1__") instead of literal strings
130 private function replace_strings( $code ) {
133 // Split script into individual lines.
134 $lines = explode("\n", $code);
135 for( $i = 0; $i < count( $lines ); $i++ ) {
139 while ($j < strlen( $lines[$i] ) ) {
140 $c = $lines[ $i ][ $j ];
142 // If not already in a string, look for the start of one.
144 if ($c == '"' || $c == "'") {
154 // Already in a string, look for end and copy characters.
156 if ($c == $quoteChar && !$escaped) {
158 $literal .= $quoteChar;
159 $return .= "__" . count( $this->literal_strings ) . "__";
160 $this->literal_strings[ count( $this->literal_strings ) ] = $literal;
162 else if ($c == "\\" && !$escaped)
177 * Restore literal strings by replacing their placeholders with actual strings
179 * @param string $code Javascript code with placeholders
180 * @return string Javascript code with actual strings
182 function restore_strings( $code ) {
183 foreach( $this->literal_strings as $i => $string ) {
184 $code = preg_replace( '/__' . $i . '__/', $string, $code, 1 );