How to use Filter method of html Package

Best K6 code snippet using html.Filter

traversal.go

Source:traversal.go Github

copy

Full Screen

...50// containing these elements.51func (s *Selection) Contents() *Selection {52 return pushStack(s, getChildrenNodes(s.Nodes, siblingAllIncludingNonElements))53}54// ContentsFiltered gets the children of each element in the Selection,55// filtered by the specified selector. It returns a new Selection56// object containing these elements. Since selectors only act on Element nodes,57// this function is an alias to ChildrenFiltered unless the selector is empty,58// in which case it is an alias to Contents.59func (s *Selection) ContentsFiltered(selector string) *Selection {60 if selector != "" {61 return s.ChildrenFiltered(selector)62 }63 return s.Contents()64}65// ContentsMatcher gets the children of each element in the Selection,66// filtered by the specified matcher. It returns a new Selection67// object containing these elements. Since matchers only act on Element nodes,68// this function is an alias to ChildrenMatcher.69func (s *Selection) ContentsMatcher(m Matcher) *Selection {70 return s.ChildrenMatcher(m)71}72// Children gets the child elements of each element in the Selection.73// It returns a new Selection object containing these elements.74func (s *Selection) Children() *Selection {75 return pushStack(s, getChildrenNodes(s.Nodes, siblingAll))76}77// ChildrenFiltered gets the child elements of each element in the Selection,78// filtered by the specified selector. It returns a new79// Selection object containing these elements.80func (s *Selection) ChildrenFiltered(selector string) *Selection {81 return filterAndPush(s, getChildrenNodes(s.Nodes, siblingAll), compileMatcher(selector))82}83// ChildrenMatcher gets the child elements of each element in the Selection,84// filtered by the specified matcher. It returns a new85// Selection object containing these elements.86func (s *Selection) ChildrenMatcher(m Matcher) *Selection {87 return filterAndPush(s, getChildrenNodes(s.Nodes, siblingAll), m)88}89// Parent gets the parent of each element in the Selection. It returns a90// new Selection object containing the matched elements.91func (s *Selection) Parent() *Selection {92 return pushStack(s, getParentNodes(s.Nodes))93}94// ParentFiltered gets the parent of each element in the Selection filtered by a95// selector. It returns a new Selection object containing the matched elements.96func (s *Selection) ParentFiltered(selector string) *Selection {97 return filterAndPush(s, getParentNodes(s.Nodes), compileMatcher(selector))98}99// ParentMatcher gets the parent of each element in the Selection filtered by a100// matcher. It returns a new Selection object containing the matched elements.101func (s *Selection) ParentMatcher(m Matcher) *Selection {102 return filterAndPush(s, getParentNodes(s.Nodes), m)103}104// Closest gets the first element that matches the selector by testing the105// element itself and traversing up through its ancestors in the DOM tree.106func (s *Selection) Closest(selector string) *Selection {107 cs := compileMatcher(selector)108 return s.ClosestMatcher(cs)109}110// ClosestMatcher gets the first element that matches the matcher by testing the111// element itself and traversing up through its ancestors in the DOM tree.112func (s *Selection) ClosestMatcher(m Matcher) *Selection {113 return pushStack(s, mapNodes(s.Nodes, func(i int, n *html.Node) []*html.Node {114 // For each node in the selection, test the node itself, then each parent115 // until a match is found.116 for ; n != nil; n = n.Parent {117 if m.Match(n) {118 return []*html.Node{n}119 }120 }121 return nil122 }))123}124// ClosestNodes gets the first element that matches one of the nodes by testing the125// element itself and traversing up through its ancestors in the DOM tree.126func (s *Selection) ClosestNodes(nodes ...*html.Node) *Selection {127 set := make(map[*html.Node]bool)128 for _, n := range nodes {129 set[n] = true130 }131 return pushStack(s, mapNodes(s.Nodes, func(i int, n *html.Node) []*html.Node {132 // For each node in the selection, test the node itself, then each parent133 // until a match is found.134 for ; n != nil; n = n.Parent {135 if set[n] {136 return []*html.Node{n}137 }138 }139 return nil140 }))141}142// ClosestSelection gets the first element that matches one of the nodes in the143// Selection by testing the element itself and traversing up through its ancestors144// in the DOM tree.145func (s *Selection) ClosestSelection(sel *Selection) *Selection {146 if sel == nil {147 return pushStack(s, nil)148 }149 return s.ClosestNodes(sel.Nodes...)150}151// Parents gets the ancestors of each element in the current Selection. It152// returns a new Selection object with the matched elements.153func (s *Selection) Parents() *Selection {154 return pushStack(s, getParentsNodes(s.Nodes, nil, nil))155}156// ParentsFiltered gets the ancestors of each element in the current157// Selection. It returns a new Selection object with the matched elements.158func (s *Selection) ParentsFiltered(selector string) *Selection {159 return filterAndPush(s, getParentsNodes(s.Nodes, nil, nil), compileMatcher(selector))160}161// ParentsMatcher gets the ancestors of each element in the current162// Selection. It returns a new Selection object with the matched elements.163func (s *Selection) ParentsMatcher(m Matcher) *Selection {164 return filterAndPush(s, getParentsNodes(s.Nodes, nil, nil), m)165}166// ParentsUntil gets the ancestors of each element in the Selection, up to but167// not including the element matched by the selector. It returns a new Selection168// object containing the matched elements.169func (s *Selection) ParentsUntil(selector string) *Selection {170 return pushStack(s, getParentsNodes(s.Nodes, compileMatcher(selector), nil))171}172// ParentsUntilMatcher gets the ancestors of each element in the Selection, up to but173// not including the element matched by the matcher. It returns a new Selection174// object containing the matched elements.175func (s *Selection) ParentsUntilMatcher(m Matcher) *Selection {176 return pushStack(s, getParentsNodes(s.Nodes, m, nil))177}178// ParentsUntilSelection gets the ancestors of each element in the Selection,179// up to but not including the elements in the specified Selection. It returns a180// new Selection object containing the matched elements.181func (s *Selection) ParentsUntilSelection(sel *Selection) *Selection {182 if sel == nil {183 return s.Parents()184 }185 return s.ParentsUntilNodes(sel.Nodes...)186}187// ParentsUntilNodes gets the ancestors of each element in the Selection,188// up to but not including the specified nodes. It returns a189// new Selection object containing the matched elements.190func (s *Selection) ParentsUntilNodes(nodes ...*html.Node) *Selection {191 return pushStack(s, getParentsNodes(s.Nodes, nil, nodes))192}193// ParentsFilteredUntil is like ParentsUntil, with the option to filter the194// results based on a selector string. It returns a new Selection195// object containing the matched elements.196func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) *Selection {197 return filterAndPush(s, getParentsNodes(s.Nodes, compileMatcher(untilSelector), nil), compileMatcher(filterSelector))198}199// ParentsFilteredUntilMatcher is like ParentsUntilMatcher, with the option to filter the200// results based on a matcher. It returns a new Selection object containing the matched elements.201func (s *Selection) ParentsFilteredUntilMatcher(filter, until Matcher) *Selection {202 return filterAndPush(s, getParentsNodes(s.Nodes, until, nil), filter)203}204// ParentsFilteredUntilSelection is like ParentsUntilSelection, with the205// option to filter the results based on a selector string. It returns a new206// Selection object containing the matched elements.207func (s *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {208 return s.ParentsMatcherUntilSelection(compileMatcher(filterSelector), sel)209}210// ParentsMatcherUntilSelection is like ParentsUntilSelection, with the211// option to filter the results based on a matcher. It returns a new212// Selection object containing the matched elements.213func (s *Selection) ParentsMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {214 if sel == nil {215 return s.ParentsMatcher(filter)216 }217 return s.ParentsMatcherUntilNodes(filter, sel.Nodes...)218}219// ParentsFilteredUntilNodes is like ParentsUntilNodes, with the220// option to filter the results based on a selector string. It returns a new221// Selection object containing the matched elements.222func (s *Selection) ParentsFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {223 return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), compileMatcher(filterSelector))224}225// ParentsMatcherUntilNodes is like ParentsUntilNodes, with the226// option to filter the results based on a matcher. It returns a new227// Selection object containing the matched elements.228func (s *Selection) ParentsMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {229 return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), filter)230}231// Siblings gets the siblings of each element in the Selection. It returns232// a new Selection object containing the matched elements.233func (s *Selection) Siblings() *Selection {234 return pushStack(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil))235}236// SiblingsFiltered gets the siblings of each element in the Selection237// filtered by a selector. It returns a new Selection object containing the238// matched elements.239func (s *Selection) SiblingsFiltered(selector string) *Selection {240 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil), compileMatcher(selector))241}242// SiblingsMatcher gets the siblings of each element in the Selection243// filtered by a matcher. It returns a new Selection object containing the244// matched elements.245func (s *Selection) SiblingsMatcher(m Matcher) *Selection {246 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingAll, nil, nil), m)247}248// Next gets the immediately following sibling of each element in the249// Selection. It returns a new Selection object containing the matched elements.250func (s *Selection) Next() *Selection {251 return pushStack(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil))252}253// NextFiltered gets the immediately following sibling of each element in the254// Selection filtered by a selector. It returns a new Selection object255// containing the matched elements.256func (s *Selection) NextFiltered(selector string) *Selection {257 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil), compileMatcher(selector))258}259// NextMatcher gets the immediately following sibling of each element in the260// Selection filtered by a matcher. It returns a new Selection object261// containing the matched elements.262func (s *Selection) NextMatcher(m Matcher) *Selection {263 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNext, nil, nil), m)264}265// NextAll gets all the following siblings of each element in the266// Selection. It returns a new Selection object containing the matched elements.267func (s *Selection) NextAll() *Selection {268 return pushStack(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil))269}270// NextAllFiltered gets all the following siblings of each element in the271// Selection filtered by a selector. It returns a new Selection object272// containing the matched elements.273func (s *Selection) NextAllFiltered(selector string) *Selection {274 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil), compileMatcher(selector))275}276// NextAllMatcher gets all the following siblings of each element in the277// Selection filtered by a matcher. It returns a new Selection object278// containing the matched elements.279func (s *Selection) NextAllMatcher(m Matcher) *Selection {280 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextAll, nil, nil), m)281}282// Prev gets the immediately preceding sibling of each element in the283// Selection. It returns a new Selection object containing the matched elements.284func (s *Selection) Prev() *Selection {285 return pushStack(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil))286}287// PrevFiltered gets the immediately preceding sibling of each element in the288// Selection filtered by a selector. It returns a new Selection object289// containing the matched elements.290func (s *Selection) PrevFiltered(selector string) *Selection {291 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil), compileMatcher(selector))292}293// PrevMatcher gets the immediately preceding sibling of each element in the294// Selection filtered by a matcher. It returns a new Selection object295// containing the matched elements.296func (s *Selection) PrevMatcher(m Matcher) *Selection {297 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrev, nil, nil), m)298}299// PrevAll gets all the preceding siblings of each element in the300// Selection. It returns a new Selection object containing the matched elements.301func (s *Selection) PrevAll() *Selection {302 return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil))303}304// PrevAllFiltered gets all the preceding siblings of each element in the305// Selection filtered by a selector. It returns a new Selection object306// containing the matched elements.307func (s *Selection) PrevAllFiltered(selector string) *Selection {308 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil), compileMatcher(selector))309}310// PrevAllMatcher gets all the preceding siblings of each element in the311// Selection filtered by a matcher. It returns a new Selection object312// containing the matched elements.313func (s *Selection) PrevAllMatcher(m Matcher) *Selection {314 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevAll, nil, nil), m)315}316// NextUntil gets all following siblings of each element up to but not317// including the element matched by the selector. It returns a new Selection318// object containing the matched elements.319func (s *Selection) NextUntil(selector string) *Selection {320 return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,321 compileMatcher(selector), nil))322}323// NextUntilMatcher gets all following siblings of each element up to but not324// including the element matched by the matcher. It returns a new Selection325// object containing the matched elements.326func (s *Selection) NextUntilMatcher(m Matcher) *Selection {327 return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,328 m, nil))329}330// NextUntilSelection gets all following siblings of each element up to but not331// including the element matched by the Selection. It returns a new Selection332// object containing the matched elements.333func (s *Selection) NextUntilSelection(sel *Selection) *Selection {334 if sel == nil {335 return s.NextAll()336 }337 return s.NextUntilNodes(sel.Nodes...)338}339// NextUntilNodes gets all following siblings of each element up to but not340// including the element matched by the nodes. It returns a new Selection341// object containing the matched elements.342func (s *Selection) NextUntilNodes(nodes ...*html.Node) *Selection {343 return pushStack(s, getSiblingNodes(s.Nodes, siblingNextUntil,344 nil, nodes))345}346// PrevUntil gets all preceding siblings of each element up to but not347// including the element matched by the selector. It returns a new Selection348// object containing the matched elements.349func (s *Selection) PrevUntil(selector string) *Selection {350 return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,351 compileMatcher(selector), nil))352}353// PrevUntilMatcher gets all preceding siblings of each element up to but not354// including the element matched by the matcher. It returns a new Selection355// object containing the matched elements.356func (s *Selection) PrevUntilMatcher(m Matcher) *Selection {357 return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,358 m, nil))359}360// PrevUntilSelection gets all preceding siblings of each element up to but not361// including the element matched by the Selection. It returns a new Selection362// object containing the matched elements.363func (s *Selection) PrevUntilSelection(sel *Selection) *Selection {364 if sel == nil {365 return s.PrevAll()366 }367 return s.PrevUntilNodes(sel.Nodes...)368}369// PrevUntilNodes gets all preceding siblings of each element up to but not370// including the element matched by the nodes. It returns a new Selection371// object containing the matched elements.372func (s *Selection) PrevUntilNodes(nodes ...*html.Node) *Selection {373 return pushStack(s, getSiblingNodes(s.Nodes, siblingPrevUntil,374 nil, nodes))375}376// NextFilteredUntil is like NextUntil, with the option to filter377// the results based on a selector string.378// It returns a new Selection object containing the matched elements.379func (s *Selection) NextFilteredUntil(filterSelector, untilSelector string) *Selection {380 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,381 compileMatcher(untilSelector), nil), compileMatcher(filterSelector))382}383// NextFilteredUntilMatcher is like NextUntilMatcher, with the option to filter384// the results based on a matcher.385// It returns a new Selection object containing the matched elements.386func (s *Selection) NextFilteredUntilMatcher(filter, until Matcher) *Selection {387 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,388 until, nil), filter)389}390// NextFilteredUntilSelection is like NextUntilSelection, with the391// option to filter the results based on a selector string. It returns a new392// Selection object containing the matched elements.393func (s *Selection) NextFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {394 return s.NextMatcherUntilSelection(compileMatcher(filterSelector), sel)395}396// NextMatcherUntilSelection is like NextUntilSelection, with the397// option to filter the results based on a matcher. It returns a new398// Selection object containing the matched elements.399func (s *Selection) NextMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {400 if sel == nil {401 return s.NextMatcher(filter)402 }403 return s.NextMatcherUntilNodes(filter, sel.Nodes...)404}405// NextFilteredUntilNodes is like NextUntilNodes, with the406// option to filter the results based on a selector string. It returns a new407// Selection object containing the matched elements.408func (s *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {409 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,410 nil, nodes), compileMatcher(filterSelector))411}412// NextMatcherUntilNodes is like NextUntilNodes, with the413// option to filter the results based on a matcher. It returns a new414// Selection object containing the matched elements.415func (s *Selection) NextMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {416 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,417 nil, nodes), filter)418}419// PrevFilteredUntil is like PrevUntil, with the option to filter420// the results based on a selector string.421// It returns a new Selection object containing the matched elements.422func (s *Selection) PrevFilteredUntil(filterSelector, untilSelector string) *Selection {423 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,424 compileMatcher(untilSelector), nil), compileMatcher(filterSelector))425}426// PrevFilteredUntilMatcher is like PrevUntilMatcher, with the option to filter427// the results based on a matcher.428// It returns a new Selection object containing the matched elements.429func (s *Selection) PrevFilteredUntilMatcher(filter, until Matcher) *Selection {430 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,431 until, nil), filter)432}433// PrevFilteredUntilSelection is like PrevUntilSelection, with the434// option to filter the results based on a selector string. It returns a new435// Selection object containing the matched elements.436func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {437 return s.PrevMatcherUntilSelection(compileMatcher(filterSelector), sel)438}439// PrevMatcherUntilSelection is like PrevUntilSelection, with the440// option to filter the results based on a matcher. It returns a new441// Selection object containing the matched elements.442func (s *Selection) PrevMatcherUntilSelection(filter Matcher, sel *Selection) *Selection {443 if sel == nil {444 return s.PrevMatcher(filter)445 }446 return s.PrevMatcherUntilNodes(filter, sel.Nodes...)447}448// PrevFilteredUntilNodes is like PrevUntilNodes, with the449// option to filter the results based on a selector string. It returns a new450// Selection object containing the matched elements.451func (s *Selection) PrevFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {452 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,453 nil, nodes), compileMatcher(filterSelector))454}455// PrevMatcherUntilNodes is like PrevUntilNodes, with the456// option to filter the results based on a matcher. It returns a new457// Selection object containing the matched elements.458func (s *Selection) PrevMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection {459 return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,460 nil, nodes), filter)461}462// Filter and push filters the nodes based on a matcher, and pushes the results463// on the stack, with the srcSel as previous selection.464func filterAndPush(srcSel *Selection, nodes []*html.Node, m Matcher) *Selection {465 // Create a temporary Selection with the specified nodes to filter using winnow466 sel := &Selection{nodes, srcSel.document, nil}467 // Filter based on matcher and push on stack468 return pushStack(srcSel, winnow(sel, m, true))469}470// Internal implementation of Find that return raw nodes.471func findWithMatcher(nodes []*html.Node, m Matcher) []*html.Node {472 // Map nodes to find the matches within the children of each node473 return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {474 // Go down one level, becausejQuery's Find selects only within descendants475 for c := n.FirstChild; c != nil; c = c.NextSibling {476 if c.Type == html.ElementNode {477 result = append(result, m.MatchAll(c)...)478 }479 }480 return481 })...

