Skip to content

Commit

Permalink
export Element.ParseParams()
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Jan 15, 2024
1 parent 214cb61 commit 49626af
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 66 deletions.
32 changes: 32 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,35 @@ func (e *Element) Order(jidList []jid.Jid) {
func (e *Element) Remove(htmlId string) {
e.queue(what.Remove, htmlId)
}

// ParseParams parses the parameters passed to UI() when creating a new Element,
// setting event handlers and returning a list of HTML attributes.
func (e *Element) ParseParams(params []interface{}) (attrs []template.HTMLAttr) {
for i := range params {
switch data := params[i].(type) {
case template.HTMLAttr:
attrs = append(attrs, data)
case []template.HTMLAttr:
attrs = append(attrs, data...)
case string:
attrs = append(attrs, template.HTMLAttr(data))
case []string:
for _, s := range data {
attrs = append(attrs, template.HTMLAttr(s))
}
case EventFn:
if data != nil {
e.handlers = append(e.handlers, eventFnWrapper{data})
}
default:
if h, ok := data.(ClickHandler); ok {
e.handlers = append(e.handlers, clickHandlerWapper{h})
}
if h, ok := data.(EventHandler); ok {
e.handlers = append(e.handlers, h)
}
e.Tag(data)
}
}
return
}
10 changes: 5 additions & 5 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func needClosingTag(tag string) bool {
return !ok
}

func getAttrsLen(attrs []string) (attrslen int) {
func getAttrsLen(attrs []template.HTMLAttr) (attrslen int) {
for _, s := range attrs {
if s != "" {
attrslen += 1 + len(s)
Expand All @@ -43,7 +43,7 @@ func getAttrsLen(attrs []string) (attrslen int) {
return
}

func appendAttrs(b []byte, attrs []string) []byte {
func appendAttrs(b []byte, attrs []template.HTMLAttr) []byte {
for _, s := range attrs {
if s != "" {
b = append(b, ' ')
Expand All @@ -53,7 +53,7 @@ func appendAttrs(b []byte, attrs []string) []byte {
return b
}

func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...string) (err error) {
func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...template.HTMLAttr) (err error) {
need := 11 + jidPrealloc + 8 + len(typ) + 9 + len(val) + 1 + 1 + getAttrsLen(attrs) + 1
b := make([]byte, 0, need)
b = jid.AppendStartTagAttr(b, "input")
Expand All @@ -69,7 +69,7 @@ func WriteHtmlInput(w io.Writer, jid jid.Jid, typ, val string, attrs ...string)
return
}

func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HTML, attrs ...string) (err error) {
func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HTML, attrs ...template.HTMLAttr) (err error) {
need := 1 + len(tag)*2 + jidPrealloc + 8 + len(typ) + 1 + 1 + getAttrsLen(attrs) + 1 + len(inner) + 2 + 1
b := make([]byte, 0, need)
b = jid.AppendStartTagAttr(b, tag)
Expand All @@ -89,7 +89,7 @@ func WriteHtmlInner(w io.Writer, jid jid.Jid, tag, typ string, inner template.HT
return
}

func WriteHtmlSelect(w io.Writer, jid jid.Jid, nba *NamedBoolArray, attrs ...string) (err error) {
func WriteHtmlSelect(w io.Writer, jid jid.Jid, nba *NamedBoolArray, attrs ...template.HTMLAttr) (err error) {
need := 12 + jidPrealloc + 2 + getAttrsLen(attrs) + 2 + 10
nba.ReadLocked(func(nba []*NamedBool) {
for _, nb := range nba {
Expand Down
18 changes: 9 additions & 9 deletions html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestHtmlInput(t *testing.T) {
jid jid.Jid
typ string
val string
attrs []string
attrs []template.HTMLAttr
}
tests := []struct {
name string
Expand All @@ -35,7 +35,7 @@ func TestHtmlInput(t *testing.T) {
jid: 2,
typ: "input_type2",
val: "initial_val2",
attrs: []string{""},
attrs: []template.HTMLAttr{""},
},
want: `<input id="Jid.2" type="input_type2" value="initial_val2">`,
},
Expand All @@ -45,7 +45,7 @@ func TestHtmlInput(t *testing.T) {
jid: 3,
typ: "input_type2",
val: "initial_val2",
attrs: []string{"some_attr"},
attrs: []template.HTMLAttr{"some_attr"},
},
want: `<input id="Jid.3" type="input_type2" value="initial_val2" some_attr>`,
},
Expand All @@ -55,7 +55,7 @@ func TestHtmlInput(t *testing.T) {
jid: 4,
typ: "input_type2",
val: "initial_val2",
attrs: []string{"some_attr1", "", "some_attr2"},
attrs: []template.HTMLAttr{"some_attr1", "", "some_attr2"},
},
want: `<input id="Jid.4" type="input_type2" value="initial_val2" some_attr1 some_attr2>`,
},
Expand All @@ -79,7 +79,7 @@ func TestHtmlInner(t *testing.T) {
tag string
typ string
inner template.HTML
attrs []string
attrs []template.HTMLAttr
}
tests := []struct {
name string
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestHtmlInner(t *testing.T) {
tag: "tag1",
typ: "typ1",
inner: "inner_text",
attrs: []string{"some_attr1", "some_attr2", ""},
attrs: []template.HTMLAttr{"some_attr1", "some_attr2", ""},
},
want: `<tag1 id="Jid.3" type="typ1" some_attr1 some_attr2>inner_text</tag1>`,
},
Expand All @@ -135,7 +135,7 @@ func TestHtmlSelect(t *testing.T) {
type args struct {
jid jid.Jid
val *NamedBoolArray
attrs []string
attrs []template.HTMLAttr
}
tests := []struct {
name string
Expand All @@ -147,7 +147,7 @@ func TestHtmlSelect(t *testing.T) {
args: args{
jid: 2,
val: NewNamedBoolArray(),
attrs: []string{"attr1"},
attrs: []template.HTMLAttr{"attr1"},
},
want: "<select id=\"Jid.2\" attr1>\n</select>\n",
},
Expand All @@ -163,7 +163,7 @@ func TestHtmlSelect(t *testing.T) {
nba.Set("two", true)
return
}(),
attrs: []string{"", "attr2"},
attrs: []template.HTMLAttr{"", "attr2"},
},
want: `<select id="Jid.3" attr2>
<option value="one">1</option>
Expand Down
6 changes: 3 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func (rq *Request) Register(tagitem interface{}, params ...interface{}) jid.Jid
switch data := tagitem.(type) {
case jid.Jid:
if elem := rq.getElementByJid(data); elem != nil {
if uib, ok := elem.Ui().(*UiHtml); ok {
uib.parseParams(elem, params)
if _, ok := elem.Ui().(*UiHtml); ok {
elem.ParseParams(params)
}
}
return data
Expand All @@ -271,7 +271,7 @@ func (rq *Request) Register(tagitem interface{}, params ...interface{}) jid.Jid
uib := &UiHtml{}
elem := rq.NewElement(uib)
uib.parseGetter(elem, tagitem)
uib.parseParams(elem, params)
elem.ParseParams(params)
return elem.jid
}

Expand Down
4 changes: 2 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (t Template) JawsRender(e *Element, w io.Writer, params []interface{}) erro
e.Request.tagExpanded(e, expandedtags)
}
var sb strings.Builder
for _, s := range parseParams(e, params) {
for _, s := range e.ParseParams(params) {
sb.WriteByte(' ')
sb.WriteString(s)
sb.WriteString(string(s))
}
attrs := template.HTMLAttr(sb.String()) // #nosec G203
return t.Execute(w, With{
Expand Down
38 changes: 3 additions & 35 deletions uihtml.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jaws

import (
"html/template"
"io"

"github.com/linkdata/jaws/what"
Expand All @@ -28,40 +27,9 @@ func (ui *UiHtml) parseGetter(e *Element, getter interface{}) {
}
}

func parseParams(elem *Element, params []interface{}) (attrs []string) {
for i := range params {
switch data := params[i].(type) {
case template.HTML:
attrs = append(attrs, string(data))
case []template.HTML:
for _, s := range data {
attrs = append(attrs, string(s))
}
case string:
attrs = append(attrs, data)
case []string:
attrs = append(attrs, data...)
case EventFn:
if data != nil {
elem.handlers = append(elem.handlers, eventFnWrapper{data})
}
default:
if h, ok := data.(ClickHandler); ok {
elem.handlers = append(elem.handlers, clickHandlerWapper{h})
}
if h, ok := data.(EventHandler); ok {
elem.handlers = append(elem.handlers, h)
}
elem.Tag(data)
}
}
return
}

func (ui *UiHtml) parseParams(elem *Element, params []interface{}) (attrs []string) {
attrs = parseParams(elem, params)
return
}
/*func (ui *UiHtml) parseParams(elem *Element, params []interface{}) (attrs []string) {
return elem.ParseParams(params)
}*/

func (ui *UiHtml) JawsRender(e *Element, w io.Writer, params []interface{}) (err error) {
if h, ok := ui.Tag.(UI); ok {
Expand Down
2 changes: 1 addition & 1 deletion uihtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestUiHtml_JawsEvent(t *testing.T) {
defer close(msgCh)
tje := &testJawsEvent{msgCh: msgCh}

id := rq.Register(Tag("zomg"), tje, "attr1", []string{"attr2"}, template.HTML("attr3"), []template.HTML{"attr4"})
id := rq.Register(Tag("zomg"), tje, "attr1", []string{"attr2"}, template.HTMLAttr("attr3"), []template.HTMLAttr{"attr4"})

rq.inCh <- wsMsg{Data: "text", Jid: id, What: what.Input}
select {
Expand Down
3 changes: 1 addition & 2 deletions uihtmlinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ type UiHtmlInner struct {

func (ui *UiHtmlInner) renderInner(e *Element, w io.Writer, htmltag, htmltype string, params []interface{}) error {
ui.parseGetter(e, ui.HtmlGetter)
attrs := ui.parseParams(e, params)
return WriteHtmlInner(w, e.Jid(), htmltag, htmltype, ui.JawsGetHtml(e), attrs...)
return WriteHtmlInner(w, e.Jid(), htmltag, htmltype, ui.JawsGetHtml(e), e.ParseParams(params)...)
}

func (ui *UiHtmlInner) JawsUpdate(e *Element) {
Expand Down
3 changes: 2 additions & 1 deletion uiimg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jaws

import (
"html/template"
"io"
"strconv"
)
Expand All @@ -20,7 +21,7 @@ func (ui *UiImg) SrcAttr(e *Element) string {

func (ui *UiImg) JawsRender(e *Element, w io.Writer, params []interface{}) error {
ui.parseGetter(e, ui.StringSetter)
attrs := append(ui.parseParams(e, params), "src="+ui.SrcAttr(e))
attrs := append(e.ParseParams(params), template.HTMLAttr("src="+ui.SrcAttr(e)))
return WriteHtmlInner(w, e.Jid(), "img", "", "", attrs...)
}

Expand Down
2 changes: 1 addition & 1 deletion uiinputbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UiInputBool struct {

func (ui *UiInputBool) renderBoolInput(e *Element, w io.Writer, htmltype string, params ...interface{}) error {
ui.parseGetter(e, ui.BoolSetter)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
v := ui.JawsGetBool(e)
ui.Last.Store(v)
if v {
Expand Down
2 changes: 1 addition & 1 deletion uiinputdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ui *UiInputDate) str() string {

func (ui *UiInputDate) renderDateInput(e *Element, w io.Writer, jid Jid, htmltype string, params ...interface{}) error {
ui.parseGetter(e, ui.TimeSetter)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
ui.Last.Store(ui.JawsGetTime(e))
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs...)
}
Expand Down
2 changes: 1 addition & 1 deletion uiinputfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ui *UiInputFloat) str() string {

func (ui *UiInputFloat) renderFloatInput(e *Element, w io.Writer, htmltype string, params ...interface{}) error {
ui.parseGetter(e, ui.FloatSetter)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
ui.Last.Store(ui.JawsGetFloat(e))
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs...)
}
Expand Down
2 changes: 1 addition & 1 deletion uiinputtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type UiInputText struct {

func (ui *UiInputText) renderStringInput(e *Element, w io.Writer, htmltype string, params ...interface{}) error {
ui.parseGetter(e, ui.StringSetter)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
v := ui.JawsGetString(e)
ui.Last.Store(v)
return WriteHtmlInput(w, e.Jid(), htmltype, v, attrs...)
Expand Down
5 changes: 3 additions & 2 deletions uioption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package jaws

import (
"html"
"html/template"
"io"
)

type UiOption struct{ *NamedBool }

func (ui UiOption) JawsRender(e *Element, w io.Writer, params []interface{}) error {
e.Tag(ui.NamedBool)
attrs := parseParams(e, params)
attrs = append(attrs, `value="`+html.EscapeString(ui.JawsGetString(e))+`"`)
attrs := e.ParseParams(params)
attrs = append(attrs, template.HTMLAttr(`value="`+html.EscapeString(ui.JawsGetString(e))+`"`))
if ui.Checked() {
attrs = append(attrs, "selected")
}
Expand Down
2 changes: 1 addition & 1 deletion uitextarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type UiTextarea struct {

func (ui *UiTextarea) JawsRender(e *Element, w io.Writer, params []interface{}) error {
ui.parseGetter(e, ui.StringSetter)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
return WriteHtmlInner(w, e.Jid(), "textarea", "", template.HTML(ui.JawsGetString(e)), attrs...) // #nosec G203
}

Expand Down
2 changes: 1 addition & 1 deletion uiwrapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type uiWrapContainer struct {

func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag string, params []interface{}) error {
ui.parseGetter(e, ui.Container)
attrs := ui.parseParams(e, params)
attrs := e.ParseParams(params)
b := e.jid.AppendStartTagAttr(nil, outerhtmltag)
for _, attr := range attrs {
b = append(b, ' ')
Expand Down

0 comments on commit 49626af

Please sign in to comment.