Created
January 13, 2014 19:14
-
-
Save alice-liu/8406223 to your computer and use it in GitHub Desktop.
String replace function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.3.0.rc.2) | |
// Compass (v1.0.0.alpha.17) | |
// ---- | |
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
div { | |
background-image: url("http://placehold.it/200x100.svg"); | |
width: 200px; | |
height: 100px; | |
} | |
.no-svg div { | |
background-image: url("http://placehold.it/200x100.png"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment