소스 검색

perf(sanitizer): don't do a map lookup for every attribute of a tag

There is no need to do a lookup in allowedHTMLTagsAndAttributes in the loop,
as the tag never changes in this function. This is a minor optimization,
but since sanitizeAttributes is in the hot path of SanitizeHTML, it should be
worthwhile.
jvoisin 5 달 전
부모
커밋
bf24fd1ec9
1개의 변경된 파일8개의 추가작업 그리고 9개의 파일을 삭제
  1. 8 9
      internal/reader/sanitizer/sanitizer.go

+ 8 - 9
internal/reader/sanitizer/sanitizer.go

@@ -329,12 +329,17 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, tagName string, attributes []htm
 	htmlAttrs := make([]string, 0, len(attributes))
 	attrNames := make([]string, 0, len(attributes))
 
-	var err error
 	var isAnchorLink bool
 	var isYouTubeEmbed bool
 
+	allowedAttributes, ok := allowedHTMLTagsAndAttributes[tagName]
+	if !ok {
+		// This should never happen, as the tag was validated in the caller of `sanitizeAttributes`
+		return []string{}, ""
+	}
+
 	for _, attribute := range attributes {
-		if !isValidAttribute(tagName, attribute.Key) {
+		if !slices.Contains(allowedAttributes, attribute.Key) {
 			continue
 		}
 
@@ -389,6 +394,7 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, tagName string, attributes []htm
 				value = attribute.Val
 				isAnchorLink = true
 			default:
+				var err error
 				value, err = urllib.ResolveToAbsoluteURLWithParsedBaseURL(parsedBaseUrl, value)
 				if err != nil {
 					continue
@@ -455,13 +461,6 @@ func getExtraAttributes(tagName string, isYouTubeEmbed bool, sanitizerOptions *S
 	}
 }
 
-func isValidAttribute(tagName, attributeName string) bool {
-	if attributes, ok := allowedHTMLTagsAndAttributes[tagName]; ok {
-		return slices.Contains(attributes, attributeName)
-	}
-	return false
-}
-
 func isExternalResourceAttribute(attribute string) bool {
 	switch attribute {
 	case "src", "href", "poster", "cite":