scss-library/components/00_Tools/_bemGenerator.styl

165 lines
3.7 KiB
Stylus
Raw Normal View History

2021-03-07 16:58:56 +01:00
_bem_context = {
_alias_map: {}
_alias_stacks: {
}
2021-03-07 16:58:56 +01:00
}
2021-03-07 16:06:21 +01:00
work_stack(stack, callback)
while length(stack) > 0
2021-03-07 16:58:56 +01:00
entry = pop(stack)
2021-01-17 15:57:17 +01:00
if entry != ()
callback(entry)
2021-01-06 19:01:29 +01:00
2021-03-07 16:06:21 +01:00
set_alias(alias, name)
if name != null && alias != null
2021-03-07 16:58:56 +01:00
_bem_context._alias_map[alias] = name
2021-03-07 16:06:21 +01:00
lookup_alias(name)
2021-03-07 16:58:56 +01:00
if name in _bem_context._alias_map
return _bem_context._alias_map[name]
2021-03-07 16:06:21 +01:00
else
return name
2021-03-07 16:06:21 +01:00
store_alias(alias)
level_name = called-from[1]
if not level_name in _bem_context._alias_stacks
_bem_context._alias_stacks[level_name] = () ()
2021-03-07 16:06:21 +01:00
if alias != null
push(_bem_context._alias_stacks[level_name], alias)
2021-01-17 15:57:17 +01:00
2021-03-07 16:06:21 +01:00
pop_last_alias()
level_name = called-from[1]
if not level_name in _bem_context._alias_stacks
return null
return pop(_bem_context._alias_stacks[level_name])
2021-03-07 16:06:21 +01:00
apply_alias_stack(name)
level_name = called-from[1]
if level_name in _bem_context._alias_stacks
work_stack(_bem_context._alias_stacks[level_name], @(element){
set_alias(element, name)
})
2021-01-07 00:15:19 +01:00
2021-03-07 16:06:21 +01:00
auto_name(delimiter)
if length(called-from) < 3
return null
2021-03-12 16:22:23 +01:00
block_name = called-from[2]
if delimiter == null
return block_name
name = split(delimiter, block_name)[-1]
if name in ('RV-Element' 'RV-Block' 'RV-Block--shorthand' 'RV-Element--modifier' 'RV-Block--modifier' 'RV-Generate')
2021-03-07 16:06:21 +01:00
return null
return name
is_nested(mixin_name=null)
_free_hits = 0
if mixin_name == null
shift(called-from)
mixin_name = shift(called-from)
else
_free_hits = 1
for parent in called-from
if parent == mixin_name
if _free_hits == 0
return true
else
_free_hits = _free_hits - 1
return false
is_nested_in(parent_name)
return parent_name in called-from
2021-01-17 15:57:17 +01:00
2021-03-07 16:06:21 +01:00
not_nested_in(parent_name)
return !(parent_name in called-from)
is_any_parent_nested()
//RV-Level RV-Element--modifier RV-Element RV-Block--modifier RV-BreakPoint--horizontal RV-Block--modifier RV-Fan--horizontal RV-Block
for i in (length(called-from)...0)
i = i - 1
if i != index(called-from, called-from[i])
return true
return false
2021-03-07 16:06:21 +01:00
2021-03-12 16:22:23 +01:00
RV-Level(name_delimiter, allowed_parent_levels=null, selector_callback, squash_if_parent_nested=false)
name = auto_name(name_delimiter)
p(called-from)
2021-03-07 16:06:21 +01:00
not_in_parent = true
for parent_level in allowed_parent_levels
not_in_parent = not_in_parent && not_nested_in(parent_level)
2021-03-12 16:22:23 +01:00
if not_in_parent //|| is_nested_in('RV-Squash')
2021-01-17 16:33:09 +01:00
{block}
2021-03-07 16:06:21 +01:00
else if is_nested()
store_alias(name)
2021-01-17 16:33:09 +01:00
{block}
else
2021-03-07 16:06:21 +01:00
if name == null
name = pop_last_alias()
name = lookup_alias(name)
apply_alias_stack(name)
if squash_if_parent_nested && is_any_parent_nested()
{block}
else
selector_name = selector_callback(name)
if name == ''
selector_name = split(name_delimiter, selector_name)[0]
{selector_name}
{block}
2021-01-17 15:57:17 +01:00
2021-03-12 16:22:23 +01:00
// RV-Fan--horizontal RV-Fan--horizontal __ Base -- wurst
// RV-BreakPoint__Item--outside RV-Fan--horizontal__Base RV-Fan--horizontal
2021-01-17 15:57:17 +01:00
2021-03-07 16:06:21 +01:00
RV-Block(name)
.{name}
{block}
2021-03-07 16:58:56 +01:00
_bem_context._alias_map = {}
_bem_context._alias_stacks = {}
2021-03-12 16:22:23 +01:00
RV-Generate()
{block}
_bem_context._alias_map = {}
_bem_context._alias_stacks = {}
RV-Block()
+RV-Level(null, ('RV-Generate'), @(name){
define('current_block', name, true)
return '.'+name
})
{block}
RV-Block--shorthand(name)
+RV-Block(name)
{block}
2021-03-12 16:22:23 +01:00
RV-Element()
+RV-Level('__', ('RV-Generate' 'RV-Block--modifier'), @(name){
return '& .' + current_block +'__' + name
2021-03-07 16:06:21 +01:00
})
{block}
2021-03-12 16:22:23 +01:00
RV-Element--modifier()
+RV-Level('--', ('RV-Element'), @(name){return '&--' + name}, true)
{block}
2021-03-12 16:22:23 +01:00
RV-Block--modifier()
+RV-Level('--', ('RV-Generate' 'RV-Block--shorthand'), @(name){
'.' + current_block +'--' + name
}, true)
if is_nested_in('RV-Block--shorthand') && length(called-from) > 0
mixin_name = split('--', called-from[0])[0]
convert(unquote(mixin_name)+'()')
2021-03-07 16:06:21 +01:00
{block}
RV-Squash()
{block}