W3C

CSS Hierarchies Module Level 3

Editor's Draft 19 October 2011

This version:
http://dev.w3.org/csswg/css3-hierarchies/
Latest version:
http://www.w3.org/TR/css3-hierarchies/
Editor's draft:
http://dev.w3.org/csswg/css3-hierarchies/
Editors:
Shane Stephens, Google, Inc.,
Tab Atkins Jr., Google, Inc.

Abstract

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc. This module contains the features of CSS level 3 relating to the hierarchical nesting of style rules. It includes and extends the functionality of CSS level 2 [CSS21], which builds on CSS level 1 [CSS1]. The main extension compared to level 2 is the ability to nest a style rule within another rule, allowing greater modularisation and readibility of CSS documents.

Status of this document

This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don't cite this document other than as work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-hierarchies” in the subject, preferably like this: “[css3-hierarchies] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

The following features are at risk: …

Table of contents

1. Introduction

This section is not normative.

CSS beyond level 2 is a set of modules, divided up to allow the specifications to develop incrementally, along with their implementations. This specification is one of those modules.

This module describes support for nesting a style rule within another style rule, causing the inner rule to adopt the selector of the outer rule as a selector prefix. This feature allows related styles to be aggregated into a single structure within the CSS document, improving readability and maintainability.

1.1. Module Interactions

This module introduces new parser rules that extend the [CSS21] parser model. This module introduces new parser rules that extend the [[CSS4SELECTORS]] module.

1.2. Values

This specification does not define any new properties or values.

1.3. Motivation

CSS Rules for even moderately complicated web pages include lots of duplication for the purpose of styling related content. For example, here is a portion of the CSS markup for one version of the [CSS3COLOR] module:

table.colortable td {text-align:center }
table.colortable td.c { text-transform:uppercase }
table.colortable td:first-child, table.colortable td:first-child+td { border:1px solid black }
table.colortable th {text-align:center; background:black; color:white }

table.tprofile th.title {background:gray; color:white}
table.tprofile th { width:29%;padding:2px }
table.tprofile td { width:71%;padding:2px }

