title: How to parse invalid HTML with Perl?
body: <p>I maintain a database of articles with HTML formatting. Unfortunately the editors who wrote articles didn't know proper HTML, so they often have written stuff like:</p>
  
  <pre><code>&lt;div class="highlight"&gt;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Note that ...&lt;/p&gt;&lt;/html&gt;&lt;/div&gt;
  </code></pre>
  
  <p>I tried using <code>HTML::TreeBuilder</code> to parse this HTML but after parsing it and dumping the resulting tree, all the elements between <code>&lt;div class="highlight"&gt;...&lt;/div&gt;</code> are gone. I'm left with just <code>&lt;div class="highlight"&gt;&lt;/div&gt;</code>.</p>
  
  <p>The editors often have also done things like:</p>
  
  <pre><code>&lt;div class="article"&gt;&lt;style&gt;@font-face {   font-family: "Cambria"; }&lt;/style&gt;Article starts here&lt;/div&gt;
  </code></pre>
  
  <p>Parsing this with <code>HTML::TreeBuilder</code> results in empty <code>&lt;div class="article"&gt;&lt;/div&gt;</code> again.</p>
  
  <p>Any ideas how to approach this broken HTML and actually make sense out of it?</p>