1+ @import " rwd-variables" ;
2+
3+ @function _str-explode ($string , $delimiter : " " ) {
4+ $result : ();
5+ $length : str-length ($string );
6+
7+ @if str-length ($delimiter ) == 0 {
8+ @for $i from 1 through $length {
9+ $result : append ($result , str-slice ($string , $i , $i ));
10+ }
11+
12+ @return $result ;
13+ }
14+
15+ $running : true;
16+ $remaining : $string ;
17+
18+ @while $running {
19+ $index : str-index ($remaining , $delimiter );
20+
21+ @if $index {
22+ $slice : str-slice ($remaining , 1 , $index - 1 );
23+ $result : append ($result , $slice );
24+ $remaining : str-slice ($remaining , $index + str-length ($delimiter ));
25+ } @else {
26+ $running : false;
27+ }
28+ }
29+
30+ @return append ($result , $remaining );
31+ }
32+
33+
34+ @function get-expression-operator ($expression ) {
35+ @each $operator in (' >=' , ' <=' , ' >' , ' <' , ' ≥' , ' ≤' , ' =' ) {
36+ @if str-index ($expression , $operator ) {
37+ @return $operator ;
38+ }
39+ }
40+
41+ @return ' ' ;
42+ }
43+
44+ @function get-expression-value ($expression , $operator ) {
45+ $operator-index : str-index ($expression , $operator );
46+ $value : str-slice ($expression , $operator-index + str-length ($operator ));
47+ @return $value ;
48+ }
49+
50+ @function get-width-string ($operator , $min : 0 , $max : 0 ) {
51+ @if $operator == ' ≥' { $operator : ' >=' ; }
52+ @if $operator == ' ≤' { $operator : ' <=' ; }
53+ @if $operator == ' =' {
54+ @if $max > 0 {
55+ @return ' (min-width: ' + $min + ' ) and (max-width: ' + $max + ' )' ;
56+ } @else {
57+ @return ' (min-width: ' + $min + ' )' ;
58+ }
59+ } @else if $operator == ' <' {
60+ @return ' (max-width: ' + ($min - 1 ) + ' )' ;
61+ } @else if $operator == ' >' {
62+ @return ' (min-width: ' + ($max + 1 ) + ' )' ;
63+ } @else if $operator == ' <=' {
64+ @return ' (max-width: ' + $max + ' )' ;
65+ } @else if $operator == ' >=' {
66+ @return ' (min-width: ' + $min + ' )' ;
67+ }
68+
69+ @return ' ' ;
70+ }
71+
72+ @function get-condition-string ($viewport ,$operator ) {
73+ @each $class , $min in $grid-breakpoints {
74+ @if ($class == $viewport ) {
75+ $max : map-get-next ($grid-breakpoints ,$class , 0 );
76+ @return get-width-string ($operator ,$min ,$max );
77+ }
78+ }
79+
80+ @return ' ' ;
81+ }
82+
83+ @function is-media-type ($string ) {
84+ @if type-of ($string ) == string {
85+ $types : (all , print, screen , speech , tty, tv, projection, handheld, braille, embossed, aural);
86+ @for $i from 1 through length ($types ) {
87+ $type : nth ($types ,$i );
88+ @if $string == $type {
89+ @return true;
90+ }
91+ }
92+ }
93+
94+ @return false;
95+ }
96+
97+ @function add-condition ($conditions-string , $new-string ) {
98+ @if type-of ($conditions-string ) == null {
99+ @if not (is-media-type ($new-string )) {
100+ $conditions-string : unquote (" all" );
101+ }
102+ }
103+ }
104+
105+ @function _media ($conditions ... ) {
106+ $compiled-list : ();
107+ @for $i from 1 through length ($conditions ) {
108+ $condition : unquote (quote (nth ($conditions ,$i )));
109+
110+ // Basic String Conditions
111+ @if map-has-key ($condition-map , $condition ) {
112+ $compiled-list : append ( $compiled-list , map-get ( $condition-map ,$condition ), comma );
113+ } @else {
114+ $operator : get-expression-operator ($condition );
115+ $value : get-expression-value ($condition ,$operator );
116+ @if map-has-key ($grid-breakpoints , $value ) {
117+ // Breakpoint Based Conditions
118+ $compiled-list : append ($compiled-list , get-condition-string ($value , $operator ), comma );
119+ } @else {
120+ // Width Based Condition
121+ $compiled-list : append ($compiled-list , get-width-string ($operator ,$value ,$value ), comma );
122+ }
123+ }
124+ }
125+
126+ $conditions-string : ' ' ;
127+ @for $i from 1 through length ($compiled-list ) {
128+ $this : nth ($compiled-list , $i );
129+ $last : if ($i > 1 , nth ($compiled-list , $i - 1 ), null );
130+ $sep : ' ' ;
131+
132+ @if $i == 1 and $conditions-string == ' ' and not (is-media-type ($this )) {
133+ $last : all ;
134+ $conditions-string : all ;
135+ }
136+
137+ @if $conditions-string != ' ' {
138+ @if is-media-type ($last ) and not (is-media-type ($this )) {
139+ $sep : unquote (" and " );
140+ } @else {
141+ $sep : unquote (" , " );
142+ }
143+ }
144+
145+ $conditions-string : $conditions-string + $sep + $this ;
146+ }
147+
148+ @return $conditions-string ;
149+ }
150+
151+ @function _browser-target ($browser , $version : null , $negate : false ) {
152+ $sep : ' ' ;
153+ @if map-has-key ($target-user-agent ,$browser ) {
154+ $ua : unquote (map-get ($target-user-agent , $browser ));
155+ $sep : if ($version ,' /' ,' ' );
156+ $ua-sel : unquote (' [data-user-agent="#{$ua }#{$sep }#{$version } "]' );
157+
158+ @return if ($negate ,unquote (" :not(#{$ua-sel } )" ),$ua-sel );
159+ }
160+
161+ @return null;
162+ }
163+
164+ @function _feature-target ($feature , $negate : false ) {
165+ @if map-has-key ($target-feature , $feature ) {
166+ $sel : unquote (map-get ($target-feature , $feature ));
167+
168+ @return if ($negate , unquote (" :not(#{$sel } )" ), $sel );
169+ }
170+
171+ @return null;
172+ }
173+
174+
175+ @function _target ($targets ... ) {
176+ $class : ' ' ;
177+ $ua : ' ' ;
178+ @for $i from 1 through length ($targets ) {
179+ $list : _str-explode (nth ($targets ,$i )," " );
180+ $negate : if (nth ($list ,1 ) == unquote (" not" ), true , false );
181+ $criteria : if ($negate , nth ($list ,2 ), nth ($list ,1 ));
182+
183+ @if (map-has-key ($target-user-agent ,$criteria )) {
184+ $browser : $criteria ;
185+ $v-index : if ($negate , 3 , 2 );
186+ $version : if (length ($list ) >= $v-index , nth ($list ,$v-index ), null );
187+ $ua : $ua + _browser-target ( $criteria , $version , $negate );
188+ } @else {
189+ $class : $class + _feature-target ( $criteria , $negate );
190+ }
191+ }
192+
193+ @return unquote (' html#{$class }#{$ua } ' );
194+ }
0 commit comments