Skip to content

Instantly share code, notes, and snippets.

@evenicoulddoit
Created November 16, 2015 14:21
Show Gist options
  • Save evenicoulddoit/d43bfb53414c089d8f3d to your computer and use it in GitHub Desktop.
Save evenicoulddoit/d43bfb53414c089d8f3d to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
<div class="class">asd</div>
// ----
// Sass (v3.4.14)
// Compass (v1.0.3)
// ----
$bem-element-separator: '__';
$bem-modifier-separator: '--';
@function selectorToString($selector) {
$selector: inspect($selector); //cast to string
$selector: str-slice($selector, 2, -2); //remove brackets
@return $selector;
}
@function lastIndex($str, $needle, $from: 0) {
$index: str-index($str, $needle);
@if(not $index) {
@return $from;
}
@else {
$str: str-slice($str, $index + 1);
@return lastIndex($str, $needle, $from + $index);
}
}
@function containsModifier($selector) {
$selector: selectorToString($selector);
@if(str-index($selector, $bem-modifier-separator)) {
@return true;
}
@else {
@return false;
}
}
@function lastElement($str) {
$last-index: lastIndex($str, " ");
$str: if($last-index, str-slice($str, $last-index + 1), $str);
@return $str;
}
@function getBlock($selector) {
$selector: selectorToString($selector);
$element: lastElement($selector);
@debug($element);
$modifierStart: str-index($element, $bem-modifier-separator) - 1;
@return str-slice($element, 0, $modifierStart);
}
@mixin block($block) {
.#{$block} {
@content;
}
}
@mixin element($element) {
$selector: &;
@if(containsModifier($selector)) {
$block: getBlock($selector);
@debug($block);
@at-root {
#{$selector} {
#{$block+$bem-element-separator+$element} {
@content;
}
}
}
}
@else {
@at-root {
#{$selector+$bem-element-separator+$element} {
@content;
}
}
}
}
@mixin modifier($modifier) {
@at-root {
#{&}#{$bem-modifier-separator+$modifier} {
@content;
}
}
}
<div class="class">asd</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment