Syntaxhighlighter made XHTML compliant
Syntaxhighlighter is a powerful Wordpress plugin, based on the the google-hosted highlighter project, and it’s what I’ve chose for posted code snippets. However, there still an issue not yet solved: the generated markup is not XHTML valid. Surely, this could be insignificant for some users, but a plugin for a bloging system whose motto is “Code is Poetry”, shall, in my opinion, never break this rule. This issue seems not to be under consideration by the plugin developers, and that’s why I’ve quickly made this Hack on the plugin code to make it XHTML compliant. The W3C validator is complaining as follows: there is no attribute “name”. So one most possible solution is to used the (highly recommended) “id” attribute, and as the “id” attribute value shall be unique along the DOM, a special pattern of the value could be defined by the server-side, allowing the client side javascript code to find the target element to highlight. In a nutshell :
In syntaxhighlighter.php, change line 297 :
$content = str_replace( $match[0], '<pre name="code" class="'
to
$content = str_replace( $match[0], '<pre id="code__'.rand() . '" class="'
In shCore.js, add the following prototype at the beginning of the script:
String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}
and change line 146
if(tags[i].getAttribute('name')==name)
to
if( ((x = tags[i].getAttribute('id'))!=null) && (x.startsWith(name + '__')))
Basically, instead of looking for elements with name = “code”, it will look for elements whith id attribute starting with “code__”. That’s it. It’s working and XHTML compliant above all!
PS : The probability that few consecutive calls to PHP rand() function give 2 equal values is really infinitesimal and this guarantee to never get two id with the same value.

