How to use Each method of html Package

Best K6 code snippet using html.Each

traversal.go

Source:traversal.go Github

copy

Full Screen

1package goquery2import "golang.org/x/net/html"3type siblingType int4// Sibling type, used internally when iterating over children at the same5// level (siblings) to specify which nodes are requested.6const (7 siblingPrevUntil siblingType = iota - 38 siblingPrevAll9 siblingPrev10 siblingAll11 siblingNext12 siblingNextAll13 siblingNextUntil14 siblingAllIncludingNonElements15)16// Find gets the descendants of each element in the current set of matched17// elements, filtered by a selector. It returns a new Selection object18// containing these matched elements.19func (s *Selection) Find(selector string) *Selection {20 return pushStack(s, findWithMatcher(s.Nodes, compileMatcher(selector)))21}22// FindMatcher gets the descendants of each element in the current set of matched23// elements, filtered by the matcher. It returns a new Selection object24// containing these matched elements.25func (s *Selection) FindMatcher(m Matcher) *Selection {26 return pushStack(s, findWithMatcher(s.Nodes, m))27}28// FindSelection gets the descendants of each element in the current29// Selection, filtered by a Selection. It returns a new Selection object30// containing these matched elements.31func (s *Selection) FindSelection(sel *Selection) *Selection {32 if sel == nil {33 return pushStack(s, nil)34 }35 return s.FindNodes(sel.Nodes...)36}37// FindNodes gets the descendants of each element in the current38// Selection, filtered by some nodes. It returns a new Selection object39// containing these matched elements.40func (s *Selection) FindNodes(nodes ...*html.Node) *Selection {41 return pushStack(s, mapNodes(nodes, func(i int, n *html.Node) []*html.Node {42 if sliceContains(s.Nodes, n) {43 return []*html.Node{n}44 }45 return nil46 }))47}48// Contents gets the children of each element in the Selection,49// including text and comment nodes. It returns a new Selection object50// 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 })482}483// Internal implementation to get all parent nodes, stopping at the specified484// node (or nil if no stop).485func getParentsNodes(nodes []*html.Node, stopm Matcher, stopNodes []*html.Node) []*html.Node {486 return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {487 for p := n.Parent; p != nil; p = p.Parent {488 sel := newSingleSelection(p, nil)489 if stopm != nil {490 if sel.IsMatcher(stopm) {491 break492 }493 } else if len(stopNodes) > 0 {494 if sel.IsNodes(stopNodes...) {495 break496 }497 }498 if p.Type == html.ElementNode {499 result = append(result, p)500 }501 }502 return503 })504}505// Internal implementation of sibling nodes that return a raw slice of matches.506func getSiblingNodes(nodes []*html.Node, st siblingType, untilm Matcher, untilNodes []*html.Node) []*html.Node {507 var f func(*html.Node) bool508 // If the requested siblings are ...Until, create the test function to509 // determine if the until condition is reached (returns true if it is)510 if st == siblingNextUntil || st == siblingPrevUntil {511 f = func(n *html.Node) bool {512 if untilm != nil {513 // Matcher-based condition514 sel := newSingleSelection(n, nil)515 return sel.IsMatcher(untilm)516 } else if len(untilNodes) > 0 {517 // Nodes-based condition518 sel := newSingleSelection(n, nil)519 return sel.IsNodes(untilNodes...)520 }521 return false522 }523 }524 return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {525 return getChildrenWithSiblingType(n.Parent, st, n, f)526 })527}528// Gets the children nodes of each node in the specified slice of nodes,529// based on the sibling type request.530func getChildrenNodes(nodes []*html.Node, st siblingType) []*html.Node {531 return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {532 return getChildrenWithSiblingType(n, st, nil, nil)533 })534}535// Gets the children of the specified parent, based on the requested sibling536// type, skipping a specified node if required.537func getChildrenWithSiblingType(parent *html.Node, st siblingType, skipNode *html.Node,538 untilFunc func(*html.Node) bool) (result []*html.Node) {539 // Create the iterator function540 var iter = func(cur *html.Node) (ret *html.Node) {541 // Based on the sibling type requested, iterate the right way542 for {543 switch st {544 case siblingAll, siblingAllIncludingNonElements:545 if cur == nil {546 // First iteration, start with first child of parent547 // Skip node if required548 if ret = parent.FirstChild; ret == skipNode && skipNode != nil {549 ret = skipNode.NextSibling550 }551 } else {552 // Skip node if required553 if ret = cur.NextSibling; ret == skipNode && skipNode != nil {554 ret = skipNode.NextSibling555 }556 }557 case siblingPrev, siblingPrevAll, siblingPrevUntil:558 if cur == nil {559 // Start with previous sibling of the skip node560 ret = skipNode.PrevSibling561 } else {562 ret = cur.PrevSibling563 }564 case siblingNext, siblingNextAll, siblingNextUntil:565 if cur == nil {566 // Start with next sibling of the skip node567 ret = skipNode.NextSibling568 } else {569 ret = cur.NextSibling570 }571 default:572 panic("Invalid sibling type.")573 }574 if ret == nil || ret.Type == html.ElementNode || st == siblingAllIncludingNonElements {575 return576 }577 // Not a valid node, try again from this one578 cur = ret579 }580 }581 for c := iter(nil); c != nil; c = iter(c) {582 // If this is an ...Until case, test before append (returns true583 // if the until condition is reached)584 if st == siblingNextUntil || st == siblingPrevUntil {585 if untilFunc(c) {586 return587 }588 }589 result = append(result, c)590 if st == siblingNext || st == siblingPrev {591 // Only one node was requested (immediate next or previous), so exit592 return593 }594 }595 return596}597// Internal implementation of parent nodes that return a raw slice of Nodes.598func getParentNodes(nodes []*html.Node) []*html.Node {599 return mapNodes(nodes, func(i int, n *html.Node) []*html.Node {600 if n.Parent != nil && n.Parent.Type == html.ElementNode {601 return []*html.Node{n.Parent}602 }603 return nil604 })605}606// Internal map function used by many traversing methods. Takes the source nodes607// to iterate on and the mapping function that returns an array of nodes.608// Returns an array of nodes mapped by calling the callback function once for609// each node in the source nodes.610func mapNodes(nodes []*html.Node, f func(int, *html.Node) []*html.Node) (result []*html.Node) {611 set := make(map[*html.Node]bool)612 for i, n := range nodes {613 if vals := f(i, n); len(vals) > 0 {614 result = appendWithoutDuplicates(result, vals, set)615 }616 }617 return result618}...

