Archive for the ‘General’ Category.

Syntax Highlighting Textarea, Reloaded

A couple of years ago I tried my hand at making a DHTML-based syntax highlighting editor, for use in web applications. The result has been posted in my portfolio for quite some time, but it never worked particularly well, and was quite feature-poor. It was also very heavy - it relied on javascript for even basic editing capability, and therefore did not degrade well.

I had an idea about a year ago to start the whole thing over again with a different way of handling input (which I thought was pretty clever): Instead of building the editor from the ground up using javascript, use a textarea for the editor. Better yet, take an existing textarea, apply a javascript constructor to it, and it gets converted into a syntax highlighting textarea. I planned to accomplish this by making the textarea mostly transparent (but still slightly there, for interaction purposes) and positioning the highlighted text behind it. The highlighted text - colored with CSS - would be updated whenever the textarea changed. Simple, right?

I whipped up a quick proof of concept and quickly forgot about the whole idea as my workload became too great. A couple of days ago I decided to start fresh on this thing and do it right. Here are the results so far. It seems to work in Firefox, Opera, and IE6 to a limited extent; Safari borks it up because their tab width is different in a textarea than it is in a block of preformatted text (Don’t ask me why; this probably comes from Konq, though, and suggests that Konq will also fail).

The beauty of this is that the code will get submitted to a form containing the textarea without any trickery or additional work. Also, if javascript is disabled, the user will just be presented with a plain old textarea - and at least the application will still be usable.

There is a lot of optimization to be done. You might notice that it quickly gets unusably slow as the document gets longer and longer. This is because it typically has to re-color most of the document on every keypress. I’ve added an optimization wherein the highlighting will start from the last initial state before the cursor position (if it can be found), so this helps as long as you restrict your editing to the end of the document :-D. There has got to be a way to do a similar optimization which somehow detects when the parser returns to an initial state that was already an initial state, but I haven’t thought of it yet. It will probably involve “spying” on the input to the textarea to get an idea of what’s going on, and making parsing decisions based on that.

Anyway, stay tuned for that stuff, and e-mail me if you have a suggestion! Remember, comments are disabled because of the dirty spammers (who should all be killed in gruesome ways) - my e-mail address is jeremy at [the domain you're looking at].

Upgrade

I finally upgraded this site to the latest version of Wordpress. Things aren’t really working that well anymore, but hopefully I’ll have time to deal with it at some point in the near future.

I really wanted to ditch wordpress and roll my own publishing platform, but I just don’t have the time. Bummer.

Safari on Windows: DOM Inspector

I was unable to find any info on getting the DOM inspector working in Safari Beta 3 under Windows, so when I figured it out I thought I would post it in case anyone else is having the same problem. If you’ve tried meddling with the prefs files but can only get the Debug menu and no DOM inspector, this might help.

1. Install Safari for Windows from Apple’s site
2. Download the Windows nightly build from http://nightly.webkit.org/ (yes, you still need Safari Beta 3 installed)
3. Extract that somewhere and run the batch file. DOM inspector context menu option (”Inspect Element” on right click) is now present.

What’s The Deal with ORM?

ORM: Object-Relational Mapping. Chances are, if you’ve been doing web development for a significant amount of time, this concept has occurred to you. It’s the mapping of a high-level object definition in your OOP language of choice (e.g. a class) to a relational data storage scheme (e.g. your favorite database). I have written several ORM schemes in my day; naturally they started out as pretty naïve, but I would like to think that I’ve pretty well nailed it now, at least for my own personal needs and preferences.

Object-oriented PHP5 has been my development environment of choice for a while, but I’m getting more into Java in recent weeks. I’ve been programming in Java for many, many years, but I’ve never done any real server-side stuff with it. As luck would have it, my work duties have provided me an opportunity to dig my hands into J2EE.

I have to say, it has come a long way since I last checked it out. And I think I actually like it. It seems like a really magical MVC framework could be made for Java - it’s a pretty superior language to many of the other options (I like C# more, but I don’t like how it’s tied to the .NET framework in one way or another in all its incarnations). So I set about looking for that framework. I didn’t really like any of the ones I saw; they added lots of (in my opinion) needless complexity. I like simplicity in a framework. Minimalism, even. (Punchline coming soon, I promise)

So I thought I might set about rolling my own. My most recent framework for PHP5 is pretty money (if I do say so myself) so I thought I might carry over some of the concepts I developed there. I started looking at prefab ORM libraries for Java and of course the first one I checked out was Hibernate. After browsing the documentation, I have to say: Dear lord! It’s a great idea, but come on! A configuration file for each data class? An entirely new and dedicated query language! Sounds like once you allow Hibernate into your application, you’re stuck! Now I see why someone got fed up and made ActiveRecord - which I’m also not too fond of.

I think maybe I’m far too picky and far too lazy, which is a bad combination. The ORM solution I wrote for my PHP5 framework caters pretty well to these qualities - I write my data class code with my desired inheritance structure, add some tags in the comments for the fields (this is only necessary because PHP5 lacks strong typing) and then the ORM code takes care of the rest - determines the inheritance structure, creates the data schema (or updates it if it exists), and then can allow me to store and retrieve objects using either simple methods or real SQL if I need it. Foreign key relationships are handled automatically (example: $myPerson->employer fetches, caches, and returns the employer specified by people.employer_id) and life is good. This is what I want for Java. Do I have to do it myself? (Because that last one was a lot of work ;-))

Spam

Don’t bother trying to comment here anymore. Unfortunately, I get so pounded with spam that I can’t even filter out the legitimate comments from the spam without taking more time than I care to. So I’ve completely disabled any notification to me of comments because they are flooding my inbox. If you post a comment, it will never make it past moderation.

Sorry. I’m rebooting the whole site soon at which point I’ll fix this.