• Narrow screen resolution
  • Wide screen resolution
  • Increase font size
  • Decrease font size
  • Default font size
  • default color
  • red color
© Diana Scherff, Amas-Veritas.com

Welcome to Amas Veritas [dot] com

Updates: Media (lyrics) has a new layout. I could never figure out how to lay it out, but it's easier to navigate now. I'm still working on a better duplicate Mamblog mod. I'm trying to make submittions easier but the poetry form died for some reason. Quizzes are also on their way from the old site. Joomla content isn't very code friendly so I'm having to rewrite old code. You can still click on News > AH v21 > Screen if you wish to use the quizzes.
 
Home arrow Tools arrow CSS 2 arrow Appendix D. The grammar of CSS2
Appendix D. The grammar of CSS2 Print E-mail
Written by Diana, on 03-09-2007 21:33
Views 82    
Favoured None

Appendix D: The grammar of CSS2

Appendix D. The grammar of CSS2

This appendix is normative.

The grammar below defines the syntax of CSS2. It is in some sense, however, a superset of CSS2 as this specification imposes additional semantic constraints not expressed in this grammar. A conforming UA must also adhere to the forward-compatible parsing rules, the property and value notation, and the unit notation. In addition, the document language may impose restrictions, e.g. HTML imposes restrictions on the possible values of the "class" attribute.

D.1 Grammar

The grammar below is LL(1) (but note that most UA's should not use it directly, since it doesn't express the parsing conventions, only the CSS2 syntax). The format of the productions is optimized for human consumption and some shorthand notation beyond Yacc (see [YACC]) is used:

  • *: 0 or more
  • +: 1 or more
  • ?: 0 or 1
  • |: separates alternatives
  • [ ]: grouping

The productions are:

 
stylesheet
  : [ CHARSET_SYM S* STRING S* ';' ]?
    [S|CDO|CDC]* [ import [S|CDO|CDC]* ]*
    [ [ ruleset | media | page | font_face ] [S|CDO|CDC]* ]*
  ;
import
  : IMPORT_SYM S*
    [STRING|URI] S* [ medium [ ',' S* medium]* ]? ';' S*
  ;
media
  : MEDIA_SYM S* medium [ ',' S* medium ]* '{' S* ruleset* '}' S*
  ;
medium
  : IDENT S*
  ;
page
  : PAGE_SYM S* IDENT? pseudo_page? S*
    '{' S* declaration [ ';' S* declaration ]* '}' S*
  ;
pseudo_page
  : ':' IDENT
  ;
font_face
  : FONT_FACE_SYM S*
    '{' S* declaration [ ';' S* declaration ]* '}' S*
  ;
operator
  : '/' S* | ',' S* | /* empty */
  ;
combinator
  : '+' S* | '>' S* | /* empty */
  ;
unary_operator
  : '-' | '+'
  ;
property
  : IDENT S*
  ;
ruleset
  : selector [ ',' S* selector ]*
    '{' S* declaration [ ';' S* declaration ]* '}' S*
  ;
<a name="x1"><span class="index-inst" title="selector">selector</span></a>
  : simple_selector [ combinator simple_selector ]*
  ;
simple_selector
  : element_name? [ HASH | class | attrib | pseudo ]* S*
  ;
class
  : '.' IDENT
  ;
element_name
  : IDENT | '*'
  ;
attrib
  : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S*
    [ IDENT | STRING ] S* ]? ']'
  ;
pseudo
  : ':' [ IDENT | FUNCTION S* IDENT S* ')' ]
  ;
declaration
  : property ':' S* expr prio?
  | /* empty */
  ;
prio
  : IMPORTANT_SYM S*
  ;
expr
  : term [ operator term ]*
  ;
term
  : unary_operator?
    [ NUMBER S* | PERCENTAGE S* | LENGTH S* | EMS S* | EXS S* | ANGLE S* |
      TIME S* | FREQ S* | function ]
  | STRING S* | IDENT S* | URI S* | RGB S* | UNICODERANGE S* | hexcolor
  ;
function
  : FUNCTION S* expr ')' S*
  ;
/*
 * There is a constraint on the <a name="x2"><span class="index-inst" title="color">color</span></a> that it must
 * have either 3 or 6 hex-digits (i.e., [0-9a-fA-F])
 * after the "#"; e.g., "#000" is OK, but "#abcd" is not.
 */
hexcolor
  : HASH S*
  ;

D.2 Lexical scanner

The following is the tokenizer, written in Flex (see [FLEX]) notation. The tokenizer is case-insensitive.

The two occurrences of "\377" represent the highest character number that current versions of Flex can deal with (decimal 255). They should be read as "\4177777" (decimal 1114111), which is the highest possible code point in Unicode/ISO-10646.

 
%option case-insensitive
 
h    [0-9a-f]
nonascii  [200-377]
unicode    \{h}{1,6}[ trnf]?
escape    {unicode}|\[ -~200-377]
nmstart    [a-z]|{nonascii}|{escape}
nmchar    [a-z0-9-]|{nonascii}|{escape}
string1    "([\t !#$%&amp;(-~]|\\{nl}|\'|{nonascii}|{escape})*\"
string2    \'([\t !#$%&amp;(-~]|\\{nl}|\"|{nonascii}|{escape})*\'
 
