-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimages.go
More file actions
135 lines (113 loc) · 3.58 KB
/
Copy pathimages.go
File metadata and controls
135 lines (113 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package defuddle
import (
"log/slog"
"strconv"
"github.com/PuerkitoBio/goquery"
)
// findSmallImages finds small images that should be removed
// JavaScript original code:
//
// private findSmallImages(doc: Document): Set<string> {
// // ... (processes img and svg elements, checks dimensions)
// }
func (d *Defuddle) findSmallImages(doc *goquery.Document) map[string]bool {
const minDimension = 33
smallImages := make(map[string]bool)
processedCount := 0
// Process img and svg elements
doc.Find("img, svg").Each(func(_ int, element *goquery.Selection) {
// Get dimensions from attributes
width := parseDimensionAttr(element, "width")
height := parseDimensionAttr(element, "height")
// Check if dimensions are small
if (width > 0 && width < minDimension) || (height > 0 && height < minDimension) {
identifier := d.getElementIdentifier(element)
if identifier != "" {
smallImages[identifier] = true
processedCount++
}
}
})
if d.debug {
slog.Debug("Found small images", "count", processedCount)
}
return smallImages
}
// parseDimensionAttr returns the integer value of element's width/height attr,
// or 0 when the attribute is absent or non-numeric.
func parseDimensionAttr(element *goquery.Selection, attr string) int {
value, _ := element.Attr(attr)
if value != "" {
if n, err := strconv.Atoi(value); err == nil {
return n
}
}
return 0
}
// removeSmallImages removes small images from the document
// JavaScript original code:
//
// private removeSmallImages(doc: Document, smallImages: Set<string>) {
// // ... (removes elements matching smallImages set)
// }
func (d *Defuddle) removeSmallImages(doc *goquery.Document, smallImages map[string]bool) {
removedCount := 0
doc.Find("img, svg").Each(func(_ int, element *goquery.Selection) {
identifier := d.getElementIdentifier(element)
if identifier != "" && smallImages[identifier] {
element.Remove()
removedCount++
}
})
if d.debug {
slog.Debug("Removed small images", "count", removedCount)
}
}
// removeAllImages removes all images from the document
// Implements the removeImages option from TypeScript version
func (d *Defuddle) removeAllImages(doc *goquery.Document) {
removedCount := 0
doc.Find("img, svg, picture, video, canvas").Each(func(_ int, element *goquery.Selection) {
element.Remove()
removedCount++
})
if d.debug {
slog.Debug("Removed all images", "count", removedCount)
}
}
// getElementIdentifier creates a unique identifier for an element
// JavaScript original code:
//
// private getElementIdentifier(element: Element): string | null {
// // ... (creates identifier from src, id, viewBox, or class)
// }
func (d *Defuddle) getElementIdentifier(element *goquery.Selection) string {
tagName := goquery.NodeName(element)
if tagName == "img" {
// For lazy-loaded images, use data-src as identifier if available
if dataSrc, exists := element.Attr("data-src"); exists && dataSrc != "" {
return "src:" + dataSrc
}
if src, exists := element.Attr("src"); exists && src != "" {
return "src:" + src
}
if srcset, exists := element.Attr("srcset"); exists && srcset != "" {
return "srcset:" + srcset
}
if dataSrcset, exists := element.Attr("data-srcset"); exists && dataSrcset != "" {
return "srcset:" + dataSrcset
}
}
if id, exists := element.Attr("id"); exists && id != "" {
return "id:" + id
}
if tagName == "svg" {
if viewBox, exists := element.Attr("viewBox"); exists && viewBox != "" {
return "viewBox:" + viewBox
}
}
if className, exists := element.Attr("class"); exists && className != "" {
return "class:" + className
}
return ""
}