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

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

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

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

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

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