Skip to content

Commit

Permalink
fixed logic issue when flattening interpolation which resulted in mis…
Browse files Browse the repository at this point in the history
…sing content
  • Loading branch information
RandomHashTags committed Oct 23, 2024
1 parent 72118d1 commit a4d9ea5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/HTMLKitMacros/HTMLElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private extension HTMLElement {
let interpolation:[ExpressionSegmentSyntax] = expression.stringLiteral?.segments.compactMap({ $0.as(ExpressionSegmentSyntax.self) }) ?? []
var remaining_interpolation:Int = interpolation.count
for expr in interpolation {
string = flatten_interpolation(context: context, remaining_interpolation: &remaining_interpolation, expr: expr)
string.replace("\(expr)", with: flatten_interpolation(context: context, remaining_interpolation: &remaining_interpolation, expr: expr))
}
if returnType == .interpolation || remaining_interpolation > 0 {
if !string.contains("\\(") {
Expand Down
22 changes: 22 additions & 0 deletions Tests/HTMLKitTests/HTMLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,28 @@ extension HTMLKitTests {
let test:[Int] = [1]
bro = #area(coords: test)
}*/


@Test func interpolation_flattening() {
let title:String = "flattening"
var string:String = #meta(content: "\("interpolation \(title)")", name: "description")
#expect(string == "<meta content=\"interpolation \(title)\" name=\"description\">")

string = #meta(content: "interpolation \(title)", name: "description")
#expect(string == "<meta content=\"interpolation \(title)\" name=\"description\">")

string = #meta(content: "interpolation\(title)", name: "description")
#expect(string == "<meta content=\"interpolation\(title)\" name=\"description\">")

string = #meta(content: "\(title) interpolation", name: "description")
#expect(string == "<meta content=\"flattening interpolation\" name=\"description\">")

string = #meta(content: "\(title)interpolation", name: "description")
#expect(string == "<meta content=\"flatteninginterpolation\" name=\"description\">")

string = #meta(content: "\(title)\("interpolation")", name: "description")
#expect(string == "<meta content=\"flatteninginterpolation\" name=\"description\">")
}
}

// MARK: Attribute tests
Expand Down

0 comments on commit a4d9ea5

Please sign in to comment.