diff --git a/index.js b/index.js index 70293ae..d28d0e2 100644 --- a/index.js +++ b/index.js @@ -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 * @@ -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('.'); } } diff --git a/package.json b/package.json index aba4e92..6c79b55 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "test" }, "scripts": { - "test": "make test" + "test": "make" }, "repository": { "type": "git", diff --git a/test/index.html b/test/index.html index 48f418a..bb6a343 100644 --- a/test/index.html +++ b/test/index.html @@ -20,6 +20,10 @@