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