Skip to main content

rust_css_parser/ast/features/
mod.rs

1//! Port of `src/ast/features/index.ts` and of the `MinifyFeature` interface.
2//!
3//! A feature is registered (`static register(options)`) into the feature list of a minification when its option is
4//! set: the list holds shared instances (`Rc<RefCell<..>>`), a feature keeps its state between the nodes.
5
6pub mod calc;
7#[path = "if_.rs"]
8pub mod r#if;
9pub mod inlinecssvariables;
10pub mod mixin;
11pub mod prefix;
12pub mod shorthand;
13pub mod transform;
14pub mod r#type;
15
16use crate::ast::arena::AstArena;
17use crate::ast::minify::MinifyContext;
18use crate::options::ParserOptions;
19use std::cell::RefCell;
20use std::rc::Rc;
21
22/// result of [`MinifyFeature::run`]: the replacement of the node
23pub enum FeatureResult {
24    /// `null`: the node is kept
25    None,
26    Node(u32),
27    Nodes(Vec<u32>),
28}
29
30/// minification feature (`MinifyFeature`)
31pub trait MinifyFeature {
32    /// accepted node types, `None`: all
33    fn accept(&self) -> Option<&[u8]> {
34        None
35    }
36    fn ordering(&self) -> i32;
37    fn process_mode(&self) -> u32;
38    fn run(&mut self, arena: &AstArena, ast: u32, options: &ParserOptions, parent: u32, context: &mut MinifyContext, mode: u32) -> FeatureResult;
39    /// true if the feature has a `cleanup()` method
40    fn has_cleanup(&self) -> bool {
41        false
42    }
43    fn cleanup(&mut self, _arena: &AstArena, _ast: u32, _options: &ParserOptions, _context: &mut MinifyContext, _mode: u32) {}
44}
45
46/// a registered feature
47pub type SharedFeature = Rc<RefCell<dyn MinifyFeature>>;
48
49/// register the features enabled by the options, in the order of the exports of `features/index.ts` (the classes
50/// have no static ordering: the typescript sort keeps that order).
51pub fn register_features(options: &ParserOptions, features: &mut Vec<SharedFeature>) {
52    prefix::ComputePrefixFeature::register(options, features);
53    mixin::EvaluateMixinFeature::register(options, features);
54    inlinecssvariables::InlineCssVariablesFeature::register(options, features);
55    shorthand::ComputeShorthandFeature::register(options, features);
56    calc::ComputeCalcExpressionFeature::register(options, features);
57    transform::TransformCssFeature::register(options, features);
58    r#if::ExpandIfFeature::register(options, features);
59}