MediaWiki
REL1_19
|
00001 <?php 00034 class CSSJanus { 00035 // Patterns defined as null are built dynamically by buildPatterns() 00036 private static $patterns = array( 00037 'tmpToken' => '`TMP`', 00038 'nonAscii' => '[\200-\377]', 00039 'unicode' => '(?:(?:\\[0-9a-f]{1,6})(?:\r\n|\s)?)', 00040 'num' => '(?:[0-9]*\.[0-9]+|[0-9]+)', 00041 'unit' => '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)', 00042 'body_selector' => 'body\s*{\s*', 00043 'direction' => 'direction\s*:\s*', 00044 'escape' => null, 00045 'nmstart' => null, 00046 'nmchar' => null, 00047 'ident' => null, 00048 'quantity' => null, 00049 'possibly_negative_quantity' => null, 00050 'color' => null, 00051 'url_special_chars' => '[!#$%&*-~]', 00052 'valid_after_uri_chars' => '[\'\"]?\s*', 00053 'url_chars' => null, 00054 'lookahead_not_open_brace' => null, 00055 'lookahead_not_closing_paren' => null, 00056 'lookahead_for_closing_paren' => null, 00057 'lookbehind_not_letter' => '(?<![a-zA-Z])', 00058 'chars_within_selector' => '[^\}]*?', 00059 'noflip_annotation' => '\/\*\s*@noflip\s*\*\/', 00060 'noflip_single' => null, 00061 'noflip_class' => null, 00062 'comment' => '/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//', 00063 'direction_ltr' => null, 00064 'direction_rtl' => null, 00065 'left' => null, 00066 'right' => null, 00067 'left_in_url' => null, 00068 'right_in_url' => null, 00069 'ltr_in_url' => null, 00070 'rtl_in_url' => null, 00071 'cursor_east' => null, 00072 'cursor_west' => null, 00073 'four_notation_quantity' => null, 00074 'four_notation_color' => null, 00075 'bg_horizontal_percentage' => null, 00076 'bg_horizontal_percentage_x' => null, 00077 ); 00078 00082 private static function buildPatterns() { 00083 if ( !is_null( self::$patterns['escape'] ) ) { 00084 // Patterns have already been built 00085 return; 00086 } 00087 00088 $patterns =& self::$patterns; 00089 $patterns['escape'] = "(?:{$patterns['unicode']}|\\[^\r\n\f0-9a-f])"; 00090 $patterns['nmstart'] = "(?:[_a-z]|{$patterns['nonAscii']}|{$patterns['escape']})"; 00091 $patterns['nmchar'] = "(?:[_a-z0-9-]|{$patterns['nonAscii']}|{$patterns['escape']})"; 00092 $patterns['ident'] = "-?{$patterns['nmstart']}{$patterns['nmchar']}*"; 00093 $patterns['quantity'] = "{$patterns['num']}(?:\s*{$patterns['unit']}|{$patterns['ident']})?"; 00094 $patterns['possibly_negative_quantity'] = "((?:-?{$patterns['quantity']})|(?:inherit|auto))"; 00095 $patterns['color'] = "(#?{$patterns['nmchar']}+)"; 00096 $patterns['url_chars'] = "(?:{$patterns['url_special_chars']}|{$patterns['nonAscii']}|{$patterns['escape']})*"; 00097 $patterns['lookahead_not_open_brace'] = "(?!({$patterns['nmchar']}|\r?\n|\s|#|\:|\.|\,|\+|>)*?{)"; 00098 $patterns['lookahead_not_closing_paren'] = "(?!{$patterns['url_chars']}?{$patterns['valid_after_uri_chars']}\))"; 00099 $patterns['lookahead_for_closing_paren'] = "(?={$patterns['url_chars']}?{$patterns['valid_after_uri_chars']}\))"; 00100 $patterns['noflip_single'] = "/({$patterns['noflip_annotation']}{$patterns['lookahead_not_open_brace']}[^;}]+;?)/i"; 00101 $patterns['noflip_class'] = "/({$patterns['noflip_annotation']}{$patterns['chars_within_selector']}})/i"; 00102 $patterns['direction_ltr'] = "/({$patterns['direction']})ltr/i"; 00103 $patterns['direction_rtl'] = "/({$patterns['direction']})rtl/i"; 00104 $patterns['left'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i"; 00105 $patterns['right'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i"; 00106 $patterns['left_in_url'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_for_closing_paren']}/i"; 00107 $patterns['right_in_url'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_for_closing_paren']}/i"; 00108 $patterns['ltr_in_url'] = "/{$patterns['lookbehind_not_letter']}(ltr){$patterns['lookahead_for_closing_paren']}/i"; 00109 $patterns['rtl_in_url'] = "/{$patterns['lookbehind_not_letter']}(rtl){$patterns['lookahead_for_closing_paren']}/i"; 00110 $patterns['cursor_east'] = "/{$patterns['lookbehind_not_letter']}([ns]?)e-resize/"; 00111 $patterns['cursor_west'] = "/{$patterns['lookbehind_not_letter']}([ns]?)w-resize/"; 00112 $patterns['four_notation_quantity'] = "/{$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}/i"; 00113 $patterns['four_notation_color'] = "/(-color\s*:\s*){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}/i"; 00114 // The two regexes below are parenthesized differently then in the original implementation to make the 00115 // callback's job more straightforward 00116 $patterns['bg_horizontal_percentage'] = "/(background(?:-position)?\s*:\s*[^%]*?)(-?{$patterns['num']})(%\s*(?:{$patterns['quantity']}|{$patterns['ident']}))/"; 00117 $patterns['bg_horizontal_percentage_x'] = "/(background-position-x\s*:\s*)(-?{$patterns['num']})(%)/"; 00118 } 00119 00127 public static function transform( $css, $swapLtrRtlInURL = false, $swapLeftRightInURL = false ) { 00128 // We wrap tokens in ` , not ~ like the original implementation does. 00129 // This was done because ` is not a legal character in CSS and can only 00130 // occur in URLs, where we escape it to %60 before inserting our tokens. 00131 $css = str_replace( '`', '%60', $css ); 00132 00133 self::buildPatterns(); 00134 00135 // Tokenize single line rules with /* @noflip */ 00136 $noFlipSingle = new CSSJanus_Tokenizer( self::$patterns['noflip_single'], '`NOFLIP_SINGLE`' ); 00137 $css = $noFlipSingle->tokenize( $css ); 00138 00139 // Tokenize class rules with /* @noflip */ 00140 $noFlipClass = new CSSJanus_Tokenizer( self::$patterns['noflip_class'], '`NOFLIP_CLASS`' ); 00141 $css = $noFlipClass->tokenize( $css ); 00142 00143 // Tokenize comments 00144 $comments = new CSSJanus_Tokenizer( self::$patterns['comment'], '`C`' ); 00145 $css = $comments->tokenize( $css ); 00146 00147 // LTR->RTL fixes start here 00148 $css = self::fixDirection( $css ); 00149 if ( $swapLtrRtlInURL ) { 00150 $css = self::fixLtrRtlInURL( $css ); 00151 } 00152 00153 if ( $swapLeftRightInURL ) { 00154 $css = self::fixLeftRightInURL( $css ); 00155 } 00156 $css = self::fixLeftAndRight( $css ); 00157 $css = self::fixCursorProperties( $css ); 00158 $css = self::fixFourPartNotation( $css ); 00159 $css = self::fixBackgroundPosition( $css ); 00160 00161 // Detokenize stuff we tokenized before 00162 $css = $comments->detokenize( $css ); 00163 $css = $noFlipClass->detokenize( $css ); 00164 $css = $noFlipSingle->detokenize( $css ); 00165 00166 return $css; 00167 } 00168 00181 private static function fixDirection( $css ) { 00182 $css = preg_replace( self::$patterns['direction_ltr'], 00183 '$1' . self::$patterns['tmpToken'], $css ); 00184 $css = preg_replace( self::$patterns['direction_rtl'], '$1ltr', $css ); 00185 $css = str_replace( self::$patterns['tmpToken'], 'rtl', $css ); 00186 00187 return $css; 00188 } 00189 00195 private static function fixLtrRtlInURL( $css ) { 00196 $css = preg_replace( self::$patterns['ltr_in_url'], self::$patterns['tmpToken'], $css ); 00197 $css = preg_replace( self::$patterns['rtl_in_url'], 'ltr', $css ); 00198 $css = str_replace( self::$patterns['tmpToken'], 'rtl', $css ); 00199 00200 return $css; 00201 } 00202 00208 private static function fixLeftRightInURL( $css ) { 00209 $css = preg_replace( self::$patterns['left_in_url'], self::$patterns['tmpToken'], $css ); 00210 $css = preg_replace( self::$patterns['right_in_url'], 'left', $css ); 00211 $css = str_replace( self::$patterns['tmpToken'], 'right', $css ); 00212 00213 return $css; 00214 } 00215 00221 private static function fixLeftAndRight( $css ) { 00222 $css = preg_replace( self::$patterns['left'], self::$patterns['tmpToken'], $css ); 00223 $css = preg_replace( self::$patterns['right'], 'left', $css ); 00224 $css = str_replace( self::$patterns['tmpToken'], 'right', $css ); 00225 00226 return $css; 00227 } 00228 00234 private static function fixCursorProperties( $css ) { 00235 $css = preg_replace( self::$patterns['cursor_east'], 00236 '$1' . self::$patterns['tmpToken'], $css ); 00237 $css = preg_replace( self::$patterns['cursor_west'], '$1e-resize', $css ); 00238 $css = str_replace( self::$patterns['tmpToken'], 'w-resize', $css ); 00239 00240 return $css; 00241 } 00242 00255 private static function fixFourPartNotation( $css ) { 00256 $css = preg_replace( self::$patterns['four_notation_quantity'], '$1$2$7$4$5$6$3', $css ); 00257 $css = preg_replace( self::$patterns['four_notation_color'], '$1$2$3$8$5$6$7$4', $css ); 00258 00259 return $css; 00260 } 00261 00267 private static function fixBackgroundPosition( $css ) { 00268 $css = preg_replace_callback( self::$patterns['bg_horizontal_percentage'], 00269 array( 'self', 'calculateNewBackgroundPosition' ), $css ); 00270 $css = preg_replace_callback( self::$patterns['bg_horizontal_percentage_x'], 00271 array( 'self', 'calculateNewBackgroundPosition' ), $css ); 00272 00273 return $css; 00274 } 00275 00281 private static function calculateNewBackgroundPosition( $matches ) { 00282 return $matches[1] . ( 100 - $matches[2] ) . $matches[3]; 00283 } 00284 } 00285 00291 class CSSJanus_Tokenizer { 00292 private $regex, $token; 00293 private $originals; 00294 00300 public function __construct( $regex, $token ) { 00301 $this->regex = $regex; 00302 $this->token = $token; 00303 $this->originals = array(); 00304 } 00305 00312 public function tokenize( $str ) { 00313 return preg_replace_callback( $this->regex, array( $this, 'tokenizeCallback' ), $str ); 00314 } 00315 00320 private function tokenizeCallback( $matches ) { 00321 $this->originals[] = $matches[0]; 00322 return $this->token; 00323 } 00324 00331 public function detokenize( $str ) { 00332 // PHP has no function to replace only the first occurrence or to 00333 // replace occurrences of the same string with different values, 00334 // so we use preg_replace_callback() even though we don't really need a regex 00335 return preg_replace_callback( '/' . preg_quote( $this->token, '/' ) . '/', 00336 array( $this, 'detokenizeCallback' ), $str ); 00337 } 00338 00343 private function detokenizeCallback( $matches ) { 00344 $retval = current( $this->originals ); 00345 next( $this->originals ); 00346 00347 return $retval; 00348 } 00349 }