diff --git a/sui/api/request.go b/sui/api/request.go index 34ef558b..389a7a50 100644 --- a/sui/api/request.go +++ b/sui/api/request.go @@ -179,7 +179,7 @@ func (r *Request) Render() (string, int, error) { printData = true } - parser := core.NewTemplateParser(data, &core.ParserOption{PrintData: printData}) + parser := core.NewTemplateParser(data, &core.ParserOption{PrintData: printData, Request: true}) html, err := parser.Render(c.HTML) if err != nil { return "", 500, fmt.Errorf("render error, please re-complie the page %s", err.Error()) diff --git a/sui/core/parser.go b/sui/core/parser.go index e75ed1d1..5d4da7be 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -32,6 +32,18 @@ type ParserOption struct { Editor bool `json:"editor,omitempty"` Preview bool `json:"preview,omitempty"` PrintData bool `json:"print_data,omitempty"` + Request bool `json:"request,omitempty"` +} + +var keepWords = map[string]bool{ + "s:if": true, + "s:for": true, + "s:for-item": true, + "s:for-index": true, + "s:elif": true, + "s:else": true, + "s:set": true, + "s:bind": true, } // NewTemplateParser create a new template parser @@ -98,6 +110,15 @@ func (parser *TemplateParser) Render(html string) (string, error) { return doc.Find("body").Html() } + // For Request + if parser.option != nil && (parser.option.Request || parser.option.Preview) { + // Remove the sui-hide attribute + doc.Find("[sui-hide]").Remove() + + // Remove All comments + parser.tidy(doc.Selection) + } + // fmt.Println(doc.Html()) // fmt.Println(parser.errors) return doc.Html() @@ -406,17 +427,19 @@ func (parser *TemplateParser) hide(sel *goquery.Selection) { return } - style := sel.AttrOr("style", "") - if strings.Contains(style, "display: none") { - return - } + sel.SetAttr("sui-hide", "true") - if style != "" { - style = fmt.Sprintf("%s; display: none", style) - } else { - style = "display: none" - } - sel.SetAttr("style", style) + // style := sel.AttrOr("style", "") + // if strings.Contains(style, "display: none") { + // return + // } + + // if style != "" { + // style = fmt.Sprintf("%s; display: none", style) + // } else { + // style = "display: none" + // } + // sel.SetAttr("style", style) } func (parser *TemplateParser) show(sel *goquery.Selection) { @@ -426,18 +449,46 @@ func (parser *TemplateParser) show(sel *goquery.Selection) { return } - style := sel.AttrOr("style", "") - if !strings.Contains(style, "display: none") { - return - } + sel.RemoveAttr("sui-hide") - style = strings.ReplaceAll(style, "display: none", "") - if style == "" { - sel.RemoveAttr("style") - return - } + // style := sel.AttrOr("style", "") + // if !strings.Contains(style, "display: none") { + // return + // } - sel.SetAttr("style", style) + // style = strings.ReplaceAll(style, "display: none", "") + // if style == "" { + // sel.RemoveAttr("style") + // return + // } + + // sel.SetAttr("style", style) +} + +func (parser *TemplateParser) tidy(selection *goquery.Selection) { + selection.Contents().Each(func(i int, s *goquery.Selection) { + + if s.Nodes[0].Type == html.CommentNode { + s.Remove() + return + } + + // Remove the s:key-* attributes + if s.Nodes[0].Type == html.ElementNode { + for _, attr := range s.Nodes[0].Attr { + if attr.Key == "parsed" || keepWords[attr.Key] || strings.HasPrefix(attr.Key, "s:key") || strings.HasPrefix(attr.Key, "s:bind") { + s.RemoveAttr(attr.Key) + } + } + + if s.Nodes[0].Data == "s:set" { + s.Remove() + return + } + } + + parser.tidy(s) + }) } func (parser *TemplateParser) key(prefix string, sel *goquery.Selection) string {