Skip to content

Commit

Permalink
#10: Escape # character in svg-background
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpoort committed Mar 14, 2018
1 parent e25c782 commit 64b1191
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions scss/tools/_tools.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@import "functions/breakpointMap";
@import "functions/calcInterpolation";
@import "functions/spacing";
@import "functions/strReplace";

// Mixins
@import "mixins/breakpoint";
Expand Down
16 changes: 16 additions & 0 deletions scss/tools/functions/_strReplace.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Replace `$search` with `$replace` in `$string`
/// @author Hugo Giraudel
/// @param {String} $string - Initial string
/// @param {String} $search - Substring to replace
/// @param {String} $replace ('') - New value
/// @return {String} - Updated string

@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;
}
2 changes: 1 addition & 1 deletion scss/tools/mixins/_svg-background.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@if $viewboxHeight == false {
$viewboxHeight: $viewboxWidth;
}
background-image: url('data:image/svg+xml;utf-8,<svg style="fill: #{$color};" xmlns="http://www.w3.org/2000/svg" width="#{$width}" height="#{$height}" viewBox="0 0 #{$viewboxWidth} #{$viewboxHeight}">#{$svg}</svg>');
background-image: url(str-replace('data:image/svg+xml;utf-8,<svg style="fill: #{$color};" xmlns="http://www.w3.org/2000/svg" width="#{$width}" height="#{$height}" viewBox="0 0 #{$viewboxWidth} #{$viewboxHeight}">#{$svg}</svg>', '#', '%23'));
background-position: $background-position;
background-repeat: $background-repeat;
background-size: #{$width}px #{$height}px;
Expand Down
2 changes: 1 addition & 1 deletion tests/mixins/_svg-background.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.foo {
@include svg-background('<g transform="matrix(2.18679,0,0,2.18679,5.43964,-0.0421697)"><path d="M5.9 5.3L0.5 0.1C0.4 0 0.2 0 0.1 0.1 0 0.2 0 0.4 0.1 0.5L5.2 5.5 0.1 10.5C0 10.6 0 10.8 0.1 10.9 0.2 11 0.2 11 0.3 11 0.4 11 0.5 11 0.5 10.9L5.9 5.7C6 5.6 6 5.4 5.9 5.3Z"/></g>', black, 16);
@include svg-background('<g transform="matrix(2.18679,0,0,2.18679,5.43964,-0.0421697)"><path d="M5.9 5.3L0.5 0.1C0.4 0 0.2 0 0.1 0.1 0 0.2 0 0.4 0.1 0.5L5.2 5.5 0.1 10.5C0 10.6 0 10.8 0.1 10.9 0.2 11 0.2 11 0.3 11 0.4 11 0.5 11 0.5 10.9L5.9 5.7C6 5.6 6 5.4 5.9 5.3Z"/></g>', #f00, 16);
}

0 comments on commit 64b1191

Please sign in to comment.