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
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
//! The style sheet data structures.

use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
use core::{cell::RefCell, num::NonZeroUsize};
use parser::WarningKind;

use hashbrown::HashMap;

use super::property::*;
use super::query::*;
use super::*;
use crate::group::StyleSheetResource;
use crate::length_num::LengthNum;
use crate::parser::Warning;

mod selector;
pub use selector::PseudoElements;
pub(crate) use selector::{
    Attribute, AttributeFlags, AttributeOperator, PseudoClasses, Selector, SelectorFragment,
    SelectorRelationType, SELECTOR_WHITESPACE,
};
mod rule;
pub use rule::Rule;
mod media;
pub use media::*;
pub(crate) mod keyframes;
pub use keyframes::*;
mod font_face;
pub use font_face::*;
pub mod borrow;
pub mod borrow_resource;
pub mod str_store;
pub use rule::PropertyMeta;

#[derive(Debug, Clone)]
pub(crate) struct CompiledStyleSheet {
    imports: Vec<(String, Option<Rc<Media>>)>,
    linked: bool,
    ss: Rc<RefCell<StyleSheet>>,
}

impl CompiledStyleSheet {
    pub(crate) fn new() -> Self {
        Self {
            imports: Vec::with_capacity(0),
            linked: false,
            ss: Rc::new(RefCell::new(StyleSheet {
                rules: vec![],
                index: StyleSheetIndex::NeedUpdate,
                font_face: vec![],
                keyframes: vec![],
            })),
        }
    }

    #[cfg(feature = "deserialize")]
    pub(crate) fn new_with_config(
        imports: Vec<(String, Option<Rc<Media>>)>,
        rules: Vec<Rc<Rule>>,
        font_face: Vec<Rc<FontFace>>,
        keyframes: Vec<Rc<KeyFrames>>,
    ) -> Self {
        Self {
            imports,
            linked: false,
            ss: Rc::new(RefCell::new(StyleSheet {
                rules,
                index: StyleSheetIndex::NeedUpdate,
                font_face,
                keyframes,
            })),
        }
    }

    pub(crate) fn list_deps(&self) -> Vec<String> {
        self.imports.iter().map(|(s, _)| s.clone()).collect()
    }

    pub(crate) fn add_import(&mut self, path: String, media: Option<Rc<Media>>) {
        self.imports.push((path, media));
    }

    pub(crate) fn add_rule(&mut self, rule: Box<Rule>) {
        self.ss.borrow_mut().add_rule(rule);
    }

    pub(crate) fn add_font_face(&mut self, ff: FontFace) {
        self.ss.borrow_mut().add_font_face(ff)
    }

    pub(crate) fn add_keyframes(&mut self, keyframes: KeyFrames) {
        self.ss.borrow_mut().add_keyframes(keyframes)
    }

    pub(crate) fn add_tag_name_prefix(&mut self, prefix: &str) {
        let mut ss = self.ss.borrow_mut();
        for rule in ss.rules.iter_mut() {
            let rule = Rc::make_mut(rule);
            for frag in rule.selector.fragments.iter_mut() {
                frag.add_tag_name_prefix(prefix)
            }
        }
    }

    pub(crate) fn link(
        &mut self,
        res: &StyleSheetResource,
        scope: Option<NonZeroUsize>,
    ) -> (LinkedStyleSheet, Vec<Warning>) {
        let mut sheets = vec![];
        let mut warnings = vec![];
        self.link_self(res, &mut sheets, None, &mut warnings);
        (LinkedStyleSheet { sheets, scope }, warnings)
    }

