Bug #80679 [NEW]: if loaded document starts with text, some nodes will be lost
| From: | andrey at email dot dp dot ua | Date: | Thu, 28 Jan 2021 10:51:38 +0000 |
| Subject: | Bug #80679 [NEW]: if loaded document starts with text, some nodes will be lost | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-231808@lists.php.net to get a copy of this message | ||
From: andrey at email dot dp dot ua
Operating system: Debian Linux
PHP version: Irrelevant
Package: DOM XML related
Bug Type: Bug
Bug description:if loaded document starts with text, some nodes will be lost
Description:
------------
If document starts with text, loading it by loadHTML will corrupt some
element nodes:
1. The text at the beginning of the document will be placed inside
<p></p> tags.
2. The text inside <p></p> tags will be placed after document
declaration
3. <html> node in document will be lost but child nodes will be
preserved
4. <head> node will be lost but child nodes will be preserved
5. if document has no head element but have another one, it will be
included in <p></p> tags with starting text
The behaviour is different for some versions of libxml:
libxml 2.9.1:
starting text enclosed in <p></p> tags and <head> element's child nodes
will be placed before <body> node.
libxml 2.9.4:
starting text enclosed in <p></p> tags and <head> element's child nodes
will be included inside <body> node
Test script:
---------------
<?php
$html = "some text <!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>";
echo "\n\nStarting with text\n";
echo "----------------------\n";
$dom = new \DOMDocument();
$dom->formatOutput = true;
$dom->loadHTML($html);
echo $dom->saveHTML();
$html = str_replace('head>', 'not_head>', $html);
echo "\n\nStarting with text and no head node\n";
echo "----------------------\n";
$dom = new \DOMDocument();
$dom->formatOutput = true;
$dom->loadHTML($html);
echo $dom->saveHTML();
Expected result:
----------------
Starting with text
----------------------
some text <!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
Starting with text and no head node
----------------------
some text <!DOCTYPE html>
<html>
<not_head>
<title>Title of the document</title>
</not_head>
<body>
The content of the document......
</body>
</html>
Also some kind of expected result:
==================================
Starting with text
----------------------
<!DOCTYPE html>
some text
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
Starting with text and no head node
----------------------
<!DOCTYPE html>
some text
<html>
<not_head>
<title>Title of the document</title>
</not_head>
<body>
The content of the document......
</body>
</html>
Actual result:
--------------
Starting with text
----------------------
<!DOCTYPE html>
<html><body>
<p>some text
</p>
<title>Title of the document</title>
The content of the document......
</body></html>
Starting with text and no head node
----------------------
<!DOCTYPE html>
<html><body>
<p>some text
<not_head><title>Title of the document</title></not_head></p>
The content of the document......
</body></html>
--
Edit bug report at https://bugs.php.net/bug.php?id=80679&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80679&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80679&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80679&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80679&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80679&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80679&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80679&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80679&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80679&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80679&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80679&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80679&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80679&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80679&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80679&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80679&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80679&r=mysqlcfg