The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zhshch

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

  •  
  •   zhshch · Mar 8, 2020 · 3216 views
    This topic created in 2302 days ago, the information mentioned may be changed or developed.

    代码是这样的:

    输出是这样的:

    https 后面的 //被截没了?

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

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

    Supplement 1  ·  Mar 9, 2020

    又做了一些测试

    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 replies    2020-03-11 12:22:32 +08:00
    Reficul
        1
    Reficul  
       Mar 8, 2020
    发个最小 Demo 的 Code 上来看看?
    thefack
        2
    thefack  
       Mar 8, 2020
    有点想笑。。
    tealover007
        3
    tealover007  
       Mar 8, 2020
    试着\/\/转一下?
    CEBBCAT
        4
    CEBBCAT  
       Mar 8, 2020 via Android
    按理说不应该的,上个 demo+1
    Mohanson
        5
    Mohanson  
       Mar 8, 2020   ❤️ 1
    我的经验是, 当怀疑语言本身 /标准库 /操作系统 /计算机有 BUG 的时候, 99.9% 是自己的代码有 BUG...
    loading
        6
    loading  
       Mar 8, 2020 via Android
    为啥要在 html 里直接写 js。
    html 和 js 视乎是分开处理。
    Yoock
        7
    Yoock  
       Mar 8, 2020
    字符串转义
    reus
        8
    reus  
       Mar 8, 2020   ❤️ 1
    https://play.golang.org/p/2F-FbNXWSko 根本就不会。

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

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

    @loading #6 确实昨晚上分成两个文件规避了,但是这个现象依旧很困惑
    Vegetable
        11
    Vegetable  
       Mar 9, 2020
    你给出的 demo 无法复现
    BlackBerry999
        12
    BlackBerry999  
       Mar 9, 2020
    url 编码一下
    palytoxin
        13
    palytoxin  
       Mar 9, 2020 via iPhone
    你引入的 cdn 有没有被修改掉?
    Reficul
        14
    Reficul  
       Mar 11, 2020
    直接调用 html/template 无法复现
    noisywolf
        15
    noisywolf  
       Mar 11, 2020
    你给出的 demo 无法复现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2405 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 00:55 · PVG 08:55 · LAX 17:55 · JFK 20:55
    ♥ Do have faith in what you're doing.