[CSS 2.1] @charset, @import, and error recovery

The rules for @import are very clear, however, when it comes to error recovery, they get a bit muddied and I think the spec could use a bit of clarification.

"Any @import rules must precede all other rules (except the @charset rule, if present)."

Well that is clear. NO other rules are allowed, but what about failed rules? Rules that don't make sense:

@charset "hola"; /* I can ignore this I guess */
@foo { };
@import url(a.css); /* I should be throwing this out, or should I? */

Browsers are inconsistent here. When I read the spec, I want to write something like:

HandleCharSet();
while( AtImport ) { HandleAtImport(); }
while( !EOF ) { HandleRules(); }

The above @foo { }; doesn't allow for that if we are supposed to parse @import up until we find the first successful rule. Because of this inconsistency, some browsers are ignoring tons of stuff at the top of the sheet and still processing imports.

An interesting side note is that I think it is implied clearly in the spec that if you parse an invalid @import rule then you should continue parsing @import's further until you hit some other non import rule. Clarification would be nice though.

For @charset, if you fail to parse it, should you disable @import or should you treat it like it was an @charset anyway so imports aren't disabled, in other words, should the following work or fail:

@charset "foo" bar; /* start with HandleCharSet, fall back to HandleInvalidRuleSet */
@import url(a.css);

Another good one is multiple charset rules. The spec clearly says I can have only one, parsing the second one would technically be a "rule" which should then break the @import logic.

@charset "utf-7";
@charset "oops";
@import url(a.css);

Justin [MSFT]

Received on Saturday, 26 January 2008 22:59:48 UTC