Skip to content

Commit

Permalink
add WriteHtmlTag
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Jan 15, 2024
1 parent 49626af commit cab8efa
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
47 changes: 23 additions & 24 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,38 @@ func appendAttrs(b []byte, attrs []template.HTMLAttr) []byte {
return b
}

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")
b = append(b, ` type=`...)
b = strconv.AppendQuote(b, typ)
if val != "" {
func WriteHtmlTag(w io.Writer, jid jid.Jid, htmlTag, typeAttr, valueAttr string, attrs []template.HTMLAttr) (err error) {
b := make([]byte, 0, 64)
b = jid.AppendStartTagAttr(b, htmlTag)
if typeAttr != "" {
b = append(b, ` type=`...)
b = strconv.AppendQuote(b, typeAttr)
}
if valueAttr != "" {
b = append(b, ` value=`...)
b = strconv.AppendQuote(b, val)
b = strconv.AppendQuote(b, valueAttr)
}
b = appendAttrs(b, attrs)
b = append(b, '>')
_, err = w.Write(b)
return
}

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)
if typ != "" {
b = append(b, ` type=`...)
b = strconv.AppendQuote(b, typ)
}
b = appendAttrs(b, attrs)
b = append(b, '>')
if inner != "" || needClosingTag(tag) {
b = append(b, inner...)
b = append(b, "</"...)
b = append(b, tag...)
b = append(b, '>')
func WriteHtmlInput(w io.Writer, jid jid.Jid, typeAttr, valueAttr string, attrs []template.HTMLAttr) (err error) {
return WriteHtmlTag(w, jid, "input", typeAttr, valueAttr, attrs)
}

func WriteHtmlInner(w io.Writer, jid jid.Jid, htmlTag, typeAttr string, innerHtml template.HTML, attrs ...template.HTMLAttr) (err error) {
if err = WriteHtmlTag(w, jid, htmlTag, typeAttr, "", attrs); err == nil {
if innerHtml != "" || needClosingTag(htmlTag) {
w.Write([]byte(innerHtml))
var b []byte
b = append(b, "</"...)
b = append(b, htmlTag...)
b = append(b, '>')
w.Write(b)
}
}
_, err = w.Write(b)
return
}

Expand Down
2 changes: 1 addition & 1 deletion html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestHtmlInput(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var sb strings.Builder
if err := WriteHtmlInput(&sb, tt.args.jid, tt.args.typ, tt.args.val, tt.args.attrs...); err != nil {
if err := WriteHtmlInput(&sb, tt.args.jid, tt.args.typ, tt.args.val, tt.args.attrs); err != nil {
t.Fatal(err)
}
if got := sb.String(); got != tt.want {
Expand Down
2 changes: 1 addition & 1 deletion uiinputbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (ui *UiInputBool) renderBoolInput(e *Element, w io.Writer, htmltype string,
if v {
attrs = append(attrs, "checked")
}
return WriteHtmlInput(w, e.Jid(), htmltype, "", attrs...)
return WriteHtmlInput(w, e.Jid(), htmltype, "", attrs)
}

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

func (ui *UiInputDate) JawsUpdate(e *Element) {
Expand Down
2 changes: 1 addition & 1 deletion uiinputfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (ui *UiInputFloat) renderFloatInput(e *Element, w io.Writer, htmltype strin
ui.parseGetter(e, ui.FloatSetter)
attrs := e.ParseParams(params)
ui.Last.Store(ui.JawsGetFloat(e))
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs...)
return WriteHtmlInput(w, e.Jid(), htmltype, ui.str(), attrs)
}

func (ui *UiInputFloat) JawsUpdate(e *Element) {
Expand Down
2 changes: 1 addition & 1 deletion uiinputtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (ui *UiInputText) renderStringInput(e *Element, w io.Writer, htmltype strin
attrs := e.ParseParams(params)
v := ui.JawsGetString(e)
ui.Last.Store(v)
return WriteHtmlInput(w, e.Jid(), htmltype, v, attrs...)
return WriteHtmlInput(w, e.Jid(), htmltype, v, attrs)
}

func (ui *UiInputText) JawsUpdate(e *Element) {
Expand Down

0 comments on commit cab8efa

Please sign in to comment.