Skip to content

Commit

Permalink
feat(functions): add escape-svg and str-replace functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Jul 16, 2021
1 parent 663d374 commit 3ff2fd1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/default/scss/core/functions/_index.scss
@@ -1,3 +1,4 @@
@import "_math.scss";
@import "_config-maps.scss";
@import "_colors.scss";
@import "_misc.scss";
34 changes: 34 additions & 0 deletions packages/default/scss/core/functions/_misc.scss
@@ -0,0 +1,34 @@
$svg-escaped-characters: (
("<", "%3c"),
(">", "%3e"),
("#", "%23"),
("(", "%28"),
(")", "%29")
) !default;

// See https://www.sassmeister.com/gist/1b4f2da5527830088e4d
@function str-replace($string, $search, $replace: "") {
$index: str-index($string, $search);

@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}

@return $string;
}

// See https://codepen.io/kevinweber/pen/dXWoRw
@function escape-svg($string) {
@if str-index($string, "data:image/svg+xml") {
@each $char, $encoded in $svg-escaped-characters {
// Do not escape the url brackets
@if str-index($string, "url(") == 1 {
$string: url("#{str-replace(str-slice($string, 6, -3), $char, $encoded)}");
} @else {
$string: str-replace($string, $char, $encoded);
}
}
}

@return $string;
}

0 comments on commit 3ff2fd1

Please sign in to comment.