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
/* 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 https://mozilla.org/MPL/2.0/. */

//! A [`@page`][page] rule.
//!
//! [page]: https://drafts.csswg.org/css2/page.html#page-box

use crate::parser::{Parse, ParserContext};
use crate::properties::PropertyDeclarationBlock;
use crate::shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked};
use crate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use crate::stylesheets::CssRules;
use crate::str::CssStringWriter;
use crate::values::{AtomIdent, CustomIdent};
use cssparser::{Parser, SourceLocation, Token};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
use servo_arc::Arc;
use smallvec::SmallVec;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ParseError, ToCss};

macro_rules! page_pseudo_classes {
    ($($(#[$($meta:tt)+])* $id:ident => $val:literal,)+) => {
        /// [`@page`][page] rule pseudo-classes.
        ///
        /// https://drafts.csswg.org/css-page-3/#page-selectors
        #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)]
        #[repr(u8)]
        pub enum PagePseudoClass {
            $($(#[$($meta)+])* $id,)+
        }
        impl PagePseudoClass {
            fn parse<'i, 't>(
                input: &mut Parser<'i, 't>,
            ) -> Result<Self, ParseError<'i>> {
                let loc = input.current_source_location();
                let colon = input.next_including_whitespace()?;
                if *colon != Token::Colon {
                    return Err(loc.new_unexpected_token_error(colon.clone()));
                }

                let ident = input.next_including_whitespace()?;
                if let Token::Ident(s) = ident {
                    return match_ignore_ascii_case! { &**s,
                        $($val => Ok(PagePseudoClass::$id),)+
                        _ => Err(loc.new_unexpected_token_error(Token::Ident(s.clone()))),
                    };
                }
                Err(loc.new_unexpected_token_error(ident.clone()))
            }
            #[inline]
            fn to_str(&self) -> &'static str {
                match *self {
                    $(PagePseudoClass::$id => concat!(':', $val),)+
                }
            }
        }
    }
}

page_pseudo_classes! {
    /// [`:first`][first] pseudo-class
    ///
    /// [first] https://drafts.csswg.org/css-page-3/#first-pseudo
    First => "first",
    /// [`:blank`][blank] pseudo-class
    ///
    /// [blank] https://drafts.csswg.org/css-page-3/#blank-pseudo
    Blank => "blank",
    /// [`:left`][left] pseudo-class
    ///
    /// [left]: https://drafts.csswg.org/css-page-3/#spread-pseudos
    Left => "left",
    /// [`:right`][right] pseudo-class
    ///
    /// [right]: https://drafts.csswg.org/css-page-3/#spread-pseudos
    Right => "right",
}

bitflags! {
    /// Bit-flags for pseudo-class. This should only be used for querying if a
    /// page-rule applies.
    ///
    /// https://drafts.csswg.org/css-page-3/#page-selectors
    #[derive(Clone, Copy)]
    #[repr(C)]
    pub struct PagePseudoClassFlags : u8 {
        /// No pseudo-classes
        const NONE = 0;
        /// Flag for PagePseudoClass::First
        const FIRST = 1 << 0;
        /// Flag for PagePseudoClass::Blank
        const BLANK = 1 << 1;
        /// Flag for PagePseudoClass::Left
        const LEFT = 1 << 2;
        /// Flag for PagePseudoClass::Right
        const RIGHT = 1 << 3;
    }
}

impl PagePseudoClassFlags {
    /// Creates a pseudo-class flags object with a single pseudo-class.
    #[inline]
    pub fn new(other: &PagePseudoClass) -> Self {
        match *other {
            PagePseudoClass::First => PagePseudoClassFlags::FIRST,
            PagePseudoClass::Blank => PagePseudoClassFlags::BLANK,
            PagePseudoClass::Left => PagePseudoClassFlags::LEFT,
            PagePseudoClass::Right => PagePseudoClassFlags::RIGHT,
        }
    }
    /// Checks if the given pseudo class applies to this set of flags.
    #[inline]
    pub fn contains_class(self, other: &PagePseudoClass) -> bool {
        self.intersects(PagePseudoClassFlags::new(other))
    }
}

type PagePseudoClasses = SmallVec<[PagePseudoClass; 4]>;

/// Type of a single [`@page`][page selector]
///
/// [page-selectors]: https://drafts.csswg.org/css2/page.html#page-selectors
#[derive(Clone, Debug, MallocSizeOf, ToShmem)]
pub struct PageSelector {
    /// Page name
    ///
    /// https://drafts.csswg.org/css-page-3/#page-type-selector
    pub name: AtomIdent,
    /// Pseudo-classes for [`@page`][page-selectors]
    ///
    /// [page-selectors]: https://drafts.csswg.org/css2/page.html#page-selectors
    pub pseudos: PagePseudoClasses,
}

impl PageSelector {
    /// Checks if the ident matches a page-name's ident.
    ///
    /// This does not take pseudo selectors into account.
    #[inline]
    pub fn ident_matches(&self, other: &CustomIdent) -> bool {
        self.name.0 == other.0
    }

    /// Checks that this selector matches the ident and all pseudo classes are
    /// present in the provided flags.
    #[inline]
    pub fn matches(&self, name: &CustomIdent, flags: PagePseudoClassFlags) -> bool {
        self.ident_matches(name) && self.flags_match(flags)
    }

    /// Checks that all pseudo classes in this selector are present in the
    /// provided flags.
    ///
    /// Equivalent to, but may be more efficient than:
    ///
    /// ```
    /// match_specificity(flags).is_some()
    /// ```
    pub fn flags_match(&self, flags: PagePseudoClassFlags) -> bool {
        self.pseudos.iter().all(|pc| flags.contains_class(pc))
    }

    /// Implements specificity calculation for a page selector given a set of
    /// page pseudo-classes to match with.
    /// If this selector includes any pseudo-classes that are not in the flags,
    /// then this will return None.
    ///
    /// To fit the specificity calculation into a 32-bit value, this limits the
    /// maximum count of :first and :blank to 32767, and the maximum count of
    /// :left and :right to 65535.
    ///
    /// https://drafts.csswg.org/css-page-3/#cascading-and-page-context
    pub fn match_specificity(&self, flags: PagePseudoClassFlags) -> Option<u32> {
        let mut g: usize = 0;
        let mut h: usize = 0;
        for pc in self.pseudos.iter() {
            if !flags.contains_class(pc) {
                return None;
            }
            match pc {
                PagePseudoClass::First | PagePseudoClass::Blank => g += 1,
                PagePseudoClass::Left | PagePseudoClass::Right => h += 1,
            }
        }
        let h = h.min(0xFFFF) as u32;
        let g = (g.min(0x7FFF) as u32) << 16;
        let f = if self.name.0.is_empty() {
            0
        } else {
            0x80000000
        };
        Some(h + g + f)
    }
}

impl ToCss for PageSelector {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        self.name.to_css(dest)?;
        for pc in self.pseudos.iter() {
            dest.write_str(pc.to_str())?;
        }
        Ok(())
    }
}

fn parse_page_name<'i, 't>(input: &mut Parser<'i, 't>) -> Result<AtomIdent, ParseError<'i>> {
    let s = input.expect_ident()?;
    Ok(AtomIdent::from(&**s))
}

