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 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>") }
func TestTransformApplyAll(t *testing.T) { tree, _ := h5.NewFromString("<html><head></head><body><ul><li>foo</ul></body></html>") tf := New(tree) n := h5.Text("bar") n2 := h5.Text("quux") t1, _ := Trans(AppendChildren(n), "body li") t2, _ := Trans(AppendChildren(n2), "body li") tf.ApplyAll(t1, t2) assertEqual(t, tf.String(), "<html><head></head><body><ul><li>foobarquux</li></ul></body></html>") }
func TestDoAll(t *testing.T) { tree, _ := h5.NewFromString("<div id=\"foo\">foo</div><") node := tree.Top() preNode := h5.Text("pre node") postNode := h5.Text("post node") f := DoAll(AppendChildren(postNode), PrependChildren(preNode)) f(node) assertEqual(t, h5.Data(node.FirstChild), h5.Data(preNode)) assertEqual(t, h5.Data(node.LastChild), h5.Data(postNode)) }
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>") }
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 TestTransformApply(t *testing.T) { tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>") tf := New(tree) n := h5.Text("bar") tf.Apply(AppendChildren(n), "body") newDoc := tf.String() assertEqual(t, newDoc, "<html><head></head><body><div id=\"foo\"></div>bar</body></html>") }
func TestTransformSubtransforms(t *testing.T) { defer func() { if err := recover(); err != nil { t.Errorf("TestTransformSubtransforms paniced %s", err) } }() tree, _ := h5.NewFromString("<html><body><ul><li>foo</ul></body></html>") f, _ := Subtransform(CopyAnd( ReplaceChildren(h5.Text("bar")), ReplaceChildren(h5.Text("baz"), h5.Text("quux")), ), "li") tf := New(tree) t1, _ := Trans(f, "ul") tf.ApplyAll(t1) assertEqual(t, tf.String(), "<html><head></head><body><ul><li>bar</li><li>bazquux</li></ul></body></html>") }
func TestTransformApplyMulti(t *testing.T) { tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>") tf := New(tree) tf.Apply(AppendChildren(h5.Text("")), "body") tf.Apply(TransformAttrib("id", func(val string) string { t.Logf("Rewriting Url") return "bar" }), "div") newDoc := tf.String() assertEqual(t, newDoc, "<html><head></head><body><div id=\"bar\"></div></body></html>") }
// TODO(jwall): benchmarking tests func BenchmarkTransformApply(b *testing.B) { for i := 0; i < b.N; i++ { tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html") tf := New(tree) tf.Apply(AppendChildren(h5.Text("")), "body") tf.Apply(TransformAttrib("id", func(val string) string { return "bar" }), "div") tf.Doc() } }