Skip to content

Commit 8e83d1a

Browse files
committed
test(tag): handle nil tags in slices and getters
1 parent c6b7bd2 commit 8e83d1a

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

element_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (a testApplyGetterAll) JawsInit(elem *Element) error {
8484
return a.initErr
8585
}
8686

87+
type testNilTagGetter struct{}
88+
89+
func (testNilTagGetter) JawsGetTag(tag.Context) any { return nil }
90+
8791
func TestElement_helpers(t *testing.T) {
8892
is := newTestHelper(t)
8993
rq := newTestRequest(t)
@@ -501,6 +505,31 @@ func TestElement_ApplyGetter_NonComparableHandler(t *testing.T) {
501505
}
502506
}
503507

508+
func TestElement_ApplyGetter_NilTagGetter(t *testing.T) {
509+
rq := newTestRequest(t)
510+
defer rq.Close()
511+
512+
e := rq.NewElement(&testUi{s: "foo"})
513+
gotTag, attrs, err := e.ApplyGetter(testNilTagGetter{})
514+
if gotTag != nil {
515+
t.Fatalf("expected nil tag, got %#v", gotTag)
516+
}
517+
if len(attrs) != 0 {
518+
t.Fatalf("expected no attrs, got %#v", attrs)
519+
}
520+
if err != nil {
521+
t.Fatalf("ApplyGetter returned error: %v", err)
522+
}
523+
if got := rq.TagsOf(e); len(got) != 0 {
524+
t.Fatalf("expected nil tag getter to not tag element, got %v", got)
525+
}
526+
527+
e.Tag(nil)
528+
if got := rq.TagsOf(e); len(got) != 0 {
529+
t.Fatalf("expected explicit nil tag to be ignored, got %v", got)
530+
}
531+
}
532+
504533
func TestElement_ApplyParams_NonComparableHandler(t *testing.T) {
505534
rq := newTestRequest(t)
506535
defer rq.Close()

lib/tag/tag.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ func addActiveTags(result []any, active []any) ([]any, error) {
102102
return result, nil
103103
}
104104

105+
func hasNonNilTag(tags []any) bool {
106+
for _, tag := range tags {
107+
if tag != nil {
108+
return true
109+
}
110+
}
111+
return false
112+
}
113+
105114
func expand(depth int, ctx Context, tag any, result []any, active []any) ([]any, error) {
106115
if depth > 10 || len(result) > 100 {
107116
return result, ErrTooManyTags
@@ -128,6 +137,9 @@ func expand(depth int, ctx Context, tag any, result []any, active []any) ([]any,
128137
}
129138
return expand(depth+1, ctx, data.JawsGetTag(ctx), result, append(active, data))
130139
case []any:
140+
if !hasNonNilTag(data) {
141+
return result, nil
142+
}
131143
if idx := findActiveIndex(active, data); idx >= 0 {
132144
return addActiveTags(result, active[idx:])
133145
}

lib/tag/tag_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func TestTagExpand(t *testing.T) {
132132
tag: []any{Tag("a"), &av},
133133
want: []any{Tag("a"), &av},
134134
},
135+
{
136+
name: "[]any nils",
137+
tag: []any{nil, nil},
138+
want: nil,
139+
},
135140
{
136141
name: "error",
137142
tag: boom,

0 commit comments

Comments
 (0)