ch5ex3

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: 4 Imported by: 0

README

= Exercise 5.3
// Refs:
:url-base: https://github.com/fenegroni/TGPL-exercise-solutions
:workflow: workflows/Exercise 5.3
:action: actions/workflows/ch5ex3.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 function to print the contents of all text nodes in an HTML document tree.
Do not descend into <script> or <style> elements,
since their content are not visible in a web browser.

== Test

The new function to be tested is called `PrintAllTextNodesContent`.

The test follows the same pattern as exercises 5.1 and 5.2,
except this time the `want` element of the test is a simple string.

[source,go]
----
struct {
	document string
	want     string
}
----

== Example

To make the output more predictable
I made sure all leading and trailing white space is removed and
empty lines are not printed.

Given this example HTML document as input:

[source,html]
----
<html>
    <head></head>
    <body>
        <style>
            p {
                color: red;
            }
        </style>
        <h1>title</h1>
        <script src="javascript.js">
            document.write("hello!")
        </script>
        <p>line1</p>
        <p>line2</p>
    </body>
</html>
----

the Example function's `Output` is very simple.

[source,go]
----
// Output:
// title
// line1
// line2
----

Documentation

Overview

Exercise5.3 prints the content of all text nodes in an HTML document read from standard input, except for <script> and <style> elements.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintAllTextNodesContent

func PrintAllTextNodesContent(n *html.Node, out io.Writer)

PrintAllTextNodesContent prints the contents of all text nodes found in n into out. It does not descend into <script> or <style> elements, since their content are not visible in a web browser. Note: all leading and trailing white space is removed and empty lines are not printed

Example
document := `<html>
	<head></head>
	<body>
		<style>
			p {
				color: red;
			}
		</style>
		<h1>title</h1>
		<script src="javascript.js">
			document.write("hello!")
		</script>
		<p>line1</p>
		<p>line2</p>
	</body>
</html>`
parseTree, _ := html.Parse(strings.NewReader(document))
PrintAllTextNodesContent(parseTree, os.Stdout)
Output:

title
line1
line2

Types

This section is empty.

Jump to

Keyboard shortcuts

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