How to use Prev method of html Package

Best K6 code snippet using html.Prev

traversal.go

Source:traversal.go Github

copy

Full Screen

...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 })...

Full Screen

Full Screen

node_test.go

Source:node_test.go Github

copy

Full Screen

...47 return fmt.Errorf("html: inconsistent child/parent relationship")48 }49 }50 nBackward := 051 for c := n.LastChild; c != nil; c = c.PrevSibling {52 nBackward++53 if nBackward == 1e6 {54 return fmt.Errorf("html: backward list of children looks like an infinite loop")55 }56 if c.Parent != n {57 return fmt.Errorf("html: inconsistent child/parent relationship")58 }59 }60 if n.Parent != nil {61 if n.Parent == n {62 return fmt.Errorf("html: inconsistent parent relationship")63 }64 if n.Parent == n.FirstChild {65 return fmt.Errorf("html: inconsistent parent/first relationship")66 }67 if n.Parent == n.LastChild {68 return fmt.Errorf("html: inconsistent parent/last relationship")69 }70 if n.Parent == n.PrevSibling {71 return fmt.Errorf("html: inconsistent parent/prev relationship")72 }73 if n.Parent == n.NextSibling {74 return fmt.Errorf("html: inconsistent parent/next relationship")75 }76 parentHasNAsAChild := false77 for c := n.Parent.FirstChild; c != nil; c = c.NextSibling {78 if c == n {79 parentHasNAsAChild = true80 break81 }82 }83 if !parentHasNAsAChild {84 return fmt.Errorf("html: inconsistent parent/child relationship")85 }86 }87 if n.PrevSibling != nil && n.PrevSibling.NextSibling != n {88 return fmt.Errorf("html: inconsistent prev/next relationship")89 }90 if n.NextSibling != nil && n.NextSibling.PrevSibling != n {91 return fmt.Errorf("html: inconsistent next/prev relationship")92 }93 if (n.FirstChild == nil) != (n.LastChild == nil) {94 return fmt.Errorf("html: inconsistent first/last relationship")95 }96 if n.FirstChild != nil && n.FirstChild == n.LastChild {97 // We have a sole child.98 if n.FirstChild.PrevSibling != nil || n.FirstChild.NextSibling != nil {99 return fmt.Errorf("html: inconsistent sole child's sibling relationship")100 }101 }102 seen := map[*Node]bool{}103 var last *Node104 for c := n.FirstChild; c != nil; c = c.NextSibling {105 if seen[c] {106 return fmt.Errorf("html: inconsistent repeated child")107 }108 seen[c] = true109 last = c110 }111 if last != n.LastChild {112 return fmt.Errorf("html: inconsistent last relationship")113 }114 var first *Node115 for c := n.LastChild; c != nil; c = c.PrevSibling {116 if !seen[c] {117 return fmt.Errorf("html: inconsistent missing child")118 }119 delete(seen, c)120 first = c121 }122 if first != n.FirstChild {123 return fmt.Errorf("html: inconsistent first relationship")124 }125 if len(seen) != 0 {126 return fmt.Errorf("html: inconsistent forwards/backwards child list")127 }128 return nil129}...

Full Screen

Full Screen

Prev

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, "Error in reading input: %v\n", err)6 os.Exit(1)7 }8 forEachNode(doc, startElement, endElement)9}10func forEachNode(n *html.Node, pre, post func(*html.Node)) {11 if n == nil {12 }13 if pre != nil {14 pre(n)15 }16 for c := n.FirstChild; c != nil; c = c.NextSibling {17 forEachNode(c, pre, post)18 }19 if post != nil {20 post(n)21 }22}23func startElement(n *html.Node) {24 if n.Type == html.ElementNode {25 fmt.Printf("<%s ", n.Data)26 if len(n.Attr) > 0 {27 for _, a := range n.Attr {28 fmt.Printf("%s=%q ", a.Key, a.Val)29 }30 }31 fmt.Printf(">\n")32 }33}34func endElement(n *html.Node) {35 if n.Type == html.ElementNode {36 fmt.Printf("</%s>\n", n.Data)37 }38}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, _ := html.Parse(resp.Body)4 resp.Body.Close()5 forEachNode(doc, startElement, endElement)6}7func forEachNode(n *html.Node, pre, post func(n *html.Node)) {8 if pre != nil {9 pre(n)10 }11 for c := n.FirstChild; c != nil; c = c.NextSibling {12 forEachNode(c, pre, post)13 }14 if post != nil {15 post(n)16 }17}18func startElement(n *html.Node) {19 if n.Type == html.ElementNode {20 fmt.Print("<", n.Data)21 for _, a := range n.Attr {22 fmt.Printf(" %s = %q", a.Key, a.Val)23 }24 fmt.Println(">")25 }26}27func endElement(n *html.Node) {28 if n.Type == html.ElementNode {29 fmt.Printf("</%s>", n.Data)30 }31}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer resp.Body.Close()7 z := html.NewTokenizer(resp.Body)8 for {9 tt := z.Next()10 switch {11 t := z.Token()12 if !isAnchor {13 }14 for _, a := range t.Attr {15 if a.Key == "href" {16 }17 }18 if !hasHref {19 }20 ptt := z.Prev()21 if ptt == html.TextToken {22 fmt.Println(z.Token().Data)23 }24 }25 }26}27import (28func main() {29 if err != nil {30 fmt.Println(err)31 }32 defer resp.Body.Close()33 z := html.NewTokenizer(resp.Body)34 for {35 tt := z.Next()36 switch {37 t := z.Token()38 if !isAnchor {39 }40 for _, a := range t.Attr {41 if a.Key == "href" {42 }43 }44 if !hasHref {45 }46 ntt := z.Next()

Full Screen

Full Screen

Prev

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 forEachNode(doc, startElement, endElement)12}13func forEachNode(n *html.Node, pre, post func(n *html.Node)) {14 /*if pre != nil {15 pre(n)16 }*/17 for c := n.FirstChild; c != nil; c = c.NextSibling {18 forEachNode(c, pre, post)19 }20 /*if post != nil {21 post(n)22 }*/23}24func startElement(n *html.Node) {25 /*if n.Type == html.ElementNode {26 fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)27 }*/28 if n.Type == html.ElementNode {29 fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)30 }31 /*if n.Type == html.TextNode {32 fmt.Printf("%*s%s\n", depth*2, "", n.Data)33 }*/34 if n.Type == html.TextNode {35 fmt.Printf("%*s%s\n", depth*2, "", n.Data)36 }37 /*if n.Type == html.CommentNode {38 fmt.Printf("%*s%s\n", depth*2, "", n.Data)39 }*/40 if n.Type == html.CommentNode {41 fmt.Printf("%*s%s\n", depth*2, "", n.Data)42 }43}44func endElement(n *html.Node) {45 /*if n.Type == html.ElementNode {46 fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)47 }*/48 if n.Type == html.ElementNode {49 fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)50 }51}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, _ := html.Parse(strings.NewReader(s))4 var f func(*html.Node)5 f = func(n *html.Node) {6 if n.Type == html.ElementNode {7 fmt.Printf("%*s<%s>\n", n.PrevSibling.Data, "", n.Data)8 }9 for c := n.FirstChild; c != nil; c = c.NextSibling {10 f(c)11 }12 }13 f(doc)14}

