Здравствуйте, по какой-то причине HTMLPurifier (v4.10) добавляет тег абзаца вокруг тегов img и удаляет атрибут стиля для них.
Это мой конфиг.
$config->set("AutoFormat.AutoParagraph", false);
$config->set("AutoFormat.RemoveEmpty.Predicate", ['colgroup' => [], 'th' => [], 'td' => [], 'iframe' => ['src'], 'div' => ['class']]);
$config->set("AutoFormat.RemoveEmpty", true);
$config->set("Core.NormalizeNewlines", true);
$config->set('HTML.Allowed', "*[style|class],h1, h2, h3, h4, h5, h6, hr, pre, code, blockquote, table, tr, td, th, thead, tbody, strong, em, b, i, u, s, span, a, p, br, ul, ol, li, img[src], div");
Это пример того, что я очищаю.
<div class="some_class">
<img src='http://example.com/test.jpg' style='position:absolute;top:0px;left:0px;width:100%'>
</div>
В итоге он выплевывает:
<div class="some_class">
<p><img src='http://example.com/test.jpg' alt='test' /></p>
</div>
Также установка Core.RemoveInvalidImg true/false ничего не делает. То же самое происходит.
Изменить:
Here is full code sample:
<?php
require 'vendor/autoload.php';
$my_text = file_get_contents('test3.md');
preg_match_all('/[<]style[^>]*[>]([^<]+)[<]\/style[>]/', $my_text, $matches, PREG_OFFSET_CAPTURE);
echo '<!DOCTYPE html><html><head><style type="text/css">';
for($i = 0; $i < count($matches[1]); $i++) {
echo $matches[1][$i][0]."\n\n";
}
echo '</style></head><body>';
$config = HTMLPurifier_Config::createDefault();
$config->set("AutoFormat.AutoParagraph", false);
$config->set("AutoFormat.RemoveEmpty.Predicate", ['colgroup' => [], 'th' => [], 'td' => [], 'iframe' => ['src'], 'div' => ['class']]);
$config->set("AutoFormat.RemoveEmpty", true);
$config->set("Core.NormalizeNewlines", true);
$config->set('HTML.Allowed', "*[style|class],h1, h2, h3, h4, h5, h6, hr, pre, code, blockquote, table, tr, td, th, thead, tbody, strong, em, b, i, u, s, span, a, p, br, ul, ol, li, img[src], div");
$config->set('HTML.Allowed', "*[style|class],h1, h2, h3, h4, h5, h6, hr, pre, code, blockquote, table, tr, td, th, thead, tbody, strong, em, b, i, u, s, span, a, p, br, ul, ol, li, img[src], div");
$Purifier = new HTMLPurifier($config);
echo $Purifier->purify($page);
?>
</body></html>
$page
не установлен. :( Если я заменю его на$my_text
, он все равно не добавит<p>
к<img>
. Поскольку я не могу воспроизвести вашу проблему даже с вашим кодом, я в растерянности. Я рекомендуем пройтись поphp.ini
и поиграться с настройками, посмотреть, не вызывает ли что-то там вашу проблему. - person pinkgothic   schedule 27.07.2019