View Source Premailex.CSSParser (Premailex v0.3.20)

Module that handles CSS parsing with naive Regular Expression.

Summary

Functions

Merges CSS rules.

Parses a CSS string into a map.

Parses a CSS rules string into a map.

Transforms CSS map or list into string.

Types

rule()

@type rule() :: %{directive: String.t(), value: String.t(), important?: boolean()}

rule_set()

@type rule_set() :: %{rules: [rule()], selector: String.t(), specificity: number()}

Functions

merge(rule_sets)

@spec merge([rule_set()]) :: [rule_set()]

Merges CSS rules.

Examples

iex> rule_sets = Premailex.CSSParser.parse("p {background-color: #fff !important; color: #000;} p {background-color: #000;}")
iex> Premailex.CSSParser.merge(rule_sets)
[%{directive: "background-color", value: "#fff !important", important?: true, specificity: 1},
 %{directive: "color", value: "#000", important?: false, specificity: 1}]

parse(css)

@spec parse(String.t()) :: [rule_set()]

Parses a CSS string into a map.

Examples

iex> Premailex.CSSParser.parse("body { background-color: #fff !important; color: red; }")
[%{rules: [%{directive: "background-color", value: "#fff !important", important?: true},
           %{directive: "color", value: "red", important?: false}],
   selector: "body",
   specificity: 1}]

parse_rules(rules)

@spec parse_rules(String.t()) :: [rule()]

Parses a CSS rules string into a map.

Note: parse_rules/1 won't strip any CSS comments unlike parse/1.

Examples

iex> Premailex.CSSParser.parse_rules("background-color: #fff; color: red;")
[%{directive: "background-color", value: "#fff", important?: false},
 %{directive: "color", value: "red", important?: false}]

to_string(rules)

@spec to_string([rule()]) :: String.t()

Transforms CSS map or list into string.

Examples

iex> Premailex.CSSParser.to_string([%{directive: "background-color", value: "#fff"}, %{directive: "color", value: "#000"}])
"background-color: #fff; color: #000;"

iex> Premailex.CSSParser.to_string(%{directive: "background-color", value: "#fff"})
"background-color: #fff;"