impl Parse for PageSelector {
    fn parse<'i, 't>(
        _context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        let name = input
            .try_parse(parse_page_name)
            .unwrap_or(AtomIdent::new(atom!("")));
        let mut pseudos = PagePseudoClasses::default();
        while let Ok(pc) = input.try_parse(PagePseudoClass::parse) {
            pseudos.push(pc);
        }
        Ok(PageSelector { name, pseudos })
    }
}

/// A list of [`@page`][page selectors]
///
/// [page-selectors]: https://drafts.csswg.org/css2/page.html#page-selectors
#[derive(Clone, Debug, Default, MallocSizeOf, ToCss, ToShmem)]
#[css(comma)]
pub struct PageSelectors(#[css(iterable)] pub Box<[PageSelector]>);

impl PageSelectors {
    /// Creates a new PageSelectors from a Vec, as from parse_comma_separated
    #[inline]
    pub fn new(s: Vec<PageSelector>) -> Self {
        PageSelectors(s.into())
    }
    /// Returns true iff there are any page selectors
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }
    /// Get the underlying PageSelector data as a slice
    #[inline]
    pub fn as_slice(&self) -> &[PageSelector] {
        &*self.0
    }
}

impl Parse for PageSelectors {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        Ok(PageSelectors::new(input.parse_comma_separated(|i| {
            PageSelector::parse(context, i)
        })?))
    }
}

