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

113 lines
2.3 KiB
Stylus

_bem_context = {
_alias_map: {}
_alias_stack: () ()
}
work_stack(stack, callback)
while length(stack) > 0
entry = pop(stack)
if entry != ()
callback(entry)
set_alias(alias, name)
if name != null && alias != null
_bem_context._alias_map[alias] = name
lookup_alias(name)
if name in _bem_context._alias_map
return _bem_context._alias_map[name]
else
return name
store_alias(alias)
if alias != null
push(_bem_context._alias_stack, alias)
pop_last_alias()
return pop(_bem_context._alias_stack)
apply_alias_stack(name)
work_stack(_bem_context._alias_stack, @(element){
set_alias(element, name)
})
auto_name(delimiter)
if length(called-from) < 3
return null
name = split(delimiter, called-from[2])[-1]
if name in ('RV-Element' 'RV-Block' 'RV-Element--modifier' 'RV-Block--modifier')
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
not_nested_in(parent_name)
return !(parent_name in called-from)
RV-Level(name, name_delimiter, allowed_parent_levels, selector_callback)
if name == null
name = auto_name(name_delimiter)
not_in_parent = true
for parent_level in allowed_parent_levels
not_in_parent = not_in_parent && not_nested_in(parent_level)
if is_nested_in('RV-Squash') || not_in_parent
{block}
else if is_nested()
store_alias(name)
{block}
else
if name == null
name = pop_last_alias()
name = lookup_alias(name)
apply_alias_stack(name)
{selector_callback(name)}
{block}
RV-Block(name)
.{name}
{block}
_bem_context._alias_map = {}
_bem_context._alias_stack = () ()
RV-Element(name=null)
+RV-Level(name, '__', ('RV-Block' 'RV-Block--modifier'), @(name){
if is_nested_in('RV-Block--modifier') {
return '& ^[-1]__' + name
}
else {
return '& &__' + name
}
})
{block}
RV-Element--modifier(name=null)
+RV-Level(name, '--', ('RV-Element'), @(name){return '&--' + name})
{block}
RV-Block--modifier(name=null)
+RV-Level(name, '--', ('RV-Block'), @(name){return '&--' + name})
{block}
RV-Squash()
{block}