Skip to content

Commit d69dc44

Browse files
committed
add link integrity
1 parent 9929537 commit d69dc44

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Sources/SwiftHtml/Tags/Link.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ public extension Link {
112112
attribute("referrerpolicy", value.rawValue)
113113
}
114114

115-
/// Specifies the relationship between the current document and the linked document
116-
// func rel(_ value: String) -> Self {
117-
// attribute("rel", value)
118-
// }
119-
120-
/// Specifies the relationship between the current document and the linked document
121-
// func rel(_ value: Rel) -> Self {
122-
// rel(value.rawValue)
123-
// }
115+
/// Allows a browser to check the fetched link to ensure that the code is never loaded if the source has been manipulated
116+
func integrity(_ value: String) -> Self {
117+
attribute("integrity", value)
118+
}
124119

125120
/// Specifies the size of the linked resource. Only for `rel="icon"`
126121
func sizes(_ value: String) -> Self {

Tests/SwiftHtmlTests/Tags/LinkTagTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,17 @@ final class LinkTagTests: XCTestCase {
2626
XCTAssertEqual(#"<link rel="apple-touch-startup-image" media="screen and (device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait)" href="/img/apple/splash/1136x640.png">"#, html)
2727
}
2828

29+
func testIntegrity() {
30+
let doc = Document {
31+
Link(rel: .stylesheet)
32+
.href("https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css")
33+
.integrity("sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm")
34+
.crossorigin(.anonymous)
35+
36+
}
37+
let html = DocumentRenderer(minify: true).render(doc)
38+
XCTAssertEqual(#"<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">"#, html)
39+
}
40+
2941

3042
}

Tests/SwiftHtmlTests/Tags/ScriptTagTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ final class ScriptTagTests: XCTestCase {
2323
"""#)
2424
}
2525

26+
func testIntegrity() {
27+
let doc = Document {
28+
Script()
29+
.src("https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js")
30+
.integrity("sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl")
31+
.crossorigin(.anonymous)
32+
33+
}
34+
let html = DocumentRenderer(minify: true).render(doc)
35+
XCTAssertEqual(#"<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>"#, html)
36+
}
37+
2638
}

0 commit comments

Comments
 (0)