Skip to content

Instantly share code, notes, and snippets.

@dmitry-ilyashevich
Created January 13, 2014 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-ilyashevich/8404317 to your computer and use it in GitHub Desktop.
Save dmitry-ilyashevich/8404317 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.0.rc.2)
// Compass (v1.0.0.alpha.17)
// ----
// @tobynets
// Credit: http://hugogiraudel.com/2014/01/13/sass-string-replacement-function/
@function str-replace($string, $old, $new: "", $case-sensitive: true)
@if type-of($string) != string or type-of($old) != string or type-of($new) != string
@warn "One of the 3 arguments is not a string."
@return $string
@if str-index($new, $old) != 0
@warn "The string to be replaced is contained in the new string. Infinite recursion avoided."
@return $string
$index: if(not $case-sensitive, str-index(to-lower-case($string), to-lower-case($old)), str-index($string, $old))
@if $index > 0 and $new != $old
$new-string: quote(str-slice($string, 1, $index - 1))
@for $i from $index through str-length($string)
@if $i < $index or $i >= $index + str-length($old)
$new-string: $new-string + str-slice($string, $i, $i)
@return quote(str-replace(str-insert($new-string, $new, $index), $old, $new, $case-sensitive))
@return $string
=svg($filename)
background-image: url($filename)
+no-svg
background-image: url(str-replace($filename, ".svg", ".png"))
=no-svg()
.no-svg &
@content
div
+svg("http://placehold.it/200x100.svg")
width: 200px
height: 100px
div {
background-image: url("http://placehold.it/200x100.svg");
width: 200px;
height: 100px;
}
.no-svg div {
background-image: url("http://placehold.it/200x100.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment