@@ -49,9 +49,10 @@ module.exports = postcss.plugin('postcss-dir-pseudo-class', (opts) => (root) =>
4949 // conditionally prepend a combinator before inserting the [dir] attribute
5050 const first = selector . nodes [ 0 ] ;
5151 const firstIsSpaceCombinator = first && 'combinator' === first . type && ' ' === first . value ;
52+ const firstIsHtml = first && 'tag' === first . type && 'html' === first . value ;
5253 const firstIsRoot = first && 'pseudo' === first . type && ':root' === first . value ;
5354
54- if ( first && ! firstIsRoot && ! firstIsSpaceCombinator ) {
55+ if ( first && ! firstIsHtml && ! firstIsRoot && ! firstIsSpaceCombinator ) {
5556 selector . prepend (
5657 selectorParser . combinator ( {
5758 value : ' '
@@ -65,18 +66,42 @@ module.exports = postcss.plugin('postcss-dir-pseudo-class', (opts) => (root) =>
6566 // whether that value matches the presumed direction
6667 const isdir = opts && Object ( opts ) . dir === value ;
6768
68- selector . prepend (
69- // prepend :root if the direction is presumed
70- isdir ? selectorParser . pseudo ( {
71- value : ':root'
72- } )
73- // otherwise, prepend the dir attribute
74- : selectorParser . attribute ( {
69+ // [dir] attribute
70+ const dirAttr = selectorParser . attribute ( {
71+ attribute : 'dir' ,
72+ operator : '=' ,
73+ value : `"${ value } "`
74+ } ) ;
75+
76+ // not[dir] attribute
77+ const notDirAttr = selectorParser . pseudo ( {
78+ value : ':not'
79+ } ) ;
80+
81+ notDirAttr . append (
82+ selectorParser . attribute ( {
7583 attribute : 'dir' ,
76- operator : '=' ,
77- value : `"${ value } "`
84+ operator : '=' ,
85+ value : `"${ 'ltr' === value ? 'rtl' : 'ltr' } "`
7886 } )
7987 ) ;
88+
89+ if ( isdir ) {
90+ // if the direction is presumed
91+ if ( firstIsHtml ) {
92+ // insert :root after html tag
93+ selector . insertAfter ( first , notDirAttr ) ;
94+ } else {
95+ // prepend :root
96+ selector . prepend ( notDirAttr ) ;
97+ }
98+ } else if ( firstIsHtml ) {
99+ // otherwise, insert dir attribute after html tag
100+ selector . insertAfter ( first , dirAttr ) ;
101+ } else {
102+ // otherwise, prepend the dir attribute
103+ selector . prepend ( dirAttr ) ;
104+ }
80105 }
81106 } ) ;
82107 } ) ;
0 commit comments