1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package domutils
import (
"testing"
"github.com/JohannesKaufmann/dom"
"github.com/JohannesKaufmann/html-to-markdown/v2/internal/tester"
"golang.org/x/net/html"
)
func TestRemoveRedundant(t *testing.T) {
runs := []struct {
desc string
input string
expected string
}{
{
desc: "don't change other tags",
input: `<span>a</span> <span>b</span>`,
expected: `
├─body
│ ├─span
│ │ ├─#text "a"
│ ├─#text " "
│ ├─span
│ │ ├─#text "b"
`,
},
{
desc: "don't change simple strong",
input: `<strong>a</strong>`,
expected: `
├─body
│ ├─strong
│ │ ├─#text "a"
`,
},
{
desc: "remove double strong",
input: `<strong><strong>a</strong></strong>`,
expected: `
├─body
│ ├─strong
│ │ ├─#text "a"
`,
},
{
desc: "remove more complicated double strong",
input: `<strong><strong>a</strong> b <strong><strong>c</strong></strong></strong>`,
expected: `
├─body
│ ├─strong
│ │ ├─#text "a"
│ │ ├─#text " b "
│ │ ├─#text "c"
`,
},
{
desc: "leave italic inside bold",
input: `<strong>A<em>B</em>C</strong>`,
expected: `
├─body
│ ├─strong
│ │ ├─#text "A"
│ │ ├─em
│ │ │ ├─#text "B"
│ │ ├─#text "C"
`,
},
{
desc: "dont leave other italic inside another italic",
input: `<i>A<em>B</em>C</i>`,
expected: `
├─body
│ ├─i
│ │ ├─#text "A"
│ │ ├─#text "B"
│ │ ├─#text "C"
`,
},
}
for _, run := range runs {
t.Run(run.desc, func(t *testing.T) {
doc := tester.Parse(t, run.input, "")
RemoveRedundant(doc, func(a, b *html.Node) bool {
isItalic := func(n *html.Node) bool {
name := dom.NodeName(n)
return name == "em" || name == "i"
}
isBold := func(n *html.Node) bool {
name := dom.NodeName(n)
return name == "strong" || name == "b"
}
if isItalic(a) && isItalic(b) {
return true
}
if isBold(a) && isBold(b) {
return true
}
return false
})
tester.ExpectRepresentation(t, doc, "output", run.expected)
})
}
}
|