From 9e11024496759775b7443186875089831a13d216 Mon Sep 17 00:00:00 2001 From: Jung gyun Ahn Date: Wed, 8 Nov 2023 10:38:37 +0900 Subject: [PATCH] Prevent escaping slash of URL (#127) --- Sources/Document/CRDT/CRDTText.swift | 2 +- Sources/Util/Dictionary+Extension.swift | 4 ++-- Tests/Integration/TextIntegrationTests.swift | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/Document/CRDT/CRDTText.swift b/Sources/Document/CRDT/CRDTText.swift index b07ea561..97c3c65d 100644 --- a/Sources/Document/CRDT/CRDTText.swift +++ b/Sources/Document/CRDT/CRDTText.swift @@ -30,7 +30,7 @@ func stringifyAttributes(_ attributes: TextAttributes) -> [String: String] { } return jsonObject.mapValues { - if let result = try? JSONSerialization.data(withJSONObject: $0, options: .fragmentsAllowed) { + if let result = try? JSONSerialization.data(withJSONObject: $0, options: [.fragmentsAllowed, .withoutEscapingSlashes]) { return String(data: result, encoding: .utf8) ?? "" } else { return "" diff --git a/Sources/Util/Dictionary+Extension.swift b/Sources/Util/Dictionary+Extension.swift index 102198f8..5a0ee25e 100644 --- a/Sources/Util/Dictionary+Extension.swift +++ b/Sources/Util/Dictionary+Extension.swift @@ -33,12 +33,12 @@ extension AnyValueTypeDictionary { { convertedDictionary[key] = stringValue } else if let value = value as? [String: Any], - let jsonData = try? JSONSerialization.data(withJSONObject: value), + let jsonData = try? JSONSerialization.data(withJSONObject: value, options: [.fragmentsAllowed, .withoutEscapingSlashes]), let stringValue = String(data: jsonData, encoding: .utf8) { convertedDictionary[key] = stringValue } else if let value = value as? [[String: Any]], - let jsonData = try? JSONSerialization.data(withJSONObject: value), + let jsonData = try? JSONSerialization.data(withJSONObject: value, options: [.fragmentsAllowed, .withoutEscapingSlashes]), let stringValue = String(data: jsonData, encoding: .utf8) { convertedDictionary[key] = stringValue diff --git a/Tests/Integration/TextIntegrationTests.swift b/Tests/Integration/TextIntegrationTests.swift index fbb66287..bc5ad756 100644 --- a/Tests/Integration/TextIntegrationTests.swift +++ b/Tests/Integration/TextIntegrationTests.swift @@ -653,7 +653,7 @@ final class TextIntegrationConcurrentTests: XCTestCase { var d1JSON = await d1.toSortedJSON() var d2JSON = await d2.toSortedJSON() - XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"attrs\":{\"link\":\"https:\\/\\/www.google.com\\/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\".\"}]}") + XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"attrs\":{\"link\":\"https://www.google.com/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\".\"}]}") XCTAssertEqual(d1JSON, d2JSON) try await d1.update { root, _ in @@ -661,14 +661,14 @@ final class TextIntegrationConcurrentTests: XCTestCase { } d1JSON = await d1.toSortedJSON() - XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"val\":\"quick \"},{\"attrs\":{\"link\":\"https:\\/\\/www.google.com\\/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\".\"}]}") + XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"val\":\"quick \"},{\"attrs\":{\"link\":\"https://www.google.com/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\".\"}]}") try await d2.update { root, _ in (root.k1 as? JSONText)?.edit(14, 14, " over the dog") } d2JSON = await d2.toSortedJSON() - XCTAssertEqual(d2JSON, "{\"k1\":[{\"val\":\"The \"},{\"attrs\":{\"link\":\"https:\\/\\/www.google.com\\/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\" over the dog\"},{\"val\":\".\"}]}") + XCTAssertEqual(d2JSON, "{\"k1\":[{\"val\":\"The \"},{\"attrs\":{\"link\":\"https://www.google.com/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\" over the dog\"},{\"val\":\".\"}]}") try await c1.sync() try await c2.sync() @@ -676,7 +676,7 @@ final class TextIntegrationConcurrentTests: XCTestCase { d1JSON = await d1.toSortedJSON() d2JSON = await d2.toSortedJSON() - XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"val\":\"quick \"},{\"attrs\":{\"link\":\"https:\\/\\/www.google.com\\/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\" over the dog\"},{\"val\":\".\"}]}") + XCTAssertEqual(d1JSON, "{\"k1\":[{\"val\":\"The \"},{\"val\":\"quick \"},{\"attrs\":{\"link\":\"https://www.google.com/search?q=jumping+fox\"},\"val\":\"fox jumped\"},{\"val\":\" over the dog\"},{\"val\":\".\"}]}") XCTAssertEqual(d1JSON, d2JSON) } }