mirror of
https://github.com/material-components/material-web.git
synced 2026-03-09 00:09:23 +08:00
109 lines
3.8 KiB
SCSS
109 lines
3.8 KiB
SCSS
//
|
|
// Copyright 2022 Google LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
@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;
|
|
}
|