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

no iframes in result #112

Open
kamil-malinski opened this issue Aug 31, 2022 · 2 comments
Open

no iframes in result #112

kamil-malinski opened this issue Aug 31, 2022 · 2 comments

Comments

@kamil-malinski
Copy link

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 kamil-malinski changed the title no iframes is result no iframes in result Aug 31, 2022
@jcfiala
Copy link

jcfiala commented Sep 1, 2022

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.

@jschroed91
Copy link
Member

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

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

No branches or pull requests

3 participants