コード例 #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
ファイル: zdf.go プロジェクト: ronbu/websync
func grabParse(u url.URL) (t h5.Tree, err error) {
	c, err := grabHttp(u.String())
	if err != nil {
		return
	}
	tp, err := h5.NewFromString(c)
	if err != nil {
		return
	}
	t = *tp
	return
}
コード例 #8
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>")

}
コード例 #9
0
ファイル: PjaxFilter.go プロジェクト: happypancake/go-pjax
func rewriteBody(containerSelector string, dest io.Writer, body string) (err error) {
	if containerSelector == "" {
		dest.Write([]byte(body))
		return
	}

	var chain *selector.Chain
	var document *h5.Tree

	if document, err = h5.NewFromString(body); err != nil {
		err = fmt.Errorf("invalid html document: %v", err)
		return
	}

	var titleNode string
	if titleNode, err = getTitleNode(document); err != nil {
		return
	}

	if chain, err = selector.Selector(containerSelector); err != nil {
		err = fmt.Errorf("invalid css: %v", containerSelector)
		return
	}

	if matches := chain.Find(document.Top()); len(matches) > 0 {
		match := matches[0:1] // Take only the first match
		newBody := h5.RenderNodesToString(h5.Children(match[0]))

		fmt.Printf("data: %v", h5.Data(match[0]))

		dest.Write([]byte(titleNode))
		dest.Write([]byte(newBody))
		return
	}

	err = fmt.Errorf("container not found")
	return
}
コード例 #10
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)
}