Skip to content

Commit 03794f4

Browse files
committed
[css3-lists] Rewrite counter inheritance to be correct. Editorial tweaks elsewhere in the counter section.
--HG-- extra : rebase_source : 4bff40b5a1e7b55c2860ae398a4dfa4c9e6dd567
1 parent b8b8b68 commit 03794f4

2 files changed

Lines changed: 171 additions & 47 deletions

File tree

css3-lists/Overview.html

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
<meta content="CSS Lists and Counters Module Level 3" name=dcterms.title>
1212
<meta content=text name=dcterms.type>
13-
<meta content=2012-11-27 name=dcterms.issued>
13+
<meta content=2012-11-29 name=dcterms.issued>
1414
<meta content="http://dev.w3.org/csswg/css3-lists/" name=dcterms.creator>
1515
<meta content=W3C name=dcterms.publisher>
16-
<meta content="http://www.w3.org/TR/2012/ED-css3-lists-20121127/"
16+
<meta content="http://www.w3.org/TR/2012/ED-css3-lists-20121129/"
1717
name=dcterms.identifier>
1818
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
1919
<link href="http://dev.w3.org/csswg/default.css" rel=stylesheet
@@ -36,13 +36,13 @@
3636

3737
<h1>CSS Lists and Counters Module Level 3</h1>
3838

39-
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 27 November
39+
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 29 November
4040
2012</h2>
4141

4242
<dl>
4343
<dt>This version:
4444

45-
<dd><!--<a href="http://www.w3.org/TR/2012/WD-css3-lists-20121127">http://www.w3.org/TR/2012/WD-css3-lists-20121127</a>-->
45+
<dd><!--<a href="http://www.w3.org/TR/2012/WD-css3-lists-20121129">http://www.w3.org/TR/2012/WD-css3-lists-20121129</a>-->
4646
<a
4747
href="http://dev.w3.org/csswg/css3-lists/">http://dev.w3.org/csswg/css3-lists/</a>
4848

@@ -1496,25 +1496,21 @@ <h3 id=marker-side><span class=secno>7.2. </span> The ‘<a
14961496
<h2 id=counter><span class=secno>8. </span> Automatic Numbering With
14971497
Counters</h2>
14981498

1499-
<p> A <dfn id=counter0>counter</dfn> is a special object used, among other
1500-
things, to automatically number list items in CSS. They are created and
1501-
manipulated with the ‘<a href="#counter-increment"><code
1499+
<p> A <dfn id=counter0>counter</dfn> is a special concept used, among other
1500+
things, to automatically number list items in CSS. Every element has a
1501+
collection of zero or more counters, which are inherited through the
1502+
document tree in a way similar to inherited property values. They are
1503+
created and manipulated with the ‘<a href="#counter-increment"><code
15021504
class=property>counter-increment</code></a>’, ‘<a
15031505
href="#counter-set"><code class=property>counter-set</code></a>’ and
15041506
<a href="#counter-reset"><code
15051507
class=property>counter-reset</code></a>’ properties, and used with the
15061508
<a href="#counter-function"><code
15071509
title=counter-function>counter()</code></a> and <a
15081510
href="#counters-function"><code
1509-
title=counters-function>counters()</code></a> functions. Counters have an
1510-
integer value, a creator element, and possibly another counter nested
1511-
inside themselves.
1512-
1513-
<p> Each element has a collection of zero or more counters. Counters
1514-
inherit between elements in a way similar to other CSS values, but they
1515-
can inherit from previous siblings in addition to the normal behavior of
1516-
inheriting from parents. Counters can also "nest" inside of each other,
1517-
which makes it easier to number nested lists.
1511+
title=counters-function>counters()</code></a> functions. Counters have a
1512+
name, an integer value, a creator element, and possibly another counter
1513+
nested inside themselves.
15181514

15191515
<h3 id=counter-properties><span class=secno>8.1. </span> Manipulating
15201516
Counters: the ‘<a href="#counter-increment"><code
@@ -1724,9 +1720,10 @@ <h3 id=counter-properties><span class=secno>8.1. </span> Manipulating
17241720
}</pre>
17251721
</div>
17261722

