解析样式表

Sass 样式表是从一系列 Unicode 代码点解析而来的。它被直接解析,无需首先转换为令牌流。

输入编码输入编码 permalink

¥Input Encoding

兼容性:
Dart Sass
LibSass
Ruby Sass

Dart Sass 目前仅支持 UTF-8 编码。因此,将所有 Sass 样式表编码为 UTF-8 是最安全的。

¥Dart Sass currently only supports the UTF-8 encoding. As such, it’s safest to encode all Sass stylesheets as UTF-8.

通常情况下,文档最初仅以字节序列形式提供,必须将其解码为 Unicode。Sass 按如下方式执行此解码:

¥It’s often the case that a document is initially available only as a sequence of bytes, which must be decoded into Unicode. Sass performs this decoding as follows:

  • 如果字节序列以 U+FEFF BYTE ORDER MARKUTF-8 或 UTF-16 编码开头,则使用相应的编码。

    ¥If the sequence of bytes begins with the UTF-8 or UTF-16 encoding of U+FEFF BYTE ORDER MARK, the corresponding encoding is used.

  • 如果字节序列以纯 ASCII 字符串 @charset 开头,Sass 将使用 CSS 算法的步骤 2 确定 确定后备编码 的编码。

    ¥If the sequence of bytes begins with the plain ASCII string @charset, Sass determines the encoding using step 2 of the CSS algorithm for determining the fallback encoding.

  • 否则,使用 UTF-8。

    ¥Otherwise, UTF-8 is used.

解析错误解析错误 permalink

¥Parse Errors

当 Sass 在样式表中遇到无效语法时,解析将失败,并向用户显示错误,其中包含有关无效语法的位置及其无效原因的信息。

¥When Sass encounters invalid syntax in a stylesheet, parsing will fail and an error will be presented to the user with information about the location of the invalid syntax and the reason it was invalid.

请注意,这与 CSS 不同,CSS 指定如何从大多数错误中恢复而不是立即失败。这是 SCSS 严格来说不是 CSS 超集的少数情况之一。然而,对于 Sass 用户来说,立即看到错误比将错误传递到 CSS 输出更有用。

¥Note that this is different than CSS, which specifies how to recover from most errors rather than failing immediately. This is one of the few cases where SCSS isn’t strictly a superset of CSS. However, it’s much more useful to Sass users to see errors immediately, rather than having them passed through to the CSS output.

可以通过特定于实现的 API 访问解析错误的位置。例如,在 Dart Sass 中你可以访问 SassException.span,在 Node Sass 和 Dart Sass 的 JS API 中你可以访问 filelinecolumn 属性。

¥The location of parse errors can be accessed through implementation-specific APIs. For example, in Dart Sass you can access SassException.span, and in Node Sass’s and Dart Sass’s JS API you can access the file, line, and column properties.