Skip to content

Commit 6044b84

Browse files
authored
Merge commit from fork
1 parent 8ff0683 commit 6044b84

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

Sources/LeafKit/LeafData/LeafData.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public struct LeafData:
320320

321321
/// Return a HTML-escaped version of this data if it can be converted to a string.
322322
func htmlEscaped() -> LeafData {
323-
guard let string = self.string else {
323+
guard let string = self.convert(to: .string, .ambiguous).string else {
324324
return self
325325
}
326326

@@ -334,7 +334,7 @@ public struct LeafData:
334334
enum DataConvertible: Int, Equatable, Comparable {
335335
/// Not implicitly convertible automatically
336336
case ambiguous = 0
337-
/// A coercioni with a clear meaning in one direction
337+
/// A coercion with a clear meaning in one direction
338338
case coercible = 1
339339
/// A conversion with a well-defined bi-directional casting possibility
340340
case castable = 2

Sources/LeafKit/LeafData/LeafDataStorage.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ indirect enum LeafDataStorage: Equatable, CustomStringConvertible, Sendable {
6464

6565
/// Final serialization to a shared buffer
6666
func serialize(buffer: inout ByteBuffer) throws {
67-
switch self {
68-
case .bool, .int, .double, .string, .optional, .array, .dictionary:
69-
try buffer.writeString(self.serialize(), encoding: LeafConfiguration.encoding)
70-
case .data(let d):
71-
buffer.writeData(d)
72-
}
67+
try buffer.writeString(self.serialize(), encoding: LeafConfiguration.encoding)
7368
}
7469

7570
// MARK: - Equatable Conformance

Tests/LeafKitTests/TagTests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ final class TagTests: XCTestCase {
1212
try XCTAssertEqual(render(template, ["name": "<h1>Alex</h1>\"\'"]), expected)
1313
}
1414

15+
func testOtherThingsWithHTMLEntities() throws {
16+
try XCTAssertEqual(render("#(foo)", ["foo": .data(Data([0x3c, 0x3e, 0xc3, 0xff]))]), "")
17+
try XCTAssertEqual(render("#(foo)", ["foo": .data(Data([0x3c, 0x3e, 0xc3, 0xb7]))]), "&lt;&gt;÷")
18+
try XCTAssertEqual(render("#(foo)", ["foo": ["<img src=x onerror=alert(1337)>"]]), "[&quot;&lt;img src=x onerror=alert(1337)&gt;&quot;]")
19+
try XCTAssertEqual(render("#(foo)", ["foo": ["<img src=x onerror=alert(1337)>": "<img src=x onerror=alert(1337)>"]]), "[&lt;img src=x onerror=alert(1337)&gt;: &quot;&lt;img src=x onerror=alert(1337)&gt;&quot;]")
20+
21+
try XCTAssertThrowsError(render("#unsafeHTML(foo)", ["foo": .data(Data([0x3c, 0x3e, 0xc3, 0xff]))]))
22+
try XCTAssertEqual(render("#unsafeHTML(foo)", ["foo": .data(Data([0x3c, 0x3e, 0xc3, 0xb7]))]), "<>÷")
23+
try XCTAssertThrowsError(render("#unsafeHTML(foo)", ["foo": ["<img src=x onerror=alert(1337)>"]]))
24+
try XCTAssertThrowsError(render("#unsafeHTML(foo)", ["foo": ["<img src=x onerror=alert(1337)>": "<img src=x onerror=alert(1337)>"]]))
25+
}
26+
1527
func testUnsafeTag() throws {
1628
let template = """
1729
#unsafeHTML(name)
@@ -255,7 +267,7 @@ final class TagTests: XCTestCase {
255267
"""
256268

257269
let expected = """
258-
dumpContext should output debug description [value: "12345"]
270+
dumpContext should output debug description [value: &quot;12345&quot;]
259271
"""
260272

261273
try XCTAssertEqual(render(template, data), expected)

0 commit comments

Comments
 (0)