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
//! CSS declarations.

use std::borrow::Cow;
use std::ops::Range;

use crate::context::PropertyHandlerContext;
use crate::error::{ParserError, PrinterError};
use crate::parser::ParserOptions;
use crate::printer::Printer;
use crate::properties::box_shadow::BoxShadowHandler;
use crate::properties::masking::MaskHandler;
use crate::properties::{
  align::AlignHandler,
  animation::AnimationHandler,
  background::BackgroundHandler,
  border::BorderHandler,
  contain::ContainerHandler,
  display::DisplayHandler,
  flex::FlexHandler,
  font::FontHandler,
  grid::GridHandler,
  list::ListStyleHandler,
  margin_padding::*,
  outline::OutlineHandler,
  overflow::OverflowHandler,
  position::PositionHandler,
  prefix_handler::{FallbackHandler, PrefixHandler},
  size::SizeHandler,
  text::TextDecorationHandler,
  transform::TransformHandler,
  transition::TransitionHandler,
};
use crate::properties::{Property, PropertyId};
use crate::targets::Browsers;
use crate::traits::{PropertyHandler, ToCss};
use crate::values::string::CowArcStr;
use cssparser::*;

/// A CSS declaration block.
///
/// Properties are separated into a list of `!important` declararations,
/// and a list of normal declarations. This reduces memory usage compared
/// with storing a boolean along with each property.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeclarationBlock<'i> {
  /// A list of `!important` declarations in the block.
  #[cfg_attr(feature = "serde", serde(borrow))]
  pub important_declarations: Vec<Property<'i>>,
  /// A list of normal declarations in the block.
  pub declarations: Vec<Property<'i>>,
}

impl<'i> DeclarationBlock<'i> {
  /// Parses a declaration block from CSS syntax.
  pub fn parse<'a, 'o, 't>(
    input: &mut Parser<'i, 't>,
    options: &'a ParserOptions<'o, 'i>,
  ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
    let mut important_declarations = DeclarationList::new();
    let mut declarations = DeclarationList::new();
    let mut parser = DeclarationListParser::new(
      input,
      PropertyDeclarationParser {
        important_declarations: &mut important_declarations,
        declarations: &mut declarations,
        options,
      },
    );
    while let Some(res) = parser.next() {
      if let Err((err, _)) = res {
        if options.error_recovery {
          options.warn(err);
          continue;
        }
        return Err(err);
      }
    }

    Ok(DeclarationBlock {
      important_declarations,
      declarations,
    })
  }

  /// Parses a declaration block from a string.
  pub fn parse_string<'o>(
    input: &'i str,
    options: ParserOptions<'o, 'i>,
  ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
    let mut input = ParserInput::new(input);
    let mut parser = Parser::new(&mut input);
    let result = Self::parse(&mut parser, &options)?;
    parser.expect_exhausted()?;
    Ok(result)
  }
}

impl<'i> ToCss for DeclarationBlock<'i> {
  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    let len = self.declarations.len() + self.important_declarations.len();
    let mut i = 0;

    macro_rules! write {
      ($decls: expr, $important: literal) => {
        for decl in &$decls {
          decl.to_css(dest, $important)?;
          if i != len - 1 {
            dest.write_char(';')?;
            dest.whitespace()?;
          }
          i += 1;
        }
      };
    }

    write!(self.declarations, false);
    write!(self.important_declarations, true);
    Ok(())
  }
}

impl<'i> DeclarationBlock<'i> {
  pub(crate) fn to_css_block<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
  where
    W: std::fmt::Write,
  {
    dest.whitespace()?;
    dest.write_char('{')?;
    dest.indent();

    let mut i = 0;
    let len = self.declarations.len() + self.important_declarations.len();

    macro_rules! write {
      ($decls: expr, $important: literal) => {
        for decl in &$decls {
          dest.newline()?;
          decl.to_css(dest, $important)?;
          if i != len - 1 || !dest.minify {
            dest.write_char(';')?;
          }
          i += 1;
        }
      };
    }

    write!(self.declarations, false);
    write!(self.important_declarations, true);

    dest.dedent();
    dest.newline()?;
    dest.write_char('}')
  }
}

impl<'i> DeclarationBlock<'i> {
  pub(crate) fn minify(
    &mut self,
    handler: &mut DeclarationHandler<'i>,
    important_handler: &mut DeclarationHandler<'i>,
    context: &mut PropertyHandlerContext<'i, '_>,
  ) {
    macro_rules! handle {
      ($decls: expr, $handler: expr, $important: literal) => {
        for decl in $decls.iter() {
          context.is_important = $important;
          let handled = $handler.handle_property(decl, context);

          if !handled {
            $handler.decls.push(decl.clone());
          }
        }
      };
    }

    handle!(self.important_declarations, important_handler, true);
    handle!(self.declarations, handler, false);

    handler.finalize(context);
    important_handler.finalize(context);
    self.important_declarations = std::mem::take(&mut important_handler.decls);
    self.declarations = std::mem::take(&mut handler.decls);
  }

  /// Returns whether the declaration block is empty.
  pub fn is_empty(&self) -> bool {
    return self.declarations.is_empty() && self.important_declarations.is_empty();
  }

  pub(crate) fn property_location<'t>(
    &self,
    input: &mut Parser<'i, 't>,
    index: usize,
  ) -> Result<(Range<SourceLocation>, Range<SourceLocation>), ParseError<'i, ParserError<'i>>> {
    // Skip to the requested property index.
    for _ in 0..index {
      input.expect_ident()?;
      input.expect_colon()?;
      input.parse_until_after(Delimiter::Semicolon, |parser| {
        while parser.next().is_ok() {}
        Ok(())
      })?;
    }

    // Get property name range.
    input.skip_whitespace();
    let key_start = input.current_source_location();
    input.expect_ident()?;
    let key_end = input.current_source_location();
    let key_range = key_start..key_end;

    input.expect_colon()?;
    input.skip_whitespace();

    // Get value range.
    let val_start = input.current_source_location();
    input.parse_until_before(Delimiter::Semicolon, |parser| {
      while parser.next().is_ok() {}
      Ok(())
    })?;
    let val_end = input.current_source_location();
    let val_range = val_start..val_end;

    Ok((key_range, val_range))
  }
}

impl<'i> DeclarationBlock<'i> {
  /// Returns an iterator over all properties in the declaration.
  pub fn iter(&self) -> impl std::iter::DoubleEndedIterator<Item = (&Property<'i>, bool)> {
    self
      .declarations
      .iter()
      .map(|property| (property, false))
      .chain(self.important_declarations.iter().map(|property| (property, true)))
  }

  /// Returns a mutable iterator over all properties in the declaration.
  pub fn iter_mut(&mut self) -> impl std::iter::DoubleEndedIterator<Item = &mut Property<'i>> {
    self.declarations.iter_mut().chain(self.important_declarations.iter_mut())
  }

  /// Returns the value for a given property id based on the properties in this declaration block.
  ///
  /// If the property is a shorthand, the result will be a combined value of all of the included
  /// longhands, or `None` if some of the longhands are not declared. Otherwise, the value will be
  /// either an explicitly declared longhand, or a value extracted from a shorthand property.
  pub fn get<'a>(&'a self, property_id: &PropertyId) -> Option<(Cow<'a, Property<'i>>, bool)> {
    if property_id.is_shorthand() {
      if let Some((shorthand, important)) = property_id.shorthand_value(&self) {
        return Some((Cow::Owned(shorthand), important));
      }
    } else {
      for (property, important) in self.iter().rev() {
        if property.property_id() == *property_id {
          return Some((Cow::Borrowed(property), important));
        }

        if let Some(val) = property.longhand(&property_id) {
          return Some((Cow::Owned(val), important));
        }
      }
    }

    None
  }

  /// Sets the value and importance for a given property, replacing any existing declarations.
  ///
  /// If the property already exists within the declaration block, it is updated in place. Otherwise,
  /// a new declaration is appended. When updating a longhand property and a shorthand is defined which
  /// includes the longhand, the shorthand will be updated rather than appending a new declaration.
  pub fn set(&mut self, property: Property<'i>, important: bool) {
    let property_id = property.property_id();
    let declarations = if important {
      // Remove any non-important properties with this id.
      self.declarations.retain(|decl| decl.property_id() != property_id);
      &mut self.important_declarations
    } else {
      // Remove any important properties with this id.
      self.important_declarations.retain(|decl| decl.property_id() != property_id);
      &mut self.declarations
    };

    let longhands = property_id.longhands().unwrap_or_else(|| vec![property.property_id()]);

    for decl in declarations.iter_mut().rev() {
      {
        // If any of the longhands being set are in the same logical property group as any of the
        // longhands in this property, but in a different category (i.e. logical or physical),
        // then we cannot modify in place, and need to append a new property.
        let id = decl.property_id();
        let id_longhands = id.longhands().unwrap_or_else(|| vec![id]);
        if longhands.iter().any(|longhand| {
          let logical_group = longhand.logical_group();
          let category = longhand.category();

          logical_group.is_some()
            && id_longhands.iter().any(|id_longhand| {
              logical_group == id_longhand.logical_group() && category != id_longhand.category()
            })
        }) {
          break;
        }
      }

      if decl.property_id() == property_id {
        *decl = property;
        return;
      }

      // Update shorthand.
      if decl.set_longhand(&property).is_ok() {
        return;
      }
    }

    declarations.push(property)
  }

  /// Removes all declarations of the given property id from the declaration block.
  ///
  /// When removing a longhand property and a shorthand is defined which includes the longhand,
  /// the shorthand will be split apart into its component longhand properties, minus the property
  /// to remove. When removing a shorthand, all included longhand properties are also removed.
  pub fn remove(&mut self, property_id: &PropertyId) {
    fn remove<'i, 'a>(declarations: &mut Vec<Property<'i>>, property_id: &PropertyId<'a>) {
      let longhands = property_id.longhands().unwrap_or(vec![]);
      let mut i = 0;
      while i < declarations.len() {
        let replacement = {
          let property = &declarations[i];
          let id = property.property_id();
          if id == *property_id || longhands.contains(&id) {
            // If the property matches the requested property id, or is a longhand
            // property that is included in the requested shorthand, remove it.
            None
          } else if longhands.is_empty() && id.longhands().unwrap_or(vec![]).contains(&property_id) {
            // If this is a shorthand property that includes the requested longhand,
            // split it apart into its component longhands, excluding the requested one.
            Some(
              id.longhands()
                .unwrap()
                .iter()
                .filter_map(|longhand| {
                  if *longhand == *property_id {
                    None
                  } else {
                    property.longhand(longhand)
                  }
                })
                .collect::<Vec<Property>>(),
            )
          } else {
            i += 1;
            continue;
          }
        };

        match replacement {
          Some(properties) => {
            let count = properties.len();
            declarations.splice(i..i + 1, properties);
            i += count;
          }
          None => {
            declarations.remove(i);
          }
        }
      }
    }

    remove(&mut self.declarations, property_id);
    remove(&mut self.important_declarations, property_id);
  }
}

