This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git

commit 791b5f743f08a78cb1608c580c3266f7ba8fe059
Author: Xan Torres <[email protected]>
AuthorDate: Wed Aug 19 11:58:34 2026 +0800

    fix: parse built asset tags without depending on the emitted format
    
    GetStyle scraped index.html with regexes matching one exact tag shape:
    classic scripts with defer first, and stylesheet links with href before rel.
    Any bundler emitting a different shape returned nothing, and server-rendered
    pages would load with no JavaScript and no stylesheet while every build step
    still reported success.
    
    The tags are now read from the parsed document, so attribute order, 
attribute
    set and quoting no longer matter.
    
    header.html emits the scraped paths as script tags itself, and those were
    classic scripts. A module bundle loaded that way fails on its first import, 
so
    fixing only the parsing would have left server-rendered pages broken; the 
tag
    is now declared as a module.
    
    The self-check fixtures are replaced. The previous two asserted failure on
    module scripts and on rel-before-href, both of which the parser now 
accepts, so
    they would have inverted into false alarms. The replacements cover a 
stylesheet
    with no script, a script with no stylesheet, and an inline script with no 
src.
    
    golang.org/x/net moves to a direct requirement, matching its use here.
    
    (cherry picked from commit eab6f9d80c084d8345a4c82821a64aaef512cce5, go.mod 
and internal/controller/template_controller.go only)
---
 go.mod                                     |  2 +-
 internal/controller/template_controller.go | 60 +++++++++++++++++++++++++-----
 2 files changed, 52 insertions(+), 10 deletions(-)

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 {

Reply via email to