Skip to content

Commit a4d9ea5

Browse files
fixed logic issue when flattening interpolation which resulted in missing content
1 parent 72118d1 commit a4d9ea5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private extension HTMLElement {
296296
let interpolation:[ExpressionSegmentSyntax] = expression.stringLiteral?.segments.compactMap({ $0.as(ExpressionSegmentSyntax.self) }) ?? []
297297
var remaining_interpolation:Int = interpolation.count
298298
for expr in interpolation {
299-
string = flatten_interpolation(context: context, remaining_interpolation: &remaining_interpolation, expr: expr)
299+
string.replace("\(expr)", with: flatten_interpolation(context: context, remaining_interpolation: &remaining_interpolation, expr: expr))
300300
}
301301
if returnType == .interpolation || remaining_interpolation > 0 {
302302
if !string.contains("\\(") {

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,28 @@ extension HTMLKitTests {
299299
let test:[Int] = [1]
300300
bro = #area(coords: test)
301301
}*/
302+
303+
304+
@Test func interpolation_flattening() {
305+
let title:String = "flattening"
306+
var string:String = #meta(content: "\("interpolation \(title)")", name: "description")
307+
#expect(string == "<meta content=\"interpolation \(title)\" name=\"description\">")
308+
309+
string = #meta(content: "interpolation \(title)", name: "description")
310+
#expect(string == "<meta content=\"interpolation \(title)\" name=\"description\">")
311+
312+
string = #meta(content: "interpolation\(title)", name: "description")
313+
#expect(string == "<meta content=\"interpolation\(title)\" name=\"description\">")
314+
315+
string = #meta(content: "\(title) interpolation", name: "description")
316+
#expect(string == "<meta content=\"flattening interpolation\" name=\"description\">")
317+
318+
string = #meta(content: "\(title)interpolation", name: "description")
319+
#expect(string == "<meta content=\"flatteninginterpolation\" name=\"description\">")
320+
321+
string = #meta(content: "\(title)\("interpolation")", name: "description")
322+
#expect(string == "<meta content=\"flatteninginterpolation\" name=\"description\">")
323+
}
302324
}
303325

304326
// MARK: Attribute tests

0 commit comments

Comments
 (0)