1
0
Fork 0
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:
Julien Voisin 2024-12-08 00:30:15 +00:00 committed by GitHub
parent 92a49d7e69
commit 331c831c23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -294,24 +294,16 @@ func isPixelTracker(tagName string, attributes []html.Attribute) bool {
} }
func hasRequiredAttributes(tagName string, attributes []string) bool { func hasRequiredAttributes(tagName string, attributes []string) bool {
elements := map[string][]string{ switch tagName {
"a": {"href"}, case "a":
"iframe": {"src"}, return slices.Contains(attributes, "href")
"img": {"src"}, case "iframe", "img":
"source": {"src", "srcset"}, return slices.Contains(attributes, "src")
} case "source":
return slices.Contains(attributes, "src") || slices.Contains(attributes, "srcset")
if attrs, ok := elements[tagName]; ok { default:
for _, attribute := range attributes {
if slices.Contains(attrs, attribute) {
return true return true
} }
}
return false
}
return true
} }
// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml // See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml