Skip to content

Commit

Permalink
@baloneysandwiches added a hasClass method. closes #1464
Browse files Browse the repository at this point in the history
  • Loading branch information
albell authored and heff committed Sep 29, 2014
1 parent 2a71d69 commit fd181aa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGELOG

## HEAD (Unreleased)
* @deedos added a Brazilian Portuguese translation ([view](https://github.com/videojs/video.js/pull/1520))
* @baloneysandwiches added a hasClass method ([view](https://github.com/videojs/video.js/pull/1464))

--------------------

Expand Down
10 changes: 10 additions & 0 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ vjs.Component.prototype.triggerReady = function(){
/* Display
============================================================================= */

/**
* Check if a component's element has a CSS class name
*
* @param {String} classToCheck Classname to check
* @return {vjs.Component}
*/
vjs.Component.prototype.hasClass = function(classToCheck){
return vjs.hasClass(this.el_, classToCheck);
};

/**
* Add a CSS class name to the component's element
*
Expand Down
15 changes: 13 additions & 2 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,25 @@ vjs.isEmpty = function(obj) {
return true;
};

/**
* Check if an element has a CSS class
* @param {Element} element Element to check
* @param {String} classToCheck Classname to check
* @private
*/
vjs.hasClass = function(element, classToCheck){
return ((' ' + element.className + ' ').indexOf(' ' + classToCheck + ' ') !== -1);
};


/**
* Add a CSS class name to an element
* @param {Element} element Element to add class name to
* @param {String} classToAdd Classname to add
* @private
*/
vjs.addClass = function(element, classToAdd){
if ((' '+element.className+' ').indexOf(' '+classToAdd+' ') == -1) {
if (!vjs.hasClass(element, classToAdd)) {
element.className = element.className === '' ? classToAdd : element.className + ' ' + classToAdd;
}
};
Expand All @@ -325,7 +336,7 @@ vjs.addClass = function(element, classToAdd){
vjs.removeClass = function(element, classToRemove){
var classNames, i;

if (element.className.indexOf(classToRemove) == -1) { return; }
if (!vjs.hasClass(element, classToRemove)) {return;}

classNames = element.className.split(' ');

Expand Down
8 changes: 8 additions & 0 deletions test/unit/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ test('should add and remove a class name on an element', function(){
ok(el.className === 'test-class2', 'removed first class');
});

test('should read class names on an element', function(){
var el = document.createElement('div');
vjs.addClass(el, 'test-class1');
ok(vjs.hasClass(el, 'test-class1') === true, 'class detected');
ok(vjs.hasClass(el, 'test-class') === false, 'substring correctly not detected');

});

test('should get and remove data from an element', function(){
var el = document.createElement('div');
var data = vjs.getData(el);
Expand Down

0 comments on commit fd181aa

Please sign in to comment.