struct PropertyDeclarationParser<'a, 'o, 'i> {
  important_declarations: &'a mut Vec<Property<'i>>,
  declarations: &'a mut Vec<Property<'i>>,
  options: &'a ParserOptions<'o, 'i>,
}

/// Parse a declaration within {} block: `color: blue`
impl<'a, 'o, 'i> cssparser::DeclarationParser<'i> for PropertyDeclarationParser<'a, 'o, 'i> {
  type Declaration = ();
  type Error = ParserError<'i>;

  fn parse_value<'t>(
    &mut self,
    name: CowRcStr<'i>,
    input: &mut cssparser::Parser<'i, 't>,
  ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> {
    parse_declaration(
      name,
      input,
      &mut self.declarations,
      &mut self.important_declarations,
      &self.options,
    )
  }
}

/// Default methods reject all at rules.
impl<'a, 'o, 'i> AtRuleParser<'i> for PropertyDeclarationParser<'a, 'o, 'i> {
  type Prelude = ();
  type AtRule = ();
  type Error = ParserError<'i>;
}

pub(crate) fn parse_declaration<'i, 't>(
  name: CowRcStr<'i>,
  input: &mut cssparser::Parser<'i, 't>,
  declarations: &mut DeclarationList<'i>,
  important_declarations: &mut DeclarationList<'i>,
  options: &ParserOptions,
) -> Result<(), cssparser::ParseError<'i, ParserError<'i>>> {
  let property = input.parse_until_before(Delimiter::Bang, |input| {
    Property::parse(PropertyId::from(CowArcStr::from(name)), input, options)
  })?;
  let important = input
    .try_parse(|input| {
      input.expect_delim('!')?;
      input.expect_ident_matching("important")
    })
    .is_ok();
  if important {
    important_declarations.push(property);
  } else {
    declarations.push(property);
  }
  Ok(())
}

pub(crate) type DeclarationList<'i> = Vec<Property<'i>>;

pub(crate) struct DeclarationHandler<'i> {
  background: BackgroundHandler<'i>,
  border: BorderHandler<'i>,
  outline: OutlineHandler,
  flex: FlexHandler,
  grid: GridHandler<'i>,
  align: AlignHandler,
  size: SizeHandler,
  margin: MarginHandler<'i>,
  padding: PaddingHandler<'i>,
  scroll_margin: ScrollMarginHandler<'i>,
  scroll_padding: ScrollPaddingHandler<'i>,
  font: FontHandler<'i>,
  text: TextDecorationHandler<'i>,
  list: ListStyleHandler<'i>,
  transition: TransitionHandler<'i>,
  animation: AnimationHandler<'i>,
  display: DisplayHandler<'i>,
  position: PositionHandler,
  inset: InsetHandler<'i>,
  overflow: OverflowHandler,
  transform: TransformHandler,
  box_shadow: BoxShadowHandler,
  mask: MaskHandler<'i>,
  container: ContainerHandler<'i>,
  fallback: FallbackHandler,
  prefix: PrefixHandler,
  decls: DeclarationList<'i>,
}

