Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: is it possible to have self-closing tags? #81

Open
etiennerunge opened this issue Apr 23, 2024 · 5 comments
Open

Question: is it possible to have self-closing tags? #81

etiennerunge opened this issue Apr 23, 2024 · 5 comments
Labels

Comments

@etiennerunge
Copy link

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

@xemlock
Copy link
Owner

xemlock commented Apr 23, 2024

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.

@etiennerunge
Copy link
Author

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 ?

@etiennerunge
Copy link
Author

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! :) :)

@xemlock
Copy link
Owner

xemlock commented Apr 24, 2024

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.

@etiennerunge
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants