Skip to content

Commit

Permalink
Merge pull request #718 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor slot cloning logic in build.go
  • Loading branch information
trheyi authored Jul 29, 2024
2 parents df1e793 + a91e47b commit 5640fd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
18 changes: 13 additions & 5 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,11 @@ func (page *Page) parseImports(doc *goquery.Document) {

// Copy the slots
slots := selection.Find("slot").Clone()
for i = 0; i < slots.Length(); i++ {
slot := slots.Eq(i)
for j := 0; j < slots.Length(); j++ {
slot := slots.Eq(j)
slotName, has := slot.Attr("name")
if !has {
imp.slots[slotName] = slot
continue
}
if impSlot, has := imp.slots[slotName]; has {
Expand Down Expand Up @@ -799,10 +800,17 @@ func (page *Page) BuildHTML(option *BuildOption) (string, error) {
}

func setError(sel *goquery.Selection, err error) {

errSel := sel
// If sel is input or textarea, set the error to the parent
if sel.Get(0).Data == "input" || sel.Get(0).Data == "textarea" {
errSel = sel.Parent()
}

html := `<div style="color:red; margin:10px 0px; font-size: 12px; font-family: monospace; padding: 10px; border: 1px solid red; background-color: #f8d7da;">%s</div>`
sel.SetHtml(fmt.Sprintf(html, err.Error()))
if sel.Nodes != nil || len(sel.Nodes) > 0 {
sel.Nodes[0].Data = "Error"
errSel.SetHtml(fmt.Sprintf(html, err.Error()))
if errSel.Nodes != nil || len(errSel.Nodes) > 0 {
errSel.Nodes[0].Data = "Error"
}
}

Expand Down
34 changes: 17 additions & 17 deletions sui/storages/local/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestTemplateBuildAsComponent(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Web.GetTemplate("default")
tmpl, err := tests.Test.GetTemplate("advanced")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}
Expand All @@ -76,15 +76,15 @@ func TestTemplateBuildAsComponent(t *testing.T) {
t.Fatalf("Components error: %v", err)
}

cselect := "/flowbite/components/edit/select.jit"
cinput := "/flowbite/components/edit/input.jit"
block := "/i18n/block.jit"
bar := "/backend/bar.jit"

// Check JIT
assert.FileExists(t, filepath.Join(path, cselect))
assert.FileExists(t, filepath.Join(path, cinput))
assert.FileExists(t, filepath.Join(path, block))
assert.FileExists(t, filepath.Join(path, bar))
assert.Len(t, warnings, 0)

content, err := os.ReadFile(filepath.Join(path, cselect))
content, err := os.ReadFile(filepath.Join(path, bar))
if err != nil {
t.Fatalf("ReadFile error: %v", err)
}
Expand All @@ -96,8 +96,8 @@ func TestTemplateBuildAsComponent(t *testing.T) {
assert.Contains(t, string(content), `<script name="scripts" type="json">`)
assert.Contains(t, string(content), `<script name="styles" type="json">`)
assert.Contains(t, string(content), `<script name="option" type="json">`)
assert.Contains(t, string(content), "function Init()")
assert.Contains(t, string(content), `type="flowbite-edit-select"`)
assert.Contains(t, string(content), "this.Constants")
assert.Contains(t, string(content), `type="hook-bar"`)
}

func TestPageBuild(t *testing.T) {
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestPageBuildAsComponent(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Web.GetTemplate("default")
tmpl, err := tests.Test.GetTemplate("advanced")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}
Expand All @@ -165,7 +165,7 @@ func TestPageBuildAsComponent(t *testing.T) {
t.Fatalf("RemoveAll error: %v", err)
}

page, err := tmpl.Page("/[form]")
page, err := tmpl.Page("/backend")
if err != nil {
t.Fatalf("Page error: %v", err)
}
Expand All @@ -176,14 +176,14 @@ func TestPageBuildAsComponent(t *testing.T) {
}
assert.Len(t, warnings, 0)

cselect := "/flowbite/components/edit/select.jit"
cinput := "/flowbite/components/edit/input.jit"
foo := "/backend/foo.jit"
bar := "/backend/bar.jit"

// Check JIT
assert.FileExists(t, filepath.Join(path, cselect))
assert.FileExists(t, filepath.Join(path, cinput))
assert.FileExists(t, filepath.Join(path, foo))
assert.FileExists(t, filepath.Join(path, bar))

content, err := os.ReadFile(filepath.Join(path, cselect))
content, err := os.ReadFile(filepath.Join(path, bar))
if err != nil {
t.Fatalf("ReadFile error: %v", err)
}
Expand All @@ -195,8 +195,8 @@ func TestPageBuildAsComponent(t *testing.T) {
assert.Contains(t, string(content), `<script name="scripts" type="json">`)
assert.Contains(t, string(content), `<script name="styles" type="json">`)
assert.Contains(t, string(content), `<script name="option" type="json">`)
assert.Contains(t, string(content), "function Init()")
assert.Contains(t, string(content), `type="flowbite-edit-select"`)
assert.Contains(t, string(content), "this.Constants")
assert.Contains(t, string(content), `type="hook-bar"`)
}

func TestPageTrans(t *testing.T) {
Expand Down

0 comments on commit 5640fd4

Please sign in to comment.