OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview |
| 7 * Simple utility functions for Chromoting. |
| 8 */ |
| 9 |
| 10 /** |
| 11 * @param {string} classes A space-separated list of classes. |
| 12 * @param {string} cls The class to check for. |
| 13 * @return {boolean} True if |cls| is found within |classes|. |
| 14 */ |
| 15 function hasClass(classes, cls) { |
| 16 return classes.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')) != null; |
| 17 } |
| 18 |
| 19 /** |
| 20 * @param {Element} element The element to which to add the class. |
| 21 * @param {string} cls The new class. |
| 22 * @return {void} Nothing. |
| 23 */ |
| 24 function addClass(element, cls) { |
| 25 if (!hasClass(element.className, cls)) { |
| 26 var padded = element.className == '' ? '' : element.className + ' '; |
| 27 element.className = padded + cls; |
| 28 } |
| 29 } |
| 30 |
| 31 /** |
| 32 * @param {Element} element The element from which to remove the class. |
| 33 * @param {string} cls The new class. |
| 34 * @return {void} Nothing. |
| 35 */ |
| 36 function removeClass(element, cls) { |
| 37 element.className = |
| 38 element.className.replace(new RegExp('\\b' + cls + '\\b', 'g'), '') |
| 39 .replace(' ', ' '); |
| 40 } |
OLD | NEW |