Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Syntax Highlighting with Javascript

If you were curious enough to dig around the source code of yesterday’s CSS tutorial, you may have noticed a couple of classes that didn’t appear to be doing anything:

<pre><code class="css">h1 {
  color: navy;
  border-bottom: 1px solid black;
}
</code></pre>

<pre><code class="html">...
&lt;head&gt;
&lt;style type="text/css"&gt;
h1 {
  color: blue;
}
...
&lt;/style&gt;
...
</code></pre>

I added the classes in the hope that they would be useful some time in the future. This afternoon, the Internet connection in the University library stopped working so I decided to take the time to play with an idea I’ve been tossing around for a few weeks. The result: Javascript Syntax Highlighting. The concept is pretty simple—it grabs the code samples with css or html as the class, then uses regular expressions (along with some crafty callback functions) to add <span> elements to specific bits of code to allow them to be highlighted. It works in Firebird / Mozilla but fails pitifully in IE 5, which appears not to support callbacks from the string.replace() function. I haven’t tried it in IE 6 yet.

Unfortunately, it relies on the non-standards compliant innerHTML property which causes it not to work in Mozilla in XML mode, so I can’t deploy it on my blog at the moment. Due to the way the regular expressions work I’m not entirely sure how I can rectify this using standard DOM methods, so I’m leaving it as it is for the moment.

This is Syntax Highlighting with Javascript by Simon Willison, posted on 19th May 2003.

View blog reactions

Next: New Gestures Build

Previous: The anatomy of a stylesheet

17 comments

  1. Works fine for me in IE6 and Opera 7.10. Cool idea.

    kusor - 19th May 2003 17:25 - #

  2. There is a bug actually in MSIE6.0 instead of leaving the code like it is. MSIE6.0 stretches it.

    Too bad, I thought we could all blame Bill again, but Opera 7.11 does exactly the same. Firebird (0.6) get it right however.

    Anne van Kesteren - 19th May 2003 18:16 - #

  3. Hey, it's cool! :) It works in IE6(6.0.2800) with a little problem: the code is joined into a single line. Here is the screenshot.

    hlb - 19th May 2003 18:21 - #

  4. I'm looking out for the solution you find. As far as I have seen nobody has made some sort of XMLElement.innerXML prototype function for Mozilla (which would be handy). I don't know if that is feasible at all.

    Martijn - 19th May 2003 23:24 - #

  5. Just thought you might like to know the one I started in the library is now finished.

    It's basically the same as yours.

    Andrew Hayward - 20th May 2003 04:52 - #

  6. I saw you where looking for a DOM method. Maybe replaceChild is something you might need?

    Anne van Kesteren - 20th May 2003 07:15 - #

  7. Perhaps this was mentioned already somewhere above, has anyone noticed that the current HTML version of Scripting.com black-date-bars render differently in Mozilla (I'm using Firebird 0.6) then in IE (ugly extra line making them taller than they should be).

    Steven Garrity - 1st June 2003 17:14 - #

    1. kkjkj

    test - 5th October 2003 22:23 - #

  8. I don't know if you read old comments, but I think I have the solution for Mozilla. See: http://home.hccnet.nl/m.wargers/test/mozilla/synta xHighlight.xml You can read about some details here: http://www.codingforums.com/showthread.php?s=93950 459582996e259178676a3305635&threadid=31489

    Martijn - 16th February 2004 14:53 - #

  9. Hi, Wondering if it is possible to highlight a string as a user types words into a text area?

    Eric - 13th May 2004 01:00 - #

  10. Yes, it is possible to highlight text as the user type into a text area (that support rich text). Just use an HTML document page as a rich text. I have done an editor that will highligh as user type like in DOT Net Studio and the highlight are even better than MS Frontpage. Hit me back if you need infos.

    Eddy - 5th October 2004 19:37 - #

  11. Oh, by the way my contact email is lo6in@hotmail.com.

    Eddy - 5th October 2004 19:38 - #

  12. Here's a screen shot of my editor writing completely by pure Javascript. It is still in the beta process, but the core syntax highlight is complete as well as "highlight as the user type". Check it out at: http://ee.1asphost.com/lo6in

    Eddy - 12th October 2004 20:29 - #

  13. Hi Eddy Im Impressed how your edditor looks with this fabulous highlight as the user type Can you please explane a bit more how to do it myself. Greetings Cok Sorry for bad English

    Cok - 15th March 2005 11:09 - #

  14. Hi Eddy! I would be very interested in your editor. Have you finished this programm yet or can I download it anywhere?

    Karl - 17th May 2005 18:00 - #

  15. Simon, that's exactly what I'm using in Nvu and CaScadeS II.
    Eddy: very nice...

    Daniel Glazman - 12th October 2005 08:52 - #

  16. Check this browser based syntax highlighting editor: http://www.aboutedit.com

    aboutedit - 8th February 2006 14:06 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/05/19/syntaxHighlighting

A django site