importNode issue
| From: | Michael A. Peters | Date: | Mon, 25 Jan 2010 08:55:37 +0000 |
| Subject: | importNode issue | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-301398@lists.php.net to get a copy of this message | ||
I'm experiencing a slight problem with importNODE putting unwanted carriage returns in the the output.
Here's my function:
// syntax highlighting
include_once('Text/Highlighter.php');
function syntaxHighlight($dom,$lang,$code) {
$hl =& Text_Highlighter::factory($lang);
$out = $hl->highlight($code);
//die($out);
$tmpDOM = new DOMDocument('1.0','UTF-8');
$tmpDOM->loadXML($out);
$foo = $tmpDOM->saveXML();
//die($foo);
$nodeList = $tmpDOM->getElementsByTagName('div');
$impDIV = $nodeList->item(0);
$returnDIV = $dom->importNode($impDIV,true);
return $returnDIV;
}
-=-
Here's my test:
$code ="<?php" . "\n\n";
$code .="require_once('/path/to/something');" . "\n";
$code .="function somefunc(\$myfoo,\$mybar) {" . "\n";
$code .=" \$myfoobar = \$myfoo . \$mybar;" . "\n";
$code .=" return \$myfoobar;" . "\n";
$code .=" }" . "\n";
$code .="?>" . "\n";
$fooTest = syntaxHighlight($dom,'PHP',$code);
-=-
If I uncomment the die($out) - I get what I expect spit to the screen, view source shows code that will do what I want.
If instead I uncomment die($foo) - I also get what I expect spit to screen. view source shows code that will do what I want.
However, if the function is allowed to continue, the imported div has carriage returns between each and every </span><span> which of course completely breaks the browser display because they are inside a <pre></pre> node.
Anyone know why importNode does this and how to fix it?
The only (untried) solution I can think of is to replace each carriage return with a <br /> and every space with   and then replace the <pre> with a <div class='monospace'> or some such hackery before running loadXML() on it. But I would rather not do that.
php 5.2.12 built against libxml 2.6.26