SASS is prepending unicode content with backslash (\)

♀尐吖头ヾ 提交于 2019-12-12 10:48:40

问题


Problem

I'm trying to produce some unicode characters after compiling my *.scss file.

As an example, I have the following (SCSS):

.element:after {
    content: "\a0";
}

When the file is compiled, it outputs the following (CSS):

.element:after {
    content: "\\a0";
}

Notice the extra unwanted backslash (\).

Attempted Solution #1

I did try the solution here: Sass: unicode escape is not preserved in .css file, which suggests introducing the following function:

@function unicode($str) {
    @return unquote("\"")+unquote(str-insert($str, "\\", 1))+unquote("\"")
}

And using it like so (SCSS):

.element:after {
    content: unicode("a0");
}

However, this produces the following (CSS)

.element:after {
    content: "\\" ")+unquote(str-insert($str, " \\\\ ", 1))+unquote(" \\ ""; 
}

Notice, it's not even invoking the function as intended. Why is that?

Project Details

I'm using these libraries in Maven:

<dependency>
    <groupId>net.jawr</groupId>
    <artifactId>jawr-core</artifactId>
    <version>3.9</version>
</dependency>

<dependency>
    <groupId>net.jawr.extensions</groupId>
    <artifactId>jawr-spring-extension</artifactId>
    <version>3.9</version>
</dependency>

<dependency>
    <groupId>com.darrinholst</groupId>
    <artifactId>sass-java-gems</artifactId>
    <version>3.4.20.0</version>
</dependency>

<dependency>
    <groupId>org.jruby</groupId>
    <artifactId>jruby-core</artifactId>
    <version>9.1.5.0</version>
</dependency>

<dependency>
    <groupId>org.jruby</groupId>
    <artifactId>jruby-stdlib</artifactId>
    <version>9.1.5.0</version>
</dependency>

Temporary solution

Don't use unicodes in SCSS. Instead, use Font Awesome in the HTML (keeping Font Awesome in a CSS file).


回答1:


I had the same issue while trying to use a custom icons font. The solution I found is creating a mixin which looks like this :

@mixin class-for-icon($name, $code) {
    .icon-#{$name}::before {
        content: unquote("\"\\#{$code}\"");
    }
}

And then I can use it like this :

@include class-for-icon('myIcon', 'e1ff');

/* generates */

.icon-myIcon::before {
    content: "\e1ff";
}

I use node-sass 4.5.3 to compile my sass files into css, and this is working well (currently using it in production on a few projects)

Hope this helps !



来源:https://stackoverflow.com/questions/40375261/sass-is-prepending-unicode-content-with-backslash

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