[mediaqueries] calc() and other syntax restrictions (was: Re: Validating media queries in Java)

On Friday 16 November 2007 18:02, fantasai wrote:
> Henri Sivonen wrote:
> > I'm trying to validate CSS Media Queries as HTML5 attribute values.
> > I'm using Java.
> >
> > I first thought that media queries would be syntactically very
> > simple--something for which importing the W3C CSS Validator or a
> > parser generator as a project dependency would be and overkill. I
> > have since learned that most of the complications are hiding inside
> > <value> and the language is nowhere near as simple as the
> > pseudo-BNF in the Media Queries spec makes it appear. In
> > particular, even traditional CSS lengths make the states in the
> > parser balloon and support for calc() would make things much, much
> > more complex.
> >
> > I'm now considering abandoning my hand-rolled code in favor of
> > either a customized generated parser or a generic CSS parser that
> > someone has already written. I checked out the W3C CSS Validator
> > source today. Unfortunately, it seems that it doesn't have an entry
> > point for parsing media queries in isolation and the JavaCC grammar
> > it uses is tightly coupled with the program internals.
> >
> > I'd appreciate advice:
> > Is there some Open Source work that I'm missing but could use?
> > Other recommendations on how to proceed?
> We could disallow calc() in media queries. I don't see much of a use
> case for it.

No doubt somebody some day wants to write

    @media screen and (min-width: calc(20em + 6px))

because he has a column of text with two borders of 3px each...

But I agree. We can exclude calc(). Media Queries aren't just for CSS, 
they are meant to be used in HTML and XML, too, so let's restrict the 
syntax and not require a full CSS parser just to parse Media Queries. 
Which also means the Media Queries module should be as self-contained 
as possible and require implementers to read as few other CSS modules 
as possible.

There has to be a balance between on the one hand being able to cut and 
paste media queries between CSS, HTML and XML and on the other hand a 
syntax that is "natural" for the host language. I think the definitions 
of "white space," "identifier" and character escapes should be from the 
host language as much as possible, even if the media query is a quoted 
string in that language.

That is, we should try to make the syntax such that you can cut and 
paste a media query with a simple text editor in all cases that a 
normal user is likely to encounter.

But without forbidding *all* differences. For example, where the syntax 
requires white space, we can leave it to the host language to define 
what that is. XML disallows FF (Form Feed, ASCII 12), while CSS allows 
it. It's so unlikely that a CSS author puts FF inside a media query, 
that I wouldn't worry about that difference. Ditto for escapes. We 
can't forbid an HTML author from using a character 
entity "min-width&#58; 7em" although that's clearly not allowed in CSS. 
Similarly, a CSS author may use a backslash escape, but we don't need 
to allow that in other host languages.

The desire to allow cut and paste *does* require media queries to be 
case-insensitive, even though that is not "natural" for XML.

  Bert Bos                                ( W 3 C ) http://www.w3.org/
  http://www.w3.org/people/bos                               W3C/ERCIM
  bert@w3.org                             2004 Rt des Lucioles / BP 93
  +33 (0)4 92 38 76 92            06902 Sophia Antipolis Cedex, France

Received on Monday, 19 November 2007 13:35:25 UTC