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
Functions
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}]
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}]
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;"