File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
Sources/App/Views/PackageController
__Snapshots__/PackageReadmeModelTests Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ extension PackageReadme {
56
56
readmeElement. rewriteRelativeImages ( to: repoTriple)
57
57
readmeElement. rewriteRelativeLinks ( to: repoTriple)
58
58
readmeElement. fixInlineAnchors ( )
59
+ readmeElement. fixProtectedCachedImages ( )
59
60
readmeElement. disableTurboOnLinks ( )
60
61
return readmeElement
61
62
}
@@ -124,6 +125,21 @@ extension Element {
124
125
}
125
126
}
126
127
128
+ func fixProtectedCachedImages( ) {
129
+ do {
130
+ let imageElements = try select ( " img[data-canonical-src] " )
131
+ for imageElement in imageElements {
132
+ let originalUrl = try imageElement. attr ( " data-canonical-src " )
133
+ if originalUrl. hasPrefix ( " http " ) {
134
+ try imageElement. attr ( " src " , originalUrl)
135
+ }
136
+ }
137
+ } catch {
138
+ // Errors are being intentionally eaten here. The worst that can happen if the
139
+ // HTML selection/parsing fails is that cached images don't get corrected.
140
+ }
141
+ }
142
+
127
143
func disableTurboOnLinks( ) {
128
144
do {
129
145
let linkElements = try select ( " a " )
Original file line number Diff line number Diff line change @@ -130,6 +130,30 @@ class PackageReadmeModelTests: SnapshotTestCase {
130
130
assertSnapshot ( of: html, as: . lines)
131
131
}
132
132
133
+ func test_Element_fixProtectedCachedImages( ) throws {
134
+ // setup
135
+ let element = Element . extractReadme ( """
136
+ <div id= " readme " >
137
+ <article>
138
+ <p>README content.</p>
139
+ <img src= " https://example.com/standard.png " />
140
+ <img src= " https://camo.githubusercontent.com/0123456789 " data-canonical-src= " https://example.com/cached.png " />
141
+ <img src= " https://camo.githubusercontent.com/0123456789 " data-canonical-src= " " />
142
+ <img src= " https://camo.githubusercontent.com/0123456789 " />
143
+ <img src= " https://example.com/0123456789 " data-canonical-src= " https://example.com/cached.png " />
144
+ <img />
145
+ </article>
146
+ </div>
147
+ """ )
148
+
149
+ // MUT
150
+ element? . fixProtectedCachedImages ( )
151
+
152
+ // validate
153
+ let html = try XCTUnwrap ( try element? . html ( ) )
154
+ assertSnapshot ( of: html, as: . lines)
155
+ }
156
+
133
157
func test_Element_disableTurboOnLinks( ) throws {
134
158
// setup
135
159
let element = Element . extractReadme ( """
Original file line number Diff line number Diff line change
1
+ <p>README content.</p>
2
+ <img src="https://example.com/standard.png" />
3
+ <img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
4
+ <img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="" />
5
+ <img src="https://camo.githubusercontent.com/0123456789" />
6
+ <img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
7
+ <img />
You can’t perform that action at this time.
0 commit comments