Premailex v0.2.2 Premailex.CSSParser View Source

Module that handles CSS parsing with naive Regular Expression.

Link to this section 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

Link to this section Types

Link to this type rule() View Source
rule() :: %{directive: String.t(), value: String.t(), important?: boolean()}
Link to this type rule_set() View Source
rule_set() :: %{rules: [rule()], selector: String.t(), specificity: number()}

Link to this section Functions

Link to this function merge(rule_sets) View Source
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}]

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}]
Link to this function parse_rules(rules) View Source
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}]
Link to this function to_string(rules) View Source
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;"