Skip to content

Commit

Permalink
Merge pull request #80 from backspace/tbody-with-class
Browse files Browse the repository at this point in the history
tbody with class does not make it to output
  • Loading branch information
danschultzer committed Apr 7, 2023
2 parents 4ab6500 + 2631242 commit a59de8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.3.18 (TBA)

* Fixed bug in `Premailex.HTMLToPlainText.parse/3` with `<thread>`, `<tbody>`, `<tfoot>` being excluded if the HTML element had any attributes

## v0.3.17 (2023-02-21)

* `Premailex.HTMLInlineStyles.process/3` now warns when styles can't be loaded from URL's
Expand Down
6 changes: 3 additions & 3 deletions lib/premailex/html_to_plain_text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ defmodule Premailex.HTMLToPlainText do

defp flatten_table_elements(elements), do: Enum.flat_map(elements, &flatten_table_element/1)

defp flatten_table_element({"thead", [], table_cells}), do: table_cells
defp flatten_table_element({"tbody", [], table_cells}), do: table_cells
defp flatten_table_element({"tfoot", [], table_cells}), do: table_cells
defp flatten_table_element({"thead", _, table_cells}), do: table_cells
defp flatten_table_element({"tbody", _, table_cells}), do: table_cells
defp flatten_table_element({"tfoot", _, table_cells}), do: table_cells
defp flatten_table_element(elem), do: [elem]

defp wordwrap(text) do
Expand Down
19 changes: 19 additions & 0 deletions test/premailex/html_to_plain_text_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,23 @@ defmodule Premailex.HTMLToPlainTextTest do
test "process/1" do
assert Premailex.HTMLToPlainText.process(@input) == String.trim(@parsed)
end

test "process/1 with attributes" do
assert Premailex.HTMLToPlainText.process(add_attributes(@input)) == String.trim(@parsed)
end

defp add_attributes(html) do
html
|> Premailex.HTMLParser.parse()
|> add_attribute("data-attribute", "value")
|> Premailex.HTMLParser.to_string()
end

defp add_attribute(elements, key, value) when is_list(elements) do
Enum.map(elements, &add_attribute(&1, key, value))
end
defp add_attribute({tag, attrs, children}, key, value) do
{tag, attrs ++ [{key, value}], add_attribute(children, key, value)}
end
defp add_attribute(other, _key, _value), do: other
end

0 comments on commit a59de8e

Please sign in to comment.