Skip to content

Instantly share code, notes, and snippets.

@yookoala
Last active November 17, 2018 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yookoala/a10caeb62f0576ce926f76acd8b7e517 to your computer and use it in GitHub Desktop.
Save yookoala/a10caeb62f0576ce926f76acd8b7e517 to your computer and use it in GitHub Desktop.
HTMLPurifier add support to details and summary
{
"name": "yookoala/htmlpurifier-example",
"authors": [
{
"name": "Koala Yeung",
"email": "koalay at gmail dot com"
}
],
"require": {
"ezyang/htmlpurifier": "^4.10"
}
}
<?php
require_once __DIR__ . '/vendor/autoload.php';
ob_start();
?>
<html>
<head>
<title>HTMLPurifier test</title>
</head>
<body>
<div>
<p>Hello world</p>
</div>
<details open="true">
<summary>Hello content</summary>
<div>
<p>Something new</p>
</div>
</details>
</body>
</html>
<?php
$contents = ob_get_clean();
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', 'div,p,ul[style],ol,li,details[open],summary');
$def = $config->getHTMLDefinition(true);
$def->addElement('details', 'Block', 'Flow', 'Common', [
'open' => 'Bool#open',
]);
$def->addElement('summary', 'Inline', 'Inline', 'Common');
$purifier = new HTMLPurifier($config);
echo $purifier->purify($contents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment