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
|
package domutils
import (
"context"
"github.com/JohannesKaufmann/dom"
"golang.org/x/net/html"
)
func isFakeSpan(node *html.Node) bool {
name := dom.NodeName(node)
if name != "span" {
return false
}
var containsBlockNode = false
var finder func(*html.Node)
finder = func(node *html.Node) {
name := dom.NodeName(node)
if dom.NameIsBlockNode(name) {
containsBlockNode = true
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
finder(child)
}
}
finder(node)
return containsBlockNode
}
// RenameFakeSpans renames all "span" nodes to "div" if
// any block element is found as a child.
func RenameFakeSpans(ctx context.Context, doc *html.Node) {
var finder func(node *html.Node)
finder = func(node *html.Node) {
if isFakeSpan(node) {
node.Data = "div"
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
finder(child)
}
}
finder(doc)
}
|