Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: parse built asset tags without depending on the emitted format

Xan Torres791b5f743f08
diff --git a/go.mod b/go.mod
index 5787c8b1..7b68c180 100644
--- a/go.mod
+++ b/go.mod
@@ -64,6 +64,7 @@ require (
go.uber.org/mock v0.6.0
golang.org/x/crypto v0.53.0
golang.org/x/image v0.20.0
+ golang.org/x/net v0.56.0
golang.org/x/term v0.44.0
golang.org/x/text v0.39.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
@@ -170,7 +171,6 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/arch v0.10.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
- golang.org/x/net v0.56.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/tools v0.47.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 31cc5152..8290d8b6 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -20,6 +20,7 @@
package controller
import (
+ "bytes"
"encoding/json"
"fmt"
"html/template"
@@ -50,6 +51,7 @@ import (
"github.com/apache/answer/ui"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/log"
+ "golang.org/x/net/html"
)
var SiteUrl = ""
@@ -88,19 +90,59 @@ func GetStyle() (script []string, css string) {
if err != nil {
return
}
- scriptRegexp := regexp.MustCompile(`<script defer="defer" src="([^"]*)"></script>`)
- scriptData := scriptRegexp.FindAllStringSubmatch(string(file), -1)
- for _, s := range scriptData {
- if len(s) == 2 {
- script = append(script, s[1])
+
+ // The frontend build tool controls attribute order, attribute set (e.g.
+ // module vs classic scripts), and quoting for the emitted tags, and that
+ // shape has already changed once. Walk the parsed DOM instead of matching
+ // a literal tag shape so the next bundler change fails a test instead of
+ // silently shipping pages with no JS or CSS.
+ doc, err := html.Parse(bytes.NewReader(file))
+ if err != nil {
+ return
+ }
+
+ attr := func(n *html.Node, key string) (string, bool) {
+ for _, a := range n.Attr {
+ if a.Key == key {
+ return a.Val, true
+ }
+ }
+ return "", false
+ }
+ isStylesheet := func(n *html.Node) bool {
+ rel, ok := attr(n, "rel")
+ if !ok {
+ return false
+ }
+ for _, tok := range strings.Fields(rel) {
+ if strings.EqualFold(tok, "stylesheet") {
+ return true
+ }
}
+ return false
}
- cssRegexp := regexp.MustCompile(`<link href="(.*)" rel="stylesheet">`)
- cssListData := cssRegexp.FindStringSubmatch(string(file))
- if len(cssListData) == 2 {
- css = cssListData[1]
+ var walk func(*html.Node)
+ walk = func(n *html.Node) {
+ if n.Type == html.ElementNode {
+ switch n.Data {
+ case "script":
+ if src, ok := attr(n, "src"); ok && src != "" {
+ script = append(script, src)
+ }
+ case "link":
+ if css == "" && isStylesheet(n) {
+ if href, ok := attr(n, "href"); ok && href != "" {
+ css = href
+ }
+ }
+ }
+ }
+ for c := n.FirstChild; c != nil; c = c.NextSibling {
+ walk(c)
+ }
}
+ walk(doc)
return
}
func (tc *TemplateController) SiteInfo(ctx *gin.Context) *schema.TemplateSiteInfoResp {