@material/web 2.4.1 → 2.4.2-nightly.555b0ad.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/field/internal/field.js +12 -1
- package/field/internal/field.js.map +1 -1
- package/package.json +23 -1
- package/sass/ext/_assert.scss +82 -0
- package/sass/ext/_list_ext.scss +114 -0
- package/sass/ext/_map_ext.scss +222 -0
- package/sass/ext/_string_ext.scss +153 -0
- package/sass/ext/_throw.scss +83 -0
- package/sass/ext/_type.scss +66 -0
- package/{internal/sass → sass/ext}/_var.scss +86 -96
- package/sass/ext/tests.css +1 -0
- package/sass/ext/tests.css.map +1 -0
- package/sass/ext/tests.scss +18 -0
- package/slider/internal/_slider.scss +1 -0
- package/slider/internal/slider-styles.css +1 -1
- package/slider/internal/slider-styles.css.map +1 -1
- package/slider/internal/slider-styles.js +1 -1
- package/slider/internal/slider-styles.js.map +1 -1
- package/tabs/internal/_tab.scss +0 -1
- package/tabs/internal/tab-styles.css.map +1 -1
- package/internal/sass/_string-ext.scss +0 -195
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 Google LLC
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
// Extensions to the go/sass:string built-in module.
|
|
7
|
+
|
|
8
|
+
// go/keep-sorted start by_regex='(.+) prefix_order=sass:
|
|
9
|
+
@use 'sass:list';
|
|
10
|
+
@use 'sass:string';
|
|
11
|
+
// go/keep-sorted end
|
|
12
|
+
|
|
13
|
+
/// Checks if a string starts with a given prefix.
|
|
14
|
+
///
|
|
15
|
+
/// @example scss
|
|
16
|
+
/// @debug string_ext.starts-with('var(--foo)', 'var('); // true
|
|
17
|
+
///
|
|
18
|
+
/// @param {string} $string - The string to test.
|
|
19
|
+
/// @param {string} $prefix - The prefix to check.
|
|
20
|
+
/// @return {boolean} True if the string starts with the given prefix.
|
|
21
|
+
@function starts-with($string, $prefix) {
|
|
22
|
+
@return string.slice($string, 1, string.length($prefix)) == $prefix;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// Checks if a string ends with a given suffix.
|
|
26
|
+
///
|
|
27
|
+
/// @example scss
|
|
28
|
+
/// @debug string_ext.ends-with('var(--foo)', ')'); // true
|
|
29
|
+
///
|
|
30
|
+
/// @param {string} $string - The string to test.
|
|
31
|
+
/// @param {string} $suffix - The suffix to check.
|
|
32
|
+
/// @return {boolean} True if the string ends with the given suffix.
|
|
33
|
+
@function ends-with($string, $suffix) {
|
|
34
|
+
@return string.slice($string, -1 * string.length($suffix)) == $suffix;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Trims leading whitespace from the start of a string.
|
|
38
|
+
///
|
|
39
|
+
/// @example scss
|
|
40
|
+
/// @debug string_ext.trim-start(' foo bar '); // "foo bar "
|
|
41
|
+
///
|
|
42
|
+
/// @param {string} $string - The string to trim.
|
|
43
|
+
/// @return {string} The string with whitespace trimmed from the start.
|
|
44
|
+
@function trim-start($string) {
|
|
45
|
+
@while starts-with($string, ' ') {
|
|
46
|
+
$string: replace-start($string, ' ', '');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@return $string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Trims trailing whitespace from the end of a string.
|
|
53
|
+
///
|
|
54
|
+
/// @example scss
|
|
55
|
+
/// @debug string_ext.trim-end(' foo bar '); // " foo bar"
|
|
56
|
+
///
|
|
57
|
+
/// @param {string} $string - The string to trim.
|
|
58
|
+
/// @return {string} The string with trailing whitespace trimmed from the end.
|
|
59
|
+
@function trim-end($string) {
|
|
60
|
+
@while ends-with($string, ' ') {
|
|
61
|
+
$string: replace-end($string, ' ', '');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@return $string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Trims leading and trailing whitespace from a string.
|
|
68
|
+
///
|
|
69
|
+
/// @example scss
|
|
70
|
+
/// @debug string_ext.trim(' foo bar '); // "foo bar"
|
|
71
|
+
///
|
|
72
|
+
/// @param {string} $string - The string to trim.
|
|
73
|
+
/// @return {string} The string with leading and trailing whitespace trimmed.
|
|
74
|
+
@function trim($string) {
|
|
75
|
+
@return trim-start(trim-end($string));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Returns a new string with the first match of a pattern replaced by a given
|
|
79
|
+
/// string.
|
|
80
|
+
///
|
|
81
|
+
/// @example scss
|
|
82
|
+
/// @debug string_ext.replace('foo bar baz', 'bar', 'quux'); // "foo quux baz"
|
|
83
|
+
///
|
|
84
|
+
/// @param {string} $string - The string to be searched.
|
|
85
|
+
/// @param {string} $pattern - The pattern to search for.
|
|
86
|
+
/// @param {string} $replacement - The value to replace the pattern.
|
|
87
|
+
/// @return {string} The string with the first match of pattern replaced by the
|
|
88
|
+
/// replacement or the initial string itself.
|
|
89
|
+
@function replace($string, $pattern, $replacement) {
|
|
90
|
+
$pattern-index: string.index($string, $pattern);
|
|
91
|
+
@if not $pattern-index {
|
|
92
|
+
@return $string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
$before: string.slice($string, 1, $pattern-index - 1);
|
|
96
|
+
$after: string.slice($string, string.length($pattern) + $pattern-index);
|
|
97
|
+
|
|
98
|
+
@return $before + $replacement + $after;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// Returns a new string with all matches of a pattern replaced by a given
|
|
102
|
+
/// string.
|
|
103
|
+
///
|
|
104
|
+
/// @example scss
|
|
105
|
+
/// @debug string_ext.replace-all('foo bar baz', 'ba', 'qua'); // "foo quar quaz"
|
|
106
|
+
///
|
|
107
|
+
/// @param {string} $string - The string to be searched.
|
|
108
|
+
/// @param {string} $pattern - The pattern to search for.
|
|
109
|
+
/// @param {string} $replacement - The value to replace the pattern.
|
|
110
|
+
/// @return {string} The string with all matches of pattern replaced by the
|
|
111
|
+
/// replacement or the initial string itself.
|
|
112
|
+
@function replace-all($string, $pattern, $replacement) {
|
|
113
|
+
@while string.index($string, $pattern) {
|
|
114
|
+
$string: replace($string, $pattern, $replacement);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@return $string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Returns a new string that replaces a prefix at the start of the string.
|
|
121
|
+
///
|
|
122
|
+
/// @example scss
|
|
123
|
+
/// @debug string_ext.replace-start('var(--foo)', 'var(', ''); // "--foo)"
|
|
124
|
+
///
|
|
125
|
+
/// @param {string} $string - The string to be searched.
|
|
126
|
+
/// @param {string} $prefix - The prefix string to replace.
|
|
127
|
+
/// @param {string} $replacement - The value to replace the prefix.
|
|
128
|
+
/// @return {string} The string with the prefix replaced.
|
|
129
|
+
@function replace-start($string, $prefix, $replacement) {
|
|
130
|
+
@if starts-with($string, $prefix) {
|
|
131
|
+
$string: $replacement + string.slice($string, string.length($prefix) + 1);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@return $string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Returns a new string that replaces a suffix at the end of the string.
|
|
138
|
+
///
|
|
139
|
+
/// @example scss
|
|
140
|
+
/// @debug string_ext.replace-end('var(--foo)', ')', ''); // "var(--foo"
|
|
141
|
+
///
|
|
142
|
+
/// @param {string} $string - The string to be searched.
|
|
143
|
+
/// @param {string} $suffix - The suffix string to replace.
|
|
144
|
+
/// @param {string} $replacement - The value to replace the suffix.
|
|
145
|
+
/// @return {string} The string with the suffix trimmed from the end.
|
|
146
|
+
@function replace-end($string, $suffix, $replacement) {
|
|
147
|
+
@if ends-with($string, $suffix) {
|
|
148
|
+
$string: string.slice($string, 1, -1 * string.length($suffix) - 1) +
|
|
149
|
+
$replacement;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@return $string;
|
|
153
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 Google LLC
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
// Utilities for `sass-true` errors, to support testing error behavior.
|
|
7
|
+
|
|
8
|
+
// go/keep-sorted start by_regex='(.+) prefix_order=sass:
|
|
9
|
+
@use 'sass:meta';
|
|
10
|
+
@use 'sass:string';
|
|
11
|
+
// go/keep-sorted end
|
|
12
|
+
|
|
13
|
+
@forward 'true' show error;
|
|
14
|
+
|
|
15
|
+
/// Returns false if none of the given values are error strings, or returns an
|
|
16
|
+
/// error string if any value has an error.
|
|
17
|
+
///
|
|
18
|
+
/// This is used to support testing error behavior with `sass-true`, since
|
|
19
|
+
/// `@error` messages cannot be caught at build time.
|
|
20
|
+
///
|
|
21
|
+
/// @example scss
|
|
22
|
+
/// // A function that may return an "ERROR:" string in a test.
|
|
23
|
+
/// @function get-value($map, $key) {
|
|
24
|
+
/// @if meta.type-of($map) != 'map' {
|
|
25
|
+
/// // Identical to `@error 'ERROR: Arg is not a map'` outside of tests.
|
|
26
|
+
/// @return throw.error('Arg is not a map');
|
|
27
|
+
/// }
|
|
28
|
+
/// @return map.get($map, $key);
|
|
29
|
+
/// }
|
|
30
|
+
///
|
|
31
|
+
/// // A function that needs to handle potential errors from other functions.
|
|
32
|
+
/// @function mix-primary-on-surface($values) {
|
|
33
|
+
/// $primary: get-value($values, 'primary');
|
|
34
|
+
/// $surface: get-value($values, 'surface');
|
|
35
|
+
/// $error: throw.get-error($primary, $surface);
|
|
36
|
+
/// @if $error {
|
|
37
|
+
/// // Return early to guard logic against additional errors since
|
|
38
|
+
/// // $primary or $surface may be a string instead of a color.
|
|
39
|
+
/// @return $error;
|
|
40
|
+
/// }
|
|
41
|
+
///
|
|
42
|
+
/// @return color.mix($primary, $surface, 10%);
|
|
43
|
+
/// }
|
|
44
|
+
///
|
|
45
|
+
/// Note: `throw.error()` and `throw.get-error()` are only useful when testing
|
|
46
|
+
/// error behavior using `sass-true`. If you are not testing a function, use
|
|
47
|
+
/// `@error` instead.
|
|
48
|
+
///
|
|
49
|
+
/// @example scss
|
|
50
|
+
/// // In a `sass-true` test, `throw.get-error()` can be used to assert that
|
|
51
|
+
/// // an error is thrown.
|
|
52
|
+
/// @use 'true' as test with ($catch-errors: true);
|
|
53
|
+
///
|
|
54
|
+
/// @include test.describe('module.get-value()') {
|
|
55
|
+
/// @include test.it('throws an error if the value is not a map') {
|
|
56
|
+
/// $result: module.get-value('not a map', 'primary');
|
|
57
|
+
/// @include test.assert-truthy(throw.get-error($result), '$result is an error');
|
|
58
|
+
/// }
|
|
59
|
+
/// }
|
|
60
|
+
///
|
|
61
|
+
/// @param {*} $error - The value to check.
|
|
62
|
+
/// @param {list} $errors - Additional values to check. Useful for checking
|
|
63
|
+
/// multiple errors at the same time.
|
|
64
|
+
/// @return {string|boolean} The error string if any value is an error, or false
|
|
65
|
+
/// otherwise.
|
|
66
|
+
@function get-error($error, $errors...) {
|
|
67
|
+
@if _is-error($error) {
|
|
68
|
+
@return $error;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@each $additional-error in $errors {
|
|
72
|
+
@if _is-error($additional-error) {
|
|
73
|
+
@return $additional-error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@function _is-error($error) {
|
|
81
|
+
@return (meta.type-of($error) == 'string') and
|
|
82
|
+
(string.index($error, 'ERROR') == 1);
|
|
83
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 Google LLC
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
// Utilities for Sass type checking.
|
|
7
|
+
|
|
8
|
+
// go/keep-sorted start by_regex='(.+) prefix_order=sass:
|
|
9
|
+
@use 'sass:meta';
|
|
10
|
+
@use 'sass:string';
|
|
11
|
+
@use 'throw';
|
|
12
|
+
// go/keep-sorted end
|
|
13
|
+
|
|
14
|
+
/// Returns true if the given value matches the provided type string.
|
|
15
|
+
///
|
|
16
|
+
/// The type string supports multiple types separated by `|`, such as
|
|
17
|
+
/// `'string|null'`. Type options are any values returned by `meta.type-of()`.
|
|
18
|
+
///
|
|
19
|
+
/// @example scss
|
|
20
|
+
/// @function is-empty($value) {
|
|
21
|
+
/// @if type.matches($value, 'list|map') {
|
|
22
|
+
/// @return list.length($value) == 0;
|
|
23
|
+
/// }
|
|
24
|
+
/// @if type.matches($value, 'string') {
|
|
25
|
+
/// @return $value == '';
|
|
26
|
+
/// }
|
|
27
|
+
/// @return type.matches($value, 'null');
|
|
28
|
+
/// }
|
|
29
|
+
///
|
|
30
|
+
/// @param {*} $value - The value to check the type of.
|
|
31
|
+
/// @param {string} $type-string - The type to check. May be multiple types
|
|
32
|
+
/// separated by `|`.
|
|
33
|
+
/// @return {boolean} True if the value matches the type string.
|
|
34
|
+
@function matches($value, $type-string) {
|
|
35
|
+
@if meta.type-of($type-string) != 'string' or $type-string == '' {
|
|
36
|
+
@return throw.error(
|
|
37
|
+
'$type-string must be a non-empty string',
|
|
38
|
+
$source: 'type.matches'
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
@if string.index($type-string, ' ') {
|
|
42
|
+
@return throw.error(
|
|
43
|
+
'$type-string may not contain spaces',
|
|
44
|
+
$source: 'type.matches'
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
@if string.index($type-string, 'boolean') {
|
|
48
|
+
@return throw.error(
|
|
49
|
+
'Use "bool" instead of "boolean"',
|
|
50
|
+
$source: 'type.matches'
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
$value-type: meta.type-of($value);
|
|
55
|
+
@if $value-type == $type-string {
|
|
56
|
+
@return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@each $type in string.split($type-string, '|') {
|
|
60
|
+
@if $value-type == $type {
|
|
61
|
+
@return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@return false;
|
|
66
|
+
}
|
|
@@ -3,90 +3,79 @@
|
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
//
|
|
5
5
|
|
|
6
|
-
//
|
|
6
|
+
// Utility functions for "var()" custom property string manipulation.
|
|
7
|
+
|
|
8
|
+
// go/keep-sorted start by_regex='(.+) prefix_order=sass:
|
|
7
9
|
@use 'sass:map';
|
|
8
10
|
@use 'sass:meta';
|
|
9
11
|
@use 'sass:string';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@use '
|
|
12
|
+
@use 'assert';
|
|
13
|
+
@use 'string_ext';
|
|
14
|
+
@use 'throw';
|
|
13
15
|
// go/keep-sorted end
|
|
14
16
|
|
|
15
17
|
/// Creates a custom property `var()` string.
|
|
16
18
|
///
|
|
17
|
-
/// @
|
|
19
|
+
/// @example scss
|
|
20
|
+
/// @debug var.create(--foo); // "var(--foo)"
|
|
21
|
+
/// @debug var.create(--foo, 8px); // "var(--foo, 8px)"
|
|
22
|
+
///
|
|
23
|
+
/// @param {string} $name - The name of the custom property.
|
|
18
24
|
/// @param {*} $fallback [null] - Optional `var()` fallback value.
|
|
19
|
-
/// @return {
|
|
25
|
+
/// @return {string} A custom property `var()` string.
|
|
20
26
|
@function create($name, $fallback: null) {
|
|
21
|
-
$name:
|
|
22
|
-
@if $
|
|
23
|
-
@return
|
|
27
|
+
$name: assert.is-type($name, 'string');
|
|
28
|
+
@if throw.get-error($name) {
|
|
29
|
+
@return $name;
|
|
24
30
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
@if not string_ext.starts-with($name, '--') {
|
|
32
|
+
@return throw.error(
|
|
33
|
+
'Custom property names must start with "--". $name: #{meta.inspect($name)}',
|
|
34
|
+
$source: 'var.create'
|
|
35
|
+
);
|
|
30
36
|
}
|
|
31
37
|
|
|
32
|
-
@
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/// Create a custom property variable name.
|
|
36
|
-
///
|
|
37
|
-
/// Providing a custom property name with `--*` will ignore the global prefix.
|
|
38
|
-
///
|
|
39
|
-
/// @example - scss
|
|
40
|
-
/// .foo {
|
|
41
|
-
/// color: var(#{var.create-name(foo)});
|
|
42
|
-
/// background: var(#{var.create-name(--bar)});
|
|
43
|
-
/// }
|
|
44
|
-
///
|
|
45
|
-
/// @example - css
|
|
46
|
-
/// .foo {
|
|
47
|
-
/// color: var(--md-foo);
|
|
48
|
-
/// background: var(--bar);
|
|
49
|
-
/// }
|
|
50
|
-
///
|
|
51
|
-
/// @param {String} $name - The name of the custom property.
|
|
52
|
-
/// @return {String} The full valid CSS custom property variable name.
|
|
53
|
-
@function create-name($name) {
|
|
54
|
-
@if string-ext.has-prefix($name, '--') {
|
|
55
|
-
@return $name;
|
|
38
|
+
@if $fallback == null {
|
|
39
|
+
@return var(#{$name});
|
|
56
40
|
}
|
|
57
41
|
|
|
58
|
-
@return
|
|
42
|
+
@return var(#{$name}, #{$fallback});
|
|
59
43
|
}
|
|
60
44
|
|
|
61
45
|
/// Returns the custom property variable name of `var()` string.
|
|
62
46
|
///
|
|
47
|
+
/// @example scss
|
|
48
|
+
/// $var: var(--foo);
|
|
49
|
+
/// @debug var.name($var); // "--foo"
|
|
50
|
+
///
|
|
51
|
+
/// @param {string} $var - A custom property `var()` string.
|
|
52
|
+
/// @return {string} The custom property variable name.
|
|
63
53
|
/// @throw If the value is not a custom property.
|
|
64
|
-
/// @param {String} $var - A custom property `var()` string.
|
|
65
|
-
/// @return {String} The custom property variable name.
|
|
66
54
|
@function name($var) {
|
|
67
55
|
$var: _parse-and-validate($var);
|
|
68
|
-
@
|
|
56
|
+
@if throw.get-error($var) {
|
|
57
|
+
@return $var;
|
|
58
|
+
}
|
|
59
|
+
@return map.get($var, 'name');
|
|
69
60
|
}
|
|
70
61
|
|
|
71
62
|
/// Returns the fallback value of a custom property `var()` string. The value
|
|
72
63
|
/// may be null if the `var()` does not have a fallback value.
|
|
73
64
|
///
|
|
74
|
-
/// @example
|
|
75
|
-
/// $
|
|
76
|
-
/// // "var(--bar, 8px)"
|
|
65
|
+
/// @example scss
|
|
66
|
+
/// $var: var(--foo, var(--bar, 8px));
|
|
67
|
+
/// @debug var.fallback($var); // "var(--bar, 8px)"
|
|
77
68
|
///
|
|
78
|
-
/// @
|
|
79
|
-
/// @
|
|
80
|
-
/// @return {String} The fallback value of the `var()` string. May be null if
|
|
69
|
+
/// @param {string} $var - A custom property `var()` string.
|
|
70
|
+
/// @return {string} The fallback value of the `var()` string. May be null if
|
|
81
71
|
/// the `var()` does not have a fallback value.
|
|
72
|
+
/// @throw If the value is not a custom property.
|
|
82
73
|
@function fallback($var) {
|
|
83
74
|
$var: _parse-and-validate($var);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
@return create(name($fallback), fallback($fallback));
|
|
75
|
+
@if throw.get-error($var) {
|
|
76
|
+
@return $var;
|
|
87
77
|
}
|
|
88
|
-
|
|
89
|
-
@return $fallback;
|
|
78
|
+
@return map.get($var, 'fallback');
|
|
90
79
|
}
|
|
91
80
|
|
|
92
81
|
/// Returns the deep fallback value of a custom property `var()` string. The
|
|
@@ -95,18 +84,18 @@
|
|
|
95
84
|
/// If a fallback value is another `var()`, this function will return the final
|
|
96
85
|
/// concrete value in the chain.
|
|
97
86
|
///
|
|
98
|
-
/// @example
|
|
99
|
-
/// $
|
|
100
|
-
/// // "8px"
|
|
87
|
+
/// @example scss
|
|
88
|
+
/// $var: var(--foo, var(--bar, 8px));
|
|
89
|
+
/// @debug var.deep-fallback($var); // "8px"
|
|
101
90
|
///
|
|
102
|
-
/// @
|
|
103
|
-
/// @
|
|
104
|
-
/// @return {String} The deep fallback value of the `var()` string. May be null
|
|
91
|
+
/// @param {string} $var - A custom property `var()` string.
|
|
92
|
+
/// @return {string} The deep fallback value of the `var()` string. May be null
|
|
105
93
|
/// if the `var()` does not have a fallback value.
|
|
94
|
+
/// @throw If the value is not a custom property.
|
|
106
95
|
@function deep-fallback($var) {
|
|
107
96
|
$fallback: fallback($var);
|
|
108
|
-
@
|
|
109
|
-
|
|
97
|
+
@while is-var($fallback) {
|
|
98
|
+
$fallback: fallback($fallback);
|
|
110
99
|
}
|
|
111
100
|
|
|
112
101
|
@return $fallback;
|
|
@@ -115,16 +104,17 @@
|
|
|
115
104
|
/// Creates a new custom property `var()` string and returns it with the
|
|
116
105
|
/// specified new fallback value.
|
|
117
106
|
///
|
|
118
|
-
/// @example
|
|
119
|
-
/// $
|
|
120
|
-
///
|
|
107
|
+
/// @example scss
|
|
108
|
+
/// $var: var(--foo, var(--bar, 8px));
|
|
109
|
+
/// $new-var: set-fallback($var, 16px);
|
|
110
|
+
/// @debug $new-var; // "var(--foo, 16px)"
|
|
121
111
|
///
|
|
122
|
-
/// @
|
|
123
|
-
/// @param {String} $var - A custom property `var()` string.
|
|
112
|
+
/// @param {string} $var - A custom property `var()` string.
|
|
124
113
|
/// @param {*} $new-fallback - The new fallback value. May be null to remove a
|
|
125
114
|
/// value.
|
|
126
|
-
/// @return {
|
|
115
|
+
/// @return {string} A custom property `var()` string with the new fallback
|
|
127
116
|
/// value.
|
|
117
|
+
/// @throw If the value is not a custom property.
|
|
128
118
|
@function set-fallback($var, $new-fallback) {
|
|
129
119
|
$name: name($var);
|
|
130
120
|
@return create($name, $new-fallback);
|
|
@@ -136,16 +126,17 @@
|
|
|
136
126
|
/// If the provided `var()` string's fallback value is another `var()`, this
|
|
137
127
|
/// function will set the final fallback value in the chain.
|
|
138
128
|
///
|
|
139
|
-
/// @example
|
|
140
|
-
/// $
|
|
141
|
-
///
|
|
129
|
+
/// @example scss
|
|
130
|
+
/// $var: var(--foo, var(--bar, 8px));
|
|
131
|
+
/// $new-var: var.deep-set-fallback($var, 16px);
|
|
132
|
+
/// @debug $new-var; // "var(--foo, var(--bar, 16px))"
|
|
142
133
|
///
|
|
143
|
-
/// @
|
|
144
|
-
/// @param {String} $var - A custom property `var()` string.
|
|
134
|
+
/// @param {string} $var - A custom property `var()` string.
|
|
145
135
|
/// @param {*} $new-fallback - The new fallback value. May be null to remove a
|
|
146
136
|
/// value.
|
|
147
|
-
/// @return {
|
|
137
|
+
/// @return {string} A custom property `var()` string with the new deep
|
|
148
138
|
/// fallback value.
|
|
139
|
+
/// @throw If the value is not a custom property.
|
|
149
140
|
@function deep-set-fallback($var, $new-fallback) {
|
|
150
141
|
$old-fallback: fallback($var);
|
|
151
142
|
@if is-var($old-fallback) {
|
|
@@ -157,11 +148,12 @@
|
|
|
157
148
|
|
|
158
149
|
/// Indicates whether or not a value is a custom property `var()` string.
|
|
159
150
|
///
|
|
160
|
-
/// @example
|
|
161
|
-
/// $
|
|
151
|
+
/// @example scss
|
|
152
|
+
/// $var: var(--foo);
|
|
153
|
+
/// @debug var.is-var($var); // true
|
|
162
154
|
///
|
|
163
155
|
/// @param {*} $var - The value to test.
|
|
164
|
-
/// @return {
|
|
156
|
+
/// @return {boolean} True if the value is a custom property `var()` string, or
|
|
165
157
|
/// false if not.
|
|
166
158
|
@function is-var($var) {
|
|
167
159
|
@return _parse($var) != null;
|
|
@@ -170,24 +162,25 @@
|
|
|
170
162
|
/// Indicates whether or not a value is a `var()` string.
|
|
171
163
|
///
|
|
172
164
|
/// @param {*} $var - The value to test.
|
|
173
|
-
/// @return {
|
|
165
|
+
/// @return {boolean} True if the value is a custom property `var()` string, or
|
|
174
166
|
/// false if not.
|
|
175
167
|
@function _is-var-string($var) {
|
|
176
|
-
@return meta.type-of($var) == 'string' and
|
|
168
|
+
@return meta.type-of($var) == 'string' and
|
|
169
|
+
string_ext.starts-with($var, 'var(');
|
|
177
170
|
}
|
|
178
171
|
|
|
179
172
|
/// Parses a `var()` string into a Map with `name` and `fallback` keys. This
|
|
180
173
|
/// function returns null if the value is invalid.
|
|
181
174
|
///
|
|
182
175
|
/// @param {*} $var - The value to parse.
|
|
183
|
-
/// @return {
|
|
176
|
+
/// @return {map} A Map containing a string `name` key with the custom property
|
|
184
177
|
/// name and a `fallback` key with the fallback value (which may be null).
|
|
185
178
|
/// The returned Map itself may be null if the provided value is not valid.
|
|
186
179
|
@function _parse($var) {
|
|
187
180
|
@if meta.type-of($var) ==
|
|
188
181
|
'map' and
|
|
189
|
-
map.has-key($var, name) and
|
|
190
|
-
map.has-key($var, fallback)
|
|
182
|
+
map.has-key($var, 'name') and
|
|
183
|
+
map.has-key($var, 'fallback')
|
|
191
184
|
{
|
|
192
185
|
@return $var;
|
|
193
186
|
}
|
|
@@ -197,40 +190,37 @@
|
|
|
197
190
|
}
|
|
198
191
|
|
|
199
192
|
// Remove function name and parens
|
|
200
|
-
$var:
|
|
193
|
+
$var: string_ext.replace-start($var, 'var(', '');
|
|
194
|
+
$var: string_ext.replace-end($var, ')', '');
|
|
201
195
|
|
|
202
|
-
$name:
|
|
196
|
+
$name: string_ext.trim($var);
|
|
203
197
|
$fallback: null;
|
|
204
198
|
$comma: string.index($var, ',');
|
|
205
199
|
@if $comma != null {
|
|
206
|
-
$name:
|
|
207
|
-
$fallback:
|
|
208
|
-
@if _is-var-string($fallback) {
|
|
209
|
-
$fallback: _parse($fallback);
|
|
210
|
-
@if $fallback == null {
|
|
211
|
-
// Invalid var() fallback string
|
|
212
|
-
@return null;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
200
|
+
$name: string_ext.trim(string.slice($var, 1, $comma - 1));
|
|
201
|
+
$fallback: string_ext.trim(string.slice($var, $comma + 1));
|
|
215
202
|
}
|
|
216
203
|
|
|
217
204
|
@if $name == '' or $fallback == '' {
|
|
218
205
|
@return null;
|
|
219
206
|
}
|
|
220
207
|
|
|
221
|
-
@return (name: $name, fallback: $fallback);
|
|
208
|
+
@return ('name': $name, 'fallback': $fallback);
|
|
222
209
|
}
|
|
223
210
|
|
|
224
211
|
/// Parses a `var()` string into a Map with `name` and `fallback` keys.
|
|
225
212
|
///
|
|
226
|
-
/// @throw If the value is not a custom property.
|
|
227
213
|
/// @param {*} $var - The value to parse.
|
|
228
|
-
/// @return {
|
|
214
|
+
/// @return {map} A Map containing a string `name` key with the custom property
|
|
229
215
|
/// name and a `fallback` key with the fallback value (which may be null).
|
|
216
|
+
/// @throw If the value is not a custom property.
|
|
230
217
|
@function _parse-and-validate($var) {
|
|
231
218
|
$var-map: _parse($var);
|
|
232
219
|
@if $var-map == null {
|
|
233
|
-
@error
|
|
220
|
+
@return throw.error(
|
|
221
|
+
'#{meta.inspect($var)} is not a valid var() string',
|
|
222
|
+
$source: 'var._parse-and-validate'
|
|
223
|
+
);
|
|
234
224
|
}
|
|
235
225
|
|
|
236
226
|
@return $var-map;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/*# sourceMappingURL=tests.css.map */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sourceRoot":"","sources":[],"names":[],"mappings":"","file":"tests.css"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 Google LLC
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
@use 'true' as test with (
|
|
7
|
+
$catch-errors: true
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
// go/keep-sorted start
|
|
11
|
+
@use 'assert_test';
|
|
12
|
+
@use 'list_ext_test';
|
|
13
|
+
@use 'map_ext_test';
|
|
14
|
+
@use 'string_ext_test';
|
|
15
|
+
@use 'throw_test';
|
|
16
|
+
@use 'type_test';
|
|
17
|
+
@use 'var_test';
|
|
18
|
+
// go/keep-sorted end
|