Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json string is wrapped with backtick #621

Closed
gyuber opened this issue Oct 27, 2023 · 7 comments
Closed

Json string is wrapped with backtick #621

gyuber opened this issue Oct 27, 2023 · 7 comments

Comments

@gyuber
Copy link

gyuber commented Oct 27, 2023

if exist double, single quote in string, string is wrapped with backtick
but backtick is compatible depending on the browser. ( ie browser )
example)
https://play.golang.org/p/I5c1a6yOtjk

type Json struct {
    HTML string `json:"html"`
}
func main() {
    json := Json{}
    json.HTML = `<div class="test" onclick="test('test'))">`
    html := `<script> var json = {{ . }}</script>>`

    t := template.Must(template.New("html").Parse(html))
    var tpl bytes.Buffer
    t.Execute(&tpl, json)

    s, err := m.String("text/html", tpl.String())
    if err != nil {
        panic(err)
    }
    fmt.Println(s)
}

<script>var json={html:`<div class="test" onclick="test('test'))">`}</script>>

Can You Keep value in html/template (tpl.String() ?

@gyuber gyuber changed the title json string is wrapped with backtick Json string is wrapped with backtick Oct 27, 2023
@tdewolff
Copy link
Owner

Using backticks (template literals) is the smallest version of the code. The only browser that doesn't support this is IE, but since it is not supported anymore (https://support.microsoft.com/en-us/windows/internet-explorer-help-23360e49-9cd3-4dda-ba52-705336cc0de2) I wasn't planning on supporting it. However, perhaps we could support ECMAVersion < 2015 (template literals were added in 2015) in the JS minifier.

@tdewolff
Copy link
Owner

Implemented, please specify Version < 2015 in the JS minifier options! Please let me know if that fixes your issue.

@gyuber
Copy link
Author

gyuber commented Oct 30, 2023

@tdewolff
Add option !

m.Add("text/html", &js.Minifier{
    Version: 2014,
})

but panic..

panic: unexpected < in expression on line 1 and column 1
    1: <script> var json = {"html":"\u003cdiv class=\"test\" onc...

how to use JS minifier options?

Share my code
https://go.dev/play/p/aIz-lw9ps-I

@tdewolff
Copy link
Owner

Please see the documentation, you're trying to minify text/html with a JavaScript minifier! Use:

m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$"), &js.Minifier{
    Version: 2014,
})

// ...

s, err := m.String("text/html", tpl.String())

@gyuber
Copy link
Author

gyuber commented Oct 31, 2023

@tdewolff
my html code mixed html & javscript
See below code

<body>
   <div>...</div>
</body>
<script>
  var json = {{ . }}
</script>

So I minified it to text/html
if html & script are mixed, must be run it separately?

@tdewolff
Copy link
Owner

No, you minify a file only once. It is initially parsed as HTML, but since HTML can contains XML, JavaScript or CSS, each part is parsed successively by the corresponding minifiers. I've added support for HTML templates in script/style by the way.

@gyuber
Copy link
Author

gyuber commented Nov 1, 2023

oh! perpect!
my code works well!
https://go.dev/play/p/OKtgsUk4Qoy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants