-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathproperties.rs.mako
1945 lines (1764 loc) · 77.3 KB
/
properties.rs.mako
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This file is a Mako template: http://www.makotemplates.org/
#![allow(non_camel_case_types, uppercase_variables)]
pub use std::ascii::StrAsciiExt;
use serialize::{Encodable, Encoder};
pub use servo_util::url::parse_url;
use sync::Arc;
pub use url::Url;
use servo_util::cowarc::CowArc;
pub use cssparser::*;
pub use cssparser::ast::*;
use errors::{ErrorLoggerIterator, log_css_error};
pub use parsing_utils::*;
pub use self::common_types::*;
use selector_matching::MatchedProperty;
pub use self::property_bit_field::PropertyBitField;
pub mod common_types;
<%!
def to_rust_ident(name):
name = name.replace("-", "_")
if name in ["static", "super"]: # Rust keywords
name += "_"
return name
class Longhand(object):
def __init__(self, name, derived_from=None):
self.name = name
self.ident = to_rust_ident(name)
self.style_struct = THIS_STYLE_STRUCT
if derived_from is None:
self.derived_from = None
else:
self.derived_from = [ to_rust_ident(name) for name in derived_from ]
class Shorthand(object):
def __init__(self, name, sub_properties):
self.name = name
self.ident = to_rust_ident(name)
self.sub_properties = [LONGHANDS_BY_NAME[s] for s in sub_properties]
class StyleStruct(object):
def __init__(self, name, inherited):
self.name = name
self.longhands = []
self.inherited = inherited
STYLE_STRUCTS = []
THIS_STYLE_STRUCT = None
LONGHANDS = []
LONGHANDS_BY_NAME = {}
DERIVED_LONGHANDS = {}
SHORTHANDS = []
def new_style_struct(name, is_inherited):
global THIS_STYLE_STRUCT
style_struct = StyleStruct(name, is_inherited)
STYLE_STRUCTS.append(style_struct)
THIS_STYLE_STRUCT = style_struct
return ""
def switch_to_style_struct(name):
global THIS_STYLE_STRUCT
for style_struct in STYLE_STRUCTS:
if style_struct.name == name:
THIS_STYLE_STRUCT = style_struct
return ""
fail()
%>
pub mod longhands {
pub use super::*;
pub use std;
pub fn computed_as_specified<T>(value: T, _context: &computed::Context) -> T {
value
}
<%def name="raw_longhand(name, no_super=False, derived_from=None)">
<%
if derived_from is not None:
derived_from = derived_from.split()
property = Longhand(name, derived_from=derived_from)
THIS_STYLE_STRUCT.longhands.append(property)
LONGHANDS.append(property)
LONGHANDS_BY_NAME[name] = property
if derived_from is not None:
for name in derived_from:
DERIVED_LONGHANDS.setdefault(name, []).append(property)
%>
pub mod ${property.ident} {
% if not no_super:
use super::*;
% endif
pub use self::computed_value::*;
${caller.body()}
% if derived_from is None:
pub fn parse_declared(input: &[ComponentValue], base_url: &Url)
-> Option<DeclaredValue<SpecifiedValue>> {
match CSSWideKeyword::parse(input) {
Some(Some(keyword)) => Some(CSSWideKeyword(keyword)),
Some(None) => Some(CSSWideKeyword(${
"Inherit" if THIS_STYLE_STRUCT.inherited else "Initial"})),
None => parse_specified(input, base_url),
}
}
% endif
}
</%def>
<%def name="longhand(name, no_super=False, derived_from=None)">
<%self:raw_longhand name="${name}" derived_from="${derived_from}">
${caller.body()}
% if derived_from is None:
pub fn parse_specified(_input: &[ComponentValue], _base_url: &Url)
-> Option<DeclaredValue<SpecifiedValue>> {
parse(_input, _base_url).map(super::SpecifiedValue)
}
% endif
</%self:raw_longhand>
</%def>
<%def name="single_component_value(name, derived_from=None)">
<%self:longhand name="${name}" derived_from="${derived_from}">
${caller.body()}
pub fn parse(input: &[ComponentValue], base_url: &Url) -> Option<SpecifiedValue> {
one_component_value(input).and_then(|c| from_component_value(c, base_url))
}
</%self:longhand>
</%def>
<%def name="single_keyword_computed(name, values)">
<%self:single_component_value name="${name}">
${caller.body()}
pub mod computed_value {
#[deriving(Eq, Clone, FromPrimitive)]
pub enum T {
% for value in values.split():
${to_rust_ident(value)},
% endfor
}
}
pub type SpecifiedValue = computed_value::T;
#[inline] pub fn get_initial_value() -> computed_value::T {
${to_rust_ident(values.split()[0])}
}
pub fn from_component_value(v: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
get_ident_lower(v).and_then(|keyword| {
match keyword.as_slice() {
% for value in values.split():
"${value}" => Some(${to_rust_ident(value)}),
% endfor
_ => None,
}
})
}
</%self:single_component_value>
</%def>
<%def name="single_keyword(name, values)">
<%self:single_keyword_computed name="${name}"
values="${values}">
// The computed value is the same as the specified value.
pub use to_computed_value = super::computed_as_specified;
</%self:single_keyword_computed>
</%def>
<%def name="predefined_type(name, type, initial_value, parse_method='parse')">
<%self:single_component_value name="${name}">
pub use to_computed_value = super::super::common_types::computed::compute_${type};
pub type SpecifiedValue = specified::${type};
pub mod computed_value {
pub type T = super::super::computed::${type};
}
#[inline] pub fn get_initial_value() -> computed_value::T { ${initial_value} }
#[inline] pub fn from_component_value(v: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
specified::${type}::${parse_method}(v)
}
</%self:single_component_value>
</%def>
// CSS 2.1, Section 8 - Box model
${new_style_struct("Margin", is_inherited=False)}
% for side in ["top", "right", "bottom", "left"]:
${predefined_type("margin-" + side, "LengthOrPercentageOrAuto",
"computed::LPA_Length(Au(0))")}
% endfor
${new_style_struct("Padding", is_inherited=False)}
% for side in ["top", "right", "bottom", "left"]:
${predefined_type("padding-" + side, "LengthOrPercentage",
"computed::LP_Length(Au(0))",
"parse_non_negative")}
% endfor
${new_style_struct("Border", is_inherited=False)}
% for side in ["top", "right", "bottom", "left"]:
${predefined_type("border-%s-color" % side, "CSSColor", "CurrentColor")}
% endfor
${single_keyword("border-top-style", values="none solid double dotted dashed hidden groove ridge inset outset")}
% for side in ["right", "bottom", "left"]:
<%self:longhand name="border-${side}-style", no_super="True">
pub use super::border_top_style::{get_initial_value, parse, to_computed_value};
pub type SpecifiedValue = super::border_top_style::SpecifiedValue;
pub mod computed_value {
pub type T = super::super::border_top_style::computed_value::T;
}
</%self:longhand>
% endfor
pub fn parse_border_width(component_value: &ComponentValue, _base_url: &Url)
-> Option<specified::Length> {
match component_value {
&Ident(ref value) => {
match value.to_owned().to_ascii_lower().as_slice() {
"thin" => Some(specified::Length::from_px(1.)),
"medium" => Some(specified::Length::from_px(3.)),
"thick" => Some(specified::Length::from_px(5.)),
_ => None
}
},
_ => specified::Length::parse_non_negative(component_value)
}
}
% for side in ["top", "right", "bottom", "left"]:
<%self:longhand name="border-${side}-width">
pub type SpecifiedValue = specified::Length;
pub mod computed_value {
use super::super::Au;
pub type T = Au;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
Au::from_px(3) // medium
}
pub fn parse(input: &[ComponentValue], base_url: &Url) -> Option<SpecifiedValue> {
one_component_value(input).and_then(|c| parse_border_width(c, base_url))
}
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
if !context.border_${side}_present {
Au(0)
} else {
computed::compute_Au(value, context)
}
}
</%self:longhand>
% endfor
${new_style_struct("PositionOffsets", is_inherited=False)}
% for side in ["top", "right", "bottom", "left"]:
${predefined_type(side, "LengthOrPercentageOrAuto",
"computed::LPA_Auto")}
% endfor
// CSS 2.1, Section 9 - Visual formatting model
${new_style_struct("Box", is_inherited=False)}
// TODO: don't parse values we don't support
<%self:single_keyword_computed name="display"
values="inline block inline-block
table inline-table table-row-group table-header-group table-footer-group
table-row table-column-group table-column table-cell table-caption
list-item
none">
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
// if context.is_root_element && value == list_item {
// return block
// }
if context.positioned || context.floated || context.is_root_element {
match value {
inline_table => table,
inline | inline_block
| table_row_group | table_column | table_column_group
| table_header_group | table_footer_group | table_row
| table_cell | table_caption
=> block,
_ => value,
}
} else {
value
}
}
</%self:single_keyword_computed>
${single_keyword("position", "static absolute relative fixed")}
${single_keyword("float", "none left right")}
${single_keyword("clear", "none left right both")}
// CSS 2.1, Section 10 - Visual formatting model details
${predefined_type("width", "LengthOrPercentageOrAuto",
"computed::LPA_Auto",
"parse_non_negative")}
<%self:single_component_value name="height">
pub type SpecifiedValue = specified::LengthOrPercentageOrAuto;
pub mod computed_value {
pub type T = super::super::computed::LengthOrPercentageOrAuto;
}
#[inline]
pub fn get_initial_value() -> computed_value::T { computed::LPA_Auto }
#[inline]
pub fn from_component_value(v: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
specified::LengthOrPercentageOrAuto::parse_non_negative(v)
}
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
match (value, context.inherited_height) {
(specified::LPA_Percentage(_), computed::LPA_Auto)
if !context.is_root_element && !context.positioned => {
computed::LPA_Auto
},
_ => computed::compute_LengthOrPercentageOrAuto(value, context)
}
}
</%self:single_component_value>
${predefined_type("min-width", "LengthOrPercentage",
"computed::LP_Length(Au(0))",
"parse_non_negative")}
${predefined_type("max-width", "LengthOrPercentageOrNone",
"computed::LPN_None",
"parse_non_negative")}
${predefined_type("min-height", "LengthOrPercentage",
"computed::LP_Length(Au(0))",
"parse_non_negative")}
${predefined_type("max-height", "LengthOrPercentageOrNone",
"computed::LPN_None",
"parse_non_negative")}
${new_style_struct("InheritedBox", is_inherited=True)}
<%self:single_component_value name="line-height">
#[deriving(Clone)]
pub enum SpecifiedValue {
SpecifiedNormal,
SpecifiedLength(specified::Length),
SpecifiedNumber(CSSFloat),
// percentage are the same as em.
}
/// normal | <number> | <length> | <percentage>
pub fn from_component_value(input: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
match input {
&ast::Number(ref value) if value.value >= 0.
=> Some(SpecifiedNumber(value.value)),
&ast::Percentage(ref value) if value.value >= 0.
=> Some(SpecifiedLength(specified::Em(value.value / 100.))),
&Dimension(ref value, ref unit) if value.value >= 0.
=> specified::Length::parse_dimension(value.value, unit.as_slice())
.map(SpecifiedLength),
&Ident(ref value) if value.to_owned().eq_ignore_ascii_case("normal")
=> Some(SpecifiedNormal),
_ => None,
}
}
pub mod computed_value {
use super::super::{Au, CSSFloat};
#[deriving(Eq, Clone)]
pub enum T {
Normal,
Length(Au),
Number(CSSFloat),
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T { Normal }
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
match value {
SpecifiedNormal => Normal,
SpecifiedLength(value) => Length(computed::compute_Au(value, context)),
SpecifiedNumber(value) => Number(value),
}
}
</%self:single_component_value>
<%self:longhand name="-servo-minimum-line-height" derived_from="line-height">
use super::Au;
use super::super::common_types::DEFAULT_LINE_HEIGHT;
use super::super::longhands::display;
use super::super::longhands::line_height;
pub use to_computed_value = super::computed_as_specified;
pub type SpecifiedValue = line_height::SpecifiedValue;
pub mod computed_value {
pub type T = super::super::Au;
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
Au(0)
}
#[inline]
pub fn derive_from_line_height(value: line_height::computed_value::T,
context: &computed::Context)
-> Au {
if context.display != display::computed_value::inline {
match value {
line_height::Normal => context.font_size.scale_by(DEFAULT_LINE_HEIGHT),
line_height::Number(percentage) => context.font_size.scale_by(percentage),
line_height::Length(length) => length,
}
} else {
context.inherited_minimum_line_height
}
}
</%self:longhand>
${switch_to_style_struct("Box")}
<%self:single_component_value name="vertical-align">
<% vertical_align_keywords = (
"baseline sub super top text-top middle bottom text-bottom".split()) %>
#[deriving(Clone)]
pub enum SpecifiedValue {
% for keyword in vertical_align_keywords:
Specified_${to_rust_ident(keyword)},
% endfor
SpecifiedLengthOrPercentage(specified::LengthOrPercentage),
}
/// baseline | sub | super | top | text-top | middle | bottom | text-bottom
/// | <percentage> | <length>
pub fn from_component_value(input: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
match input {
&Ident(ref value) => {
match value.to_owned().to_ascii_lower().as_slice() {
% for keyword in vertical_align_keywords:
"${keyword}" => Some(Specified_${to_rust_ident(keyword)}),
% endfor
_ => None,
}
},
_ => specified::LengthOrPercentage::parse_non_negative(input)
.map(SpecifiedLengthOrPercentage)
}
}
pub mod computed_value {
use super::super::{Au, CSSFloat};
#[deriving(Eq, Clone)]
pub enum T {
% for keyword in vertical_align_keywords:
${to_rust_ident(keyword)},
% endfor
Length(Au),
Percentage(CSSFloat),
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T { baseline }
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
match value {
% for keyword in vertical_align_keywords:
Specified_${to_rust_ident(keyword)} => ${to_rust_ident(keyword)},
% endfor
SpecifiedLengthOrPercentage(value)
=> match computed::compute_LengthOrPercentage(value, context) {
computed::LP_Length(value) => Length(value),
computed::LP_Percentage(value) => Percentage(value)
}
}
}
</%self:single_component_value>
// CSS 2.1, Section 11 - Visual effects
${single_keyword("overflow", "visible hidden")} // TODO: scroll auto
${switch_to_style_struct("InheritedBox")}
// TODO: collapse. Well, do tables first.
${single_keyword("visibility", "visible hidden")}
// CSS 2.1, Section 12 - Generated content, automatic numbering, and lists
${switch_to_style_struct("Box")}
<%self:longhand name="content">
pub use to_computed_value = super::computed_as_specified;
pub mod computed_value {
#[deriving(Eq, Clone)]
pub enum Content {
StringContent(~str),
}
#[deriving(Eq, Clone)]
pub enum T {
normal,
none,
Content(Vec<Content>),
}
}
pub type SpecifiedValue = computed_value::T;
#[inline] pub fn get_initial_value() -> computed_value::T { normal }
// normal | none | [ <string> ]+
// TODO: <uri>, <counter>, attr(<identifier>), open-quote, close-quote, no-open-quote, no-close-quote
pub fn parse(input: &[ComponentValue], _base_url: &Url) -> Option<SpecifiedValue> {
match one_component_value(input) {
Some(&Ident(ref keyword)) => {
match keyword.to_owned().to_ascii_lower().as_slice() {
"normal" => return Some(normal),
"none" => return Some(none),
_ => ()
}
},
_ => ()
}
let mut content = vec!();
for component_value in input.skip_whitespace() {
match component_value {
&String(ref value)
=> content.push(StringContent(value.to_owned())),
_ => return None // invalid/unsupported value
}
}
Some(Content(content))
}
</%self:longhand>
// CSS 2.1, Section 13 - Paged media
// CSS 2.1, Section 14 - Colors and Backgrounds
${new_style_struct("Background", is_inherited=False)}
${predefined_type("background-color", "CSSColor",
"RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. }) /* transparent */")}
<%self:single_component_value name="background-image">
// The computed value is the same as the specified value.
pub use to_computed_value = super::computed_as_specified;
pub mod computed_value {
pub use url::Url;
pub type T = Option<Url>;
}
pub type SpecifiedValue = computed_value::T;
#[inline] pub fn get_initial_value() -> SpecifiedValue {
None
}
pub fn from_component_value(component_value: &ComponentValue, base_url: &Url) -> Option<SpecifiedValue> {
match component_value {
&ast::URL(ref url) => {
let image_url = parse_url(url.as_slice(), Some(base_url.clone()));
Some(Some(image_url))
},
&ast::Ident(ref value) if value.to_owned().eq_ignore_ascii_case("none") => Some(None),
_ => None,
}
}
</%self:single_component_value>
<%self:longhand name="background-position">
use super::super::common_types::specified;
pub mod computed_value {
use super::super::super::common_types::computed::LengthOrPercentage;
#[deriving(Eq, Clone)]
pub struct T {
pub horizontal: LengthOrPercentage,
pub vertical: LengthOrPercentage,
}
}
#[deriving(Clone)]
pub struct SpecifiedValue {
pub horizontal: specified::LengthOrPercentage,
pub vertical: specified::LengthOrPercentage,
}
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
computed_value::T {
horizontal: computed::compute_LengthOrPercentage(value.horizontal, context),
vertical: computed::compute_LengthOrPercentage(value.vertical, context),
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T {
horizontal: computed::LP_Percentage(0.0),
vertical: computed::LP_Percentage(0.0),
}
}
// FIXME(#1997, pcwalton): Support complete CSS2 syntax.
pub fn parse_horizontal_and_vertical(horiz: &ComponentValue, vert: &ComponentValue)
-> Option<SpecifiedValue> {
let horiz = match specified::LengthOrPercentage::parse_non_negative(horiz) {
None => return None,
Some(value) => value,
};
let vert = match specified::LengthOrPercentage::parse_non_negative(vert) {
None => return None,
Some(value) => value,
};
Some(SpecifiedValue {
horizontal: horiz,
vertical: vert,
})
}
pub fn parse(input: &[ComponentValue], _: &Url) -> Option<SpecifiedValue> {
let mut input_iter = input.skip_whitespace();
let horizontal = input_iter.next();
let vertical = input_iter.next();
if input_iter.next().is_some() {
return None
}
match (horizontal, vertical) {
(Some(horizontal), Some(vertical)) => {
parse_horizontal_and_vertical(horizontal, vertical)
}
_ => None
}
}
</%self:longhand>
${single_keyword("background-repeat", "repeat repeat-x repeat-y no-repeat")}
${single_keyword("background-attachment", "scroll fixed")}
${new_style_struct("Color", is_inherited=True)}
<%self:raw_longhand name="color">
pub use to_computed_value = super::computed_as_specified;
pub type SpecifiedValue = RGBA;
pub mod computed_value {
pub type T = super::SpecifiedValue;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
RGBA { red: 0., green: 0., blue: 0., alpha: 1. } /* black */
}
pub fn parse_specified(input: &[ComponentValue], _base_url: &Url)
-> Option<DeclaredValue<SpecifiedValue>> {
match one_component_value(input).and_then(Color::parse) {
Some(RGBA(rgba)) => Some(SpecifiedValue(rgba)),
Some(CurrentColor) => Some(CSSWideKeyword(Inherit)),
None => None,
}
}
</%self:raw_longhand>
// CSS 2.1, Section 15 - Fonts
${new_style_struct("Font", is_inherited=True)}
<%self:longhand name="font-family">
pub use to_computed_value = super::computed_as_specified;
pub mod computed_value {
#[deriving(Eq, Clone)]
pub enum FontFamily {
FamilyName(~str),
// Generic
// Serif,
// SansSerif,
// Cursive,
// Fantasy,
// Monospace,
}
pub type T = Vec<FontFamily>;
}
pub type SpecifiedValue = computed_value::T;
#[inline] pub fn get_initial_value() -> computed_value::T { vec!(FamilyName("serif".to_owned())) }
/// <familiy-name>#
/// <familiy-name> = <string> | [ <ident>+ ]
/// TODO: <generic-familiy>
pub fn parse(input: &[ComponentValue], _base_url: &Url) -> Option<SpecifiedValue> {
from_iter(input.skip_whitespace())
}
pub fn from_iter<'a>(mut iter: SkipWhitespaceIterator<'a>) -> Option<SpecifiedValue> {
let mut result = vec!();
macro_rules! add(
($value: expr, $b: expr) => {
{
result.push($value);
match iter.next() {
Some(&Comma) => (),
None => $b,
_ => return None,
}
}
}
)
'outer: loop {
match iter.next() {
// TODO: avoid copying strings?
Some(&String(ref value)) => add!(FamilyName(value.to_owned()), break 'outer),
Some(&Ident(ref value)) => {
match value.to_owned().to_ascii_lower().as_slice() {
// "serif" => add!(Serif, break 'outer),
// "sans-serif" => add!(SansSerif, break 'outer),
// "cursive" => add!(Cursive, break 'outer),
// "fantasy" => add!(Fantasy, break 'outer),
// "monospace" => add!(Monospace, break 'outer),
_ => {
let mut idents = vec!(value.as_slice());
loop {
match iter.next() {
Some(&Ident(ref value)) => idents.push(value.as_slice()),
Some(&Comma) => {
result.push(FamilyName(idents.connect(" ")));
break
},
None => {
result.push(FamilyName(idents.connect(" ")));
break 'outer
},
_ => return None,
}
}
}
}
}
_ => return None,
}
}
Some(result)
}
</%self:longhand>
${single_keyword("font-style", "normal italic oblique")}
${single_keyword("font-variant", "normal")} // Add small-caps when supported
<%self:single_component_value name="font-weight">
#[deriving(Clone)]
pub enum SpecifiedValue {
Bolder,
Lighter,
% for weight in range(100, 901, 100):
SpecifiedWeight${weight},
% endfor
}
/// normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
pub fn from_component_value(input: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
match input {
&Ident(ref value) => {
match value.to_owned().to_ascii_lower().as_slice() {
"bold" => Some(SpecifiedWeight700),
"normal" => Some(SpecifiedWeight400),
"bolder" => Some(Bolder),
"lighter" => Some(Lighter),
_ => None,
}
},
&Number(ref value) => match value.int_value {
Some(100) => Some(SpecifiedWeight100),
Some(200) => Some(SpecifiedWeight200),
Some(300) => Some(SpecifiedWeight300),
Some(400) => Some(SpecifiedWeight400),
Some(500) => Some(SpecifiedWeight500),
Some(600) => Some(SpecifiedWeight600),
Some(700) => Some(SpecifiedWeight700),
Some(800) => Some(SpecifiedWeight800),
Some(900) => Some(SpecifiedWeight900),
_ => None,
},
_ => None
}
}
pub mod computed_value {
#[deriving(Eq, Clone)]
pub enum T {
% for weight in range(100, 901, 100):
Weight${weight},
% endfor
}
impl T {
pub fn is_bold(self) -> bool {
match self {
Weight900 | Weight800 | Weight700 | Weight600 => true,
_ => false
}
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T { Weight400 } // normal
#[inline]
pub fn to_computed_value(value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
match value {
% for weight in range(100, 901, 100):
SpecifiedWeight${weight} => Weight${weight},
% endfor
Bolder => match context.inherited_font_weight {
Weight100 => Weight400,
Weight200 => Weight400,
Weight300 => Weight400,
Weight400 => Weight700,
Weight500 => Weight700,
Weight600 => Weight900,
Weight700 => Weight900,
Weight800 => Weight900,
Weight900 => Weight900,
},
Lighter => match context.inherited_font_weight {
Weight100 => Weight100,
Weight200 => Weight100,
Weight300 => Weight100,
Weight400 => Weight100,
Weight500 => Weight100,
Weight600 => Weight400,
Weight700 => Weight400,
Weight800 => Weight700,
Weight900 => Weight700,
},
}
}
</%self:single_component_value>
<%self:single_component_value name="font-size">
pub type SpecifiedValue = specified::Length; // Percentages are the same as em.
pub mod computed_value {
use super::super::Au;
pub type T = Au;
}
#[inline] pub fn get_initial_value() -> computed_value::T {
Au::from_px(16) // medium
}
#[inline]
pub fn to_computed_value(_value: SpecifiedValue, context: &computed::Context)
-> computed_value::T {
// We already computed this element's font size; no need to compute it again.
return context.font_size
}
/// <length> | <percentage>
/// TODO: support <absolute-size> and <relative-size>
pub fn from_component_value(input: &ComponentValue, _base_url: &Url)
-> Option<SpecifiedValue> {
specified::LengthOrPercentage::parse_non_negative(input).map(|value| {
match value {
specified::LP_Length(value) => value,
specified::LP_Percentage(value) => specified::Em(value),
}
})
}
</%self:single_component_value>
// CSS 2.1, Section 16 - Text
${new_style_struct("InheritedText", is_inherited=True)}
// TODO: initial value should be 'start' (CSS Text Level 3, direction-dependent.)
${single_keyword("text-align", "left right center justify")}
${new_style_struct("Text", is_inherited=False)}
<%self:longhand name="text-decoration">
pub use to_computed_value = super::computed_as_specified;
#[deriving(Eq, Clone)]
pub struct SpecifiedValue {
pub underline: bool,
pub overline: bool,
pub line_through: bool,
// 'blink' is accepted in the parser but ignored.
// Just not blinking the text is a conforming implementation per CSS 2.1.
}
pub mod computed_value {
pub type T = super::SpecifiedValue;
pub static none: T = super::SpecifiedValue { underline: false, overline: false, line_through: false };
}
#[inline] pub fn get_initial_value() -> computed_value::T {
none
}
/// none | [ underline || overline || line-through || blink ]
pub fn parse(input: &[ComponentValue], _base_url: &Url) -> Option<SpecifiedValue> {
let mut result = SpecifiedValue {
underline: false, overline: false, line_through: false,
};
let mut blink = false;
let mut empty = true;
for component_value in input.skip_whitespace() {
match get_ident_lower(component_value) {
None => return None,
Some(keyword) => match keyword.as_slice() {
"underline" => if result.underline { return None }
else { empty = false; result.underline = true },
"overline" => if result.overline { return None }
else { empty = false; result.overline = true },
"line-through" => if result.line_through { return None }
else { empty = false; result.line_through = true },
"blink" => if blink { return None }
else { empty = false; blink = true },
"none" => return if empty { Some(result) } else { None },
_ => return None,
}
}
}
if !empty { Some(result) } else { None }
}
</%self:longhand>
${switch_to_style_struct("InheritedText")}
<%self:longhand name="-servo-text-decorations-in-effect"
derived_from="display text-decoration">
use super::RGBA;
use super::super::longhands::display;
pub use to_computed_value = super::computed_as_specified;
#[deriving(Clone, Eq)]
pub struct SpecifiedValue {
pub underline: Option<RGBA>,
pub overline: Option<RGBA>,
pub line_through: Option<RGBA>,
}
pub mod computed_value {
pub type T = super::SpecifiedValue;
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
SpecifiedValue {
underline: None,
overline: None,
line_through: None,
}
}
fn maybe(flag: bool, context: &computed::Context) -> Option<RGBA> {
if flag {
Some(context.color)
} else {
None
}
}
fn derive(context: &computed::Context) -> computed_value::T {
// Start with no declarations if this is a block; otherwise, start with the
// declarations in effect and add in the text decorations that this inline specifies.
let mut result = match context.display {
display::computed_value::inline => context.inherited_text_decorations_in_effect,
_ => {
SpecifiedValue {
underline: None,
overline: None,
line_through: None,
}
}
};
if result.underline.is_none() {
result.underline = maybe(context.text_decoration.underline, context)
}
if result.overline.is_none() {
result.overline = maybe(context.text_decoration.overline, context)
}
if result.line_through.is_none() {
result.line_through = maybe(context.text_decoration.line_through, context)
}
result
}