css-inline 0.23.1

High-performance library for inlining CSS into HTML 'style' attributes
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
use cssparser::{ParserState, Token};
use std::ops::Range;

pub(crate) struct CSSDeclarationListParser;

pub(crate) type Name<'i> = cssparser::CowRcStr<'i>;
pub(crate) type Declaration<'i> = (Name<'i>, &'i str);

/// A style rule: its selector list, the range of its declarations and the layer it belongs to.
#[derive(Debug)]
pub(crate) struct Rule<'i> {
    pub(crate) selectors: &'i str,
    pub(crate) declarations: (usize, usize),
    pub(crate) layer: LayerId,
}

/// Split a selector list on its top-level commas, leaving commas inside `()`, `[]`, strings,
/// comments and escapes intact.
pub(crate) fn split_selector_list(list: &str) -> impl Iterator<Item = &str> {
    let mut rest = Some(list);
    std::iter::from_fn(move || {
        let current = rest?;
        if let Some(comma) = top_level_comma(current) {
            let (selector, tail) = current.split_at(comma);
            rest = tail.strip_prefix(',');
            Some(selector)
        } else {
            rest = None;
            Some(current)
        }
    })
}

#[derive(Clone, Copy)]
enum Scan {
    Code,
    String(u8),
    Comment,
}

fn top_level_comma(list: &str) -> Option<usize> {
    let bytes = list.as_bytes();
    let comma = memchr::memchr(b',', bytes)?;
    // Without these bytes nothing before the comma can nest, quote, comment or escape it
    let prefix = &bytes[..comma];
    if memchr::memchr3(b'(', b'[', b'\\', prefix).is_none()
        && memchr::memchr3(b'"', b'\'', b'/', prefix).is_none()
    {
        return Some(comma);
    }
    top_level_comma_scan(list)
}

fn top_level_comma_scan(list: &str) -> Option<usize> {
    let mut scan = Scan::Code;
    let mut depth = 0usize;
    let mut escaped = false;
    let mut previous = 0u8;
    for (idx, &byte) in list.as_bytes().iter().enumerate() {
        // Every special byte is ASCII, so an escape never hides one inside a multibyte char
        if escaped {
            escaped = false;
            previous = 0;
            continue;
        }
        match scan {
            Scan::Comment => {
                if previous == b'*' && byte == b'/' {
                    scan = Scan::Code;
                    previous = 0;
                    continue;
                }
            }
            Scan::String(quote) => {
                if byte == b'\\' {
                    escaped = true;
                } else if byte == quote {
                    scan = Scan::Code;
                }
            }
            Scan::Code => match byte {
                b'\\' => escaped = true,
                b'"' | b'\'' => scan = Scan::String(byte),
                b'*' if previous == b'/' => {
                    scan = Scan::Comment;
                    previous = 0;
                    continue;
                }
                b'(' | b'[' => depth = depth.saturating_add(1),
                b')' | b']' => depth = depth.saturating_sub(1),
                b',' if depth == 0 => return Some(idx),
                _ => {}
            },
        }
        previous = byte;
    }
    None
}

/// Index into [`Layers`]; `0` is the implicit layer of unlayered rules.
pub(crate) type LayerId = usize;

#[derive(Debug)]
struct Layer<'i> {
    parent: LayerId,
    /// Last segment of the layer name; `None` for the root and anonymous layers.
    name: Option<&'i str>,
}

/// Cascade layers in order of first appearance; children always follow their parent.
#[derive(Debug)]
pub(crate) struct Layers<'i>(Vec<Layer<'i>>);

impl<'i> Layers<'i> {
    fn new() -> Self {
        Layers(vec![Layer {
            parent: 0,
            name: None,
        }])
    }

    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }

    pub(crate) fn parent(&self, id: LayerId) -> LayerId {
        self.0[id].parent
    }

    /// Find or create the layer `name` (possibly dotted) within `parent`.
    /// Returns its id and whether any part of it is new.
    fn register(&mut self, parent: LayerId, name: &'i str) -> (LayerId, bool) {
        let mut id = parent;
        let mut created = false;
        for segment in name.split('.') {
            let child = self
                .0
                .iter()
                .position(|layer| layer.parent == id && layer.name == Some(segment));
            id = child.unwrap_or_else(|| {
                created = true;
                self.push(id, Some(segment))
            });
        }
        (id, created)
    }

    fn push(&mut self, parent: LayerId, name: Option<&'i str>) -> LayerId {
        self.0.push(Layer { parent, name });
        self.0.len().saturating_sub(1)
    }

    /// Rank layers in post-order of the layer tree: sublayers first, then the layer's own
    /// rules; the unlayered root comes last. Normal declarations in higher-ranked layers win,
    /// important ones in lower-ranked layers.
    #[allow(clippy::arithmetic_side_effects)]
    pub(crate) fn ranks(&self) -> Vec<u32> {
        let mut children: Vec<Vec<LayerId>> = vec![Vec::new(); self.0.len()];
        for (id, layer) in self.0.iter().enumerate().skip(1) {
            children[layer.parent].push(id);
        }
        let mut ranks = vec![0; self.0.len()];
        let mut rank = 0;
        let mut stack = vec![(0, 0)];
        while let Some((id, next)) = stack.last_mut() {
            if let Some(&child) = children[*id].get(*next) {
                *next += 1;
                stack.push((child, 0));
            } else {
                ranks[*id] = rank;
                rank += 1;
                stack.pop();
            }
        }
        ranks
    }
}

/// `@`-rules that the output keeps, in source offsets of the combined CSS.
#[derive(Debug)]
pub(crate) enum Span<'i> {
    /// Verbatim `@`-rule text and the layer it is in.
    AtRule { range: Range<usize>, layer: LayerId },
    /// `@layer a, b;`
    LayerStatement {
        start: usize,
        prelude: &'i str,
        layers: Vec<LayerId>,
    },
    /// `@layer a { ... }`; the range covers the block contents.
    LayerBlock {
        range: Range<usize>,
        prelude: &'i str,
        layer: LayerId,
        created: bool,
    },
}

impl Span<'_> {
    pub(crate) fn start(&self) -> usize {
        match self {
            Span::AtRule { range, .. } | Span::LayerBlock { range, .. } => range.start,
            Span::LayerStatement { start, .. } => *start,
        }
    }
}

/// Parser for a rule list: style rules are collected with their declarations, `@layer` blocks
/// are parsed recursively, and every other `@`-rule is rejected so the caller sees its span.
pub(crate) struct RuleParser<'d, 'i> {
    source: &'i str,
    declarations: &'d mut Vec<Declaration<'i>>,
    rules: &'d mut Vec<Rule<'i>>,
    spans: Option<&'d mut Vec<Span<'i>>>,
    layers: Layers<'i>,
    /// The layer of the block being parsed.
    layer: LayerId,
    /// Depth of enclosing conditional group rules (`@media` etc.). Their rules are not inlined
    /// and are kept verbatim, but the layers they declare are registered as if the condition
    /// held: css-inline cannot evaluate it.
    skipped: usize,
    /// Whether the source may contain `@layer`; otherwise conditional rules are not walked.
    has_layers: bool,
}

impl<'d, 'i> RuleParser<'d, 'i> {
    #[inline]
    pub(crate) fn new(
        source: &'i str,
        declarations: &'d mut Vec<Declaration<'i>>,
        rules: &'d mut Vec<Rule<'i>>,
        spans: Option<&'d mut Vec<Span<'i>>>,
    ) -> RuleParser<'d, 'i> {
        RuleParser {
            source,
            declarations,
            rules,
            spans,
            layers: Layers::new(),
            layer: 0,
            skipped: 0,
            has_layers: memchr::memchr_iter(b'@', source.as_bytes()).any(|idx| {
                source
                    .as_bytes()
                    .get(idx.saturating_add(1)..idx.saturating_add(6))
                    .is_some_and(|name| name.eq_ignore_ascii_case(b"layer"))
            }),
        }
    }

