Bug #72288 [Nab]: Lines longer than 1000 characters break DOMDocument::loadHTML()

From: Date: Fri, 12 Mar 2021 12:34:12 +0000
Subject: Bug #72288 [Nab]: Lines longer than 1000 characters break DOMDocument::loadHTML()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232672@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72288&edit=1 ID: 72288 Updated by: cmb@php.net Reported by: maciej at klepaczewski dot com Summary: Lines longer than 1000 characters break DOMDocument::loadHTML() Status: Not a bug Type: Bug Package: DOM XML related Operating System: Windows, Linux PHP Version: 5.6.22 Assigned To: cmb Block user comment: N Private report: N New Comment: To clarify: this is not about the line length, but rather about the amount of whitespace in the <head> element. Previous Comments: ------------------------------------------------------------------------ [2021-03-12 12:32:04] cmb@php.net This problem is not in PHP nor its usage of libxml2, but rather in libxml2 itself. During parsing, there is a buffer of 1000 chars[1]. If this is filled, apparently libxml2 assumes CDATA content in the head element (instead of ignoring the whitespace), which is not allowed[2], and so a <p> is implied, which breaks the DOM. You can see that when you inspect $y->saveHTML(). Consider to bring that up with libxml2. [1] <https://github.com/GNOME/libxml2/blob/v2.9.10/HTMLparser.c#L51> [2] <https://github.com/GNOME/libxml2/blob/v2.9.10/HTMLparser.c#L1147-L1158> ------------------------------------------------------------------------ [2016-05-31 20:27:44] cmb@php.net I can reproduce this behavior. Might be an libxml issue. ------------------------------------------------------------------------ [2016-05-30 09:28:36] maciej at klepaczewski dot com One thing I forgot to mention is that the <body> and its attributes are replaced with empty <body>. So if original body has <body class="page"> then after the warning it's replaced with vanilla <body> tag. Improved test script showing this behavior: ----- <?php error_reporting(E_ALL); $html = <<<HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Test</title> <!--placeholder--> </head> <body class="page"> </body> </html> HTML; echo "==== < 1000 spaces in head===\n"; $x = new DOMDocument(); $x->loadHTML(str_replace('<!--placeholder-->', str_repeat(' ', 995), $html)); echo $x->saveHTML(); echo "==== 1000 spaces in head===\n"; $y = new DOMDocument(); $y->loadHTML(str_replace('<!--placeholder-->', str_repeat(' ', 1000), $html)); echo $y->saveHTML(); ------------------------------------------------------------------------ [2016-05-30 09:13:05] maciej at klepaczewski dot com Description: ------------ When a single line contains more than 1000 characters (might depend on EOL format) DOMDocument::loadHTML() errs with message similar to: ===== Warning: DOMDocument::loadHTML(): Unexpected end tag : head in Entity, line: 7 in test.php on line 4 Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity, line: 8 in test.php on line 4 ===== Test script: --------------- <?php error_reporting(E_ALL); $html = <<<HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Test</title> <!--placeholder--> </head> <body> </body> </html> HTML; echo "==== < 1000 spaces in head===\n"; $x = new DOMDocument(); $x->loadHTML(str_replace('<!--placeholder-->', str_repeat(' ', 995), $html)); echo "==== 1000 spaces in head===\n"; $y = new DOMDocument(); $y->loadHTML(str_replace('<!--placeholder-->', str_repeat(' ', 1000), $html)); Expected result: ---------------- ==== < 1000 spaces in head=== ==== 1000 spaces in head=== Actual result: -------------- ==== < 1000 spaces in head=== ==== 1000 spaces in head=== Warning: DOMDocument::loadHTML(): Unexpected end tag : head in Entity, line: 7 in test.php on line 20 Warning: DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity, line: 8 in test.php on line 20 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72288&edit=1

« previous php.bugs (#232672) next »