-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
_replace.scss
125 lines (116 loc) · 4.82 KB
/
_replace.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
//
// Copyright 2020 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:meta';
@use 'sass:string';
/// Replaces all name instances in the provided string with values from the
/// provided replacement Map whose keys correspond to the name instances.
/// Returns a new string with the replacements applied.
///
/// @example
/// replace-string('calc(x + y)', (x: 16px, y: 50%)); // 'calc(16px + 50%)'
///
/// @example
/// $foo: custom-properties.create-var(custom-properties.create(--foo, 8px));
/// replace-string('calc(foo)', (foo: $foo)); // 'calc(var(--foo, 8px))';
///
/// @access private
/// @param {String} $string - The string to make replacements on.
/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
/// the Map are names that will be replaced in the string with the keys'
/// respective values.
/// @return {String} The string with replacements made, if any.
@function replace-string($string, $replace-map) {
@if meta.type-of($replace-map) != 'map' {
@error 'mdc-theme: Invalid replacement #{$replace-map}. Must be a Map.';
}
@each $name, $replacement in $replace-map {
// Since the replacement values may contain the name themselves (such as
// a custom property: name "foo" and value "var(--foo)"), gather the indices
// first before replacing.
$indices: ();
$substring: $string;
$prev-index: null;
$index: string.index($substring, $name);
@while $index {
$substring: string.slice($substring, $index + string.length($name));
@if $prev-index {
// Add previous index's value to switch from "substring index" to
// absolute string index. Also add length - 1 since slice removes
// the entire word as well as lists being 1 indexed
$index: $index + $prev-index + string.length($name) - 1;
}
// Use list.join() to "prepend" the index to the start of the list. This
// allows us to iterate "backwards" later.
$indices: list.join($index, $indices);
$prev-index: $index;
$index: string.index($substring, $name);
}
// Since we "prepended" the indices, the list is sorted backwards, with the
// last index first. Replacing values last index to first index removes the
// need for us to adjust the indices as we replace them.
@each $index in $indices {
$before: string.slice($string, 1, $index - 1);
$after: string.slice($string, $index + string.length($name));
$string: $before + $replacement + $after;
}
}
@return $string;
}
/// Replaces all value instances in the provided list with values from the
/// provided replacement Map whose keys correspond to the name instances.
/// Returns a new list with the replacements applied.
///
/// @example
/// replace-list(0 value, (value: 16px)); // (0 16px)
///
/// @see {mixin} replace-string
///
/// @access private
/// @param {List} $list - The list to make replacements on.
/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
/// the Map are names that will be replaced in the list with the keys'
/// respective values.
/// property value replacements instead of the `var()` declaration.
/// @return {List} A new list with replacements made, if any.
@function replace-list($list, $replace-map) {
$separator: list.separator($list);
$replaced-list: ();
@each $value in $list {
@if meta.type-of($value) == 'string' {
$replaced-list: list.append(
$replaced-list,
replace-string($value, $replace-map),
$separator
);
} @else if meta.type-of($value) == 'list' {
$replaced-list: list.append(
$replaced-list,
replace-list($value, $replace-map),
$separator
);
} @else {
$replaced-list: list.append($replaced-list, $value, $separator);
}
}
@return $replaced-list;
}