php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34836 After inserting a documentFragment it loses its nodes.
Submitted: 2005-10-12 12:21 UTC Modified: 2005-10-12 15:54 UTC
From: bart at mediawave dot nl Assigned: rrichards (profile)
Status: Not a bug Package: DOM XML related
PHP Version: 5.0.5 OS: Fedora Core 4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bart at mediawave dot nl
New email:
PHP Version: OS:

 

 [2005-10-12 12:21 UTC] bart at mediawave dot nl
Description:
------------
After inserting a documentFragment into a document, the documentFragment becomes empty / loses its nodes.

I'm not sure if this is by design or not. I searched the W3C DOM spec for the specified behaviour. But it isn't clear to me if this is the way it should work or not. I also tested the same code with a normal element in stead of a documentFragment. Then it does works as I expect.

Also, I'm using PHP 5.0.4 and not PHP 5.0.5 Fedora hasn't released a PHP 5.0.5 package yet. I'm sorry!

Reproduce code:
---------------
<?php 

$xml = '<root><sometag>The quick brown fox jumps over the lazy dog</sometag></root>';

$doc = new DOMDocument();
$doc->loadXML($xml);

$sometags = $doc->getElementsByTagName('sometag');

while($sometag = $sometags->item(0)) {

	$docFrag = $doc->createDocumentFragment();

	while ($childNode = $sometag->firstChild) {
		$docFrag->appendChild($childNode);
	}

	echo 'Print documentFragment contents before replacing:<br />';
	foreach ($docFrag->childNodes as $childNode) {
		echo 'textContent:', $childNode->textContent, '<br />';
	}

	$sometag->parentNode->replaceChild($docFrag, $sometag);

	echo '<br />Print documentFragment contents after replacing:<br />';
	foreach ($docFrag->childNodes as $childNode) {
		echo 'textContent:', $childNode->textContent, '<br />';
	}
}

?>

Expected result:
----------------
Print documentFragment contents before replacing:
textContent:The quick brown fox jumps over the lazy dog

Print documentFragment contents after replacing:
textContent:The quick brown fox jumps over the lazy dog


Actual result:
--------------
Print documentFragment contents before replacing:
textContent:The quick brown fox jumps over the lazy dog

Print documentFragment contents after replacing:

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-12 12:54 UTC] tony2001@php.net
Assigned to the maintainer.
 [2005-10-12 15:54 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Read specs on DOMFragment carefully. Upon insertion to another node children of frag are moved.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC