functions.scss 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// Urlencode the SVG string
  2. /// @param {String} $svg - SVG image to encode
  3. /// @return {String} - Encoded SVG data uri
  4. @function swiper-svg-uri($svg) {
  5. $encoded: '';
  6. $chunkSize: 2048;
  7. $index: 0;
  8. $loops: ceil(str-length($svg) / $chunkSize);
  9. $map: (
  10. "%": "%25",
  11. "<": "%3C",
  12. ">": "%3E",
  13. " ": "%20",
  14. "!": "%21",
  15. "*": "%2A",
  16. '"': "%22",
  17. ";": "%3B",
  18. ":": "%3A",
  19. "@": "%40",
  20. "&": "%26",
  21. "=": "%3D",
  22. "+": "%2B",
  23. "$": "%24",
  24. ",": "%2C",
  25. "/": "%2F",
  26. "?": "%3F",
  27. "#": "%23",
  28. "[": "%5B",
  29. "]": "%5D"
  30. );
  31. @for $i from 1 through $loops {
  32. $chunk: str-slice($svg, $index, $index + $chunkSize - 1);
  33. @each $search, $replace in $map {
  34. $chunk: swiper-str-replace($chunk, $search, $replace);
  35. }
  36. $encoded: #{$encoded}#{$chunk};
  37. $index: $index + $chunkSize;
  38. }
  39. @return url("data:image/svg+xml;charset=utf-8,#{$encoded}");
  40. }
  41. /// Replace `$search` with `$replace` in `$string`
  42. /// @author Hugo Giraudel
  43. /// @link http://sassmeister.com/gist/1b4f2da5527830088e4d
  44. /// @param {String} $string - Initial string
  45. /// @param {String} $search - Substring to replace
  46. /// @param {String} $replace ('') - New value
  47. /// @return {String} - Updated string
  48. @function swiper-str-replace($string, $search, $replace: '') {
  49. $index: str-index($string, $search);
  50. @if $index {
  51. @return str-slice($string, 1, $index - 1) + $replace + swiper-str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
  52. }
  53. @return $string;
  54. }