diff --git a/h.go b/h.go index ed2168e..ebb90dd 100755 --- a/h.go +++ b/h.go @@ -1,12 +1,17 @@ package vn -func H(tagName string, attrs *Attrs, children interface{}) *Vnode { +func H(tagName string, attrs *Attrs, children interface{}, params ...string) *Vnode { sanitizedAttrs := Sanitize(attrs) + var key *string + + if len(params) == 1 { + key = ¶ms[0] + } switch (children).(type) { case string: - return NewVNode(tagName, sanitizedAttrs, nil, &TextNode{(children).(string), nil}, nil) + return NewVNode(tagName, sanitizedAttrs, nil, &TextNode{(children).(string), nil}, nil, key) default: - return NewVNode(tagName, sanitizedAttrs, children.(Children), nil, nil) + return NewVNode(tagName, sanitizedAttrs, children.(Children), nil, nil, key) } } diff --git a/mock/node.go b/mock/node.go index f37e4d5..2bee3ea 100644 --- a/mock/node.go +++ b/mock/node.go @@ -76,6 +76,20 @@ func (mr *MockNodeMockRecorder) GetAttrs() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAttrs", reflect.TypeOf((*MockNode)(nil).GetAttrs)) } +// GetKey mocks base method +func (m *MockNode) GetKey() *string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetKey") + ret0, _ := ret[0].(*string) + return ret0 +} + +// GetKey indicates an expected call of GetKey +func (mr *MockNodeMockRecorder) GetKey() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetKey", reflect.TypeOf((*MockNode)(nil).GetKey)) +} + // HasElement mocks base method func (m *MockNode) HasElement() bool { m.ctrl.T.Helper() diff --git a/node.go b/node.go index 24c95cc..6a03a2a 100755 --- a/node.go +++ b/node.go @@ -6,6 +6,7 @@ type Node interface { GetElement() *vnd.DomElement GetTagName() string GetAttrs() *Attrs + GetKey() *string HasElement() bool SetElement(vnd.DomElement) HashCode() string diff --git a/vnode.go b/vnode.go index 7e1530f..9f6c7e9 100755 --- a/vnode.go +++ b/vnode.go @@ -15,13 +15,22 @@ type Vnode struct { Children Children Text *TextNode Element *vnd.DomElement + key *string } -func NewVNode(tagName string, attrs *Attrs, children Children, text *TextNode, element *vnd.DomElement) *Vnode { - return &Vnode{tagName, attrs, children, text, element} +func NewVNode(tagName string, attrs *Attrs, children Children, text *TextNode, element *vnd.DomElement, key *string) *Vnode { + return &Vnode{tagName, attrs, children, text, element, key} } func (vnode *Vnode) IsSame(other Node) bool { + currKey := vnode.GetKey() + otherKey := other.GetKey() + + if currKey != nil && otherKey != nil { + fmt.Println("They are the same => ", *currKey) + return *currKey == *otherKey + } + if vnh.IsNil(vnode.Text) { if vnh.IsNil(other.GetText()) { return vnode.HashCode() == other.HashCode() @@ -52,6 +61,10 @@ func (vnode *Vnode) ChildAt(index int) Node { return nil } +func (vnode *Vnode) GetKey() *string { + return vnode.key +} + func (vnode *Vnode) GetText() *TextNode { return vnode.Text } diff --git a/vnode_test.go b/vnode_test.go index 8f43ea9..afbf117 100755 --- a/vnode_test.go +++ b/vnode_test.go @@ -44,13 +44,21 @@ func TestVnode_IsSame_WithChildren(t *testing.T) { H("li", nil, "Second item"), }) - c := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{ + assert.Equal(t, true, a.IsSame(b)) +} + +func TestVnode_IsSame_WithKey(t *testing.T) { + a := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{ H("li", nil, "First item"), - H("li", nil, "Something else"), - }) + H("li", nil, "Second item"), + }, "1") - assert.Equal(t, true, a.IsSame(b)) - assert.Equal(t, false, a.IsSame(c)) + b := H("ul", &Attrs{Props: &Props{"class": "navbar"}}, Children{ + H("li", nil, "First item"), + H("li", nil, "Second item"), + }, "2") + + assert.Equal(t, false, a.IsSame(b)) } func TestVnode_ChildrenCount(t *testing.T) {