title: DOM parser that allows HTML5-style </ in <script> tag
body: <p><strong>Update</strong>: <code>html5lib</code> (bottom of question) seems to get close, I just need to improve my understanding of how it's used.</p>
  
  <p>I am attempting to find an HTML5-compatible DOM parser for PHP 5.3. In particular, I need to access the following HTML-like CDATA within a script tag:</p>
  
  <pre><code>&lt;script type="text/x-jquery-tmpl" id="foo"&gt;
      &lt;table&gt;&lt;tr&gt;&lt;td&gt;${name}&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>Most parsers will end parsing prematurely because HTML 4.01 <a href="http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#notes-specifying-data">ends script tag parsing</a> when it finds ETAGO (<code>&lt;/</code>) inside a <code>&lt;script&gt;</code> tag. However, HTML5 <a href="http://www.w3.org/TR/2010/WD-html5-20100624/scripting-1.html#restrictions-for-contents-of-script-elements">allows for <code>&lt;/</code></a> before <code>&lt;/script&gt;</code>. All of the parsers I have tried so far have either failed, or they are so poorly documented that I haven't figured out if they work or not.</p>
  
  <p>My requirements:</p>
  
  <ol>
  <li>Real parser, not regex hacks.</li>
  <li>Ability to load full pages or HTML fragments.</li>
  <li>Ability to pull script <em>contents</em> back out, selecting by the tag's id attribute.</li>
  </ol>
  
  <p>Input:</p>
  
  <pre><code>&lt;script id="foo"&gt;&lt;td&gt;bar&lt;/td&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Example of failing output (no closing <code>&lt;/td&gt;</code>):</p>
  
  <pre><code>&lt;script id="foo"&gt;&lt;td&gt;bar&lt;/script&gt;
  </code></pre>
  
  <p>Some parsers and their results:</p>
  
  <p><br></p>
  
  <h2><a href="http://php.net/manual/en/class.domdocument.php">DOMDocument</a> (fails)</h2>
  
  <p>Source:</p>
  
  <pre><code>&lt;?php
  
  header('Content-type: text/plain');
  $d = new DOMDocument;
  $d-&gt;loadHTML('&lt;script id="foo"&gt;&lt;td&gt;bar&lt;/td&gt;&lt;/script&gt;');
  echo $d-&gt;saveHTML();
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Warning: DOMDocument::loadHTML(): Unexpected end tag : td in Entity, line: 1 in /home/adam/public_html/2010/10/26/dom.php on line 5
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
  &lt;html&gt;&lt;head&gt;&lt;script id="foo"&gt;&lt;td&gt;bar&lt;/script&gt;&lt;/head&gt;&lt;/html&gt;
  </code></pre>
  
  <p><br></p>
  
  <h2><a href="http://fluentdom.github.com/">FluentDOM</a> (fails)</h2>
  
  <p>Source:</p>
  
  <pre><code>&lt;?php
  
  header('Content-type: text/plain');
  require_once 'FluentDOM/src/FluentDOM.php';
  $html = "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;script id='foo'&gt;&lt;td&gt;&lt;/td&gt;&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
  echo FluentDOM($html, 'text/html');
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
  &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;script id="foo"&gt;&lt;td&gt;&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p><br></p>
  
  <h2><a href="http://code.google.com/p/phpquery/">phpQuery</a> (fails)</h2>
  
  <p>Source:</p>
  
  <pre><code>&lt;?php
  
  header('Content-type: text/plain');
  
  require_once 'phpQuery.php';
  
  phpQuery::newDocumentHTML(&lt;&lt;&lt;EOF
  &lt;script type="text/x-jquery-tmpl" id="foo"&gt;
  &lt;td&gt;test&lt;/td&gt;
  &lt;/script&gt;
  EOF
  );
  </code></pre>
  
  <p>echo (string)pq('#foo');</p>
  
  <p>Output:</p>
  
  <pre><code>&lt;script type="text/x-jquery-tmpl" id="foo"&gt;
  &lt;td&gt;test
  &lt;/script&gt;
  </code></pre>
  
  <p><br></p>
  
  <h2><a href="http://code.google.com/p/html5lib/">html5lib</a> (passes)</h2>
  
  <p>Possibly promising. Can I get at the contents of the <code>script#foo</code> tag?</p>
  
  <p>Source:</p>
  
  <pre><code>&lt;?php
  
  header('Content-type: text/plain');
  
  include 'HTML5/Parser.php';
  
  $html = "&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;script id='foo'&gt;&lt;td&gt;&lt;/td&gt;&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;";
  $d = HTML5_Parser::parse($html);
  
  echo $d-&gt;saveHTML();
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;script id="foo"&gt;&lt;td&gt;&lt;/td&gt;&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>