a few more html tags, probably unlikely in AP though

This commit is contained in:
Ted Unangst 2019-05-17 19:11:17 -04:00
parent cead81bccb
commit 1813bb7c39
1 changed files with 12 additions and 8 deletions

20
html.go
View File

@ -28,11 +28,14 @@ import (
"golang.org/x/net/html" "golang.org/x/net/html"
) )
var permittedtags = []string{"div", "h1", "h2", "h3", "h4", "h5", "h6", var permittedtags = []string{
"table", "thead", "tbody", "th", "tr", "td", "div", "h1", "h2", "h3", "h4", "h5", "h6",
"p", "br", "pre", "code", "blockquote", "table", "thead", "tbody", "th", "tr", "td", "colgroup", "col",
"strong", "em", "b", "i", "s", "u", "sub", "sup", "del", "p", "br", "pre", "code", "blockquote", "q",
"ol", "ul", "li"} "samp", "mark", "ins", "dfn", "cite", "abbr", "address",
"strong", "em", "b", "i", "s", "u", "sub", "sup", "del", "tt", "small",
"ol", "ul", "li", "dl", "dt", "dd",
}
var permittedattr = []string{"colspan", "rowspan"} var permittedattr = []string{"colspan", "rowspan"}
var bannedtags = []string{"script", "style"} var bannedtags = []string{"script", "style"}
@ -72,8 +75,7 @@ func writetag(w io.Writer, node *html.Node) {
} }
func render(w io.Writer, node *html.Node) { func render(w io.Writer, node *html.Node) {
switch node.Type { if node.Type == html.ElementNode {
case html.ElementNode:
tag := node.Data tag := node.Data
switch { switch {
case tag == "a": case tag == "a":
@ -99,12 +101,14 @@ func render(w io.Writer, node *html.Node) {
case contains(bannedtags, tag): case contains(bannedtags, tag):
return return
} }
case html.TextNode: } else if node.Type == html.TextNode {
io.WriteString(w, html.EscapeString(node.Data)) io.WriteString(w, html.EscapeString(node.Data))
} }
for c := node.FirstChild; c != nil; c = c.NextSibling { for c := node.FirstChild; c != nil; c = c.NextSibling {
render(w, c) render(w, c)
} }
if node.Type == html.ElementNode { if node.Type == html.ElementNode {
tag := node.Data tag := node.Data
if tag == "a" || (contains(permittedtags, tag) && tag != "br") { if tag == "a" || (contains(permittedtags, tag) && tag != "br") {