Skip to content

Instantly share code, notes, and snippets.

@natanfelles
Created February 20, 2020 23:12
Show Gist options
  • Save natanfelles/c26e559bb1cdf28e09039f6723e3af22 to your computer and use it in GitHub Desktop.
Save natanfelles/c26e559bb1cdf28e09039f6723e3af22 to your computer and use it in GitHub Desktop.
HTML Purifier Helper for TinyMCE
<?php
/**
* @param string $content
*
* @return string
*/
function purify(string $content) : string
{
$config = \HTMLPurifier_Config::createDefault();
$config->set('HTML.AllowedElements', [
'a',
'b',
'blockquote',
'br',
'code',
'em',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'hr',
'i',
'img',
'li',
'ol',
'p',
'pre',
's',
'span',
'strong',
'sub',
'sup',
'table',
'tbody',
'td',
'th',
'thead',
'tr',
'u',
'ul',
]);
$config->set('CSS.AllowedProperties', [
'color',
'background-color',
'font-size',
'font-weight',
'padding-left',
'text-align',
'text-decoration',
]);
$config->set('Attr.AllowedClasses', [
'alert',
'alert-danger',
'alert-info',
'alert-success',
'alert-warning',
'blockquote',
'blockquote-footer',
'img-responsive',
'table',
'table-bordered',
'table-condensed',
'table-hover',
'table-responsive',
'table-striped',
]);
$config->set('Attr.AllowedFrameTargets', [
'_blank',
]);
$config->set('HTML.AllowedAttributes', [
'a.href',
'a.target',
'code.class',
'img.class',
'img.src',
'ol.start',
'p.class',
'p.style',
'span.style',
'table.class',
'td.align',
'th.align',
]);
$config->set('URI.AllowedSchemes', [
'http' => true,
'https' => true,
]);
return (new \HTMLPurifier())->purify($content, $config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment