Skip to content

Commit

Permalink
TextHasPrefix; ContainsText => TextContains; EqualText => TextEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Oct 5, 2021
1 parent 771d160 commit d48967b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 11 additions & 4 deletions hdq_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,27 @@ func (p NodeSet) ChildEqualText(text string) (ret NodeSet) {
})
}

// EqualText returns NodeSet which node type is TextNode and it's text equals `text`.
func (p NodeSet) EqualText(text string) (ret NodeSet) {
// TextEqual returns NodeSet which node type is TextNode and it's text equals `text`.
func (p NodeSet) TextEqual(text string) (ret NodeSet) {
return p.Match(func(node *html.Node) bool {
return equalText(node, text)
})
}

// ContainsText returns NodeSet which node type is TextNode and it's text contains `text`.
func (p NodeSet) ContainsText(text string) (ret NodeSet) {
// TextContains returns NodeSet which node type is TextNode and it's text contains `text`.
func (p NodeSet) TextContains(text string) (ret NodeSet) {
return p.Match(func(node *html.Node) bool {
return containsText(node, text)
})
}

// TextHasPrefix returns NodeSet which node type is TextNode and its prefix is `text`.
func (p NodeSet) TextHasPrefix(text string) (ret NodeSet) {
return p.Match(func(node *html.Node) bool {
return hasPrefixText(node, text)
})
}

func (p NodeSet) dataAtom(elem atom.Atom) (ret NodeSet) {
return p.Match(func(node *html.Node) bool {
return node.DataAtom == elem
Expand Down
8 changes: 8 additions & 0 deletions html_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func containsText(node *html.Node, text string) bool {
return strings.Contains(node.Data, text)
}

// hasPrefixText returns true if the type of node is TextNode and its Data has prefix `text`.
func hasPrefixText(node *html.Node, text string) bool {
if node.Type != html.TextNode {
return false
}
return strings.Contains(strings.TrimLeft(node.Data, " \t\r\n"), text)
}

// exactText returns text of node if the type of node is TextNode.
func exactText(node *html.Node) (string, error) {
if node.Type != html.TextNode {
Expand Down
2 changes: 1 addition & 1 deletion tutorial/02-GithubRepos/repos.gop
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func newRepo(node hdq.NodeSet) Repo {
aRepo := node.any.a.attr("itemprop", "name codeRepository").one
repo := aRepo.href!
root := aRepo.parentN(3).one
forkedFrom := root.any.span.any.containsText("Forked from").one.nextSibling(1).a.href?:""
forkedFrom := root.any.span.any.textContains("Forked from").one.nextSibling(1).a.href?:""
title := root.any.p.attr("itemprop", "description").text?:""
language := root.any.span.attr("itemprop", "programmingLanguage").one.text?:""
updateTime := root.any.element("relative-time").one.attr("datetime")?:""
Expand Down

0 comments on commit d48967b

Please sign in to comment.