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

195 lines
4.1 KiB
Stylus
Raw Normal View History

2021-03-07 16:58:56 +01:00
_bem_context = {
_alias_map: {}
_alias_stacks: {
2021-03-19 12:00:07 +01:00
},
known_names: {}
2021-03-07 16:58:56 +01:00
}
2021-03-19 12:00:07 +01:00
2021-03-19 12:23:43 +01:00
bem_debug = false
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-19 12:00:07 +01:00
apply_alias_stack(name, level_name=null)
if level_name == null
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-19 12:00:07 +01:00
auto_name(delimiter, caller)
2021-03-12 16:22:23 +01:00
if delimiter == null
2021-03-19 12:00:07 +01:00
return caller
return split(delimiter, caller)[-1]
2021-03-07 16:06:21 +01:00
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
2021-03-19 12:00:07 +01:00
is_nested_in(parent_list=null)
if parent_list == null
return false
for parent in parent_list
if parent in called-from
return true
return false
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-19 12:00:07 +01:00
string_contains(smaller, greater)
if smaller == null || greater == null
return true
if length(smaller) > length(greater)
return false
for i in range(0, length(smaller) - 1)
//p(i substr(smaller, i, 1) substr(greater, i, 1))
if substr(smaller, i, 1) != substr(greater, i, 1)
return false
return true
contigious_index(call_stack)
for i in range(0, length(call_stack) - 1)
if !string_contains(call_stack[i+1], call_stack[i])
return i
return -1
filter(list, value_list)
filtered = ()
for element in list
if not (element in value_list)
push(filtered, element)
return filtered
2021-03-19 12:23:43 +01:00
RV-Level(name_delimiter, selector_callback, name=null)
2021-03-19 12:00:07 +01:00
call_stack = split(' ', ''+called-from)
call_stack = filter(call_stack, ('RV-Level' 'RV-Block' 'RV-Block--modifier' 'RV-Element' 'RV-Element--modifier' 'RV-Modifier'))
mixin_name = call_stack[0]
if name == null
name = auto_name(name_delimiter, mixin_name)
cindex = contigious_index(call_stack)
if bem_debug
--attempt mixin_name
--cindex cindex
--nested_in_super_parents nested_in_super_parents
// TODO Clear up this logic
2021-03-19 12:23:43 +01:00
if cindex != 0
2021-03-07 16:06:21 +01:00
if name == null
name = pop_last_alias()
name = lookup_alias(name)
apply_alias_stack(name)
2021-03-19 12:00:07 +01:00
selector_name = selector_callback(name)
if name == ''
selector_name = split(name_delimiter, selector_name)[0]
2021-03-07 16:06:21 +01:00
2021-03-19 12:23:43 +01:00
if cindex > 0 && !(name in _bem_context.known_names)
2021-03-19 12:00:07 +01:00
return
else
2021-03-19 12:00:07 +01:00
_bem_context.known_names[name] = name
define('current_selector', selector_name, true)
{selector_name}
{block}
2021-03-19 12:00:07 +01:00
if bem_debug
--mixin_name mixin_name
--name name
2021-03-19 12:23:43 +01:00
else
2021-03-19 12:00:07 +01:00
store_alias(name)
{block}
2021-03-19 12:00:07 +01:00
if bem_debug
--mixin_name mixin_name
--name name
2021-03-12 16:22:23 +01:00
2021-03-19 12:00:07 +01:00
RV-Block(name=null)
+RV-Level(null, @(name){
return '.'+name
}, name)
{block}
2021-03-19 13:51:34 +01:00
if not is_nested('RV-Block')
_bem_context._alias_map = {}
_bem_context._alias_stacks = {}
_bem_context.known_names = {}
2021-03-12 16:22:23 +01:00
RV-Block--shorthand(name)
+RV-Block(name)
{block}
2021-03-12 16:22:23 +01:00
2021-03-19 12:00:07 +01:00
RV-Element(name=null)
+RV-Level('__', @(name){
return '& ^[0]__'+name
}, name)
{block}
2021-03-19 12:00:07 +01:00
RV-Element--modifier(name=null)
+RV-Level('--', @(name){
return '&--'+name
2021-03-19 13:51:34 +01:00
}, name)
{block}
2021-03-19 12:00:07 +01:00
RV-Block--modifier(name=null)
+RV-Level('--', @(name){
return '&--'+name
}, name)
2021-03-07 16:06:21 +01:00
{block}