    #[allow(clippy::type_complexity)]
    fn link_self(
        &mut self,
        res: &StyleSheetResource,
        sheets: &mut Vec<(Rc<RefCell<StyleSheet>>, Option<Rc<Media>>)>,
        parent_media: Option<Rc<Media>>,
        warnings: &mut Vec<Warning>,
    ) {
        if !self.linked {
            self.ss.borrow_mut().update_index();
            self.linked = true;
        }
        for (target_path, media) in self.imports.iter() {
            if let Some(target) = res.refs.get(target_path) {
                if let Ok(mut target) = target.try_borrow_mut() {
                    let m = match media.clone() {
                        None => parent_media.clone(),
                        Some(mut m) => {
                            Rc::make_mut(&mut m).parent.clone_from(&parent_media);
                            Some(m)
                        }
                    };
                    target.link_self(res, sheets, m, warnings);
                } else {
                    warnings.push(Warning {
                        kind: WarningKind::RecursiveImports,
                        message: format!(
                            "detected recursive style sheet import for {target_path:?}"
                        )
                        .into(),
                        start_line: 0,
                        start_col: 0,
                        end_line: 0,
                        end_col: 0,
                    });
                }
            } else {
                warnings.push(Warning {
                    kind: WarningKind::MissingImportTarget,
                    message: format!(r#"target style sheet {target_path:?} not found"#).into(),
                    start_line: 0,
                    start_col: 0,
                    end_line: 0,
                    end_col: 0,
                });
            }
        }
        sheets.push((self.ss.clone(), parent_media));
    }

    #[cfg(feature = "serialize")]
    pub(crate) fn serialize_bincode(&self) -> Vec<u8> {
        use float_pigment_consistent_bincode::Options;
        let s = borrow::StyleSheet::from_sheet(self);
        float_pigment_consistent_bincode::DefaultOptions::new()
            .allow_trailing_bytes()
            .serialize(&s)
            .unwrap()
    }

    #[cfg(feature = "deserialize")]
    pub(crate) fn deserialize_bincode(s: Vec<u8>) -> Result<Self, String> {
        use float_pigment_consistent_bincode::Options;
        let s: Result<borrow::StyleSheet, _> =
            float_pigment_consistent_bincode::DefaultOptions::new()
                .allow_trailing_bytes()
                .deserialize(&s);
        match s {
            Ok(ss) => Ok(ss.into_sheet()),
            Err(err) => Err(format!(
                "Failed to deserialize bincode formatted style sheet: {err}"
            )),
        }
    }

    #[cfg(feature = "deserialize")]
    pub(crate) unsafe fn deserialize_bincode_zero_copy(
        ptr: *const [u8],
        drop_callback: impl 'static + FnOnce(),
    ) -> Result<Self, String> {
        use float_pigment_consistent_bincode::Options;
        borrow::de_static_ref_zero_copy_env(
            ptr,
            |s| {
                let s: Result<borrow::StyleSheet, _> =
                    float_pigment_consistent_bincode::DefaultOptions::new()
                        .allow_trailing_bytes()
                        .deserialize(s);
                match s {
                    Ok(ss) => Ok(ss.into_sheet()),
                    Err(err) => Err(format!(
                        "Failed to deserialize bincode formatted style sheet: {err}"
                    )),
                }
            },
            drop_callback,
        )
    }

    #[cfg(all(feature = "serialize", feature = "serialize_json"))]
    pub(crate) fn serialize_json(&self) -> String {
        let s = borrow::StyleSheet::from_sheet(self);
        serde_json::to_string(&s).unwrap()
    }

    #[cfg(all(feature = "serialize", feature = "deserialize_json"))]
    pub(crate) fn deserialize_json(s: &str) -> Result<Self, String> {
        let s: Result<borrow::StyleSheet, _> = serde_json::from_str(s);
        match s {
            Ok(ss) => Ok(ss.into_sheet()),
            Err(err) => Err(format!(
                "Failed to deserialize json formatted style sheet: {err}"
            )),
        }
    }

    #[cfg(all(feature = "serialize", feature = "deserialize_json"))]
    pub(crate) unsafe fn deserialize_json_zero_copy(
        ptr: *mut [u8],
        drop_callback: impl 'static + FnOnce(),
    ) -> Result<Self, String> {
        borrow::de_static_ref_zero_copy_env(
            ptr,
            |s| {
                let s: Result<borrow::StyleSheet, _> =
                    serde_json::from_str(std::str::from_utf8_unchecked(s));
                match s {
                    Ok(ss) => Ok(ss.into_sheet()),
                    Err(err) => Err(format!(
                        "Failed to deserialize json formatted style sheet: {err}"
                    )),
                }
            },
            drop_callback,
        )
    }
}

/// A fully-parsed style sheet file.
///
/// A linked style sheet has a `scope` attached.
/// The scope can be used in style queries, to limit the style sheets which can be matched in the queries.
#[allow(clippy::type_complexity)]
#[derive(Debug, Clone)]
pub struct LinkedStyleSheet {
    sheets: Vec<(Rc<RefCell<StyleSheet>>, Option<Rc<Media>>)>,
    scope: Option<NonZeroUsize>,
}

impl LinkedStyleSheet {
    /// Create an empty style sheet file with no scope limits.
    pub fn new_empty() -> Self {
        let mut ss = CompiledStyleSheet::new();
        ss.link(&StyleSheetResource::new(), None).0
    }

