Debian Codegraph @

sass-spec/spec/libsass-closed-issues/issue_2520/input.scss

raw
1 // ----
2 // Sass (v3.4.21)
3 // Compass (v1.0.3)
4 // ----
5
6 @function remove-modifiers($selector) {
7 // convert selector to a string
8 $selector: inspect(nth($selector, 1));
9
10 $modifier: '';
11
12 // Find out where the first modifier starts
13 $modifier-index: str-index($selector, '"--');
14
15 @if $modifier-index {
16 // Strip the first part of the selector up until the first modifier
17 $modifier: str-slice($selector, $modifier-index);
18 // Find out where the modifier ends
19 $modifier-end: str-index($modifier, '"]');
20 // Isolate the modifier
21 $modifier: str-slice($modifier, 0, $modifier-end);
22 // Remove the modifier from the selector
23 $selector: str-replace($selector, $modifier, '');
24 // Remove junk characters
25 $selector: str-replace($selector, '[class*=]', '');
26 // Recurse the function to eliminate any remainig modifiers
27 $selector: remove-modifiers($selector);
28 }
29
30 @return $selector;
31 }
32
33 @function remove-duplicates($list, $recursive: false) {
34 $result: ();
35
36 @each $item in $list {
37 @if not index($result, $item) {
38 @if length($item) > 1 and $recursive {
39 $result: append($result, remove-duplicates($item, $recursive));
40 }
41 @else {
42 $result: append($result, $item);
43 }
44 }
45 }
46
47 @return $result;
48 }
49
50 @function str-replace($string, $search, $replace) {
51 $index: str-index($string, $search);
52
53 @if $index {
54 @return str-slice($string, 1, $index - 1) + $replace + str-replace(
55 str-slice($string, $index + str-length($search)), $search, $replace
56 );
57 }
58
59 @return $string;
60 }
61
62 @function module-tree($selector) {
63 $parent-module: $module;
64
65 // Remove any modifers
66 $selectors: remove-modifiers($selector);
67
68 // Remove any junk characters
69 $selectors: str-replace($selectors, '.', '');
70 $selectors: str-replace($selectors, '[class*="--', '');
71 $selectors: str-replace($selectors, '[class*="', '');
72 $selectors: str-replace($selectors, '--"]', '');
73 $selectors: str-replace($selectors, '"]', '');
74
75 // Spoof our modules into a list
76 $selectors: str-replace($selectors, ' ', ', ');
77 $selectors: selector-parse($selectors);
78
79 @return $selectors;
80 }
81
82 @function list-remove($list, $value, $recursive: false) {
83 $result: ();
84
85 @for $i from 1 through length($list) {
86 @if type-of(nth($list, $i)) == list and $recursive {
87 $result: append($result, list-remove(nth($list, $i), $value, $recursive), comma);
88 } @else if nth($list, $i) != $value {
89 $result: append($result, nth($list, $i), comma);
90 }
91 }
92
93 @return $result;
94 }
95
96 @function this($options...) {
97 $value: map-get($config, $options...);
98 $debug: true;
99
100 $this: &;
101
102 @if length($this) > 0 {
103 @if str-index(inspect(nth($this, 1)), '%') == 1 {
104 $debug: false;
105 }
106 }
107
108 @if $debug and not $value and $value != false {
109 @warn '#{$options} not found in #{$module} config';
110 }
111
112 @return $value;
113 }
114
115 @function config($map-old, $map-new) {
116 // Merge default and custom options
117 $map-merged: map-merge($map-old, $map-new);
118
119 // Store config in global variable
120 $config: $map-merged !global;
121
122 // Return merged map
123 @return $map-merged;
124 }
125
126 @mixin module($module: $module) {
127 $nested: &;
128
129 @if not $nested {
130 $module: $module !global;
131 }
132
133 $selectors: ();
134
135 @each $item in $module {
136 $selectors: join($selectors, '.#{$module}', comma);
137 $selectors: join($selectors, '[class*="#{$module}--"]', comma);
138 }
139
140 #{$selectors} {
141 @content;
142 }
143 }
144
145 @mixin component($components: null, $glue: '__') {
146 $selectors: '[class*="#{$module}#{$glue}"]';
147 $this: &;
148 $tree: module-tree($this);
149 $namespace: nth($tree, length($tree));
150
151 @if $components {
152 $selectors: ();
153
154 @each $component in $components {
155 $selectors: join(
156 $selectors,
157 '.#{$namespace}#{$glue}#{$component}, [class*="#{$namespace}#{$glue}#{$component}-"]',
158 comma
159 );
160 }
161 }
162
163 $parents: ();
164
165 @each $selector in & {
166 // spoof the selector into a list
167 $selector: str-replace(inspect($selector), ' ', ', ');
168 $selector: selector-parse($selector);
169
170 // if the last item isn't a modifier, remove it
171 @if not str-index(inspect(nth($selector, length($selector))), '[class*="--') {
172 $selector: list-remove($selector, nth($selector, length($selector)));
173 }
174
175 // convert the selector back into a string
176 @if length($selector) == 1 {
177 $selector: nth($selector, 1);
178 }
179 $selector: str-replace(inspect($selector), ', ', ' ');
180
181 // Re-build the parent selector
182 $parents: append($parents, $selector, comma);
183 }
184
185 // remove duplicate selectors
186 $parents: remove-duplicates($parents);
187
188 @if length($parents) == 1 {
189 $parents: nth($parents, 1);
190 }
191
192 @if ($parents == '()') {
193 @at-root #{$selectors} {
194 @content;
195 }
196 } @else {
197 @at-root #{$parents} {
198 #{$selectors} {
199 @content;
200 }
201 }
202 }
203
204 }
205
206 @mixin modifier($modifier) {
207 $selectors: &;
208
209 @if str-index(inspect(&), '.#{$module}') {
210 $selectors: ();
211
212 @for $i from 1 through length(&) {
213 @if $i % 2 == 0 {
214 $selectors: append($selectors, nth(&, $i), comma);
215 }
216 }
217 }
218
219 @at-root #{$selectors} {
220 $modifier-selectors: ();
221
222 @each $item in $modifier {
223 $modifier-selectors: join(
224 $modifier-selectors, '&[class*="--#{$modifier}"]', comma
225 );
226 }
227
228 #{$modifier-selectors} {
229 @content;
230 }
231 }
232 }
233
234 @mixin button($custom:()) {
235 $buttons: config((
236 'group-spacing': 1em
237 ), $custom);
238
239 @include module('button') {
240 @include component('group') {
241 content: 'fizzbuzz';
242 @include module {
243 margin-left: this('group-spacing');
244 &:first-child {
245 margin-left: 0;
246 }
247 }
248 }
249 }
250 }
251
252 @include button();
253
254 @include module('modal') {
255 @include modifier('animate') {
256 @include modifier('zoom') {
257 content: "fizzbuzz"
258 }
259 }
260 }