Skip to content

Swift CSS parser based on katana-parser

License

Notifications You must be signed in to change notification settings

Hanazawa/CSSParser

 
 

Repository files navigation

CSSParser is a tiny Swift wrapper around the pure C katana-parser framework.

Basic Usage

This framework is intended to be used with any kind of document that benefits from styling via stylesheets. It works by first parsing the contents of a stylesheet, and then providing hooks to query this parsed stylesheet to see which styles apply to a given node in an element tree.

The basic parts of the CSS spec are implemented, allowing for things like sibling selectors, attribute selectors, and so on.

A basic example:

let sheet = "box { height: 100 } #blah { flex: 1}"
let parsed = StyleSheet(string: sheet)!

// SomeElement conforms to the StyleElement protocol.
let element = SomeElement(tagName: "box")
let styles = parsed.stylesForElement(element)

// Values come back as strings and must be parsed into their corresponding type.
element.height = NumberFormatter().number(from: styles["height"])?.intValue ?? 0

Considerations

Much of the CSS specification is not handled, but the framework is generic enough so that it can be easily extended. Contributions are welcome!

About

Swift CSS parser based on katana-parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 67.9%
  • C 30.4%
  • Ruby 1.7%