    /// Get the scope of the style sheet file.
    pub fn scope(&self) -> Option<NonZeroUsize> {
        self.scope
    }

    /// Get all style sheets.
    ///
    /// A style sheet file can contain several `StyleSheet`.
    /// If the file has no `@import`, it has only one `StyleSheet`.
    /// Otherwise, other `StyleSheet` will be imported.
    ///
    /// All `StyleSheet`s are ordered based on the imported order.
    pub fn sheets(&self) -> Vec<Rc<RefCell<StyleSheet>>> {
        self.sheets.iter().map(|x| x.0.clone()).collect::<Vec<_>>()
    }

    #[doc(hidden)]
    pub fn rules_count(&self, sheet_index: Option<usize>) -> Option<u32> {
        if let Some(idx) = sheet_index {
            if self.sheets.len() > (idx + 1usize) {
                return None;
            }
            return Some(self.sheets.get(idx).unwrap().0.borrow().rules_count());
        }
        let mut count = 0;
        self.sheets.iter().for_each(|item| {
            count += item.0.borrow().rules_count();
        });
        Some(count)
    }

    /// Parse style sheet source to a style sheet directly.
    ///
    /// All `@import`s are ignored.
    /// It is a convinient way if there is no `@import` in the source.
    pub fn parse(source: &str, scope: Option<NonZeroUsize>) -> (Self, Vec<Warning>) {
        let (mut ss, mut warnings) = parser::parse_style_sheet("", source);
        let (ret, mut w2) = ss.link(&StyleSheetResource::new(), scope);
        warnings.append(&mut w2);
        (ret, warnings)
    }

    /// Get a rule by index.
    pub fn get_rule(&self, mut rule_index: u32) -> Option<Rc<Rule>> {
        for (sheet, _media) in self.sheets.iter() {
            let sheet = sheet.borrow();
            if rule_index < sheet.rules_count() {
                return sheet.get_rule(rule_index).cloned();
            }
            rule_index -= sheet.rules_count();
        }
        None
    }

    /// Append a new rule.
    ///
    /// Generally it is used for debugging.
    /// Re-query is needed when the style sheet is updated.
    pub fn add_rule(&mut self, rule: Box<Rule>) -> u32 {
        let mut rule_index = 0;
        for (sheet, _media) in self.sheets.iter() {
            rule_index += sheet.borrow().rules_count();
        }
        self.sheets
            .last_mut()
            .unwrap()
            .0
            .borrow_mut()
            .add_rule(rule);
        rule_index
    }

    /// Replace an existing rule with a new rule.
    ///
    /// The new rule is returned if success.
    /// Generally it is used for debugging.
    /// Re-query is needed when the style sheet is updated.
    pub fn replace_rule(
        &mut self,
        mut rule_index: u32,
        rule: Box<Rule>,
    ) -> Result<Rc<Rule>, Box<Rule>> {
        for (sheet, _media) in self.sheets.iter_mut() {
            let mut sheet = sheet.borrow_mut();
            if rule_index < sheet.rules_count() {
                return sheet.replace_rule(rule_index, rule);
            }
            rule_index -= sheet.rules_count();
        }
        Err(rule)
    }

    pub(crate) fn for_each_matched_rule<L: LengthNum, T: StyleNode>(
        &self,
        query: &[T],
        media_query_status: &MediaQueryStatus<L>,
        sheet_index: u16,
        mut f: impl FnMut(MatchedRuleRef),
    ) {
        // start from 1, so that computed weight of Matched rules is always non-zero
        let mut rule_index_offset = 1;
        for (sheet, media) in self.sheets.iter() {
            if let Some(media) = media {
                if !media.is_valid(media_query_status) {
                    continue;
                }
            }
            sheet.borrow_mut().for_each_matched_rule(
                query,
                media_query_status,
                self.scope,
                sheet_index,
                rule_index_offset,
                &mut f,
            );
            rule_index_offset += sheet.borrow().rules_count();
        }
    }

    pub(crate) fn search_keyframes<L: LengthNum>(
        &self,
        style_scope: Option<NonZeroUsize>,
        name: &str,
        media_query_status: &MediaQueryStatus<L>,
    ) -> Option<Rc<KeyFrames>> {
        if self.scope.is_some() && self.scope != style_scope {
            return None;
        }
        for (sheet, media) in self.sheets.iter() {
            if let Some(media) = media {
                if !media.is_valid(media_query_status) {
                    continue;
                }
            }
            // TODO consider build a hashmap index
            for k in sheet.borrow().keyframes.iter().rev() {
                if k.ident.as_str() == name {
                    return Some(k.clone());
                }
            }
        }
        None
    }

    /// Get all `@font-face` definitions.
    pub fn get_font_face(&self) -> Vec<Rc<FontFace>> {
        let mut ret = vec![];
        for (sheet, _) in self.sheets.iter() {
            let sheet = sheet.borrow();
            sheet.font_face().iter().for_each(|ff| ret.push(ff.clone()));
        }
        ret
    }
}

/// A style sheet body without `@import` information.
#[derive(Clone)]
pub struct StyleSheet {
    rules: Vec<Rc<Rule>>,
    index: StyleSheetIndex,
    font_face: Vec<Rc<FontFace>>,
    keyframes: Vec<Rc<KeyFrames>>,
}

#[derive(Clone)]
enum StyleSheetIndex {
    NeedUpdate,
    Updated {
        class_index: HashMap<String, Vec<Rc<Rule>>>,
        class_unindexed: Vec<Rc<Rule>>,
    },
}

impl core::fmt::Debug for StyleSheet {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "StyleSheet {{")?;
        for rule in self.rules.iter() {
            write!(f, " {rule:?}")?;
        }
        for font_face in self.font_face.iter() {
            write!(f, " {font_face:?}")?;
        }
        for keyframes in self.keyframes.iter() {
            write!(f, " {keyframes:?}")?;
        }
        write!(f, " }}")
    }
}

impl core::fmt::Display for StyleSheet {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        for rule in self.rules.iter() {
            write!(f, " {rule}")?;
        }
        for font_face in self.font_face.iter() {
            write!(f, " {font_face}")?;
        }
        for keyframes in self.keyframes.iter() {
            write!(f, " {keyframes}")?;
        }
        Ok(())
    }
}

impl StyleSheet {
    #[doc(hidden)]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> LinkedStyleSheet {
        let (mut ss, warnings) = parser::parse_style_sheet("", s);
        for warning in warnings {
            warn!("{:?}", warning);
        }
        let ret = ss.link(&StyleSheetResource::new(), None);
        ret.0
    }

    #[doc(hidden)]
    pub fn from_str_with_path(path: &str, s: &str) -> LinkedStyleSheet {
        let (mut ss, warnings) = parser::parse_style_sheet(path, s);
        for warning in warnings {
            warn!("{:?}", warning);
        }
        let ret = ss.link(&StyleSheetResource::new(), None);
        ret.0
    }

    fn rules_count(&self) -> u32 {
        self.rules.len().min(u32::MAX as usize) as u32
    }

    fn get_rule(&self, rule_index: u32) -> Option<&Rc<Rule>> {
        self.rules.get(rule_index as usize)
    }

    fn add_rule(&mut self, mut rule: Box<Rule>) {
        rule.index = self.rules.len() as u32;
        self.rules.push(Rc::from(rule));
        self.index = StyleSheetIndex::NeedUpdate;
    }

    fn replace_rule(
        &mut self,
        rule_index: u32,
        mut rule: Box<Rule>,
    ) -> Result<Rc<Rule>, Box<Rule>> {
        let index = rule_index as usize;
        if index < self.rules.len() {
            rule.index = rule_index;
            let mut rule = Rc::from(rule);
            core::mem::swap(&mut self.rules[index], &mut rule);
            self.index = StyleSheetIndex::NeedUpdate;
            Ok(rule)
        } else {
            Err(rule)
        }
    }

    fn update_index(&mut self) {
        if let StyleSheetIndex::NeedUpdate = &self.index {
            let mut class_index: HashMap<String, Vec<Rc<Rule>>> = HashMap::default();
            let mut class_unindexed = vec![];
            for rule in self.rules.iter() {
                let index_classes = rule.selector.get_index_classes();
                for c in index_classes {
                    if !c.is_empty() {
                        let c = class_index.entry(c).or_default();
                        c.push(rule.clone());
                    } else {
                        class_unindexed.push(rule.clone());
                    }
                }
            }
            self.index = StyleSheetIndex::Updated {
                class_index,
                class_unindexed,
            };
        }
    }

    fn for_each_matched_rule<L: LengthNum, T: StyleNode>(
        &mut self,
        query: &[T],
        media_query_status: &MediaQueryStatus<L>,
        sheet_style_scope: Option<NonZeroUsize>,
        sheet_index: u16,
        rule_index_offset: u32,
        mut f: impl FnMut(MatchedRuleRef),
    ) {
        self.update_index();
        if let StyleSheetIndex::Updated {
            class_index,
            class_unindexed,
        } = &self.index
        {
            if sheet_style_scope.is_none()
                || query
                    .last()
                    .is_some_and(|x| x.contain_scope(sheet_style_scope))
            {
                for r in class_unindexed.iter() {
                    if let Some(selector_weight) =
                        r.match_query(query, media_query_status, sheet_style_scope)
                    {
                        let weight = RuleWeight::new(
                            selector_weight,
                            sheet_index,
                            rule_index_offset + r.index,
                        );
                        f(MatchedRuleRef { rule: r, weight });
                    }
                }
            }
            let query_last = match query.last() {
                Some(x) => x,
                None => return,
            };
            for class in query_last.classes() {
                if sheet_style_scope.is_none() || sheet_style_scope == class.scope() {
                    if let Some(rules) = class_index.get(class.name()) {
                        for r in rules {
                            if let Some(selector_weight) =
                                r.match_query(query, media_query_status, sheet_style_scope)
                            {
                                let weight = RuleWeight::new(
                                    selector_weight,
                                    sheet_index,
                                    rule_index_offset + r.index,
                                );
                                f(MatchedRuleRef { rule: r, weight });
                            }
                        }
                    }
                }
            }
        }
    }

    /// Add a font-face definition to the style sheet.
    pub fn add_font_face(&mut self, ff: FontFace) {
        self.font_face.push(Rc::new(ff));
    }

    /// Get all font-face definitions.
    pub fn font_face(&self) -> &[Rc<FontFace>] {
        &self.font_face
    }

    pub(crate) fn add_keyframes(&mut self, keyframes: KeyFrames) {
        self.keyframes.push(Rc::new(keyframes));
    }
}

/// The weight of a rule (unique for each rule).
///
/// Weight of a rule is composed of multiple factors.
///
/// * High 16 bits is for the selector, while the detailed layout is `-MICCCCCCCCP--TT`:
///   * `M` - the important bit;
///   * `I` - the ID selector bit;
///   * `C` - the sum of the class selectors and the attribute selectors (max 255);
///   * `P` - the pseudo class bit;
///   * `T` - the sum of the tag name selector and the pseudo element selector.
/// * High 16th~31st bits is the style sheet index (0-based index).
/// * High 32nd~63rd bits is the rule index in the whole linked style sheet (1-based index).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuleWeight(u64);

impl RuleWeight {
    pub(crate) fn new(selector_weight: u16, sheet_index: u16, rule_index: u32) -> Self {
        let weight =
            ((selector_weight as u64) << 48) + ((sheet_index as u64) << 32) + rule_index as u64;
        Self(weight)
    }

    pub(crate) fn inline() -> Self {
        Self(1 << 62)
    }

    /// Get the underlying weight number.
    pub fn normal(&self) -> u64 {
        self.0
    }

    /// Get the underlying weight number with `!important` added.
    pub fn important(&self) -> u64 {
        self.0 + (1 << 63)
    }

    /// Get the style sheet index.
    pub fn sheet_index(&self) -> u16 {
        (self.0 >> 32) as u16
    }

    /// Get the rule index.
    pub fn rule_index(&self) -> u32 {
        (self.0 as u32) - 1
    }
}