-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
_selector-ext.scss
539 lines (497 loc) · 18.3 KB
/
_selector-ext.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@use 'sass:list';
@use 'sass:math';
@use 'sass:selector';
@use 'sass:string';
// A collection of extensions to the sass:selector module
// https://sass-lang.com/documentation/modules/selector
/// Recursively negates and flattens a compound selector.
///
/// This is useful for IE11 support when appending two selectors when the second
/// selector may have another nested `:not()`, since IE11 does not supported
/// complex selector lists within `:not()`.
///
/// @example - scss
/// $selector1: '.foo';
/// $selector2: '.bar:not(.baz)';
///
/// #{selector.append($selector1, ':not(#{$selector2})'} {
/// /* Modern browsers */
/// }
///
/// #{selector.append($selector1, negate($selector2)} {
/// /* IE11 support */
/// }
///
/// @example - css
/// .foo:not(.bar:not(.baz)) {
/// /* Modern browsers */
/// }
///
/// .foo:not(.bar), .foo.baz {
/// /* IE11 support */
/// }
///
/// @param {String} $compound-selector - A compound selector to negate.
/// @return {List} The negated selector in selector value format.
@function negate($compound-selector) {
$result: null;
@each $simple-selector in selector.simple-selectors($compound-selector) {
$to-append: null;
@if string.index($simple-selector, ':not(') == 1 {
$inside-not: string.slice($simple-selector, 6, -2);
$inside-not-simple-selectors: selector.simple-selectors($inside-not);
$inside-not-result: null;
@each $inside-not-simple-selector
in selector.simple-selectors($inside-not)
{
@if $inside-not-result == null {
// Skip the first simple selector, which has already been negated by
// removing :not() when parsing $inside-not.
$inside-not-result: selector.parse($inside-not-simple-selector);
} @else {
// Flatten nested :not()s ".foo:not(.bar:not(.baz))" (IE11 support)
@if string.index($inside-not-simple-selector, ':not(') == 1 {
$inside-not-result: list.join(
$inside-not-result,
_negate($inside-not-simple-selector)
);
} @else {
$inside-not-result: selector.append(
$inside-not-result,
$inside-not-simple-selector
);
}
}
}
$result: if(
$result,
list.join($result, $inside-not-result),
$inside-not-result
);
} @else {
$to-append: string.unquote(':not(#{$simple-selector})');
$result: if($result, selector.append($result, $to-append), $to-append);
}
}
@return $result;
}
/// Identical to `selector.append()`, but adheres strictly to CSS compound
/// selector order.
///
/// @example - scss
/// .foo::before {
/// &[dir=rtl] { /* Invalid */ }
/// }
///
/// .foo::before {
/// @include append-strict(&, '[dir=rtl]') { /* Valid */ }
/// }
///
/// @example - css
/// .foo::before[dir=rtl] {
/// /* Invalid */
/// }
///
/// .foo[dir=rtl]::before {
/// /* Valid */
/// }
///
/// This is useful for mixins where the parent selector is unknown and the
/// appended selector's position is critical to maintain valid CSS.
///
/// @param {List} $selectors - One or more selectors to append.
@mixin append-strict($selectors...) {
@at-root {
#{append-strict($selectors...)} {
@content;
}
}
}
/// Function version of `append-strict()`. Use this instead of the mixin along
/// with `@at-root` when combining the result of `append-strict()` with other
/// selectors.
///
/// @example - scss
/// .foo::before {
/// // Cannot add a list of other selectors with an @include mixin.
/// // @include append-strict(&, ':hover'), & {}
///
/// @at-root {
/// // Use @at-root and interpolation to add additional selectors
/// #{append-strict(&, ':hover')},
/// & {
/// color: inherit;
/// }
/// }
/// }
///
/// @example - css
/// .foo:hover::before,
/// .foo::before {
/// color: inherit;
/// }
///
/// @see {mixin} append-strict
///
/// @param {List} $selectors - One or more selectors to append.
/// @return {List} The appended selectors in selector value format.
@function append-strict($selectors...) {
$selector-lists: ();
@each $selector in $selectors {
$selector-lists: list.append($selector-lists, selector.parse($selector));
}
@return _append-strict($selector-lists);
}
/// Iterates through multiple selector Lists and strictly appends every
/// combination of each selector lists' complex selectors.
///
/// @see {mixin} _append-strict-complex-selectors
///
/// @param {List} $selector-lists - A List of selector lists to append.
/// @return {List} A single selector List resulting from appending all the
/// provided selector lists.
@function _append-strict($selector-lists) {
$length: list.length($selector-lists);
// Track the current index of each complex selector (within each selector
// list) that we are creating a combination of.
//
// Selectors: ((1), (2a, 2b), (3))
// Combinations: (1, 2a, 3), (1, 2b, 3)
//
// Initialize it to the first complex selector index for each selector list.
$current-indices: ();
@for $i from 1 through $length {
$current-indices: list.append($current-indices, 1);
}
// The final result: a single selector list resulting from appending the
// provided selector lists.
$selector-list-result: ();
$has-more-combinations: true;
@while $has-more-combinations {
// A combination of complex selectors to add to the selector list result.
$complex-selector-combination: ();
@for $i from 1 through $length {
$selector-list: list.nth($selector-lists, $i);
$current-index: list.nth($current-indices, $i);
$complex-selector: list.nth($selector-list, $current-index);
$complex-selector-combination: _append-strict-complex-selectors(
$complex-selector-combination,
$complex-selector
);
}
$selector-list-result: list.append(
$selector-list-result,
$complex-selector-combination,
$separator: comma
);
// Increase the index of the last selector list's complex selector to its
// next one. If it reaches the length of the array, reset to 1 and bump the
// next selector list index.
//
// Given selector lists: ((1), (2a, 2b), (3))
// At indices: ((1), (1), (1))
// Try bumping: ((1), (1), (2*)) *This index is >length of 1 for the list
// Bump the next: ((1), (2), (1))
$bump-next-list-index: true;
@for $neg-i from $length * -1 through -1 {
@if $bump-next-list-index {
$i: math.abs($neg-i);
$selector-list: list.nth($selector-lists, $i);
$current-index: list.nth($current-indices, $i);
$next-index: $current-index + 1;
@if $next-index > list.length($selector-list) {
// Reset to start for the list and bump the next list (technically
// previous since we're iterating backwards).
$next-index: 1;
$bump-next-list-index: true;
} @else {
// If we bumped to the next index for this selector list, we can
// "break" the loop and continue to form the next combination.
$bump-next-list-index: false;
}
// Save the new current index for this selector list.
$current-indices: list.set-nth($current-indices, $i, $next-index);
}
}
// When the first selector list reaches its length, it will ask to bump the
// next selector list index. There are no more selector lists, which means
// there are no more combinations and the loop may end.
@if $bump-next-list-index {
$has-more-combinations: false;
}
}
@return $selector-list-result;
}
/// Appends two complex selectors together, strictly adhering to the CSS
/// `<compound-selector>` type definition to avoid forming invalid resulting
/// compound selectors.
///
/// @param {List} $complex-selector-a - The first List of space-separated
/// compound selectors.
/// @param {List} $complex-selector-a - The second List of space-separated
/// compound selectors.
/// @return {List} A resulting appended complex selector.
@function _append-strict-complex-selectors(
$complex-selector-a,
$complex-selector-b
) {
// If one of the lists is empty, return the other list.
@if list.length($complex-selector-a) < 1 {
@return $complex-selector-b;
}
@if list.length($complex-selector-b) < 1 {
@return $complex-selector-a;
}
// The "joining" of A and B happens at the last compound selector of A and the
// first compound selector of B.
//
// Example:
// ".foo .bar" and ".baz :hover" will append as
// ".foo .bar.baz :hover"
$last-compound-selector-a: list.nth(
$complex-selector-a,
list.length($complex-selector-a)
);
$first-compound-selector-b: list.nth($complex-selector-b, 1);
// The compound selector CSS joining (".bar" and ".baz") and their sorting
// only needs to happen on the last/first of A/B.
$simple-selectors-a: selector.simple-selectors($last-compound-selector-a);
$simple-selectors-b: selector.simple-selectors($first-compound-selector-b);
$sorted-simple-selectors: _sort-simple-selectors(
list.join($simple-selectors-a, $simple-selectors-b)
);
// The result can start to form by setting the last compound selector of A to
// the result of the sorted and joined ".bar.baz"...
$result: list.set-nth(
$complex-selector-a,
list.length($complex-selector-a),
_join-simple-selectors($sorted-simple-selectors)
);
// ...then adding the remaining compound selectors (excluding the first one,
// which was already appended) from B.
@if list.length($complex-selector-b) > 1 {
@for $i from 2 through list.length($complex-selector-b) {
$result: list.append(list.nth($complex-selector-b, $i));
}
}
@return $result;
}
/// Combines a List of simple selectors together to form a compound selector.
/// If there are any pseudo class function selectors that should nest their
/// selectors within their parentheses, this function will do so.
///
/// @param {List} $simple-selectors - A List of simple selectors to join.
/// @return {String} A compound selector.
@function _join-simple-selectors($simple-selectors) {
$compound-selector: '';
$parens-index: _get-nestable-parens-index($simple-selectors);
@if $parens-index {
// Contains a selector, such as :host() that other selectors must be placed
// within the parentheses of. This selector should be moved to the front of
// the compound selector.
$compound-selector: list.nth($simple-selectors, $parens-index);
@if string.index($compound-selector, '(') != null {
// Already has parens. Remove the final closing parens so that additional
// selectors are placed within the parentheses.
$compound-selector: string.slice(
$compound-selector,
1,
string.length($compound-selector) - 1
);
} @else {
// Otherwise, add an opening parens.
$compound-selector: #{$compound-selector}#{string.unquote('(')};
}
}
@for $i from 1 through list.length($simple-selectors) {
@if $i != $parens-index {
// Skip the parens selector that was moved to the front, if any
$simple-selector: list.nth($simple-selectors, $i);
$compound-selector: #{$compound-selector}#{$simple-selector};
}
}
@if $parens-index {
// Add the closing parens
$compound-selector: #{$compound-selector}#{string.unquote(')')};
}
@return $compound-selector;
}
/// Searches a List of simple selectors for any pseudo class functions that can
/// and should be nested with other selectors. If one is found, the index is
/// returned.
///
/// @see {mixin} _can-and-should-nest-pseudo-class
///
/// @param {List} $simple-selectors - A List of simple selectors to search.
/// @return {Number} The index of the selector with parens to nest, or null if
/// there is none.
@function _get-nestable-parens-index($simple-selectors) {
@for $i from 1 through list.length($simple-selectors) {
$simple-selector: list.nth($simple-selectors, $i);
@if _can-and-should-nest-pseudo-class($simple-selector) {
@return $i;
}
}
@return null;
}
/// Checks two things:
///
/// 1. If a simple selector is a pseudo class function that accepts selectors
/// as its arguments.
/// 2. If this selector is commonly used for nesting within.
///
/// For example, `:host` satisfies both #1 and #2, but the `:not()` pseudo class
/// is not commonly used in abstract nesting within Sass.
///
/// @example - scss
/// :host(:hover) {
/// :enabled {
/// // commonly expect :host(:hover:enabled),
/// // since :host(:hover):enabled is invalid CSS
/// }
/// }
///
/// :not(:hover) {
/// :enabled {
/// // commonly expect :not(:hover):enabled
/// // do not expect :not(:hover:enabled) as the intention
/// }
/// }
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the simple selector is a pseudo class function that
/// should nest additional selectors within its parentheses.
@function _can-and-should-nest-pseudo-class($simple-selector) {
@return string.index($simple-selector, ':host') != null or
string.index($simple-selector, '::slotted') != null;
}
/// Sorts a List of simple selectors according to the `<compound-selector>` CSS
/// type definition.
///
/// ```
/// <compound-selector> = [ <type-selector>? <subclass-selector>*
/// [ <pseudo-element-selector> <pseudo-class-selector>* ]* ]!
/// ```
///
/// @example - scss
/// $unsorted: (':hover', '::before', 'h1');
/// #{selector.append(_sort-simple-selectors($unsorted...))} {}
///
/// @example - css
/// h1::before:hover {}
///
/// @link https://drafts.csswg.org/selectors/#typedef-compound-selector
///
/// @param {List} $simple-selectors - A List of simple selectors.
/// @return {List} A List of sorted simple selectors.
@function _sort-simple-selectors($simple-selectors) {
@if list.length($simple-selectors) <= 1 {
@return $simple-selectors;
}
$type-selectors: ();
$pseudo-element-selectors: ();
$subclass-selectors: ();
@each $simple-selector in $simple-selectors {
@if _is-type-selector($simple-selector) {
$type-selectors: list.append($type-selectors, $simple-selector);
} @else if _is-pseudo-element-selector($simple-selector) {
$pseudo-element-selectors: list.append(
$pseudo-element-selectors,
$simple-selector
);
} @else {
$subclass-selectors: list.append($subclass-selectors, $simple-selector);
}
}
@return list.join(
$type-selectors,
list.join($subclass-selectors, $pseudo-element-selectors)
);
}
/// Checks if a simple selector is a `<type-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-type-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is a type selector.
@function _is-type-selector($simple-selector) {
@return not _is-subclass-selector($simple-selector);
}
/// Checks if a simple selector is a `<subclass-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-subclass-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is a subclass selector.
@function _is-subclass-selector($simple-selector) {
@return _is-id-selector($simple-selector) or
_is-class-selector($simple-selector) or
_is-attribute-selector($simple-selector) or
_is-pseudo-class-selector($simple-selector);
}
/// Checks if a simple selector is an `<id-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-id-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is an ID selector.
@function _is-id-selector($simple-selector) {
@return string.index($simple-selector, '#') == 1;
}
/// Checks if a simple selector is a `<class-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-class-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is a class selector.
@function _is-class-selector($simple-selector) {
@return string.index($simple-selector, '.') == 1;
}
/// Checks if a simple selector is an `<attribute-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-attribute-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is an attribute selector.
@function _is-attribute-selector($simple-selector) {
@return string.index($simple-selector, '[') == 1;
}
/// Checks if a simple selector is a `<pseudo-class-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-pseudo-class-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is a pseudo class selector.
@function _is-pseudo-class-selector($simple-selector) {
@return string.index($simple-selector, ':') == 1;
}
/// Checks if a simple selector is a `<pseudo-element-selector>`.
///
/// @link https://drafts.csswg.org/selectors/#typedef-pseudo-element-selector
///
/// @param {String} $simple-selector - The simple selector to check.
/// @return {Bool} True if the selector is a pseudo element selector.
@function _is-pseudo-element-selector($simple-selector) {
@return string.index($simple-selector, '::') == 1;
}