Reputation: 35
I have this string
<ol>
<span class="ql-ui" contenteditable="false"></span>
<li data-list="bullet">AAA</li>
</ol>
and this is my HTMLPurifier settings
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'b,i,strong,em,ol,ul,li,br');
$config->set('AutoFormat.Linkify', false);
$purifier = new HTMLPurifier($config);
After purify the result will became like this
<ol>
<li>1</li>
</ol>
How can I retain the data-list="bullet"
I don't want it to be removed after purify.
Upvotes: 0
Views: 86
Reputation: 13
To retain the data-list="bullet" attribute on <li>
elements after purification with HTMLPurifier, you need to adjust your configuration to allow this specific attribute. HTMLPurifier provides a way to customize which tags and attributes are allowed through its configuration settings.
By default, HTMLPurifier strips out any attributes that are not explicitly allowed by its configuration. To keep the data-list attribute on <li>
elements, you should add this attribute to the list of allowed attributes for the <li>
tag. Here's how you can do it:
$config->set('HTML.Allowed', 'b,i,strong,em,ol,ul,li,br');
$config->set('HTML.DefinitionRev', 1);
// Add a custom definition for allowing 'data-list' attribute on 'li' elements
if ($def = $config->maybeGetRawHTMLDefinition()) {
$li = $def->addBlankElement('li');
$li->attr['data-list'] = 'Enum#bullet'; // Allow 'bullet' as a value for 'data-list'
}
$config->set('AutoFormat.Linkify', false);
$purifier = new HTMLPurifier($config);
In this code:
$config->set('HTML.DefinitionRev', 1); is used to set a revision for the custom HTML definition, ensuring that your changes are applied. This is useful for caching purposes; you should increment this value if you make further changes to the configuration.
$def->addBlankElement('li'); is used to add or modify the definition for the <li>
element.
$li->attr['data-list'] = 'Enum#bullet'; adds the data-list attribute to the allowed list for <li>
elements, specifying that bullet is an allowed value. If you expect other values besides bullet, you can adjust this part accordingly.
By customizing the HTMLPurifier configuration in this way, you're instructing HTMLPurifier to allow the data-list attribute with a specific value (bullet) on <li>
elements, which should prevent it from being removed during the purification process.
Upvotes: -2