title: PHP domDocument works incorrectly when the node wrapper in figure?
body: <p>I'm trying to add some HTML to all links that contain image. </p>
  
  <p>Basic HTML loaded into dom looks like </p>
  
  <pre><code>&lt;div class='content'&gt;
      &lt;a href="..."&gt;&lt;img src=""&gt;&lt;/a&gt;
  
      &lt;figure&gt;
         &lt;a href="..."&gt;&lt;img src=""&gt;&lt;/a&gt;
         &lt;figcaption&gt;Caption&lt;/figcaption&gt;
      &lt;/figure&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The code: </p>
  
  <pre><code>$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
  $dom = new DOMDocument();
  @$dom-&gt;loadHTML($content);
  
  // Convert Images
  $images = [];
  
  foreach ($dom-&gt;getElementsByTagName('img') as $node) {
      $images[] = $node;
  }
  
  foreach ($images as $node) {    
       $field_html = $dom-&gt;createDocumentFragment(); // create fragment
       $field_html-&gt;appendXML('&lt;span&gt;11&lt;/span&gt;'); // create fragment
       $node-&gt;parentNode-&gt;appendChild($field_html);  
  
  }
  
  $newHtml = preg_replace('/^&lt;!DOCTYPE.+?&gt;/', '', str_replace( array('&lt;html&gt;', '&lt;/html&gt;', '&lt;body&gt;', '&lt;/body&gt;'), array('', '', '', ''), $dom-&gt;saveHTML()));
  return $newHtml; 
  </code></pre>
  
  <p>So when it's a regular link with img, it produces correct output:</p>
  
  <p><code>&lt;a href="..."&gt;&lt;img src=""&gt;&lt;span&gt;11&lt;/span&gt;&lt;/a&gt;</code></p>
  
  <p>But when it's a figure, output is very strange — link is duplicated and inserted into <code>figcaption</code>: </p>
  
  <pre><code>&lt;figure&gt;
      &lt;a href="..."&gt;&lt;img src=""&gt;&lt;/a&gt;
      &lt;figcaption&gt;Caption &lt;a href="..."&gt;&lt;span&gt;11&lt;/span&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
  </code></pre>
  
  <p>Is that because DOMDocument doesn't understand <code>figure</code> thing?  </p>