    pub(crate) fn into_layers(self) -> Layers<'i> {
        self.layers
    }

    /// Keep `span` in the output, unless an enclosing conditional rule is kept as a whole.
    fn record(&mut self, span: Span<'i>) {
        if self.skipped == 0 {
            if let Some(spans) = &mut self.spans {
                spans.push(span);
            }
        }
    }

    /// Keep the rejected `@`-rule `rule` that ends at byte `end` of the source.
    fn record_at_rule(&mut self, rule: &'i str, end: usize) {
        if self.spans.is_some() && rule.starts_with('@') {
            let start = offset_in(self.source, rule);
            self.record(Span::AtRule {
                range: start..end,
                layer: self.layer,
            });
        }
    }
}

fn offset_in(source: &str, slice: &str) -> usize {
    (slice.as_ptr() as usize).wrapping_sub(source.as_ptr() as usize)
}

/// Drive `items` to the end, recording the spans of rejected `@`-rules.
pub(crate) fn collect_rules<'i>(
    items: &mut cssparser::StyleSheetParser<'i, '_, RuleParser<'_, 'i>>,
) {
    // A rejected rule's block stays pending until the parser moves on; skipping
    // whitespace consumes it, so the rule's span ends there.
    // The error carries the rule's start, past any whitespace and comments.
    let mut rejected = None;
    loop {
        if let Some(rule) = rejected.take() {
            items.input.skip_whitespace();
            let end = items.input.position().byte_index();
            items.parser.record_at_rule(rule, end);
        }
        match items.next() {
            None => break,
            Some(Ok(())) => {}
            Some(Err((_, rule, _))) => rejected = Some(rule),
        }
    }
}

/// The declaration value without its `!important` flag, or `None` if it has no such flag.
///
/// The flag is `!` and `important` (any ASCII case) as the last tokens, with optional
/// whitespace and comments around and between them.
pub(crate) fn strip_important(value: &str) -> Option<&str> {
    // Common spelling, with the flag at the top level
    if let Some(rest) = value.trim_ascii_end().strip_suffix("!important") {
        if is_closed_plain(rest) {
            return Some(rest.trim_ascii_end());
        }
    }
    // Most values have no `!`, skip tokenizing them
    if !value.as_bytes().contains(&b'!') {
        return None;
    }
    let mut parser = cssparser::Parser::new(value);
    loop {
        if let Token::Delim('!') = parser.next().ok()? {
            // `!` is one byte, so it starts right before the current position
            let bang = parser.position().byte_index().saturating_sub(1);
            let state = parser.state();
            if parser.expect_ident_matching("important").is_ok() && parser.is_exhausted() {
                return Some(value[..bang].trim_ascii_end());
            }
            parser.reset(&state);
        }
    }
}

/// Whether `value` has no strings, comments, escapes or brackets other than balanced
/// parentheses, so what follows it is at the top level.
fn is_closed_plain(value: &str) -> bool {
    let mut depth = 0usize;
    for byte in value.bytes() {
        match byte {
            b'"' | b'\'' | b'\\' | b'/' | b'[' | b']' | b'{' | b'}' => return false,
            b'(' => depth = depth.saturating_add(1),
            b')' => {
                let Some(outer) = depth.checked_sub(1) else {
                    return false;
                };
                depth = outer;
            }
            _ => {}
        }
    }
    depth == 0
}

fn exhaust<'i>(input: &mut cssparser::Parser<'i>) -> &'i str {
    let start = input.position();
    while input.next().is_ok() {}
    input.slice_from(start)
}

