danschultzer / premailex

Preflight for your HTML emails - inline styling and plain text.
MIT License
170 stars 19 forks source link

traverse issues #34

Closed astjohn closed 5 years ago

astjohn commented 5 years ago

Thanks for this library. I'm keen to use it! We have noticed issues when using deeply nested <th> tags. For example:

<table class="body">
      <tr>
        <td class="center" align="center" valign="top">
          <center data-parsed>
            <table align="center" class="wrapper header float-center">
              <tr>
                <td class="wrapper-inner">
                  <table align="center" class="container">
                    <tbody>
                      <tr>
                        <td>
                          <table class="row collapse">
                            <tbody>
                              <tr>
                                <th class="small-12 large-12 columns first last">
                                  <table>
                                    <tr>
                                      <th>
                                        <center data-parsed>
                                          <p>Whatever</p>
                                        </center>
                                      </th>
                                      <th class="expander"></th>
                                    </tr>
                                  </table>
                                </th>
                              </tr>
                            </tbody>
                          </table>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </td>
              </tr>
            </table>
          </center>
        </td>
      </tr>
    </table>

The following snippet results in a blowup with this error:

(Protocol.UndefinedError) protocol String.Chars not implemented for {"th", [], [{"center", [{"data-parsed", "data-parsed"}], [""]}]}. This protocol is implemented for: Geo.Polygon, Geo.PointZ, Geo.MultiPoint, Geo.GeometryCollection, Geo.PointZM, Geo.Point, Geo.MultiPolygon, Geo.LineString, Geo.PointM, Geo.MultiLineString, Ecto.Date, Ecto.Time, Ecto.DateTime, Postgrex.Copy, Postgrex.Query, Postgrex.Stream, Floki.Selector.AttributeSelector, Floki.Selector, Floki.Selector.Functional, Floki.Selector.Combinator, Floki.Selector.PseudoClass, Decimal, Float, DateTime, Time, List, Version.Requirement, Atom, Integer, Version, Date, BitString, NaiveDateTime, URI...

stacktrace:
       (elixir) /private/tmp/elixir-20190114-93661-1hq8sk/elixir-1.8.0/lib/elixir/lib/string/chars.ex:3: String.Chars.impl_for!/1
       (elixir) /private/tmp/elixir-20190114-93661-1hq8sk/elixir-1.8.0/lib/elixir/lib/string/chars.ex:22: String.Chars.to_string/1
       (elixir) lib/enum.ex:1940: Enum."-join/2-lists^foldl/2-0-"/3
       (elixir) lib/enum.ex:1940: Enum.join/2
       (premailex) lib/premailex/util.ex:51: Premailex.Util.maybe_traverse/2
       (elixir) lib/enum.ex:1431: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (premailex) lib/premailex/util.ex:30: Premailex.Util.traverse/3
       (premailex) lib/premailex/html_to_plain_text.ex:134: Premailex.HTMLToPlainText.table/1
       (premailex) lib/premailex/util.ex:51: Premailex.Util.maybe_traverse/2
       (elixir) lib/enum.ex:1431: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (premailex) lib/premailex/util.ex:30: Premailex.Util.traverse/3
       (premailex) lib/premailex/util.ex:60: Premailex.Util.handle_traversed/3
       (premailex) lib/premailex/util.ex:51: Premailex.Util.maybe_traverse/2
       (elixir) lib/enum.ex:1431: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (premailex) lib/premailex/util.ex:30: Premailex.Util.traverse/3
       (premailex) lib/premailex/util.ex:60: Premailex.Util.handle_traversed/3
       (premailex) lib/premailex/util.ex:51: Premailex.Util.maybe_traverse/2
       (elixir) lib/enum.ex:1431: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (premailex) lib/premailex/util.ex:30: Premailex.Util.traverse/3
       (premailex) lib/premailex/util.ex:60: Premailex.Util.handle_traversed/3

Replacing the th tags with td works. I tried naively inserting the table snippet into the test suite to try to get the tests to break, but they passed. So I'm not sure what I'm missing and have yet to dig in too deep to the traverse logic.

Any help is greatly appreciated! Thanks!

astjohn commented 5 years ago

This looks likely due to Premailex.HTMLToPlainText.table_rows/1 not being able to handle th tags. Will keep digging.

danschultzer commented 5 years ago

Yeah, it's the table_rows/1 method that doesn't handle <th>. I'm working on a fix right now 😄

astjohn commented 5 years ago

sweet. thank you!

danschultzer commented 5 years ago

You can try out #35 now, I'll merge to master later and publish a new release.

danschultzer commented 5 years ago

v0.3.5 has been released. Thanks for letting me know of this issue!