Full Screen

Full Screen

filter.go

Source:filter.go Github

copy

Full Screen

1package goquery2import "golang.org/x/net/html"3// Filter reduces the set of matched elements to those that match the selector string.4// It returns a new Selection object for this subset of matching elements.5func (s *Selection) Filter(selector string) *Selection {6 return s.FilterMatcher(compileMatcher(selector))7}8// FilterMatcher reduces the set of matched elements to those that match9// the given matcher. It returns a new Selection object for this subset10// of matching elements.11func (s *Selection) FilterMatcher(m Matcher) *Selection {12 return pushStack(s, winnow(s, m, true))13}14// Not removes elements from the Selection that match the selector string.15// It returns a new Selection object with the matching elements removed.16func (s *Selection) Not(selector string) *Selection {17 return s.NotMatcher(compileMatcher(selector))18}19// NotMatcher removes elements from the Selection that match the given matcher.20// It returns a new Selection object with the matching elements removed.21func (s *Selection) NotMatcher(m Matcher) *Selection {22 return pushStack(s, winnow(s, m, false))23}24// FilterFunction reduces the set of matched elements to those that pass the function's test.25// It returns a new Selection object for this subset of elements.26func (s *Selection) FilterFunction(f func(int, *Selection) bool) *Selection {27 return pushStack(s, winnowFunction(s, f, true))28}29// NotFunction removes elements from the Selection that pass the function's test.30// It returns a new Selection object with the matching elements removed.31func (s *Selection) NotFunction(f func(int, *Selection) bool) *Selection {32 return pushStack(s, winnowFunction(s, f, false))33}34// FilterNodes reduces the set of matched elements to those that match the specified nodes.35// It returns a new Selection object for this subset of elements.36func (s *Selection) FilterNodes(nodes ...*html.Node) *Selection {37 return pushStack(s, winnowNodes(s, nodes, true))38}39// NotNodes removes elements from the Selection that match the specified nodes.40// It returns a new Selection object with the matching elements removed.41func (s *Selection) NotNodes(nodes ...*html.Node) *Selection {42 return pushStack(s, winnowNodes(s, nodes, false))43}44// FilterSelection reduces the set of matched elements to those that match a45// node in the specified Selection object.46// It returns a new Selection object for this subset of elements.47func (s *Selection) FilterSelection(sel *Selection) *Selection {48 if sel == nil {49 return pushStack(s, winnowNodes(s, nil, true))50 }51 return pushStack(s, winnowNodes(s, sel.Nodes, true))52}53// NotSelection removes elements from the Selection that match a node in the specified54// Selection object. It returns a new Selection object with the matching elements removed.55func (s *Selection) NotSelection(sel *Selection) *Selection {56 if sel == nil {57 return pushStack(s, winnowNodes(s, nil, false))58 }59 return pushStack(s, winnowNodes(s, sel.Nodes, false))60}61// Intersection is an alias for FilterSelection.62func (s *Selection) Intersection(sel *Selection) *Selection {63 return s.FilterSelection(sel)64}65// Has reduces the set of matched elements to those that have a descendant66// that matches the selector.67// It returns a new Selection object with the matching elements.68func (s *Selection) Has(selector string) *Selection {69 return s.HasSelection(s.document.Find(selector))70}71// HasMatcher reduces the set of matched elements to those that have a descendant72// that matches the matcher.73// It returns a new Selection object with the matching elements.74func (s *Selection) HasMatcher(m Matcher) *Selection {75 return s.HasSelection(s.document.FindMatcher(m))76}77// HasNodes reduces the set of matched elements to those that have a78// descendant that matches one of the nodes.79// It returns a new Selection object with the matching elements.80func (s *Selection) HasNodes(nodes ...*html.Node) *Selection {81 return s.FilterFunction(func(_ int, sel *Selection) bool {82 // Add all nodes that contain one of the specified nodes83 for _, n := range nodes {84 if sel.Contains(n) {85 return true86 }87 }88 return false89 })90}91// HasSelection reduces the set of matched elements to those that have a92// descendant that matches one of the nodes of the specified Selection object.93// It returns a new Selection object with the matching elements.94func (s *Selection) HasSelection(sel *Selection) *Selection {95 if sel == nil {96 return s.HasNodes()97 }98 return s.HasNodes(sel.Nodes...)99}100// End ends the most recent filtering operation in the current chain and101// returns the set of matched elements to its previous state.102func (s *Selection) End() *Selection {103 if s.prevSel != nil {104 return s.prevSel105 }106 return newEmptySelection(s.document)107}108// Filter based on the matcher, and the indicator to keep (Filter) or109// to get rid of (Not) the matching elements.110func winnow(sel *Selection, m Matcher, keep bool) []*html.Node {111 // Optimize if keep is requested112 if keep {113 return m.Filter(sel.Nodes)114 }115 // Use grep116 return grep(sel, func(i int, s *Selection) bool {117 return !m.Match(s.Get(0))118 })119}120// Filter based on an array of nodes, and the indicator to keep (Filter) or121// to get rid of (Not) the matching elements.122func winnowNodes(sel *Selection, nodes []*html.Node, keep bool) []*html.Node {123 if len(nodes)+len(sel.Nodes) < minNodesForSet {124 return grep(sel, func(i int, s *Selection) bool {125 return isInSlice(nodes, s.Get(0)) == keep126 })127 }128 set := make(map[*html.Node]bool)129 for _, n := range nodes {130 set[n] = true131 }132 return grep(sel, func(i int, s *Selection) bool {133 return set[s.Get(0)] == keep134 })135}136// Filter based on a function test, and the indicator to keep (Filter) or137// to get rid of (Not) the matching elements.138func winnowFunction(sel *Selection, f func(int, *Selection) bool, keep bool) []*html.Node {139 return grep(sel, func(i int, s *Selection) bool {140 return f(i, s) == keep141 })142}...

