Skip to content

Commit e1351e6

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. Bug: T371466 Change-Id: Ifb38c598c3147689d084ec2535849a8224b794a6
1 parent 3e4afb8 commit e1351e6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/cssjanus.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function CSSJanus() {
9696
var
9797
// Tokens
9898
temporaryToken = '`TMP`',
99+
temporaryLtrToken = '`TMPLTR`',
100+
temporaryRtlToken = '`TMPRTL`',
99101
noFlipSingleToken = '`NOFLIP_SINGLE`',
100102
noFlipClassToken = '`NOFLIP_CLASS`',
101103
commentToken = '`COMMENT`',
@@ -139,6 +141,8 @@ function CSSJanus() {
139141
suffixPattern = '(\\s*(?:!important\\s*)?[;}])',
140142
// Regular expressions
141143
temporaryTokenRegExp = /`TMP`/g,
144+
temporaryLtrTokenRegExp = /`TMPLTR`/g,
145+
temporaryRtlTokenRegExp = /`TMPRTL`/g,
142146
commentRegExp = new RegExp( commentPattern, 'gi' ),
143147
noFlipSingleRegExp = new RegExp( '(' + noFlipPattern + lookAheadNotOpenBracePattern + '[^;}]+;?)', 'gi' ),
144148
noFlipClassRegExp = new RegExp( '(' + noFlipPattern + charsWithinSelectorPattern + '})', 'gi' ),
@@ -148,6 +152,8 @@ function CSSJanus() {
148152
rightRegExp = new RegExp( nonLetterPattern + '(right)' + lookAheadNotLetterPattern + lookAheadNotClosingParenPattern + lookAheadNotOpenBracePattern, 'gi' ),
149153
leftInUrlRegExp = new RegExp( nonLetterPattern + '(left)' + lookAheadForClosingParenPattern, 'gi' ),
150154
rightInUrlRegExp = new RegExp( nonLetterPattern + '(right)' + lookAheadForClosingParenPattern, 'gi' ),
155+
ltrDirSelector = /(:dir\( *)ltr( *\))/g,
156+
rtlDirSelector = /(:dir\( *)rtl( *\))/g,
151157
ltrInUrlRegExp = new RegExp( nonLetterPattern + '(ltr)' + lookAheadForClosingParenPattern, 'gi' ),
152158
rtlInUrlRegExp = new RegExp( nonLetterPattern + '(rtl)' + lookAheadForClosingParenPattern, 'gi' ),
153159
cursorEastRegExp = new RegExp( nonLetterPattern + '([ns]?)e-resize', 'gi' ),
@@ -348,9 +354,13 @@ function CSSJanus() {
348354
if ( options.transformDirInUrl ) {
349355
// Replace 'ltr' with 'rtl' and vice versa in background URLs
350356
css = css
357+
.replace( ltrDirSelector, '$1' + temporaryLtrToken + '$2' )
358+
.replace( rtlDirSelector, '$1' + temporaryRtlToken + '$2' )
351359
.replace( ltrInUrlRegExp, '$1' + temporaryToken )
352360
.replace( rtlInUrlRegExp, '$1ltr' )
353-
.replace( temporaryTokenRegExp, 'rtl' );
361+
.replace( temporaryTokenRegExp, 'rtl' )
362+
.replace( temporaryLtrTokenRegExp, 'ltr' )
363+
.replace( temporaryRtlTokenRegExp, 'rtl' );
354364
}
355365
if ( options.transformEdgeInUrl ) {
356366
// Replace 'left' with 'right' and vice versa in background URLs

test/data.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,10 @@
904904
]
905905
},
906906
"do not touch CSS Logical": {
907+
"options": {
908+
"transformDirInUrl": true,
909+
"transformEdgeInUrl": true
910+
},
907911
"cases": [
908912
[
909913
"ul { margin-top: 0.3em; margin-right: 0; margin-bottom: 0; margin-left: 1.6em; margin-inline-start: 1.6em; margin-inline-end: 0; padding: 0; }",
@@ -925,6 +929,10 @@
925929
]
926930
},
927931
"do not touch dir attribute selector and dir pseudo-class selector": {
932+
"options": {
933+
"transformDirInUrl": true,
934+
"transformEdgeInUrl": true
935+
},
928936
"cases": [
929937
[
930938
"div[dir=rtl] { padding: 1em } html[ dir='ltr' ] { padding: 1em } div[dir=\"rtl\"] { padding: 1em }"

0 commit comments

Comments
 (0)