Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix nth-child calculation

  • Loading branch information...
commit 504480ce3d43349c4b89e6be0d02b81107c3dbcb 1 parent f0a0b08
@ericclemmons authored
Showing with 13 additions and 5 deletions.
  1. +3 −0  index.js
  2. +3 −3 test/index.html
  3. +7 −2 test/unique-selector.js
View
3  index.js
@@ -25,6 +25,9 @@ function unique(el) {
if (matches.length > 1) {
for (var i = 0; i < matches.length; i++) {
if (el === matches[i]) {
+ // Recalculate index based on position of el amongst siblings
+ i = [].indexOf.call(el.parentNode.children, el);
+
selector += ':nth-child(' + (i + 1) +')';
break;
}
View
6 test/index.html
@@ -19,7 +19,7 @@
<body>
<div id="mocha"></div>
- <div id="fixture" style="display: none;">
+ <div id="fixture">
<h3>Test Fixture <small>(for Mocha)</small></h3>
<p>
@@ -28,14 +28,14 @@
<ul id="nav">
<li class="first item">Item 1</li>
- <li class="item">
+ <li class="collapsed item">
Item 2
<ul id="nested">
<li class="child"><img alt="Some Title" /></li>
</ul>
</li>
- <li class="last item">Item 3</li>
+ <li class="last collapsed item">Item 3</li>
</ul>
</div>
View
9 test/unique-selector.js
@@ -24,14 +24,19 @@ describe('unique-selector', function() {
});
var selectors = {
+ // Selector: expected
'HTML > BODY':
'html > body',
'HTML > BODY > DIV#fixture':
'#fixture',
'HTML > BODY > DIV#fixture > H3 > SMALL':
'#fixture > h3 > small',
- 'HTML > BODY > DIV#fixture > UL#nav > LI.item:nth-child(2)':
- '#nav > li.item:nth-child(2)',
+ 'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(1)':
+ '#nav > li.first.item',
+ 'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(2)':
+ '#nav > li.collapsed.item:nth-child(2)',
+ 'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(3)':
+ '#nav > li.last.collapsed.item',
'HTML > BODY > DIV#fixture > UL#nav > LI.item > UL#nested > LI.child > IMG':
'#nested > li.child > img[alt="Some Title"]'
};
Please sign in to comment.
Something went wrong with that request. Please try again.