5

Let's look at this CSS rule:

#foo { color: red; }

As you can see, the declaration block of the above rule contains one declaration, namely color: red. However, according to my interpretation of the CSS standard, the above declaration block also contains a second, empty declaration, which is located between the chars ; and }.

#foo { color: red; }
                  ^ --- an empty declaration is located here

I'm asking this question on Stack Overflow to determine if my interpretation is correct, i.e. if there indeed exists a second, empty declaration in the above CSS rule.

Btw, I'm using the CSS 2.1 standard, specifically "Chapter 4: Syntax", since the "CSS Syntax" module is outdated, and not safe to use.

OK, let me explain. I base my interpretation on these definitions:

  • The standard states that, within a declaration block, a semi-colon is a separator which appears between individual declarations:

    A declaration block starts with a left curly brace ({) and ends with the matching right curly brace (}). In between there must be a list of zero or more semicolon-separated (;) declarations.

    So, according to the above definition, a semi-colon my only appear between two declarations, i.e. a semi-colon must be both preceded, and followed by a declaration.

  • The standard defines a declaration as:

    A declaration is either empty or consists of a property name, followed by a colon (:), followed by a property value.

    The above definition states that a declaration can be empty. Unfortunately, the standard does not define the term "empty declaration", nor is that term mentioned anywhere else in the standard.

Let's go back to the example:

#foo { color: red; }

The above rule is valid CSS. By applying the definition for semi-colons (from above), the semi-colon in this rule must both be preceded, and followed by a declaration. However, the semi-colon is followed by the closing curly brace (which ends the declaration). In order to explain this contradiction, I insert an empty declaration between ; and }, and provide this definition:

An empty declaration is the absence of a declaration in a position within a declaration block where a declaration i required, but not found.

