Skip to content

Instantly share code, notes, and snippets.

@mmousawy
Created February 28, 2019 13:24
Show Gist options
  • Save mmousawy/83f8761d075324d977e03dc1e65795cd to your computer and use it in GitHub Desktop.
Save mmousawy/83f8761d075324d977e03dc1e65795cd to your computer and use it in GitHub Desktop.
Encode SVG URI and inline SCSS
@function encode-uri($uri) {
$enc: str-replace($uri, '"', "'");
$enc: str-replace($enc, '<', '%3C');
$enc: str-replace($enc, '>', '%3E');
$enc: str-replace($enc, '&', '%26');
$enc: str-replace($enc, '#', '%23');
@return $enc;
}
@function svg-uri($svg) {
$encoded: '';
$slice: 2000;
$index: 0;
$loops: ceil(str-length($svg) / $slice);
@for $i from 1 through $loops {
$chunk: str-slice($svg, $index, $index + $slice - 1);
$chunk: encode-uri($chunk);
$encoded: #{$encoded}#{$chunk};
$index: $index + $slice;
}
@return url("data:image/svg+xml;charset=utf8,#{$encoded}");
}
@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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment