From 6e718e5580e8976f3913cdd92068400c5302941f Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Mon, 16 Dec 2024 08:01:38 +0100 Subject: [PATCH] remove UiHtml, refactor --- uihtml_test.go => jawsevent_test.go | 1 - uihtml.go | 9 -- uihtmlinner.go | 3 +- uiimg.go | 3 +- uiinput.go | 6 +- uiselect.go | 2 +- uiwrapcontainer.go | 4 +- html.go => writehtml.go | 0 html_test.go => writehtml_test.go | 134 ++++++++++++++-------------- 9 files changed, 77 insertions(+), 85 deletions(-) rename uihtml_test.go => jawsevent_test.go (99%) delete mode 100644 uihtml.go rename html.go => writehtml.go (100%) rename html_test.go => writehtml_test.go (96%) diff --git a/uihtml_test.go b/jawsevent_test.go similarity index 99% rename from uihtml_test.go rename to jawsevent_test.go index 995a6ef..b5b28a0 100644 --- a/uihtml_test.go +++ b/jawsevent_test.go @@ -10,7 +10,6 @@ import ( ) type testJawsEvent struct { - UiHtml msgCh chan string tag any clickerr error diff --git a/uihtml.go b/uihtml.go deleted file mode 100644 index b180a07..0000000 --- a/uihtml.go +++ /dev/null @@ -1,9 +0,0 @@ -package jaws - -type UiHtml struct { - Tag any -} - -func (ui *UiHtml) applyGetter(e *Element, getter any) { - ui.Tag = e.ApplyGetter(getter) -} diff --git a/uihtmlinner.go b/uihtmlinner.go index 17ce0c7..609889b 100644 --- a/uihtmlinner.go +++ b/uihtmlinner.go @@ -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)...) } diff --git a/uiimg.go b/uiimg.go index 4e015a2..f6100c1 100644 --- a/uiimg.go +++ b/uiimg.go @@ -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...) diff --git a/uiinput.go b/uiinput.go index f9d5f9d..757ada7 100644 --- a/uiinput.go +++ b/uiinput.go @@ -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 { diff --git a/uiselect.go b/uiselect.go index c725209..1ca3251 100644 --- a/uiselect.go +++ b/uiselect.go @@ -12,7 +12,7 @@ type UiSelect struct { func NewUiSelect(sh SelectHandler) *UiSelect { return &UiSelect{ - uiWrapContainer{ + uiWrapContainer: uiWrapContainer{ Container: sh, }, } diff --git a/uiwrapcontainer.go b/uiwrapcontainer.go index 1c6c87f..aff19d7 100644 --- a/uiwrapcontainer.go +++ b/uiwrapcontainer.go @@ -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 { diff --git a/html.go b/writehtml.go similarity index 100% rename from html.go rename to writehtml.go diff --git a/html_test.go b/writehtml_test.go similarity index 96% rename from html_test.go rename to writehtml_test.go index 67862b9..d86c910 100644 --- a/html_test.go +++ b/writehtml_test.go @@ -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: ``, - }, - { - name: "HtmlInput one empty attr", - args: args{ - jid: 2, - typ: "input_type2", - val: "initial_val2", - attrs: []template.HTMLAttr{""}, - }, - want: ``, - }, - { - name: "HtmlInput one filled attr", - args: args{ - jid: 3, - typ: "input_type2", - val: "initial_val2", - attrs: []template.HTMLAttr{"some_attr"}, - }, - want: ``, - }, - { - 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: ``, - }, - } - 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 @@ -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 @@ -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: ``, + }, + { + name: "HtmlInput one empty attr", + args: args{ + jid: 2, + typ: "input_type2", + val: "initial_val2", + attrs: []template.HTMLAttr{""}, + }, + want: ``, + }, + { + name: "HtmlInput one filled attr", + args: args{ + jid: 3, + typ: "input_type2", + val: "initial_val2", + attrs: []template.HTMLAttr{"some_attr"}, + }, + want: ``, + }, + { + 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: ``, + }, + } + 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) + } + }) + } +}