Is this interpretation of the standard correct, and does the declaration block indeed contain two declarations?

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • 6
    Sorry for the insensitivity, but why would one care? Perhaps someone could explain it to me. – Kermit Dec 07 '12 at 20:50
  • 3
    @njk It's called [pedantry](http://en.wiktionary.org/wiki/pedantry). I didn't choose to be pedantic, that's just how my brain works. – Šime Vidas Dec 07 '12 at 20:57
  • 1
    I think you should further evaluate what would happen if there were... two, yes two spaces after the semi-colon. Would there then be two empty declarations? We may never know. – Kermit Dec 07 '12 at 21:00
  • 4
    Did you not answer your own question here: http://stackoverflow.com/a/5732519/116614? – mellamokb Dec 07 '12 at 21:03
  • @njk White-space in that context is optional, and is discarded. – Šime Vidas Dec 07 '12 at 21:04
  • 1
    @mellamokb No. That answer merely uses my interpretation, but I obviously cannot validate my own interpretation. For that I need second opinions (I guess that's called peer review), which is why I asked this question. – Šime Vidas Dec 07 '12 at 21:11
  • @njk He cares because we were having this discussion and both wanted to find out if this interpretation was factual. I do believe this is acceptable grounds here on SO... – o_O Dec 07 '12 at 21:19
  • @njk--while I have no data to prove it, as my answer notes, it might be an ever so slight performance hit in the speed of the css parsing. – ScottS Dec 07 '12 at 21:25
  • ... and in the speed of reading the CSS, both from disk and with your eyes. If the extra character makes a read hit a page boundary you might see some really significant effects on disk and memory caching! – Lightness Races in Orbit Dec 07 '12 at 22:28

3 Answers3

2

Yes, this is normal in computer languages that define the semicolon as a separator and not part of a statement (or declaration or whatever). There is no other possible interpretation, and the issue has no practical impact, as an empty declaration has no effect.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Though I do not have the data to prove it (or disprove it), I would tend to disagree that it as "no effect." I note in my answer "why." – ScottS Dec 07 '12 at 21:28
  • @ScottS: No functional effect. It's trivially obvious that there's _some_ effect; for starters, the colon must be read from the file on disk. – Lightness Races in Orbit Dec 07 '12 at 22:25
  • @LightnessRacesinOrbit--agree. It seems others have misunderstood my point as well, so I clarified that the objection was in the context of "no practical impact," when speed can be a practical matter. Granted, it would take _numerous_ cases to probably see an effect visibly on rendering speed. – ScottS Dec 07 '12 at 22:39
2

I agree with you that the reading states it such that your situation shows an empty declaration. Indeed, your reference page in most cases has no final ; before the end brace in its examples. I think your definition is close, but did not quite hit on the idea of "empty." Instead:

An empty declaration is a declaration without any content within a declaration block where a declaration is required.

or perhaps, given the note here, this...

An empty declaration is a declaration with no more than white-space as content within a declaration block where a declaration is required.

Contra Jukka's comment about "no practical impact ... no effect," I would tend to think that there would be an ever so slight impact in that their is one additional parse to do for such a code block (realizing, as some seem to think I was arguing when I was not, that it has no functional effect). The page states:

This specification defines ignore to mean that the user agent parses the illegal part (in order to find its beginning and end), but otherwise acts as if it had not been there.

While an empty declaration is not illegal, I believe the parser would need to perform one extra step to determine it is empty before realizing it was at the end of the declaration block.

ScottS
  • 71,703
  • 13
  • 126
  • 146
  • 1
    I disagree in principle with this answer. The presence or absence of a semicolon will obviously have an effect on the universe: it will affect the byte-sequence that's stored on a server or transmitted to a browser, it will affect the optic nerve of anyone reading the CSS, and so on. But these kinds of effects simply do not count. When the spec says that aside from `collapse`, ["Other values for 'visibility' have no effect"](http://www.w3.org/TR/CSS21/tables.html) (on a table-column), it doesn't mean this in a metaphysical sense. It doesn't require that browsers employ *[continued]* – ruakh Dec 07 '12 at 21:38
  • *[continued]* quantum-computing techniques to ensure that the state of the universe will be identical whether or not one of the other values for `visibility` is used. It simply means that those values have no effect on the semantics. – ruakh Dec 07 '12 at 21:39
  • It could also be argued that the aggregate of the additional space shows a noticeable difference in download time and rendering speed, due to the added size of the file that a whitespace character creates. (But then, that's what minification is for, anyway. ;) ) – Shauna Dec 07 '12 at 21:57
  • @ruakh--my argument of "no effect" was also in the context that Jukka had said "no practical impact," and I was pointing out that it may have a practical impact on parsing speed. This is more than simply "not count[ing]." @Shauna added the note about download speed, which minification helps, but parsing is the browser actually running through the interpretation of commands to execute. So while the effect may be small in most cases, a huge file with a bunch of unneeded `;` causing an extra parsing step each time might be noticeable to a sensitive speed measuring device. – ScottS Dec 07 '12 at 22:16
  • @ScottS: or conversely, the `;` might enable infinitesimally faster parsing. It depends entirely on the parser, and is absolutely not worth worrying about. – ruakh Dec 07 '12 at 23:16
  • @ruakh--whether faster (hard to fathom, but I'll grant it) or slower, it would still have "practical impact." However, I do agree it is "not worth worrying about" as the practical impact is not likely something that is going to affect things at a level detectable to normal human senses. – ScottS Dec 07 '12 at 23:37
  • @ScottS: I think you and I must have different definitions of "practical". By the way -- Stack Overflow hasn't been notifying me of your replies; I think it's interpreting `@ruakh--my` and `@ruakh--whether` as trying to reply to users named `ruakh--my` and `ruakh--whether`. I recommend you write something like `@ruakh: my` or `@ruakh -- my` instead. – ruakh Dec 10 '12 at 14:14
  • @ruakh: Perhaps. I am looking at it from a definition of pertaining to "practice" or "action," but specifically with reference to the "action" of the parsing itself. Anything that affects "time" is having some type of "practical" effect, however small. Whether that effect itself should change one's "practice" of putting a `;` before the final `}` is unlikely, and not something to worry about (unless someone has a test to prove there could in fact be a noticeable speed change on large files). Hope that clarifies my use of the term. – ScottS Dec 10 '12 at 20:25
1

At first glance, the specification seems slightly inconsistent in this respect; despite the passages that you quote, the formal grammar defines the declaration production as property ':' S* expr prio?, which is never empty. However, whenever declaration appears in any other production, it is always followed by ?, marking it as optional; in other words, anywhere that the grammar allows declaration, it also allows [nothing].

We can resolve the inconsistency by saying that the term "declaration", where appearing in the specification's English prose, refers to declaration? (declaration-or-[nothing]) rather than to declaration; but I think the main take-home point is that it doesn't matter. The specification is written in such a way that an empty declaration truly has no effect.

(And hey, it could be worse. Standard ML allows empty declarations, and allows semicolons to be omitted between declarations; so something like val a = 7 val b = 20 can be parsed as a sequence of arbitrarily many empty declarations, with two non-empty declarations mixed in.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • The formal grammar provides different definitions. The relevant part is `'{' S* declaration? [ ';' S* declaration? ]* '}'`, which "covers all bases", as it enables stuff like `{ color: red ;;;;;;;; }`. – Šime Vidas Dec 07 '12 at 21:16
  • @Šime Vidas: Yes, absolutely. – ruakh Dec 07 '12 at 21:18