Sass 3.4 Removing forward slash on a string

纵然是瞬间 提交于 2019-12-01 10:51:02

You stumbled across a currently-debated issue with escape characters in Sass. It seems that, currently, Sass will either add too many characters, or winds up putting the unicode character into the output css.

UPDATE:

@mixin icon ($name, $code) {
  $withslash: "\"\\#{$code}\"";
  .#{$name}:before {
    content: unquote($withslash);
  }
}
@include icon('test', '4556');

outputs

.test:before {
  content: "\4556";
}

http://sassmeister.com/gist/04f5be11c5a6b26b8ff9

Obviously the drawback to this approach is that it relies on doing weird things with an escape character and tricking Sass into unquoting a possibly malformed string.

Alternatively, you can use a helper function to delete whitespace from a string:

@function nospaces($str) {
  @while (str-index($str, ' ') != null) {
    $index: str-index($str, ' ');
    $str: "#{str-slice($str, 0, $index - 1)}#{str-slice($str, $index + 1)}";
  }
  @return $str;
}

Then the function would look like this:

@mixin icon ($name, $code) {
  .#{$name}:before {
    content: nospaces("\ #{$code}");
  }
}

@include icon('test', '4556');

Or without quotes:

@mixin icon ($name, $code) {
  .#{$name}:before {
    content: nospaces(\ #{$code});
  }
}

@include icon('test', '4556');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!