DOM-Parser, der das HTML5-artige </ in <script> -Tag @ ermöglic
Aktualisiere: html5lib
(Ende der Frage) scheint sich zu nähern, ich muss nur mein Verständnis dafür verbessern, wie es verwendet wird.
Ich versuche, einen HTML5-kompatiblen DOM-Parser für PHP 5.3 zu finden. Insbesondere muss ich innerhalb eines Skript-Tags auf die folgenden HTML-ähnlichen CDATA zugreifen:
<script type="text/x-jquery-tmpl" id="foo">
<table><tr><td>${name}</td></tr></table>
</script>
Die meisten Parser beenden das Parsen vorzeitig, da HTML 4.01ends script tag parsing wenn es ETAGO findet </
) in einem<script>
Etikett. HTML5ermöglicht</
Vor</script>
. Alle Parser, die ich bisher ausprobiert habe, sind entweder fehlgeschlagen oder sie sind so schlecht dokumentiert, dass ich nicht herausgefunden habe, ob sie funktionieren oder nicht.
Meine Anforderungen:
Real Parser, keine Regex-Hacks.Möglichkeit, ganze Seiten oder HTML-Fragmente zu laden. Fähigkeit, Skript zu ziehenInhal Zurück, Auswahl anhand des ID-Attributs des Tags.Eingang
<script id="foo"><td>bar</td></script>
Beispiel für fehlgeschlagene Ausgabe (kein Schließen</td>
):
<script id="foo"><td>bar</script>
Einige Parser und ihre Ergebnisse:
Quelle
<?php
header('Content-type: text/plain');
$d = new DOMDocument;
$d->loadHTML('<script id="foo"><td>bar</td></script>');
echo $d->saveHTML();
Ausgabe
Warning: DOMDocument::loadHTML(): Unexpected end tag : td in Entity, line: 1 in /home/adam/public_html/2010/10/26/dom.php on line 5
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><script id="foo"><td>bar</script></head></html>
Quelle
<?php
header('Content-type: text/plain');
require_once 'FluentDOM/src/FluentDOM.php';
$html = "<html><head></head><body><script id='foo'><td></td></script></body></html>";
echo FluentDOM($html, 'text/html');
Ausgabe
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head></head><body><script id="foo"><td></script></body></html>
Quelle
<?php
header('Content-type: text/plain');
require_once 'phpQuery.php';
phpQuery::newDocumentHTML(<<<EOF
<script type="text/x-jquery-tmpl" id="foo">
<td>test</td>
</script>
EOF
);
echo (string) pq ('# foo');
Ausgabe
<script type="text/x-jquery-tmpl" id="foo">
<td>test
</script>
öglicherweise vielversprechend. Kann ich den Inhalt des @ bekommscript#foo
Etikett
Quelle
<?php
header('Content-type: text/plain');
include 'HTML5/Parser.php';
$html = "<!DOCTYPE html><html><head></head><body><script id='foo'><td></td></script></body></html>";
$d = HTML5_Parser::parse($html);
echo $d->saveHTML();
Ausgabe
<html><head></head><body><script id="foo"><td></td></script></body></html>