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

add a hasClass() method #1464

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,18 @@ 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){
vjs.hasClass(this.el_, classToCheck);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should return the result of vjs.hasClass.

return this;
};


/**
* 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 @@ -293,14 +293,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 @@ -314,7 +325,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