Bug #80679 [Opn->Nab]: if loaded document starts with text, some nodes will be lost

From: Date: Thu, 28 Jan 2021 10:55:40 +0000
Subject: Bug #80679 [Opn->Nab]: if loaded document starts with text, some nodes will be lost
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-231809@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80679&edit=1 ID: 80679 Updated by: cmb@php.net Reported by: andrey at email dot dp dot ua Summary: if loaded document starts with text, some nodes will be lost -Status: Open +Status: Not a bug Type: Bug Package: DOM XML related Operating System: Debian Linux PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: > The behaviour is different for some versions of libxml: So this is apparently an upstream (i.e. libxml2) issue. Consider to report it there. Previous Comments: ------------------------------------------------------------------------ [2021-01-28 10:51:38] andrey at email dot dp dot ua 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 this bug report at https://bugs.php.net/bug.php?id=80679&edit=1

« previous php.bugs (#231809) next »