forked from SquareBracketAssociates/EnterprisePharo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenoirST.pillar
1240 lines (971 loc) · 36.3 KB
/
RenoirST.pillar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"metadata" : {
"title": "Cascading Style Sheets with RenoirSt",
"attribution": "Gabriel Omar Cotelli with Damien Cassou"
},
"headingLevelOffset":2
}
@cha:renoirst
RenoirST is a DSL enabling programmatic cascading style sheet generation for Pharo developed by Gabriel Omar Cotelli.
RenoirST aims to improve CSS integration with existing web frameworks. To do that, RenoirST generates CSS out of Pharo code. Renoir features are:
common properties declaration, CSS3 selector support, important rules, font face rules and media queries support.
In this tutorial we will present the key features of RenoirSt with a large set of examples.
This tutorial assumes some knowledge of CSS and Pharo. For a little introduction about CSS you can read the
*Seaside's book CSS chapter>http://book.seaside.st/book/fundamentals/css*.
!Getting Started
To load the library in your image, evaluate:
[[[
Metacello new
configuration: 'RenoirSt';
githubUser: 'gcotelli' project: 'RenoirSt' commitish: 'master' path: 'source';
load
]]]
download a ready to use image from the *Pharo contribution CI Server>https://ci.inria.fr/pharo-contribution/job/RenoirSt* or install it using the Catalog/Configuration Browser.
The main entry point for the library is the class ==CascadingStyleSheetBuilder==. In a workspace or playground, inspect the result of the following expression:
[[[
CascadingStyleSheetBuilder new build
]]]
You now have an inspector on your first (empty and useless) style sheet. Real stylesheets are composed of rules (or rule-sets), where each one has a selector
and a declaration group. The selector determines if the rule applies to some element in the DOM, and the declaration group specifies the style to apply.
Our first useful style sheet will simply assign a margin to every div element in the DOM.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style margin: 2 px ];
build
]]]
the expected result in CSS is:
[[[
div
{
margin: 2px;
}
]]]
The message ==declareRuleSetFor:with:== is used to configure a rule-set in the builder. The message requires two blocks: the first block defines the selector
and the second defines the style to apply to elements matching the selector. The ==selector== argument of the first block is an entry point to construct the
selector (more on this later). The ==style== argument of the second block is an entry point to declare CSS properties and values.
The properties API is mostly defined following this rules:
- Properties without dashes in the name are directly mapped: the margin CSS property is mapped to a ==margin:== message send.
- Properties with one or more dashes are mapped using camel case: the margin-top CSS property is mapped to the ==marginTop:== message send.
!Defining the Rules
We now present how the various CSS rules can be expressed with RenoirSt. RenoirSt supports many CSS types, comments, and even functional notation.
!!Basic CSS Types
!!!Lengths, Angles, Times and Frequencies
The library provides out-of-the-box support for the length, angle, time and frequency units in the CSS spec. There are extensions for ==Integer== and ==Float== classes allowing to obtain lengths.
The supported length units are:
- ==em== relative to font size
- ==ex== relative to "x" height
- ==ch== relative to width of the zero glyph in the element's font
- ==rem== relative to font size of root element
- ==vw== 1% of viewport's width
- ==vh== 1% of viewport's height
- ==vmin== 1% of viewport's smaller dimension
- ==vmax== 1% of viewport's larger dimension
- ==cm== centimeters
- ==mm== millimeteres
- ==in== inches
- ==pc== picas
- ==pt== points
- ==px== pixels (note that CSS has some special definition for pixel)
The supported angle units are:
- ==deg== degrees
- ==grad== gradians
- ==rad== radians
- ==turn== turns
The supported time units are:
- ==s== seconds
- ==ms== milliseconds
The supported frequency units are:
- ==Hz== Hertz
- ==kHz== KiloHertz
RenoirST also supports the creation of percentages: ==50 percent== is mapped to ==50%== in the resulting CSS.
Some properties require integer or floating point values. In these cases just use the standard Pharo integer and float support. For example:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style zIndex: 2 ];
build
]]]
!!!Colors
The library supports abstractions for properties requiring color values. The shared pool ==CssSVGColors== provides easy access to colors in the SVG 1.0 list,
and the abstractions ==CssRGBColor== and ==CssHSLColor== allow the creation of colors in the RGB and HSL spaces including alpha support.
For example,
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style |
style
backgroundColor: CssSVGColors aliceBlue;
borderColor: (CssRGBColor red: 0 green: 128 blue: 0 alpha: 0.5) ];
build
]]]
evaluates to:
[[[language=css
div
{
background-color: aliceblue;
border-color: rgba(0,128,0,0.5);
}
]]]
@@note In a real scenario you should avoid hard coding colors as in the examples. It is recommended to put colors in objects representing a theme or something that gives them a name related to your application.
RGB-Colors also support percentage values:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style |
style borderColor: (CssRGBColor red: 0 percent green: 50 percent blue: 0 percent) ];
build
]]]
evaluates to:
[[[language=css
div
{
border-color: rgb(0%,50%,0%);
}
]]]
Notice the difference in the used message because there is no alpha channel specification.
!!!Constants
A lot of values for CSS properties are just keyword constants. This support is provided by the classes ==CssConstants== and ==CssFontConstants==.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style textAlign: CssConstants justify ];
build
]]]
evaluates to:
[[[language=css
div
{
text-align: justify;
}
]]]
!!!Several Property Values
Some properties support a wide range of values. For example the ==margin== property can have 1, 2 , 3 or 4 values specified. If you only need one value, just
pass it as a parameter. For more than one value, use an array:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style margin: { 2 px. 4 px } ];
build
]]]
evaluates to:
[[[language=css
div
{
margin: 2px 4px;
}
]]]
!!!URLs
==ZnUrl== instances can be used as the value for properties requiring an URI. Both relative and absolute URLs are accepted. A relative URL is considered by default relative to the site root.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div class: 'logo' ]
with: [ :style | style backgroundImage: 'images/logo.png' asZnUrl ];
declareRuleSetFor: [ :selector | selector div class: 'logo' ]
with: [ :style | style backgroundImage: 'http://www.example.com/images/logo.png' asZnUrl ];
build
]]]
Evaluates to:
[[[language=css
div.logo
{
background-image: url("/images/logo.png");
}
div.logo
{
background-image: url("http://www.example.com/images/logo.png");
}
]]]
To use a URL relative to the style sheet, send to it the message ==relativeToStyleSheet==.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div class: 'logo' ]
with: [ :style | style backgroundImage: 'images/logo.png' asZnUrl relativeToStyleSheet];
build
]]]
Evaluates to:
[[[language=css
div.logo
{
background-image: url("images/logo.png");
}
]]]
!!Comments
When declaring rule sets, the library supports attaching comments to them with the ==declareRuleSetFor:with:andComment:== message:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style margin: 2 pc ]
andComment: 'Two picas margin';
build
]]]
evaluates to:
[[[language=css
/*Two picas margin*/
div
{
margin: 2pc;
}
]]]
RenoirST also supports defining stand-alone comments (not attached to any rule):
[[[
CascadingStyleSheetBuilder new
comment: 'A general comment';
build
]]]
evaluates to:
[[[language=css
/*A general comment*/
]]]
!!Functional Notation
A functional notation is a type of CSS component value that can represent complex types or invoke special processing. Mathematical expressions, toggling
between values, attribute references, and gradients are all supported in RenoirST.
!!!Mathematical Expressions
The library provides support for math expressions using the ==CssMathExpression== abstraction:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style margin: (CssMathExpression on: 2 pc) / 3 + 2 percent ];
build
]]]
evaluates to:
[[[language=css
div
{
margin: calc(2pc / 3 + 2%);
}
]]]
!!!Toggling Between Values
To let descendant elements cycle over a list of values instead of inheriting the same value, one can use the ==CssToggle== abstraction:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector unorderedList unorderedList ]
with: [ :style | style listStyleType: (CssToggle cyclingOver: { CssConstants disc. CssConstants circle. CssConstants square}) ];
build
]]]
evaluates to:
[[[language=css
ul ul
{
list-style-type: toggle(disc, circle, square);
}
]]]
!!!Attribute References
The ==attr()== function is allowed as a component value in properties applied to an element or pseudo-element. The function returns the value of an attribute on
the element. If used on a pseudo-element, it returns the value of the attribute on the pseudo-element's originating element. This function is supported using
the ==CssAttributeReference== abstraction and can be used simply providing an attribute name:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div before ]
with: [ :style | style content: (CssAttributeReference toAttributeNamed: 'title') ];
build
]]]
Evaluates to:
[[[language=css
div::before
{
content: attr(title string);
}
]]]
RenoirST allows for providing the type or unit of the attribute (if no type or unit is specified the ==string== type is assumed):
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style |
style width: (CssAttributeReference
toAttributeNamed: 'height'
ofType: CssLengthUnits pixel) ];
build
]]]
evaluates to:
[[[language=css
div
{
width: attr(height px);
}
]]]
Additionally, it is possible to provide a value to use when the attribute is absent:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div before ]
with: [ :style |
style content: (CssAttributeReference
toStringAttributeNamed: 'title'
withFallback: 'Missing title')
];
build
]]]
evaluates to:
[[[language=css
div::before
{
content: attr(title string, "Missing title");
}
]]]
!!!Gradients
A gradient is an image that smoothly fades from one color to another. Gradients are commonly used for subtle shading in background images, buttons, and many
other places. The gradient notations described in this section allow an author to specify such an image in a terse syntax. This notation is supported using
==CssLinearGradient== and ==CssRadialGradient== abstractions.
To represent a simple linear gradient from a color to another, send the ==fading:== message to ==CssLinearGradient== with the two colors in an array as a parameter:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | style background: (CssLinearGradient
fading: { CssSVGColors yellow. CssSVGColors blue }) ]
]]]
The above code evaluates to:
[[[language=css
div
{
background: linear-gradient(yellow, blue);
}
]]]
By default, a gradient's direction is from top to bottom. To specify a different direction, the author can use the ==to:fading:== message instead:
[[[
CssLinearGradient
to: CssConstants right
fading: { CssSVGColors yellow. CssSVGColors blue }
]]]
The above code will result in a gradient with yellow on the left side and blue on the right side. The equivalent CSS is:
[[[language=css
linear-gradient(to right, yellow, blue);
]]]
To specify a diagonal direction, an array must be passed as the ==to:== argument:
[[[
CssLinearGradient
to: { CssConstants top. CssConstants right }
fading: { CssSVGColors yellow. CssSVGColors blue }
]]]
The above code will result in a gradient with blue in the top right corner and yellow in the bottom left one. The equivalent CSS is:
[[[language=css
linear-gradient(to top right, yellow, blue);
]]]
Directions can also be specified as an angle by sending the ==rotated:fading:== message:
[[[
CssLinearGradient
rotated: 45 deg
fading: { CssSVGColors yellow. CssSVGColors blue }
]]]
The above code maps to:
[[[language=css
linear-gradient(45deg, yellow, blue);
]]]
Gradients can be fine-tuned by manipulating so-called color stops:
[[[
CssLinearGradient
to: CssConstants right
fading: {
CssSVGColors yellow.
CssColorStop for: CssSVGColors blue at: 30 percent }
]]]
The above code maps to:
[[[language=css
linear-gradient(to right, yellow, blue 30%);
]]]
This results in a linear gradient from left to right with yellow at the left side and plain blue from 30% (of the horizontal line) to the right side. More than
two colors can be passed as argument to ==fading:==. This can be used to create rainbows:
[[[
CssLinearGradient
to: CssConstants right
fading: {
CssSVGColors red.
CssSVGColors orange.
CssSVGColors yellow.
CssSVGColors green.
CssSVGColors blue.
CssSVGColors indigo.
CssSVGColors violet.
}
]]]
This maps to:
[[[language=css
linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
]]]
To create radial gradients, the author must send messages to ==CssRadialGradient==. For example,
[[[
CssRadialGradient fading: { CssSVGColors yellow. CssSVGColors green }
]]]
maps to:
[[[language=css
radial-gradient(yellow,green)
]]]
This results in a radial gradient with yellow at the center and green all around. Coordinates can be passed to both the first and second parameters of the
==elliptical:at:fading:== message:
[[[
(CssRadialGradient
elliptical: {20 px. 30 px}
at: { 20 px. 30 px}
fading: { CssSVGColors red. CssSVGColors yellow. CssSVGColors green })
]]]
This maps to:
[[[language=css
background: radial-gradient(20px 30px ellipse at 20px 30px, red, yellow, green);
]]]
To make the gradient repeatable, just send to it the message ==beRepeating==. For Example:
[[[
(CssRadialGradient fading: { CssSVGColors yellow. CssSVGColors green }) beRepeating
]]]
renders as:
[[[language=css
repeating-radial-gradient(yellow, green);
]]]
!!!Box Shadows
Box Shadows are supported with ==CssBoxShadow== abstraction. This abstraction simplifies the use of the ==box-shadow== property.
[[[
CssBoxShadow
horizontalOffset: 64 px
verticalOffset: 64 px
blurRadius: 12 px
spreadDistance: 40 px
color: (CssSVGColors black newWithAlpha: 0.4)
]]]
evaluates to:
[[[language=css
64px 64px 12px 40px rgba(0,0,0,0.4)
]]]
Several shadows can be combined:
[[[
(CssBoxShadow horizontalOffset: 64 px verticalOffset: 64 px blurRadius: 12 px spreadDistance: 40 px color: (CssSVGColors black newWithAlpha: 0.4)) ,
(CssBoxShadow horizontalOffset: 12 px verticalOffset: 11 px blurRadius: 0 px spreadDistance: 8 px color: (CssSVGColors black newWithAlpha: 0.4)) beInset
]]]
Evaluates to:
[[[language=css
64px 64px 12px 40px rgba(0,0,0,0.4), inset 12px 11px 0px 8px rgba(0,0,0,0.4)
]]]
!Defining the selectors
So far our focus was on the ''style'' part of the rule. Let's focus now on the available ''selectors''. Remember that a CSS selector represents a structure used to match elements in the document tree. This chapter asume some familiarity with the CSS selectors and will not go in detail about the exact meaning of each one. For more details you can take a look at *CSS3 selector documentation>http://www.w3.org/TR/css3-selectors*.
!! Type Selectors
These selectors match a specific element type in the DOM. The library provides out-of-the-box support for HTML elements. One example is the ==div== selector used in the previous chapter:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [ :selector | selector div ]
with: [ :style | ... ];
build
]]]
The following other example matches ==<ol>== (ordered list) elements:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector orderedList ]
with: [ :style | ... ]
]]]
evaluating to:
[[[language=css
ol
{
...
}
]]]
To get a list of the supported type selectors evaluate ==CssSelector selectorsInProtocol: '*RenoirSt-HTML'==.
!! Combinators
Selectors can be combined to represent complex queries. One of the most common use cases is the ''descendant combinator'':
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div orderedList ]
with: [:style | ... ]
]]]
evaluating to:
[[[language=css
div ol
{
...
}
]]]
This only matches if an ==ol== element is a descendant (direct or not) of a ==div== element.
The ''child combinator'' only matches when an element is a direct child of another one:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div > selector orderedList ]
with: [:style | ]
]]]
evaluates to
[[[language=css
div > ol
{
...
}
]]]
''Siblings combinators'' can be created using ==\+== and ==\~==:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector div + selector orderedList ]
with: [:style | ]
declareRuleSetFor: [:selector | selector div ~ selector orderedList ]
with: [:style | ]
]]]
Class selectors can be created by sending ==class:== and id selectors can be created by sending ==id:==. For example,
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector div class: 'pastoral') id: #account5 ]
with: [:style | ]
]]]
evaluates to:
[[[language=css
div.pastoral#account5
{
...
}
]]]
@@note You should not hardcode the classes and ids, they should be obtained from the same object that holds them for the HTML generation. You probably have some code setting the class(es) and/or id(s) to a particular HTML element.
A comma-separated list of selectors represents the union of all elements selected by each of the individual selectors in the list. For example, in CSS when
several selectors share the same declarations, they may be grouped into a comma-separated list.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph , selector div ]
with: [:style | ... ]
]]]
evaluates to:
[[[language=css
p, div
{
...
}
]]]
!! Attribute Selectors
Attribute selectors are useful to match an element based on its attributes and their values.
The ''attribute presence'' selector matches an element having an attribute (without considering the value of the attribute):
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector h1 havingAttribute: 'title' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
evaluates to:
[[[language=css
h1[title]
{
color: blue;
}
]]]
The ''attribute value exact matching'' selectors matches an element having an attribute with a specific value:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector span withAttribute: 'class' equalTo: 'example' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
evaluates to:
[[[language=css
span[class="example"]
{
color: blue;
}
]]]
The ''attribute value inclusion'' selector matches an element having an attribute with a value including as a word the matching term:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'rel' includes: 'copyright' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
a[rel~="copyright"]
{
color: blue;
}
]]]
Other attribute selectors are used for substring matching:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor firstValueOfAttribute: 'hreflang' beginsWith: 'en' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
a[hreflang|="en"]
{
color: blue;
}
]]]
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'type' beginsWith: 'image/' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
a[type^="image/"]
{
color: blue;
}
]]]
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor attribute: 'type' endsWith: '.html' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
a[type$=".html"]
{
color: blue;
}
]]]
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph attribute: 'title' includesSubstring: 'hello' ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
p[title*="hello"]
{
color: blue;
}
]]]
!! Pseudo-Classes
The pseudo-class concept is introduced to allow selection based on information that lies outside of the document tree or that cannot be expressed using the simpler selectors. Most pseudo-classes are supported just by sending one of the following messages ==link==, ==visited==, ==active==, ==hover==, ==focus==, ==target==, ==enabled==, ==disabled== or ==checked==.
Here is a small example that uses the pseudo-classes:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector anchor link ]
with: [:style | style color: CssSVGColors blue ];
declareRuleSetFor: [:selector | selector anchor visited active]
with: [:style | style color: CssSVGColors green ];
declareRuleSetFor: [:selector | selector anchor focus hover enabled]
with: [:style | style color: CssSVGColors green ];
declareRuleSetFor: [:selector | (selector paragraph class: 'note') target disabled]
with: [:style | style color: CssSVGColors green ];
declareRuleSetFor: [:selector | selector input checked ]
with: [:style | style color: CssSVGColors green ];
build
]]]
evaluates to:
[[[language=css
a:link
{
color: blue;
}
a:visited:active
{
color: green;
}
a:focus:hover:enabled
{
color: green;
}
p.note:target:disabled
{
color: green;
}
input:checked
{
color: green;
}
]]]
!!! Language Pseudo-Class:
The ==:lang(C)== pseudo-class can be used by sending the message ==lang:==:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector lang: 'es') > selector div ]
with: [:style | style quotes: { '"«"'. '"»"' } ];
build
]]]
evaluates to:
[[[language=css
:lang(es) > div
{
quotes: "«" "»";
}
]]]
!!! Negation Pseudo-Class:
The negation pseudo-class, ==:not(X)==, is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument. For more information take a look at the CSS spec.
This selector is supported sending the message ==not:==
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector button not: (selector havingAttribute: 'DISABLED') ]
with: [:style | style color: CssSVGColors blue ];
build
]]]
[[[language=css
button:not([DISABLED])
{
color: blue;
}
]]]
!!! Structural Pseudo-Classes
These selectors allow selection based on extra information that lies in the document tree but cannot be represented by other simpler selectors nor combinators.
The ==:root== pseudo-class represents an element that is the root of the document. To build this kind of selector just send the message ==root== to another selector:
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector root ]
with: [:style | style color: CssSVGColors grey ];
build
]]]
evaluates to:
[[[language=css
:root
{
color: grey;
}
]]]
The ==:nth-child(an+b)== pseudo-class notation represents an element that has ==an+b-1== siblings before it in the document tree, for any positive integer or
zero value of ==n==, and has a parent element. For values of ==a== and ==b== greater than zero, this effectively divides the element's children into groups of ==a== elements (the last group taking the remainder), and selecting the ==b==th element of each group. The ==a== and ==b== values must be integers (positive, negative, or zero). The index of the first child of an element is 1.
In addition to this, ==:nth-child()== can take a number, ==odd== and ==even== as arguments. The value ==odd== is equivalent to ==2n\+1==, whereas ==even== is equivalent to ==2n==.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector childAt: 3 n + 1 ]
with: [:style | style color: CssSVGColors blue ];
declareRuleSetFor: [:selector | selector childAt: 5 ]
with: [:style | style color: CssSVGColors blue ];
declareRuleSetFor: [:selector | selector childAt: CssConstants even]
with: [:style | style color: CssSVGColors blue ];
build
]]]
evaluates to:
[[[language=css
:nth-child(3n+1)
{
color: blue;
}
:nth-child(5)
{
color: blue;
}
:nth-child(even)
{
color: blue;
}
]]]
All structural pseudo-classes can be generated using the following messages:
|! CSS pseudo-class |! RenoirST selector message
| ==root()== | ==root==
| ==nth-child()== | ==childAt:==
| ==nth-last-child()== | ==childFromLastAt:==
| ==nth-of-type()== | ==siblingOfTypeAt:==
| ==nth-last-of-type()== | ==siblingOfTypeFromLastAt:==
| ==first-child== | ==firstChild==
| ==last-child== | ==lastChild==
| ==first-of-type== | ==firstOfType==
| ==last-of-type== | ==lastOfType==
| ==only-child== | ==onlyChild==
| ==only-of-type== | ==onlyOfType==
| ==empty== | ==empty==
!! Pseudo-Elements
Pseudo-elements create abstractions about the document tree beyond those specified by the document language. For instance, document languages do not offer
mechanisms to access the first letter or first line of an element's content. Pseudo-elements allow authors to refer to this otherwise inaccessible information.
Pseudo-elements may also provide authors a way to refer to content that does not exist in the source document.
The ==firstLine== pseudo-element describes the contents of the first formatted line of an element.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph firstLine ]
with: [:style | style textTransform: CssConstants uppercase ];
build
]]]
evaluates to:
[[[language=css
p::first-line
{
text-transform: uppercase;
}
]]]
The ==firstLetter== pseudo-element represents the first letter of an element, if it is not preceded by any other content (such as images or inline tables) on its line.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | selector paragraph firstLetter ]
with: [:style | style fontSize: 200 percent ];
build
]]]
evaluates to:
[[[language=css
p::first-letter
{
font-size: 200%;
}
]]]
The ==before== and ==after== pseudo-elements can be used to describe generated content before or after an element's content. The ==content== property, in conjunction with these pseudo-elements, specifies what is inserted.
[[[
CascadingStyleSheetBuilder new
declareRuleSetFor: [:selector | (selector paragraph class: 'note') before ]