1
0
mirror of https://github.com/videojs/video.js.git synced 2025-01-10 23:30:03 +02:00

@baloneysandwiches added a hasClass method. closes #1464

This commit is contained in:
baloneysandwiches 2014-09-29 15:46:18 -07:00 committed by Steve Heffernan
parent 2a71d69c72
commit fd181aa9cd
4 changed files with 32 additions and 2 deletions

View File

@ -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))
--------------------

View File

@ -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
*

View File

@ -304,6 +304,17 @@ 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
@ -311,7 +322,7 @@ vjs.isEmpty = function(obj) {
* @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;
}
};
@ -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(' ');

View File

@ -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);