Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't HTML working in Bootstrap v5 Popover?

When I add HTML into my Bootstrap v5 popover, it does not display the HTML in the popover. I believe that I have all of the correct options passed as described in the docs. Almost all of the results which I've gotten from searches to try and understand this issue deal with Bootstrap v4 and v3, so they aren't of use to me.

The popover all of the way on the right should have the switch affordance ("Toggle") appear below "Hello World" in the popover. The HTML code is present.

Bootstrap v5 HTML Popover Not Working

The code of the above tests centers around these 3 affordances:

<!-- Popover Works -->
<span class="fs-3 actions-link-style ai-more-horizontal" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="top" title="Title" data-bs-sanitize="false" data-bs-html="true" data-bs-content="Hello World"></span>

<!-- HTML Switch Works -->
<div class='form-check form-switch'><input type='checkbox' class='form-check-input' id='customSwitch1'><label class='form-check-label' for='customSwitch1'>Toggle</label></div>

<!-- Popover with HTML Switch Does Not Work -->
<span class="fs-3 actions-link-style ai-more-horizontal" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="top" title="Title" data-bs-sanitize="false" data-bs-html="true" data-bs-content="Hello World<br><div class='form-check form-switch'><input type='checkbox' class='form-check-input' id='customSwitch2'><label class='form-check-label' for='customSwitch2'>Toggle</label></div>"></span>

Have I missed something? Please let me know. Thank you!

like image 830
Berklie Avatar asked Oct 16 '25 10:10

Berklie


1 Answers

Ok, reading and re-reading the Bootstrap v5 documentation's fine print finally led me to the solution.

Even though data-bs-sanitize="false" is a valid option that can be passed, per the documentation... the documentation also notes elsewhere that:

Note that for security reasons the sanitize, sanitizeFn, and allowList options cannot be supplied using data attributes.

So... in order for the HTML to execute, because data-bs-sanitize="false" is ignored, one has to manually add non-default values to the Sanitizer whitelist which are used with the HTML code inside the popover. In my case, they were as follows:

var myDefaultAllowList = bootstrap.Tooltip.Default.allowList

// To allow elements
myDefaultAllowList.input = ['type', 'checked']
myDefaultAllowList.label = ['for']

And now... it's working as intended. A lesson in sanitization.

like image 67
Berklie Avatar answered Oct 19 '25 00:10

Berklie