Controlling Content Editable

I’m burned out. I busted my rear end last week writing a piece of code that has been a major problem for us. Equals uses a web technology called content editable. In short it allows a developer to segment a section of a web page and say that the user can type there. It is similar to a text area but content editable allows for rich text editing (bold, lists, headlines, etc).

The concept of content editable is amazing but there are a couple of major problems with it:

  1. The standards are different across browsers. How returns are treated, for example, is completely different on WebKit (Safari and Chrome) than IE than Firefox. I haven’t even tried it on Opera and other browsers.
  2. The cursor is a foreign thing to most browsers. Except for text areas and text fields, we were never really meant to be able to see a cursor on an HTML page.

We had to make a number of very difficult decisions here and have to admit that one of the reasons Equals is taking longer than we anticipated is simply because we ended up re-writing most of the way the note is handled. Equals relies heavily on every line having an appropriate container, whether that is a div, a p, or a header tag.

When we started out we thought we would write our code to control the cursor and then would modify the content editable area as we went along. But what we found is that controlling the cursor is very difficult. Since controlling the cursor isn’t well supported [1], making sure it always does the thing we want it to do is dangerous. We had all kinds of weird behavior.

Furthermore, the original code we wrote for this is the first Javascript code we had written. What a horrible way to get our feet wet! Upon further study, it became clear that there was a better way to write this portion of Equals, one that would be much more stable, much more portable, and allow us to do things in the future we couldn’t fathom doing today with the original code.

So a few weeks ago we decided to re-write it. The goal was to generally let content editable do what it wants and then adjust after the fact. We also moved more HTML out of the content editable area, which will stabilize the content and make it easier for us to handle stuff like copy and other across-document functions.

We also found that how we start the HTML within the content editable area makes a big difference on how the browser forms its HTML. It will repeat that first line for every return key if set up correctly. Firefox, though, prefers <p><br></p>, Safari and Chrome prefer <div><br></div>, and IE prefers <div></div> without the break. It also matters when this is set. Firefox and IE don’t seem to care, but Safari and Chrome require page load.

Once I figured this out, other pieces fell into place and typing notes became routine. It all just worked. I took 750 lines of code and reduced it to 150 lines of code. I even was able to handle cut and copy correctly.

Until a user pasted something into the content editable area. Then all hell broke loose. And that’s why I’m burned out.

Last week I wrote 150 lines of code, all of it dealing with the process of pasting [2]. The problem with paste is that it pastes whatever it has in its paste memory buffer, whether that is well-formed HTML or not. Again, I didn’t want to control the contents of the paste. So I let the content editable do what it wants and then, after the fact, go through the entire document and clean it up [3].

The edge cases are ridiculous on this one and I have only tested it in Safari so far, but the code feels fairly solid. It handled a plethora of variations and bizarre behaviors. And if it does end up working out, I have a nice function we can call if an Equals note does get out of whack for any reason. Here’s hoping the code stands and I don’t have to patch it further. This 150 lines could be spaghetti code really really easily.

[1] If you need to deal with cursor stuff, I highly recommend Rangy. We still use it some but not as much as we did in the first revision.

[2] And undo and redo, technically, although I consider both of those a side-benefit of fixing paste.

[3] I thought of alternatives like pasting into a hidden text area, cleaning up the code, then moving it into the content editable area. That may still be an option if my way fails completely in the end. But that required me to control the paste process, and that is something I wasn’t comfortable with having control over in all browsers.

Steve Ballmer’s Retirement Timing Sucks

Steve Ballmer, in his retirement letter to Microsoft employees:

There is never a perfect time for this type of transition, but now is the right time. My original thoughts on timing would have had my retirement happen in the middle of our transformation to a devices and services company focused on empowering customers in the activities they value most. We need a CEO who will be here longer term for this new direction.

I’d like to know what goes through Steve Ballmer’s head. [1] How, in a million years and at this seminal point in Microsoft’s history, did he possibly think he could start a major re-organization of the company and then hand it over to a new CEO part way through? And when he said he’d do it in the middle of the “transformation,” where in that transformation does he actually think the company will be in a year?

I feel bad for the next girl. Not only is she going to have to complete a half finished transition, but she will have to pick up the pieces and try to make it her own company at the same time. If Ballmer was going to retire, he should have done it before the re-org announcement, helping the new CEO formulate the plan and act as an advisor during the transition. Then Microsoft would truly be her company.

I’ve already expressed my perspective on Microsoft and what direction I think the company should go. It’s no where close to the direction Ballmer has set for it. If Microsoft wants to talk anyway, though, I’d be happy to go to Seattle. 🙂

[1] I’d also like to know if this is him being forced to retire, but that’s a separate issue.

The Imperfect Craft

Nice post by Daniel Jalkut on his philosophical change regarding refining his craft:

As a modern software developer, I derive as much joy from remaining relevant as I do from the thrill of identifying and solving the particular problems in my work. To remain relevant, I have to reject my previous assumption that I would spend a lifetime refining my craft. Instead, I will spend a lifetime adapting the techniques of yesterday’s craft to the sometimes radically different challenges of today. I may never become “a real expert,” as I hoped I might be. But by diligently throwing out the old rules and embracing the new ones, I hope to come close.

I would argue that remaining relevant is refining his craft. It is really hard, I think, in the modern coding era to be good at only one thing. Once upon a time I was a very good Palm OS programmer. That skill is completely irrelevant today.

Today I’m a solid iOS, Rails and Javascript programmer. I’ve been programming for almost 30 years and I have never been more relevant and never felt as capable of crafting amazing software products as I do right now.

The Massachusetts Software Tax

I am lucky enough to work in an industry that, compared to other industries, is lightly regulated. When odd regulations pop up, however, they are glaring. Massachusetts has apparently passed a new tax law for software companies who do any business at all in the Commonwealth. It makes no sense, even after Fast Co Labs tried to explain it:

This added levy is not only cumbersome, it’s super confusing. For example:

  • if you install software (Microsoft Office, Constant Contact, Drupal, etc.), it’s taxable
  • if your client clicks the mouse to install it, it’s not taxable
  • training your client to use this software is not taxable
  • but if you “customize” or configure the software in any way, it’s taxable
  • if you don’t actually make any changes, but just discuss them and plan them, it’s consulting and not taxable
  • if you create graphic design mockups, it’s not taxable
  • but as soon as you implement that design (i.e. program it), it becomes taxable if you’re using “prewritten” software “not developed” by you (such as WordPress)

At least, that’s how we think it works.

Read the entire article. I emailed the article to my accountant, here in Oregon. Sadly he has to be an expert in tax law for all 50 states and maybe overseas, too.

We are starting to see the desire to charge software taxes, first with the Internet sales tax and now with laws like this. Our landscape is only going to get more confusing. And, sadly, we are all going to have to be more active politically to protect our sanity.

via O’Reilly Radar

Jeff Bezos And The Future Of News

Jeff Bezos, in a memo to Washington Post employees after he purchased the company:

The values of The Post do not need changing. The paper’s duty will remain to its readers and not to the private interests of its owners. We will continue to follow the truth wherever it leads, and we’ll work hard not to make mistakes. When we do, we will own up to them quickly and completely.

Once upon a time, when towns were small, all news was passed over the fence, neighbor to neighbor. Did you hear about the Johnson girl? I didn’t but I did hear that there’s a sale down at the general store.

As towns grew bigger the dissemination of news became more centralized and the desire for a single news source increased. The town paper had authority and ethics and all the things we expect from a paper of record.

As the towns grew into cities it was no longer good enough for that paper to just provide the local sports scores and happenings on Main Street on Saturday night. Now it had to provide national news as well. Every city bigger than a couple  hundred thousand people had a paper with both national and local news. Every paper, outside of the evening news, became the source for all information.

And then the Internet came along and suddenly we could get national news from anywhere. This diminished the value proposition of the local paper and that diminishing value meant diminishing financial returns.

You know all this, of course. It’s been discussed ad nauseum for years now.

I believe, however, that at some point we will reach a tipping point where local papers become local papers again and a few national papers become the paper-of-record for US news. At this point it is clear that three papers have the ability to make it as national news sites (paper will go away) of record: The New York Times, The Wall Street Journal and The Washington Post. Others may survive also — The LA Times, The Atlanta Constitution, the Miami Herald — but these are the three worth betting on.

A massive overhaul will be required, a re-organization of the mission and goals of these venerable institutions. But if I was a betting man — one with an interest in journalism and money to burn like Jeff Bezos — a bet on one of the Big Three sure makes for an interesting play.