From d07b14f879d02abb1e6750cb1638fe97bb9d65d9 Mon Sep 17 00:00:00 2001 From: reverend Date: Sat, 9 Jan 2021 16:28:36 +0100 Subject: [PATCH] new attempt --- components/00_Global/_bemGenerator.styl | 107 +++++++----------------- 1 file changed, 29 insertions(+), 78 deletions(-) diff --git a/components/00_Global/_bemGenerator.styl b/components/00_Global/_bemGenerator.styl index a8b2a75..8fdc243 100644 --- a/components/00_Global/_bemGenerator.styl +++ b/components/00_Global/_bemGenerator.styl @@ -6,43 +6,15 @@ */ _css-prefix = 'RV' -_global_scope = { - _selector_blocks_to_generate: {} - _element-modifier: () - _additional-element-modifier-blocks: () - _last_element_name: null - _blocks_to_generate: () - _element_modifier_block_stack: () - _element_block_stack: () - _element_modifier_stack: {} -} -_block_building_lane = () -_element_building_lange = () +// Cache for Blocks +_last_block_name = null -_work_lane(lane, callback) - l = length(lane) - for _ in (0..l) - piece = pop(lane) - callback(piece) +_element_building_lane = {} +_element_block_stack = () -/* - * Gets the selector of the parent class - */ -_get_parent_selector() - return split(' ', selector())[-1] - - -/* - * Gets the selector of the current block - */ -_get_block_selector() - return unquote(split('__', _get_parent_selector())[0]) - - -_get_element_selector() - element--modifier = split('__', _get_parent_selector())[1] - return unquote(split('--', element--modifier)[0]) +_block_modifier_building_lane = {} +_block_modifier_block_stack = () /* @@ -50,21 +22,7 @@ _get_element_selector() * Block according to BEM */ RV-Block(name) - .{name} - push(_block_building_lane, { - elements: {} - block_modifiers: {} - }) - {block} - - for name, element in _global_scope['_selector_blocks_to_generate'] - & &__{name} - for current_block in element.block_list - {current_block} - for modifier_name, modifier_block_list in element.modifier_list - &--{modifier_name} - for current_block in modifier_block_list - {current_block} + tow(name, block) /* @@ -72,11 +30,25 @@ RV-Block(name) * according to BEM. */ RV-Block__Modifier(name) - if 'RV-Block__Modifier' in called-from - {block} + // Determining if we are inside a block or after a block + call_stack = _call_stack_till_first_block() + if 'RV-Block' in call_stack + // We are called inside a block + push(_block_modifier_block_stack, @block{block}) + + //Detect if the selector will be generated + //by an earlier call of RV-Element + if not 'RV-Block__Modifier' in call_stack + block_list = _pop_stack(_block_modifier_block_stack) + _block_modifier_building_lane[name] = { + block_list: block_list + } else - &{_get_block_selector()}--{name} - {block} + // We are not called inside a block + // Is there a block to attach to + if _last_block_name != null + .{_last_block_name}--{name} + {block} /* * Generates an CSS class for an element @@ -84,32 +56,11 @@ RV-Block__Modifier(name) * For more see RV-Element___Modifier */ RV-Element(name) - // Is there a block to build - block_index = (index(called-from, 'RV-Block')) - p(called-from block_index slice(called-from, 0, block_index)) - - if length(_block_building_lane) >= 1 - push(_block_building_lane, block) - - current_block = _block_building_lane[-1] - - //Detect if the selector will be generated - //by an earlier call of RV-Element - if not 'RV-Element' in called-from - // Saving accumulated modifiers and blocks - element = {} - element['block_list'] = _global_scope['_element_block_stack'] - element['modifier_list'] = _global_scope['_element_modifier_stack'] - - //Reseting - _global_scope['_element_block_stack'] = () - _global_scope['_element_modifier_stack'] = {} - - //Saving element - _global_scope['_selector_blocks_to_generate'][name] = element - _global_scope['_last_element_name'] = name + attach('RV-Block', unquote('__' + name), block) -/* + + +/* p(_block_building_lane) * Generates a CSS class for an element modifier * according to BEM. These are ment to be part of the * content block of an element and since child-mixins