php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28584 DOMText::splitText() does not split text properly (includes patch)
Submitted: 2004-05-31 00:19 UTC Modified: 2004-05-31 14:51 UTC
From: benjcarson at digitaljunkies dot ca Assigned:
Status: Closed Package: DOM XML related
PHP Version: 5CVS-2004-05-30 (dev) OS: Linux
Private report: No CVE-ID: None
 [2004-05-31 00:19 UTC] benjcarson at digitaljunkies dot ca
Description:
------------
DOMText->splitText() does not split text nodes correctly.  The node returned from the splitText() includes text from additional DOMText nodes following the one being split.  Instead, the returned node should only containg text that was in the original node, following the split offset.

Here is a html version of the reproduce code, along with some javascript to demonstrate the splitText() method:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
</head>

<body>
<p id="p" onclick="splitText(this)">text1<i>i</i>text2</p>

<script type="text/javascript">
function splitText(node) {
  text1 = node.firstChild;
  split = text1.splitText(2);
  text2 = node.lastChild;
  str = 'text1:  ' + text1.nodeValue + '\n' +
        'split:  ' + split.nodeValue + '\n' +
        'text2:  ' + text2.nodeValue;
  alert(str);
}
</script>
</body></html>


It turns out the fix for this is a 1-liner in ext/dom/text.c.  I've created a diff available here:
http://www.digitaljunkies.ca/~benj/text.c.diff



Reproduce code:
---------------
#!/usr/bin/php
<?php

$xml = new DomDocument();

$p = $xml->createElement("p");
$p->insertBefore($text1 = $xml->createTextNode("text1"));
$p->insertBefore($i = $xml->createElement("i"));
$i->insertBefore($itext = $xml->createTextNode("i"));
$p->insertBefore($text2 = $xml->createTextNode("text2"));

$split = $text1->splitText(2);

echo ("text1:  " . $text1->nodeValue . "\n");
echo ("split:  " . $split->nodeValue . "\n");
echo ("text2:  " . $text2->nodeValue ."\n");

?>

Expected result:
----------------
text1:  te
split:  xt1
text2:  text2


Actual result:
--------------
text1:  te
split:  xt1text2
text2:  text2


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-31 14:51 UTC] rrichards@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Thanks for the patch
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 06 15:01:32 2024 UTC