/// Parser for qualified rules - a prelude + a simple {} block.
///
/// Usually these rules are a selector + list of declarations: `p { color: blue; font-size: 2px }`
impl<'i> cssparser::QualifiedRuleParser<'i> for RuleParser<'_, 'i> {
    type Prelude = &'i str;
    type QualifiedRule = ();
    type Error = ();

    fn parse_prelude(
        &mut self,
        input: &mut cssparser::Parser<'i>,
    ) -> Result<Self::Prelude, cssparser::ParseError<Self::Error>> {
        // Proceed with parsing until the end of the prelude.
        Ok(exhaust(input))
    }

    fn parse_block(
        &mut self,
        prelude: Self::Prelude,
        _: &ParserState,
        input: &mut cssparser::Parser<'i>,
    ) -> Result<Self::QualifiedRule, cssparser::ParseError<Self::Error>> {
        if self.skipped > 0 {
            exhaust(input);
            return Ok(());
        }
        let declarations = parse_declarations_into(input, self.declarations);
        self.rules.push(Rule {
            selectors: prelude,
            declarations,
            layer: self.layer,
        });
        Ok(())
    }
}

/// Prelude of an `@layer` rule: its raw text and the layer names in it.
pub(crate) struct LayerPrelude<'i> {
    raw: &'i str,
    names: Vec<&'i str>,
}

pub(crate) enum AtRulePrelude<'i> {
    Layer(LayerPrelude<'i>),
    /// A conditional group rule, whose block is a rule list.
    Conditional,
}

/// `<layer-name> = <ident> [ '.' <ident> ]*`, without whitespace.
fn parse_layer_name<'i>(
    input: &mut cssparser::Parser<'i>,
) -> Result<&'i str, cssparser::ParseError<()>> {
    input.skip_whitespace();
    let start = input.position();
    input.expect_ident()?;
    loop {
        let state = input.state();
        if !matches!(input.next_including_whitespace(), Ok(&Token::Delim('.'))) {
            input.reset(&state);
            break;
        }
        if !matches!(input.next_including_whitespace(), Ok(&Token::Ident(_))) {
            return Err(cssparser::ParseError::custom(()));
        }
    }
    let name = input.slice_from(start);
    input.expect_exhausted()?;
    Ok(name)
}

/// `@layer` rules are parsed. Conditional group rules are walked for the layers they declare
/// and then rejected like every other at-rule, as `style` attributes cannot hold them.
impl<'i> cssparser::AtRuleParser<'i> for RuleParser<'_, 'i> {
    type Prelude = AtRulePrelude<'i>;
    type AtRule = ();
    type Error = ();

    fn parse_prelude(
        &mut self,
        name: cssparser::CowRcStr<'i>,
        input: &mut cssparser::Parser<'i>,
    ) -> Result<Self::Prelude, cssparser::ParseError<Self::Error>> {
        if cssparser::match_ignore_ascii_case! { &name,
            "media" | "supports" | "container" | "document" | "-moz-document" => true,
            _ => false,
        } {
            exhaust(input);
            return Ok(AtRulePrelude::Conditional);
        }
        if !name.eq_ignore_ascii_case("layer") {
            return Err(cssparser::ParseError::custom(()));
        }
        let start = input.position();
        let names = if input.is_exhausted() {
            Vec::new()
        } else {
            input.parse_comma_separated(parse_layer_name)?
        };
        Ok(AtRulePrelude::Layer(LayerPrelude {
            raw: input.slice_from(start),
            names,
        }))
    }

    fn rule_without_block(
        &mut self,
        prelude: Self::Prelude,
        start: &ParserState,
    ) -> Result<Self::AtRule, ()> {
        let AtRulePrelude::Layer(prelude) = prelude else {
            return Err(());
        };
        if prelude.names.is_empty() {
            return Err(());
        }
        let layers = prelude
            .names
            .iter()
            .map(|name| self.layers.register(self.layer, name).0)
            .collect();
        self.record(Span::LayerStatement {
            start: start.position().byte_index(),
            prelude: prelude.raw,
            layers,
        });
        Ok(())
    }

