Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Add support for complex nth-child selectors in omega()
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Ogle committed Apr 27, 2015
1 parent 0debfae commit 1ee7a1d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/assets/stylesheets/functions/_private.scss
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,36 @@

@return $opposite-direction;
}


@function to-number($string) {
$string: str-replace($string, " ", "");
$strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$numbers: 0 1 2 3 4 5 6 7 8 9;
$result: 0;

@for $i from 1 through str-length($string) {
$character: str-slice($string, $i, $i);
$index: index($strings, $character);

@if not $index {
@warn "Unknown character `#{$character}`.";
@return false;
}

$number: nth($numbers, $index);
$result: $result * 10 + $number;
}

@return $result;
}

@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);

@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}

@return $string;
}
27 changes: 26 additions & 1 deletion app/assets/stylesheets/grid/_omega.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,32 @@
margin-#{$direction}: 0;
}

@if type-of($query) == number and unit($query) == "n" {
@if type-of($query) == string {
$query: str-replace($query, " ", "");
$operator: false;

@if str_index($query, "+") {
$operator: "+";
} @else if str_index($query, "-") {
$operator: "-";
}

@if $operator {
$operator-index: str_index($query, $operator);
$first: str-slice($query, 0, ($operator-index - 1));
$last: to-number(str-slice($query, ($operator-index + 1), -1));
@if $operator == "+" {
$last: $last + 1;
} @else if $operator == "-" {
$last: $last - 1;
}
$nth: "#{$first}#{$operator}#{$last}";

&:nth-child(#{$nth}) {
clear: $opposite-direction;
}
}
} @else if type-of($query) == number && unit($query) == "n" {
&:nth-child(#{$query}+1) {
clear: $opposite-direction;
}
Expand Down
14 changes: 14 additions & 0 deletions spec/neat/omega_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
it "removes right margin of nth-child(4n+1)" do
expect(".omega-complex-nth:nth-child(4n+1)").to have_rule("margin-right: 0")
end

it "adds clear to nth-child('4n+2')" do
expect(".omega-complex-nth:nth-child(4n+2)").to have_rule("clear: left")
end
end

context "with argument ('3n-1')" do
it "removes right margin of nth-child(3n-1)" do
expect(".omega-complex-nth-negative:nth-child(3n-1)").to have_rule("margin-right: 0")
end

it "adds clear to nth-child('3n-0')" do
expect(".omega-complex-nth-negative:nth-child(3n-0)").to have_rule("clear: left")
end
end

context "when called inside an RTL row" do
Expand Down
4 changes: 4 additions & 0 deletions test/omega.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
@include omega("4n+1");
}

.omega-complex-nth-negative {
@include omega("3n-1");
}

section {
@include row($direction: RTL);

Expand Down

0 comments on commit 1ee7a1d

Please sign in to comment.