Full Screen

Full Screen

manipulation.go

Source:manipulation.go Github

copy

Full Screen

...291// Unwrap removes the parents of the set of matched elements, leaving the matched292// elements (and their siblings, if any) in their place.293// It returns the original selection.294func (s *Selection) Unwrap() *Selection {295 s.Parent().Each(func(i int, ss *Selection) {296 // For some reason, jquery allows unwrap to remove the <head> element, so297 // allowing it here too. Same for <html>. Why it allows those elements to298 // be unwrapped while not allowing body is a mystery to me.299 if ss.Nodes[0].Data != "body" {300 ss.ReplaceWithSelection(ss.Contents())301 }302 })303 return s304}305// Wrap wraps each element in the set of matched elements inside the first306// element matched by the given selector. The matched child is cloned before307// being inserted into the document.308//309// It returns the original set of elements.310func (s *Selection) Wrap(selector string) *Selection {311 return s.WrapMatcher(compileMatcher(selector))312}313// WrapMatcher wraps each element in the set of matched elements inside the314// first element matched by the given matcher. The matched child is cloned315// before being inserted into the document.316//317// It returns the original set of elements.318func (s *Selection) WrapMatcher(m Matcher) *Selection {319 return s.wrapNodes(m.MatchAll(s.document.rootNode)...)320}321// WrapSelection wraps each element in the set of matched elements inside the322// first element in the given Selection. The element is cloned before being323// inserted into the document.324//325// It returns the original set of elements.326func (s *Selection) WrapSelection(sel *Selection) *Selection {327 return s.wrapNodes(sel.Nodes...)328}329// WrapHtml wraps each element in the set of matched elements inside the inner-330// most child of the given HTML.331//332// It returns the original set of elements.333func (s *Selection) WrapHtml(htmlStr string) *Selection {334 nodesMap := make(map[string][]*html.Node)335 for _, context := range s.Nodes {336 var parent *html.Node337 if context.Parent != nil {338 parent = context.Parent339 } else {340 parent = &html.Node{Type: html.ElementNode}341 }342 nodes, found := nodesMap[nodeName(parent)]343 if !found {344 nodes = parseHtmlWithContext(htmlStr, parent)345 nodesMap[nodeName(parent)] = nodes346 }347 newSingleSelection(context, s.document).wrapAllNodes(cloneNodes(nodes)...)348 }349 return s350}351// WrapNode wraps each element in the set of matched elements inside the inner-352// most child of the given node. The given node is copied before being inserted353// into the document.354//355// It returns the original set of elements.356func (s *Selection) WrapNode(n *html.Node) *Selection {357 return s.wrapNodes(n)358}359func (s *Selection) wrapNodes(ns ...*html.Node) *Selection {360 s.Each(func(i int, ss *Selection) {361 ss.wrapAllNodes(ns...)362 })363 return s364}365// WrapAll wraps a single HTML structure, matched by the given selector, around366// all elements in the set of matched elements. The matched child is cloned367// before being inserted into the document.368//369// It returns the original set of elements.370func (s *Selection) WrapAll(selector string) *Selection {371 return s.WrapAllMatcher(compileMatcher(selector))372}373// WrapAllMatcher wraps a single HTML structure, matched by the given Matcher,374// around all elements in the set of matched elements. The matched child is375// cloned before being inserted into the document.376//377// It returns the original set of elements.378func (s *Selection) WrapAllMatcher(m Matcher) *Selection {379 return s.wrapAllNodes(m.MatchAll(s.document.rootNode)...)380}381// WrapAllSelection wraps a single HTML structure, the first node of the given382// Selection, around all elements in the set of matched elements. The matched383// child is cloned before being inserted into the document.384//385// It returns the original set of elements.386func (s *Selection) WrapAllSelection(sel *Selection) *Selection {387 return s.wrapAllNodes(sel.Nodes...)388}389// WrapAllHtml wraps the given HTML structure around all elements in the set of390// matched elements. The matched child is cloned before being inserted into the391// document.392//393// It returns the original set of elements.394func (s *Selection) WrapAllHtml(htmlStr string) *Selection {395 var context *html.Node396 var nodes []*html.Node397 if len(s.Nodes) > 0 {398 context = s.Nodes[0]399 if context.Parent != nil {400 nodes = parseHtmlWithContext(htmlStr, context)401 } else {402 nodes = parseHtml(htmlStr)403 }404 }405 return s.wrapAllNodes(nodes...)406}407func (s *Selection) wrapAllNodes(ns ...*html.Node) *Selection {408 if len(ns) > 0 {409 return s.WrapAllNode(ns[0])410 }411 return s412}413// WrapAllNode wraps the given node around the first element in the Selection,414// making all other nodes in the Selection children of the given node. The node415// is cloned before being inserted into the document.416//417// It returns the original set of elements.418func (s *Selection) WrapAllNode(n *html.Node) *Selection {419 if s.Size() == 0 {420 return s421 }422 wrap := cloneNode(n)423 first := s.Nodes[0]424 if first.Parent != nil {425 first.Parent.InsertBefore(wrap, first)426 first.Parent.RemoveChild(first)427 }428 for c := getFirstChildEl(wrap); c != nil; c = getFirstChildEl(wrap) {429 wrap = c430 }431 newSingleSelection(wrap, s.document).AppendSelection(s)432 return s433}434// WrapInner wraps an HTML structure, matched by the given selector, around the435// content of element in the set of matched elements. The matched child is436// cloned before being inserted into the document.437//438// It returns the original set of elements.439func (s *Selection) WrapInner(selector string) *Selection {440 return s.WrapInnerMatcher(compileMatcher(selector))441}442// WrapInnerMatcher wraps an HTML structure, matched by the given selector,443// around the content of element in the set of matched elements. The matched444// child is cloned before being inserted into the document.445//446// It returns the original set of elements.447func (s *Selection) WrapInnerMatcher(m Matcher) *Selection {448 return s.wrapInnerNodes(m.MatchAll(s.document.rootNode)...)449}450// WrapInnerSelection wraps an HTML structure, matched by the given selector,451// around the content of element in the set of matched elements. The matched452// child is cloned before being inserted into the document.453//454// It returns the original set of elements.455func (s *Selection) WrapInnerSelection(sel *Selection) *Selection {456 return s.wrapInnerNodes(sel.Nodes...)457}458// WrapInnerHtml wraps an HTML structure, matched by the given selector, around459// the content of element in the set of matched elements. The matched child is460// cloned before being inserted into the document.461//462// It returns the original set of elements.463func (s *Selection) WrapInnerHtml(htmlStr string) *Selection {464 nodesMap := make(map[string][]*html.Node)465 for _, context := range s.Nodes {466 nodes, found := nodesMap[nodeName(context)]467 if !found {468 nodes = parseHtmlWithContext(htmlStr, context)469 nodesMap[nodeName(context)] = nodes470 }471 newSingleSelection(context, s.document).wrapInnerNodes(cloneNodes(nodes)...)472 }473 return s474}475// WrapInnerNode wraps an HTML structure, matched by the given selector, around476// the content of element in the set of matched elements. The matched child is477// cloned before being inserted into the document.478//479// It returns the original set of elements.480func (s *Selection) WrapInnerNode(n *html.Node) *Selection {481 return s.wrapInnerNodes(n)482}483func (s *Selection) wrapInnerNodes(ns ...*html.Node) *Selection {484 if len(ns) == 0 {485 return s486 }487 s.Each(func(i int, s *Selection) {488 contents := s.Contents()489 if contents.Size() > 0 {490 contents.wrapAllNodes(ns...)491 } else {492 s.AppendNodes(cloneNode(ns[0]))493 }494 })495 return s496}497func parseHtml(h string) []*html.Node {498 // Errors are only returned when the io.Reader returns any error besides499 // EOF, but strings.Reader never will500 nodes, err := html.ParseFragment(strings.NewReader(h), &html.Node{Type: html.ElementNode})501 if err != nil {...

