= 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`.