ch5ex17

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2022 License: GPL-3.0 Imports: 1 Imported by: 0

README

= Exercise 5.17
// Refs:
:url-base: https://github.com/fenegroni/TGPL-exercise-solutions
:workflow: workflows/Exercise 5.17
:action: actions/workflows/ch5ex17.yml
:url-workflow: {url-base}/{workflow}
:url-action: {url-base}/{action}
:badge-exercise: image:{url-workflow}/badge.svg?branch=main[link={url-action}]

{badge-exercise}

Write a variadic function `ElementsByTagName` that, given an HTML node tree
and zero or more names, returns all the elements that match one of those names.

Here are two example calls:
[source,go]
----
func ElementsByTagName(doc *html.Node, name ...string) []*html.Node

images := ElementsByTagName(doc, "img")
headings := ElementsByTagName(doc, "h1", "h2", "h3", "h4")
----

== Test

We build a map with the count of each node we expect to find in the result from `ElementsByTagName`.
We then use `reflect.DeepEqual` to compare the counts of all elements in the result set with
each test's expectations.

This strategy validates that `ElementsByTagName` does not return nodes that we didn't ask for,
and does not return elements we did ask for but are not in the document.

== `forEachNode`

We use `forEachNode` from exercise 5.13, and an anonymous function, to implement `ElementsByTagName`.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElementsByTagName

func ElementsByTagName(doc *html.Node, tags ...string) (matchingNodes []*html.Node)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL