diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-06 03:42:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-09 01:10:17 +0700 |
| commit | e730418289d80985c5d48946353961a357a4b532 (patch) | |
| tree | 9e573bf4fca975775eb25f32448f755a325880a8 /lib/html/example_node_iterator_test.go | |
| parent | 1e7cb99f42bcd41e98326bd9406d3cecfb2a4542 (diff) | |
| download | pakakeh.go-e730418289d80985c5d48946353961a357a4b532.tar.xz | |
lib: move package "net/html" to "lib/html"
Putting "html" under "net" package make no sense.
Another reason is to make the package flat under "lib/" directory.
Diffstat (limited to 'lib/html/example_node_iterator_test.go')
| -rw-r--r-- | lib/html/example_node_iterator_test.go | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/html/example_node_iterator_test.go b/lib/html/example_node_iterator_test.go new file mode 100644 index 00000000..94ece050 --- /dev/null +++ b/lib/html/example_node_iterator_test.go @@ -0,0 +1,96 @@ +// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package html + +import ( + "fmt" + "log" + "strings" +) + +func ExampleParse() { + rawHTML := ` +<ul> + <li> + <b>item</b> + <span>one</span> + </li> +</ul> +` + + r := strings.NewReader(rawHTML) + + iter, err := Parse(r) + if err != nil { + log.Fatal(err) + } + + for node := iter.Next(); node != nil; node = iter.Next() { + if node.IsElement() { + fmt.Printf("%s\n", node.Data) + } else { + fmt.Printf("\t%s\n", node.Data) + } + } + + // Output: + // html + // head + // body + // ul + // li + // b + // item + // b + // span + // one + // span + // li + // ul + // body + // html +} + +func ExampleNodeIterator_SetNext() { + rawHTML := ` +<ul> + <li> + <b>item</b> + <span>one</span> + </li> +</ul> +<h2>Jump here</h2> +` + + r := strings.NewReader(rawHTML) + + iter, err := Parse(r) + if err != nil { + log.Fatal(err) + } + + for node := iter.Next(); node != nil; node = iter.Next() { + if node.IsElement() { + if node.Data == "ul" { + // Skip iterating the "ul" element. + iter.SetNext(node.GetNextSibling()) + continue + } + fmt.Printf("%s\n", node.Data) + } else { + fmt.Printf("\t%s\n", node.Data) + } + } + + // Output: + // html + // head + // body + // h2 + // Jump here + // h2 + // body + // html +} |
