Posted by & filed under Programming.

Had a very odd, very frustrating issue come up trying to get SyntaxHighlighter working on a project I’m working on. When the code to be highlighted was only two characters long it would blank it out and the code would display empty.

After much digging I found this little bit of code inside of the stripCData function in shCore.js around line 1110.

	if (copy.indexOf(right) == copyLength - rightLength)
	{
		copy = copy.substring(0, copyLength - rightLength);
		changed = true;
	}

Turns out rightLength is 3 – the length of ]]>, the closing CDATA tag. And copyLength was 2. So 2 – 3 = -1, which works out to the same thing as an indexOf call that’s not found. The substring then took care of blanking it out.

What I did was add a condition to the if and it appears to be working fine. This is what it looks like now.

	if (copy.length > 2 && copy.indexOf(right) == copyLength - rightLength)
	{
		copy = copy.substring(0, copyLength - rightLength);
		changed = true;
	}

Just a real simple check to make sure the string copy is longer than 2 characters before looking for the CDATA closing tag.

Leave a Reply

Your email address will not be published. Required fields are marked *