Skip to content

Commit

Permalink
fix: allow single and double quotes in comments within script templat…
Browse files Browse the repository at this point in the history
…es (#384)
  • Loading branch information
a-h authored Jan 5, 2024
1 parent a7090d1 commit 668a888
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 23 deletions.
6 changes: 4 additions & 2 deletions cmd/templ/visualize/sourcemapvisualisation_templ.go

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

3 changes: 2 additions & 1 deletion examples/external-libraries/components_templ.go

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

4 changes: 3 additions & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

_ "embed"

Expand Down Expand Up @@ -1420,8 +1421,9 @@ func (g *generator) writeScript(t parser.ScriptTemplate) error {
}
// Function: `function scriptName(a, b, c){` + `constantScriptValue` + `}`,
prefix := "function " + fn + "(" + stripTypes(t.Parameters.Value) + "){"
body := strings.TrimLeftFunc(t.Value, unicode.IsSpace)
suffix := "}"
if _, err = g.w.WriteIndent(indentLevel, "Function: "+createGoString(prefix+strings.TrimSpace(t.Value)+suffix)+",\n"); err != nil {
if _, err = g.w.WriteIndent(indentLevel, "Function: "+createGoString(prefix+body+suffix)+",\n"); err != nil {
return err
}
// Call: templ.SafeScript(scriptName, a, b, c)
Expand Down
8 changes: 5 additions & 3 deletions generator/test-script-inline/expected.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script type="text/javascript">
function __templ_withoutParameters_6bbf(){alert("hello");}
function __templ_withoutParameters_6bbf(){alert("hello");
}
</script>
<script type="text/javascript">
__templ_withoutParameters_6bbf()
</script>
<script type="text/javascript">
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);}
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}
</script>
<script type="text/javascript">
__templ_withParameters_1056("injected","test",123)
Expand All @@ -15,4 +17,4 @@
</script>
<script type="text/javascript">
__templ_withParameters_1056("injected","test",123)
</script>
</script>
10 changes: 6 additions & 4 deletions generator/test-script-inline/template_templ.go

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

10 changes: 7 additions & 3 deletions generator/test-script-usage/expected.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<script type="text/javascript">
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);}function __templ_withoutParameters_6bbf(){alert("hello");}
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
}function __templ_withoutParameters_6bbf(){alert("hello");
}
</script>
<button onClick="__templ_withParameters_1056(&#34;test&#34;,&#34;A&#34;,123)" onMouseover="__templ_withoutParameters_6bbf()" type="button">A</button>
<button onClick="__templ_withParameters_1056(&#34;test&#34;,&#34;B&#34;,123)" onMouseover="__templ_withoutParameters_6bbf()" type="button">B</button>
<button onMouseover="console.log(&#39;mouseover&#39;)" type="button">Button C</button>
<button hx-on::click="alert('clicked inline')" type="button">Button D</button>
<script type="text/javascript">
function __templ_onClick_657d(){alert("clicked");}
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");}
function __templ_conditionalScript_de41(){alert("conditional");
}
</script>
<input type="button" value="Click me" onclick="__templ_conditionalScript_de41()" />
4 changes: 4 additions & 0 deletions generator/test-script-usage/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ templ Button(text string) {
<button onClick={ withParameters("test", text, 123) } onMouseover={ withoutParameters() } type="button">{ text }</button>
}

script withComment() {
//'
}

templ ThreeButtons() {
@Button("A")
@Button("B")
Expand Down
30 changes: 22 additions & 8 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.

23 changes: 22 additions & 1 deletion parser/v2/expressionparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ var stringUntilNewLine = parse.StringUntil[string](parse.NewLine)
var newLineOrEOF = parse.Or(parse.NewLine, parse.EOF[string]())
var stringUntilNewLineOrEOF = parse.StringUntil(newLineOrEOF)

var jsOrGoSingleLineComment = parse.StringFrom(parse.String("//"), parse.StringUntil(parse.Any(parse.NewLine, parse.EOF[string]())))
var jsOrGoMultiLineComment = parse.StringFrom(parse.String("/*"), parse.StringUntil(parse.String("*/")))

var exp = expressionParser{
startBraceCount: 1,
}
Expand All @@ -66,7 +69,25 @@ loop:
for {
var result string

// Try to read a string literal first.
// Try to parse a single line comment.
if result, ok, err = jsOrGoSingleLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}

// Try to parse a multi-line comment.
if result, ok, err = jsOrGoMultiLineComment.Parse(pi); err != nil {
return
}
if ok {
sb.WriteString(result)
continue
}

// Try to read a string literal.
if result, ok, err = string_lit.Parse(pi); err != nil {
return
}
Expand Down
39 changes: 39 additions & 0 deletions parser/v2/scripttemplateparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,45 @@ console.log(value);
Value: `console.log(value);` + "\n",
},
},
{
name: "script: comment with single quote",
input: `script Name() {
//'
} Trailing '`, // Without a single quote later, issue #360 isn't triggered.
expected: ScriptTemplate{
Name: Expression{
Value: "Name",
Range: Range{
From: Position{
Index: 7,
Line: 0,
Col: 7,
},
To: Position{
Index: 11,
Line: 0,
Col: 11,
},
},
},
Parameters: Expression{
Value: "",
Range: Range{
From: Position{
Index: 12,
Line: 0,
Col: 12,
},
To: Position{
Index: 12,
Line: 0,
Col: 12,
},
},
},
Value: ` //'` + "\n",
},
},
}
for _, tt := range tests {
tt := tt
Expand Down

0 comments on commit 668a888

Please sign in to comment.