From e5703328c8225c1c4980d61981f90ac67a2a521c Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Tue, 3 Oct 2017 18:46:11 +0500 Subject: [PATCH] :zap: Refactor checks attributes and values --- content.go | 50 +++++++++++--------------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/content.go b/content.go index 8116269..aafb35a 100644 --- a/content.go +++ b/content.go @@ -5,41 +5,7 @@ import ( "errors" "strings" - html "golang.org/x/net/html" -) - -var ( - availableTags = map[string]bool{ - "a": true, - "aside": true, - "b": true, - "blockquote": true, - "br": true, - "code": true, - "em": true, - "figcaption": true, - "figure": true, - "h3": true, - "h4": true, - "hr": true, - "i": true, - "iframe": true, - "img": true, - "li": true, - "ol": true, - "p": true, - "pre": true, - "s": true, - "strong": true, - "u": true, - "ul": true, - "video": true, - } - - availableAttributes = map[string]bool{ - "href": true, - "src": true, - } + "golang.org/x/net/html" ) // ContentFormat transforms data to a DOM-based format to represent the @@ -79,12 +45,18 @@ func domToNode(domNode *html.Node) interface{} { } var nodeElement NodeElement - if _, ok := availableTags[strings.ToLower(domNode.Data)]; ok { + switch strings.ToLower(domNode.Data) { + case "a", "aside", "b", "blockquote", "br", "code", "em", "figcaption", "figure", "h3", "h4", "hr", "i", "iframe", "img", "li", "ol", "p", "pre", "s", "strong", "u", "ul", "video": nodeElement.Tag = domNode.Data - for _, attr := range domNode.Attr { - if _, ok := availableAttributes[strings.ToLower(attr.Key)]; ok { - nodeElement.Attrs = map[string]string{attr.Key: attr.Val} + for i := range domNode.Attr { + switch strings.ToLower(domNode.Attr[i].Key) { + case "href", "src": + nodeElement.Attrs = map[string]string{ + domNode.Attr[i].Key: domNode.Attr[i].Val, + } + default: + continue } } }