Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created July 9, 2014 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseppstein/373293534f1fb33b9183 to your computer and use it in GitHub Desktop.
Save chriseppstein/373293534f1fb33b9183 to your computer and use it in GitHub Desktop.
@function index-of-last-class($selector) {
$index: null;
$counter: 1;
@each $component in $selector {
@if str-index($component, ".") == 1 {
$index: $counter;
}
$counter: $counter + 1;
}
@return $index;
}
@function list-slice($list, $start, $end: length($list)) {
$new-list: ();
@for $i from $start through $end {
$new-list: append($new-list, nth($list, $i), list-separator($list));
}
@return $new-list;
}
@mixin increased-specificity($selector) {
@if length($selector) > 1 {
@warn "Does not work on compound selectors at this time. Keeping the first.";
}
$selector: nth($selector, 1);
$class-index: index-of-last-class($selector);
@if $class-index {
$before: list-slice($selector, 1, $class-index);
$at: nth($selector,$class-index);
$after: list-slice($selector, $class-index + 1, length($selector));
$after: if(length($after) > 0, $after, null);
@at-root #{$before}#{$at} #{$after} {
@content;
}
}
@else {
@warn "Cannot increase the specificity of #{$selector}. It does not include a class name.";
@content;
}
}
.increase .me .please {
@include increased-specificity(&) {
content: wow;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment