Anne van Kesteren

Error handling

Eric Meyer posted some thoughts on malformed things that might become dangerous. In passing, he says that XML (and XHTML as application/xhtml+xml) saves the world from that by having strict error recovery. Also that HTML is doomed because of this. Fair point. He doesn’t mention, however, CSS. CSS is not an XML based language. While some people wanted it to be and even today people want Selectors to be XPath; it’s not. And I think that is one reason for its success.

I also believe that is one of the reasons HTML became so successful and perhaps it even explains RSS’s success… CSS is different from HTML and RSS though. I don’t mean in terms of syntax, but in terms of forward compatible parsing rules. In CSS it is defined precisely what parsers should do with CSS that does not strictly conform to the specification. This not only allows extensions to be made in future versions of CSS it also ensures interoperability on what rules are applied and how they are applied.

(It is irrelevant here that browsers do or do not support the features of the specification; it is important that they follow the parsing guidelines and most browsers do. Even Internet Explorer, although not as good as Mozilla does it or Opera.)

Because error handling has been defined, CSS is actually quite a strict language. However, unlike XML it remains usable when some parts of the document are ill-formed or non compliant. This is a big win as there are a lot of people out there writing non compliant CSS by copying bad examples, et cetera. This allows people to easily play with CSS without being punished for a little typo right from the start. Eventually they’ll learn how to write CSS correctly and even then you occasionally make an error. Some error might even make it to a live site and still no harm is done except for people who like to validate style sheets…

Validating someone else his/her style sheets is really the most useless thing to do. By this I mean that if people use hacks on purpose and the validator complains that’s ok because browsers will treat invalid CSS correctly anyway. (Missing units, incorrect property names, et cetera are of course mistakes that should be fixed right from the start.) For HTML and XHTML with a HTML MIME type the opposite is true. You need to validate your document to ensure interoperability. This is because for HTML no error handling has been defined as of now. Eventually that will come, similar to CSS.