Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle extra whitespace in className #9

Merged
merged 3 commits into from
Oct 26, 2014
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
28 changes: 25 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ function unique(el) {
return selector;
};

/**
* Get class names for an element
*
* @pararm {Element} el
* @return {Array}
*/

function getClassNames(el) {
var className = el.getAttribute('class');
if (!className || !className.length) { return []; }

// remove duplicate whitespace
className = className.replace(/\s+/g, ' ');

// trim leading and trailing whitespace
className = className.replace(/^\s+|\s+$/g, '');

// split into separate classnames
return className.split(' ');
}

/**
* CSS selectors to generate unique selector for DOM element
*
Expand All @@ -60,11 +81,12 @@ function selectors(el) {
} else {
// Otherwise, use tag name
label = el.tagName.toLowerCase();
var className = el.getAttribute('class');

var classNames = getClassNames(el);

// Tag names could use classes for specificity
if (className && className.length) {
label += '.' + className.split(' ').join('.');
if (classNames.length) {
label += '.' + classNames.join('.');
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "test"
},
"scripts": {
"test": "make test"
"test": "make"
},
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ <h3>Test Fixture <small>(for Mocha)</small></h3>
Lorem <em>ipsum</em> dolor sit amet, consectetur adipisicing elit.
</p>

<div class=" classnames with extra space ">
<span></span>
</div>

<ul id="nav">
<li class="first item">Item 1</li>
<li class="collapsed item">
Expand Down
3 changes: 2 additions & 1 deletion test/unique-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('unique-selector', function() {
'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"]',
'HTML > BODY > DIV#fixture > FORM > SELECT > OPTION[selected]': '#fixture > form > select[name="state"] > option[value="TX"]',
'HTML > BODY > DIV#fixture > FORM > P > LABEL > INPUT[checked]': '#fixture > form > p > label > input[name="computer"][value="1"]'
'HTML > BODY > DIV#fixture > FORM > P > LABEL > INPUT[checked]': '#fixture > form > p > label > input[name="computer"][value="1"]',
'HTML > BODY > DIV#fixture > DIV.classnames.with.extra.space > SPAN': '#fixture > div.classnames.with.extra.space > span'
};

for (selector in selectors) {
Expand Down