Skip to content

Commit c5868a5

Browse files
committed
Don't change dir pseudo-selector target
CSSJanus is taking :dir(ltr) as an URL, this fixes it by first turning them into two temporarily tokens, then proceeding with URL change logic, then putting back the original values, a similar pattern done to swap ltr and rtl in the same place. This is porting back Ifb38c598c3147689d084ec2535849a8224b794a6 to PHP. Bug: T371466 Change-Id: I9198294c6bd98c9c064f461e251778185161abcc
1 parent 81afe9f commit c5868a5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/CSSJanus.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*/
2929
class CSSJanus {
3030
private const TOKEN_TMP = '`TMP`';
31+
private const TOKEN_LTR_TMP = '`TMPLTR`';
32+
private const TOKEN_RTL_TMP = '`TMPRTL`';
3133
private const TOKEN_COMMENT = '`COMMENT`';
3234

3335
private static $patterns = null;
@@ -38,7 +40,6 @@ private static function buildPatterns() {
3840
}
3941
// Patterns defined as null are built dynamically
4042
$patterns = [
41-
'tmpToken' => '`TMP`',
4243
'nonAscii' => '[\200-\377]',
4344
'unicode' => '(?:(?:\\\\[0-9a-f]{1,6})(?:\r\n|\s)?)',
4445
'num' => '(?:[0-9]*\.[0-9]+|[0-9]+)',
@@ -71,6 +72,8 @@ private static function buildPatterns() {
7172
'right' => null,
7273
'left_in_url' => null,
7374
'right_in_url' => null,
75+
'ltr_dir_selector' => '/(:dir\( *)ltr( *\))/',
76+
'rtl_dir_selector' => '/(:dir\( *)rtl( *\))/',
7477
'ltr_in_url' => null,
7578
'rtl_in_url' => null,
7679
'cursor_east' => null,
@@ -238,9 +241,13 @@ private static function fixDirection( $css ) {
238241
* @return string
239242
*/
240243
private static function fixLtrRtlInURL( $css ) {
244+
$css = preg_replace( self::$patterns['ltr_dir_selector'], '$1' . self::TOKEN_LTR_TMP . '$2', $css );
245+
$css = preg_replace( self::$patterns['rtl_dir_selector'], '$1' . self::TOKEN_RTL_TMP . '$2', $css );
241246
$css = preg_replace( self::$patterns['ltr_in_url'], self::TOKEN_TMP, $css );
242247
$css = preg_replace( self::$patterns['rtl_in_url'], 'ltr', $css );
243248
$css = str_replace( self::TOKEN_TMP, 'rtl', $css );
249+
$css = str_replace( self::TOKEN_LTR_TMP, 'ltr', $css );
250+
$css = str_replace( self::TOKEN_RTL_TMP, 'rtl', $css );
244251

245252
return $css;
246253
}

test/suites/CSSJanusTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testTransform( $input, $args, $output, $name ) {
6262
protected static function getSpec() {
6363
static $json;
6464
if ( $json == null ) {
65-
$version = '2.2.0';
65+
$version = '2.3.0';
6666
$dir = dirname( __DIR__ );
6767
$file = "$dir/data-v$version.json";
6868
if ( !is_readable( $file ) ) {

0 commit comments

Comments
 (0)