Go html/template 过滤 script 里的注释?! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zhshch
V2EX    Go 编程语言

Go html/template 过滤 script 里的注释?!

  •  
  •   zhshch 2020-03-08 20:40:40 +08:00 2854 次点击
    这是一个创建于 2051 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码是这样的:

    输出是这样的:

    https 后面的 //被截没了?

    我之前见过这个标准库过滤<!---->的注释,这 js 里的注释也会处理掉?还处理错了位置?

    我肯定是跟这个库有什么误会。。。

    第 1 条附言    2020-03-09 08:27:06 +08:00

    又做了一些测试

    1. 这个问题只发现于Gin里使用两个文件保存模板
    2. 手动破坏template里的isComment函数可以使返回结果正常
    3. 更新到Go1.14没有变化

    简化后的代码

    //Go 1.14 amd64 win10 package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { r := gin.Default() r.Static("/assets", "./assets") r.LoadHTMLGlob("tmpl/*") r.GET("/test", func(ctx *gin.Context) { ctx.HTML(http.StatusOK, "test.tmpl", map[string]interface{}{}) }) if err := r.Run(); err != nil { panic(err) } } 
    <!-- test.tmpl --> <!doctype html> <html lang="zh-cn"> <head> {{template "head"}} </head> <body> {{template "foot"}} </body> </html> <!-- base.tmpl --> {{define "head"}} <meta charset="UTF-8"> <!-- bulabula... --> {{end}} {{define "foot"}} <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <script type="text/Javascript" src="http://www.v2ex.com//js.users.51.la/00000.js"></script> <script> if ($("#Client-IP").length > 0) { $.ajax({ url: "https://myip.ipip.net/", success: function (result) {} ); $.ajax({ url: "https://api.ip.sb/geoip", # 这里就是图片哪里被吃的地方 success: function (result) {} }); } </script> {{end}} 
    15 条回复    2020-03-11 12:22:32 +08:00
    Reficul
        1
    Reficul  
       2020-03-08 21:53:33 +08:00
    发个最小 Demo 的 Code 上来看看?
    thefack
        2
    thefack  
       2020-03-08 21:56:27 +08:00
    有点想笑。。
    tealover007
        3
    tealover007  
       2020-03-08 22:21:22 +08:00
    试着\/\/转一下?
    CEBBCAT
        4
    CEBBCAT  
       2020-03-08 22:22:10 +08:00 via Android
    按理说不应该的,上个 demo+1
    loading
        6
    loading  
       2020-03-08 22:27:05 +08:00 via Android
    为啥要在 html 里直接写 js。
    html 和 js 视乎是分开处理。
    Yoock
        7
    Yoock  
       2020-03-08 22:30:21 +08:00
    字符串转义
    reus
        8
    reus  
       2020-03-08 23:28:11 +08:00   1
    https://play.golang.org/p/2F-FbNXWSko 根本就不会。

    几行代码就能验证的事情。你的判断是错误的。
    reus
        9
    reus  
       2020-03-08 23:38:07 +08:00
    不过字符串外面的注释确实会过滤掉,可能旧版本有 bug 也不奇怪,升级到 1.14 试试
    zhshch
        10
    zhshch  
    OP
       2020-03-09 08:30:07 +08:00
    @Reficul #1
    @CEBBCAT #4 Code 上了

    @tealover007 #3 输出结果变成 https:\/\/api.ip.sb/geoip 也不是预期

    @loading #6 确实昨晚上分成两个文件规避了,但是这个现象依旧很困惑
    Vegetable
        11
    Vegetable  
       2020-03-09 10:06:25 +08:00
    你给出的 demo 无法复现
    BlackBerry999
        12
    BlackBerry999  
       2020-03-09 11:23:11 +08:00
    url 编码一下
    palytoxin
        13
    palytoxin  
       2020-03-09 14:42:29 +08:00 via iPhone
    你引入的 cdn 有没有被修改掉?
    Reficul
        14
    Reficul  
       2020-03-11 10:29:57 +08:00
    直接调用 html/template 无法复现
    noisywolf
        15
    noisywolf  
       2020-03-11 12:22:32 +08:00
    你给出的 demo 无法复现
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1167 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 23:50 PVG 07:50 LAX 16:50 JFK 19:50
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86