/**
* Utility function to replace a substring as Sass doesn't provide the built-in method to do it
*/
@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;
}

// Characters which are escaped by the escape-svg function
$escaped-characters: (('<', '%3c'), ('>', '%3e'), ('#', '%23'), ('(', '%28'), (')', '%29')) !default;

/**
* Method implementation taken from Bootstrap
* ref: https://github.com/twbs/bootstrap/blob/cacbdc680ecdfee5f0c7fbb876ad15188eaf697d/scss/_functions.scss#L131
*/
@function escape-svg($string) {
	@if str-index($string, 'data:image/svg+xml') {
		@each $char, $encoded in $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;
}
