1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-06 17:41:00 +00:00

refactor(icon): simplify findIconURLsFromHTMLDocument

- Don't define the queries before possible early returns
- Check for the presence of the href attribute in the queries, instead of later
  on iterating on the selection
- Add two edge-cases to the tests
- Use EachIter instead of Each, if only to avoid the lambda
This commit is contained in:
jvoisin 2025-07-11 00:05:22 +02:00 committed by Frédéric Guillot
parent 57bd384951
commit 0e9da3a090
2 changed files with 16 additions and 17 deletions

View file

@ -241,13 +241,6 @@ func resizeIcon(icon *model.Icon) *model.Icon {
} }
func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string, error) { func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string, error) {
queries := []string{
"link[rel='icon' i]",
"link[rel='shortcut icon' i]",
"link[rel='icon shortcut' i]",
"link[rel='apple-touch-icon-precomposed.png']",
}
htmlDocumentReader, err := encoding.NewCharsetReader(body, contentType) htmlDocumentReader, err := encoding.NewCharsetReader(body, contentType)
if err != nil { if err != nil {
return nil, fmt.Errorf("icon: unable to create charset reader: %w", err) return nil, fmt.Errorf("icon: unable to create charset reader: %w", err)
@ -258,12 +251,19 @@ func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string,
return nil, fmt.Errorf("icon: unable to read document: %v", err) return nil, fmt.Errorf("icon: unable to read document: %v", err)
} }
queries := []string{
"link[rel='icon' i][href]",
"link[rel='shortcut icon' i][href]",
"link[rel='icon shortcut' i][href]",
"link[rel='apple-touch-icon-precomposed.png'][href]",
}
var iconURLs []string var iconURLs []string
for _, query := range queries { for _, query := range queries {
slog.Debug("Searching icon URL in HTML document", slog.String("query", query)) slog.Debug("Searching icon URL in HTML document", slog.String("query", query))
doc.Find(query).Each(func(i int, s *goquery.Selection) { for _, s := range doc.Find(query).EachIter() {
if href, exists := s.Attr("href"); exists { href, _ := s.Attr("href")
if iconURL := strings.TrimSpace(href); iconURL != "" { if iconURL := strings.TrimSpace(href); iconURL != "" {
iconURLs = append(iconURLs, iconURL) iconURLs = append(iconURLs, iconURL)
slog.Debug("Found icon URL in HTML document", slog.Debug("Found icon URL in HTML document",
@ -271,7 +271,6 @@ func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string,
slog.String("icon_url", iconURL)) slog.String("icon_url", iconURL))
} }
} }
})
} }
return iconURLs, nil return iconURLs, nil

View file

@ -115,7 +115,7 @@ func TestParseInvalidImageDataURLWithWrongPrefix(t *testing.T) {
func TestParseDocumentWithWhitespaceIconURL(t *testing.T) { func TestParseDocumentWithWhitespaceIconURL(t *testing.T) {
html := `<link rel="shortcut icon" href=" html := `<link rel="shortcut icon" href="
/static/img/favicon.ico /static/img/favicon.ico
">` "><link rel='shortcut icon'><link rel='shortcut icon' href=" ">`
iconURLs, err := findIconURLsFromHTMLDocument(strings.NewReader(html), "text/html") iconURLs, err := findIconURLsFromHTMLDocument(strings.NewReader(html), "text/html")
if err != nil { if err != nil {