Full Screen

Full Screen

getElementByID.go

Source:getElementByID.go Github

copy

Full Screen

...35 node := ElementByID(doc, id)36 if node == nil {37 fmt.Printf("No element with id %q found\n", id)38 } else {39 forEachNode(node, printStartElement, printEndElement)40 }41 return nil42}43//!+forEachNode44// forEachNode calls the functions pre(x) and post(x) for each node45// x in the tree rooted at n. Both functions are optional.46// pre is called before the children are visited (preorder) and47// post is called after (postorder).48// pre/post/forEachNode return value:49// true to stop, false to continue50func forEachNode(n *html.Node, pre, post func(n *html.Node) bool) bool {51 if pre != nil {52 if pre(n) {53 return true54 }55 }56 for c := n.FirstChild; c != nil; c = c.NextSibling {57 if forEachNode(c, pre, post) {58 return true59 }60 }61 if post != nil {62 if post(n) {63 return true64 }65 }66 return false67}68//!-forEachNode69func ElementByID(doc *html.Node, id string) *html.Node {70 var node *html.Node71 forEachNode(doc, func(n *html.Node) bool {72 if n.Type != html.ElementNode {73 return false74 }75 for _, a := range n.Attr {76 if a.Key == "id" && a.Val == id {77 node = n78 return true79 }80 }81 return false82 }, nil)83 return node84}85var depth int...

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 log.Fatal(err)13 }14 doc.Find(".gb_P").Each(func(i int, s *goquery.Selection) {15 band := strings.TrimSpace(s.Find("span").Text())16 title := strings.TrimSpace(s.Find("a").Text())17 fmt.Printf("Review %d: %s - %s18 })19}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 doc.Find("a").Each(func(i int, s *goquery.Selection) {7 link, _ := s.Attr("href")8 fmt.Printf("Link %d: %s", i, link)9 })10}11import (12func main() {13 if err != nil {14 log.Fatal(err)15 }16 doc.Find("a").EachWithBreak(func(i int, s *goquery.Selection) bool {17 link, _ := s.Attr("href")18 fmt.Printf("Link %d: %s", i, link)19 })20}21import (22func main() {23 if err != nil {24 log.Fatal(err)25 }26 doc.Find("a").EachWithBreak(func(i int, s *goquery.Selection) bool {27 link, _ := s.Attr("href")28 fmt.Printf("Link %d: %s", i, link)29 })30}31import (32func main() {33 if err != nil {34 log.Fatal(err)35 }36 doc.Find("a").Filter(".gb_e").Each(func(i int, s *goquery.Selection) {37 link, _ := s.Attr("href")38 fmt.Printf("Link %d: %s", i, link)39 })40}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 log.Fatal(err)13 }14 doc.Find("img").Each(func(i int, s *goquery.Selection) {15 src, _ := s.Attr("src")16 fmt.Printf("Review %d: %s \n", i, src)17 })18}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 log.Fatal(err)13 }14 doc.Find(".review").Each(func(i int, s *goquery.Selection) {15 band := s.Find("a").Text()16 title := s.Find("i").Text()17 fmt.Printf("Review %d: %s - %s18 })19}20import (21func main() {22 if err != nil {23 log.Fatal(err)24 }25 defer res.Body.Close()26 if res.StatusCode != 200 {27 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)28 }29 doc, err := goquery.NewDocumentFromReader(res.Body)30 if err != nil {31 log.Fatal(err)32 }33 doc.Find(".review").Each(func(i int, s *goquery.Selection) {34 band := s.Find("a").Text()35 title := s.Find("i").Text()36 fmt.Printf("Review %d: %s - %s37 })38}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 panic(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 panic("status code error: " + res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 panic(err)13 }14 doc.Find(".gb_P").Each(func(i int, s *goquery.Selection) {15 band := s.Find("a").Text()16 title := s.Find("a").Text()17 fmt.Printf("Review %d: %s - %s18 })19}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 doc, err := goquery.NewDocumentFromReader(res.Body)7 if err != nil {8 log.Fatal(err)9 }10 doc.Find("h2").Each(func(i int, s *goquery.Selection) {11 title := s.Find("a").Text()12 linkTag := s.Find("a")13 link, _ := linkTag.Attr("href")14 fmt.Printf("Post #%d: %s - %s\n", i, title, link)15 })16}

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println("Error: ", err)5 }6 doc.Find("div").Each(func(i int, s *goquery.Selection) {7 band := s.Find("a.band").Text()8 title := s.Find("a.title").Text()9 fmt.Printf("Review %d: %s - %s10 })11}12import (13func main() {14 if err != nil {15 fmt.Println("Error: ", err)16 }17 doc.Find("div").Each(func(i int, s *goquery.Selection) {18 band := s.Find("a.band").Text()19 title := s.Find("a.title").Text()20 fmt.Printf("Review %d: %s - %s21 })22}23import (24func main() {25 if err != nil {26 fmt.Println("Error: ", err)27 }28 doc.Find("div").Each(func(i int, s *goquery.Selection) {29 band := s.Find("a.band").Text()30 title := s.Find("a.title").Text()31 fmt.Printf("Review %d: %s - %s32 })33}34import (35func main() {

Full Screen

Full Screen

Each

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))4 body := doc.Find("body")5 div := doc.Find("div")6 body.AppendSelection(div)7 text := doc.Find("div").First().Text()8 div.AppendHtml(text)9 body.Each(func(i int, s *goquery.Selection) {10 fmt.Println(s.Text())11 })12}13import (14func main() {

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