An H1
13 |Some text
14 |Some more text
15 |An H2
16 |Another
17 | Bob 18 |Another H2
19 | me 20 | Test foo1bar1 21 | bar2 22 | OK foo2 seriously 23 |English
25 |English UK
26 |English US
27 |French
28 |├── AUTHORS ├── .gitmodules ├── test.js ├── nodelint.js ├── package.json ├── BENCHMARK.md ├── testdata ├── test.html └── benchmark.html ├── benchmark.js ├── example.js ├── README.md ├── lib └── soupselect.js └── tests └── soupselect.js /AUTHORS: -------------------------------------------------------------------------------- 1 | # Authors ordered by first contribution 2 | 3 | Simon Willison https://github.com/simonw 4 | Harry Fuecks https://github.com/harryf 5 | Chris O'Hara https://github.com/chriso -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "deps/nodeunit"] 2 | path = deps/nodeunit 3 | url = git://github.com/caolan/nodeunit.git 4 | [submodule "deps/htmlparser"] 5 | path = deps/htmlparser 6 | url = http://github.com/tautologistics/node-htmlparser.git 7 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | require.paths.unshift('lib') 2 | require.paths.push('.') 3 | require.paths.push('deps/nodeunit/lib') 4 | 5 | var reporter = require('reporters/default'); 6 | var args = process.ARGV.slice(2); 7 | if(args.length > 0) { 8 | reporter.run(args); 9 | } else { 10 | reporter.run(['tests']) 11 | } 12 | -------------------------------------------------------------------------------- /nodelint.js: -------------------------------------------------------------------------------- 1 | // nodelint (JSLint) options 2 | // run like: nodelint lib/soupselect.js --config nodelint.js 3 | var options = { 4 | adsafe : false, 5 | bitwise : false, 6 | browser : false, 7 | error_prefix : "\u001b[1m", 8 | error_suffix : ":\u001b[0m ", 9 | onevar : false, 10 | plusplus : false, 11 | regexp : false, 12 | undef : false, 13 | white : false, 14 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "soupselect", 3 | "version": "0.2.0", 4 | "engines": { 5 | "node": ">=0.2.0" 6 | }, 7 | "author": { 8 | "name": "Harry Fuecks", 9 | "email": "hfuecks@gmail.com", 10 | "url": "http://twitter.com/hfuecks" 11 | }, 12 | "url": "http://github.com/harryf/node-soupselect", 13 | "dependencies": { 14 | "htmlparser": ">= 1.6.2", 15 | "nodeunit": ">= 0.3.0" 16 | }, 17 | "repository" : [ 18 | { "type":"git", "url":"git://github.com/harryf/node-soupselect.git" } 19 | ], 20 | "main": "./lib/soupselect", 21 | "license": "MIT", 22 | "description": "Adds CSS selector support to htmlparser for scraping activities - port of soupselect (python)" 23 | } 24 | -------------------------------------------------------------------------------- /BENCHMARK.md: -------------------------------------------------------------------------------- 1 | A log of benchmarks to track changes across releases. See benchmark.js 2 | 3 | 2010.10.06, version 0.1.0 4 | ------------------------- 5 | 6 | body : 4ms, 1 elements 7 | div : 3ms, 51 elements 8 | body div : 7ms, 51 elements 9 | div p : 4ms, 137 elements 10 | div p a : 3ms, 29 elements 11 | .note : 3ms, 14 elements 12 | div.example : 2ms, 43 elements 13 | ul .tocline2 : 3ms, 12 elements 14 | #title : 0ms, 1 elements 15 | h1#title : 0ms, 1 elements 16 | div #title : 3ms, 1 elements 17 | ul.toc li.tocline2 : 2ms, 12 elements 18 | div[class] : 3ms, 51 elements 19 | div[class=example] : 2ms, 43 elements 20 | div[class^=exa] : 3ms, 43 elements 21 | div[class$=mple] : 3ms, 43 elements 22 | div[class*=e] : 2ms, 50 elements 23 | div[class|=dialog] : 5ms, 0 elements 24 | div[class!=made_up] : 2ms, 51 elements 25 | div[class~=example] : 6ms, 43 elements 26 | -------------------------------------------------------------------------------- /testdata/test.html: -------------------------------------------------------------------------------- 1 | 3 | 4 |
5 |Some text
14 |Some more text
15 |Another
17 | Bob 18 |English
25 |English UK
26 |English US
27 |French
28 |87 | Copyright © 2005 W3C® 89 | (MIT, ERCIM, Keio), All Rights Reserved. W3C 94 | liability, 96 | trademark, 98 | document 100 | use rules apply. 101 | 102 |
Selectors are patterns that match against elements in a 109 | tree. Selectors have been optimized for use with HTML and XML, and 110 | are designed to be usable in performance-critical code.
111 | 112 |CSS (Cascading 113 | Style Sheets) is a language for describing the rendering of HTML and XML documents on 116 | screen, on paper, in speech, etc. CSS uses Selectors for binding 117 | style properties to elements in the document. This document 118 | describes extensions to the selectors defined in CSS level 2. These 119 | extended selectors will be used by CSS level 3. 120 | 121 |
Selectors define the following function:
122 | 123 |expression ∗ element → boolean124 | 125 |
That is, given an element and a selector, this specification 126 | defines whether that element matches the selector.
127 | 128 |These expressions can also be used, for instance, to select a set 129 | of elements, or a single element from a set of elements, by 130 | evaluating the expression across all the elements in a 131 | subtree. STTS (Simple Tree Transformation Sheets), a 133 | language for transforming XML trees, uses this mechanism. [STTS]
134 | 135 |This section describes the status of this document at the 138 | time of its publication. Other documents may supersede this 139 | document. A list of current W3C publications and the latest revision 140 | of this technical report can be found in the W3C technical reports index at 142 | http://www.w3.org/TR/.
143 | 144 |This document describes the selectors that already exist in CSS1 and CSS2, and 147 | also proposes new selectors for CSS3 and other languages that may need them.
149 | 150 |The CSS Working Group doesn't expect that all implementations of 151 | CSS3 will have to implement all selectors. Instead, there will 152 | probably be a small number of variants of CSS3, called profiles. For 153 | example, it may be that only a profile for interactive user agents 154 | will include all of the selectors.
155 | 156 |This specification is a last call working draft for the the CSS Working Group 158 | (Style Activity). This 159 | document is a revision of the Candidate 161 | Recommendation dated 2001 November 13, and has incorporated 162 | implementation feedback received in the past few years. It is 163 | expected that this last call will proceed straight to Proposed 164 | Recommendation stage since it is believed that interoperability will 165 | be demonstrable.
166 | 167 |All persons are encouraged to review and implement this 168 | specification and return comments to the (archived) 170 | public mailing list www-style 172 | (see instructions). W3C 173 | Members can also send comments directly to the CSS Working 174 | Group. 175 | The deadline for comments is 14 January 2006.
176 | 177 |This is still a draft document and may be updated, replaced, or 178 | obsoleted by other documents at any time. It is inappropriate to 179 | cite a W3C Working Draft as other than "work in progress". 180 | 181 |
This document may be available in translation. 183 | The English version of this specification is the only normative 184 | version. 185 | 186 |
Some features of this specification are specific to CSS, or have 284 | particular limitations or rules specific to CSS. In this 285 | specification, these have been described in terms of CSS2.1. [CSS21]
287 | 288 |All of the text of this specification is normative except 291 | examples, notes, and sections explicitly marked as 292 | non-normative.
293 | 294 |This section is non-normative.
297 | 298 |The main differences between the selectors in CSS2 and those in 299 | Selectors are: 300 | 301 |
This section is non-normative, as it merely summarizes the 337 | following sections.
338 | 339 |A Selector represents a structure. This structure can be used as a 340 | condition (e.g. in a CSS rule) that determines which elements a 341 | selector matches in the document tree, or as a flat description of the 342 | HTML or XML fragment corresponding to that structure.
343 | 344 |Selectors may range from simple element names to rich contextual 345 | representations.
346 | 347 |The following table summarizes the Selector syntax:
348 | 349 || Pattern | 353 |Meaning | 354 |Described in section | 355 |First defined in CSS level |
|---|---|---|---|
| * | 359 |any element | 360 |Universal 362 | selector | 363 |2 |
| E | 366 |an element of type E | 367 |Type selector | 369 |1 |
| E[foo] | 372 |an E element with a "foo" attribute | 373 |Attribute 375 | selectors | 376 |2 |
| E[foo="bar"] | 379 |an E element whose "foo" attribute value is exactly 380 | equal to "bar" | 381 |Attribute 383 | selectors | 384 |2 |
| E[foo~="bar"] | 387 |an E element whose "foo" attribute value is a list of 388 | space-separated values, one of which is exactly equal to "bar" | 389 |Attribute 391 | selectors | 392 |2 |
| E[foo^="bar"] | 395 |an E element whose "foo" attribute value begins exactly 396 | with the string "bar" | 397 |Attribute 399 | selectors | 400 |3 |
| E[foo$="bar"] | 403 |an E element whose "foo" attribute value ends exactly 404 | with the string "bar" | 405 |Attribute 407 | selectors | 408 |3 |
| E[foo*="bar"] | 411 |an E element whose "foo" attribute value contains the 412 | substring "bar" | 413 |Attribute 415 | selectors | 416 |3 |
| E[hreflang|="en"] | 419 |an E element whose "hreflang" attribute has a hyphen-separated 420 | list of values beginning (from the left) with "en" | 421 |Attribute 423 | selectors | 424 |2 |
| E:root | 427 |an E element, root of the document | 428 |Structural 430 | pseudo-classes | 431 |3 |
| E:nth-child(n) | 434 |an E element, the n-th child of its parent | 435 |Structural 437 | pseudo-classes | 438 |3 |
| E:nth-last-child(n) | 441 |an E element, the n-th child of its parent, counting 442 | from the last one | 443 |Structural 445 | pseudo-classes | 446 |3 |
| E:nth-of-type(n) | 449 |an E element, the n-th sibling of its type | 450 |Structural 452 | pseudo-classes | 453 |3 |
| E:nth-last-of-type(n) | 456 |an E element, the n-th sibling of its type, counting 457 | from the last one | 458 |Structural 460 | pseudo-classes | 461 |3 |
| E:first-child | 464 |an E element, first child of its parent | 465 |Structural 467 | pseudo-classes | 468 |2 |
| E:last-child | 471 |an E element, last child of its parent | 472 |Structural 474 | pseudo-classes | 475 |3 |
| E:first-of-type | 478 |an E element, first sibling of its type | 479 |Structural 481 | pseudo-classes | 482 |3 |
| E:last-of-type | 485 |an E element, last sibling of its type | 486 |Structural 488 | pseudo-classes | 489 |3 |
| E:only-child | 492 |an E element, only child of its parent | 493 |Structural 495 | pseudo-classes | 496 |3 |
| E:only-of-type | 499 |an E element, only sibling of its type | 500 |Structural 502 | pseudo-classes | 503 |3 |
| E:empty | 506 |an E element that has no children (including text 507 | nodes) | 508 |Structural 510 | pseudo-classes | 511 |3 |
| E:link E:visited |
514 | an E element being the source anchor of a hyperlink of 515 | which the target is not yet visited (:link) or already visited 516 | (:visited) | 517 |The link 519 | pseudo-classes | 520 |1 |
| E:active E:hover E:focus |
523 | an E element during certain user actions | 524 |The user 526 | action pseudo-classes | 527 |1 and 2 |
| E:target | 530 |an E element being the target of the referring URI | 531 |The target 533 | pseudo-class | 534 |3 |
| E:lang(fr) | 537 |an element of type E in language "fr" (the document 538 | language specifies how language is determined) | 539 |The :lang() 541 | pseudo-class | 542 |2 |
| E:enabled E:disabled |
545 | a user interface element E which is enabled or 546 | disabled | 547 |The UI element states 549 | pseudo-classes | 550 |3 |
| E:checked | 553 |a user interface element E which is checked (for instance a radio-button or checkbox) | 555 |The UI element states 557 | pseudo-classes | 558 |3 |
| E::first-line | 561 |the first formatted line of an E element | 562 |The ::first-line 564 | pseudo-element | 565 |1 |
| E::first-letter | 568 |the first formatted letter of an E element | 569 |The ::first-letter 571 | pseudo-element | 572 |1 |
| E::selection | 575 |the portion of an E element that is currently 576 | selected/highlighted by the user | 577 |The UI element 579 | fragments pseudo-elements | 580 |3 |
| E::before | 583 |generated content before an E element | 584 |The ::before 586 | pseudo-element | 587 |2 |
| E::after | 590 |generated content after an E element | 591 |The ::after 593 | pseudo-element | 594 |2 |
| E.warning | 597 |an E element whose class is 598 | "warning" (the document language specifies how class is determined). | 599 |Class 601 | selectors | 602 |1 |
| E#myid | 605 |an E element with ID equal to "myid". | 606 |ID 608 | selectors | 609 |1 |
| E:not(s) | 612 |an E element that does not match simple selector s | 613 |Negation 615 | pseudo-class | 616 |3 |
| E F | 619 |an F element descendant of an E element | 620 |Descendant 622 | combinator | 623 |1 |
| E > F | 626 |an F element child of an E element | 627 |Child 629 | combinator | 630 |2 |
| E + F | 633 |an F element immediately preceded by an E element | 634 |Adjacent sibling combinator | 636 |2 |
| E ~ F | 639 |an F element preceded by an E element | 640 |General sibling combinator | 642 |3 |
The meaning of each selector is derived from the table above by 645 | prepending "matches" to the contents of each cell in the "Meaning" 646 | column.
647 | 648 |The case sensitivity of document language element names, attribute 651 | names, and attribute values in selectors depends on the document 652 | language. For example, in HTML, element names are case-insensitive, 653 | but in XML, they are case-sensitive.
654 | 655 |A selector is a chain of one 658 | or more sequences of simple selectors 659 | separated by combinators.
660 | 661 |A sequence of simple selectors 662 | is a chain of simple selectors 663 | that are not separated by a combinator. It 664 | always begins with a type selector or a 665 | universal selector. No other type 666 | selector or universal selector is allowed in the sequence.
667 | 668 |A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, content selector, or pseudo-class. One pseudo-element may be appended to the last 678 | sequence of simple selectors.
679 | 680 |Combinators are: white space, "greater-than
681 | sign" (U+003E, >), "plus sign" (U+002B,
682 | +) and "tilde" (U+007E, ~). White
683 | space may appear between a combinator and the simple selectors around
684 | it. Only the characters "space" (U+0020), "tab"
685 | (U+0009), "line feed" (U+000A), "carriage return" (U+000D), and "form
686 | feed" (U+000C) can occur in white space. Other space-like characters,
687 | such as "em-space" (U+2003) and "ideographic space" (U+3000), are
688 | never part of white space.
The elements of a document tree that are represented by a selector 691 | are the subjects of the selector. A 692 | selector consisting of a single sequence of simple selectors 693 | represents any element satisfying its requirements. Prepending another 694 | sequence of simple selectors and a combinator to a sequence imposes 695 | additional matching constraints, so the subjects of a selector are 696 | always a subset of the elements represented by the last sequence of 697 | simple selectors.
698 | 699 |An empty selector, containing no sequence of simple selectors and 700 | no pseudo-element, is an invalid 701 | selector.
702 | 703 |When several selectors share the same declarations, they may be 706 | grouped into a comma-separated list. (A comma is U+002C.)
707 | 708 |CSS examples:
710 |In this example, we condense three rules with identical 711 | declarations into one. Thus,
712 |h1 { font-family: sans-serif }
713 | h2 { font-family: sans-serif }
714 | h3 { font-family: sans-serif }
715 | is equivalent to:
716 |h1, h2, h3 { font-family: sans-serif }
717 | Warning: the equivalence is true in this example 720 | because all the selectors are valid selectors. If just one of these 721 | selectors were invalid, the entire group of selectors would be 722 | invalid. This would invalidate the rule for all three heading 723 | elements, whereas in the former case only one of the three individual 724 | heading rules would be invalidated.
725 | 726 | 727 |A type selector is the name of a document language 732 | element type. A type selector represents an instance of the element 733 | type in the document tree.
734 | 735 |Example:
737 |The following selector represents an h1 element in the document tree:
h1739 |
Type selectors allow an optional namespace ([XMLNAMES]) component. A namespace prefix
746 | that has been previously declared may be prepended to the element name
747 | separated by the namespace separator "vertical bar"
748 | (U+007C, |).
The namespace component may be left empty to indicate that the 751 | selector is only to represent elements with no declared namespace.
752 | 753 |An asterisk may be used for the namespace prefix, indicating that 754 | the selector represents elements in any namespace (including elements 755 | with no namespace).
756 | 757 |Element type selectors that have no namespace component (no
758 | namespace separator), represent elements without regard to the
759 | element's namespace (equivalent to "*|") unless a default
760 | namespace has been declared. If a default namespace has been declared,
761 | the selector will represent only elements in the default
762 | namespace.
A type selector containing a namespace prefix that has not been 765 | previously declared is an invalid selector. 766 | The mechanism for declaring a namespace prefix is left up to the 767 | language implementing Selectors. In CSS, such a mechanism is defined 768 | in the General Syntax module.
769 | 770 |In a namespace-aware client, element type selectors will only match 771 | against the local part 773 | of the element's qualified 775 | name. See below for notes about matching 776 | behaviors in down-level clients.
777 | 778 |In summary:
779 | 780 |ns|E*|E|EECSS examples:
795 | 796 |@namespace foo url(http://www.example.com);
797 | foo|h1 { color: blue }
798 | foo|* { color: yellow }
799 | |h1 { color: red }
800 | *|h1 { color: green }
801 | h1 { color: green }
802 |
803 | The first rule will match only h1 elements in the
804 | "http://www.example.com" namespace.
The second rule will match all elements in the 807 | "http://www.example.com" namespace.
808 | 809 |The third rule will match only h1 elements without
810 | any declared namespace.
The fourth rule will match h1 elements in any
813 | namespace (including those without any declared namespace).
The last rule is equivalent to the fourth rule because no default 816 | namespace has been defined.
817 | 818 |The universal selector, written "asterisk"
823 | (*), represents the qualified name of any element
824 | type. It represents any single element in the document tree in any
825 | namespace (including those without any declared namespace) if no
826 | default namespace has been specified. If a default namespace has been
827 | specified, see Universal selector and
828 | Namespaces below.
If the universal selector is not the only component of a sequence
831 | of simple selectors, the * may be omitted.
Examples:
835 |*[hreflang|=en] and [hreflang|=en] are equivalent,*.warning and .warning are equivalent,*#myid and #myid are equivalent.Note: it is recommended that the
843 | *, representing the universal selector, not be
844 | omitted.
The universal selector allows an optional namespace component. It 849 | is used as follows:
850 | 851 |ns|**|*|**A universal selector containing a namespace prefix that has not 864 | been previously declared is an invalid 865 | selector. The mechanism for declaring a namespace prefix is left up 866 | to the language implementing Selectors. In CSS, such a mechanism is 867 | defined in the General Syntax module.
868 | 869 | 870 |Selectors allow the representation of an element's attributes. When 873 | a selector is used as an expression to match against an element, 874 | attribute selectors must be considered to match an element if that 875 | element has an attribute that matches the attribute represented by the 876 | attribute selector.
877 | 878 |CSS2 introduced four attribute selectors:
882 | 883 |[att]
885 | att attribute, whatever the value of
886 | the attribute.[att=val]att attribute whose value is exactly
889 | "val".[att~=val]att attribute whose value is a whitespace-separated list of words, one of
893 | which is exactly "val". If "val" contains whitespace, it will never
894 | represent anything (since the words are separated by
895 | spaces).[att|=val]
897 | att attribute, its value either
898 | being exactly "val" or beginning with "val" immediately followed by
899 | "-" (U+002D). This is primarily intended to allow language subcode
900 | matches (e.g., the hreflang attribute on the
901 | link element in HTML) as described in RFC 3066 ([RFC3066]). For lang (or
903 | xml:lang) language subcode matching, please see the :lang pseudo-class.Attribute values must be identifiers or strings. The 908 | case-sensitivity of attribute names and values in selectors depends on 909 | the document language.
910 | 911 |Examples:
914 | 915 |The following attribute selector represents an h1
916 | element that carries the title attribute, whatever its
917 | value:
h1[title]920 | 921 |
In the following example, the selector represents a
922 | span element whose class attribute has
923 | exactly the value "example":
span[class="example"]926 | 927 |
Multiple attribute selectors can be used to represent several
928 | attributes of an element, or several conditions on the same
929 | attribute. Here, the selector represents a span element
930 | whose hello attribute has exactly the value "Cleveland"
931 | and whose goodbye attribute has exactly the value
932 | "Columbus":
span[hello="Cleveland"][goodbye="Columbus"]935 | 936 |
The following selectors illustrate the differences between "="
937 | and "~=". The first selector will represent, for example, the value
938 | "copyright copyleft copyeditor" on a rel attribute. The
939 | second selector will only represent an a element with
940 | an href attribute having the exact value
941 | "http://www.w3.org/".
a[rel~="copyright"] 944 | a[href="http://www.w3.org/"]945 | 946 |
The following selector represents a link element
947 | whose hreflang attribute is exactly "fr".
link[hreflang=fr]950 | 951 |
The following selector represents a link element for
952 | which the values of the hreflang attribute begins with
953 | "en", including "en", "en-US", and "en-cockney":
link[hreflang|="en"]956 | 957 |
Similarly, the following selectors represents a
958 | DIALOGUE element whenever it has one of two different
959 | values for an attribute character:
DIALOGUE[character=romeo] 962 | DIALOGUE[character=juliet]963 | 964 |
Three additional attribute selectors are provided for matching 970 | substrings in the value of an attribute:
971 | 972 |[att^=val]att attribute whose value begins
975 | with the prefix "val".[att$=val]
977 | att attribute whose value ends with
978 | the suffix "val".[att*=val]
980 | att attribute whose value contains
981 | at least one instance of the substring "val".Attribute values must be identifiers or strings. The 985 | case-sensitivity of attribute names in selectors depends on the 986 | document language.
987 | 988 |Examples:
990 |The following selector represents an HTML object, referencing an
991 | image:
object[type^="image/"]993 |
The following selector represents an HTML anchor a with an
994 | href attribute whose value ends with ".html".
a[href$=".html"]996 |
The following selector represents an HTML paragraph with a title
997 | attribute whose value contains the substring "hello"
p[title*="hello"]999 |
Attribute selectors allow an optional namespace component to the
1004 | attribute name. A namespace prefix that has been previously declared
1005 | may be prepended to the attribute name separated by the namespace
1006 | separator "vertical bar" (|). In keeping with
1007 | the Namespaces in the XML recommendation, default namespaces do not
1008 | apply to attributes, therefore attribute selectors without a namespace
1009 | component apply only to attributes that have no declared namespace
1010 | (equivalent to "|attr"). An asterisk may be used for the
1011 | namespace prefix indicating that the selector is to match all
1012 | attribute names without regard to the attribute's namespace.
1013 |
1014 |
An attribute selector with an attribute name containing a namespace 1015 | prefix that has not been previously declared is an invalid selector. The mechanism for declaring 1017 | a namespace prefix is left up to the language implementing Selectors. 1018 | In CSS, such a mechanism is defined in the General Syntax module. 1019 | 1020 |
CSS examples:
1022 |@namespace foo "http://www.example.com";
1023 | [foo|att=val] { color: blue }
1024 | [*|att] { color: yellow }
1025 | [|att] { color: green }
1026 | [att] { color: green }
1027 |
1028 | The first rule will match only elements with the attribute
1029 | att in the "http://www.example.com" namespace with the
1030 | value "val".
The second rule will match only elements with the attribute
1033 | att regardless of the namespace of the attribute
1034 | (including no declared namespace).
The last two rules are equivalent and will match only elements
1037 | with the attribute att where the attribute is not
1038 | declared to be in a namespace.
Attribute selectors represent explicitly set attribute values in 1045 | the document tree. Default attribute values may be defined in a DTD or 1046 | elsewhere, but cannot always be selected by attribute 1047 | selectors. Selectors should be designed so that they work even if the 1048 | default values are not included in the document tree.
1049 | 1050 |More precisely, a UA is not required to read an "external 1051 | subset" of the DTD but is required to look for default 1052 | attribute values in the document's "internal subset." (See [XML10] for definitions of these subsets.)
1054 | 1055 |A UA that recognizes an XML namespace [XMLNAMES] is not required to use its 1057 | knowledge of that namespace to treat default attribute values as if 1058 | they were present in the document. (For example, an XHTML UA is not 1059 | required to use its built-in knowledge of the XHTML DTD.)
1060 | 1061 |Note: Typically, implementations 1062 | choose to ignore external subsets.
1063 | 1064 |Example:
1066 | 1067 |Consider an element EXAMPLE with an attribute "notation" that has a 1068 | default value of "decimal". The DTD fragment might be
1069 | 1070 |<!ATTLIST EXAMPLE notation (decimal,octal) "decimal">1071 | 1072 |
If the style sheet contains the rules
1073 | 1074 |EXAMPLE[notation=decimal] { /*... default property settings ...*/ }
1075 | EXAMPLE[notation=octal] { /*... other settings...*/ }
1076 |
1077 | the first rule will not match elements whose "notation" attribute 1078 | is set by default, i.e. not set explicitly. To catch all cases, the 1079 | attribute selector for the default value must be dropped:
1080 | 1081 |EXAMPLE { /*... default property settings ...*/ }
1082 | EXAMPLE[notation=octal] { /*... other settings...*/ }
1083 |
1084 | Here, because the selector EXAMPLE[notation=octal] is
1085 | more specific than the tag
1086 | selector alone, the style declarations in the second rule will override
1087 | those in the first for elements that have a "notation" attribute value
1088 | of "octal". Care has to be taken that all property declarations that
1089 | are to apply only to the default case are overridden in the non-default
1090 | cases' style rules.
Working with HTML, authors may use the period (U+002E,
1097 | .) notation as an alternative to the ~=
1098 | notation when representing the class attribute. Thus, for
1099 | HTML, div.value and div[class~=value] have
1100 | the same meaning. The attribute value must immediately follow the
1101 | "period" (.).
UAs may apply selectors using the period (.) notation in XML 1104 | documents if the UA has namespace-specific knowledge that allows it to 1105 | determine which attribute is the "class" attribute for the 1106 | respective namespace. One such example of namespace-specific knowledge 1107 | is the prose in the specification for a particular namespace (e.g. SVG 1108 | 1.0 [SVG] describes the SVG 1110 | "class" attribute and how a UA should interpret it, and 1111 | similarly MathML 1.01 [MATH] describes the MathML 1113 | "class" attribute.)
1114 | 1115 |CSS examples:
1117 | 1118 |We can assign style information to all elements with
1119 | class~="pastoral" as follows:
*.pastoral { color: green } /* all elements with class~=pastoral */
1122 |
1123 | or just
1124 | 1125 |.pastoral { color: green } /* all elements with class~=pastoral */
1126 |
1127 | The following assigns style only to H1 elements with
1128 | class~="pastoral":
H1.pastoral { color: green } /* H1 elements with class~=pastoral */
1131 |
1132 | Given these rules, the first H1 instance below would not have 1133 | green text, while the second would:
1134 | 1135 |<H1>Not green</H1> 1136 | <H1 class="pastoral">Very green</H1>1137 | 1138 |
To represent a subset of "class" values, each value must be preceded 1141 | by a ".", in any order.
1142 | 1143 |CSS example:
1146 | 1147 |The following rule matches any P element whose "class" attribute 1148 | has been assigned a list of whitespace-separated values that includes 1150 | "pastoral" and "marine":
1151 | 1152 |p.pastoral.marine { color: green }
1153 |
1154 | This rule matches when class="pastoral blue aqua
1155 | marine" but does not match for class="pastoral
1156 | blue".
Note: Because CSS gives considerable 1161 | power to the "class" attribute, authors could conceivably design their 1162 | own "document language" based on elements with almost no associated 1163 | presentation (such as DIV and SPAN in HTML) and assigning style 1164 | information through the "class" attribute. Authors should avoid this 1165 | practice since the structural elements of a document language often 1166 | have recognized and accepted meanings and author-defined classes may 1167 | not.
1168 | 1169 |Note: If an element has multiple 1170 | class attributes, their values must be concatenated with spaces 1171 | between the values before searching for the class. As of this time the 1172 | working group is not aware of any manner in which this situation can 1173 | be reached, however, so this behavior is explicitly non-normative in 1174 | this specification.
1175 | 1176 |Document languages may contain attributes that are declared to be 1179 | of type ID. What makes attributes of type ID special is that no two 1180 | such attributes can have the same value in a document, regardless of 1181 | the type of the elements that carry them; whatever the document 1182 | language, an ID typed attribute can be used to uniquely identify its 1183 | element. In HTML all ID attributes are named "id"; XML applications 1184 | may name ID attributes differently, but the same restriction 1185 | applies.
1186 | 1187 |An ID-typed attribute of a document language allows authors to
1188 | assign an identifier to one element instance in the document tree. W3C
1189 | ID selectors represent an element instance based on its identifier. An
1190 | ID selector contains a "number sign" (U+0023,
1191 | #) immediately followed by the ID value, which must be an
1192 | identifier.
Selectors does not specify how a UA knows the ID-typed attribute of 1195 | an element. The UA may, e.g., read a document's DTD, have the 1196 | information hard-coded or ask the user. 1197 | 1198 |
Examples:
1200 |The following ID selector represents an h1 element
1201 | whose ID-typed attribute has the value "chapter1":
h1#chapter11203 |
The following ID selector represents any element whose ID-typed 1204 | attribute has the value "chapter1":
1205 |#chapter11206 |
The following selector represents any element whose ID-typed 1207 | attribute has the value "z98y".
1208 |*#z98y1209 |
Note. In XML 1.0 [XML10], the information about which attribute
1213 | contains an element's IDs is contained in a DTD or a schema. When
1214 | parsing XML, UAs do not always read the DTD, and thus may not know
1215 | what the ID of an element is (though a UA may have namespace-specific
1216 | knowledge that allows it to determine which attribute is the ID
1217 | attribute for that namespace). If a style sheet designer knows or
1218 | suspects that a UA may not know what the ID of an element is, he
1219 | should use normal attribute selectors instead:
1220 | [name=p371] instead of #p371. Elements in
1221 | XML 1.0 documents without a DTD do not have IDs at all.
If an element has multiple ID attributes, all of them must be 1224 | treated as IDs for that element for the purposes of the ID 1225 | selector. Such a situation could be reached using mixtures of xml:id, 1226 | DOM3 Core, XML DTDs, and namespace-specific knowledge.
1227 | 1228 |The pseudo-class concept is introduced to permit selection based on 1231 | information that lies outside of the document tree or that cannot be 1232 | expressed using the other simple selectors.
1233 | 1234 |A pseudo-class always consists of a "colon"
1235 | (:) followed by the name of the pseudo-class and
1236 | optionally by a value between parentheses.
Pseudo-classes are allowed in all sequences of simple selectors 1239 | contained in a selector. Pseudo-classes are allowed anywhere in 1240 | sequences of simple selectors, after the leading type selector or 1241 | universal selector (possibly omitted). Pseudo-class names are 1242 | case-insensitive. Some pseudo-classes are mutually exclusive, while 1243 | others can be applied simultaneously to the same 1244 | element. Pseudo-classes may be dynamic, in the sense that an element 1245 | may acquire or lose a pseudo-class while a user interacts with the 1246 | document.
1247 | 1248 | 1249 |Dynamic pseudo-classes classify elements on characteristics other 1252 | than their name, attributes, or content, in principle characteristics 1253 | that cannot be deduced from the document tree.
1254 | 1255 |Dynamic pseudo-classes do not appear in the document source or 1256 | document tree.
1257 | 1258 | 1259 |User agents commonly display unvisited links differently from
1262 | previously visited ones. Selectors
1263 | provides the pseudo-classes :link and
1264 | :visited to distinguish them:
:link pseudo-class applies to links that have
1268 | not yet been visited.:visited pseudo-class applies once the link has
1270 | been visited by the user. After some amount of time, user agents may choose to return a 1274 | visited link to the (unvisited) ':link' state.
1275 | 1276 |The two states are mutually exclusive.
1277 | 1278 |Example:
1281 | 1282 |The following selector represents links carrying class
1283 | external and already visited:
a.external:visited1286 | 1287 |
Note: It is possible for style sheet 1290 | authors to abuse the :link and :visited pseudo-classes to determine 1291 | which sites a user has visited without the user's consent. 1292 | 1293 |
UAs may therefore treat all links as unvisited links, or implement 1294 | other measures to preserve the user's privacy while rendering visited 1295 | and unvisited links differently.
1296 | 1297 |Interactive user agents sometimes change the rendering in response 1301 | to user actions. Selectors provides 1302 | three pseudo-classes for the selection of an element the user is 1303 | acting on.
1304 | 1305 |:hover pseudo-class applies while the user
1308 | designates an element with a pointing device, but does not activate
1309 | it. For example, a visual user agent could apply this pseudo-class
1310 | when the cursor (mouse pointer) hovers over a box generated by the
1311 | element. User agents not that do not support interactive
1313 | media do not have to support this pseudo-class. Some conforming
1314 | user agents that support interactive
1316 | media may not be able to support this pseudo-class (e.g., a pen
1317 | device that does not detect hovering).:active pseudo-class applies while an element
1320 | is being activated by the user. For example, between the times the
1321 | user presses the mouse button and releases it.:focus pseudo-class applies while an element
1324 | has the focus (accepts keyboard or mouse events, or other forms of
1325 | input). There may be document language or implementation specific limits on
1330 | which elements can become :active or acquire
1331 | :focus.
These pseudo-classes are not mutually exclusive. An element may 1334 | match several pseudo-classes at the same time.
1335 | 1336 |Selectors doesn't define if the parent of an element that is 1337 | ':active' or ':hover' is also in that state.
1338 | 1339 |Examples:
1341 |a:link /* unvisited links */ 1342 | a:visited /* visited links */ 1343 | a:hover /* user hovers */ 1344 | a:active /* active links */1345 |
An example of combining dynamic pseudo-classes:
1346 |a:focus 1347 | a:focus:hover1348 |
The last selector matches a elements that are in
1349 | the pseudo-class :focus and in the pseudo-class :hover.
Note: An element can be both ':visited' 1353 | and ':active' (or ':link' and ':active').
1354 | 1355 |Some URIs refer to a location within a resource. This kind of URI 1358 | ends with a "number sign" (#) followed by an anchor 1359 | identifier (called the fragment identifier).
1360 | 1361 |URIs with fragment identifiers link to a certain element within the
1362 | document, known as the target element. For instance, here is a URI
1363 | pointing to an anchor named section_2 in an HTML
1364 | document:
http://example.com/html/top.html#section_21367 | 1368 |
A target element can be represented by the :target
1369 | pseudo-class. If the document's URI has no fragment identifier, then
1370 | the document has no target element.
Example:
1374 |p.note:target1375 |
This selector represents a p element of class
1376 | note that is the target element of the referring
1377 | URI.
CSS example:
1382 |Here, the :target pseudo-class is used to make the
1383 | target element red and place an image before it, if there is one:
*:target { color : red }
1385 | *:target::before { content : url(target.png) }
1386 | If the document language specifies how the human language of an
1391 | element is determined, it is possible to write selectors that
1392 | represent an element based on its language. For example, in HTML [HTML4], the language is determined by a
1394 | combination of the lang attribute, the meta
1395 | element, and possibly by information from the protocol (such as HTTP
1396 | headers). XML uses an attribute called xml:lang, and
1397 | there may be other document language-specific methods for determining
1398 | the language.
The pseudo-class :lang(C) represents an element that
1401 | is in language C. Whether an element is represented by a
1402 | :lang() selector is based solely on the identifier C
1403 | being either equal to, or a hyphen-separated substring of, the
1404 | element's language value, in the same way as if performed by the '|=' operator in attribute
1406 | selectors. The identifier C does not have to be a valid language
1407 | name.
C must not be empty. (If it is, the selector is invalid.)
1410 | 1411 |Note: It is recommended that 1412 | documents and protocols indicate language using codes from RFC 3066 [RFC3066] or its successor, and by means of 1414 | "xml:lang" attributes in the case of XML-based documents [XML10]. See 1417 | "FAQ: Two-letter or three-letter language codes."
1418 | 1419 |Examples:
1421 |The two following selectors represent an HTML document that is in
1422 | Belgian, French, or German. The two next selectors represent
1423 | q quotations in an arbitrary element in Belgian, French,
1424 | or German.
html:lang(fr-be) 1426 | html:lang(de) 1427 | :lang(fr-be) > q 1428 | :lang(de) > q1429 |
The :enabled pseudo-class allows authors to customize
1436 | the look of user interface elements that are enabled — which the
1437 | user can select or activate in some fashion (e.g. clicking on a button
1438 | with a mouse). There is a need for such a pseudo-class because there
1439 | is no way to programmatically specify the default appearance of say,
1440 | an enabled input element without also specifying what it
1441 | would look like when it was disabled.
Similar to :enabled, :disabled allows the
1444 | author to specify precisely how a disabled or inactive user interface
1445 | element should look.
Most elements will be neither enabled nor disabled. An element is 1448 | enabled if the user can either activate it or transfer the focus to 1449 | it. An element is disabled if it could be enabled, but the user cannot 1450 | presently activate it or transfer focus to it.
1451 | 1452 | 1453 |Radio and checkbox elements can be toggled by the user. Some menu
1456 | items are "checked" when the user selects them. When such elements are
1457 | toggled "on" the :checked pseudo-class applies. The
1458 | :checked pseudo-class initially applies to such elements
1459 | that have the HTML4 selected and checked
1460 | attributes as described in Section
1462 | 17.2.1 of HTML4, but of course the user can toggle "off" such
1463 | elements in which case the :checked pseudo-class would no
1464 | longer apply. While the :checked pseudo-class is dynamic
1465 | in nature, and is altered by user action, since it can also be based
1466 | on the presence of the semantic HTML4 selected and
1467 | checked attributes, it applies to all media.
1468 |
1469 |
1470 |
Radio and checkbox elements can be toggled by the user, but are 1475 | sometimes in an indeterminate state, neither checked nor unchecked. 1476 | This can be due to an element attribute, or DOM manipulation.
1477 | 1478 |A future version of this specification may introduce an
1479 | :indeterminate pseudo-class that applies to such elements.
1480 |
Selectors introduces the concept of structural 1493 | pseudo-classes to permit selection based on extra information that lies in 1494 | the document tree but cannot be represented by other simple selectors or 1495 | combinators. 1496 | 1497 |
Note that standalone pieces of PCDATA (text nodes in the DOM) are 1498 | not counted when calculating the position of an element in the list of 1499 | children of its parent. When calculating the position of an element in 1500 | the list of children of its parent, the index numbering starts at 1. 1501 | 1502 | 1503 |
The :root pseudo-class represents an element that is
1506 | the root of the document. In HTML 4, this is always the
1507 | HTML element.
1508 |
1509 |
1510 |
The
1513 | :nth-child(a
1514 | pseudo-class notation represents an element that has
1515 | an+b)n+b-1 siblings
1516 | before it in the document tree, for a given positive
1517 | integer or zero value of n, and has a parent element. In
1518 | other words, this matches the bth child of an element after
1519 | all the children have been split into groups of a elements
1520 | each. For example, this allows the selectors to address every other
1521 | row in a table, and could be used to alternate the color
1522 | of paragraph text in a cycle of four. The a and
1523 | b values must be zero, negative integers or positive
1524 | integers. The index of the first child of an element is 1.
1525 |
1526 |
In addition to this, :nth-child() can take
1527 | 'odd' and 'even' as arguments instead.
1528 | 'odd' has the same signification as 2n+1,
1529 | and 'even' has the same signification as 2n.
1530 |
1531 |
1532 |
Examples:
1534 |tr:nth-child(2n+1) /* represents every odd row of an HTML table */
1535 | tr:nth-child(odd) /* same */
1536 | tr:nth-child(2n) /* represents every even row of an HTML table */
1537 | tr:nth-child(even) /* same */
1538 |
1539 | /* Alternate paragraph colours in CSS */
1540 | p:nth-child(4n+1) { color: navy; }
1541 | p:nth-child(4n+2) { color: green; }
1542 | p:nth-child(4n+3) { color: maroon; }
1543 | p:nth-child(4n+4) { color: purple; }
1544 | When a=0, no repeating is used, so for example
1547 | :nth-child(0n+5) matches only the fifth child. When
1548 | a=0, the an part need not be
1549 | included, so the syntax simplifies to
1550 | :nth-child(b) and the last example simplifies
1551 | to :nth-child(5).
1552 |
1553 |
Examples:
1555 |foo:nth-child(0n+1) /* represents an element foo, first child of its parent element */ 1556 | foo:nth-child(1) /* same */1557 |
When a=1, the number may be omitted from the rule. 1560 | 1561 |
Examples:
1563 |The following selectors are therefore equivalent:
1564 |bar:nth-child(1n+0) /* represents all bar elements, specificity (0,1,1) */ 1565 | bar:nth-child(n+0) /* same */ 1566 | bar:nth-child(n) /* same */ 1567 | bar /* same but lower specificity (0,0,1) */1568 |
If b=0, then every ath element is picked. In 1571 | such a case, the b part may be omitted. 1572 | 1573 |
Examples:
1575 |tr:nth-child(2n+0) /* represents every even row of an HTML table */ 1576 | tr:nth-child(2n) /* same */1577 |
If both a and b are equal to zero, the 1580 | pseudo-class represents no element in the document tree.
1581 | 1582 |The value a can be negative, but only the positive
1583 | values of an+b, for
1584 | n≥0, may represent an element in the document
1585 | tree.
Example:
1589 |html|tr:nth-child(-n+6) /* represents the 6 first rows of XHTML tables */1590 |
When the value b is negative, the "+" character in the 1593 | expression must be removed (it is effectively replaced by the "-" 1594 | character indicating the negative value of b).
1595 | 1596 |Examples:
1598 |:nth-child(10n-1) /* represents the 9th, 19th, 29th, etc, element */ 1599 | :nth-child(10n+9) /* Same */ 1600 | :nth-child(10n+-1) /* Syntactically invalid, and would be ignored */1601 |
The :nth-last-child(an+b)
1607 | pseudo-class notation represents an element that has
1608 | an+b-1 siblings
1609 | after it in the document tree, for a given positive
1610 | integer or zero value of n, and has a parent element. See
1611 | :nth-child() pseudo-class for the syntax of its argument.
1612 | It also accepts the 'even' and 'odd' values
1613 | as arguments.
1614 |
1615 |
1616 |
Examples:
1618 |tr:nth-last-child(-n+2) /* represents the two last rows of an HTML table */ 1619 | 1620 | foo:nth-last-child(odd) /* represents all odd foo elements in their parent element, 1621 | counting from the last one */1622 |
The :nth-of-type(an+b)
1628 | pseudo-class notation represents an element that has
1629 | an+b-1 siblings with the same
1630 | element name before it in the document tree, for a
1631 | given zero or positive integer value of n, and has a
1632 | parent element. In other words, this matches the bth child
1633 | of that type after all the children of that type have been split into
1634 | groups of a elements each. See :nth-child() pseudo-class
1635 | for the syntax of its argument. It also accepts the
1636 | 'even' and 'odd' values.
1637 |
1638 |
1639 |
CSS example:
1641 |This allows an author to alternate the position of floated images:
1642 |img:nth-of-type(2n+1) { float: right; }
1643 | img:nth-of-type(2n) { float: left; }
1644 | The :nth-last-of-type(an+b)
1650 | pseudo-class notation represents an element that has
1651 | an+b-1 siblings with the same
1652 | element name after it in the document tree, for a
1653 | given zero or positive integer value of n, and has a
1654 | parent element. See :nth-child() pseudo-class for the
1655 | syntax of its argument. It also accepts the 'even' and 'odd' values.
1656 |
1657 |
1658 |
Example:
1660 |To represent all h2 children of an XHTML
1661 | body except the first and last, one could use the
1662 | following selector:
body > h2:nth-of-type(n+2):nth-last-of-type(n+2)1664 |
In this case, one could also use :not(), although the
1665 | selector ends up being just as long:
body > h2:not(:first-of-type):not(:last-of-type)1667 |
Same as :nth-child(1). The :first-child pseudo-class
1673 | represents an element that is the first child of some other element.
1674 |
1675 |
1676 |
Examples:
1678 |The following selector represents a p element that is
1679 | the first child of a div element:
div > p:first-child1681 |
This selector can represent the p inside the
1682 | div of the following fragment:
<p> The last P before the note.</p> 1684 | <div class="note"> 1685 | <p> The first P inside the note.</p> 1686 | </div>but cannot represent the second
p in the following
1687 | fragment:
1688 | <p> The last P before the note.</p> 1689 | <div class="note"> 1690 | <h2> Note </h2> 1691 | <p> The first P inside the note.</p> 1692 | </div>1693 |
The following two selectors are usually equivalent:
1694 |* > a:first-child /* a is first child of any element */ 1695 | a:first-child /* Same (assuming a is not the root element) */1696 |
Same as :nth-last-child(1). The :last-child pseudo-class
1701 | represents an element that is the last child of some other element.
1702 |
1703 |
Example:
1705 |The following selector represents a list item li that
1706 | is the last child of an ordered list ol.
1707 |
ol > li:last-child1708 |
Same as :nth-of-type(1). The :first-of-type pseudo-class
1713 | represents an element that is the first sibling of its type in the list of
1714 | children of its parent element.
1715 |
1716 |
Example:
1718 |The following selector represents a definition title
1719 | dt inside a definition list dl, this
1720 | dt being the first of its type in the list of children of
1721 | its parent element.
dl dt:first-of-type1723 |
It is a valid description for the first two dt
1724 | elements in the following example but not for the third one:
<dl> 1726 | <dt>gigogne</dt> 1727 | <dd> 1728 | <dl> 1729 | <dt>fusée</dt> 1730 | <dd>multistage rocket</dd> 1731 | <dt>table</dt> 1732 | <dd>nest of tables</dd> 1733 | </dl> 1734 | </dd> 1735 | </dl>1736 |
Same as :nth-last-of-type(1). The
1741 | :last-of-type pseudo-class represents an element that is
1742 | the last sibling of its type in the list of children of its parent
1743 | element.
Example:
1747 |The following selector represents the last data cell
1748 | td of a table row.
tr > td:last-of-type1750 |
Represents an element that has a parent element and whose parent
1755 | element has no other element children. Same as
1756 | :first-child:last-child or
1757 | :nth-child(1):nth-last-child(1), but with a lower
1758 | specificity.
Represents an element that has a parent element and whose parent
1763 | element has no other element children with the same element name. Same
1764 | as :first-of-type:last-of-type or
1765 | :nth-of-type(1):nth-last-of-type(1), but with a lower
1766 | specificity.
The :empty pseudo-class represents an element that has
1772 | no children at all. In terms of the DOM, only element nodes and text
1773 | nodes (including CDATA nodes and entity references) whose data has a
1774 | non-zero length must be considered as affecting emptiness; comments,
1775 | PIs, and other nodes must not affect whether an element is considered
1776 | empty or not.
Examples:
1780 |p:empty is a valid representation of the following fragment:
<p></p>1782 |
foo:empty is not a valid representation for the
1783 | following fragments:
<foo>bar</foo>1785 |
<foo><bar>bla</bar></foo>1786 |
<foo>this is not <bar>:empty</bar></foo>1787 |
This section intentionally left blank.
1792 | 1793 | 1794 |The negation pseudo-class, :not(X), is a
1797 | functional notation taking a simple
1798 | selector (excluding the negation pseudo-class itself and
1799 | pseudo-elements) as an argument. It represents an element that is not
1800 | represented by the argument.
1801 |
1802 |
1804 |
1805 |
Examples:
1807 |The following CSS selector matches all button
1808 | elements in an HTML document that are not disabled.
button:not([DISABLED])1810 |
The following selector represents all but FOO
1811 | elements.
*:not(FOO)1813 |
The following group of selectors represents all HTML elements 1814 | except links.
1815 |html|*:not(:link):not(:visited)1816 |
Default namespace declarations do not affect the argument of the 1819 | negation pseudo-class unless the argument is a universal selector or a 1820 | type selector.
1821 | 1822 |Examples:
1824 |Assuming that the default namespace is bound to 1825 | "http://example.com/", the following selector represents all 1826 | elements that are not in that namespace:
1827 |*|*:not(*)1828 |
The following CSS selector matches any element being hovered, 1829 | regardless of its namespace. In particular, it is not limited to 1830 | only matching elements in the default namespace that are not being 1831 | hovered, and elements not in the default namespace don't match the 1832 | rule when they are being hovered.
1833 |*|*:not(:hover)1834 |
Note: the :not() pseudo allows
1837 | useless selectors to be written. For instance :not(*|*),
1838 | which represents no element at all, or foo:not(bar),
1839 | which is equivalent to foo but with a higher
1840 | specificity.
Pseudo-elements create abstractions about the document tree beyond
1845 | those specified by the document language. For instance, document
1846 | languages do not offer mechanisms to access the first letter or first
1847 | line of an element's content. Pseudo-elements allow designers to refer
1848 | to this otherwise inaccessible information. Pseudo-elements may also
1849 | provide designers a way to refer to content that does not exist in the
1850 | source document (e.g., the ::before and
1851 | ::after pseudo-elements give access to generated
1852 | content).
A pseudo-element is made of two colons (::) followed
1855 | by the name of the pseudo-element.
This :: notation is introduced by the current document
1858 | in order to establish a discrimination between pseudo-classes and
1859 | pseudo-elements. For compatibility with existing style sheets, user
1860 | agents must also accept the previous one-colon notation for
1861 | pseudo-elements introduced in CSS levels 1 and 2 (namely,
1862 | :first-line, :first-letter,
1863 | :before and :after). This compatibility is
1864 | not allowed for the new pseudo-elements introduced in CSS level 3.
Only one pseudo-element may appear per selector, and if present it 1867 | must appear after the sequence of simple selectors that represents the 1868 | subjects of the selector. A 1869 | future version of this specification may allow multiple 1870 | pesudo-elements per selector.
1871 | 1872 |The ::first-line pseudo-element describes the contents
1875 | of the first formatted line of an element.
1876 |
1877 |
CSS example:
1879 |p::first-line { text-transform: uppercase }
1880 | The above rule means "change the letters of the first line of every 1881 | paragraph to uppercase".
1882 |The selector p::first-line does not match any real
1885 | HTML element. It does match a pseudo-element that conforming user
1886 | agents will insert at the beginning of every paragraph.
Note that the length of the first line depends on a number of 1889 | factors, including the width of the page, the font size, etc. Thus, 1890 | an ordinary HTML paragraph such as:
1891 | 1892 |1893 | <P>This is a somewhat long HTML 1894 | paragraph that will be broken into several 1895 | lines. The first line will be identified 1896 | by a fictional tag sequence. The other lines 1897 | will be treated as ordinary lines in the 1898 | paragraph.</P> 1899 |1900 | 1901 |
the lines of which happen to be broken as follows: 1902 | 1903 |
1904 | THIS IS A SOMEWHAT LONG HTML PARAGRAPH THAT 1905 | will be broken into several lines. The first 1906 | line will be identified by a fictional tag 1907 | sequence. The other lines will be treated as 1908 | ordinary lines in the paragraph. 1909 |1910 | 1911 |
This paragraph might be "rewritten" by user agents to include the
1912 | fictional tag sequence for ::first-line. This
1913 | fictional tag sequence helps to show how properties are inherited.
1916 | <P><P::first-line> This is a somewhat long HTML 1917 | paragraph that </P::first-line> will be broken into several 1918 | lines. The first line will be identified 1919 | by a fictional tag sequence. The other lines 1920 | will be treated as ordinary lines in the 1921 | paragraph.</P> 1922 |1923 | 1924 |
If a pseudo-element breaks up a real element, the desired effect
1925 | can often be described by a fictional tag sequence that closes and
1926 | then re-opens the element. Thus, if we mark up the previous paragraph
1927 | with a span element:
1930 | <P><SPAN class="test"> This is a somewhat long HTML 1931 | paragraph that will be broken into several 1932 | lines.</SPAN> The first line will be identified 1933 | by a fictional tag sequence. The other lines 1934 | will be treated as ordinary lines in the 1935 | paragraph.</P> 1936 |1937 | 1938 |
the user agent could simulate start and end tags for
1939 | span when inserting the fictional tag sequence for
1940 | ::first-line.
1941 |
1942 |
1943 | <P><P::first-line><SPAN class="test"> This is a 1944 | somewhat long HTML 1945 | paragraph that will </SPAN></P::first-line><SPAN class="test"> be 1946 | broken into several 1947 | lines.</SPAN> The first line will be identified 1948 | by a fictional tag sequence. The other lines 1949 | will be treated as ordinary lines in the 1950 | paragraph.</P> 1951 |1952 | 1953 |
In CSS, the ::first-line pseudo-element can only be
1954 | attached to a block-level element, an inline-block, a table-caption,
1955 | or a table-cell.
The "first formatted line" of an
1958 | element may occur inside a
1959 | block-level descendant in the same flow (i.e., a block-level
1960 | descendant that is not positioned and not a float). E.g., the first
1961 | line of the div in <DIV><P>This
1962 | line...</P></DIV> is the first line of the p (assuming
1963 | that both p and div are block-level).
1964 |
1965 |
The first line of a table-cell or inline-block cannot be the first
1966 | formatted line of an ancestor element. Thus, in <DIV><P
1967 | STYLE="display: inline-block">Hello<BR>Goodbye</P>
1968 | etcetera</DIV> the first formatted line of the
1969 | div is not the line "Hello".
1970 |
1971 |
Note that the first line of the p in this
1972 | fragment: <p><br>First... doesn't contain any
1973 | letters (assuming the default style for br in HTML
1974 | 4). The word "First" is not on the first formatted line.
1975 |
1976 |
A UA should act as if the fictional start tags of the
1977 | ::first-line pseudo-elements were nested just inside the
1978 | innermost enclosing block-level element. (Since CSS1 and CSS2 were
1979 | silent on this case, authors should not rely on this behavior.) Here
1980 | is an example. The fictional tag sequence for
1983 | <DIV> 1984 | <P>First paragraph</P> 1985 | <P>Second paragraph</P> 1986 | </DIV> 1987 |1988 | 1989 |
is
1990 | 1991 |1992 | <DIV> 1993 | <P><DIV::first-line><P::first-line>First paragraph</P::first-line></DIV::first-line></P> 1994 | <P><P::first-line>Second paragraph</P::first-line></P> 1995 | </DIV> 1996 |1997 | 1998 |
The ::first-line pseudo-element is similar to an
1999 | inline-level element, but with certain restrictions. In CSS, the
2000 | following properties apply to a ::first-line
2001 | pseudo-element: font properties, color property, background
2002 | properties, 'word-spacing', 'letter-spacing', 'text-decoration',
2003 | 'vertical-align', 'text-transform', 'line-height'. UAs may apply other
2004 | properties as well.
The ::first-letter pseudo-element represents the first
2010 | letter of the first line of a block, if it is not preceded by any
2011 | other content (such as images or inline tables) on its line. The
2012 | ::first-letter pseudo-element may be used for "initial caps" and "drop
2013 | caps", which are common typographical effects. This type of initial
2014 | letter is similar to an inline-level element if its 'float' property
2015 | is 'none'; otherwise, it is similar to a floated element.
In CSS, these are the properties that apply to ::first-letter
2018 | pseudo-elements: font properties, 'text-decoration', 'text-transform',
2019 | 'letter-spacing', 'word-spacing' (when appropriate), 'line-height',
2020 | 'float', 'vertical-align' (only if 'float' is 'none'), margin
2021 | properties, padding properties, border properties, color property,
2022 | background properties. UAs may apply other properties as well. To
2023 | allow UAs to render a typographically correct drop cap or initial cap,
2024 | the UA may choose a line-height, width and height based on the shape
2025 | of the letter, unlike for normal elements.
Example:
2029 |This example shows a possible rendering of an initial cap. Note
2030 | that the 'line-height' that is inherited by the ::first-letter
2031 | pseudo-element is 1.1, but the UA in this example has computed the
2032 | height of the first letter differently, so that it doesn't cause any
2033 | unnecessary space between the first two lines. Also note that the
2034 | fictional start tag of the first letter is inside the span, and thus
2035 | the font weight of the first letter is normal, not bold as the span:
2036 |
2037 | p { line-height: 1.1 }
2038 | p::first-letter { font-size: 3em; font-weight: normal }
2039 | span { font-weight: bold }
2040 | ...
2041 | <p><span>Het hemelsche</span> gerecht heeft zich ten lange lesten<br>
2042 | Erbarremt over my en mijn benaeuwde vesten<br>
2043 | En arme burgery, en op mijn volcx gebed<br>
2044 | En dagelix geschrey de bange stad ontzet.
2045 |
2046 |
2048 |
The following CSS will make a drop cap initial letter span about two lines:
2053 | 2054 |
2055 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
2056 | <HTML>
2057 | <HEAD>
2058 | <TITLE>Drop cap initial letter</TITLE>
2059 | <STYLE type="text/css">
2060 | P { font-size: 12pt; line-height: 1.2 }
2061 | P::first-letter { font-size: 200%; font-weight: bold; float: left }
2062 | SPAN { text-transform: uppercase }
2063 | </STYLE>
2064 | </HEAD>
2065 | <BODY>
2066 | <P><SPAN>The first</SPAN> few words of an article
2067 | in The Economist.</P>
2068 | </BODY>
2069 | </HTML>
2070 |
2071 |
2072 | This example might be formatted as follows:
2073 | 2074 |
The fictional tag sequence is:
2080 | 2081 |2082 | <P> 2083 | <SPAN> 2084 | <P::first-letter> 2085 | T 2086 | </P::first-letter>he first 2087 | </SPAN> 2088 | few words of an article in the Economist. 2089 | </P> 2090 |2091 | 2092 |
Note that the ::first-letter pseudo-element tags abut
2093 | the content (i.e., the initial character), while the ::first-line
2094 | pseudo-element start tag is inserted right after the start tag of the
2095 | block element.
In order to achieve traditional drop caps formatting, user agents 2098 | may approximate font sizes, for example to align baselines. Also, the 2099 | glyph outline may be taken into account when formatting.
2100 | 2101 |Punctuation (i.e, characters defined in Unicode in the "open" (Ps), 2102 | "close" (Pe), "initial" (Pi). "final" (Pf) and "other" (Po) 2103 | punctuation classes), that precedes or follows the first letter should 2104 | be included. [UNICODE]
2105 | 2106 |
The ::first-letter also applies if the first letter is
2112 | in fact a digit, e.g., the "6" in "67 million dollars is a lot of
2113 | money."
In CSS, the ::first-letter pseudo-element applies to
2116 | block, list-item, table-cell, table-caption, and inline-block
2117 | elements. A future version of this specification
2118 | may allow this pesudo-element to apply to more element
2119 | types.
The ::first-letter pseudo-element can be used with all
2122 | such elements that contain text, or that have a descendant in the same
2123 | flow that contains text. A UA should act as if the fictional start tag
2124 | of the ::first-letter pseudo-element is just before the first text of
2125 | the element, even if that first text is in a descendant.
Example:
2129 |The fictional tag sequence for this HTMLfragment: 2130 |
<div> 2131 | <p>The first text.2132 |
is: 2133 |
<div> 2134 | <p><div::first-letter><p::first-letter>T</...></...>he first text.2135 |
The first letter of a table-cell or inline-block cannot be the
2138 | first letter of an ancestor element. Thus, in <DIV><P
2139 | STYLE="display: inline-block">Hello<BR>Goodbye</P>
2140 | etcetera</DIV> the first letter of the div is not the
2141 | letter "H". In fact, the div doesn't have a first letter.
2142 |
2143 |
The first letter must occur on the first formatted line. For example, in
2145 | this fragment: <p><br>First... the first line
2146 | doesn't contain any letters and ::first-letter doesn't
2147 | match anything (assuming the default style for br in HTML
2148 | 4). In particular, it does not match the "F" of "First."
2149 |
2150 |
In CSS, if an element is a list item ('display: list-item'), the
2151 | ::first-letter applies to the first letter in the
2152 | principal box after the marker. UAs may ignore
2153 | ::first-letter on list items with 'list-style-position:
2154 | inside'. If an element has ::before or
2155 | ::after content, the ::first-letter applies
2156 | to the first letter of the element including that content.
2157 |
2158 |
Example:
2160 |After the rule 'p::before {content: "Note: "}', the selector 2161 | 'p::first-letter' matches the "N" of "Note".
2162 |Some languages may have specific rules about how to treat certain
2165 | letter combinations. In Dutch, for example, if the letter combination
2166 | "ij" appears at the beginning of a word, both letters should be
2167 | considered within the ::first-letter pseudo-element.
2168 |
2169 |
If the letters that would form the ::first-letter are not in the
2170 | same element, such as "'T" in <p>'<em>T..., the UA
2171 | may create a ::first-letter pseudo-element from one of the elements,
2172 | both elements, or simply not create a pseudo-element.
Similarly, if the first letter(s) of the block are not at the start 2175 | of the line (for example due to bidirectional reordering), then the UA 2176 | need not create the pseudo-element(s). 2177 | 2178 |
Example:
2180 |The following example illustrates 2181 | how overlapping pseudo-elements may interact. The first letter of 2182 | each P element will be green with a font size of '24pt'. The rest of 2183 | the first formatted line will be 'blue' while the rest of the 2184 | paragraph will be 'red'.
2185 | 2186 |p { color: red; font-size: 12pt }
2187 | p::first-letter { color: green; font-size: 200% }
2188 | p::first-line { color: blue }
2189 |
2190 | <P>Some text that ends up on two lines</P>
2191 |
2192 | Assuming that a line break will occur before the word "ends", the 2193 | fictional tag 2194 | sequence for this fragment might be:
2195 | 2196 |<P> 2197 | <P::first-line> 2198 | <P::first-letter> 2199 | S 2200 | </P::first-letter>ome text that 2201 | </P::first-line> 2202 | ends up on two lines 2203 | </P>2204 | 2205 |
Note that the ::first-letter element is inside the ::first-line
2206 | element. Properties set on ::first-line are inherited by
2207 | ::first-letter, but are overridden if the same property is set on
2208 | ::first-letter.
The ::selection pseudo-element applies to the portion
2215 | of a document that has been highlighted by the user. This also
2216 | applies, for example, to selected text within an editable text
2217 | field. This pseudo-element should not be confused with the :checked pseudo-class (which used to be
2219 | named :selected)
2220 |
2221 |
Although the ::selection pseudo-element is dynamic in
2222 | nature, and is altered by user action, it is reasonable to expect that
2223 | when a UA re-renders to a static medium (such as a printed page, see
2224 | [CSS21]) which was originally rendered to a
2225 | dynamic medium (like screen), the UA may wish to transfer the current
2226 | ::selection state to that other medium, and have all the
2227 | appropriate formatting and rendering take effect as well. This is not
2228 | required — UAs may omit the ::selection
2229 | pseudo-element for static media.
2230 |
2231 |
These are the CSS properties that apply to ::selection
2232 | pseudo-elements: color, background, cursor (optional), outline
2233 | (optional). The computed value of the 'background-image' property on
2234 | ::selection may be ignored.
2235 |
2236 |
2237 |
The ::before and ::after pseudo-elements
2240 | can be used to describe generated content before or after an element's
2241 | content. They are explained in CSS 2.1 [CSS21].
When the ::first-letter and ::first-line
2245 | pseudo-elements are combined with ::before and
2246 | ::after, they apply to the first letter or line of the
2247 | element including the inserted text.
At times, authors may want selectors to describe an element that is
2254 | the descendant of another element in the document tree (e.g., "an
2255 | EM element that is contained within an H1
2256 | element"). Descendant combinators express such a relationship. A
2257 | descendant combinator is white space that
2258 | separates two sequences of simple selectors. A selector of the form
2259 | "A B" represents an element B that is an
2260 | arbitrary descendant of some ancestor element A.
2261 |
2262 |
Examples:
2264 |For example, consider the following selector:
2265 |h1 em2266 |
It represents an em element being the descendant of
2267 | an h1 element. It is a correct and valid, but partial,
2268 | description of the following fragment:
<h1>This <span class="myclass">headline 2270 | is <em>very</em> important</span></h1>2271 |
The following selector:
2272 |div * p2273 |
represents a p element that is a grandchild or later
2274 | descendant of a div element. Note the whitespace on
2275 | either side of the "*" is not part of the universal selector; the
2276 | whitespace is a combinator indicating that the DIV must be the
2277 | ancestor of some element, and that that element must be an ancestor
2278 | of the P.
The following selector, which combines descendant combinators and
2280 | attribute selectors, represents an
2281 | element that (1) has the href attribute set and (2) is
2282 | inside a p that is itself inside a div:
div p *[href]2284 |
A child combinator describes a childhood relationship
2289 | between two elements. A child combinator is made of the
2290 | "greater-than sign" (>) character and
2291 | separates two sequences of simple selectors.
2292 |
2293 |
2294 |
Examples:
2296 |The following selector represents a p element that is
2297 | child of body:
body > p2299 |
The following example combines descendant combinators and child 2300 | combinators.
2301 |div ol>li p2302 |
It represents a p element that is a descendant of an
2303 | li element; the li element must be the
2304 | child of an ol element; the ol element must
2305 | be a descendant of a div. Notice that the optional white
2306 | space around the ">" combinator has been left out.
For information on selecting the first child of an element, please
2310 | see the section on the :first-child pseudo-class
2312 | above.
There are two different sibling combinators: the adjacent sibling 2317 | combinator and the general sibling combinator. In both cases, 2318 | non-element nodes (e.g. text between elements) are ignored when 2319 | considering adjacency of elements.
2320 | 2321 |The adjacent sibling combinator is made of the "plus
2324 | sign" (U+002B, +) character that separates two
2325 | sequences of simple selectors. The elements represented by the two
2326 | sequences share the same parent in the document tree and the element
2327 | represented by the first sequence immediately precedes the element
2328 | represented by the second one.
Examples:
2332 |The following selector represents a p element
2333 | immediately following a math element:
math + p2335 |
The following selector is conceptually similar to the one in the
2336 | previous example, except that it adds an attribute selector — it
2337 | adds a constraint to the h1 element, that it must have
2338 | class="opener":
h1.opener + h22340 |
The general sibling combinator is made of the "tilde"
2346 | (U+007E, ~) character that separates two sequences of
2347 | simple selectors. The elements represented by the two sequences share
2348 | the same parent in the document tree and the element represented by
2349 | the first sequence precedes (not necessarily immediately) the element
2350 | represented by the second one.
Example:
2354 |h1 ~ pre2355 |
represents a pre element following an h1. It
2356 | is a correct and valid, but partial, description of:
<h1>Definition of the function a</h1> 2358 | <p>Function a(x) has to be applied to all figures in the table.</p> 2359 | <pre>function a(x) = 12x/13.5</pre>2360 |
A selector's specificity is calculated as follows:
2365 | 2366 |Selectors inside the negation pseudo-class 2374 | are counted like any other, but the negation itself does not count as 2375 | a pseudo-class.
2376 | 2377 |Concatenating the three numbers a-b-c (in a number system with a 2378 | large base) gives the specificity.
2379 | 2380 |Examples:
2382 |* /* a=0 b=0 c=0 -> specificity = 0 */ 2383 | LI /* a=0 b=0 c=1 -> specificity = 1 */ 2384 | UL LI /* a=0 b=0 c=2 -> specificity = 2 */ 2385 | UL OL+LI /* a=0 b=0 c=3 -> specificity = 3 */ 2386 | H1 + *[REL=up] /* a=0 b=1 c=1 -> specificity = 11 */ 2387 | UL OL LI.red /* a=0 b=1 c=3 -> specificity = 13 */ 2388 | LI.red.level /* a=0 b=2 c=1 -> specificity = 21 */ 2389 | #x34y /* a=1 b=0 c=0 -> specificity = 100 */ 2390 | #s12:not(FOO) /* a=1 b=0 c=1 -> specificity = 101 */ 2391 |2392 |
Note: the specificity of the styles
2395 | specified in an HTML style attribute is described in CSS
2396 | 2.1. [CSS21].
The grammar below defines the syntax of Selectors. It is globally 2403 | LL(1) and can be locally LL(2) (but note that most UA's should not use 2404 | it directly, since it doesn't express the parsing conventions). The 2405 | format of the productions is optimized for human consumption and some 2406 | shorthand notations beyond Yacc (see [YACC]) 2407 | are used:
2408 | 2409 |The productions are:
2418 | 2419 |selectors_group 2420 | : selector [ COMMA S* selector ]* 2421 | ; 2422 | 2423 | selector 2424 | : simple_selector_sequence [ combinator simple_selector_sequence ]* 2425 | ; 2426 | 2427 | combinator 2428 | /* combinators can be surrounded by white space */ 2429 | : PLUS S* | GREATER S* | TILDE S* | S+ 2430 | ; 2431 | 2432 | simple_selector_sequence 2433 | : [ type_selector | universal ] 2434 | [ HASH | class | attrib | pseudo | negation ]* 2435 | | [ HASH | class | attrib | pseudo | negation ]+ 2436 | ; 2437 | 2438 | type_selector 2439 | : [ namespace_prefix ]? element_name 2440 | ; 2441 | 2442 | namespace_prefix 2443 | : [ IDENT | '*' ]? '|' 2444 | ; 2445 | 2446 | element_name 2447 | : IDENT 2448 | ; 2449 | 2450 | universal 2451 | : [ namespace_prefix ]? '*' 2452 | ; 2453 | 2454 | class 2455 | : '.' IDENT 2456 | ; 2457 | 2458 | attrib 2459 | : '[' S* [ namespace_prefix ]? IDENT S* 2460 | [ [ PREFIXMATCH | 2461 | SUFFIXMATCH | 2462 | SUBSTRINGMATCH | 2463 | '=' | 2464 | INCLUDES | 2465 | DASHMATCH ] S* [ IDENT | STRING ] S* 2466 | ]? ']' 2467 | ; 2468 | 2469 | pseudo 2470 | /* '::' starts a pseudo-element, ':' a pseudo-class */ 2471 | /* Exceptions: :first-line, :first-letter, :before and :after. */ 2472 | /* Note that pseudo-elements are restricted to one per selector and */ 2473 | /* occur only in the last simple_selector_sequence. */ 2474 | : ':' ':'? [ IDENT | functional_pseudo ] 2475 | ; 2476 | 2477 | functional_pseudo 2478 | : FUNCTION S* expression ')' 2479 | ; 2480 | 2481 | expression 2482 | /* In CSS3, the expressions are identifiers, strings, */ 2483 | /* or of the form "an+b" */ 2484 | : [ [ PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT ] S* ]+ 2485 | ; 2486 | 2487 | negation 2488 | : NOT S* negation_arg S* ')' 2489 | ; 2490 | 2491 | negation_arg 2492 | : type_selector | universal | HASH | class | attrib | pseudo 2493 | ;2494 | 2495 | 2496 |
The following is the tokenizer, written in Flex (see 2499 | [FLEX]) notation. The tokenizer is 2500 | case-insensitive.
2501 | 2502 |The two occurrences of "\377" represent the highest character 2503 | number that current versions of Flex can deal with (decimal 255). They 2504 | should be read as "\4177777" (decimal 1114111), which is the highest 2505 | possible code point in Unicode/ISO-10646. [UNICODE]
2507 | 2508 |%option case-insensitive
2509 |
2510 | ident [-]?{nmstart}{nmchar}*
2511 | name {nmchar}+
2512 | nmstart [_a-z]|{nonascii}|{escape}
2513 | nonascii [^\0-\177]
2514 | unicode \\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
2515 | escape {unicode}|\\[^\n\r\f0-9a-f]
2516 | nmchar [_a-z0-9-]|{nonascii}|{escape}
2517 | num [0-9]+|[0-9]*\.[0-9]+
2518 | string {string1}|{string2}
2519 | string1 \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*\"
2520 | string2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*\'
2521 | invalid {invalid1}|{invalid2}
2522 | invalid1 \"([^\n\r\f\\"]|\\{nl}|{nonascii}|{escape})*
2523 | invalid2 \'([^\n\r\f\\']|\\{nl}|{nonascii}|{escape})*
2524 | nl \n|\r\n|\r|\f
2525 | w [ \t\r\n\f]*
2526 |
2527 | %%
2528 |
2529 | [ \t\r\n\f]+ return S;
2530 |
2531 | "~=" return INCLUDES;
2532 | "|=" return DASHMATCH;
2533 | "^=" return PREFIXMATCH;
2534 | "$=" return SUFFIXMATCH;
2535 | "*=" return SUBSTRINGMATCH;
2536 | {ident} return IDENT;
2537 | {string} return STRING;
2538 | {ident}"(" return FUNCTION;
2539 | {num} return NUMBER;
2540 | "#"{name} return HASH;
2541 | {w}"+" return PLUS;
2542 | {w}">" return GREATER;
2543 | {w}"," return COMMA;
2544 | {w}"~" return TILDE;
2545 | ":not(" return NOT;
2546 | @{ident} return ATKEYWORD;
2547 | {invalid} return INVALID;
2548 | {num}% return PERCENTAGE;
2549 | {num}{ident} return DIMENSION;
2550 | "<!--" return CDO;
2551 | "-->" return CDC;
2552 |
2553 | "url("{w}{string}{w}")" return URI;
2554 | "url("{w}([!#$%&*-~]|{nonascii}|{escape})*{w}")" return URI;
2555 | U\+[0-9a-f?]{1,6}(-[0-9a-f]{1,6})? return UNICODE_RANGE;
2556 |
2557 | \/\*[^*]*\*+([^/*][^*]*\*+)*\/ /* ignore comments */
2558 |
2559 | . return *yytext;
2560 |
2561 |
2562 |
2563 | An important issue is the interaction of CSS selectors with XML 2566 | documents in web clients that were produced prior to this 2567 | document. Unfortunately, due to the fact that namespaces must be 2568 | matched based on the URI which identifies the namespace, not the 2569 | namespace prefix, some mechanism is required to identify namespaces in 2570 | CSS by their URI as well. Without such a mechanism, it is impossible 2571 | to construct a CSS style sheet which will properly match selectors in 2572 | all cases against a random set of XML documents. However, given 2573 | complete knowledge of the XML document to which a style sheet is to be 2574 | applied, and a limited use of namespaces within the XML document, it 2575 | is possible to construct a style sheet in which selectors would match 2576 | elements and attributes correctly.
2577 | 2578 |It should be noted that a down-level CSS client will (if it
2579 | properly conforms to CSS forward compatible parsing rules) ignore all
2580 | @namespace at-rules, as well as all style rules that make
2581 | use of namespace qualified element type or attribute selectors. The
2582 | syntax of delimiting namespace prefixes in CSS was deliberately chosen
2583 | so that down-level CSS clients would ignore the style rules rather
2584 | than possibly match them incorrectly.
The use of default namespaces in CSS makes it possible to write 2587 | element type selectors that will function in both namespace aware CSS 2588 | clients as well as down-level clients. It should be noted that 2589 | down-level clients may incorrectly match selectors against XML 2590 | elements in other namespaces.
2591 | 2592 |The following are scenarios and examples in which it is possible to 2593 | construct style sheets which would function properly in web clients 2594 | that do not implement this proposal.
2595 | 2596 |The XML document does not use namespaces.
2600 | 2601 ||name")
2613 | will guarantee that selectors will match only XML elements that do
2614 | not have a declared namespace. The XML document defines a single, default namespace used 2623 | throughout the document. No namespace prefixes are used in element 2624 | names.
2625 | 2626 |The XML document does not use a default namespace, all 2640 | namespace prefixes used are known to the style sheet author, and 2641 | there is a direct mapping between namespace prefixes and namespace 2642 | URIs. (A given prefix may only be mapped to one namespace URI 2643 | throughout the XML document; there may be multiple prefixes mapped 2644 | to the same URI).
2645 | 2646 |\:"
2653 | to describe the fully qualified names, e.g.
2654 | "html\:h1" will match
2655 | <html:h1>. Selectors using the qualified name
2656 | will only match XML elements that use the same prefix. Other
2657 | namespace prefixes used in the XML that are mapped to the same URI
2658 | will not match as expected unless additional CSS style rules are
2659 | declared for them.In other scenarios: when the namespace prefixes used in the XML are 2675 | not known in advance by the style sheet author; or a combination of 2676 | elements with no namespace are used in conjunction with elements using 2677 | a default namespace; or the same namespace prefix is mapped to 2678 | different namespace URIs within the same document, or in 2679 | different documents; it is impossible to construct a CSS style sheet 2680 | that will function properly against all elements in those documents, 2681 | unless, the style sheet is written using a namespace URI syntax (as 2682 | outlined in this document or similar) and the document is processed by 2683 | a CSS and XML namespace aware client.
2684 | 2685 |Each specification using Selectors must define the subset of W3C 2688 | Selectors it allows and excludes, and describe the local meaning of 2689 | all the components of that subset.
2690 | 2691 |Non normative examples: 2692 | 2693 |
| Selectors profile | |
|---|---|
| Specification | 2700 |CSS level 1 |
| Accepts | 2703 |type selectors class selectors ID selectors :link, 2704 | :visited and :active pseudo-classes descendant combinator 2705 | ::first-line and ::first-letter pseudo-elements |
| Excludes | 2708 |
2709 |
2710 | universal selector namespaces |
| Extra constraints | 2720 |only one class selector allowed per sequence of simple 2721 | selectors |
| Selectors profile | |
|---|---|
| Specification | 2728 |CSS level 2 |
| Accepts | 2731 |type selectors universal selector attribute presence and 2732 | values selectors class selectors ID selectors :link, :visited, 2733 | :active, :hover, :focus, :lang() and :first-child pseudo-classes 2734 | descendant combinator child combinator adjacent sibling 2735 | combinator ::first-line and ::first-letter pseudo-elements ::before 2736 | and ::after pseudo-elements |
| Excludes | 2739 |
2740 |
2741 | content selectors namespaces |
| Extra constraints | 2750 |more than one class selector per sequence of simple selectors (CSS1 2751 | constraint) allowed |
In CSS, selectors express pattern matching rules that determine which style 2754 | rules apply to elements in the document tree. 2755 | 2756 |
The following selector (CSS level 2) will match all anchors a
2757 | with attribute name set inside a section 1 header h1:
2758 |
h1 a[name]2759 | 2760 |
All CSS declarations attached to such a selector are applied to elements 2761 | matching it.
| Selectors profile | |
|---|---|
| Specification | 2770 |STTS 3 | 2771 |
| Accepts | 2774 |
2775 |
2776 | type selectors namespaces |
| Excludes | 2783 |non-accepted pseudo-classes pseudo-elements |
| Extra constraints | 2786 |some selectors and combinators are not allowed in fragment 2787 | descriptions on the right side of STTS declarations. |
Selectors can be used in STTS 3 in two different 2790 | manners: 2791 |
This section defines conformance with the present specification only. 2800 | 2801 |
The inability of a user agent to implement part of this specification due to 2802 | the limitations of a particular device (e.g., non interactive user agents will 2803 | probably not implement dynamic pseudo-classes because they make no sense without 2804 | interactivity) does not imply non-conformance. 2805 | 2806 |
All specifications reusing Selectors must contain a Profile listing the 2808 | subset of Selectors it accepts or excludes, and describing the constraints 2809 | it adds to the current specification. 2810 | 2811 |
Invalidity is caused by a parsing error, e.g. an unrecognized token or a token 2812 | which is not allowed at the current parsing point. 2813 | 2814 |
User agents must observe the rules for handling parsing errors: 2815 |
Specifications reusing Selectors must define how to handle parsing 2823 | errors. (In the case of CSS, the entire rule in which the selector is 2824 | used is dropped.)
2825 | 2826 | 2835 | 2836 |This specification has a test 2840 | suite allowing user agents to verify their basic conformance to 2841 | the specification. This test suite does not pretend to be exhaustive 2842 | and does not cover all possible combined cases of Selectors.
2843 | 2844 |The CSS working group would like to thank everyone who has sent 2847 | comments on this specification over the years.
2848 | 2849 |The working group would like to extend special thanks to Donna 2850 | McManus, Justin Baker, Joel Sklar, and Molly Ives Brower who perfermed 2851 | the final editorial review.
2852 | 2853 |http://www.w3.org/TR/REC-CSS1)
2860 |
2861 | http://www.w3.org/TR/CSS21)
2864 |
2865 | http://www.w3.org/TR/charmod/)
2868 |
2869 | http://www.w3.org/TR/html4/)
2875 |
2876 | http://www.w3.org/TR/REC-MathML/)
2879 |
2880 | http://www.ietf.org/rfc/rfc3066.txt)
2883 |
2884 | http://www.w3.org/TR/NOTE-STTS3)
2887 |
2888 | http://www.w3.org/TR/SVG/)
2891 |
2892 | http://www.unicode.org/versions/)http://www.w3.org/TR/REC-xml/)
2900 |
2901 | http://www.w3.org/TR/REC-xml-names/)
2904 |
2905 |