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>")
}
示例#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())
	}
}
// Trace is a debugging wrapper for transform funcs.
// It prints debugging information before and after the TransformFunc
// is applied.
func Trace(f TransformFunc, msg string, args ...interface{}) TransformFunc {
	return func(n *html.Node) {
		log.Printf("TRACE: "+msg, args...)
		p := n.Parent
		if p == nil {
			p = n
		}
		log.Printf("TRACE: Before: %s", h5.NewTree(p).String())
		f(n)
		log.Printf("TRACE: 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>")
}
示例#6
0
文件: zdf.go 项目: ronbu/websync
func treeFind(t h5.Tree, e, key string, f func(h5.Tree, html.Attribute)) {
	t.Walk(func(n *html.Node) {
		if a := getAttr(n, key); n.Data == e && a != nil {
			f(h5.NewTree(n), *a)
		}
	})

}
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>")
}
示例#9
0
func parseGetChild(node *html.Node, childType atom.Atom, count int) (*html.Node, error) {
	if node.FirstChild == nil {
		return nil, errors.New("No children")
	}
	if node.LastChild == node.FirstChild {
		if node.FirstChild.DataAtom != childType {
			return parseGetChild(node.FirstChild, childType, count)
		}
	}
	child := node.FirstChild
	for {
		if child.DataAtom == childType {
			count = count - 1
			if count == 0 {
				return child, nil
			}
		}
		if child == node.LastChild {
			return nil, errors.New("Atom not found " + childType.String() + " in " + h5.NewTree(node).String())
		}
		child = child.NextSibling
	}
	return nil, errors.New("Atom not found " + childType.String() + " in " + h5.NewTree(node).String())
}
func TestRemoveChildren(t *testing.T) {
	node := h5.Anchor("", "foo")
	RemoveChildren()(node)
	assertEqual(t, h5.NewTree(node).String(), "<a></a>")
}
示例#11
0
func nodeToString(n *html.Node) string {
	t := h5.NewTree(n)
	return t.String()
}
示例#12
0
func parseExecutors(rdr io.Reader) ([]Build, error) {
	tree, err := h5.New(rdr)
	if err != nil {
		return nil, err
	}
	body, err := parseGetChild(tree.Top(), atom.Body, 1)
	if err != nil {
		return nil, err
	}
	table, err := parseGetChild(body, atom.Table, 1)
	if err != nil {
		return nil, err
	}
	tbody, err := parseGetChild(table, atom.Tbody, 2)
	if err != nil {
		return nil, err
	}
	tr := tbody.FirstChild
	var builds []Build
	for {
		th, err := parseGetChild(tr, atom.Th, 1)
		if err == nil {
			nameLink, err := parseGetChild(th, atom.A, 1)
			if err != nil {
				//fmt.Println("link not found in " + h5.NewTree(th).String())
				if tr == tbody.LastChild {
					break
				}
				tr = tr.NextSibling
				continue
			}
			if tr.NextSibling == nil {
				builds = append(builds, Build{nameLink.FirstChild.Data, ""})
				break
			}
			tr = tr.NextSibling
			_, err = parseGetChild(tr, atom.Th, 1)
			if err == nil {
				// no data row
				builds = append(builds, Build{nameLink.FirstChild.Data, ""})
				continue
			}
			if tr.FirstChild == nil || tr.FirstChild.NextSibling == nil {
				return nil, errors.New("Build without div")
			}
			buildTd := tr.FirstChild.NextSibling
			if buildTd.DataAtom != atom.Td {
				return nil, errors.New("Expected td but got " + h5.NewTree(buildTd).String())
			}
			buildDiv, err := parseGetChild(buildTd, atom.Div, 1)
			if err != nil {
				// empty data row
				builds = append(builds, Build{nameLink.FirstChild.Data, ""})
			} else {
				build, err := parseGetChild(buildDiv, atom.A, 1)
				if err != nil {
					return nil, err
				}
				builds = append(builds, Build{nameLink.FirstChild.Data, build.FirstChild.Data})
			}
		}
		if tr == tbody.LastChild {
			break
		}
		tr = tr.NextSibling
	}

	return builds, nil
}
示例#13
0
func parsePrint(n *html.Node) {
	fmt.Println(h5.NewTree(n).String())
}