Skip to main content

rust_css_parser/ast/mixins/
utils.rs

1//! Port of `src/ast/mixins/utils.ts`: helpers shared by the minification features.
2
3use crate::ast::arena::AstArena;
4use crate::ast::types::EnumToken;
5
6/// true if the node is a `@mixin` or a `@function` rule, or is located inside one of them. the body of a mixin or a
7/// custom function is a template: it is evaluated when the mixin is applied or the function is called
8pub fn is_in_template(arena: &AstArena, node: u32) -> bool {
9    let mut node = node;
10    while node != 0 {
11        if arena.data_types.get(node) == EnumToken::AtRuleNodeType
12            && (arena.string_equals(arena.name.get(node), "mixin") || arena.string_equals(arena.name.get(node), "function"))
13        {
14            return true;
15        }
16        node = arena.parents.get(node);
17    }
18    false
19}
20
21/// move the last `result` descriptor of a custom function after its conditional rules: there is no early return.
22/// `result: 16px; @media (...) { result: 20px }` -> `@media (...) { result: 20px } result: 16px`
23pub fn move_result(arena: &AstArena, node: u32) {
24    let mut last = arena.last_child.get(node);
25    // the last result
26    while last != 0 && !(arena.data_types.get(last) == EnumToken::DeclarationNodeType && arena.string_equals(arena.name.get(last), "result")) {
27        last = arena.previous_sibling.get(last);
28    }
29    if last == 0 {
30        return;
31    }
32    let mut next = arena.next_sibling.get(last);
33    while next != 0 {
34        if arena.data_types.get(next) == EnumToken::AtRuleNodeType {
35            arena.detach(last);
36            arena.append_child(node, last);
37            return;
38        }
39        next = arena.next_sibling.get(next);
40    }
41}