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

138 lines
3.0 KiB
Stylus

_bem_context = {
_alias_map: {}
_alias_stacks: {
}
}
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)
level_name = called-from[1]
if not level_name in _bem_context._alias_stacks
_bem_context._alias_stacks[level_name] = () ()
if alias != null
push(_bem_context._alias_stacks[level_name], alias)
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])
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)
})
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)
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
RV-Level(name, name_delimiter, allowed_parent_levels, selector_callback, squash_if_parent_nested=false)
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 not_in_parent || is_nested_in('RV-Squash')
{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)
if squash_if_parent_nested && is_any_parent_nested()
{block}
else
{selector_callback(name)}
{block}
RV-Block(name)
.{name}
{block}
_bem_context._alias_map = {}
_bem_context._alias_stacks = {}
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}, true)
{block}
RV-Block--modifier(name=null)
+RV-Level(name, '--', ('RV-Block'), @(name){return '&--' + name}, true)
{block}
RV-Squash()
{block}