-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert-selector.scss
80 lines (67 loc) · 2.97 KB
/
insert-selector.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
@use "sass:list";
@use "sass:map";
@use "sass:meta";
@use "sass:selector";
@use "sass:string";
@use "src/functions" as *;
/// Inserts a modifier to a parent element in a complex selector
///
/// @param {string} $target
/// the target element in the tree
/// @param {string} $insert
/// the element or modifier you want to insert
/// @param {boolean} $child, default false
/// if true, the $insert is a separate child element in the selector tree
/// @return {string}
/// full new element tree
@mixin insert-selector($target, $insert, $child: false) {
$target: str-trim($target);
$insert: str-trim($insert);
$is-attributes: check-args($target, $insert);
$match: 0;
$compound-list: ();
$selectors: selector.parse(#{&});
$complex-selector: list.nth($selectors, 1);
@each $compound-selector in $complex-selector {
$compound-index: list.index($complex-selector, $compound-selector);
$simple-selectors: selector.simple-selectors($compound-selector);
@each $simple-selector in $simple-selectors {
$selector-index: list.index($simple-selectors, $simple-selector);
@if ($simple-selector == $target) {
$match: $match + 1;
@if $child {
$simple-selector: '#{$simple-selector} #{$insert}';
} @else if (map.get($is-attributes, 'target') == "element" and map.get($is-attributes, 'insert') == "element") {
$simple-selector: '#{$insert} #{$simple-selector}';
} @else if (map.get($is-attributes, 'target') == "attribute" and map.get($is-attributes, 'insert') == "attribute") {
$simple-selector: '#{$simple-selector}#{$insert}';
} @else if (map.get($is-attributes, 'target') == "attribute" and map.get($is-attributes, 'insert') == "element") {
$simple-selector: '#{$insert}#{$simple-selector}';
} @else if (map.get($is-attributes, 'target') == "element" and map.get($is-attributes, 'insert') == "attribute") {
$simple-selector: '#{$simple-selector}#{$insert}';
}
// Get the selector and where it is
$winner-index: $compound-index !global;
$winner: $simple-selector !global;
// Add multiple simple selectors together with the new insert selector
@if (list.length($simple-selectors) > 1) {
$winner: list.set-nth($simple-selectors, $selector-index, $winner);
$winner: str-implode($winner) !global;
}
}
}
// Rebuild the compound list after breaking into individual simple selectors
$compound-list: list.append($compound-list, $compound-selector);
}
// Make sure the target matches a selector
@if not $winner {
@error "The argument '#{$target}' is not in '#{&}'. Adjust the target!";
} @else if ($match > 1) {
@error "The original selector '#{&}' contains more than one of the argument '#{$target}'. Adjust the target!";
}
// Insert the insert at the right location
$return: list.set-nth($compound-list, $winner-index, $winner);
@at-root #{$return} {
@content;
}
}