vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

Wrong replacing of @ to $ in links in html vweb templates #11978

Open teaalltr opened 3 years ago

teaalltr commented 3 years ago

It looks like @ is a valid character in urls/links. V template engine wrongly replaces it to $ The problem in the code should be somewhere here.

Here is my bug report:

V version: V 0.2.4 d329e1d OS: Win 10

What did you do? Written <script src="https://unpkg.com/htmx.org@1.5.0"></script> in a html template. @ got replaced with $

What did you expect to see?

<html>
<!-- load htmx lib -->
<script src="https://unpkg.com/htmx.org@1.5.0"></script>
<script src="https://unpkg.com/hyperscript.org@0.8.1"></script>
etc...

What did you see instead?

<html>
<!-- load htmx lib -->
<script src="https://unpkg.com/htmx.org$1.5.0"></script>
<script src="https://unpkg.com/hyperscript.org$0.8.1"></script>
etc...
vincenzopalazzo commented 3 years ago

HI @Piruzzolo,

Can you add also a small example to reproduce the problem?

Thanks.

teaalltr commented 3 years ago

Can you add also a small example to reproduce the problem?

Sure:

module main

import vweb

struct App {
    vweb.Context
}

fn main() {
    mut app := App {}
    vweb.run(app, 8081)
}

['/index']
pub fn (app &App) index() vweb.Result {
    return $vweb.html()
}

and in index.html:

<html>
    <script src="https://unpkg.com/htmx.org@1.5.0"></script>
    <body>
    </body>
</html>

Here the files for convenience (zipped because GitHub doesn't support the file type): blog.zip

JalonSolov commented 3 years ago

Use @@ if you don't want it changed.

Otherwise, you could be doing a template replacement, even in a URL.

teaalltr commented 3 years ago

@JalonSolov thanks, that's a neat workaroud

teaalltr commented 3 years ago

I think we shouldn't close it, the problem is still present in the code and a workaround can't be the the definitive solution @JalonSolov

JalonSolov commented 3 years ago

Why not? It's not a workaround, it's the way to escape the @ to get a literal @ instead of a template replacement.

The same as you have to escape other characters in strings, etc. For example, if you wanted to print a literal ${ in V, you would have to escape the $ or V will think you're doing string interpolation. Every language in existence requires escaping something...

teaalltr commented 3 years ago

Indeed but having to skip it is not what a user would expect I guess. At least I didn't expect it at all

JalonSolov commented 3 years ago

Then at the very least, it needs to be documented better.

vladimir-light commented 1 year ago

I think it would be nice to have a special template directive, which prevents parsing within {...}

Maybe something like

@raw {
  <div>@include 'header/base'</div>
}

In this case, parser wouldn't try to include './header/base.html' rather just output as is.

Many other template engines provide such functionality. e.G. Twig with {% verbatim %} (https://twig.symfony.com/doc/3.x/tags/verbatim.html)