htmlpurifier-html5
htmlpurifier-html5 copied to clipboard
Question: is it possible to have self-closing tags?
Setting HTML.XHTML to true doesn't affect tags, however, it is written in documentation that
[...] in HTML5 it's used for enabling support for namespaced attributes and XML self-closing tags.
I tried to pass this option in Custom HTMLPurifier Class:
namespace App\HtmlPurifier;
final class CustomPurifier extends \HTMLPurifier
{
public function __construct($var)
{
$config = \HTMLPurifier_HTML5Config::createDefault();
$config->set('HTML.XHTML', true);
parent::__construct($config);
}
}
Service :
services:
# HTMLPurifier
App\HtmlPurifier\CustomPurifier:
tags:
- name: exercise.html_purifier
profile: default
exercise_html_purifier.default: '@App\HtmlPurifier\CustomPurifier'
In Controller, I use the right HTMLPurifier, it's ok. But purify() method do not convert html tags to self-closing tags.
$html = "<img src='test.png'/><hr/><br/>";
$purifier->purify($html); // "<img src="test.png" alt="test.png"><hr><br>"
I was expecting that it will return <img src="test.png" alt="test.png"/><hr/><br/>
Maybe I did not understand what HTML.XHTML is supposed to do
Can you provide a minimal reproduction? Are you sure the right config is used?
The following code works as expected (which means that self-closing tags are supported correctly):
<?php
require 'vendor/autoload.php';
$config = \HTMLPurifier_HTML5Config::createDefault();
$config->set('HTML.XHTML', true);
$purifier = new \HTMLPurifier($config);
$html = "<img src='test.png'/><hr/><br/>";
echo $purifier->purify($html);
// <img src="test.png" alt="test.png" /><hr /><br />
What does get_class($purifier->config) returns in your code? It should be HTMLPurifier_HTML5Config in order to work correctly.
Many thanks for your fast feedback. I greatly appreciate your help.
I test with your code in my Symfony application, in controller.
$config = \HTMLPurifier_HTML5Config::createDefault();
$config->set('HTML.XHTML', true);
$purifier = new \HTMLPurifier($config);
dump(get_class($purifier->config)); // HTMLPurifier_HTML5Config
dump($config->get('HTML.XHTML')); // true
$html = "<img src='test.png'/><hr/><br/>";
dump($purifier->purify($html)); // <img src="test.png" alt="test.png"><hr><br>
It returns html5 with no self-closing tags.
I forgot to mention this Symfony Application uses HTMLPurifierBundle
HTML5 Doctype registered by this repository contains xml variable, set to false. Should it be turned to true to format HTML with self closing tags ?
Ok, I think I found the problem. My project uses Composer to install dependency. And documentation says that HTML.XHTML parameter is available since v0.1.12 (Version added: 0.1.12)
But Composer does not found this tagged version because it doesn't exist. There's no 0.1.12 version and composer install 0.1.11 instead.
I switch your repository to dev-master version and it works!! :)
Do you think you could create a new release tagged v0.1.12 ?
Many thanks for your help! :) :)
Yes, exactly! I was testing against master branch, that is why it was working fine for me, and wasn't working on latest version pulled from composer.
I (still) need to make some adjustments before publishing 0.1.12 version.
Do you need help ? The project is very complex, and I don't know if I could help you. But it seems that many things are already implemented (html5 input types seems to work) Maybe can I try to make pull request. If I refer to this list: https://github.com/xemlock/htmlpurifier-html5/projects/1, I just don't understand what "Add script for auto-generating input types lookup" means. And maybe some tests need to be written