func TestReplaceChildren(t *testing.T) {
	node := h5.Anchor("", "foo")
	assertEqual(t, h5.NewTree(node).String(), "<a>foo</a>")
	child := h5.Text("baz ")
	child2 := h5.Text("quux")
	ReplaceChildren(child, child2)(node)
	assertEqual(t, h5.NewTree(node).String(), "<a>baz quux</a>")
}
Example #2
0
// Trace is a debugging wrapper for transform funcs.
// It calls traceFunc with debugging information before and after the
// TransformFunc is applied.
func Trace(f TransformFunc, traceFunc func(msg string, args ...interface{}), msg string, args ...interface{}) TransformFunc {
	return func(n *html.Node) {
		traceFunc(msg, args...)
		p := n.Parent
		if p == nil {
			p = n
		}
		traceFunc("Before: %s", h5.NewTree(p).String())
		f(n)
		traceFunc("After: %s", h5.NewTree(p).String())
	}
}
func TestAppendChildren(t *testing.T) {
	node := h5.Anchor("", "")
	child := h5.Text("foo ")
	child2 := h5.Text("bar")
	AppendChildren(child, child2)(node)
	assertEqual(t, h5.NewTree(node).String(), "<a>foo bar</a>")
}
func TestCopyAnd(t *testing.T) {
	defer func() {
		if err := recover(); err != nil {
			t.Errorf("TestCopyAnd paniced %s", err)
		}
	}()
	node := h5.Div("", nil, h5.Div("", nil, h5.Text("foo")))
	assertEqual(t, h5.NewTree(node).String(),
		"<div><div>foo</div></div>")
	CopyAnd(
		AppendChildren(h5.Text("bar")),
		ReplaceChildren(h5.Text("baz")),
	)(node.FirstChild)
	assertEqual(t, h5.NewTree(node).String(),
		"<div><div>foobar</div><div>baz</div></div>")
}
func TestReplace(t *testing.T) {
	defer func() {
		if err := recover(); err != nil {
			t.Error("TestReplace paniced")
		}
	}()
	node := h5.Div("", nil, h5.Div("", nil, h5.Text("foo")))
	replacement := h5.Div("", nil, h5.Text("bar"))
	Replace(replacement)(node.FirstChild)
	assertEqual(t, h5.NewTree(node).String(),
		"<div><div>bar</div></div>")
}
func TestReplaceSplice(t *testing.T) {
	defer func() {
		if err := recover(); err != nil {
			t.Error("TestReplaceSplice paniced")
		}
	}()
	node := h5.Div("foo", nil,
		h5.Text("foo"),
		h5.Element("span", nil, h5.Text("bar")),
	)
	node2 := h5.Element("span", nil, h5.Text("foo"))
	Replace(node2)(node.FirstChild)
	assertEqual(t, h5.NewTree(node).String(),
		"<div id=\"foo\"><span>foo</span><span>bar</span></div>")
}
Example #7
0
func nodeToString(n *html.Node) string {
	t := h5.NewTree(n)
	return t.String()
}
func TestRemoveChildren(t *testing.T) {
	node := h5.Anchor("", "foo")
	RemoveChildren()(node)
	assertEqual(t, h5.NewTree(node).String(), "<a></a>")
}