-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
_strings.scss
142 lines (131 loc) · 4.14 KB
/
_strings.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
@use 'sass:string';
@use 'sass:meta';
@use '../variables/variables' as vars;
@forward 'type-checking';
/// Check if `$string` starts with the given `$substring`.
///
/// @parameter {String} $string - String to check
/// @parameter {String} $substring - Substring to search for at the start of `$string`
/// @return {Boolean}
/// @group String
/// @author Felix Scholze
/// @since v1.3.0
///
/// @example
/// @debug str-starts-with('hello world', 'hello');
/// //=> true
///
@function str-starts-with($string, $substring) {
@if meta.type-of($string) != string {
@error '❌ ===> #{$string} is not a string';
}
@if meta.type-of($substring) != string {
@error '❌ ===> #{$substring} is not a string';
}
@return string.index($string, $substring) == 1;
}
/// Check if `$string` ends with the given `$substring`.
///
/// @param {String} $string - String to check
/// @param {String} $substring - Substring to search for the end of `$string`
/// @return {Boolean}
/// @group String
/// @author Felix Scholze
/// @since v1.3.0
///
/// @example
/// @debug str-ends-with('hello world', 'world');
///
@function str-ends-with($string, $substring) {
@if meta.type-of($string) != string {
@error '❌ ===> #{$string} is not a string';
}
@if meta.type-of($substring) != string {
@error '❌ ===> #{$substring} is not a string';
}
@return string.slice($string, string.length($substring) * -1) == $substring;
}
/// Check if `$string` contains the given `$substring`.
///
/// @param {String} $string - String to check
/// @param {String} $substring - Substring to search for in `$string`
/// @return {Boolean}
/// @group String
/// @author Felix Scholze
/// @since v1.3.0
///
/// @example
/// @debug str-contains('i love SCSS', 'SCSS');
/// //=> true
///
@function str-contains($string, $substring) {
@if meta.type-of($string) != string {
@error '❌ ===> #{$string} is not a string';
}
@if meta.type-of($substring) != string {
@error '❌ ===> #{$substring} is not a string';
}
@return string.index($string, $substring) != null;
}
/// Replace `$search` with `$replace` in `$string`
/// Used on our SVG icon backgrounds for custom forms.
///
/// @param {String} $string - Initial string
/// @param {String} $search - Substring to replace
/// @param {String} $replace ('') - New value
/// @return {String} - Updated string
/// @group String
/// @author Hugo Giraudel
/// @since v1.0.0
///
/// @example
/// $string: 'I hate SCSS!';
/// @include str-replace($string, 'hate', 'love');
/// //=> I love SCSS!
///
@function str-replace($string, $search, $replace: '') {
$index: string.index($string, $search);
@if meta.type-of($string) != string {
@error '❌ ===> #{$string} is not a string';
}
@if meta.type-of($search) != string {
@error '❌ ===> #{$search} is not a string';
}
@if $index {
@return string.slice($string, 1, $index - 1) + $replace + str-replace(
string.slice($string, $index + string.length($search)),
$search,
$replace
);
}
@return $string;
}
/// Requires the use of quotes around data URIs.
/// @param {String} $string - SVG code
/// @return {String} - SCSS compiled output after URL-encoding
/// @group String
/// @author Kevin Weber
/// @link https://codepen.io/kevinweber/pen/dXWoRw
/// @since v1.0.0
///
/// @example
/// $icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'></polyline></svg>");
///
/// background-image: escape-svg($icon);
///
@function escape-svg($string) {
@if meta.type-of($string) != string {
@error '❌ ===> #{$string} is not a string';
}
@if string.index($string, 'data:image/svg+xml') {
@each $char, $encoded in vars.$escaped-characters {
// Do not escape the url brackets
@if string.index($string, 'url(') == 1 {
$string: url('#{str-replace(string.slice($string, 6, -3), $char, $encoded)}');
} @else {
$string: str-replace($string, $char, $encoded);
}
}
}
@return $string;
}