Skip to content

Commit

Permalink
ParseParams returns attrs as string slice
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Apr 10, 2024
1 parent 160a625 commit 3966717
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 6 additions & 2 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,15 @@ func (e *Element) Remove(htmlId string) {

// ApplyParams parses the parameters passed to UI() when creating a new Element,
// adding UI tags, setting event handlers and returning a list of HTML attributes.
func (e *Element) ApplyParams(params []any) []template.HTMLAttr {
func (e *Element) ApplyParams(params []any) (retv []template.HTMLAttr) {
tags, handlers, attrs := ParseParams(params)
if !e.deleted {
e.handlers = append(e.handlers, handlers...)
e.Tag(tags...)
for _, s := range attrs {
attr := template.HTMLAttr(s) // #nosec G203
retv = append(retv, attr)
}
}
return attrs
return
}
16 changes: 7 additions & 9 deletions parseparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import "html/template"

// ParseParams parses the parameters passed to UI() when creating a new Element,
// returning UI tags, event handlers and HTML attributes.
func ParseParams(params []any) (tags []any, handlers []EventHandler, attrs []template.HTMLAttr) {
func ParseParams(params []any) (tags []any, handlers []EventHandler, attrs []string) {
for i := range params {
switch data := params[i].(type) {
case template.HTMLAttr:
attrs = append(attrs, data)
attrs = append(attrs, string(data))
case []template.HTMLAttr:
attrs = append(attrs, data...)
case string:
attr := template.HTMLAttr(data) // #nosec G203
attrs = append(attrs, attr)
case []string:
for _, s := range data {
attr := template.HTMLAttr(s) // #nosec G203
attrs = append(attrs, attr)
attrs = append(attrs, string(s))
}
case string:
attrs = append(attrs, data)
case []string:
attrs = append(attrs, data...)
case EventFn:
if data != nil {
handlers = append(handlers, eventFnWrapper{data})
Expand Down
12 changes: 5 additions & 7 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@ func (t Template) JawsRender(e *Element, w io.Writer, params []any) error {
if expandedtags, err := TagExpand(e.Request, t.Dot); err != ErrIllegalTagType {
e.Request.tagExpanded(e, expandedtags)
}
var sb strings.Builder
for _, s := range e.ApplyParams(params) {
sb.WriteByte(' ')
sb.WriteString(string(s))
}
attrs := template.HTMLAttr(sb.String()) // #nosec G203
tags, handlers, attrs := ParseParams(params)
e.Tag(tags...)
e.handlers = append(e.handlers, handlers...)
attrstr := template.HTMLAttr(strings.Join(attrs, " ")) // #nosec G203
return e.Request.MustTemplate(t.Template).Execute(w, With{
Element: e,
RequestWriter: e.Request.Writer(w),
Dot: t.Dot,
Attrs: attrs,
Attrs: attrstr,
})
}

Expand Down
2 changes: 1 addition & 1 deletion testjaws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newTestJaws() (tj *testJaws) {
Jaws: New(),
}
tj.Jaws.Logger = log.New(&tj.log, "", 0)
tj.testtmpl = template.Must(template.New("testtemplate").Parse(`{{with $.Dot}}<div id="{{$.Jid}}"{{$.Attrs}}>{{.}}</div>{{end}}`))
tj.testtmpl = template.Must(template.New("testtemplate").Parse(`{{with $.Dot}}<div id="{{$.Jid}}" {{$.Attrs}}>{{.}}</div>{{end}}`))
tj.AddTemplateLookuper(tj.testtmpl)
tj.Jaws.updateTicker = time.NewTicker(time.Millisecond)
go tj.Serve()
Expand Down

0 comments on commit 3966717

Please sign in to comment.