Full Screen

Full Screen

char_filter.go

Source:char_filter.go Github

copy

Full Screen

2import (3 "fmt"4)5const (6 CharFilterNameHtmlStrip CharFilterName = "html_strip"7 CharFilterTypeHtmlStrip CharFilterType = "html_strip"8 CharFilterTypeMapping CharFilterType = "mapping"9 CharFilterTypePatternReplace CharFilterType = "pattern_replace"10)11type CharFilterType string12type CharFilterName string13type CharFilter interface {14 Type() CharFilterType15 Name() CharFilterName16 Source() (interface{}, error)17}18// see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-htmlstrip-charfilter.html19type CharFilterHtmlStrip struct {20 name CharFilterName21 escapedTags []string22}23func NewCharFilterHtmlStrip(name string) *CharFilterHtmlStrip {24 return &CharFilterHtmlStrip{name: CharFilterName(name)}25}26func (filter *CharFilterHtmlStrip) AddEscapedTags(escapedTags ...string) *CharFilterHtmlStrip {27 filter.escapedTags = append(filter.escapedTags, escapedTags...)28 return filter29}30func (filter *CharFilterHtmlStrip) Type() CharFilterType {31 return CharFilterTypeHtmlStrip32}33func (filter *CharFilterHtmlStrip) Name() CharFilterName {34 return filter.name35}36func (filter *CharFilterHtmlStrip) Source() (interface{}, error) {37 source := map[string]interface{}{38 "type": filter.Type(),39 }40 if len(filter.escapedTags) > 0 {41 source["escaped_tags"] = filter.escapedTags42 }43 return source, nil44}45// see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-mapping-charfilter.html46type CharFilterMapping struct {47 name CharFilterName48 mappingsPath *string49 mappings []*CharMapping50}51func NewCharFilterMapping(name string) *CharFilterMapping {52 return &CharFilterMapping{name: CharFilterName(name)}53}54func (filter *CharFilterMapping) SetMappingsPath(mappingsPath string) *CharFilterMapping {55 filter.mappingsPath = &mappingsPath56 return filter57}58func (filter *CharFilterMapping) AddMappings(mappings ...*CharMapping) *CharFilterMapping {59 filter.mappings = append(filter.mappings, mappings...)60 return filter61}62func (filter *CharFilterMapping) Type() CharFilterType {63 return CharFilterTypeMapping64}65func (filter *CharFilterMapping) Name() CharFilterName {66 return filter.name67}68func (filter *CharFilterMapping) Source() (interface{}, error) {69 source := map[string]interface{}{70 "type": filter.Type(),71 }72 if filter.mappingsPath != nil {73 source["mappings_path"] = *filter.mappingsPath74 }75 if len(filter.mappings) > 0 {76 var mappings []string77 for _, charMapping := range filter.mappings {78 mappings = append(mappings, charMapping.String())79 }80 source["mappings"] = mappings81 }82 return source, nil83}84type CharMapping struct {85 from string86 to string87}88func NewCharMapping(from string, to string) *CharMapping {89 return &CharMapping{from: from, to: to}90}91func (charMapping *CharMapping) String() string {92 return fmt.Sprintf("%s => %s", charMapping.from, charMapping.to)93}94// see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html95type CharFilterPatternReplace struct {96 name CharFilterName97 pattern *string98 replacement *string99 flags []JavaRegularFlag100}101func NewCharFilterPatternReplace(name string) *CharFilterPatternReplace {102 return &CharFilterPatternReplace{name: CharFilterName(name)}103}104func (filter *CharFilterPatternReplace) SetPattern(pattern string) *CharFilterPatternReplace {105 filter.pattern = &pattern106 return filter107}108func (filter *CharFilterPatternReplace) SetReplacement(replacement string) *CharFilterPatternReplace {109 filter.replacement = &replacement110 return filter111}112func (filter *CharFilterPatternReplace) AddFlags(flags ...JavaRegularFlag) *CharFilterPatternReplace {113 filter.flags = append(filter.flags, flags...)114 return filter115}116func (filter *CharFilterPatternReplace) Type() CharFilterType {117 return CharFilterTypePatternReplace118}119func (filter *CharFilterPatternReplace) Name() CharFilterName {120 return filter.name121}122func (filter *CharFilterPatternReplace) Source() (interface{}, error) {123 source := map[string]interface{}{124 "type": filter.Type(),125 }126 if filter.pattern != nil {127 source["pattern"] = *filter.pattern128 }129 if filter.replacement != nil {130 source["replacement"] = *filter.replacement131 }132 if len(filter.flags) > 0 {133 flags := ""134 for _, flag := range filter.flags {135 flags += string(flag) + JavaRegularFlagSeparator136 }...

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks2: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = visit(links, c)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks3: %v52 os.Exit(1)53 }54 for _, link := range visit(nil, doc) {55 fmt.Println(link)56 }57}58func visit(links []string, n *html.Node) []string {

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks2: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = visit(links, c)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks3: %v52 os.Exit(1)53 }54 for _, link := range visit(nil, doc) {55 fmt.Println(link)56 }57}58func visit(links

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks1: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = visit(links, c)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks1: %v52 os.Exit(1)53 }54 for _, link := range visit(nil, doc) {55 fmt.Println(link)56 }57}58func visit(links

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 doc, err := html.Parse(resp.Body)7 resp.Body.Close()8 if err != nil {9 log.Fatal(err)10 }11 for _, link := range visit(nil, doc) {12 fmt.Println(link)13 }14}15func visit(links []string, n *html.Node) []string {16 if n.Type == html.ElementNode && n.Data == "a" {17 for _, a := range n.Attr {18 if a.Key == "href" {19 links = append(links, a.Val)20 }21 }22 }23 for c := n.FirstChild; c != nil; c = c.NextSibling {24 links = visit(links, c)25 }26}27import (28func main() {29 if err != nil {30 log.Fatal(err)31 }32 doc, err := html.Parse(resp.Body)33 resp.Body.Close()34 if err != nil {35 log.Fatal(err)36 }37 for _, link := range visit(nil, doc) {38 fmt.Println(link)39 }40}41func visit(links []string, n *html.Node) []string {42 if n.Type == html.ElementNode && n.Data == "a" {43 for _, a := range n.Attr {44 if a.Key == "href" {45 links = append(links, a.Val)46 }47 }48 }49 for c := n.FirstChild; c != nil; c = c.NextSibling {50 links = visit(links, c)51 }52}53import (54func main() {55 if err != nil {56 log.Fatal(err)57 }58 doc, err := html.Parse(resp.Body)

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("test.html")4 if err != nil {5 fmt.Println(err)6 }7 doc, err := html.Parse(f)8 if err != nil {9 fmt.Println(err)10 }11 links := Filter(doc, func(n *html.Node) bool {12 if n.Type == html.ElementNode && n.Data == "a" {13 for _, a := range n.Attr {14 if a.Key == "href" {15 }16 }17 }18 })19 for _, link := range links {20 fmt.Println(link)21 }22}23func Filter(n *html.Node, pre func(n *html.Node) bool) []*html.Node {24 if pre(n) {25 nodes = append(nodes, n)26 }27 for c := n.FirstChild; c != nil; c = c.NextSibling {28 nodes = append(nodes, Filter(c, pre)...)29 }30}31import (32func main() {33 f, err := os.Open("test.html")34 if err != nil {35 fmt.Println(err)36 }37 doc, err := html.Parse(f)38 if err != nil {39 fmt.Println(err)40 }41 links := html.Filter(doc, func(n *html.Node) bool {42 if n.Type == html.ElementNode && n.Data == "a" {43 for _, a := range n.Attr {44 if a.Key == "href" {45 }46 }47 }48 })49 for _, link := range links {50 fmt.Println(link)51 }52}

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer resp.Body.Close()7 doc, err := html.Parse(resp.Body)8 if err != nil {9 log.Fatal(err)10 }

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 z := html.NewTokenizer(resp.Body)7 for {8 tt := z.Next()9 switch {10 t := z.Token()11 if isAnchor {12 for _, a := range t.Attr {13 if a.Key == "href" {14 fmt.Printf("Link: %s15 }16 }17 }18 }19 }20}

Full Screen

Full Screen

Filter

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 resp, err := http.Get(url)4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 defer resp.Body.Close()9 data, err := html.Parse(resp.Body)10 if err != nil {11 fmt.Println(err)12 os.Exit(1)13 }14 filterData(data)15}16func filterData(data *html.Node) {17 if data.Type == html.ElementNode {18 fmt.Println(data.Data)19 }20 for c := data.FirstChild; c != nil; c = c.NextSibling {21 filterData(c)22 }23}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful