Merge pull request #718 from trheyi/main
Refactor slot cloning logic in build.go
This commit is contained in:
commit
5640fd4398
2 changed files with 30 additions and 22 deletions
|
|
@ -305,10 +305,11 @@ func (page *Page) parseImports(doc *goquery.Document) {
|
||||||
|
|
||||||
// Copy the slots
|
// Copy the slots
|
||||||
slots := selection.Find("slot").Clone()
|
slots := selection.Find("slot").Clone()
|
||||||
for i = 0; i < slots.Length(); i++ {
|
for j := 0; j < slots.Length(); j++ {
|
||||||
slot := slots.Eq(i)
|
slot := slots.Eq(j)
|
||||||
slotName, has := slot.Attr("name")
|
slotName, has := slot.Attr("name")
|
||||||
if !has {
|
if !has {
|
||||||
|
imp.slots[slotName] = slot
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if impSlot, has := imp.slots[slotName]; has {
|
if impSlot, has := imp.slots[slotName]; has {
|
||||||
|
|
@ -799,10 +800,17 @@ func (page *Page) BuildHTML(option *BuildOption) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setError(sel *goquery.Selection, err 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>`
|
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()))
|
errSel.SetHtml(fmt.Sprintf(html, err.Error()))
|
||||||
if sel.Nodes != nil || len(sel.Nodes) > 0 {
|
if errSel.Nodes != nil || len(errSel.Nodes) > 0 {
|
||||||
sel.Nodes[0].Data = "Error"
|
errSel.Nodes[0].Data = "Error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func TestTemplateBuildAsComponent(t *testing.T) {
|
||||||
tests := prepare(t)
|
tests := prepare(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|
||||||
tmpl, err := tests.Web.GetTemplate("default")
|
tmpl, err := tests.Test.GetTemplate("advanced")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetTemplate error: %v", err)
|
t.Fatalf("GetTemplate error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -76,15 +76,15 @@ func TestTemplateBuildAsComponent(t *testing.T) {
|
||||||
t.Fatalf("Components error: %v", err)
|
t.Fatalf("Components error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cselect := "/flowbite/components/edit/select.jit"
|
block := "/i18n/block.jit"
|
||||||
cinput := "/flowbite/components/edit/input.jit"
|
bar := "/backend/bar.jit"
|
||||||
|
|
||||||
// Check JIT
|
// Check JIT
|
||||||
assert.FileExists(t, filepath.Join(path, cselect))
|
assert.FileExists(t, filepath.Join(path, block))
|
||||||
assert.FileExists(t, filepath.Join(path, cinput))
|
assert.FileExists(t, filepath.Join(path, bar))
|
||||||
assert.Len(t, warnings, 0)
|
assert.Len(t, warnings, 0)
|
||||||
|
|
||||||
content, err := os.ReadFile(filepath.Join(path, cselect))
|
content, err := os.ReadFile(filepath.Join(path, bar))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ReadFile error: %v", err)
|
t.Fatalf("ReadFile error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -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="scripts" type="json">`)
|
||||||
assert.Contains(t, string(content), `<script name="styles" 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), `<script name="option" type="json">`)
|
||||||
assert.Contains(t, string(content), "function Init()")
|
assert.Contains(t, string(content), "this.Constants")
|
||||||
assert.Contains(t, string(content), `type="flowbite-edit-select"`)
|
assert.Contains(t, string(content), `type="hook-bar"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPageBuild(t *testing.T) {
|
func TestPageBuild(t *testing.T) {
|
||||||
|
|
@ -150,7 +150,7 @@ func TestPageBuildAsComponent(t *testing.T) {
|
||||||
tests := prepare(t)
|
tests := prepare(t)
|
||||||
defer clean()
|
defer clean()
|
||||||
|
|
||||||
tmpl, err := tests.Web.GetTemplate("default")
|
tmpl, err := tests.Test.GetTemplate("advanced")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("GetTemplate error: %v", err)
|
t.Fatalf("GetTemplate error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +165,7 @@ func TestPageBuildAsComponent(t *testing.T) {
|
||||||
t.Fatalf("RemoveAll error: %v", err)
|
t.Fatalf("RemoveAll error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
page, err := tmpl.Page("/[form]")
|
page, err := tmpl.Page("/backend")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Page error: %v", err)
|
t.Fatalf("Page error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -176,14 +176,14 @@ func TestPageBuildAsComponent(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.Len(t, warnings, 0)
|
assert.Len(t, warnings, 0)
|
||||||
|
|
||||||
cselect := "/flowbite/components/edit/select.jit"
|
foo := "/backend/foo.jit"
|
||||||
cinput := "/flowbite/components/edit/input.jit"
|
bar := "/backend/bar.jit"
|
||||||
|
|
||||||
// Check JIT
|
// Check JIT
|
||||||
assert.FileExists(t, filepath.Join(path, cselect))
|
assert.FileExists(t, filepath.Join(path, foo))
|
||||||
assert.FileExists(t, filepath.Join(path, cinput))
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("ReadFile error: %v", err)
|
t.Fatalf("ReadFile error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -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="scripts" type="json">`)
|
||||||
assert.Contains(t, string(content), `<script name="styles" 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), `<script name="option" type="json">`)
|
||||||
assert.Contains(t, string(content), "function Init()")
|
assert.Contains(t, string(content), "this.Constants")
|
||||||
assert.Contains(t, string(content), `type="flowbite-edit-select"`)
|
assert.Contains(t, string(content), `type="hook-bar"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPageTrans(t *testing.T) {
|
func TestPageTrans(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue