Re: [PHP] importNode issue
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 ="" . "\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 which of course completely breaks the browser display because they are inside a 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 and every space with and then replace the with a 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("/\n/","\nclass=\"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
Re: [PHP] importNode issue
Jochem Maas wrote: highlight_string() function might be an easier route? If I only ever wanted to highlight php it might be. I found a workaround, though I don't like it. add $dom->formatOutput = false; to the function and it displays perfectly, though viewing the generated source isn't as nice (hence why I add it only when that function is called). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] importNode issue
highlight_string() function might be an easier route? Op 1/25/10 9:55 AM, Michael A. Peters schreef: > 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 =" $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 which of course > completely breaks the browser display because they are inside a > 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 and every space with and then replace the > with a 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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] importNode issue
Michael A. Peters wrote: The only (untried) solution I can think of is to replace each carriage return with a and every space with and then replace the with a or some such hackery before running loadXML() on it. But I would rather not do that. Even that isn't really working but what I think I may be able to do, though it would be a PITA, is go through the list of nodes one by one and create identical nodes and append them to a node that isn't imported. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php