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

Add support for complex nth-child selectors in omega() #340

Merged
merged 1 commit into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/assets/stylesheets/functions/_private.scss
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,39 @@

@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 {
$first: str-slice($string, 1, $index - 1);
$last-slice: str-slice($string, $index + str-length($search));
$last: str-replace($last-slice, $search, $replace);
@return $first + $replace + $last;
}

@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
35 changes: 29 additions & 6 deletions spec/neat/omega_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,59 @@

context "with no argument" do
it "removes right margin" do
expect(".omega-default").to have_rule("margin-right: 0")
selector = ".omega-default"
expect(selector).to have_rule("margin-right: 0")
end
end

context "with argument (4n)" do
it "removes right margin of nth-child(4n)" do
expect(".omega-nth-default:nth-child(4n)").to have_rule("margin-right: 0")
selector = ".omega-nth-default:nth-child(4n)"
expect(selector).to have_rule("margin-right: 0")
end

it "adds clear to nth-child(4n+1)" do
expect(".omega-nth-default:nth-child(4n+1)").to have_rule("clear: left")
selector = ".omega-nth-default:nth-child(4n+1)"
expect(selector).to have_rule("clear: left")
end
end

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

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

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

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

context "when called inside an RTL row" do
context "with no argument" do
it "removes left margin" do
expect("section .omega-default-left").to have_rule("margin-left: 0")
selector = "section .omega-default-left"
expect(selector).to have_rule("margin-left: 0")
end
end

context "with argument (4n block)" do
it "removes left margin of nth-child(4n)" do
expect("section .omega-nth-default-left:nth-child(4n)").to have_rule("margin-left: 0")
selector = "section .omega-nth-default-left:nth-child(4n)"
expect(selector).to have_rule("margin-left: 0")
end
end
end
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