Re: importNode issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Michael A. Peters wrote:
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 &#160; 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


Found the solution - the problem was where Text/Highlighter.php was putting the newline in the code it generates.

$out = preg_replace("/<span class=\"hl-code\">\n/","\n<span class=\"hl-code\">",$out);

fixes the issue, I don't have to set

$dom->formatOutput = false;

to avoid broken display now.
I think it's a DOMDocument bug, well, maybe, it shouldn't do any modifications with newlines inside a pre node with formatOutput - but I suppose it has no way of knowing what the pre node is use for since html 5 doctype doesn't identify itself as html.

But anyway, that preg_replace fixes it.
I may send a demo of problem and patch to the pear Text/Highlighter.php maintainer so that the preg_replace isn't needed.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux