aboutsummaryrefslogtreecommitdiffstats
path: root/components/style/legacy.rs
blob: d1451ae6bc4d5c609df266661572d805d54e3ecc (plain) (blame)
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
/* 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/. */

//! Legacy presentational attributes defined in the HTML5 specification: `<td width>`,
//! `<input size>`, and so forth.

use std::sync::Arc;

use selectors::tree::{TElement, TNode};
use selectors::matching::DeclarationBlock;
use node::TElementAttributes;
use values::specified::CSSColor;
use values::{CSSFloat, specified};
use properties::DeclaredValue::SpecifiedValue;
use properties::PropertyDeclaration;
use properties::longhands;
use selector_matching::Stylist;

use cssparser::Color;
use selectors::smallvec::VecLike;
use util::geometry::Au;
use util::str::LengthOrPercentageOrAuto;

/// Legacy presentational attributes that take a length as defined in HTML5 § 2.4.4.4.
#[derive(Copy, PartialEq, Eq)]
pub enum LengthAttribute {
    /// `<td width>`
    Width,
}

/// Legacy presentational attributes that take an integer as defined in HTML5 § 2.4.4.2.
#[derive(Copy, PartialEq, Eq)]
pub enum IntegerAttribute {
    /// `<input size>`
    Size,
    Cols,
    Rows,
}

/// Legacy presentational attributes that take a nonnegative integer as defined in HTML5 § 2.4.4.2.
#[derive(Copy, PartialEq, Eq)]
pub enum UnsignedIntegerAttribute {
    /// `<td border>`
    Border,
    /// `<td colspan>`
    ColSpan,
}

/// Legacy presentational attributes that take a simple color as defined in HTML5 § 2.4.6.
#[derive(Copy, PartialEq, Eq)]
pub enum SimpleColorAttribute {
    /// `<body bgcolor>`
    BgColor,
}

/// Extension methods for `Stylist` that cause rules to be synthesized for legacy attributes.
pub trait PresentationalHintSynthesis {
    /// Synthesizes rules from various HTML attributes (mostly legacy junk from HTML4) that confer
    /// *presentational hints* as defined in the HTML5 specification. This handles stuff like
    /// `<body bgcolor>`, `<input size>`, `<td width>`, and so forth.
    ///
    /// NB: Beware! If you add an attribute to this list, be sure to add it to
    /// `common_style_affecting_attributes` or `rare_style_affecting_attributes` as appropriate. If
    /// you don't, you risk strange random nondeterministic failures due to false positives in
    /// style sharing.
    fn synthesize_presentational_hints_for_legacy_attributes<'a,E,N,V>(
                                                             &self,
                                                             node: &N,
                                                             matching_rules_list: &mut V,
                                                             shareable: &mut bool)
                                                             where E: TElement<'a> +
                                                                      TElementAttributes,
                                                                   N: TNode<'a,E>,
                                                                   V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>>;
    /// Synthesizes rules for the legacy `bgcolor` attribute.
    fn synthesize_presentational_hint_for_legacy_background_color_attribute<'a,E,V>(
                                                                            &self,
                                                                            element: E,
                                                                            matching_rules_list:
                                                                                &mut V,
                                                                            shareable: &mut bool)
                                                                            where
                                                                            E: TElement<'a> +
                                                                               TElementAttributes,
                                                                            V: VecLike<
                                                                                DeclarationBlock<Vec<PropertyDeclaration>>>;
    /// Synthesizes rules for the legacy `border` attribute.
    fn synthesize_presentational_hint_for_legacy_border_attribute<'a,E,V>(
                                                                  &self,
                                                                  element: E,
                                                                  matching_rules_list: &mut V,
                                                                  shareable: &mut bool)
                                                                  where
                                                                    E: TElement<'a> +
                                                                       TElementAttributes,
                                                                    V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>>;
}

