用于解析和创建 CSS 样式表的轻量级 CSS 解析器

SwiftCSSParser

一个用于 Swift 的轻量级 CSS 解析器,在引擎盖下使用 cssparser (cpp)。

基本用法

这里有一个简单的代码片段可以帮助您入门。

// An example stylesheet (with horrible formatting)
let css = """
div { color: blue
  }
"""

// Parse the stylesheet
let stylesheet = try Stylesheet.parse(css)

print(stylesheet.minified())
// div{color:blue}

print(stylesheet.prettyPrinted(with: .spaces(2))
// div {
//   color: blue;
// }

print(stylesheet)
// Stylesheet([
//     .ruleSet(RuleSet(
//         selector: "div",
//         declarations: [
//             Declaration(property: "color", value: "blue")
//         ]
//     ))
// ])

语句

主要的 CSS 解析 API 建立在 CSS 语句的概念之上。它是 SwiftCSSParser 提供的用于解析和创建 CSS 文档的最容易使用和类型最安全的 API。但是,语句 API 的主要限制是它不处理注释,也可能只是忽略它认为无效的令牌。如果发现从文档生成的语句错误地忽略有效令牌的任何情况,请打开问题。

若要将文档分析为语句,请使用该方法。这相当于 。Stylesheet.parseStatements(from:)Stylesheet.parse(from: css).statements

令 牌

基于令牌的 API 是语句 API 的较低级别和更简单的版本。它是 API 解析方面的直接 Swift 翻译。cssparser

To parse a document into tokens, use the method. The resulting token stream includes comments.Stylesheet.parseTokens(from:)

Contributing

# 1. Fork swift-css-parser
# 2. Clone your fork
git clone https://github.com/yourusername/swift-css-parser 

# 3. Finish cloning the repository
cd swift-css-parser
git submodule update --init --recursive

GitHub

点击跳转