_replace.scss 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. @use 'sass:list';
  23. @use 'sass:meta';
  24. @use 'sass:string';
  25. /// Replaces all name instances in the provided string with values from the
  26. /// provided replacement Map whose keys correspond to the name instances.
  27. /// Returns a new string with the replacements applied.
  28. ///
  29. /// @example
  30. /// replace-string('calc(x + y)', (x: 16px, y: 50%)); // 'calc(16px + 50%)'
  31. ///
  32. /// @example
  33. /// $foo: custom-properties.create-var(custom-properties.create(--foo, 8px));
  34. /// replace-string('calc(foo)', (foo: $foo)); // 'calc(var(--foo, 8px))';
  35. ///
  36. /// @access private
  37. /// @param {String} $string - The string to make replacements on.
  38. /// @param {Map} $replace-map - A Map of name/value replacements. The keys of
  39. /// the Map are names that will be replaced in the string with the keys'
  40. /// respective values.
  41. /// @return {String} The string with replacements made, if any.
  42. @function replace-string($string, $replace-map) {
  43. @if meta.type-of($replace-map) != 'map' {
  44. @error 'mdc-theme: Invalid replacement #{$replace-map}. Must be a Map.';
  45. }
  46. @each $name, $replacement in $replace-map {
  47. // Since the replacement values may contain the name themselves (such as
  48. // a custom property: name "foo" and value "var(--foo)"), gather the indices
  49. // first before replacing.
  50. $indices: ();
  51. $substring: $string;
  52. $prev-index: null;
  53. $index: string.index($substring, $name);
  54. @while $index {
  55. $substring: string.slice($substring, $index + string.length($name));
  56. @if $prev-index {
  57. // Add previous index's value to switch from "substring index" to
  58. // absolute string index. Also add length - 1 since slice removes
  59. // the entire word as well as lists being 1 indexed
  60. $index: $index + $prev-index + string.length($name) - 1;
  61. }
  62. // Use list.join() to "prepend" the index to the start of the list. This
  63. // allows us to iterate "backwards" later.
  64. $indices: list.join($index, $indices);
  65. $prev-index: $index;
  66. $index: string.index($substring, $name);
  67. }
  68. // Since we "prepended" the indices, the list is sorted backwards, with the
  69. // last index first. Replacing values last index to first index removes the
  70. // need for us to adjust the indices as we replace them.
  71. @each $index in $indices {
  72. $before: string.slice($string, 1, $index - 1);
  73. $after: string.slice($string, $index + string.length($name));
  74. $string: $before + $replacement + $after;
  75. }
  76. }
  77. @return $string;
  78. }
  79. /// Replaces all value instances in the provided list with values from the
  80. /// provided replacement Map whose keys correspond to the name instances.
  81. /// Returns a new list with the replacements applied.
  82. ///
  83. /// @example
  84. /// replace-list(0 value, (value: 16px)); // (0 16px)
  85. ///
  86. /// @see {mixin} replace-string
  87. ///
  88. /// @access private
  89. /// @param {List} $list - The list to make replacements on.
  90. /// @param {Map} $replace-map - A Map of name/value replacements. The keys of
  91. /// the Map are names that will be replaced in the list with the keys'
  92. /// respective values.
  93. /// property value replacements instead of the `var()` declaration.
  94. /// @return {List} A new list with replacements made, if any.
  95. @function replace-list($list, $replace-map) {
  96. $separator: list.separator($list);
  97. $replaced-list: ();
  98. @each $value in $list {
  99. @if meta.type-of($value) == 'string' {
  100. $replaced-list: list.append(
  101. $replaced-list,
  102. replace-string($value, $replace-map),
  103. $separator
  104. );
  105. } @else if meta.type-of($value) == 'list' {
  106. $replaced-list: list.append(
  107. $replaced-list,
  108. replace-list($value, $replace-map),
  109. $separator
  110. );
  111. } @else {
  112. $replaced-list: list.append($replaced-list, $value, $separator);
  113. }
  114. }
  115. @return $replaced-list;
  116. }