Current section

Files

Jump to
semtex lib semtex.ex
Raw

lib/semtex.ex

defmodule Semtex do
@config %{
"allowed_attributes" => %{
"all_tags" => ["lang", "title"],
"by_tag" => %{
"a" => ["href", "hreflang"],
"bdo" => ["dir"],
"blockquote" => ["cite"],
"col" => ["align", "char", "charoff", "span"],
"colgroup" => ["align", "char", "charoff", "span"],
"del" => ["cite", "datetime"],
"hr" => ["align", "size", "width"],
"img" => ["align", "alt", "height", "src", "width"],
"ins" => ["cite", "datetime"],
"ol" => ["start"],
"q" => ["cite"],
"table" => ["align", "char", "charoff", "summary"],
"tbody" => ["align", "char", "charoff"],
"td" => ["align", "char", "charoff", "colspan", "headers", "rowspan"],
"tfoot" => ["align", "char", "charoff"],
"th" => ["align", "char", "charoff", "colspan", "headers", "rowspan", "scope"],
"thead" => ["align", "char", "charoff"],
"tr" => ["align", "char", "charoff"]
}
},
"allowed_tags" => [
"a",
"abbr",
"acronym",
"area",
"article",
"aside",
"b",
"bdi",
"bdo",
"blockquote",
"br",
"caption",
"center",
"cite",
"code",
"col",
"colgroup",
"data",
"dd",
"del",
"details",
"dfn",
"div",
"dl",
"dt",
"em",
"figcaption",
"figure",
"footer",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"i",
"img",
"ins",
"kbd",
"kbd",
"li",
"map",
"mark",
"nav",
"ol",
"p",
"pre",
"q",
"rp",
"rt",
"rtc",
"ruby",
"s",
"samp",
"small",
"span",
"strike",
"strong",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"th",
"thead",
"time",
"tr",
"tt",
"u",
"ul",
"var",
"wbr"
],
"url_encode_attributes" => ["href", "src"],
"allowed_url_scheme_attributes" => ["href", "src"],
"allowed_url_schemes" => [
"bitcoin",
"ftp",
"ftps",
"geo",
"http",
"https",
"im",
"irc",
"ircs",
"magnet",
"mailto",
"mms",
"mx",
"news",
"nntp",
"openpgp4fpr",
"sip",
"sms",
"smsto",
"ssh",
"tel",
"url",
"webcal",
"wtai",
"xmpp"
],
"anchor_rel_values" => ["noopener", "noreferrer", "nofollow"],
"strip_comments" => true,
"escape_serialization" => true
}
def config() do
@config
end
def sanitize_str_fragment!(raw_html, config \\ @config) when is_binary(raw_html) do
raw_html
|> String.trim()
|> Semtex.Parser.parse_fragment!(config)
|> Semtex.Sanitizer.sanitize!(config)
|> Semtex.Serializer.serialize!(config)
end
end