func TestSelectorMatch(t *testing.T) { for _, spec := range matchers { chn, err := Selector(spec.s) if err != nil { t.Errorf("Error parsing selector %q", err) } if !chn.Head.Match(spec.n) { t.Errorf("spec %q didn't match %q when it should have", chn, h5.RenderNodesToString([]*html.Node{spec.n})) } if chn.Head.Match(spec.n2) { t.Errorf("spec %q matched %q when it shouldn't have", chn, h5.RenderNodesToString([]*html.Node{spec.n2})) } } }
func TestSelectorFind(t *testing.T) { for _, spec := range finders { chn, err := Selector(spec.s) if err != nil { t.Errorf("Error parsing selector %q", err) } ns := chn.Find(spec.n) if len(ns) < 1 { t.Errorf("%q didn't find any nodes in %q", chn, h5.RenderNodesToString([]*html.Node{spec.n})) } if h5.RenderNodesToString(ns) != h5.RenderNodesToString(spec.ns) { t.Errorf("Got: %q Expected: %q", h5.RenderNodesToString(ns), h5.RenderNodesToString(spec.ns)) } } }
// Replace constructs a TransformFunc that replaces a node with the nodes passed // in. func Replace(ns ...*html.Node) TransformFunc { return func(n *html.Node) { p := n.Parent switch p { case nil: panic(fmt.Sprintf("Attempt to replace Root node: %s", h5.RenderNodesToString([]*html.Node{n}))) default: for _, nc := range ns { p.InsertBefore(nc, n) } p.RemoveChild(n) } } }