1727-
<p> Resetting counters must be done <em>before</em> setting counters, which
1728-
must be done <em>before</em> incrementing counters, which must be done
1729-
<em>before</em> using counters (for example, in the ‘<code
1723+
<p> Inheriting counters must be done <em>before</em> resetting counters,
1724+
which must be done <em>before</em> setting counters, which must be done
1725+
<em>before</em> incrementing counters, which must be done <em>before</em>
1726+
using counters (for example, in the ‘<code
17301727
class=property>content</code>’ property).
17311728

17321729
<div class=note>
@@ -1756,15 +1753,78 @@ <h3 id=creating-counters><span class=secno>8.2. </span> Creating and
17561753
Inheriting Counters</h3>
17571754

17581755
<p> Every element has a (possibly empty) set of counters. Like many other
1759-
CSS values, an element can inherit counters from another element. Unlike
1760-
many other CSS values, elements can inherit counters from their previous
1761-
sibling as well as their parent.
1756+
CSS values, an element can inherit counters from another element. However,
1757+
unlike other CSS values, the method that counters are inherited is
1758+
somewhat complex. A counter and its value are inherited
1759+
<em>separately</em>, possibly from different elements.
1760+
1761+
<p> If an element has a previous sibling, it must inherit all of the
1762+
sibling's counters. Otherwise, if the element has a parent, it must
1763+
inherit all of the parent's counters. Otherwise, the element must have an
1764+
empty set of counters.
1765+
1766+
<p> The element then inherits counter <em>values</em> from the immediately
1767+
preceding element <strong>in document order</strong>. This must be done by
1768+
examining the set of counters that the immediately preceding element has,
1769+
and, for every counter that exists in both the element's set and the
1770+
preceding element's set, giving the element's counter the same value. (If
1771+
an element is the first element in the document, and thus has no
1772+
immediately preceding element, it also doesn't have a parent or a previous
1773+
sibling, and thus no counters to begin with.)
1774+
1775+
<div class=example id=counter-inheritance-example>
1776+
<p> Take the following code as an example:
1777+
1778+
<pre>
1779+
&lt;ul style='counter-reset: example 0;'>
1780+
<b
1781+
class=foo>&lt;li id='foo' style='counter-increment: example;'></b>
1782+
foo
1783+
<b
1784+
class=bar>&lt;div id='bar' style='counter-increment: example;'>bar&lt;/div></b>
1785+
&lt;/li>
1786+
<b
1787+
class=baz>&lt;li id='baz'></b>
1788+
baz
1789+
&lt;/li>
1790+
&lt;/ul></pre>
1791+
1792+
<p> Recall that "in document order" turns a document tree into an ordered
1793+
list, where an element comes before its children, and its children come
1794+
before its next sibling. In other words, for a language like HTML, its
1795+
the order in which the parser encounters start tags as it reads the
1796+
document.
1797+
1798+
<p> In here, the <code>&lt;ul></code> element establishes a new counter
1799+
named "example", and sets its value to 0. The <b class=foo>"foo"</b>
1800+
element, being the first child of the <code>&lt;ul></code>, inherits this
1801+
counter. Its parent is also its immediately preceding element in document
1802+
order, so it inherits the value 0 with it, and then immediately
1803+
increments the value to 1.
1804+
1805+
<p> The same happens with the <b class=bar>"bar"</b> element. It inherits
1806+
the "example" counter from <b class=foo>"foo"</b>, and inherits the value
1807+
1 from it as well and increments it to 2.
1808+
1809+
<p> However, the <b class=baz>"baz"</b> element is a bit different. It
1810+
inherits the "example" counter from the <b class=foo>"foo"</b> element,
1811+
its previous sibling. However, rather than inheriting the value 1 from <b
1812+
class=foo>"foo"</b> along with the counter, in inherits the value 2 from
1813+
<b class=bar>"bar"</b>, the previous element in document order.
1814+
1815+
<p> This behavior allows a single counter to be used throughout a
1816+
document, continuously incrementing, without the author having to worry
1817+
about the nested structure of their document.
1818+
</div>
17621819

1763-
<p> If an element has a previous sibling, it inherits all of the sibling's
1764-
counters (both those the sibling inherited, and ones the sibling created).
1765-
Otherwise, if an element has a parent, it inherits all of the parent's
1766-
counters. Otherwise, the element starts with an empty set of counters.
1820+
<head>
1821+
<style>
1822+
#counter-inheritance-example b.foo { color: hsl(0, 80%, 40%); }
1823+
#counter-inheritance-example b.bar { color: hsl(120, 80%, 30%); }
1824+
#counter-inheritance-example b.baz { color: hsl(240, 80%, 50%); }
1825+
</style>
17671826

1827+
<body>
17681828
<p> Elements can create additional counters on themselves, which can then
17691829
be passed onto siblings or children. To <dfn
17701830
id=create-a-new-counter>create a new counter</dfn>, specify an element
@@ -1775,8 +1835,8 @@ <h3 id=creating-counters><span class=secno>8.2. </span> Creating and
17751835
<li> If no counters of that name exist on the element, create a new
17761836
counter with that name on the element.
17771837

1778-
<li> Otherwise, if a counter of the name exists on the element, and it was
1779-
created by a preceding sibling, replace the innermost counter of that
1838+
<li> Otherwise, if a counter of that name exists on the element, and it
1839+
was created by a preceding sibling, replace the innermost counter of that
17801840
name on the element with a newly-created counter with that name.
17811841

17821842
<li> Otherwise, create a new counter with that name and nest it inside of

css3-lists/Overview.src.html

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -978,21 +978,17 @@ <h2 id='counter'>
978978
Automatic Numbering With Counters</h2>
979979

980980
<p>
981-
A <dfn>counter</dfn> is a special object
981+
A <dfn>counter</dfn> is a special concept
982982
used, among other things, to automatically number list items in CSS.
983+
Every element has a collection of zero or more counters,
984+
which are inherited through the document tree in a way similar to inherited property values.
983985
They are created and manipulated with the 'counter-increment', 'counter-set' and 'counter-reset' properties,
984986
and used with the <code title='counter-function'>counter()</code> and <code title='counters-function'>counters()</code> functions.
985-
Counters have an integer value,
987+
Counters have a name,
988+
an integer value,
986989
a creator element,
987990
and possibly another counter nested inside themselves.
988991

989-
<p>
990-
Each element has a collection of zero or more counters.
991-
Counters inherit between elements in a way similar to other CSS values,
992-
but they can inherit from previous siblings in addition to the normal behavior of inheriting from parents.
993-
Counters can also "nest" inside of each other,
994-
which makes it easier to number nested lists.
995-
996992
<h3 id='counter-properties'>
997993
Manipulating Counters: the 'counter-increment', 'counter-set' and 'counter-reset' properties</h2>
998994

@@ -1141,7 +1137,8 @@ <h3 id='counter-properties'>
11411137
</div>
11421138

11431139
<p>
1144-
Resetting counters must be done <em>before</em> setting counters,
1140+
Inheriting counters must be done <em>before</em> resetting counters,
1141+
which must be done <em>before</em> setting counters,
11451142
which must be done <em>before</em> incrementing counters,
11461143
which must be done <em>before</em> using counters
11471144
(for example, in the 'content' property).
@@ -1178,17 +1175,84 @@ <h3 id="creating-counters">
11781175
Every element has a (possibly empty) set of counters.
11791176
Like many other CSS values,
11801177
an element can inherit counters from another element.
1181-
Unlike many other CSS values,
1182-
elements can inherit counters from their previous sibling
1183-
as well as their parent.
1178+
However, unlike other CSS values,
1179+
the method that counters are inherited is somewhat complex.
1180+
A counter and its value are inherited <em>separately</em>,
1181+
possibly from different elements.
11841182

11851183
<p>
11861184
If an element has a previous sibling,
1187-
it inherits all of the sibling's counters
1188-
(both those the sibling inherited, and ones the sibling created).
1189-
Otherwise, if an element has a parent,
1190-
it inherits all of the parent's counters.
1191-
Otherwise, the element starts with an empty set of counters.
1185+
it must inherit all of the sibling's counters.
1186+
Otherwise, if the element has a parent,
1187+
it must inherit all of the parent's counters.
1188+
Otherwise, the element must have an empty set of counters.
1189+
1190+
<p>
1191+
The element then inherits counter <em>values</em>
1192+
from the immediately preceding element <strong>in document order</strong>.
1193+
This must be done by examining the set of counters that the immediately preceding element has,
1194+
and, for every counter that exists in both the element's set and the preceding element's set,
1195+
giving the element's counter the same value.
1196+
(If an element is the first element in the document,
1197+
and thus has no immediately preceding element,
1198+
it also doesn't have a parent or a previous sibling,
1199+
and thus no counters to begin with.)
1200+
1201+
<div class='example' id='counter-inheritance-example'>
1202+
<p>
1203+
Take the following code as an example:
1204+
1205+
<pre>
1206+
&lt;ul style='counter-reset: example 0;'>
1207+
<b class='foo'>&lt;li id='foo' style='counter-increment: example;'></b>
1208+
foo
1209+
<b class='bar'>&lt;div id='bar' style='counter-increment: example;'>bar&lt;/div></b>
1210+
&lt;/li>
1211+
<b class='baz'>&lt;li id='baz'></b>
1212+
baz
1213+
&lt;/li>
1214+
&lt;/ul></pre>
1215+
1216+
<p>
1217+
Recall that "in document order" turns a document tree into an ordered list,
1218+
where an element comes before its children,
1219+
and its children come before its next sibling.
1220+
In other words,
1221+
for a language like HTML,
1222+
its the order in which the parser encounters start tags as it reads the document.
1223+
1224+
<p>
1225+
In here, the <code>&lt;ul></code> element establishes a new counter named "example",
1226+
and sets its value to 0.
1227+
The <b class='foo'>"foo"</b> element, being the first child of the <code>&lt;ul></code>,
1228+
inherits this counter.
1229+
Its parent is also its immediately preceding element in document order,
1230+
so it inherits the value 0 with it,
1231+
and then immediately increments the value to 1.
1232+
1233+
<p>
1234+
The same happens with the <b class='bar'>"bar"</b> element.
1235+
It inherits the "example" counter from <b class='foo'>"foo"</b>,
1236+
and inherits the value 1 from it as well
1237+
and increments it to 2.
1238+
1239+
<p>
1240+
However, the <b class='baz'>"baz"</b> element is a bit different.
1241+
It inherits the "example" counter from the <b class='foo'>"foo"</b> element,
1242+
its previous sibling.
1243+
However, rather than inheriting the value 1 from <b class='foo'>"foo"</b> along with the counter,
1244+
in inherits the value 2 from <b class='bar'>"bar"</b>, the previous element in document order.
1245+
1246+
<p>
1247+
This behavior allows a single counter to be used throughout a document,
1248+
continuously incrementing,
1249+
without the author having to worry about the nested structure of their document.
1250+
</div>
1251+
<style>
1252+
#counter-inheritance-example b.foo { color: hsl(0, 80%, 40%); }
1253+
#counter-inheritance-example b.bar { color: hsl(120, 80%, 30%); }
1254+
#counter-inheritance-example b.baz { color: hsl(240, 80%, 50%); }
1255+
</style>
11921256

11931257
<p>
11941258
Elements can create additional counters on themselves,
@@ -1205,7 +1269,7 @@ <h3 id="creating-counters">
12051269
create a new counter with that name on the element.
12061270

12071271
<li>
1208-
Otherwise, if a counter of the name exists on the element,
1272+
Otherwise, if a counter of that name exists on the element,
12091273
and it was created by a preceding sibling,
12101274
replace the innermost counter of that name on the element
12111275
with a newly-created counter with that name.

0 commit comments

Comments
 (0)