php-htmldiff icon indicating copy to clipboard operation
php-htmldiff copied to clipboard

no iframes in result

Open kamil-malinski opened this issue 3 years ago • 2 comments

Hi!

I have a html that contains iframes, for example a youtube-embed-code. HtmlDiff filters the iframes out when comparing (the iframes are missing in the result).

Is there a reason for that and can it be avoided?

Cheers

Kamil

kamil-malinski avatar Aug 31 '22 12:08 kamil-malinski

Hello! Astonishingly, I was just visiting this page today for the same reason. So I dug into the code.

The current code starts by calling HtmlPurifier, and uses that to clean up the html of the input values, which is removing the values we want. :(

However, we can override the HtmlPurifier settings to allow iframes around youtube to work. Part of this code is copied from an answer at https://stackoverflow.com/questions/4739284/htmlpurifier-iframe-vimeo-and-youtube-video

		$htmlDiff = new Caxy\HtmlDiff\HtmlDiff($old, $new);

		$HtmlPurifierConfig = \HTMLPurifier_Config::createDefault();
		$HtmlPurifierConfig->set('HTML.SafeIframe', true);
		$HtmlPurifierConfig->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo
		$htmlDiff->setHTMLPurifierConfig($HtmlPurifierConfig);

		return $htmlDiff->build();

Once I changed my code to the above, the iframe (and youtube) came through fine.

Edit: Changed how markdown is being applied to the code snippet for better readability.

jcfiala avatar Sep 01 '22 18:09 jcfiala

Hmm, it seems like HTMLPurifier is doing a lot more than just "fixing" invalid HTML (which is why it's used in this library), it shouldn't be trying to strip out anything in particular... it's meant to address invalid HTML to avoid parsing errors (and potentially make it more uniform).

As @jcfiala mentioned, adjusting the config that way is a great solution. There is also the option to disable the HTML purifier altogether, just note that it's possible to encounter errors being thrown if invalid HTML is passed in

jschroed91 avatar Nov 06 '23 01:11 jschroed91