Full Screen

Full Screen

Prev

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:%v\n", err)6 os.Exit(1)7 }8 forEachNode(doc, startElement, endElement)9}10func forEachNode(n *html.Node, pre, post func(n *html.Node) bool) {11 if pre != nil {12 if !pre(n) {13 }14 }15 for c := n.FirstChild; c != nil; c = c.NextSibling {16 forEachNode(c, pre, post)17 }18 if post != nil {19 if !post(n) {20 }21 }22}23func startElement(n *html.Node) bool {24 if n.Type == html.ElementNode {25 fmt.Printf("<%s>", n.Data)26 }27}28func endElement(n *html.Node) bool {29 if n.Type == html.ElementNode {30 fmt.Printf("</%s>", n.Data)31 }32}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer resp.Body.Close()7 body, err := ioutil.ReadAll(resp.Body)8 if err != nil {9 fmt.Println(err)10 }11 doc, err := html.Parse(resp.Body)12 if err != nil {13 fmt.Println(err)14 }15 printLinks(doc)16}17func printLinks(doc *html.Node) {18 if doc.Type == html.ElementNode && doc.Data == "a" {19 for _, attr := range doc.Attr {20 if attr.Key == "href" {21 fmt.Println(attr.Val)22 }23 }24 }25 for c := doc.FirstChild; c != nil; c = c.NextSibling {26 printLinks(c)27 }28}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(http.Get(url))4 if err != nil {5 log.Fatal(err)6 }7 fmt.Println(title(doc))8}9func title(doc *html.Node) string {10 if doc.Type == html.ElementNode && doc.Data == "title" {11 }12 for c := doc.FirstChild; c != nil; c = c.NextSibling {13 t := title(c)14 if t != "" {15 }16 }17}

Full Screen

Full Screen

Prev

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 forEachNode(doc, startElement, endElement)7}8 if pre != nil {9 pre(n)10 }11 forEachNode(c, pre, post)12 }13 if post != nil {14 post(n)15 }16}17func startElement(n *html.Node) {18 if n.Type == html.ElementNode {19 fmt.Printf("%*s<%s>\n", depth*2, "", n.Data)20 } else if n.Type == html.TextNode {21 fmt.Printf("%*s%s\n", depth*2, "", n.Data)22 }23}24func endElement(n *html.Node) {25 if n.Type == html.ElementNode {26 fmt.Printf("%*s</%s>\n", depth*2, "", n.Data)27 }28}

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