Skip to content

Commit

Permalink
fix: scripts not rendered when script expression is in a conditional …
Browse files Browse the repository at this point in the history
…attribute, fixes #260
  • Loading branch information
a-h committed Oct 30, 2023
1 parent 8d6bbc2 commit 63bc705
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
27 changes: 20 additions & 7 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,8 @@ func isScriptAttribute(name string) bool {

func (g *generator) writeElementScript(indentLevel int, n parser.Element) (err error) {
var scriptExpressions []string
for i := 0; i < len(n.Attributes); i++ {
if attr, ok := n.Attributes[i].(parser.ExpressionAttribute); ok {
name := html.EscapeString(attr.Name)
if isScriptAttribute(name) {
scriptExpressions = append(scriptExpressions, attr.Expression.Value)
}
}
for _, attr := range n.Attributes {
scriptExpressions = append(scriptExpressions, getAttributeScripts(attr)...)
}
if len(scriptExpressions) == 0 {
return
Expand All @@ -984,6 +979,24 @@ func (g *generator) writeElementScript(indentLevel int, n parser.Element) (err e
return err
}

func getAttributeScripts(attr parser.Attribute) (scripts []string) {
if attr, ok := attr.(parser.ConditionalAttribute); ok {
for _, attr := range attr.Then {
scripts = append(scripts, getAttributeScripts(attr)...)
}
for _, attr := range attr.Else {
scripts = append(scripts, getAttributeScripts(attr)...)
}
}
if attr, ok := attr.(parser.ExpressionAttribute); ok {
name := html.EscapeString(attr.Name)
if isScriptAttribute(name) {
scripts = append(scripts, attr.Expression.Value)
}
}
return scripts
}

func (g *generator) writeBoolConstantAttribute(indentLevel int, attr parser.BoolConstantAttribute) (err error) {
name := html.EscapeString(attr.Name)
if _, err = g.w.WriteStringLiteral(indentLevel, fmt.Sprintf(` %s`, name)); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion generator/test-script-usage/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
function __templ_onClick_657d(){alert("clicked");}
</script>
<button hx-on::click="__templ_onClick_657d()" type="button">Button E</button>

<script type="text/javascript">
function __templ_conditionalScript_de41(){alert("conditional");}
</script>
<input type="button" value="Click me" onclick="__templ_conditionalScript_de41()" />
15 changes: 15 additions & 0 deletions generator/test-script-usage/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ templ ThreeButtons() {
<button onMouseover="console.log(&#39;mouseover&#39;)" type="button">Button C</button>
<button hx-on::click="alert(&#39;clicked inline&#39;)" type="button">Button D</button>
<button hx-on::click={ onClick() } type="button">Button E</button>
@Conditional(true)
}

script conditionalScript() {
alert("conditional");
}

templ Conditional(show bool) {
<input
type="button"
value="Click me"
if show {
onclick={ conditionalScript() }
}
/>
}
59 changes: 59 additions & 0 deletions generator/test-script-usage/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (ca ConstantAttribute) Write(w io.Writer, indent int) error {
return writeIndent(w, indent, ca.String())
}

// href={ templ.Bool(...) }
// noshade={ templ.Bool(...) }
type BoolExpressionAttribute struct {
Name string
Expression Expression
Expand Down

0 comments on commit 63bc705

Please sign in to comment.