【问题标题】:Test for unicode character in SASS在 SASS 中测试 unicode 字符
【发布时间】:2016-06-30 16:01:50
【问题描述】:

如何在 SASS mixin 中测试字符是否为 unicode?​​p>

我有一个 mixin,我想接受 (1) unicode 字符或 (2) 描述符字符串作为参数。如果参数是 unicode 字符,那么我只想显示它。如果参数是描述符字符串,我会查找 unicode 字符以配合它。

例如:

@include glyph('\f002');
@include glyph('search');

mixin 类似于:

@mixin glyph($glyph) {
  @if type-of($glyph) == unicode {
    content: '$glyph';
  } @ else {
    content: get_glyph_for($glyph);
  }
}

type-of($glyph) 返回string,因为没有“unicode”类型。

我没有在 SASS 中找到正则表达式功能,也没有找到拆分单个 unicode 字符的方法。

有没有办法可以使用 SASS 运行正则表达式,或者将角色分开?

【问题讨论】:

  • 我看不出如何使用有限数量的 Sass 函数来实现。我只能建议您更改您的 mixin 以接受第二个参数来指示第一个参数是否为 unicode。

标签: regex unicode sass string-matching


【解决方案1】:

您可以使用 str-slice 来检测第一个字符是否为“\”。为了将 unicode 字符转换为字符串,首先您必须在“inspect”中运行它。

str-slice(inspect(\f002),0,1)   => "\\"

http://sass-lang.com/documentation/Sass/Script/Functions.html#str_slice-instance_method

【讨论】:

  • 如果我不引用 unicode,这确实可以捕捉到它。引用 unicode 会破坏它,但是很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2010-12-13
  • 1970-01-01
相关资源
最近更新 更多