How to output raw content to HTML attribute value?

The template:

{{ $test := "1+1" }}

<tag attr="{{ $test }}" />
<tag attr="{{ $test | safeHTML }}" />
<tag attr="{{ $test | safeHTMLAttr }}" />

The output:

<tag attr="1&#43;1" />
<tag attr="1&#43;1" />
<tag attr="1&#43;1" />

These are not the results I expected

I expected result is:

<tag attr="1+1" />

Now, this problem causes all base64 encoded attribute values to be output incorrectly, such as integrity

<sctipt src="http://localhost:1313/js/common.59942397d9c3fb2ecf1c5638c3ce872bfbec0413f5b53dbfc8db257ec5a6bf8c.js" integrity="sha256-WZQjl9nD&#43;y7PHFY4w86HK/vsBBP1tT2/yNslfsWmv4w="></script>

what should I do?

Hugo/v0.73.0

<tag {{ printf `attr="%s"` $test | safeHTMLAttr }} />
2 Likes