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 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") }
// Match returns true if this SimpleSelector matches this node false otherwise. func (ss SimpleSelector) Match(n *html.Node) bool { if n == nil { return false } if ss.Type == Tag { return strings.ToLower(ss.Tag) == strings.ToLower(h5.Data(n)) } if ss.Type == PseudoClass { switch ss.Value { case "root": return n.Parent == nil case "first-child": return n.Parent != nil && n.Parent.FirstChild == n case "last-child": return n.Parent != nil && n.Parent.LastChild == n case "only-child": return n.PrevSibling == nil && n.NextSibling == nil case "empty": return n.FirstChild == nil default: // TODO(jwall): panic(fmt.Errorf("Can't match with PseudoClass %s", ss.Value)) } } else if ss.Type == PseudoElement { panic(fmt.Errorf("Can't match with PseudoElement %s", ss.Value)) } for _, a := range n.Attr { switch ss.Type { case Id: if strings.ToLower(a.Key) == "id" { return a.Val == ss.Value } case Class: if strings.ToLower(a.Key) == "class" { return attrContains(ss.Value, &a) } case Attr: if strings.ToLower(a.Key) == strings.ToLower(ss.AttrName) { switch ss.AttrMatch { case Exactly: return attrExactly(ss.Value, &a) case Contains: return attrContains(ss.Value, &a) case DashPrefix: return attrDashPrefix(ss.Value, &a) } return true } } } return false }