|
5 | 5 | "context"
|
6 | 6 | "crypto/sha256"
|
7 | 7 | "encoding/hex"
|
8 |
| - "encoding/json" |
9 | 8 | "errors"
|
10 | 9 | "fmt"
|
11 | 10 | "html"
|
@@ -491,42 +490,7 @@ func RenderAttributes(ctx context.Context, w io.Writer, attributes Attributes) (
|
491 | 490 | return nil
|
492 | 491 | }
|
493 | 492 |
|
494 |
| -// Script handling. |
495 |
| - |
496 |
| -func safeEncodeScriptParams(escapeHTML bool, params []any) []string { |
497 |
| - encodedParams := make([]string, len(params)) |
498 |
| - for i := 0; i < len(encodedParams); i++ { |
499 |
| - enc, _ := json.Marshal(params[i]) |
500 |
| - if !escapeHTML { |
501 |
| - encodedParams[i] = string(enc) |
502 |
| - continue |
503 |
| - } |
504 |
| - encodedParams[i] = EscapeString(string(enc)) |
505 |
| - } |
506 |
| - return encodedParams |
507 |
| -} |
508 |
| - |
509 |
| -// SafeScript encodes unknown parameters for safety for inside HTML attributes. |
510 |
| -func SafeScript(functionName string, params ...any) string { |
511 |
| - encodedParams := safeEncodeScriptParams(true, params) |
512 |
| - sb := new(strings.Builder) |
513 |
| - sb.WriteString(functionName) |
514 |
| - sb.WriteRune('(') |
515 |
| - sb.WriteString(strings.Join(encodedParams, ",")) |
516 |
| - sb.WriteRune(')') |
517 |
| - return sb.String() |
518 |
| -} |
519 |
| - |
520 |
| -// SafeScript encodes unknown parameters for safety for inline scripts. |
521 |
| -func SafeScriptInline(functionName string, params ...any) string { |
522 |
| - encodedParams := safeEncodeScriptParams(false, params) |
523 |
| - sb := new(strings.Builder) |
524 |
| - sb.WriteString(functionName) |
525 |
| - sb.WriteRune('(') |
526 |
| - sb.WriteString(strings.Join(encodedParams, ",")) |
527 |
| - sb.WriteRune(')') |
528 |
| - return sb.String() |
529 |
| -} |
| 493 | +// Context. |
530 | 494 |
|
531 | 495 | type contextKeyType int
|
532 | 496 |
|
@@ -603,95 +567,6 @@ func getContext(ctx context.Context) (context.Context, *contextValue) {
|
603 | 567 | return ctx, v
|
604 | 568 | }
|
605 | 569 |
|
606 |
| -// ComponentScript is a templ Script template. |
607 |
| -type ComponentScript struct { |
608 |
| - // Name of the script, e.g. print. |
609 |
| - Name string |
610 |
| - // Function to render. |
611 |
| - Function string |
612 |
| - // Call of the function in JavaScript syntax, including parameters, and |
613 |
| - // ensures parameters are HTML escaped; useful for injecting into HTML |
614 |
| - // attributes like onclick, onhover, etc. |
615 |
| - // |
616 |
| - // Given: |
617 |
| - // functionName("some string",12345) |
618 |
| - // It would render: |
619 |
| - // __templ_functionName_sha("some string",12345)) |
620 |
| - // |
621 |
| - // This is can be injected into HTML attributes: |
622 |
| - // <button onClick="__templ_functionName_sha("some string",12345))">Click Me</button> |
623 |
| - Call string |
624 |
| - // Call of the function in JavaScript syntax, including parameters. It |
625 |
| - // does not HTML escape parameters; useful for directly calling in script |
626 |
| - // elements. |
627 |
| - // |
628 |
| - // Given: |
629 |
| - // functionName("some string",12345) |
630 |
| - // It would render: |
631 |
| - // __templ_functionName_sha("some string",12345)) |
632 |
| - // |
633 |
| - // This is can be used to call the function inside a script tag: |
634 |
| - // <script>__templ_functionName_sha("some string",12345))</script> |
635 |
| - CallInline string |
636 |
| -} |
637 |
| - |
638 |
| -var _ Component = ComponentScript{} |
639 |
| - |
640 |
| -func writeScriptHeader(ctx context.Context, w io.Writer) (err error) { |
641 |
| - var nonceAttr string |
642 |
| - if nonce := GetNonce(ctx); nonce != "" { |
643 |
| - nonceAttr = " nonce=\"" + EscapeString(nonce) + "\"" |
644 |
| - } |
645 |
| - _, err = fmt.Fprintf(w, `<script type="text/javascript"%s>`, nonceAttr) |
646 |
| - return err |
647 |
| -} |
648 |
| - |
649 |
| -func (c ComponentScript) Render(ctx context.Context, w io.Writer) error { |
650 |
| - err := RenderScriptItems(ctx, w, c) |
651 |
| - if err != nil { |
652 |
| - return err |
653 |
| - } |
654 |
| - if len(c.Call) > 0 { |
655 |
| - if err = writeScriptHeader(ctx, w); err != nil { |
656 |
| - return err |
657 |
| - } |
658 |
| - if _, err = io.WriteString(w, c.CallInline); err != nil { |
659 |
| - return err |
660 |
| - } |
661 |
| - if _, err = io.WriteString(w, `</script>`); err != nil { |
662 |
| - return err |
663 |
| - } |
664 |
| - } |
665 |
| - return nil |
666 |
| -} |
667 |
| - |
668 |
| -// RenderScriptItems renders a <script> element, if the script has not already been rendered. |
669 |
| -func RenderScriptItems(ctx context.Context, w io.Writer, scripts ...ComponentScript) (err error) { |
670 |
| - if len(scripts) == 0 { |
671 |
| - return nil |
672 |
| - } |
673 |
| - _, v := getContext(ctx) |
674 |
| - sb := new(strings.Builder) |
675 |
| - for _, s := range scripts { |
676 |
| - if !v.hasScriptBeenRendered(s.Name) { |
677 |
| - sb.WriteString(s.Function) |
678 |
| - v.addScript(s.Name) |
679 |
| - } |
680 |
| - } |
681 |
| - if sb.Len() > 0 { |
682 |
| - if err = writeScriptHeader(ctx, w); err != nil { |
683 |
| - return err |
684 |
| - } |
685 |
| - if _, err = io.WriteString(w, sb.String()); err != nil { |
686 |
| - return err |
687 |
| - } |
688 |
| - if _, err = io.WriteString(w, `</script>`); err != nil { |
689 |
| - return err |
690 |
| - } |
691 |
| - } |
692 |
| - return nil |
693 |
| -} |
694 |
| - |
695 | 570 | var bufferPool = sync.Pool{
|
696 | 571 | New: func() any {
|
697 | 572 | return new(bytes.Buffer)
|
|
0 commit comments