Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from bfolkens/fix_binary_data_uri
Browse files Browse the repository at this point in the history
Fix data corruption with binary data uri
  • Loading branch information
siemensikkema authored Apr 13, 2021
2 parents 8c82fea + aaf88b1 commit 6a9f50b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Sources/DataURI/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ extension DataURIParser {
}

if typeMetadata == "base64".bytes,
let decodedData = Data(base64Encoded: data.convertToData()),
let dataString = String(data: decodedData, encoding: .utf8)
let decodedData = Data(base64Encoded: data.convertToData())
{
data = dataString.bytes
data = Array(decodedData)
}

return (data, type, typeMetadata)
Expand Down
10 changes: 10 additions & 0 deletions Tests/DataURITests/DataURITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class DataURITests: XCTestCase {
XCTAssertEqual(meta?.makeString(), "base64")
}

func testBase64Binary() {
let (data, type, meta) = try! DataURIParser.parse(
uri: "data:text/plain;base64,AAECA3Rlc3QK"
)

XCTAssertEqual(data, [0, 1, 2, 3, 116, 101, 115, 116, 10])
XCTAssertEqual(type.makeString(), "text/plain")
XCTAssertEqual(meta?.makeString(), "base64")
}

func testHTMLText() {
let (data, type, meta) = try! DataURIParser.parse(
uri: "data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E"
Expand Down

0 comments on commit 6a9f50b

Please sign in to comment.