use super::r#type::FeatureWalkMode;
use super::{FeatureResult, MinifyFeature, SharedFeature};
use crate::ast::arena::AstArena;
use crate::ast::minify::MinifyContext;
use crate::ast::mixins::utils::move_result;
use crate::ast::types::{EnumAstNodeStatus, EnumToken};
use crate::options::ParserOptions;
use crate::parser::declaration::list::{DeclarationMemo, PropertyList};
use crate::validation::config::get_syntax_rule;
use crate::validation::parser::typedef::ValidationSyntaxGroupEnum;
use std::cell::RefCell;
use std::rc::Rc;
pub struct ComputeShorthandFeature;
const ACCEPT: [u8; 3] = [EnumToken::RuleNodeType, EnumToken::AtRuleNodeType, EnumToken::KeyframesRuleNodeType];
impl ComputeShorthandFeature {
pub fn register(options: &ParserOptions, features: &mut Vec<SharedFeature>) {
if options.compute_shorthand == Some(true) {
features.push(Rc::new(RefCell::new(ComputeShorthandFeature)));
}
}
}
impl MinifyFeature for ComputeShorthandFeature {
fn accept(&self) -> Option<&[u8]> {
Some(&ACCEPT)
}
fn ordering(&self) -> i32 {
5
}
fn process_mode(&self) -> u32 {
FeatureWalkMode::Post
}
fn run(&mut self, arena: &AstArena, ast: u32, options: &ParserOptions, _parent: u32, context: &mut MinifyContext, _mode: u32) -> FeatureResult {
if arena.flags.get(ast) & EnumAstNodeStatus::HasChildren == 0 || arena.first_child.get(ast) == 0 {
return FeatureResult::None;
}
let t = |x: u32| arena.data_types.get(x);
let memo: Rc<RefCell<DeclarationMemo>> = context.get_or_insert_with("declarations", || Rc::new(RefCell::new(DeclarationMemo::default()))).clone();
let mut do_not_sort = false;
let mut descriptors = None;
let mut is_function = false;
if t(ast) == EnumToken::AtRuleNodeType {
match &*arena.resolve_string(arena.name.get(ast)) {
"function" => {
do_not_sort = true;
is_function = true;
descriptors = get_syntax_rule(ValidationSyntaxGroupEnum::AtRules, "@function").and_then(|r| r.get_property_descriptors());
}
"mixin" => do_not_sort = true,
_ => {}
}
}
let mut child = arena.first_child.get(ast);
while child != 0 {
let mut list = PropertyList::new(options, Some(memo.clone()), descriptors.clone());
let mut has_declarations = false;
let mut first = 0u32;
let mut boundary = 0u32;
while child != 0 {
let mut last = child;
if !do_not_sort {
while arena.next_sibling.get(last) != 0 && t(last) == EnumToken::CommentNodeType {
last = arena.next_sibling.get(last);
}
}
let next = arena.next_sibling.get(last);
if t(last) == EnumToken::AtRuleNodeType
&& (arena.flags.get(last) & EnumAstNodeStatus::HasChildren == 0 || arena.string_equals(arena.name.get(last), "apply"))
{
boundary = child;
child = next;
break;
}
if do_not_sort || t(last) == EnumToken::DeclarationNodeType {
has_declarations = true;
let mut node = child;
while node != next {
let next_node = arena.next_sibling.get(node);
arena.detach(node);
list.add_declaration(arena, node);
node = next_node;
}
} else if first == 0 {
first = child;
}
child = next;
}
if has_declarations {
let reference = if first != 0 { first } else { boundary };
for node in list.iterate(arena) {
if reference == 0 {
arena.append_child(ast, node);
} else {
arena.insert_before(reference, &[node]);
}
}
}
}
if is_function {
move_result(arena, ast);
}
FeatureResult::Node(ast)
}
}