| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package tpl |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
func TestStripHTML(t *testing.T) { |
| 21 |
type test struct { |
| 22 |
input, expected string |
| 23 |
} |
| 24 |
data := []test{ |
| 25 |
{"<h1>strip h1 tag <h1>", "strip h1 tag "}, |
| 26 |
{"<p> strip p tag </p>", " strip p tag "}, |
| 27 |
{"</br> strip br<br>", " strip br\n"}, |
| 28 |
{"</br> strip br2<br />", " strip br2\n"}, |
| 29 |
{"This <strong>is</strong> a\nnewline", "This is a newline"}, |
| 30 |
{"No Tags", "No Tags"}, |
| 31 |
{`<p>Summary Next Line. |
| 32 |
<figure > |
| 33 |
|
| 34 |
<img src="/not/real" /> |
| 35 |
|
| 36 |
|
| 37 |
</figure> |
| 38 |
. |
| 39 |
More text here.</p> |
| 40 |
|
| 41 |
<p>Some more text</p>`, "Summary Next Line. . More text here.\nSome more text\n"}, |
| 42 |
|
| 43 |
// Issue 9199 |
| 44 |
{"<div data-action='click->my-controller#doThing'>qwe</div>", "qwe"}, |
| 45 |
{"Hello, World!", "Hello, World!"}, |
| 46 |
{"foo&bar", "foo&bar"}, |
| 47 |
{`Hello <a href="www.example.com/">World</a>!`, "Hello World!"}, |
| 48 |
{"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"}, |
| 49 |
{"Foo <!-- Bar --> Baz", "Foo Baz"}, |
| 50 |
} |
| 51 |
for i, d := range data { |
| 52 |
output := StripHTML(d.input) |
| 53 |
if d.expected != output { |
| 54 |
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, output) |
| 55 |
} |
| 56 |
} |
| 57 |
} |