    fn parse_block(
        &mut self,
        prelude: Self::Prelude,
        start: &ParserState,
        input: &mut cssparser::Parser<'i>,
    ) -> Result<Self::AtRule, cssparser::ParseError<Self::Error>> {
        let prelude = match prelude {
            AtRulePrelude::Layer(prelude) => prelude,
            AtRulePrelude::Conditional if !self.has_layers => {
                return Err(cssparser::ParseError::custom(()));
            }
            AtRulePrelude::Conditional => {
                self.skipped = self.skipped.saturating_add(1);
                collect_rules(&mut cssparser::StyleSheetParser::new(input, self));
                self.skipped = self.skipped.saturating_sub(1);
                // Rejected so the caller keeps the whole rule verbatim.
                return Err(cssparser::ParseError::custom(()));
            }
        };
        let (layer, created) = match prelude.names.as_slice() {
            [] => (self.layers.push(self.layer, None), true),
            [name] => self.layers.register(self.layer, name),
            _ => return Err(cssparser::ParseError::custom(())),
        };
        let outer = std::mem::replace(&mut self.layer, layer);
        collect_rules(&mut cssparser::StyleSheetParser::new(input, self));
        self.layer = outer;
        self.record(Span::LayerBlock {
            range: start.position().byte_index()..input.position().byte_index(),
            prelude: prelude.raw,
            layer,
            created,
        });
        Ok(())
    }
}

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

    fn parse_value(
        &mut self,
        name: Name<'i>,
        input: &mut cssparser::Parser<'i>,
        _declaration_start: &ParserState,
    ) -> Result<Self::Declaration, cssparser::ParseError<Self::Error>> {
        Ok((name, exhaust(input)))
    }
}

/// Parsing for at-rules, e.g: `@charset "utf-8";`
/// Since they are can not be inlined we use the default implementation, that rejects all at-rules.
impl<'i> cssparser::AtRuleParser<'i> for CSSDeclarationListParser {
    type Prelude = String;
    type AtRule = Declaration<'i>;
    type Error = ();
}

impl<'i> cssparser::RuleBodyItemParser<'i, Declaration<'i>, ()> for CSSDeclarationListParser {
    fn parse_declarations(&self) -> bool {
        true
    }

    fn parse_qualified(&self) -> bool {
        true
    }
}

impl<'i> cssparser::QualifiedRuleParser<'i> for CSSDeclarationListParser {
    type Prelude = String;
    type QualifiedRule = Declaration<'i>;
    type Error = ();
}

fn parse_declarations_into<'i>(
    input: &mut cssparser::Parser<'i>,
    declarations: &mut Vec<Declaration<'i>>,
) -> (usize, usize) {
    let mut parser = CSSDeclarationListParser;
    let parser = cssparser::RuleBodyParser::new(input, &mut parser);
    let start = declarations.len();
    for item in parser.flatten() {
        declarations.push(item);
    }
    (start, declarations.len())
}

#[cfg(test)]
mod tests {
    use super::{split_selector_list, top_level_comma, top_level_comma_scan};
    use test_case::test_case;

    #[test_case("a, b", Some(1); "plain")]
    #[test_case("a/b, c", Some(3); "slash without comment")]
    #[test_case("a/, b", Some(2); "slash before comma")]
    #[test_case("a*/, b", Some(3); "comment end without start")]
    #[test_case(r"a\,b, c", Some(4); "escaped comma")]
    #[test_case(r#"[x=","], p"#, Some(7); "comma in attribute")]
    #[test_case(":not(a, b), c", Some(10); "comma in parentheses")]
    #[test_case("a), b", Some(2); "unbalanced closing")]
    #[test_case(r#""a,b", c"#, Some(5); "comma in string")]
    #[test_case("'a,b', c", Some(5); "comma in single-quoted string")]
    #[test_case("/* , */ a, b", Some(9); "comma in comment")]
    #[test_case("a,", Some(1); "trailing")]
    #[test_case(",a", Some(0); "leading")]
    #[test_case("", None; "empty")]
    #[test_case("a b", None; "no comma")]
    #[test_case("(a, b", None; "unclosed parenthesis")]
    #[test_case("é, b", Some(2); "unicode before comma")]
    #[test_case("é(, b", None; "unicode before parenthesis")]
    fn comma(list: &str, expected: Option<usize>) {
        assert_eq!(top_level_comma_scan(list), expected);
        assert_eq!(top_level_comma(list), expected);
    }

