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