From 435a950d6451c47858dd8b5e831284fc1da5a332 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 1 Jul 2025 16:17:14 +0200 Subject: [PATCH] refactor(sanitizer): minor refactorization Use a proper switch-case instead of a bunch of if. --- internal/reader/sanitizer/sanitizer.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/internal/reader/sanitizer/sanitizer.go b/internal/reader/sanitizer/sanitizer.go index 299a976b..4c6e30a7 100644 --- a/internal/reader/sanitizer/sanitizer.go +++ b/internal/reader/sanitizer/sanitizer.go @@ -312,17 +312,20 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attribu } for _, attribute := range attributes { - value := attribute.Val - if !isValidAttribute(tagName, attribute.Key) { continue } - if tagName == "math" && attribute.Key == "xmlns" && value != "http://www.w3.org/1998/Math/MathML" { - value = "http://www.w3.org/1998/Math/MathML" - } + value := attribute.Val - if tagName == "img" { + switch tagName { + case "math": + if attribute.Key == "xmlns" { + if value != "http://www.w3.org/1998/Math/MathML" { + value = "http://www.w3.org/1998/Math/MathML" + } + } + case "img": switch attribute.Key { case "fetchpriority": if !isValidFetchPriorityValue(value) { @@ -336,11 +339,13 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attribu if isImageLargerThanLayout || !isPositiveInteger(value) { continue } + case "srcset": + value = sanitizeSrcsetAttr(baseURL, value) + } + case "source": + if attribute.Key == "srcset" { + value = sanitizeSrcsetAttr(baseURL, value) } - } - - if (tagName == "img" || tagName == "source") && attribute.Key == "srcset" { - value = sanitizeSrcsetAttr(baseURL, value) } if isExternalResourceAttribute(attribute.Key) {