Skip to content

Instantly share code, notes, and snippets.

@AntonioPrimera
Created January 11, 2018 17:51
Show Gist options
  • Save AntonioPrimera/ab8646567bac2b81c09298a83bc50532 to your computer and use it in GitHub Desktop.
Save AntonioPrimera/ab8646567bac2b81c09298a83bc50532 to your computer and use it in GitHub Desktop.
HtmlPurifier - Trix Editor
public function htmlPurifier()
{
$config = \HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'basecamp-trix-editor');
$config->set('HTML.DefinitionRev', 1);
if ($def = $config->maybeGetRawHTMLDefinition()) {
$def->addElement('figure', 'Block', 'Flow', 'Common');
$def->addAttribute('figure', 'class', 'Text');
$def->addElement('figcaption', 'Block', 'Flow', 'Common');
$def->addAttribute('figcaption', 'class', 'Text');
$def->addAttribute('figcaption', 'data-trix-placeholder', 'Text');
$def->addAttribute('a', 'rel', 'Text');
$def->addAttribute('a', 'tabindex', 'Text');
$def->addAttribute('a', 'contenteditable', 'Enum#true,false');
$def->addAttribute('a', 'data-trix-attachment', 'Text');
$def->addAttribute('a', 'data-trix-content-type', 'Text');
$def->addAttribute('a', 'data-trix-id', 'Number');
$def->addElement('span', 'Block', 'Flow', 'Common');
$def->addAttribute('span', 'data-trix-cursor-target', 'Enum#right,left');
$def->addAttribute('span', 'data-trix-serialize', 'Enum#true,false');
$def->addAttribute('img', 'data-trix-mutable', 'Enum#true,false');
$def->addAttribute('img', 'data-trix-store-key', 'Text');
}
$purifier = new \HTMLPurifier($config);
$original = file_get_contents(base_path('/reference/trix-output-original.html'));
$processed = $purifier->purify($original);
return view('Home.htmlPurifier', compact('original', 'processed'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment