Add stripping HTML attributes

This commit is contained in:
Thelonius Kort
2023-01-04 14:23:55 +01:00
parent 4949797343
commit 80a21b8606

View File

@ -3,6 +3,25 @@ defmodule Outlook.InternalTree.Html do
alias Outlook.InternalTree.InternalNode
alias Outlook.InternalTree.TranslationUnit
@desirable_atts %{"a" => [:href], "img" => [:src]}
def strip_attributes([%InternalNode{type: :element} = node | rest]) do
d_atts = Map.get(@desirable_atts, node.name, [])
atts = Map.reject(node.attributes, fn {k,_} -> k not in d_atts end)
[ %{node |
attributes: atts,
content: strip_attributes(node.content)
}
| strip_attributes(rest) ]
end
def strip_attributes([node | rest]) do
[ node | strip_attributes(rest)]
end
def strip_attributes([]), do: []
def to_html([ %InternalNode{type: :element} = node | rest]) do
attr_string = Map.put(node.attributes, :uuid, node.uuid)
|> Enum.map_join(" ", fn {k,v} -> "#{k}=\"#{v}\"" end)