How to use Elements method of html Package

Best K6 code snippet using html.Elements

traversal.go

Source:traversal.go Github

copy

Full Screen

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

Full Screen

Full Screen

manipulation.go

Source:manipulation.go Github

copy

Full Screen

...57// in the set of matched elements, following those rules:58//59// 1) The selector is applied to the root document.60//61// 2) Elements that are part of the document will be moved to the new location.62//63// 3) If there are multiple locations to append to, cloned nodes will be64// appended to all target locations except the last one, which will be moved65// as noted in (2).66func (s *Selection) Append(selector string) *Selection {67 return s.AppendMatcher(compileMatcher(selector))68}69// AppendMatcher appends the elements specified by the matcher to the end of each element70// in the set of matched elements.71//72// This follows the same rules as Selection.Append.73func (s *Selection) AppendMatcher(m Matcher) *Selection {74 return s.AppendNodes(m.MatchAll(s.document.rootNode)...)75}...

Full Screen

Full Screen

getElementsByTagname.go

Source:getElementsByTagname.go Github

copy

Full Screen

...9 "golang.org/x/net/html"10)11func main() {12 if len(os.Args) < 3 {13 fmt.Println("usage: getElementsByTagName url names")14 os.Exit(1)15 }16 if err := getElementsByTagName(os.Args[1], os.Args[2:]); err != nil {17 fmt.Printf("getElementsByTagName failed: %v\n", err)18 os.Exit(1)19 }20}21func getElementsByTagName(url string, name []string) error {22 resp, err := http.Get(url)23 if err != nil {24 return err25 }26 defer resp.Body.Close()27 if resp.StatusCode != http.StatusOK {28 return fmt.Errorf("Get Failed: %s", resp.Status)29 }30 doc, err := html.Parse(resp.Body)31 if err != nil {32 return err33 }34 node := ElementsByTagName(doc, name...)35 if node == nil {36 fmt.Printf("No element with names given %v found\n", name)37 } else {38 fmt.Printf("Found %d elements with names given %v\n", len(node), name)39 }40 images := ElementsByTagName(doc, "img")41 headings := ElementsByTagName(doc, "h1", "h2", "h3", "h4")42 fmt.Printf("found %d images, %d headings", len(images), len(headings))43 return nil44}45//!+forEachNode46// forEachNode calls the functions pre(x) and post(x) for each node47// x in the tree rooted at n. Both functions are optional.48// pre is called before the children are visited (preorder) and49// post is called after (postorder).50func forEachNode(n *html.Node, pre, post func(n *html.Node)) {51 if pre != nil {52 pre(n)53 }54 for c := n.FirstChild; c != nil; c = c.NextSibling {55 forEachNode(c, pre, post)56 }57 if post != nil {58 post(n)59 }60}61//!-forEachNode62func ElementsByTagName(doc *html.Node, name ...string) []*html.Node {63 var node []*html.Node64 var nameMap = make(map[string]bool, len(name))65 for _, n := range name {66 nameMap[n] = true67 }68 forEachNode(doc, func(n *html.Node) {69 if n.Type == html.ElementNode && nameMap[n.Data] {70 node = append(node, n)71 }72 }, nil)73 return node74}...

Full Screen

Full Screen

Elements

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, "Elements: %v\n", err)6 os.Exit(1)7 }8 for _, element := range Elements(doc) {9 fmt.Println(element)10 }11}12func Elements(n *html.Node) []string {13 var f func(*html.Node)14 f = func(n *html.Node) {15 if n.Type == html.ElementNode {16 elements = append(elements, n.Data)17 }18 for c := n.FirstChild; c != nil; c = c.NextSibling {19 f(c)20 }21 }22 f(n)23}

Full Screen

Full Screen

Elements

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, "Elements: %v\n", err)6 os.Exit(1)7 }8 for _, link := range Elements(doc) {9 fmt.Println(link)10 }11}12func Elements(n *html.Node) []string {13 var visit func(*html.Node)14 visit = func(n *html.Node) {15 if n.Type == html.ElementNode {16 links = append(links, n.Data)17 }18 for c := n.FirstChild; c != nil; c = c.NextSibling {19 visit(c)20 }21 }22 visit(n)23}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Open("1.html")4 if err != nil {5 log.Fatal(err)6 }7 defer f.Close()8 doc, err := html.Parse(f)9 if err != nil {10 log.Fatal(err)11 }12 for _, n := range Elements(doc) {13 fmt.Println(n.Data)14 }15}16func Elements(n *html.Node) []*html.Node {17 if n.Type == html.ElementNode {18 els = append(els, n)19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 els = append(els, Elements(c)...)22 }23}

Full Screen

Full Screen

