Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions Sources/HTMLKit/Framework/Rendering/Encoding/HtmlString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// A type that remains unescaped during rendering.
///
/// > Warning: Use with caution, as this may lead to security vulnerabilities
/// > if the argument is not properly validated or not trusted.
@_documentation(visibility: internal)
public struct HtmlString: Content {

/// The wrapped string
internal let value: String

/// Initializes an html string
public init(_ value: String) {
self.value = value
}
}
6 changes: 6 additions & 0 deletions Sources/HTMLKit/Framework/Rendering/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public struct Renderer {
case let string as EnvironmentString:
try render(envstring: string, on: &result)

case let string as HtmlString:
result += string.value

case let doubleValue as Double:
result += String(doubleValue)

Expand Down Expand Up @@ -501,6 +504,9 @@ public struct Renderer {
case let string as EnvironmentString:
try render(envstring: string, on: &result)

case let string as HtmlString:
result += string.value

case let string as String:
result += escape(content: string)

Expand Down
20 changes: 20 additions & 0 deletions Tests/HTMLKitTests/SecurityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,25 @@ final class SecurityTests: XCTestCase {
"""
)
}

/// Tests the renderers behaviour when handling a desired unescaped string.
///
/// The renderer is expected to emit the string as-is.
func testIgnoringHtmlString() throws {

let html = "<script></script>"

let view = TestView {
Paragraph {
HtmlString(html)
}
}

XCTAssertEqual(try renderer.render(view: view),
"""
<p><script></script></p>
"""
)
}
}