Skip to content

Commit

Permalink
node manager update raw html
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Oct 12, 2023
1 parent 763bd56 commit d1465da
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pkg/app/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func (m *nodeManager) mountRawHTML(depth uint, v *raw) (UI, error) {
return nil, errors.New("raw html is already mounted").
WithTag("parent-type", reflect.TypeOf(v.parent())).
WithTag("type", reflect.TypeOf(v)).
WithTag("depth", v.depth())
WithTag("depth", v.depth()).
WithTag("raw-preview", previewText(v.value))
}

wrapper, _ := Window().createElement("div", "")
Expand Down Expand Up @@ -675,7 +676,27 @@ func (m *nodeManager) updateComponent(v, new Composer) (UI, error) {
}

func (m *nodeManager) updateRawHTML(v, new *raw) (UI, error) {
panic("not implemented")
if v.value == new.value {
return v, nil
}

newMount, err := m.Mount(v.depth(), new)
if err != nil {
return nil, errors.New("mounting updated raw html failed").
WithTag("type", reflect.TypeOf(v)).
WithTag("depth", v.depth()).
WithTag("raw-preview", previewText(v.value)).
Wrap(err)
}

for parent := v.parent(); parent != nil; parent = parent.parent() {
if parent, isHTML := parent.(HTML); isHTML {
parent.JSValue().replaceChild(newMount, v)
break
}
}
m.Dismount(v)
return newMount, nil
}

// MakeContext creates and returns a new context derived from the nodeManager's
Expand Down
46 changes: 46 additions & 0 deletions pkg/app/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ func TestNodeManagerMount(t *testing.T) {
require.NotZero(t, span)
require.NotNil(t, span.(*raw).jsElement)
require.True(t, span.Mounted())
require.Equal(t, "<span>hello</span>", span.(*raw).value)
})

t.Run("mounting an already mounted raw html returns an error", func(t *testing.T) {
Expand Down Expand Up @@ -883,6 +884,51 @@ func TestNodeManagerUpdate(t *testing.T) {
require.Error(t, err)
require.Nil(t, newCompo)
})

t.Run("update raw html replaces its value", func(t *testing.T) {
var m nodeManager

body, err := m.Mount(1, Body().privateBody(
Raw("<span>hi</span>"),
))
require.NoError(t, err)
require.NotZero(t, body)
require.NotEmpty(t, body.(HTML).body())

span := body.(HTML).body()[0]
require.NotZero(t, span)
require.IsType(t, Raw(""), span)

newSpan, err := m.Update(span, Raw("<span>bye</span>"))
require.NoError(t, err)
require.NotEqual(t, span, newSpan)
require.IsType(t, Raw(""), newSpan)
require.Equal(t, "<span>bye</span>", newSpan.(*raw).value)
require.True(t, newSpan.Mounted())
require.False(t, span.Mounted())
})

t.Run("update raw html skips update", func(t *testing.T) {
var m nodeManager

body, err := m.Mount(1, Body().privateBody(
Raw("<span>hi</span>"),
))
require.NoError(t, err)
require.NotZero(t, body)
require.NotEmpty(t, body.(HTML).body())

span := body.(HTML).body()[0]
require.NotZero(t, span)
require.IsType(t, Raw(""), span)

newSpan, err := m.Update(span, Raw("<span>hi</span>"))
require.NoError(t, err)
require.Equal(t, span, newSpan)
require.IsType(t, Raw(""), newSpan)
require.Equal(t, "<span>hi</span>", newSpan.(*raw).value)
require.True(t, newSpan.Mounted())
})
}

func TestNodeManagerMakeContext(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type raw struct {
}

func (r *raw) JSValue() Value {
if r.jsElement != nil {
return r.jsElement
}
return r.jsvalue
}

Expand Down

0 comments on commit d1465da

Please sign in to comment.