1. Introduction
This section is non-normative.
This CSS Namespaces module defines syntax for using namespaces in CSS. It defines the @namespace rule for declaring a default namespace and for binding namespaces to namespace prefixes. It also defines a syntax for using those prefixes to represent namespace-qualified names. It does not define where such names are valid or what they mean: that depends on their context and is defined by a host language, such as Selectors ([SELECT]), that references the syntax defined in the CSS Namespaces module.
Note that a CSS client that does not support this module will (if it properly conforms to CSS’s forward-compatible parsing rules) ignore all @namespace rules, as well as all style rules that make use of namespace qualified names. The syntax of delimiting namespace prefixes in CSS was deliberately chosen so that these CSS clients would ignore the style rules rather than possibly match them incorrectly.
1.1. Terminology
Besides terms introduced by this specification, CSS Namespaces uses the terminology defined in Namespaces in XML 1.0. [XML-NAMES] However, the syntax defined here is not restricted to representing XML element and attribute names and may represent other kinds of namespaces as defined by the host language.
In CSS Namespaces a namespace name consisting of the empty string is taken to represent the null namespace or lack of a namespace.
@namespace empty ""; @namespace "";
The type selectors elem
, |elem
,
and empty|elem
are equivalent.
2. Declaring namespaces: the @namespace rule
In all current engines.
Opera8+Edge79+
Edge (Legacy)12+IE9+
Firefox for Android?iOS Safari?Chrome for Android?Android WebView37+Samsung Internet?Opera Mobile?
In all current engines.
Opera8+Edge79+
Edge (Legacy)12+IE9+
Firefox for Android?iOS Safari1+Chrome for Android?Android WebView37+Samsung Internet?Opera Mobile?
The @namespace at-rule declares a namespace prefix and associates it with a given namespace name (a string). This namespace prefix can then be used in namespace-qualified names such as the CSS qualified names defined below.
@namespace "http://www.w3.org/1999/xhtml"; @namespace svg "http://www.w3.org/2000/svg";
The first rule declares a default namespace http://www.w3.org/1999/xhtml
to be applied to names that have no explicit namespace component.
The second rule declares a namespace prefix svg
that is used to apply the namespace http://www.w3.org/2000/svg
where the svg
namespace prefix is used.
In CSS Namespaces, as in Namespaces in XML 1.0, the prefix is merely a syntactic construct; it is the expanded name (the tuple of local name and namespace name) that is significant. Thus the actual prefixes used in a CSS style sheet, and whether they are defaulted or not, are independent of the namespace prefixes used in the markup and whether these are defaulted or not.
<qml:elem xmlns:qml="http://example.com/q-markup"></qml:elem>
and the following @namespace declarations at the beginning of a CSS file:
@namespace Q "http://example.com/q-markup"; @namespace lq "http://example.com/q-markup";
The selectors Q|elem and lq|elem in that CSS file
would both match the element <qml:elem>
.
(The selector qml|elem would be invalid, because CSS namespaces only recognize prefixes declared in CSS, not those declared by the document language.)
2.1. Syntax
The syntax for the @namespace rule is:
@namespace <namespace-prefix>? [ <string> | <url> ] ; <namespace-prefix> = <ident>
Any @namespace rules must follow all @charset and @import rules
and precede all other non-ignored at-rules and style rules in a style sheet.
For CSS syntax this adds [ namespace [SCDOCDC]* ]*
immediately after [ import [SCDOCDC]* ]*
in the stylesheet
grammar.
A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored. A CSS style sheet containing an invalid @namespace rule is not a valid style sheet.
A URI string parsed from the URI
syntax must be treated as a literal string:
as with the STRING
syntax,
no URI-specific normalization is applied.
All strings—
2.2. Scope
The namespace prefix is declared only within the style sheet in which its @namespace rule appears. It is not declared in any style sheets importing or imported by that style sheet, nor in any other style sheets applying to the document.
2.3. Declaring Prefixes
A namespace prefix, once declared, represents the namespace for which it was declared and can be used to indicate the namespace of a namespace-qualified name. Namespace prefixes are, like CSS counter names, case-sensitive.
If in the namespace declaration the namespace prefix is omitted,
then the namespace so declared is the default namespace.
The default namespace may apply to names that have no explicit namespace prefix:
modules that employ namespace prefixes must define in which contexts the default namespace applies.
For example, following [XML-NAMES],
in Selectors [SELECT] the default namespace applies to type selectors—
Note: Note that using default namespaces in conjunction with type selectors can cause UAs that support default namespaces and UAs that don’t support default namespaces to interpret selectors differently.
If a namespace prefix or default namespace is declared more than once only the last declaration shall be used. Declaring a namespace prefix or default namespace more than once is nonconforming.
3. CSS Qualified Names
A CSS qualified name is a name explicitly located within (associated with) a namespace.
To form a qualified name in CSS syntax,
a namespace prefix that has been declared within scope is prepended to a local name
(such as an element or attribute name),
separated by a "vertical bar" (|
, U+007C).
The prefix, representing the namespace for which it has been declared,
indicates the namespace of the local name.
The prefix of a qualified name may be omitted to indicate that the name belongs to no namespace,
i.e. that the namespace name part of the expanded name has no value.
Some contexts (as defined by the host language)
may allow the use of an asterisk (*
, U+002A)
as a wildcard prefix to indicate a name in any namespace,
including no namespace.
@namespace toto "http://toto.example.org"; @namespace "http://example.com/foo";
In a context where the default namespace applies
toto|A
- represents the name
A
in thehttp://toto.example.org
namespace. |B
- represents the name
B
that belongs to no namespace. *|C
- represents the name
C
in any namespace, including no namespace. D
- represents the name
D
in thehttp://example.com/foo
namespace.
The syntax for the portion of a CSS qualified name before the local name is given below,
both for qualified names that allow wildcard prefixes (wqname
)
and for qualified names that disallow wildcard prefixes (qname
).
(The syntax uses notation from the Grammar appendix of CSS 2.1. [CSS21] Note this means that comments, but not white space, are implicitly allowed between tokens.):
qname_prefix : namespace_prefix? '|' ; wqname_prefix : [ namespace_prefix? | '*' ] '|' ; qname : qname_prefix? ident ; wqname : wqname_prefix? ident ; wqwname : wqname_prefix? [ ident | '*' ] ;
CSS qualified names can be used in (for example) selectors and property values as described in other modules. Those modules must define handling of namespace prefixes that have not been properly declared. Such handling should treat undeclared namespace prefixes as a parsing error that will cause the selector or declaration (etc.) to be considered invalid and, in CSS, ignored.
Changes
Changes made since the 29 September 2011 Recommendation:
- Added predefined qname, wqname, and wqwname productions, to make those constructs easier for other specs to use.
Acknowledgments
This draft borrows heavily from earlier drafts on CSS namespace support by Chris Lilley and by Peter Linss and early (unpublished) drafts on CSS and XML by Håkon Lie and Bert Bos, and XML Namespaces and CSS by Bert Bos and Steven Pemberton. Many current and former members of the CSS Working Group have contributed to this document. Discussions on www-style@w3.org and in other places have also contributed ideas to this specification. Special thanks goes to L. David Baron, Karl Dubost, Ian Hickson, Björn Höhrmann, and Lachlan Hunt for their comments.