Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Last active August 26, 2016 03:06
Show Gist options
  • Save yomotsu/62c504de9389255c2c2345ec2233955e to your computer and use it in GitHub Desktop.
Save yomotsu/62c504de9389255c2c2345ec2233955e to your computer and use it in GitHub Desktop.
replace SVG source to dataURI in Sass
// -----------------------------------------------------------------
// 文字列置換
// @param {string} $string - 元となる文字列
// @param {string} $search - 検索する文字列
// @param {string} $replace - 置換する文字列
// @return {string} 置換された文字列
// @example
// `str-replace( 'abcd', 'bc', 'BC');`
// // 'aBCd'
// -----------------------------------------------------------------
@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;
}
// -----------------------------------------------------------------
// SVGソースのdataUri化
// @param {string} $svgSrc - SVGソース
// @return {string} dataUrl化された文字列
// -----------------------------------------------------------------
@function svgToUri ( $svgSrc ) {
$res: str-replace( $svgSrc, '<', '%3C');
$res: str-replace( $res, ' ', '%20');
$res: str-replace( $res, '"', '%22');
$res: str-replace( $res, '#', '%23');
$res: str-replace( $res, '>', '%3E');
@return 'data:image/svg+xml,' + $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment