本文整理匯總了Golang中code/google/com/p/cascadia.MustCompile函數的典型用法代碼示例。如果您正苦於以下問題:Golang MustCompile函數的具體用法?Golang MustCompile怎麽用?Golang MustCompile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了MustCompile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetFeedUrl
func GetFeedUrl(u string) (string, error) {
resp, err := http.Get(u)
if err != nil {
return "", err
}
if strings.Contains(resp.Header.Get("Content-Type"), "xml") {
return u, nil
}
tree, err := html.Parse(resp.Body)
if err != nil {
return "", err
}
sel := cascadia.MustCompile("link[rel=alternate][type*=xml]")
alt := sel.MatchFirst(tree)
if alt == nil {
return "", errors.New("no feed link found")
}
altUrl, found := FindAttr("href", alt.Attr)
if !found {
return "", errors.New("missing link in alternate")
}
return ToAbsolute(resp.Request.URL, altUrl.Val), nil
}
示例2: Is
// Is() checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (this *Selection) Is(selector string) bool {
if len(this.Nodes) > 0 {
// The selector must be done on the document if it has positional criteria
// TODO : Not sure it is required, as Cascadia's selector checks within the parent of the
// node when there is such a positionaly selector... In jQuery, this is for the
// non-css selectors (Sizzle-implemented selectors, an extension of CSS)
/*if ok, e := regexp.MatchString(rxNeedsContext, selector); ok {
sel := this.document.Root.Find(selector)
for _, n := range this.Nodes {
if sel.IndexOfNode(n) > -1 {
return true
}
}
} else if e != nil {
panic(e.Error())
} else {*/
// Attempt a match with the selector
cs := cascadia.MustCompile(selector)
if len(this.Nodes) == 1 {
return cs.Match(this.Nodes[0])
} else {
return len(cs.Filter(this.Nodes)) > 0
}
//}
}
return false
}
示例3: Is
// Is checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (s *Selection) Is(selector string) bool {
if len(s.Nodes) > 0 {
return s.IsMatcher(cascadia.MustCompile(selector))
}
return false
}
示例4: GetPageInfo
func GetPageInfo(u string) (*PageInfo, error) {
res, err := http.Get(u)
if err != nil {
return nil, err
}
defer res.Body.Close()
tree, err := html.Parse(res.Body)
if err != nil {
return nil, err
}
sel := cascadia.MustCompile("meta")
meta := sel.MatchAll(tree)
found, title := findTitle(tree)
if !found {
_, title = findProperty(meta, "title", "twitter:title")
}
_, description := findProperty(meta, "description", "og:description")
_, image := findProperty(meta, "og:image")
return &PageInfo{
Title: title,
Description: description,
Image: image,
RawURL: u,
}, nil
}
示例5: Is
// Is checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (s *Selection) Is(selector string) bool {
if len(s.Nodes) > 0 {
// Attempt a match with the selector
cs := cascadia.MustCompile(selector)
if len(s.Nodes) == 1 {
return cs.Match(s.Nodes[0])
}
return len(cs.Filter(s.Nodes)) > 0
}
return false
}
示例6: winnow
// Filter based on a selector string, and the indicator to keep (Filter) or
// to get rid of (Not) the matching elements.
func winnow(sel *Selection, selector string, keep bool) []*html.Node {
cs := cascadia.MustCompile(selector)
// Optimize if keep is requested
if keep {
return cs.Filter(sel.Nodes)
}
// Use grep
return grep(sel, func(i int, s *Selection) bool {
return !cs.Match(s.Get(0))
})
}
示例7: Closest
// Closest() gets the first element that matches the selector by testing the
// element itself and traversing up through its ancestors in the DOM tree.
func (this *Selection) Closest(selector string) *Selection {
cs := cascadia.MustCompile(selector)
return pushStack(this, mapNodes(this.Nodes, func(i int, n *html.Node) []*html.Node {
// For each node in the selection, test the node itself, then each parent
// until a match is found.
for ; n != nil; n = n.Parent {
if cs.Match(n) {
return []*html.Node{n}
}
}
return nil
}))
}
示例8: findWithSelector
// Internal implementation of Find that return raw nodes.
func findWithSelector(nodes []*html.Node, selector string) []*html.Node {
// Compile the selector once
sel := cascadia.MustCompile(selector)
// Map nodes to find the matches within the children of each node
return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
// Go down one level, becausejQuery's Find() selects only within descendants
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
result = append(result, sel.MatchAll(c)...)
}
}
return
})
}
示例9: findTitle
func findTitle(tree *html.Node) (found bool, title string) {
sel := cascadia.MustCompile("title")
node := sel.MatchFirst(tree)
if node == nil {
return false, ""
}
if node.Type == html.ElementNode {
node = node.FirstChild
}
buf := new(bytes.Buffer)
for node != nil {
if node.Type == html.TextNode {
buf.WriteString(node.Data)
}
node = node.NextSibling
}
return true, string(buf.Bytes())
}
示例10: GetFavicon
func GetFavicon(url string) (string, error) {
if favicon, err := GetCanonicalFavicon(url); err == nil {
fmt.Println("found favicon.ico")
return favicon, nil
} else if *debug {
fmt.Printf("Error: getting /favicon.ico: %s\n", err)
}
resp, err := http.Get(url)
if *debug {
fmt.Println("get html", resp, err)
}
if err != nil {
return "", err
}
defer resp.Body.Close()
tree, err := html.Parse(resp.Body)
if *debug {
fmt.Println("parse html", tree, err)
}
if err != nil {
return "", err
}
sel := cascadia.MustCompile("link[rel~=icon]")
node := sel.MatchFirst(tree)
if node == nil {
return "", errors.New("no favicon found")
}
favicon, found := FindAttr("href", node.Attr)
if !found {
return "", errors.New("no link found")
}
return ToAbsolute(resp.Request.URL, favicon.Val), nil
}
示例11: Closest
// Closest gets the first element that matches the selector by testing the
// element itself and traversing up through its ancestors in the DOM tree.
func (s *Selection) Closest(selector string) *Selection {
cs := cascadia.MustCompile(selector)
return s.ClosestMatcher(cs)
}
示例12: WrapInner
// WrapInner wraps an HTML structure, matched by the given selector, around the
// content of element in the set of matched elements. The matched child is
// cloned before being inserted into the document.
//
// It returns the original set of elements.
func (s *Selection) WrapInner(selector string) *Selection {
return s.WrapInnerMatcher(cascadia.MustCompile(selector))
}
示例13: Append
// Append appends the elements specified by the selector to the end of each element
// in the set of matched elements, following those rules:
//
// 1) The selector is applied to the root document.
//
// 2) Elements that are part of the document will be moved to the new location.
//
// 3) If there are multiple locations to append to, cloned nodes will be
// appended to all target locations except the last one, which will be moved
// as noted in (2).
func (s *Selection) Append(selector string) *Selection {
return s.AppendMatcher(cascadia.MustCompile(selector))
}
示例14: RemoveFiltered
// RemoveFiltered removes the set of matched elements by selector.
// It returns the Selection of removed nodes.
func (s *Selection) RemoveFiltered(selector string) *Selection {
return s.RemoveMatcher(cascadia.MustCompile(selector))
}
示例15: ReplaceWith
// ReplaceWith replaces each element in the set of matched elements with the
// nodes matched by the given selector.
// It returns the removed elements.
//
// This follows the same rules as Selection.Append.
func (s *Selection) ReplaceWith(selector string) *Selection {
return s.ReplaceWithMatcher(cascadia.MustCompile(selector))
}