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 · 2430 次点击
    这是一个创建于 1512 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码是这样的:

    输出是这样的:

    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="//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
    Mohanson
        5
    Mohanson  
       2020-03-08 22:26:54 +08:00   ❤️ 1
    我的经验是, 当怀疑语言本身 /标准库 /操作系统 /计算机有 BUG 的时候, 99.9% 是自己的代码有 BUG...
    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   ·   我们的愿景   ·   实用小工具   ·   2962 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:33 · PVG 22:33 · LAX 07:33 · JFK 10:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.