diff --git a/.gitignore b/.gitignore index 183037e..318cfe6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ vendor/ /demo/bower_components /demo/node_modules .DS_Store -.idea \ No newline at end of file +.idea +.phpunit.result.cache \ No newline at end of file diff --git a/lib/Caxy/HtmlDiff/ListDiffLines.php b/lib/Caxy/HtmlDiff/ListDiffLines.php index cfc68c7..ea3cb8c 100644 --- a/lib/Caxy/HtmlDiff/ListDiffLines.php +++ b/lib/Caxy/HtmlDiff/ListDiffLines.php @@ -87,12 +87,19 @@ protected function listByLines(string $old, string $new) : string $new = mb_encode_numericentity($new, [0x80, 0x10FFFF, 0, ~0], 'UTF-8'); $old = mb_encode_numericentity($old, [0x80, 0x10FFFF, 0, ~0], 'UTF-8'); + // Suppress libxml errors to avoid warnings from malformed HTML fragments + $previousLibxmlUseInternalErrors = libxml_use_internal_errors(true); + $newDom = new DOMDocument(); $newDom->loadHTML($new); $oldDom = new DOMDocument(); $oldDom->loadHTML($old); + // Restore previous libxml error handling state + libxml_clear_errors(); + libxml_use_internal_errors($previousLibxmlUseInternalErrors); + $newListNode = $this->findListNode($newDom); $oldListNode = $this->findListNode($oldDom); @@ -405,9 +412,16 @@ private function setInnerHtml(DOMNode $node, string $html) : void $node->nodeValue = ''; + // Suppress libxml errors to avoid warnings from malformed HTML fragments + $previousLibxmlUseInternalErrors = libxml_use_internal_errors(true); + $bufferDom = new DOMDocument('1.0', 'UTF-8'); $bufferDom->loadHTML($html); + // Restore previous libxml error handling state + libxml_clear_errors(); + libxml_use_internal_errors($previousLibxmlUseInternalErrors); + $bodyNode = $bufferDom->getElementsByTagName('body')->item(0); foreach ($bodyNode->childNodes as $childNode) { diff --git a/lib/Caxy/HtmlDiff/Table/TableDiff.php b/lib/Caxy/HtmlDiff/Table/TableDiff.php index ed46d94..4ff899f 100644 --- a/lib/Caxy/HtmlDiff/Table/TableDiff.php +++ b/lib/Caxy/HtmlDiff/Table/TableDiff.php @@ -626,9 +626,16 @@ protected function buildTableDoms() */ protected function createDocumentWithHtml($text) { + // Suppress libxml errors to avoid warnings from malformed HTML fragments + $previousLibxmlUseInternalErrors = libxml_use_internal_errors(true); + $dom = new \DOMDocument(); $dom->loadHTML(htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1//IGNORE', htmlentities($text, ENT_COMPAT, 'UTF-8')), ENT_QUOTES)); + // Restore previous libxml error handling state + libxml_clear_errors(); + libxml_use_internal_errors($previousLibxmlUseInternalErrors); + return $dom; } diff --git a/tests/fixtures/HtmlDiff/nested-list-warning.html b/tests/fixtures/HtmlDiff/nested-list-warning.html new file mode 100644 index 0000000..1a4d590 --- /dev/null +++ b/tests/fixtures/HtmlDiff/nested-list-warning.html @@ -0,0 +1,46 @@ + + + +
    +
  1. +
      +
    1. This sentence.
    2. +
    +
  2. +
  3. : +
      +
    1. Another item
    2. +
    +
  4. +
+
    +
  1. :
  2. +
  3. Saturn
  4. +
+ +
    +
  1. +
      +
    1. This sentence.
    2. +
    +
  2. +
  3. : +
      +
    1. Another item
    2. +
    +
  4. +
  5. : +
      +
    1. Saturn
    2. +
    +
  6. +
+ +
  1. +
      +
    1. This sentence.
    2. +
    +
  2. : +
    1. This sentence.
    2. Another item
  3. : +
    1. Another item
    2. Saturn
  1. :
  2. Saturn