Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/App/Views/PackageController/PackageReadme+Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
readmeElement.rewriteRelativeImages(to: repoTriple)
readmeElement.rewriteRelativeLinks(to: repoTriple)
readmeElement.fixInlineAnchors()
readmeElement.fixProtectedCachedImages()
readmeElement.disableTurboOnLinks()
return readmeElement
}
Expand Down Expand Up @@ -124,6 +125,22 @@
}
}

func fixProtectedCachedImages() {
do {
let imageElements = try select("img[data-canonical-src]")
for imageElement in imageElements {
let cachedUrl = try imageElement.attr("src")

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Query Performance Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Query Performance Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Query Performance Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 132 in Sources/App/Views/PackageController/PackageReadme+Model.swift

View workflow job for this annotation

GitHub Actions / Test

initialization of immutable value 'cachedUrl' was never used; consider replacing with assignment to '_' or removing it
let originalUrl = try imageElement.attr("data-canonical-src")
if originalUrl.hasPrefix("http") {
try imageElement.attr("src", originalUrl)
}
}
} catch {
// Errors are being intentionally eaten here. The worst that can happen if the
// HTML selection/parsing fails is that cached images don't get corrected.
}
}

func disableTurboOnLinks() {
do {
let linkElements = try select("a")
Expand Down
24 changes: 24 additions & 0 deletions Tests/AppTests/PackageReadmeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ class PackageReadmeModelTests: SnapshotTestCase {
assertSnapshot(of: html, as: .lines)
}

func test_Element_fixProtectedCachedImages() throws {
// setup
let element = Element.extractReadme("""
<div id="readme">
<article>
<p>README content.</p>
<img src="https://example.com/standard.png" />
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="https://example.com/cached.png" />
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="" />
<img src="https://camo.githubusercontent.com/0123456789" />
<img src="https://example.com/0123456789" data-canonical-src="https://example.com/cached.png" />
<img />
</article>
</div>
""")

// MUT
element?.fixProtectedCachedImages()

// validate
let html = try XCTUnwrap(try element?.html())
assertSnapshot(of: html, as: .lines)
}

func test_Element_disableTurboOnLinks() throws {
// setup
let element = Element.extractReadme("""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>README content.</p>
<img src="https://example.com/standard.png" />
<img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="" />
<img src="https://camo.githubusercontent.com/0123456789" />
<img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
<img />
Loading