impl<'i> DeclarationHandler<'i> {
  pub fn new(targets: Option<Browsers>) -> Self {
    DeclarationHandler {
      background: BackgroundHandler::new(targets),
      border: BorderHandler::new(targets),
      outline: OutlineHandler::new(targets),
      flex: FlexHandler::new(targets),
      grid: GridHandler::default(),
      align: AlignHandler::new(targets),
      size: SizeHandler::default(),
      margin: MarginHandler::default(),
      padding: PaddingHandler::default(),
      scroll_margin: ScrollMarginHandler::default(),
      scroll_padding: ScrollPaddingHandler::default(),
      font: FontHandler::default(),
      text: TextDecorationHandler::new(targets),
      list: ListStyleHandler::new(targets),
      transition: TransitionHandler::new(targets),
      animation: AnimationHandler::new(targets),
      display: DisplayHandler::new(targets),
      position: PositionHandler::new(targets),
      inset: InsetHandler::default(),
      overflow: OverflowHandler::new(targets),
      transform: TransformHandler::new(targets),
      box_shadow: BoxShadowHandler::new(targets),
      mask: MaskHandler::default(),
      container: ContainerHandler::default(),
      fallback: FallbackHandler::new(targets),
      prefix: PrefixHandler::new(targets),
      decls: DeclarationList::new(),
    }
  }

  pub fn handle_property(
    &mut self,
    property: &Property<'i>,
    context: &mut PropertyHandlerContext<'i, '_>,
  ) -> bool {
    if !context.unused_symbols.is_empty()
      && matches!(property, Property::Custom(custom) if context.unused_symbols.contains(custom.name.as_ref()))
    {
      return true;
    }

    self.background.handle_property(property, &mut self.decls, context)
      || self.border.handle_property(property, &mut self.decls, context)
      || self.outline.handle_property(property, &mut self.decls, context)
      || self.flex.handle_property(property, &mut self.decls, context)
      || self.grid.handle_property(property, &mut self.decls, context)
      || self.align.handle_property(property, &mut self.decls, context)
      || self.size.handle_property(property, &mut self.decls, context)
      || self.margin.handle_property(property, &mut self.decls, context)
      || self.padding.handle_property(property, &mut self.decls, context)
      || self.scroll_margin.handle_property(property, &mut self.decls, context)
      || self.scroll_padding.handle_property(property, &mut self.decls, context)
      || self.font.handle_property(property, &mut self.decls, context)
      || self.text.handle_property(property, &mut self.decls, context)
      || self.list.handle_property(property, &mut self.decls, context)
      || self.transition.handle_property(property, &mut self.decls, context)
      || self.animation.handle_property(property, &mut self.decls, context)
      || self.display.handle_property(property, &mut self.decls, context)
      || self.position.handle_property(property, &mut self.decls, context)
      || self.inset.handle_property(property, &mut self.decls, context)
      || self.overflow.handle_property(property, &mut self.decls, context)
      || self.transform.handle_property(property, &mut self.decls, context)
      || self.box_shadow.handle_property(property, &mut self.decls, context)
      || self.mask.handle_property(property, &mut self.decls, context)
      || self.container.handle_property(property, &mut self.decls, context)
      || self.fallback.handle_property(property, &mut self.decls, context)
      || self.prefix.handle_property(property, &mut self.decls, context)
  }

  pub fn finalize(&mut self, context: &mut PropertyHandlerContext<'i, '_>) {
    self.background.finalize(&mut self.decls, context);
    self.border.finalize(&mut self.decls, context);
    self.outline.finalize(&mut self.decls, context);
    self.flex.finalize(&mut self.decls, context);
    self.grid.finalize(&mut self.decls, context);
    self.align.finalize(&mut self.decls, context);
    self.size.finalize(&mut self.decls, context);
    self.margin.finalize(&mut self.decls, context);
    self.padding.finalize(&mut self.decls, context);
    self.scroll_margin.finalize(&mut self.decls, context);
    self.scroll_padding.finalize(&mut self.decls, context);
    self.font.finalize(&mut self.decls, context);
    self.text.finalize(&mut self.decls, context);
    self.list.finalize(&mut self.decls, context);
    self.transition.finalize(&mut self.decls, context);
    self.animation.finalize(&mut self.decls, context);
    self.display.finalize(&mut self.decls, context);
    self.position.finalize(&mut self.decls, context);
    self.inset.finalize(&mut self.decls, context);
    self.overflow.finalize(&mut self.decls, context);
    self.transform.finalize(&mut self.decls, context);
    self.box_shadow.finalize(&mut self.decls, context);
    self.mask.finalize(&mut self.decls, context);
    self.container.finalize(&mut self.decls, context);
    self.fallback.finalize(&mut self.decls, context);
    self.prefix.finalize(&mut self.decls, context);
  }
}