impl PresentationalHintSynthesis for Stylist {
    fn synthesize_presentational_hints_for_legacy_attributes<'a,E,N,V>(
                                                             &self,
                                                             node: &N,
                                                             matching_rules_list: &mut V,
                                                             shareable: &mut bool)
                                                             where E: TElement<'a> +
                                                                      TElementAttributes,
                                                                   N: TNode<'a,E>,
                                                                   V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>> {
        let element = node.as_element();
        match element.get_local_name() {
            name if *name == atom!("td") => {
                match element.get_length_attribute(LengthAttribute::Width) {
                    LengthOrPercentageOrAuto::Auto => {}
                    LengthOrPercentageOrAuto::Percentage(percentage) => {
                        let width_value = specified::LengthOrPercentageOrAuto::Percentage(percentage);
                        matching_rules_list.vec_push(from_declaration(
                                PropertyDeclaration::Width(SpecifiedValue(width_value))));
                        *shareable = false
                    }
                    LengthOrPercentageOrAuto::Length(length) => {
                        let width_value = specified::LengthOrPercentageOrAuto::Length(specified::Length::Absolute(length));
                        matching_rules_list.vec_push(from_declaration(
                                PropertyDeclaration::Width(SpecifiedValue(width_value))));
                        *shareable = false
                    }
                }
                self.synthesize_presentational_hint_for_legacy_background_color_attribute(
                    element,
                    matching_rules_list,
                    shareable);
                self.synthesize_presentational_hint_for_legacy_border_attribute(
                    element,
                    matching_rules_list,
                    shareable);
            }
            name if *name == atom!("table") => {
                self.synthesize_presentational_hint_for_legacy_background_color_attribute(
                    element,
                    matching_rules_list,
                    shareable);
                self.synthesize_presentational_hint_for_legacy_border_attribute(
                    element,
                    matching_rules_list,
                    shareable);
            }
            name if *name == atom!("body") || *name == atom!("tr") || *name == atom!("thead") ||
                    *name == atom!("tbody") || *name == atom!("tfoot") => {
                self.synthesize_presentational_hint_for_legacy_background_color_attribute(
                    element,
                    matching_rules_list,
                    shareable);
            }
            name if *name == atom!("input") => {
                match element.get_integer_attribute(IntegerAttribute::Size) {
                    Some(value) if value != 0 => {
                        // Per HTML 4.01 § 17.4, this value is in characters if `type` is `text` or
                        // `password` and in pixels otherwise.
                        //
                        // FIXME(pcwalton): More use of atoms, please!
                        let value = match element.get_attr(&ns!(""), &atom!("type")) {
                            Some("text") | Some("password") => {
                                specified::Length::ServoCharacterWidth(specified::CharacterWidth(value))
                            }
                            _ => specified::Length::Absolute(Au::from_px(value as int)),
                        };
                        matching_rules_list.vec_push(from_declaration(
                                PropertyDeclaration::Width(SpecifiedValue(
                                    specified::LengthOrPercentageOrAuto::Length(value)))));
                        *shareable = false
                    }
                    Some(_) | None => {}
                }
            }
            name if *name == atom!("textarea") => {
                match element.get_integer_attribute(IntegerAttribute::Cols) {
                    Some(value) if value != 0 => {
                        // TODO(mttr) ServoCharacterWidth uses the size math for <input type="text">, but
                        // the math for <textarea> is a little different since we need to take
                        // scrollbar size into consideration (but we don't have a scrollbar yet!)
                        //
                        // https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-width
                        let value = specified::Length::ServoCharacterWidth(specified::CharacterWidth(value));
                        matching_rules_list.vec_push(from_declaration(
                                PropertyDeclaration::Width(SpecifiedValue(
                                    specified::LengthOrPercentageOrAuto::Length(value)))));
                        *shareable = false
                    }
                    Some(_) | None => {}
                }
                match element.get_integer_attribute(IntegerAttribute::Rows) {
                    Some(value) if value != 0 => {
                        // TODO(mttr) This should take scrollbar size into consideration.
                        //
                        // https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-height
                        let value = specified::Length::FontRelative(specified::FontRelativeLength::Em(value as CSSFloat));
                        matching_rules_list.vec_push(from_declaration(
                                PropertyDeclaration::Height(SpecifiedValue(
                                    longhands::height::SpecifiedValue(
                                        specified::LengthOrPercentageOrAuto::Length(value))))));
                        *shareable = false
                    }
                    Some(_) | None => {}
                }
            }
            _ => {}
        }
    }

    fn synthesize_presentational_hint_for_legacy_background_color_attribute<'a,E,V>(
                                                                            &self,
                                                                            element: E,
                                                                            matching_rules_list:
                                                                                &mut V,
                                                                            shareable: &mut bool)
                                                                            where
                                                                            E: TElement<'a> +
                                                                               TElementAttributes,
                                                                            V: VecLike<
                                                                                DeclarationBlock<Vec<PropertyDeclaration>>> {
        match element.get_simple_color_attribute(SimpleColorAttribute::BgColor) {
            None => {}
            Some(color) => {
                matching_rules_list.vec_push(from_declaration(
                        PropertyDeclaration::BackgroundColor(SpecifiedValue(
                            CSSColor { parsed: Color::RGBA(color), authored: None }))));
                *shareable = false
            }
        }
    }

    fn synthesize_presentational_hint_for_legacy_border_attribute<'a,E,V>(
                                                                  &self,
                                                                  element: E,
                                                                  matching_rules_list: &mut V,
                                                                  shareable: &mut bool)
                                                                  where
                                                                    E: TElement<'a> +
                                                                       TElementAttributes,
                                                                    V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>> {
        match element.get_unsigned_integer_attribute(UnsignedIntegerAttribute::Border) {
            None => {}
            Some(length) => {
                let width_value = specified::Length::Absolute(Au::from_px(length as int));
                matching_rules_list.vec_push(from_declaration(
                        PropertyDeclaration::BorderTopWidth(SpecifiedValue(
                            longhands::border_top_width::SpecifiedValue(width_value)))));
                matching_rules_list.vec_push(from_declaration(
                        PropertyDeclaration::BorderLeftWidth(SpecifiedValue(
                            longhands::border_left_width::SpecifiedValue(width_value)))));
                matching_rules_list.vec_push(from_declaration(
                        PropertyDeclaration::BorderBottomWidth(SpecifiedValue(
                            longhands::border_bottom_width::SpecifiedValue(width_value)))));
                matching_rules_list.vec_push(from_declaration(
                        PropertyDeclaration::BorderRightWidth(SpecifiedValue(
                            longhands::border_right_width::SpecifiedValue(width_value)))));
                *shareable = false
            }
        }
    }
}


/// A convenience function to create a declaration block from a single declaration. This is
/// primarily used in `synthesize_rules_for_legacy_attributes`.
#[inline]
pub fn from_declaration(rule: PropertyDeclaration) -> DeclarationBlock<Vec<PropertyDeclaration>> {
    DeclarationBlock::from_declarations(Arc::new(vec![rule]))
}