Пример #1
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))
}
Пример #2
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")
}
Пример #3
0
// 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
}
Пример #4
0
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
}