Skip to content

Commit 3795ba6

Browse files
normalizeInterpolation changes
1 parent 9705a00 commit 3795ba6

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Sources/HTMLKitParse/ExpandHTMLMacro.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ extension HTMLKitUtilities {
183183
let interp = interpolation.removeFirst()
184184
let left = encodedResult[index..<interp.range.lowerBound]
185185
values.append("StaticString(\(left))")
186-
187-
values.append(normalizeInterpolation(encodedResult[interp.range]))
186+
values.append(normalizeInterpolation(encodedResult[interp.range], withQuotationMarks: true))
188187
index = interp.range.upperBound
189188

190189
reserveCapacity += left.count + 32
@@ -196,12 +195,16 @@ extension HTMLKitUtilities {
196195
}
197196
return "HTMLOptimizedLiteral(reserveCapacity: \(reserveCapacity)).render((\n\(values.joined(separator: ",\n"))\n))"
198197
}
199-
static func normalizeInterpolation(_ value: Substring) -> String {
198+
static func normalizeInterpolation(_ value: Substring, withQuotationMarks: Bool) -> String {
200199
var value = value
201200
value.removeFirst(22) // ` + String(describing: `.count
202201
value.removeLast(3) // ` + `.count
203202
value.removeAll(where: { $0.isNewline })
204-
return String("\"\\(" + value + "\"")
203+
if withQuotationMarks {
204+
value.insert("\"", at: value.startIndex)
205+
value.append("\"")
206+
}
207+
return String("\\(" + value)
205208
}
206209
}
207210

@@ -231,16 +234,14 @@ extension HTMLKitUtilities {
231234
var slice = encodedResult[range]
232235
var interpolation:String?
233236
if let interp = interpolationMatches.first, range.contains(interp.range.lowerBound) { // chunk contains interpolation
234-
var normalized = normalizeInterpolation(encodedResult[interp.range])
235-
normalized.removeFirst()
237+
let normalized = normalizeInterpolation(encodedResult[interp.range], withQuotationMarks: false)
236238
interpolation = normalized
237239
if !range.contains(interp.range.upperBound) {
238240
endIndex = encodedResult.index(before: interp.range.lowerBound)
239241
slice = slice[slice.startIndex..<endIndex]
240242
i += encodedResult.distance(from: range.upperBound, to: interp.range.upperBound)
241243
} else {
242244
interpolation = nil
243-
normalized.removeLast()
244245
slice.remove(at: interp.range.upperBound) // "
245246
slice.replaceSubrange(interp.range, with: normalized)
246247
slice.remove(at: slice.index(before: interp.range.lowerBound)) // "
@@ -262,9 +263,9 @@ extension HTMLKitUtilities {
262263
string += interpolation
263264
} else {
264265
string += slice
265-
if let l = slice.last, let d = delimiter(l) {
266-
string += d
267-
}
266+
}
267+
if let l = slice.last, let d = delimiter(l) {
268+
string += d
268269
}
269270
chunks.append(string)
270271
}

Tests/HTMLKitTests/StreamTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct StreamTests {
8787
extension StreamTests {
8888
@Test
8989
func streamInterpolation() async {
90-
let rawHTMLInterpolationTest = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567689"
90+
let rawHTMLInterpolationTest = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
9191
let expected:String = #html(
9292
html {
9393
body {

0 commit comments

Comments
 (0)