Skip to content

Commit

Permalink
remove UiHtml, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Dec 16, 2024
1 parent 7af84d4 commit 6e718e5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 85 deletions.
1 change: 0 additions & 1 deletion uihtml_test.go → jawsevent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

type testJawsEvent struct {
UiHtml
msgCh chan string
tag any
clickerr error
Expand Down
9 changes: 0 additions & 9 deletions uihtml.go

This file was deleted.

3 changes: 1 addition & 2 deletions uihtmlinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
)

type UiHtmlInner struct {
UiHtml
HtmlGetter
}

func (ui *UiHtmlInner) renderInner(e *Element, w io.Writer, htmltag, htmltype string, params []any) error {
ui.applyGetter(e, ui.HtmlGetter)
e.ApplyGetter(ui.HtmlGetter)
return WriteHtmlInner(w, e.Jid(), htmltag, htmltype, ui.JawsGetHtml(e), e.ApplyParams(params)...)
}

Expand Down
3 changes: 1 addition & 2 deletions uiimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
)

type UiImg struct {
UiHtml
Getter[string]
}

func (ui *UiImg) JawsRender(e *Element, w io.Writer, params []any) error {
ui.applyGetter(e, ui.Getter)
e.ApplyGetter(ui.Getter)
srcattr := template.HTMLAttr("src=" + strconv.Quote(ui.JawsGet(e))) // #nosec G203
attrs := append(e.ApplyParams(params), srcattr)
return WriteHtmlInner(w, e.Jid(), "img", "", "", attrs...)
Expand Down
6 changes: 5 additions & 1 deletion uiinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package jaws
import "sync/atomic"

type UiInput struct {
UiHtml
Tag any
Last atomic.Value
}

func (ui *UiInput) applyGetter(e *Element, getter any) {
ui.Tag = e.ApplyGetter(getter)
}

func (ui *UiInput) maybeDirty(val any, e *Element, err error) error {
var changed bool
if changed, err = e.maybeDirty(ui.Tag, err); changed {
Expand Down
2 changes: 1 addition & 1 deletion uiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type UiSelect struct {

func NewUiSelect(sh SelectHandler) *UiSelect {
return &UiSelect{
uiWrapContainer{
uiWrapContainer: uiWrapContainer{
Container: sh,
},
}
Expand Down
4 changes: 2 additions & 2 deletions uiwrapcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

type uiWrapContainer struct {
UiHtml
Container
Tag any
mu deadlock.Mutex
contents []*Element
}

func (ui *uiWrapContainer) renderContainer(e *Element, w io.Writer, outerhtmltag string, params []any) (err error) {
ui.applyGetter(e, ui.Container)
ui.Tag = e.ApplyGetter(ui.Container)
attrs := e.ApplyParams(params)
b := e.Jid().AppendStartTagAttr(nil, outerhtmltag)
for _, attr := range attrs {
Expand Down
File renamed without changes.
134 changes: 67 additions & 67 deletions html_test.go → writehtml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,7 @@ import (
"github.com/linkdata/jaws/jid"
)

func TestHtmlInput(t *testing.T) {
type args struct {
jid jid.Jid
typ string
val string
attrs []template.HTMLAttr
}
tests := []struct {
name string
args args
want string
}{
{
name: "HtmlInput no attrs",
args: args{
jid: 1,
typ: "input_type",
val: "initial_val",
},
want: `<input id="Jid.1" type="input_type" value="initial_val">`,
},
{
name: "HtmlInput one empty attr",
args: args{
jid: 2,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{""},
},
want: `<input id="Jid.2" type="input_type2" value="initial_val2">`,
},
{
name: "HtmlInput one filled attr",
args: args{
jid: 3,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{"some_attr"},
},
want: `<input id="Jid.3" type="input_type2" value="initial_val2" some_attr>`,
},
{
name: "HtmlInput two filled attr, one empty",
args: args{
jid: 4,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{"some_attr1", "", "some_attr2"},
},
want: `<input id="Jid.4" type="input_type2" value="initial_val2" some_attr1 some_attr2>`,
},
}
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 {
t.Fatal(err)
}
if got := sb.String(); got != tt.want {
t.Errorf("HtmlInput() = %v, want %v", got, tt.want)
}
})
}
}

func TestHtmlInner(t *testing.T) {
func Test_WriteHtmlInner(t *testing.T) {
type args struct {
jid jid.Jid
tag string
Expand Down Expand Up @@ -131,7 +66,7 @@ func TestHtmlInner(t *testing.T) {
}
}

func TestHtmlSelect(t *testing.T) {
func Test_WriteHtmlSelect(t *testing.T) {
type args struct {
jid jid.Jid
val *NamedBoolArray
Expand Down Expand Up @@ -184,3 +119,68 @@ func TestHtmlSelect(t *testing.T) {
})
}
}

func Test_WriteHtmlInput(t *testing.T) {
type args struct {
jid jid.Jid
typ string
val string
attrs []template.HTMLAttr
}
tests := []struct {
name string
args args
want string
}{
{
name: "HtmlInput no attrs",
args: args{
jid: 1,
typ: "input_type",
val: "initial_val",
},
want: `<input id="Jid.1" type="input_type" value="initial_val">`,
},
{
name: "HtmlInput one empty attr",
args: args{
jid: 2,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{""},
},
want: `<input id="Jid.2" type="input_type2" value="initial_val2">`,
},
{
name: "HtmlInput one filled attr",
args: args{
jid: 3,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{"some_attr"},
},
want: `<input id="Jid.3" type="input_type2" value="initial_val2" some_attr>`,
},
{
name: "HtmlInput two filled attr, one empty",
args: args{
jid: 4,
typ: "input_type2",
val: "initial_val2",
attrs: []template.HTMLAttr{"some_attr1", "", "some_attr2"},
},
want: `<input id="Jid.4" type="input_type2" value="initial_val2" some_attr1 some_attr2>`,
},
}
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 {
t.Fatal(err)
}
if got := sb.String(); got != tt.want {
t.Errorf("HtmlInput() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 6e718e5

Please sign in to comment.