|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-01-23 11:24 UTC] rrichards@php.net
[2012-05-20 08:39 UTC] it dot registrations at symphony-group dot co dot uk
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 08:00:01 2025 UTC |
Description: ------------ From what I understand, DOMDocument::validateOnParse() = TRUE & DOMDocument::validate() should return the same list of errors for a given document. But when dealing with HTML code, validate() seems to fail to deal with the DTD correctly. Therefore, using validate() & validateOnParse gives different results. I think that in the case of validate(), PHP calls libxml with options that make it think it will be dealing with XML code and thus, an XML DTD. So, once libxml loads the HTML DTD, it fails to parse it correctly and returns a bunch of errors. (HTML & XML DTDs are similar, except that HTML ones allow for some more constructs like the '&' operator in ELEMENT declarations) Reproduce code: --------------- <?php /* Note: removing the system identifier doesn't change the result, * except that "errors" in the DTD are caught immediately. * (My guess would be that libxml tries to fetch the DTD from the * system identifier instead of using a catalog for resolution) */ $markup = <<<HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>Foo</title></head> <body><p>Bar</p></body> </html> HTML; header('Content-Type: text/plain'); libxml_use_internal_errors(TRUE); // First, using DOMDocument::validateOnParse. libxml_clear_errors(); $dd1 = new DOMDocument(); $dd1->validateOnParse = TRUE; echo "Using validateOnParse:\n"; $dd1->loadHTML($markup); var_dump(libxml_get_errors()); echo "\n\n"; // Now, using DOMDocument::validate() instead. libxml_clear_errors(); $dd2 = new DOMDocument(); $dd2->validateOnParse = FALSE; echo "Using validate():\n"; $dd2->loadHTML($markup); $dd2->validate(); var_dump(libxml_get_errors()); ?> Expected result: ---------------- Using validateOnParse: array(0) { } Using validate(): array(0) { } Actual result: -------------- Using validateOnParse: array(0) { } Using validate(): array(3) { [0]=> object(LibXMLError)#3 (6) { ["level"]=> int(3) ["code"]=> int(37) ["column"]=> int(1) ["message"]=> string(55) "xmlParseEntityDecl: entity HTML.Version not terminated " ["file"]=> string(36) "http://www.w3.org/TR/html4/loose.dtd" ["line"]=> int(31) } [1]=> object(LibXMLError)#4 (6) { ["level"]=> int(3) ["code"]=> int(60) ["column"]=> int(1) ["message"]=> string(37) "Content error in the external subset " ["file"]=> string(36) "http://www.w3.org/TR/html4/loose.dtd" ["line"]=> int(31) } [2]=> object(LibXMLError)#5 (6) { ["level"]=> int(2) ["code"]=> int(517) ["column"]=> int(0) ["message"]=> string(74) "Could not load the external subset "http://www.w3.org/TR/html4/loose.dtd" " ["file"]=> string(0) "" ["line"]=> int(0) } }