I am trying to use a string containing a ' character as an attribute value. However, ' is always replaced with '. Is there a way around this? Since the value is enclosed with ", ' wouldn't necessarily have to be escaped, would it? The same happens when I try to add a plain text html node.
package main
import (
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
var htmlString = `<div class="catch-me-if-you-can">♂️</div>`
func main() {
qHtml, err := goquery.NewDocumentFromReader(strings.NewReader(htmlString))
if err != nil {
panic(err)
}
n := &html.Node{
Type: html.ElementNode,
Data: "zombie-hunter",
Attr: []html.Attribute{
{Key: "meta", Val: "{name: 'RedEye!', equipment: 'Baseball Bat'}"},
},
}
qHtml.Find(`div`).First().ReplaceWithNodes(n)
goquery.Render(os.Stdout, qHtml.Contents())
}
Output:
<html><head></head><body><zombie-hunter meta="{name: 'RedEye!', equipment: 'Baseball Bat'}"></zombie-hunter></body></html>
No, it is not (easily) possible as
goquery
relies ongolang.org/x/net/html.Render()
for HTML output and that hardcodes text-node escaping.If you really need it you'd probably need to re-implement
html.Render()
(and related functions) to not escape this one character and patchgoquery
to use your implementation then. Given'
is valid HTML escape and will render same as ' in browser I'd not recommend doing that.