@@ -1475,22 +1475,20 @@ func (g *generator) writeScriptContents(indentLevel int, c parser.ScriptContents
1475
1475
return nil
1476
1476
}
1477
1477
// This is a JS expression and can be written directly to the output.
1478
- _ , err := g .w .Write (* c .Value )
1479
- return err
1478
+ return g .writeText (indentLevel , parser.Text {Value : * c .Value })
1480
1479
}
1481
1480
if c .GoCode != nil {
1482
- // This is a Go code block. The code needs to be evaluated, and the result written to the output
1483
- // using the correct JS and HTML escaping based on whether the expression output is within a string
1484
- // or not.
1481
+ // This is a Go code block. The code needs to be evaluated, and the result written to the output.
1482
+ // The variable is JSON encoded to ensure that it is safe to use within a script tag.
1485
1483
var r parser.Range
1486
1484
vn := g .createVariableName ()
1487
- // var vn string
1488
- if _ , err = g .w .WriteIndent (indentLevel , "var " + vn + " string\n " ); err != nil {
1489
- return err
1485
+ // Here, we need to get the result, which might be any type. We can use templ.ScriptContent to get the result.
1486
+ // vn, templ_7745c5c3_Err := templruntime.ScriptContent(
1487
+ fnCall := "templruntime.ScriptContentOutsideStringLiteral"
1488
+ if c .InsideStringLiteral {
1489
+ fnCall = "templruntime.ScriptContentInsideStringLiteral"
1490
1490
}
1491
- // Here, we need to get the result, which might be any type. We can use templ.JoinErrs to get the result.
1492
- // vn, templ_7745c5c3_Err = templruntime.JoinErrs(
1493
- if _ , err = g .w .WriteIndent (indentLevel , vn + ", templ_7745c5c3_Err = templruntime.JoinErrs(" ); err != nil {
1491
+ if _ , err = g .w .WriteIndent (indentLevel , vn + ", templ_7745c5c3_Err := " + fnCall + "(" ); err != nil {
1494
1492
return err
1495
1493
}
1496
1494
// p.Name()
@@ -1509,42 +1507,22 @@ func (g *generator) writeScriptContents(indentLevel int, c parser.ScriptContents
1509
1507
return err
1510
1508
}
1511
1509
1512
- // Then, we need to JSON marshal it, and if the expression is within a string, we need to unquote it.
1513
- jvn := g .createVariableName ()
1514
- // var jvn string
1515
- if _ , err = g .w .WriteIndent (indentLevel , "var " + jvn + " string\n " ); err != nil {
1516
- return err
1517
- }
1518
- // jvn, templ_7745c5c3_Err = json.Marshal(vn)
1519
- if _ , err = g .w .WriteIndent (indentLevel , jvn + ", templ_7745c3_Err = templ.JSONString(" + vn + ")\n " ); err != nil {
1510
+ // _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(jvn)
1511
+ if _ , err = g .w .WriteIndent (indentLevel , "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" + vn + ")\n " ); err != nil {
1520
1512
return err
1521
1513
}
1522
- // Expression error handler.
1523
- err = g .writeExpressionErrorHandler (indentLevel , c .GoCode .Expression )
1524
- if err != nil {
1514
+ if err = g .writeErrorHandler (indentLevel ); err != nil {
1525
1515
return err
1526
1516
}
1527
1517
1528
- // If we're in a string, unquote the result.
1529
- if c .Quoted {
1530
- // jvn, templ_7745c5c3_Err = strconv.Unquote(jvn)
1531
- if _ , err = g .w .WriteIndent (indentLevel , jvn + ", templ_7745c3_Err = strconv.Unquote(" + jvn + ")\n " ); err != nil {
1532
- return err
1533
- }
1534
- // Expression error handler.
1535
- err = g .writeExpressionErrorHandler (indentLevel , c .GoCode .Expression )
1536
- if err != nil {
1518
+ // Write any trailing space.
1519
+ if c .GoCode .TrailingSpace != "" {
1520
+ if err = g .writeText (indentLevel , parser.Text {Value : string (c .GoCode .TrailingSpace )}); err != nil {
1537
1521
return err
1538
1522
}
1539
1523
}
1540
1524
1541
- // _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(jvn)
1542
- if _ , err = g .w .WriteIndent (indentLevel , "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(" + jvn + "))\n " ); err != nil {
1543
- return err
1544
- }
1545
- if err = g .writeErrorHandler (indentLevel ); err != nil {
1546
- return err
1547
- }
1525
+ return nil
1548
1526
}
1549
1527
return errors .New ("unknown script content" )
1550
1528
}
0 commit comments