mirror of
https://github.com/a-h/templ.git
synced 2025-02-06 10:03:16 +00:00
refactor: don't generate a type
attribute for <script>
elements, update associated docs (#1053)
This commit is contained in:
parent
6ea47fff11
commit
2514d89075
@ -225,7 +225,7 @@ templ Button(text string) {
|
||||
```
|
||||
|
||||
```html title="Output"
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);}function __templ_withoutParameters_6bbf(){alert("hello");}
|
||||
</script>
|
||||
<button onclick="__templ_withParameters_1056("test","Say hello",123)" onmouseover="__templ_withoutParameters_6bbf()" type="button">
|
||||
|
@ -6,7 +6,7 @@ Use standard `<script>` tags, and standard HTML attributes to run JavaScript on
|
||||
|
||||
```templ
|
||||
templ body() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function handleClick(event) {
|
||||
alert(event + ' clicked');
|
||||
}
|
||||
@ -49,7 +49,7 @@ HTML element `on*` attributes pass an event object to the function. To pass the
|
||||
:::
|
||||
|
||||
```templ title="input.templ"
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function clickHandler(event, message) {
|
||||
alert(message);
|
||||
event.preventDefault();
|
||||
@ -61,7 +61,7 @@ HTML element `on*` attributes pass an event object to the function. To pass the
|
||||
The output would be:
|
||||
|
||||
```html title="output.html"
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function clickHandler(event, message) {
|
||||
alert(message);
|
||||
event.preventDefault();
|
||||
@ -87,7 +87,7 @@ templ InitializeClientSideScripts(data CustomType) {
|
||||
This will output a `<script>` tag that calls the `functionToCall` function with the `Name` and `Age` properties of the `data` object.
|
||||
|
||||
```html title="output.html"
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
functionToCall("John", 42);
|
||||
</script>
|
||||
```
|
||||
@ -169,7 +169,7 @@ var helloHandle = templ.NewOnceHandle()
|
||||
templ hello(label, name string) {
|
||||
// This script is only rendered once per HTTP request.
|
||||
@helloHandle.Once() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
@ -177,7 +177,7 @@ templ hello(label, name string) {
|
||||
}
|
||||
<div>
|
||||
<input type="button" value={ label } data-name={ name }/>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// To prevent the variables from leaking into the global scope,
|
||||
// this script is wrapped in an IIFE (Immediately Invoked Function Expression).
|
||||
(() => {
|
||||
@ -213,7 +213,7 @@ var surrealHandle = templ.NewOnceHandle()
|
||||
|
||||
templ hello(label, name string) {
|
||||
@helloHandle.Once() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
@ -224,7 +224,7 @@ templ hello(label, name string) {
|
||||
}
|
||||
<div>
|
||||
<input type="button" value={ label } data-name={ name }/>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// me("-") returns the previous sibling element.
|
||||
me("-").addEventListener('click', function() {
|
||||
let name = this.getAttribute('data-name');
|
||||
@ -492,9 +492,9 @@ After building and running the executable, running `curl http://localhost:8080/`
|
||||
```html title="Output"
|
||||
<html>
|
||||
<body>
|
||||
<script type="text/javascript">function __templ_printToConsole_5a85(content){console.log(content)}</script>
|
||||
<script type="text/javascript">__templ_printToConsole_5a85("2023-11-11 01:01:40.983381358 +0000 UTC")</script>
|
||||
<script type="text/javascript">__templ_printToConsole_5a85("Again: 2023-11-11 01:01:40.983381358 +0000 UTC")</script>
|
||||
<script>function __templ_printToConsole_5a85(content){console.log(content)}</script>
|
||||
<script>__templ_printToConsole_5a85("2023-11-11 01:01:40.983381358 +0000 UTC")</script>
|
||||
<script>__templ_printToConsole_5a85("Again: 2023-11-11 01:01:40.983381358 +0000 UTC")</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
@ -188,7 +188,7 @@ import "fmt"
|
||||
|
||||
templ Hello(name string) {
|
||||
<div data-name={ name }>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
bundle.renderHello(document.currentScript.closest('div'));
|
||||
</script>
|
||||
</div>
|
||||
@ -243,19 +243,19 @@ The HTML that's rendered is:
|
||||
<script src="static/index.js"></script>
|
||||
|
||||
<div data-name="Alice">
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// Place the React component into the parent div.
|
||||
bundle.renderHello(document.currentScript.closest('div'));
|
||||
</script>
|
||||
</div>
|
||||
<div data-name="Bob">
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// Place the React component into the parent div.
|
||||
bundle.renderHello(document.currentScript.closest('div'));
|
||||
</script>
|
||||
</div>
|
||||
<div data-name="Charlie">
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// Place the React component into the parent div.
|
||||
bundle.renderHello(document.currentScript.closest('div'));
|
||||
</script>
|
||||
|
@ -19,7 +19,7 @@ var helloHandle = templ.NewOnceHandle()
|
||||
|
||||
templ hello(label, name string) {
|
||||
@helloHandle.Once() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
@ -35,7 +35,7 @@ templ page() {
|
||||
```
|
||||
|
||||
```html title="Output"
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ templ is designed to prevent user-provided data from being used to inject vulner
|
||||
|
||||
```html
|
||||
templ Example() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function showAlert() {
|
||||
alert("hello");
|
||||
}
|
||||
|
@ -75,12 +75,12 @@ func main() {
|
||||
```
|
||||
|
||||
```html title="Output"
|
||||
<script type="text/javascript" nonce="randomly generated nonce">
|
||||
<script nonce="randomly generated nonce">
|
||||
function __templ_onLoad_5a85() {
|
||||
alert("Hello, world!")
|
||||
}
|
||||
</script>
|
||||
<script type="text/javascript" nonce="randomly generated nonce">
|
||||
<script nonce="randomly generated nonce">
|
||||
__templ_onLoad_5a85()
|
||||
</script>
|
||||
```
|
||||
|
@ -2,7 +2,7 @@ package main
|
||||
|
||||
templ Hello(name string) {
|
||||
<div data-name={ name }>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// Place the React component into the parent div.
|
||||
bundle.renderHello(document.currentScript.closest('div'));
|
||||
</script>
|
||||
|
@ -41,7 +41,7 @@ func Hello(name string) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\"><script type=\"text/javascript\">\n\t\t\t// Place the React component into the parent div.\n\t\t\tbundle.renderHello(document.currentScript.closest('div'));\n\t\t</script></div>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\"><script>\n\t\t\t// Place the React component into the parent div.\n\t\t\tbundle.renderHello(document.currentScript.closest('div'));\n\t\t</script></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@ -973,7 +973,7 @@ func (g *generator) writeElement(indentLevel int, n parser.Element) (err error)
|
||||
if err = g.writeElementCSS(indentLevel, attrs); err != nil {
|
||||
return err
|
||||
}
|
||||
// <script type="text/javascript"></script>
|
||||
// <script></script>
|
||||
if err = g.writeElementScript(indentLevel, attrs); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -1356,7 +1356,7 @@ func (g *generator) writeRawElement(indentLevel int, n parser.RawElement) (err e
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// <script type="text/javascript"></script>
|
||||
// <script></script>
|
||||
if err = g.writeElementScript(indentLevel, n.Attributes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<button onClick="anythingILike('blah')">
|
||||
Click me
|
||||
</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
// Arbitrary JS code
|
||||
</script>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<button onclick="alert("Hello, World!")">
|
||||
Click me
|
||||
</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function customAlert(msg, date) {
|
||||
alert(msg + " " + date);
|
||||
}
|
||||
@ -12,7 +12,7 @@
|
||||
<button onclick="customAlert("Hello, custom alert 2: ","2020-01-01T00:00:00Z")">
|
||||
Click me
|
||||
</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
customAlert("Runs on page load","2020-01-01T00:00:00Z")
|
||||
</script>
|
||||
<script>
|
||||
|
@ -7,7 +7,7 @@ var onceHandle = templ.NewOnceHandle()
|
||||
templ TestComponent() {
|
||||
<button onClick={ templ.JSFuncCall("alert", "Hello, World!") }>Click me</button>
|
||||
@onceHandle.Once() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function customAlert(msg, date) {
|
||||
alert(msg + " " + date);
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ func TestComponent() templ.Component {
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<script type=\"text/javascript\">\n\t\t\tfunction customAlert(msg, date) {\n\t\t\t\talert(msg + \" \" + date);\n\t\t\t}\n\t\t</script>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<script>\n\t\t\tfunction customAlert(msg, date) {\n\t\t\t\talert(msg + \" \" + date);\n\t\t\t}\n\t\t</script>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ var helloHandle = templ.NewOnceHandle()
|
||||
|
||||
templ hello(label, name string) {
|
||||
@helloHandle.Once() {
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function hello(name) {
|
||||
alert('Hello, ' + name + '!');
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func hello(label, name string) templ.Component {
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<script type=\"text/javascript\">\n\t\t\tfunction hello(name) {\n\t\t\t\talert('Hello, ' + name + '!');\n\t\t\t}\n\t\t</script>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<script>\n\t\t\tfunction hello(name) {\n\t\t\t\talert('Hello, ' + name + '!');\n\t\t\t}\n\t\t</script>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
|
||||
}
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
__templ_withParameters_1056("hello","world",42069)
|
||||
</script>
|
@ -7,7 +7,7 @@
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
$("div").marquee();
|
||||
function test() {
|
||||
window.open("https://example.com")
|
||||
|
@ -10,7 +10,7 @@ templ Example() {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
$("div").marquee();
|
||||
function test() {
|
||||
window.open("https://example.com")
|
||||
|
@ -28,7 +28,7 @@ func Example() templ.Component {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html><head></head><body><style><!-- Some stuff --></style><style>\n .customClass {\n border: 1px solid black;\n }\n </style><script type=\"text/javascript\">\n $(\"div\").marquee();\n function test() {\n window.open(\"https://example.com\")\n }\n </script><h1>Hello</h1>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html><head></head><body><style><!-- Some stuff --></style><style>\n .customClass {\n border: 1px solid black;\n }\n </style><script>\n $(\"div\").marquee();\n function test() {\n window.open(\"https://example.com\")\n }\n </script><h1>Hello</h1>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
@ -1,20 +1,20 @@
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_withoutParameters_6bbf(){alert("hello");
|
||||
}
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
__templ_withoutParameters_6bbf()
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
|
||||
}
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
__templ_withParameters_1056("injected","test",123)
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
__templ_withoutParameters_6bbf()
|
||||
</script>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
__templ_withParameters_1056("injected","test",123)
|
||||
</script>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<script type="text/javascript" nonce="nonce1">
|
||||
<script nonce="nonce1">
|
||||
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
|
||||
}function __templ_withoutParameters_6bbf(){alert("hello");
|
||||
}
|
||||
@ -7,12 +7,12 @@
|
||||
<button onClick="__templ_withParameters_1056("test","B",123)" onMouseover="__templ_withoutParameters_6bbf()" type="button">B</button>
|
||||
<button onMouseover="console.log('mouseover')" type="button">Button C</button>
|
||||
<button hx-on::click="alert('clicked inline')" type="button">Button D</button>
|
||||
<script type="text/javascript" nonce="nonce1">
|
||||
<script nonce="nonce1">
|
||||
function __templ_onClick_657d(){alert("clicked");
|
||||
}
|
||||
</script>
|
||||
<button hx-on::click="__templ_onClick_657d()" type="button">Button E</button>
|
||||
<script type="text/javascript" nonce="nonce1">
|
||||
<script nonce="nonce1">
|
||||
function __templ_conditionalScript_de41(){alert("conditional");
|
||||
}
|
||||
</script>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_withParameters_1056(a, b, c){console.log(a, b, c);
|
||||
}function __templ_withoutParameters_6bbf(){alert("hello");
|
||||
}
|
||||
@ -7,22 +7,22 @@
|
||||
<button onClick="__templ_withParameters_1056("test","B",123)" onMouseover="__templ_withoutParameters_6bbf()" type="button">B</button>
|
||||
<button onMouseover="console.log('mouseover')" type="button">Button C</button>
|
||||
<button hx-on::click="alert('clicked inline')" type="button">Button D</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_onClick_657d(){alert("clicked");
|
||||
}
|
||||
</script>
|
||||
<button hx-on::click="__templ_onClick_657d()" type="button">Button E</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_whenButtonIsClicked_253e(event){console.log(event.target)
|
||||
}
|
||||
</script>
|
||||
<button onclick="__templ_whenButtonIsClicked_253e(event)">Button F</button>
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_conditionalScript_de41(){alert("conditional");
|
||||
}
|
||||
</script>
|
||||
<input type="button" value="Click me" onclick="__templ_conditionalScript_de41()" />
|
||||
<script type="text/javascript">
|
||||
<script>
|
||||
function __templ_alertTest_eadf(){alert('testing');
|
||||
}
|
||||
</script>
|
||||
|
10
js_test.go
10
js_test.go
@ -68,7 +68,7 @@ func TestJSFuncCall(t *testing.T) {
|
||||
Call: "doSomething()",
|
||||
CallInline: "doSomething()",
|
||||
},
|
||||
expectedComponentOutput: `<script type="text/javascript">doSomething()</script>`,
|
||||
expectedComponentOutput: `<script>doSomething()</script>`,
|
||||
},
|
||||
{
|
||||
name: "single argument is supported",
|
||||
@ -80,7 +80,7 @@ func TestJSFuncCall(t *testing.T) {
|
||||
Call: "alert("hello")",
|
||||
CallInline: `alert("hello")`,
|
||||
},
|
||||
expectedComponentOutput: `<script type="text/javascript">alert("hello")</script>`,
|
||||
expectedComponentOutput: `<script>alert("hello")</script>`,
|
||||
},
|
||||
{
|
||||
name: "multiple arguments are supported",
|
||||
@ -92,7 +92,7 @@ func TestJSFuncCall(t *testing.T) {
|
||||
Call: "console.log("hello","world")",
|
||||
CallInline: `console.log("hello","world")`,
|
||||
},
|
||||
expectedComponentOutput: `<script type="text/javascript">console.log("hello","world")</script>`,
|
||||
expectedComponentOutput: `<script>console.log("hello","world")</script>`,
|
||||
},
|
||||
{
|
||||
name: "attribute injection fails",
|
||||
@ -104,7 +104,7 @@ func TestJSFuncCall(t *testing.T) {
|
||||
Call: "__templ_invalid_js_function_name()",
|
||||
CallInline: "__templ_invalid_js_function_name()",
|
||||
},
|
||||
expectedComponentOutput: `<script type="text/javascript">__templ_invalid_js_function_name()</script>`,
|
||||
expectedComponentOutput: `<script>__templ_invalid_js_function_name()</script>`,
|
||||
},
|
||||
{
|
||||
name: "closing the script and injecting HTML fails",
|
||||
@ -116,7 +116,7 @@ func TestJSFuncCall(t *testing.T) {
|
||||
Call: "__templ_invalid_js_function_name()",
|
||||
CallInline: "__templ_invalid_js_function_name()",
|
||||
},
|
||||
expectedComponentOutput: `<script type="text/javascript">__templ_invalid_js_function_name()</script>`,
|
||||
expectedComponentOutput: `<script>__templ_invalid_js_function_name()</script>`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -49,7 +49,7 @@ func writeScriptHeader(ctx context.Context, w io.Writer) (err error) {
|
||||
if nonce := GetNonce(ctx); nonce != "" {
|
||||
nonceAttr = " nonce=\"" + EscapeString(nonce) + "\""
|
||||
}
|
||||
_, err = fmt.Fprintf(w, `<script type="text/javascript"%s>`, nonceAttr)
|
||||
_, err = fmt.Fprintf(w, `<script%s>`, nonceAttr)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ func TestRenderScriptItems(t *testing.T) {
|
||||
name: "if none are ignored, everything is rendered",
|
||||
toIgnore: nil,
|
||||
toRender: []templ.ComponentScript{s1, s2},
|
||||
expected: `<script type="text/javascript">` + s1.Function + s2.Function + `</script>`,
|
||||
expected: `<script>` + s1.Function + s2.Function + `</script>`,
|
||||
},
|
||||
{
|
||||
name: "if something outside the expected is ignored, if has no effect",
|
||||
@ -39,13 +39,13 @@ func TestRenderScriptItems(t *testing.T) {
|
||||
},
|
||||
},
|
||||
toRender: []templ.ComponentScript{s1, s2},
|
||||
expected: `<script type="text/javascript">` + s1.Function + s2.Function + `</script>`,
|
||||
expected: `<script>` + s1.Function + s2.Function + `</script>`,
|
||||
},
|
||||
{
|
||||
name: "if one is ignored, it's not rendered",
|
||||
toIgnore: []templ.ComponentScript{s1},
|
||||
toRender: []templ.ComponentScript{s1, s2},
|
||||
expected: `<script type="text/javascript">` + s2.Function + `</script>`,
|
||||
expected: `<script>` + s2.Function + `</script>`,
|
||||
},
|
||||
{
|
||||
name: "if all are ignored, not even style tags are rendered",
|
||||
|
Loading…
x
Reference in New Issue
Block a user