Skip to content

Commit

Permalink
fix: classList.toString don't remove extra whitespace characters (#29)
Browse files Browse the repository at this point in the history
* fix: classList.toString don't remove extra whitespace characters

* chore: delete old parts of build script, delete unsuported require param, add latest preset

* fix: add polyfill for array.from

* chore: remove polyfill
  • Loading branch information
jeetiss authored and AvraamMavridis committed Jul 4, 2017
1 parent dfb6598 commit fbfaaf4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015","stage-0"]
"presets": ["latest"]
}
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"test": "test"
},
"scripts": {
"build": "webpack -p --config --progress --colors",
"test": "npm run compile && mocha --ignore-leaks --compilers js:babel-register --require babel-polyfill",
"compile": "babel --presets es2015,stage-0 --require babel-polyfill -d lib/ src/",
"build": "npm run compile",
"test": "npm run compile && mocha --ignore-leaks --compilers js:babel-register",
"compile": "babel -d lib/ src/",
"prepublish": "npm run compile",
"watch": "npm-scripts-watcher"
},
Expand Down Expand Up @@ -41,9 +41,7 @@
"devDependencies": {
"babel-cli": "^6.1.18",
"babel-core": "^6.1.18",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.1.18",
"babel-preset-stage-0": "^6.1.18",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.3.13",
"chai": "^3.5.0",
"component": "~0.10.1",
Expand All @@ -55,7 +53,6 @@
"jsdom": "^8.3.0",
"mocha": "~1.7.4",
"mocha-jsdom": "^1.1.0",
"mocha-phantomjs": "~1.1.1",
"open-browser-webpack-plugin": "0.0.2"
"mocha-phantomjs": "~1.1.1"
}
}
19 changes: 6 additions & 13 deletions src/getClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,22 @@
*/
export function getClasses( el )
{
let classNames;

try
if( !el.hasAttribute( 'class' ) )
{
classNames = el.classList.toString().split( ' ' );
return [];
}
catch ( e )
{
if( !el.hasAttribute( 'class' ) )
{
return [];
}

try {
return Array.prototype.slice.call( el.classList );
} catch (e) {
let className = el.getAttribute( 'class' );

// remove duplicate and leading/trailing whitespaces
className = className.trim().replace( /\s+/g, ' ' );

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

return classNames;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/unique-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ describe( 'Unique Selector Tests', () =>
expect( uniqueSelector ).to.equal( '.cc.cx' );
} );

it( 'Classes with newline', () =>
{
$( 'body' ).get( 0 ).innerHTML = ''; //Clear previous appends
$( 'body' ).append( '<div class="test2\n ca\n cb\n cc\n cd\n cx"></div><div class="test2\n ca\n cb\n cc\n cd\n ce"></div><div class="test2\n ca\n cb\n cc\n cd\n ce"></div><div class="test2\n ca\n cb\n cd\n ce\n cf\n cx"></div>' );
const findNode = $( 'body' ).find( '.test2' ).get( 0 );
const uniqueSelector = unique( findNode );
expect( uniqueSelector ).to.equal( '.cc.cx' );
} );

it( 'Tag', () =>
{
Expand Down

0 comments on commit fbfaaf4

Please sign in to comment.