✌️
I am trying to replace an html parent element with its child elements using "github.com/PuerkitoBio/goquery". However, ReplaceWithSelection does not replace anything and the selection remains unchanged.
package main
import (
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
var html = `
<section>
<article>
<h2>Article 1</h2>
<p>Text for article #1</p>
</article>
<article>
<h2>Article 2</h2>
<p>Text for article #2</p>
</article>
</section>
`
func main() {
qHtml, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
panic(err)
}
section := qHtml.Find(`section`)
sectionChildren := section.Children().Clone()
section.ReplaceWithSelection(sectionChildren)
goquery.Render(os.Stdout, section)
}
Since
section
just references one of the children in actualDocument
on which we are usingReplaceWithSelection
the changes will be reflected in actual Document instead of reference which may have be replaced from actualDocument
but since you have the reference still you are to view it with the actual changes.To view the changes instead of using
section
useDocument
on which the changes are done.Change
To