Masterminds / html5-php

An HTML5 parser and serializer for PHP.
http://masterminds.github.io/html5-php/
Other
1.55k stars 114 forks source link

img inside a figure will create an opening and closing img tag #222

Closed creutz closed 1 year ago

creutz commented 1 year ago

<figure><img src=""></figure> will create <figure><img src=""></img></figure>. Is this by design?

goetas commented 1 year ago

I have tried and i'm not able to reproduce it. for me <figure><img src=""></figure> it is serialized correctly as <figure><img src=""/></figure>.

can you please provide a failing test case?

stof commented 1 year ago

I cannot reproduce this when using $html5->saveHTML($dom);

Using $dom->saveHTML() indeed produces <figure><img src=""></img></figure>, but this is because it does not use the HTML5 renderer. The built-in PHP HTML renderer uses XHTML, not HTML5.

creutz commented 1 year ago

Thanks for replying so quickly. I had this code:

$html5 = new HTML5();
$doc = $html5->loadHTML($string);
echo $html5->saveHTML($doc);

where $string is some HTML without doctype, html, body.

but this is because it does not use the HTML5 renderer. The built-in PHP HTML renderer uses XHTML, not HTML5.

But I used the methods described in the documentation?

I tried ivopetkov/html5-dom-document-php and that worked better for me. I dont know if this here is a bug or if I am too stupid to use this lib.

ohader commented 1 year ago

I also was not able to reproduce this behavior.

<?php
require 'vendor/autoload.php';
$html5 = new Masterminds\HTML5();
$string = '<figure><img src=""></figure>';
$doc = $html5->loadHTML($string);
echo $html5->saveHTML($doc);

outputs

<!DOCTYPE html>
<html><figure><img src=""></figure></html>

By using DOMDocumentFragment it is possible to focus on the submitted fragment only.

<?php
require 'vendor/autoload.php';
$html5 = new Masterminds\HTML5();
$string = '<figure><img src=""></figure>';
$fragment = $html5->parseFragment($string);
echo $html5->saveHTML($fragment);

outputs

<figure><img src=""></figure>
goetas commented 1 year ago

closing as "can not reproduce"