float-pigment-css 0.8.2

The CSS parser for the float-pigment project.
Documentation
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
use alloc::borrow::Cow;
use core::fmt;

use super::*;
use crate::property::Property;

/// A CSS property with some metadata.
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyMeta {
    /// A single normal property, e.g. `font-size: 16px`.
    Normal {
        /// The property body.
        property: Property,
    },
    /// A single property with `!important`, e.g. `font-size: 16px !important`.
    Important {
        /// The property body.
        property: Property,
    },
    /// A group of properties.
    ///
    /// It is designed for debugging only.
    /// In production environment, properties are well-normalized -
    /// shorthand properties (e.g. `font` `background`) are splitted in advance.
    /// However, we may add new shorthand properties in debugger -
    /// we can keep the shorthand properties as-is with `DebugGroup`s.
    DebugGroup {
        /// The original name-value string pair.
        original_name_value: Box<(String, String)>,
        /// The parsed property list.
        properties: Box<[Property]>,
        /// `!important` or not.
        important: bool,
        /// Disabled or not.
        disabled: bool,
    },
}

impl PropertyMeta {
    /// Generate a new property.
    ///
    /// Note that the property is in *debug* mode so that:
    /// * it cannot be serialized even if it has been inserted to a rule;
    /// * it has a little performance penalty.
    pub fn new_debug_properties(source: &str) -> Vec<Self> {
        parser::parse_inline_style(source, parser::StyleParsingDebugMode::Debug).0
    }

    /// Clone the property and set the disable state of the new property.
    ///
    /// Note that the new property is in *debug* mode so that:
    /// * it cannot be serialized even if it has been inserted to a rule;
    /// * it has a little performance penalty.
    pub fn to_debug_state(&self, disabled: bool) -> Self {
        match self {
            Self::Normal { property } => Self::DebugGroup {
                original_name_value: Box::new((
                    self.get_property_name().into(),
                    self.get_property_value_string(),
                )),
                properties: Box::new([property.clone()]),
                important: false,
                disabled,
            },
            Self::Important { property } => Self::DebugGroup {
                original_name_value: Box::new((
                    self.get_property_name().into(),
                    self.get_property_value_string(),
                )),
                properties: Box::new([property.clone()]),
                important: false,
                disabled,
            },
            Self::DebugGroup {
                original_name_value,
                properties,
                important,
                ..
            } => Self::DebugGroup {
                original_name_value: original_name_value.clone(),
                properties: properties.clone(),
                important: *important,
                disabled,
            },
        }
    }

    /// The property is `!important` or not.
    pub fn is_important(&self) -> bool {
        match self {
            Self::Normal { .. } => false,
            Self::Important { .. } => true,
            Self::DebugGroup { important, .. } => *important,
        }
    }

    /// Get the property name.
    pub fn get_property_name(&self) -> Cow<'static, str> {
        match self {
            Self::Normal { property } => property.get_property_name().into(),
            Self::Important { property } => property.get_property_name().into(),
            Self::DebugGroup {
                original_name_value,
                ..
            } => original_name_value.0.clone().into(),
        }
    }

    /// Get the property value as a string.
    ///
    /// Note that it may (and may not) be normalized.
    pub fn get_property_value_string(&self) -> String {
        match self {
            Self::Normal { property } => property.get_property_value_string(),
            Self::Important { property } => {
                let mut v = property.get_property_value_string();
                v.push_str(" !important");
                v
            }
            Self::DebugGroup {
                original_name_value,
                ..
            } => original_name_value.1.clone(),
        }
    }

    /// The property is disabled (only possible in debug state) or not.
    pub fn is_disabled(&self) -> bool {
        match self {
            Self::Normal { .. } => false,
            Self::Important { .. } => false,
            Self::DebugGroup { disabled, .. } => *disabled,
        }
    }

    /// The property is invalid (only possible in debug state) or not.
    pub fn is_invalid(&self) -> bool {
        match self {
            Self::Normal { .. } => false,
            Self::Important { .. } => false,
            Self::DebugGroup { properties, .. } => properties.is_empty(),
        }
    }

    /// The property is deprecated or not.
    pub fn is_deprecated(&self) -> bool {
        match self {
            Self::Normal { property, .. } | Self::Important { property, .. } => {
                property.is_deprecated()
            }
            Self::DebugGroup { .. } => false,
        }
    }

    /// Merge the property into a `NodeProperties`.
    pub fn merge_to_node_properties(
        &self,
        node_properties: &mut NodeProperties,
        parent_node_properties: Option<&NodeProperties>,
        current_font_size: f32,
    ) {
        match self {
            PropertyMeta::Normal { property: p } => {
                node_properties.merge_property(p, parent_node_properties, current_font_size)
            }
            PropertyMeta::Important { property: p } => {
                node_properties.merge_property(p, parent_node_properties, current_font_size)
            }
            PropertyMeta::DebugGroup {
                properties,
                disabled,
                ..
            } => {
                if !disabled {
                    for p in &**properties {
                        node_properties.merge_property(p, parent_node_properties, current_font_size)
                    }
                }
            }
        }
    }

    /// Get an iterate of the properties.
    pub fn iter(&self) -> PropertyMetaIter<'_> {
        PropertyMetaIter { pm: self, cur: 0 }
    }

    #[cfg(test)]
    #[doc(hidden)]
    pub fn property(&self) -> Option<Property> {
        match self {
            Self::Normal { property } | Self::Important { property } => Some(property.clone()),
            Self::DebugGroup { .. } => None,
        }
    }
}

impl fmt::Display for PropertyMeta {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        if self.is_disabled() {
            write!(
                f,
                "/* {}: {}; */",
                self.get_property_name(),
                self.get_property_value_string(),
            )
        } else {
            write!(
                f,
                "{}: {};",
                self.get_property_name(),
                self.get_property_value_string(),
            )
        }
    }
}

/// The iterator for `PropertyMeta` .
pub struct PropertyMetaIter<'a> {
    pm: &'a PropertyMeta,
    cur: usize,
}

impl<'a> Iterator for PropertyMetaIter<'a> {
    type Item = &'a Property;

    fn next(&mut self) -> Option<Self::Item> {
        match self.pm {
            PropertyMeta::Normal { property } | PropertyMeta::Important { property } => {
                if self.cur == 0 {
                    self.cur = 1;
                    Some(property)
                } else {
                    None
                }
            }
            PropertyMeta::DebugGroup { properties, .. } => {
                if self.cur < properties.len() {
                    let ret = &properties[self.cur];
                    self.cur += 1;
                    Some(ret)
                } else {
                    None
                }
            }
        }
    }
}

/// A CSS rule, e.g. `.my-class { ... }`.
#[derive(Clone, Debug)]
pub struct Rule {
    pub(crate) selector: Selector,
    pub(crate) properties: Vec<PropertyMeta>,
    pub(crate) media: Option<Rc<Media>>,
    pub(super) index: u32,
    pub(crate) has_font_size: bool,
}

impl fmt::Display for Rule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let media_queries = self.get_media_query_string_list();
        for media in media_queries.iter() {
            write!(f, "@media {media} {{ ")?;
        }
        let selector = self.get_selector_string();
        write!(f, "{selector} {{ ")?;
        for prop in self.properties() {
            write!(
                f,
                "{}: {}; ",
                prop.get_property_name(),
                prop.get_property_value_string()
            )?;
        }
        write!(f, "}}")?;
        for _ in media_queries.iter() {
            write!(f, " }}")?;
        }
        Ok(())
    }
}

impl Rule {
    /// Create an empty rule.
    pub fn new_empty() -> Box<Self> {
        Box::new(Self {
            selector: Selector::star_selector(),
            properties: Vec::with_capacity(0),
            media: None,
            index: 0,
            has_font_size: false,
        })
    }

    pub(crate) fn new(
        selector: Selector,
        properties: Vec<PropertyMeta>,
        media: Option<Rc<Media>>,
    ) -> Box<Self> {
        Self::new_with_index(selector, properties, media, 0)
    }

    pub(crate) fn new_with_index(
        selector: Selector,
        properties: Vec<PropertyMeta>,
        media: Option<Rc<Media>>,
        index: u32,
    ) -> Box<Self> {
        let mut has_font_size = false;
        for p in properties.iter() {
            match p {
                PropertyMeta::Normal { property } | PropertyMeta::Important { property } => {
                    match property {
                        Property::FontSize(..) => {
                            has_font_size = true;
                        }
                        _ => {}
                    }
                }
                PropertyMeta::DebugGroup { properties, .. } => {
                    for property in properties.iter() {
                        match property {
                            Property::FontSize(..) => {
                                has_font_size = true;
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        Box::new(Self {
            selector,
            properties,
            media,
            index,
            has_font_size,
        })
    }

    /// Construct a new rule with media query strings and a selector string
    ///
    /// The media query strings should not contain the leading `@media` segment (matches the output of `get_media_query_string_list` ).
    pub fn from_parts_str<'a>(
        media_query_str_list: impl IntoIterator<Item = &'a str>,
        selector_str: &str,
    ) -> Result<Box<Self>, Warning> {
        let mut media = None;
        for (index, media_str) in media_query_str_list.into_iter().enumerate() {
            let cur_media = parser::parse_media_expression_only(media_str).map_err(|mut w| {
                w.message = format!("{} (in media index {})", w.message.as_str(), index).into();
                w
            })?;
            media = Some(Rc::new(cur_media));
        }
        let selector = parser::parse_selector_only(selector_str)?;
        Ok(Self::new(selector, vec![], media))
    }

    /// Modify the rule with a different selector (and construct a new one as the result)
    pub fn modify_selector(&self, selector_str: &str) -> Result<Box<Self>, Warning> {
        let media = self.media.clone();
        let selector = parser::parse_selector_only(selector_str)?;
        let properties = self.properties.clone();
        Ok(Self::new(selector, properties, media))
    }

    /// Modify the rule by adding a new property (and construct a new one as the result)
    pub fn add_properties(&self, p: impl IntoIterator<Item = PropertyMeta>) -> Box<Self> {
        let media = self.media.clone();
        let selector = self.selector.clone();
        let mut properties = self.properties.clone();
        for p in p {
            properties.push(p);
        }
        Self::new(selector, properties, media)
    }

    /// Enable or disable the rule (and construct a new one as the result if success)
    pub fn set_property_disabled(&self, index: usize, disabled: bool) -> Option<Box<Self>> {
        let media = self.media.clone();
        let selector = self.selector.clone();
        let mut properties = self.properties.clone();
        if index < properties.len() {
            properties[index] = properties[index].to_debug_state(disabled);
            Some(Self::new(selector, properties, media))
        } else {
            None
        }
    }

    /// Modify the rule by removing a property (and construct a new one as the result if success)
    pub fn remove_property(&self, index: usize) -> Option<Box<Self>> {
        let media = self.media.clone();
        let selector = self.selector.clone();
        let mut properties = self.properties.clone();
        if index < properties.len() {
            properties.remove(index);
            Some(Self::new(selector, properties, media))
        } else {
            None
        }
    }

    /// Modify the rule by replacing properties (and construct a new one as the result if success)
    pub fn replace_properties(
        &self,
        range: impl core::ops::RangeBounds<usize>,
        p: impl IntoIterator<Item = PropertyMeta>,
    ) -> Option<Box<Self>> {
        use core::ops::Bound;
        let media = self.media.clone();
        let selector = self.selector.clone();
        let mut properties = self.properties.clone();
        let no_overflow = match range.end_bound() {
            Bound::Unbounded => true,
            Bound::Included(stp) => *stp < properties.len(),
            Bound::Excluded(stp) => *stp <= properties.len(),
        };
        let no_reversed = match range.start_bound() {
            Bound::Unbounded => true,
            Bound::Included(st) => match range.end_bound() {
                Bound::Unbounded => true,
                Bound::Included(stp) => *st <= *stp,
                Bound::Excluded(stp) => *st < *stp,
            },
            Bound::Excluded(st) => match range.end_bound() {
                Bound::Unbounded => true,
                Bound::Included(stp) => *st < *stp,
                Bound::Excluded(stp) => st + 1 < *stp,
            },
        };
        if no_overflow && no_reversed {
            properties.splice(range, p);
            Some(Self::new(selector, properties, media))
        } else {
            None
        }
    }

    /// Get the `@media` list.
    pub fn get_media_query_string_list(&self) -> Vec<String> {
        let mut list = vec![];
        if let Some(x) = &self.media {
            x.to_media_query_string_list(&mut list);
        }
        list
    }

    /// Get the selector as a string.
    pub fn get_selector_string(&self) -> String {
        format!("{}", self.selector)
    }

    /// Get an iterator of the properties.
    pub fn properties(&self) -> impl Iterator<Item = &PropertyMeta> {
        self.properties.iter()
    }

    pub(crate) fn match_query<L: LengthNum, T: StyleNode>(
        &self,
        query: &[T],
        media_query_status: &MediaQueryStatus<L>,
        sheet_style_scope: Option<NonZeroUsize>,
    ) -> Option<u16> {
        match &self.media {
            Some(media) => {
                if !media.is_valid(media_query_status) {
                    return None;
                }
            }
            None => {}
        }
        self.selector.match_query(query, sheet_style_scope)
    }
}