ident    {nmstart}{nmchar}*
name    {nmchar}+
num    [0-9]+|[0-9]*"."[0-9]+
string    {string1}|{string2}
url    ([!#$%&amp;*-~]|{nonascii}|{escape})*
w    [ \t\r\n\f]*
nl    \n|\r\n|\r|\f
range    \?{1,6}|{h}(\?{0,5}|{h}(\?{0,4}|{h}(\?{0,3}|{h}(\?{0,2}|{h}(\??|{h})))))
 
%%
 
[ \t\r\n\f]+    {return S;}
 
\/\*[^*]*\*+([^/][^*]*\*+)*\/  /* ignore comments */
 
"<!--"      {return CDO;}
"-->"      {return CDC;}
"~="      {return INCLUDES;}
"|="      {return DASHMATCH;}
 
{string}    {return STRING;}
 
{ident}      {return IDENT;}
 
"#"{name}    {return HASH;}
 
"@import"    {return IMPORT_SYM;}
"@page"      {return PAGE_SYM;}
"@media"    {return MEDIA_SYM;}
"@font-face"    {return FONT_FACE_SYM;}
"@charset"    {return CHARSET_SYM;}
"@"{ident}    {return ATKEYWORD;}
 
"!{w}important"    {return IMPORTANT_SYM;}
 
{num}em      {return EMS;}
{num}ex      {return EXS;}
{num}px      {return LENGTH;}
{num}cm      {return LENGTH;}
{num}mm      {return LENGTH;}
{num}in      {return LENGTH;}
{num}pt      {return LENGTH;}
{num}pc      {return LENGTH;}
{num}deg    {return ANGLE;}
{num}rad    {return ANGLE;}
{num}grad    {return ANGLE;}
{num}ms      {return TIME;}
{num}s      {return TIME;}
{num}Hz      {return FREQ;}
{num}kHz    {return FREQ;}
{num}{ident}    {return DIMEN;}
{num}%      {return PERCENTAGE;}
{num}      {return NUMBER;}
 
"url("{w}{string}{w}")"  {return URI;}
"url("{w}{url}{w}")"  {return URI;}
{ident}"("    {return FUNCTION;}
 
U+{range}    {return UNICODERANGE;}
U+{h}{1,6}-{h}{1,6}  {return UNICODERANGE;}
 
.      {return *yytext;}

D.3 Comparison of tokenization in CSS2 and CSS1

There are some differences in the syntax specified in the CSS1 recommendation ([CSS1]), and the one above. Most of these are due to new tokens in CSS2 that didn't exist in CSS1. Others are because the grammar has been rewritten to be more readable. However, there are some incompatible changes, that were felt to be errors in the CSS1 syntax. They are explained below.

  • CSS1 style sheets could only be in 1-byte-per-character encodings, such as ASCII and ISO-8859-1. CSS2 has no such limitation. In practice, there was little difficulty in extrapolating the CSS1 tokenizer, and some UAs have accepted 2-byte encodings.
  • CSS1 only allowed four hex-digits after the backslash (\) to refer to Unicode characters, CSS2 allows six. Furthermore, CSS2 allows a whitespace character to delimit the escape sequence. E.g., according to CSS1, the string "\abcdef" has 3 letters (\abcd, e, and f), according to CSS2 it has only one (\abcdef).
  • The tab character (ASCII 9) was not allowed in strings. However, since strings in CSS1 were only used for font names and for URLs, the only way this can lead to incompatibility between CSS1 and CSS2 is if a style sheet contains a font family that has a tab in its name.
  • Similarly, newlines (escaped with a backslash) were not allowed in strings in CSS1.
  • CSS2 parses a number immediately followed by an identifier as a DIMEN token (i.e., an unknown unit), CSS1 parsed it as a number and an identifier. That means that in CSS1, the declaration 'font: 10pt/1.2serif' was correct, as was 'font: 10pt/12pt serif'; in CSS2, a space is required before "serif". (Some UAs accepted the first example, but not the second.)
  • In CSS1, a class name could start with a digit (".55ft"), unless it was a dimension (".55in"). In CSS2, such classes are parsed as unknown dimensions (to allow for future additions of new units). To make ".55ft" a valid class, CSS2 requires the first digit to be escaped (".\55ft")

Last update: 03-09-2007 21:33

Published in : Tools, CSS 2

Users' Comments (0) RSS feed comment

No comment posted

Add your comment



mXcomment 1.0.4 © 2007-2009 - visualclinic.fr
License Creative Commons - Some rights reserved
 
< Prev   Next >




Double click any word on this page for a definition.
Using Firefox? Enable definitions by downloading the extension.
Sorry, this feature does not currently work in Opera or Safari.

No Users Online

Statistics

OS: FreeBSD
PHP: 5.2.1
MySQL: 4.1.21-log
Time: 23:59
Caching: Disabled
GZIP: Disabled
Members: 36
News: 2448
Web Links: 39
Visitors: 1444011

Syndicate

Login

Particls