Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Apr 15, 2018
1 parent e5665e4 commit efa2281
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/premailex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Premailex do
"<html><head><style>p{background-color: #fff;}</style></head><body><p style=\\\"background-color:#fff;color:#000;\\\">Text</p></body></html>"
"""
@spec to_inline_css(String.t()) :: String.t()
def to_inline_css(html) do
Premailex.HTMLInlineStyles.process(html)
end
Expand All @@ -25,6 +26,7 @@ defmodule Premailex do
"Text"
"""
@spec to_text(String.t()) :: String.t()
def to_text(html) do
html
|> Floki.find("body")
Expand Down
10 changes: 9 additions & 1 deletion lib/premailex/css_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ defmodule Premailex.CSSParser do
Module that handles CSS parsing with naive Regular Expression.
"""

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

@css_selector_rules ~r/([\s\S]*?){([\s\S]*?)}/mi

@non_id_attributes_and_pseudo_classes ~r/
Expand Down Expand Up @@ -45,6 +48,7 @@ defmodule Premailex.CSSParser do
selector: "body",
specificity: 1}]
"""
@spec parse(String.t()) :: [rule_set]
def parse(""), do: []

def parse(css) do
Expand Down Expand Up @@ -79,6 +83,7 @@ defmodule Premailex.CSSParser do
[%{directive: "background-color", value: "#fff", important?: false},
%{directive: "color", value: "red", important?: false}]
"""
@spec parse_rules(String.t()) :: [rule]
def parse_rules(rules) do
rules
|> String.split(";")
Expand Down Expand Up @@ -116,10 +121,12 @@ defmodule Premailex.CSSParser do
## Examples
iex> "p {background-color: #fff !important; color: #000;} p {background-color: #000;}" |> Premailex.CSSParser.parse() |> Premailex.CSSParser.merge()
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}]
"""
@spec merge([rule_set]) :: [rule_set]
def merge(rule_sets) do
rule_sets
|> Enum.map(fn rule_set ->
Expand Down Expand Up @@ -165,6 +172,7 @@ defmodule Premailex.CSSParser do
iex> Premailex.CSSParser.to_string(%{directive: "background-color", value: "#fff"})
"background-color:#fff;"
"""
@spec to_string([rule]) :: String.t()
def to_string(rules) when is_list(rules),
do: Enum.reduce(rules, "", &"#{&2}#{__MODULE__.to_string(&1)}")

Expand Down
1 change: 1 addition & 0 deletions lib/premailex/html_inline_styles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Premailex.HTMLInlineStyles do
alias Premailex.{CSSParser, HTMLParser, Util}

@doc false
@spec process(String.t()) :: String.t()
def process(html) do
tree = HTMLParser.parse(html)

Expand Down
2 changes: 1 addition & 1 deletion lib/premailex/html_to_plain_text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule Premailex.HTMLToPlainText do
@moduledoc """
Module that converts HTML emails to plain text.
"""

alias Premailex.Util

@doc """
Expand All @@ -14,6 +13,7 @@ defmodule Premailex.HTMLToPlainText do
"* Test"
"""
@spec process(String.t() | Util.html_tree()) :: String.t()
def process(html) when is_binary(html), do: html |> Floki.parse() |> process()

def process(html) do
Expand Down

0 comments on commit efa2281

Please sign in to comment.