mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
refactor(sanitizer): simplify hasRequiredAttributes
This function takes around 1.5% of the total CPU time on my instance, and most of it is spent in `mapassign_faststr` to initialize the `map`. This is replaced with a switch-case construct, that should be both significantly faster as well as pretty dull in term of memory consumption.
This commit is contained in:
parent
92a49d7e69
commit
331c831c23
1 changed files with 9 additions and 17 deletions
|
@ -294,24 +294,16 @@ func isPixelTracker(tagName string, attributes []html.Attribute) bool {
|
|||
}
|
||||
|
||||
func hasRequiredAttributes(tagName string, attributes []string) bool {
|
||||
elements := map[string][]string{
|
||||
"a": {"href"},
|
||||
"iframe": {"src"},
|
||||
"img": {"src"},
|
||||
"source": {"src", "srcset"},
|
||||
switch tagName {
|
||||
case "a":
|
||||
return slices.Contains(attributes, "href")
|
||||
case "iframe", "img":
|
||||
return slices.Contains(attributes, "src")
|
||||
case "source":
|
||||
return slices.Contains(attributes, "src") || slices.Contains(attributes, "srcset")
|
||||
default:
|
||||
return true
|
||||
}
|
||||
|
||||
if attrs, ok := elements[tagName]; ok {
|
||||
for _, attribute := range attributes {
|
||||
if slices.Contains(attrs, attribute) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue