Пример #1
0
func TestReplaceSpliceOnRootNode(t *testing.T) {
	defer func() {
		if err := recover(); err == nil {
			t.Error("TestReplaceSpliceOnRootNode didn't panic")
		}
	}()
	tree, _ := h5.NewFromString("<div id=\"foo\">foo<span>bar</span></div><")
	doc := tree.Top()
	ns, _ := h5.NewFromString("<span>foo</span>")
	f := Replace(ns.Top())
	f(doc)
	assertEqual(t, h5.Data(doc.FirstChild), "span")
	assertEqual(t, h5.Data(doc.FirstChild.FirstChild), "foo")
}
Пример #2
0
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>")
}
Пример #3
0
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>")
}
Пример #4
0
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))
}
Пример #5
0
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>")
}
Пример #6
0
// 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()
	}
}
Пример #7
0
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>")

}
Пример #8
0
func TestNewTransformer(t *testing.T) {
	tree, _ := h5.NewFromString("<html><body><div id=\"foo\"></div></body></html>")
	tf := New(tree)
	// hacky way of comparing an uncomparable type
	assertEqual(t, tf.Doc().Type, tree.Top().Type)
}