1 | <?php |
||
21 | class Bookmarkletgen { |
||
22 | |||
23 | private $literal_strings = array(); |
||
24 | |||
25 | /** |
||
26 | * Main function, calls all others |
||
27 | * |
||
28 | * @param string $code Javascript code to bookmarkletify |
||
29 | * @return string Bookmarklet link |
||
30 | */ |
||
31 | public function crunch( $code ) { |
||
44 | |||
45 | /** |
||
46 | * PHP port of Javascript function encodeURIComponent |
||
47 | * |
||
48 | * From http://stackoverflow.com/a/1734255/36850 |
||
49 | * |
||
50 | * @since |
||
51 | * @param string $str String to encode |
||
52 | * @return string Encoded string |
||
53 | */ |
||
54 | // |
||
55 | private function encodeURIComponent( $str ) { |
||
62 | |||
63 | /** |
||
64 | * Kill comment lines and blocks |
||
65 | * |
||
66 | * @param string $code Commented Javascript code |
||
67 | * @return string Commentless code |
||
68 | */ |
||
69 | private function kill_comments( $code ) { |
||
75 | |||
76 | /** |
||
77 | * Compress white space |
||
78 | * |
||
79 | * Remove some extraneous spaces and make the whole script a one liner |
||
80 | * |
||
81 | * @param string $code Javascript code |
||
82 | * @return string Compressed code |
||
83 | */ |
||
84 | private function compress_white_space( $code ) { |
||
103 | |||
104 | /** |
||
105 | * Combine any consecutive strings |
||
106 | * |
||
107 | * In the case we have two consecutive quoted strings (eg: "hello" + "world"), save a couple more |
||
108 | * length and combine them |
||
109 | * |
||
110 | * @param string $code Javascript code |
||
111 | * @return string Javascript code |
||
112 | */ |
||
113 | private function combine_strings( $code ) { |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Replace all literal strings (eg: "hello world") with a placeholder and collect them in an array |
||
123 | * |
||
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 |
||
126 | * |
||
127 | * @param string $code Javascript code |
||
128 | * @return string Javascript code with placeholders (eg "__1__") instead of literal strings |
||
129 | */ |
||
130 | private function replace_strings( $code ) { |
||
182 | |||
183 | /** |
||
184 | * Restore literal strings by replacing their placeholders with actual strings |
||
185 | * |
||
186 | * @param string $code Javascript code with placeholders |
||
187 | * @return string Javascript code with actual strings |
||
188 | */ |
||
189 | private function restore_strings( $code ) { |
||
196 | |||
197 | } |
||
198 |