/// A [`@page`][page] rule.
///
/// This implements only a limited subset of the CSS
/// 2.2 syntax.
///
/// [page]: https://drafts.csswg.org/css2/page.html#page-box
/// [page-selectors]: https://drafts.csswg.org/css2/page.html#page-selectors
#[derive(Clone, Debug, ToShmem)]
pub struct PageRule {
    /// Selectors of the page-rule
    pub selectors: PageSelectors,
    /// Nested rules.
    pub rules: Arc<Locked<CssRules>>,
    /// The declaration block this page rule contains.
    pub block: Arc<Locked<PropertyDeclarationBlock>>,
    /// The source position this rule was found at.
    pub source_location: SourceLocation,
}

impl PageRule {
    /// Measure heap usage.
    #[cfg(feature = "gecko")]
    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
        // Measurement of other fields may be added later.
        self.rules.unconditional_shallow_size_of(ops) +
            self.rules.read_with(guard).size_of(guard, ops) +
            self.block.unconditional_shallow_size_of(ops) +
            self.block.read_with(guard).size_of(ops) +
            self.selectors.size_of(ops)
    }
    /// Computes the specificity of this page rule when matched with flags.
    ///
    /// Computing this value has linear-complexity with the size of the
    /// selectors, so the caller should usually call this once and cache the
    /// result.
    ///
    /// Returns None if the flags do not match this page rule.
    ///
    /// The return type is ordered by page-rule specificity.
    pub fn match_specificity(&self, flags: PagePseudoClassFlags) -> Option<u32> {
        let mut specificity = None;
        for s in self.selectors.0.iter().map(|s| s.match_specificity(flags)) {
            specificity = s.max(specificity);
        }
        specificity
    }
}

impl ToCssWithGuard for PageRule {
    /// Serialization of PageRule is not specced, adapted from steps for
    /// StyleRule.
    fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
        // https://drafts.csswg.org/cssom/#serialize-a-css-rule
        dest.write_str("@page ")?;
        if !self.selectors.is_empty() {
            self.selectors.to_css(&mut CssWriter::new(dest))?;
            dest.write_char(' ')?;
        }
        dest.write_char('{')?;

        // TODO: share more/most of this with style rules
        // https://bugzilla.mozilla.org/1867164
        let declaration_block = self.block.read_with(guard);
        let has_declarations = !declaration_block.declarations().is_empty();

        let rules = self.rules.read_with(guard);
        if !rules.is_empty() {
            if has_declarations {
                dest.write_str("\n  ")?;
                declaration_block.to_css(dest)?;
            }
            return rules.to_css_block_without_opening(guard, dest);
        }

        if has_declarations {
            dest.write_char(' ')?;
            declaration_block.to_css(dest)?;
        }
        dest.write_str(" }")
    }
}

impl DeepCloneWithLock for PageRule {
    fn deep_clone_with_lock(
        &self,
        lock: &SharedRwLock,
        guard: &SharedRwLockReadGuard,
        params: &DeepCloneParams,
    ) -> Self {
        let rules = self.rules.read_with(&guard);
        PageRule {
            selectors: self.selectors.clone(),
            block: Arc::new(lock.wrap(self.block.read_with(&guard).clone())),
            rules: Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard, params))),
            source_location: self.source_location.clone(),
        }
    }
}