A lightweight CSS parser for parsing and creating CSS stylesheets

Overview

SwiftCSSParser

A lightweight CSS parser for Swift that uses cssparser (cpp) under the hood.

Basic usage

Here's a simple code snippet to get you started.

// 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")
//         ]
//     ))
// ])

Statements

The main CSS parsing API is built on the concept of CSS statements. It is the easiest to use and most type-safe API that SwiftCSSParser offers for parsing and creating CSS documents. However, the main limitation of the statements API is that it does not handle comments, it also may just ignore tokens that it deems to be invalid. If you find any cases where the statements generated from a document incorrectly ignore a valid token, open an issue.

To parse a document into statements, use the Stylesheet.parseStatements(from:) method. This is equivalent to Stylesheet.parse(from: css).statements.

Tokens

The tokens based API is a lower level and more simplistic version of the statements API. It is a direct Swift translation of the parsing aspect of the cssparser API.

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

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
You might also like...
Swift-json - High-performance json parsing in swift

json 0.1.4 swift-json is a pure-Swift JSON parsing library designed for high-per

Developed with use Swift language. As a third party library used SDWebImage. JSON parsing using URLSession with TMDB API. This app provide by the Core Data structure.

Capstone Project 🎯 About Developed with use Swift language. As a third party library used SDWebImage. JSON parsing using URLSession with TMDB API. Ad

Functional JSON Parser - Linux Ready
Functional JSON Parser - Linux Ready

Functional JSON Parser Feature Linux Ready Type-safe JSON parsing Functional value transformation Easy to parse nested value Dependency free No define

JSONFeed parser for swift

JSONFeed Swift parsing for JSON Feed Spec Installation Carthage You can install Carthage with Homebrew using the following command: brew update brew i

A JSON parser with concise API written in Swift.
A JSON parser with concise API written in Swift.

A JSON parser with concise API written in Swift Maps JSON attributes to different Swift types with just two methods: map and mapArrayOfObjects. The li

An iOS framework for creating JSON-based models. Written in Swift.
An iOS framework for creating JSON-based models. Written in Swift.

An iOS framework for creating JSON-based models. Written in Swift (because it totally rules!) Requirements iOS 8.0+ Xcode 7.3 Swift 2.2 Installation E

JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift.

JSEN /ˈdʒeɪsən/ JAY-sən JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift. A JSON, as defined in the EC

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift

EVReflection General information At this moment the master branch is tested with Swift 4.2 and 5.0 beta If you want to continue using EVReflection in

A fast, convenient and nonintrusive conversion framework between JSON and model. Your model class doesn't need to extend any base class. You don't need to modify any model file.

MJExtension A fast, convenient and nonintrusive conversion framework between JSON and model. 转换速度快、使用简单方便的字典转模型框架 📜 ✍🏻Release Notes: more details Co

Comments
  • An example, how to read from `stylesheet.statements`

    An example, how to read from `stylesheet.statements`

    As the title states. It would be nice to have an example somewhere which states how to access the statements since they are as the enumeration Statement. Lots of people are not used to accessing properties by enum.

    opened by sems 3
Releases(v0.1.2)
Owner
Creator of Delta Client. Loves Swift, Rust and Svelte.
null
📱 A comprehensive test task for creating an autolayout interface, requesting an API and JSON parsing from Effective Mobile.

ECOMMERCE A comprehensive test task for creating an autolayout interface, requesting an API and JSON parsing from Effective Mobile. ??‍?? Design ✨ Fea

Daniel Tvorun 4 Nov 21, 2022
Networking, JSON Parsing, APIs and Core Location

Clima Our Goal It’s time to take our app development skills to the next level. We’re going to introduce you to the wonderful world of Application Prog

Dessana Caldeira M. Santos 1 Nov 25, 2021
A sweet and swifty YAML parser built on LibYAML.

Yams A sweet and swifty YAML parser built on LibYAML. Installation Building Yams requires Xcode 11.x or a Swift 5.1+ toolchain with the Swift Package

JP Simard 930 Jan 4, 2023
This framework implements a strict JSON parser and generator in Objective-C.

SBJson 5 Chunk-based JSON parsing and generation in Objective-C. Overview SBJson's number one feature is stream/chunk-based operation. Feed the parser

null 3.8k Jan 5, 2023
Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON.

JSONFeed Swift parser for JSON Feed — a new format similar to RSS and Atom but in JSON. For more information about this new feed format visit: https:/

Toto Tvalavadze 31 Nov 22, 2021
Jay - Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready.

Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization.

Danielle 132 Dec 5, 2021
Freddy - A reusable framework for parsing JSON in Swift.

Why Freddy? Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It ha

Big Nerd Ranch 1.1k Jan 1, 2023
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

?? Deprecation Notice ?? Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

Harlan Kellaway 1.6k Nov 24, 2022
Precise JSON decimal parsing for Swift 🧮

PreciseDecimal Introduction Swift has long suffered a problem with its Decimal type: unapparent loss of precision. This happens with all common ways o

David Roman 22 Dec 15, 2022
Ss-json - High-performance json parsing in swift

json 0.1.1 swift-json is a pure-Swift JSON parsing library designed for high-per

kelvin 43 Dec 15, 2022