table.hslexample { background: #808080; padding:1em; margin:0; float:left; }
table.hslexample td,table.hslexample th { font-size:smaller;width:3em }

Hierarchies allow the grouping of related style rules, like this:

table {
	&.colortable {
		& td {text-align:center }
		&.c { text-transform:uppercase }
		& td:first-child, & td:first-child+td { border:1px solid black }
		& th {text-align:center; background:black; color:white }

	&.tprofile {
		& th {
			width:29%;padding:2px;
			&.title {background:gray; color:white}
		}
		& td { width:71%;padding:2px }
	}

	&.hslexample { 
		background: #808080; padding:1em; margin:0; float:left;
		& td, & th { font-size:smaller;width:3em }
	}
}

Besides removing duplication, the grouping of related rules improves readability and maintainability of the resulting CSS.

2. The & Selector

This specification provides a mechanism that allows for the nesting of style rules within other style rules. A nested style rule can used anywhere that a declaration can be used.

In order to accomplish nesting, this specification defines a new simple selector called the nesting selector, represented in selectors by the ‘&’ character. All complex selectors in the selector lists of nested rule sets must start with the nesting selector. The nesting selector represents the elements matches by the parent rule set's selector list.

The following example using Hierarchies:

div {
	& .keyword {color: red;}
	&:hover {background-color: rgb(200, 255, 255);}
}

...produces the same results as the following CSS:

div .keyword {color:red;}

div:hover {background-color: rgb(200, 255, 255);}

The following example using Hierarchies:

div, p {
	& .keyword, & .constant {color: red;}
}

...produces the same results as the following CSS:

div .keyword {color:red;}
div .constant {color:red;}
p .keyword {color:red;}
p .constant {color:red;}

Multiple style rules can be embedded within a style rule. Style rules can be embedded arbitrarily deeply. Embedded style rules and properties can co-exist.

The following example using Hierarchies:

div, p {
	& .keyword {color: green;} 
	font-size: 10px;
	& .constant {
		color: red;
		&:hover:after { content: " [" attr(value) "]";}
		background-color: green;
	}
}

...produces the same results as the following CSS:

div, p {font-size: 10px;}
div .keyword, p .keyword {color: green;}
div .constant, p .constant {color: red; background-color: green;}
div .constant:hover:after, p .constant:hover:after {content: " [" attr(value) "]";}

3. Grammar modifications

This specification alters the CSS Core Grammar and the Selectors grammar.

3.1. Core CSS Grammar

The following modifications are required to the [CSS21] grammar as defined in Section 4.1.1.

The ruleset rule is changed to:

ruleset : selectors_group? ruleset-body

The following rules are added:

ruleset-body           : '{' S* ruleitem? [ ';' S* ruleitem? ]* '}' S*
ruleitem               : declaration | nested-ruleset
nested-ruleset         : nested-selectors-group ruleset-body

3.2. Selectors 4 Grammar

The following modifications are required to the [[!CSS4SELECTORS]] grammar as defined in Section 16.1.

The following rules are added:

nested-selectors-group   : nested-selector [ COMMA S* nested-selector ]
nested-selector          : nested-compound-selector [ combinator compound-selector ]*
nested-compound-selector : NEST [ HASH | class | attrib | pseudo | negation ]*

3.3. Selectors 4 Lexical Scanner

The following modifications are required to the [[!CSS4SELECTORS]] lexical scanner as defined in Section 16.2.

Immediately below:

"-->"            return CDC;

the following rule is inserted:

"&"              return NEST;

4. CSS Object Model Modifications

The following attribute is required to be added to the CSSStyleRule object defined in Section 6.4.3 of [CSSOM]:

interface CSSStyleRule : CSSRule {
           attribute DOMString selectorText;
  readonly attribute CSSRuleList cssRules;
  readonly attribute CSSStyleDeclaration style;
};

The cssRules attribute must return a CSSRuleList object for the list of CSS rules specified within the style rule.

5. Conformance

5.1. Document Conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words “for example” or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word “Note” and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

5.2. Conformance Classes

Conformance to CSS TEMPLATE Module is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to CSS TEMPLATE Module if all of its declarations that use properties defined in this module have values that are valid according to the generic CSS grammar and the individual grammars of each property as given in this module.

A renderer is conformant to CSS TEMPLATE Module if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by CSS TEMPLATE Module by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to CSS TEMPLATE Module if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

5.3. Partial Implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

5.4. Experimental Implementations

To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.

Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.

5.5. Non-Experimental Implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group's website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

5.6. CR Exit Criteria

[Change or remove the following CR exit criteria if the spec is not a module, but, e.g., a Note or a profile. This text was decided on 2008-06-04.]

For this specification to be advanced to Proposed Recommendation, there must be at least two independent, interoperable implementations of each feature. Each feature may be implemented by a different set of products, there is no requirement that all features be implemented by a single product. For the purposes of this criterion, we define the following terms:

independent
each implementation must be developed by a different party and cannot share, reuse, or derive from code used by another qualifying implementation. Sections of code that have no bearing on the implementation of this specification are exempt from this requirement.
interoperable
passing the respective test case(s) in the official CSS test suite, or, if the implementation is not a Web browser, an equivalent test. Every relevant test in the test suite should have an equivalent test created if such a user agent (UA) is to be used to claim interoperability. In addition if such a UA is to be used to claim interoperability, then there must one or more additional UAs which can also pass those equivalent tests in the same way for the purpose of interoperability. The equivalent tests must be made publicly available for the purposes of peer review.
implementation
a user agent which:
  1. implements the specification.
  2. is available to the general public. The implementation may be a shipping product or other publicly available version (i.e., beta version, preview release, or “nightly build”). Non-shipping product releases must have implemented the feature(s) for a period of at least one month in order to demonstrate stability.
  3. is not experimental (i.e., a version specifically designed to pass the test suite and is not intended for normal usage going forward).

The specification will remain Candidate Recommendation for at least six months.

Acknowledgments

[acknowledgments]

References

Normative references

[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-CSS2-20110607/
[CSSOM]
Anne van Kesteren. CSSOM. 12 July 2011. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2011/WD-cssom-20110712/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. Internet RFC 2119. URL: http://www.ietf.org/rfc/rfc2119.txt

Other references

[CSS1]
Håkon Wium Lie; Bert Bos. Cascading Style Sheets (CSS1) Level 1 Specification. 11 April 2008. W3C Recommendation. URL: http://www.w3.org/TR/2008/REC-CSS1-20080411
[CSS3COLOR]
Tantek Çelik; Chris Lilley; L. David Baron. CSS Color Module Level 3. 7 June 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-css3-color-20110607/

Index