Elements

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, "Elements: %v\n", err)6 os.Exit(1)7 }8 for _, link := range Elements(doc) {9 fmt.Println(link)10 }11}12func Elements(n *html.Node) []string {13 if n.Type == html.ElementNode {14 links = append(links, n.Data)15 }16 for c := n.FirstChild; c != nil; c = c.NextSibling {17 links = append(links, Elements(c)...)18 }19}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func ElementsByTagName(doc *html.Node, name ...string) []*html.Node {3 visit := func(n *html.Node) {4 if n.Type == html.ElementNode {5 for _, tag := range name {6 if n.Data == tag {7 elements = append(elements, n)8 }9 }10 }11 }12 forEachNode(doc, visit, nil)13}14func forEachNode(n *html.Node, pre, post func(n *html.Node)) {15 if n == nil {16 }17 if pre != nil {18 pre(n)19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 forEachNode(c, pre, post)22 }23 if post != nil {24 post(n)25 }26}27func main() {28 doc, _ := html.Parse(strings.NewReader("<html><head></head><body><h1>hello</h1><h2>world</h2></body></html>"))29 fmt.Printf("%v", ElementsByTagName(doc, "h1", "h2"))30}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Println("error in parsing html")6 os.Exit(1)7 }8 for _, link := range Elements(doc) {9 fmt.Println(link)10 }11}12func Elements(n *html.Node) []string {13 if n.Type == html.ElementNode {14 links = append(links, n.Data)15 }16 for c := n.FirstChild; c != nil; c = c.NextSibling {17 links = append(links, Elements(c)...)18 }19}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 resp, err := http.Get(url)4 if err != nil {5 log.Fatal(err)6 }7 defer resp.Body.Close()8 doc, err := html.Parse(resp.Body)9 if err != nil {10 log.Fatal(err)11 }12 for _, tag := range ElementsByTagName(doc, "h1", "h2", "h3", "h4", "h5", "h6") {13 fmt.Println(tag.Data)14 }15}16func ElementsByTagName(doc *html.Node, name ...string) []*html.Node {17 if doc == nil {18 }19 if name == nil {20 }21 forEachNode(doc, func(n *html.Node) {22 if n.Type == html.ElementNode {23 for _, tag := range name {24 if tag == n.Data {25 nodes = append(nodes, n)26 }27 }28 }29 }, nil)30}31func forEachNode(n *html.Node, pre, post func(n *html.Node)) {32 if n == nil {33 }34 if pre != nil {35 pre(n)36 }37 for c := n.FirstChild; c != nil; c = c.NextSibling {38 forEachNode(c, pre, post)39 }40 if post != nil {41 post(n)42 }43}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Println("error in parsing html")6 os.Exit(1)7 }8 for _, link := range Elements(doc) {9 fmt.Println(link)10 }11}12func Elements(n *html.Node) []string {13 if n.Type == html.ElementNode {14 links = append(links, n.Data)15 }16 for c := n.FirstChild; c != nil; c = c.NextSibling {17 links = append(links, Elements(c)...)18 }19}20 fmt.Println(doc)21}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2fnc main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "Elents: %v\", err)6 os.Exi(1)7 }8 for _, link := range Elements(doc, "a") {9= ======f("%s\n", ik)10 }11}12func Elementsn *html.Node, name string) []string {13 if n.Type == html.ElementNode && n.Data == name {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = appen(links, a.Val)17 }18 }19 }20 fr c := n.FirstChild; != nil; c = c.NextSibling {21 }22import (23func main() {24 resp, err := http.Get(url)25 if err != nil {26 log.Fatal(err)27 }28 defer resp.Body.Close()29 doc, err := html.Parse(resp.Body)30 if err != nil {31 log.Fatal(err)32 }33 for _, tag := range ElementsByTagName(doc, "h1", "h2", "h3", "h4", "h5", "h6") {34 fmt.Println(tag.Data)35 }36}37func ElementsByTagName(doc *html.Node, name ...string) []*html.Node {38 if doc == nil {39 }40 if name == nil {41 }42 forEachNode(doc, func(n *html.Node) {43 if n.Type == html.ElementNode {44 for _, tag := range name {45 if tag == n.Data {46 nodes = append(nodes, n)47 }48 }49 }50 }, nil)51}52func forEachNode(n *html.Node, pre, post func(n *html.Node)) {53 if n == nil {54 }55 if pre != nil {56 pre(n)57 }58 for c := n.FirstChild; c != nil; c = c.NextSibling {59 forEachNode(c, pre, post)60 }61 if post != nil {62 post(n)63 }64}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 resp, err := http.Get(url)4 if err != nil {5 log.Fatal(err)6 }7 doc, err := html.Parse(resp.Body)8 if err != nil {9 log.Fatal(err)10 }11 resp.Body.Close()12 fmt.Println(doc)13}

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