Coding with Jesse

onAfterPaste

Working with designMode="on" or contentEditable (what can we call this? I want to say Rich Text Editors or Midas Editors Web-Based HTML Editors or something. We don't really have a nice buzzword for this..), a common problem is dealing with pasted content. The user can paste any HTML into these areas, and more often than not, web applications don't want form elements or IFrames or other HTML included in the content.

It's not so difficult to process the innerHTML of a document to strip out bad HTML using regular expressions. The problem is, when can this cleaning happen?

Mozilla has no paste events at all. Internet Explorer has onBeforePaste and onPaste events, but no onAfterPaste. onPaste fires when the user pastes, but before the HTML actually goes into the editor. The idea is that the developer has a chance to look into the clipboard using window.clipboardData.getData(). Unfortunately, you can only retrieve the contents in URL or Text format, not HTML. Instead, it would be easier to allow the HTML to be pasted, then process the editor contents afterwards.

To accomplish this in Internet Explorer, we can simply set a timeout in the onPaste event. This works by allowing the browser time to finish its internal onPaste event before executing the code in the timeout. The onPaste event needs to be attached to the BODY of the editor IFrame using designMode, or the DIV element when using contentEditable.

function onPasteHandler(e) {
     setTimeout(function() {
          // editor cleaning code goes here
     }, 1); // 1ms should be enough
}

In Firefox, we can't use paste events. However, probably the best we can do is set a keypress handler and look for CTRL+V or SHIFT+INSERT and then do the same thing with a timeout. The keypress event handler needs to be attached to the document element in the IFrame.

function onKeyPressHandler(e) {
     if ((e.ctrlKey && e.keyCode == e.DOM_VK_V)
      || (e.shiftKey && e.keyCode == e.DOM_VK_INSERT)) {
          setTimeout(function() {
               // editor cleaning code goes here
          }, 1);
     }
}

This should only work with Mozilla/Firefox because e.DOM_VK_V and e.DOM_VK_INSERT are not defined in Internet Explorer.

Also note that there are still other ways to get HTML into the editor through Drag-and-Drop, or by using Edit>Paste on the Firefox menu. If you are serious about stripping HTML you will need to do it at other times as well. At least this way it will happen quickly enough that the editor won't misleadingly contain these elements, only to strip them out at an unpredictable time in the future.

Published on November 10th, 2005. © Jesse Skinner

New Tools

Just for fun, I added a few simple JavaScript powered conversion tools, and PHP powered "geek" tools. There are just a few, but I will probably add more in the future. I don't know if they will be of use to anyone (except me perhaps), but feel free to steal the JavaScript for your own purposes.

Published on November 6th, 2005. © Jesse Skinner

Don't Quit Your Day Job

For a long time now, since I was in high school, I've had an itch to start my own business. I've had many ideas, most based around the idea of a web design company. I've never followed any of them through though. I've always felt like I would in the near future, but it was never the right time.

Lately though, I've started to think about this a lot more. I guess I always need to have some kind of goal to look towards. Over the past year, a lot has happened. I moved from Canada to Germany. But now that the dust has settled, I guess I feel like I need to be doing something new. I need a challenge in my life, and the clearest way to do this seemed like starting my own business.

I say seemed because that was before I read the sex & cash theory. In a chapter of his excellent piece on How to be Creative, Hugh Macleod says, "The creative person basically has two kinds of jobs: One is the sexy, creative kind. Second is the kind that pays the bills. Sometimes the task in hand covers both bases, but not often. This tense duality will always play center stage. It will never be transcended."

This really struck a chord with me. I've often thought that starting my own business might be a bad idea. If I did, I wouldn't want to do the marketing. I would hate doing sales. I would want to get someone else to do the designs. I wouldn't mind doing management and support, but it would distract me from what I really love doing: developing software. So, then, what's so bad about being a software developer in a larger company?

Hugh made me realise that I could have the freedom to be creative in my spare time, while still being able to pay off my massive debt by working during the day. And it's not like my work is a bad place. It's a really interesting job and a great company too. I get to do exactly what I want to be doing. I might not have the sort of control I would have with my own business...but then again, if I had my own business I would have to do what clients wants most of the time (if I want to get paid!) So it's really the same situation.

By keeping my day job, I also don't have to worry about making money. If I had my own business, I'd have to find ways to make lots of money with my talents. By letting my day job take care of the steady flow of money, I can feel free to contribute to open source software, volunteer for organisations, or work on web sites with my mom and dad. I can do anything I want! Being self-employed, I wouldn't have this freedom because I would be trying to focus on paying the bills.

I feel a real desire to do something big with my life. I'm really excited about the potential of the Internet (ie., The Future of the Web :) and the ways it is beginning to grow and evolve. I love the way it is transcending hierarchy and reshaping the social structures of the human species. It has already changed so much and we can't even imagine what the impact will be a hundred years from now. I think being a software/web developer in this era might be the most exciting job in the most exciting time period ever.

But, I now realise that there are millions of ways to participate without starting my own business. I just want to be able to continue to work on exciting projects that let me push the envelope of web technologies and web application development. I leave the business-starting to those who are passionate about business, and I'll focus on doing what it is I'm passionate about.

How do you express your creativity and passions at work and at home, or is there even a distinction between the two for you?

Published on October 31st, 2005. © Jesse Skinner

Comments

I finally got around to adding comments to the site. Feel free to test them out if you like. No html is supported..that is, everything will be converted to HTML entities. There is no preview or edit. It's pretty basic stuff. I guess that's the problem with writing my own blog code.. I never get around to adding the most basic of features. At least now you can comment on the lack of features :)

And on an unrelated note, I put the design back to the original one. The new one was a bit too boring I think. I'm not sure when I'll change it again...

Published on October 23rd, 2005. © Jesse Skinner

New Design

So what do you think? I redesigned the web site, as I'm sure you'll notice. Unless it's your first time here, of course. It took me about 15 minutes to do the design. I was kind of bored of the other one so I thought I'd go for the terminal-window look. I should put some of my old ANSI art here next.. ha.

Published on September 14th, 2005. © Jesse Skinner
<< older posts newer posts >> All posts