    #[test_case("h1, h2", &["h1", " h2"]; "plain list")]
    #[test_case("h1", &["h1"]; "single")]
    #[test_case("", &[""]; "empty")]
    #[test_case("h1,", &["h1", ""]; "trailing comma")]
    #[test_case(".x:not(.a, .b), p", &[".x:not(.a, .b)", " p"]; "inside parentheses")]
    #[test_case(":is(a, :not(b, c)), d", &[":is(a, :not(b, c))", " d"]; "nested parentheses")]
    #[test_case(r#"[title="a,b"], p"#, &[r#"[title="a,b"]"#, " p"]; "double-quoted string")]
    #[test_case("[title='a,b'], p", &["[title='a,b']", " p"]; "single-quoted string")]
    #[test_case(r#"[title="a'b,c"], p"#, &[r#"[title="a'b,c"]"#, " p"]; "other quote in string")]
    #[test_case(r#"[title="("], p"#, &[r#"[title="("]"#, " p"]; "parenthesis in string")]
    #[test_case(r#"[title="a\",b"], p"#, &[r#"[title="a\",b"]"#, " p"]; "escaped quote")]
    #[test_case(r".a\,b, p", &[r".a\,b", " p"]; "escaped comma")]
    #[test_case(r".a\(, p", &[r".a\(", " p"]; "escaped parenthesis")]
    #[test_case(".é\\é, p", &[".é\\é", " p"]; "escaped non-ASCII")]
    #[test_case("h1 /* a, b */, h2", &["h1 /* a, b */", " h2"]; "comment")]
    #[test_case("h1, /* a, b", &["h1", " /* a, b"]; "unterminated comment")]
    fn split(list: &str, expected: &[&str]) {
        assert_eq!(split_selector_list(list).collect::<Vec<_>>(), expected);
    }

    #[test_case("red !important", Some("red"); "plain")]
    #[test_case("red!important  ", Some("red"); "no spaces")]
    #[test_case("!important", Some(""); "flag only")]
    #[test_case("red !IMPORTANT", Some("red"); "uppercase")]
    #[test_case("red ! /* c */ Important /* d */", Some("red"); "comments")]
    #[test_case("rgb(0, 0, 0) !important", Some("rgb(0, 0, 0)"); "after function")]
    #[test_case(r#""a" !important"#, Some(r#""a""#); "after string")]
    #[test_case("red", None; "no flag")]
    #[test_case("important", None; "no bang")]
    #[test_case("red !important blue", None; "not last")]
    #[test_case(r"red \!important", None; "escaped bang")]
    #[test_case(r#""a !important""#, None; "inside string")]
    #[test_case("red /* !important */", None; "inside comment")]
    #[test_case("rgb(0 !important", None; "inside unclosed function")]
    #[test_case("var(--a, rgb(0 0 0)) !important", Some("var(--a, rgb(0 0 0))"); "after nested functions")]
    #[test_case("a) (b !important", None; "inside block after stray parenthesis")]
    #[test_case("[a !important", None; "inside unclosed bracket")]
    #[test_case("red !importantx", None; "longer ident")]
    fn strip_important(value: &str, expected: Option<&str>) {
        assert_eq!(super::strip_important(value), expected);
    }
}