\usemodule[present-lines] \definecolor[maincolor][b=.4] \setuppapersize[A4][A4] \startdocument[title=CSS selectors,subtitle={\CONTEXT\ 2017 Maibach}] \startbuffer[selector-001] b.one b.two b.one.two b.three b#first c d e d e d e e d f @foo = bar @bar = foo @bar = foo1 @bar = foo2 @bar = foo3 @bar = foo+4 g g gg d g gg f g gg f.one g g gg f.two g gg f.three g f.one g f.three @whatever = four five six \stopbuffer \startbuffer[selector-002] title 1 title 2 title 3 title 4 \stopbuffer \startbuffer[selector-003] title 1 title 1.1 title 2 title 2.1 title 3 title 3.1 title 4 title 4.1 \stopbuffer \xmlloadbuffer{selector-001}{selector-001} \xmlloadbuffer{selector-002}{selector-002} \xmlloadbuffer{selector-003}{selector-003} \startxmlsetups xml:selector:demo \ignorespaces\xmlverbatim{#1}\par \stopxmlsetups \unexpanded\def\showCSSdemo#1#2% {\blank \textrule{\tttf#2} \startlines \dontcomplain \tttf \obeyspaces \xmlcommand{#1}{#2}{xml:selector:demo} \stoplines \blank} \startchapter[title=Needed or not?] \startitemize \startitem used in \HTML\ styling \stopitem \startitem a bit different from the path based method \stopitem \startitem shortcuts for filtering by attribute \stopitem \startitem class filtering is special because it checks for list \stopitem \startitem one can select more at the same time \stopitem \startitem performance is okay compared to path lookup \stopitem \startitem selectors go between curly braces: \starttyping \xmlall {#1} {{foo bar .whatever, bar foo .whatever}} \stoptyping \stopitem \stopitemize \stopchapter \startchapter[title=Supported methods] The following methods are supported: \starttabulate[|T||] \NC \type {element} \NC all tags element \NC \NR \NC \type {element-1 > element-2} \NC all tags element-2 with parent tag element-1 \NC \NR \NC \type {element-1 + element-2} \NC all tags element-2 preceded by tag element-1 \NC \NR \NC \type {element-1 ~ element-2} \NC all tags element-2 preceded by tag element-1 \NC \NR \NC \type {element-1 element-2} \NC all tags element-2 inside tag element-1 \NC \NR \NC \type {[attribute]} \NC has attribute \NC \NR \NC \type {[attribute=value]} \NC attribute equals value\NC \NR \NC \type {[attribute~=value]} \NC attribute contains value (space is separator) \NC \NR \NC \type {[attribute^="value"]} \NC attribute starts with value \NC \NR \NC \type {[attribute$="value"]} \NC attribute ends with value \NC \NR \NC \type {[attribute*="value"]} \NC attribute contains value \NC \NR \NC \type {.class} \NC has class \NC \NR \NC \expanded{\type {\letterhash id}} \NC has id \NC \NR \NC \type {:nth-child(n)} \NC the child at index n \NC \NR \NC \type {:nth-last-child(n)} \NC the child at index n from the end \NC \NR \NC \type {:first-child} \NC the first child \NC \NR \NC \type {:last-child} \NC the last child \NC \NR \NC \type {:nth-of-type(n)} \NC the match at index n \NC \NR \NC \type {:nth-last-of-type(n)} \NC the match at index n from the end \NC \NR \NC \type {:first-of-type} \NC the first match \NC \NR \NC \type {:last-of-type} \NC the last match \NC \NR \NC \type {:only-of-type} \NC the only match or nothing \NC \NR \NC \type {:only-child} \NC the only child or nothing \NC \NR \NC \type {:empty} \NC only when empty \NC \NR \NC \type {:root} \NC the whole tree \NC \NR \stoptabulate \stopchapter \startchapter[title=Filtering classes] \typebuffer[selector-001] \showCSSdemo{selector-001}{{.one}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{.one, .two}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{.one, .two, \letterhash first}} \page \stopchapter \startchapter[title=Filtering attributes] \typebuffer[selector-001] \showCSSdemo{selector-001}{{[foo], [bar=foo]}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{[bar\lettertilde=foo]}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{[bar\letterhat="foo"]}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{[whatever\lettertilde="five"]}} \page \stopchapter \startchapter[title=Combining methods] \typebuffer[selector-001] \showCSSdemo{selector-001}{{g f .one, g f .three}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{g > f .one, g > f .three}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{d + e}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{d ~ e}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{d ~ e, g f .one, g f .three}} \page \stopchapter % \startchapter[title=Negation] % \typebuffer[selector-001] \showCSSdemo{selector-001}{{:not([whatever\lettertilde="five"])}} \page % \typebuffer[selector-001] \showCSSdemo{selector-001}{{:not(d)}} \page \stopchapter \startchapter[title=Child selectors] \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-child(3)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-last-child(3)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{g:nth-of-type(3)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{g:nth-last-of-type(3)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:first-child}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:last-child}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{e:first-of-type}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{gg d:only-of-type}} \page \stopchapter \startchapter[title=Simple formulas] \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-child(even)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-child(odd)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-child(3n+1)}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{a:nth-child(2n+3)}} \page \stopchapter \startchapter[title=Special cases] \typebuffer[selector-001] \showCSSdemo{selector-001}{{g:empty}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{g:root}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{{*}} \page \stopchapter \startchapter[title=Combinations] \typebuffer[selector-001] \showCSSdemo{selector-001}{{g gg f .one}} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{g/gg/f[@class='one']} \page \typebuffer[selector-001] \showCSSdemo{selector-001}{g/{gg f .one}} \page \stopchapter \startchapter[title=Comparison (1)] \typebuffer[selector-002] \showCSSdemo{selector-002}{{document title .one, document title .three}} \page \typebuffer[selector-002] \showCSSdemo{selector-002}{/document/title[(@class='one') or (@class='three')]} \page \stopchapter \startchapter[title=Comparison (2)] \typebuffer[selector-003] \showCSSdemo{selector-003}{{document title .one + subtitle, document title .two + subtitle}} {\em A combined filter triggers a sorting pass!} \stopchapter \stopdocument