From 0c0289d5bce973d7fd3f53252f239312cb57a1e8 Mon Sep 17 00:00:00 2001 From: Kieran Brahney Date: Fri, 21 May 2021 12:30:18 +0100 Subject: [PATCH] Add ability to set lib xml parameters --- src/CssToInlineStyles.php | 18 +++++++++++++++++- tests/CssToInlineStylesTest.php | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/CssToInlineStyles.php b/src/CssToInlineStyles.php index 3268c5f..8c25cb4 100644 --- a/src/CssToInlineStyles.php +++ b/src/CssToInlineStyles.php @@ -13,6 +13,9 @@ class CssToInlineStyles { private $cssConverter; + /** @var int */ + private $libXmlOptions = 0; + public function __construct() { if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) { @@ -20,6 +23,19 @@ public function __construct() } } + /** + * Set DOMDocument LibXML parameters. + * + * @param int $options + * @return self + */ + public function setLibXmlOptions($options) + { + $this->libXmlOptions = $options; + + return $this; + } + /** * Will inline the $css into the given $html * @@ -113,7 +129,7 @@ protected function createDomDocumentFromHtml($html) { $document = new \DOMDocument('1.0', 'UTF-8'); $internalErrors = libxml_use_internal_errors(true); - $document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); + $document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), $this->libXmlOptions); libxml_use_internal_errors($internalErrors); $document->formatOutput = true; diff --git a/tests/CssToInlineStylesTest.php b/tests/CssToInlineStylesTest.php index 4fbd1b3..9c7899a 100644 --- a/tests/CssToInlineStylesTest.php +++ b/tests/CssToInlineStylesTest.php @@ -261,6 +261,23 @@ public function testSelfClosingTags() $this->assertCorrectConversion($expected, $html, $css); } + public function testSetLibXmlOptions() + { + $dtd = ''; + $expected = '

'; + + $this->assertSame( + $dtd . "\n" . $expected, + $this->cssToInlineStyles->convert('

') + ); + + $this->cssToInlineStyles->setLibXmlOptions(LIBXML_HTML_NODEFDTD); + $this->assertSame( + "\n" . $expected, + $this->cssToInlineStyles->convert('

') + ); + } + private function assertCorrectConversion($expected, $html, $css = null) { $this->assertEquals(