UNPKG

4.93 kBSCSSView Raw
1//
2// Copyright 2020 Google Inc.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21//
22
23@use 'sass:list';
24@use 'sass:meta';
25@use 'sass:string';
26
27/// Replaces all name instances in the provided string with values from the
28/// provided replacement Map whose keys correspond to the name instances.
29/// Returns a new string with the replacements applied.
30///
31/// @example
32/// replace-string('calc(x + y)', (x: 16px, y: 50%)); // 'calc(16px + 50%)'
33///
34/// @example
35/// $foo: custom-properties.create-var(custom-properties.create(--foo, 8px));
36/// replace-string('calc(foo)', (foo: $foo)); // 'calc(var(--foo, 8px))';
37///
38/// @access private
39/// @param {String} $string - The string to make replacements on.
40/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
41/// the Map are names that will be replaced in the string with the keys'
42/// respective values.
43/// @return {String} The string with replacements made, if any.
44@function replace-string($string, $replace-map) {
45 @if meta.type-of($replace-map) != 'map' {
46 @error 'mdc-theme: Invalid replacement #{$replace-map}. Must be a Map.';
47 }
48
49 @each $name, $replacement in $replace-map {
50 // Since the replacement values may contain the name themselves (such as
51 // a custom property: name "foo" and value "var(--foo)"), gather the indices
52 // first before replacing.
53 $indices: ();
54 $substring: $string;
55 $prev-index: null;
56 $index: string.index($substring, $name);
57 @while $index {
58 $substring: string.slice($substring, $index + string.length($name));
59
60 @if $prev-index {
61 // Add previous index's value to switch from "substring index" to
62 // absolute string index. Also add length - 1 since slice removes
63 // the entire word as well as lists being 1 indexed
64 $index: $index + $prev-index + string.length($name) - 1;
65 }
66
67 // Use list.join() to "prepend" the index to the start of the list. This
68 // allows us to iterate "backwards" later.
69 $indices: list.join($index, $indices);
70 $prev-index: $index;
71 $index: string.index($substring, $name);
72 }
73
74 // Since we "prepended" the indices, the list is sorted backwards, with the
75 // last index first. Replacing values last index to first index removes the
76 // need for us to adjust the indices as we replace them.
77 @each $index in $indices {
78 $before: string.slice($string, 1, $index - 1);
79 $after: string.slice($string, $index + string.length($name));
80 $string: $before + $replacement + $after;
81 }
82 }
83
84 @return $string;
85}
86
87/// Replaces all value instances in the provided list with values from the
88/// provided replacement Map whose keys correspond to the name instances.
89/// Returns a new list with the replacements applied.
90///
91/// @example
92/// replace-list(0 value, (value: 16px)); // (0 16px)
93///
94/// @see {mixin} replace-string
95///
96/// @access private
97/// @param {List} $list - The list to make replacements on.
98/// @param {Map} $replace-map - A Map of name/value replacements. The keys of
99/// the Map are names that will be replaced in the list with the keys'
100/// respective values.
101/// property value replacements instead of the `var()` declaration.
102/// @return {List} A new list with replacements made, if any.
103@function replace-list($list, $replace-map) {
104 $separator: list.separator($list);
105 $replaced-list: ();
106 @each $value in $list {
107 @if meta.type-of($value) == 'string' {
108 $replaced-list: list.append(
109 $replaced-list,
110 replace-string($value, $replace-map),
111 $separator
112 );
113 } @else if meta.type-of($value) == 'list' {
114 $replaced-list: list.append(
115 $replaced-list,
116 replace-list($value, $replace-map),
117 $separator
118 );
119 } @else {
120 $replaced-list: list.append($replaced-list, $value, $separator);
121 }
122 }
123
124 @return $replaced-list;
125}