Exemple #1
0
// Render renders the generated markup for this view
func (v *View) Render(m ...string) trees.Markup {
	if len(m) <= 0 {
		m = []string{"."}
	}

	v.Engine().All(m[0])

	if v.rview == nil {
		return elems.Div()
	}

	dom := v.rview.Render(m...)

	if dom == nil {
		return elems.Div()
	}

	// // swap the uid for the new dom
	// // to ensure we keep the sync between backend and frontend in sync.
	v.backdoor.M = dom
	v.backdoor.SwapUID(v.uid)
	v.backdoor.M = nil

	if v.liveMarkup != nil {
		dom.Reconcile(v.liveMarkup)
	}

	dom.UseEventManager(v.events)
	v.events.LoadUpEvents()
	v.liveMarkup = dom

	return dom
}
func (v videoList) Render(m ...string) trees.Markup {
	dom := elems.Div()
	for _, data := range v {
		dom.Augment(elems.Video(
			attrs.Src(data["src"]),
			elems.Text(data["name"]),
		))
	}
	return dom
}
func TestMarkupReconciliation(t *testing.T) {
	div := elems.Div(
		elems.Span(elems.Text("30")),
		elems.Text("20"),
	)

	divCl := elems.Div(
		elems.Span(elems.Text("30")),
		elems.Text("20"),
		elems.Text("400"),
	)

	//lets remove the span with its text child and the parents text child
	trees.ElementsWithTag(trees.ElementsWithTag(divCl, "span")[0], "text")[0].Remove()
	trees.ElementsWithTag(divCl, "text")[0].Remove()
	divCl.CleanRemoved()

	printer := trees.NewElementWriter(trees.SimpleAttrWriter, trees.SimpleStyleWriter, trees.SimpleTextWriter)
	printer.AllowRemoved()

	nrender := printer.Print(div)
	crender := printer.Print(divCl)

	if !strings.Contains(nrender, ">20") && !strings.Contains(nrender, ">30") {
		tests.FatalFailed(t, "Inaccurate rendering occured, has no '>20' or '>30' set", nrender)
	}

	if strings.Contains(crender, ">20") && strings.Contains(crender, ">30") {
		tests.FatalFailed(t, "Inaccurate rendering occured, has '>20' or '>30' and  set", crender)
	}

	//reconcile with the original div
	divCl.Reconcile(div)

	rcrender := printer.Print(divCl)

	if strings.Contains(rcrender, ">20") && strings.Contains(rcrender, ">30") && !strings.Contains(rcrender, ">400") {
		tests.FatalFailed(t, "Inaccurate rendering occured, has '>20' or '>30' and  set", rcrender)
	}

	tests.LogPassed(t, "Successfully reconciled dom markup!")
}
func BenchmarkElementPrinter(t *testing.B) {
	div := elems.Div()

	for i := 0; i < 10000; i++ {
		trees.NewText(fmt.Sprintf("%d", i)).Apply(div)
	}

	for i := 0; i < t.N; i++ {
		trees.SimpleElementWriter.Print(div)
	}
}
func TestMarkupRemoveRender(t *testing.T) {
	div := elems.Div(
		elems.Text("20"),
	)

	if len(div.Children()) <= 0 {
		tests.FatalFailed(t, "Inaccurate size of children, expected %d for %s", 1, len(div.Children()))
	}

	divCl := div.Clone().(*trees.Element)

	if len(divCl.Children()) <= 0 {
		tests.FatalFailed(t, "Inaccurate size of clone's children, expected %d for %s", 1, len(div.Children()))
	}

	printer := trees.NewElementWriter(trees.SimpleAttrWriter, trees.SimpleStyleWriter, trees.SimpleTextWriter)
	printer.AllowRemoved()

	if ds := printer.Print(div); len(ds) != len(normalRender) {
		tests.FatalFailed(t, "1 Renders produced unequal results between \n %s and \n %s", ds, normalRender)
	}

	trees.ElementsWithTag(divCl, "text")[0].Remove()

	if dl, dcl := len(div.Children()), len(divCl.Children()); dl != dcl {
		tests.FatalFailed(t, "Clone children size is inaccurate, expected %d but got %d", dl, dcl)
	}

	if ds := printer.Print(divCl); len(ds) != len(removedRender) {
		tests.FatalFailed(t, "2 Renders produced unequal results between \n %s and \n %s", ds, removedRender)
	}

	printer.DisallowRemoved()

	if ds := printer.Print(divCl); len(ds) != len(cleanRender) {
		tests.FatalFailed(t, "3 Renders produced unequal results between \n %s and \n %s", ds, cleanRender)
	}

	divCl.CleanRemoved()

	if dl, dcl := len(div.Children()), len(divCl.Children()); dl == dcl {
		tests.FatalFailed(t, "Clone children size is inaccurate, expected %d but got %d", dl, dcl)
	}

	tests.LogPassed(t, "Successfully asserted proper markup operation with .Remove()!")
}
func TestElementPrinter(t *testing.T) {
	elem := trees.NewElement("bench", false)
	attrs.ClassName("grid col1").Apply(elem)
	elems.Div(trees.NewText("thunder")).Apply(elem)

	classes := &trees.ClassList{}
	classes.Add("x-icon")
	classes.Add("x-lock")

	classes.Apply(elem)

	res := trees.SimpleElementWriter.Print(elem)

	tests.Truthy(t, "Contains '<bench'", strings.Contains(res, "<bench"))
	tests.Truthy(t, "contains '</bench>'", strings.Contains(res, "</bench>"))
	tests.Truthy(t, "contains 'hash='", strings.Contains(res, "hash="))
	tests.Truthy(t, "contains 'uid='", strings.Contains(res, "uid="))
}
func TestMarkup(t *testing.T) {
	div := elems.Div(
		elems.Text("20"),
	)

	if len(div.Children()) <= 0 {
		tests.FatalFailed(t, "Inaccurate size of children, expected %d for %s", 1, len(div.Children()))
	}

	firstRender := trees.SimpleElementWriter.Print(div)
	secondRender := trees.SimpleElementWriter.Print(div)

	if firstRender != secondRender {
		tests.FatalFailed(t, "Renders produced unequal results between \n %s and \n %s", firstRender, secondRender)
	}

	tests.LogPassed(t, "Successfully asserted proper markup operation!")
}