@samitouri / QOS-React-2 / commits / 65c4ef91c7

Sync CSSCore from upstream

Paul O’Shannessy committed Sep 4, 2013 at 16:47 UTC 65c4ef91c7fe65fdd7631ca1de923c5f7b6e2d0c
1 file changed +114
src/vendor/core/CSSCore.js new
+114
@@ -0,0 +1,114 @@
1 +/**
2 + * Copyright 2013 Facebook, Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + * @providesModule CSSCore
17 + * @typechecks
18 + */
19 +
20 +var invariant = require('invariant');
21 +
22 +/**
23 + * The CSSCore module specifies the API (and implements most of the methods)
24 + * that should be used when dealing with the display of elements (via their
25 + * CSS classes and visibility on screeni. It is an API focused on mutating the
26 + * display and not reading it as no logical state should be encoded in the
27 + * display of elements.
28 + */
29 +
30 +/**
31 + * Tests whether the element has the class specified.
32 + *
33 + * Note: This function is not exported in CSSCore because CSS classNames should
34 + * not store any logical information about the element. Use DataStore to store
35 + * information on an element.
36 + *
37 + * @param {DOMElement} element the element to set the class on
38 + * @param {string} className the CSS className
39 + * @returns {boolean} true if the element has the class, false if not
40 + */
41 +function hasClass(element, className) {
42 + if (element.classList) {
43 + return !!className && element.classList.contains(className);
44 + }
45 + return (' ' + element.className + ' ').indexOf(' ' + className + ' ') > -1;
46 +}
47 +
48 +var CSSCore = {
49 +
50 + /**
51 + * Adds the class passed in to the element if it doesn't already have it.
52 + *
53 + * @param {DOMElement} element the element to set the class on
54 + * @param {string} className the CSS className
55 + * @return {DOMElement} the element passed in
56 + */
57 + addClass: function(element, className) {
58 + invariant(
59 + !/\s/.test(className),
60 + 'CSSCore.addClass takes only a single class name. "%s" contains ' +
61 + 'multiple classes.', className
62 + );
63 +
64 + if (className) {
65 + if (element.classList) {
66 + element.classList.add(className);
67 + } else if (!hasClass(element, className)) {
68 + element.className = element.className + ' ' + className;
69 + }
70 + }
71 + return element;
72 + },
73 +
74 + /**
75 + * Removes the class passed in from the element
76 + *
77 + * @param {DOMElement} element the element to set the class on
78 + * @param {string} className the CSS className
79 + * @return {DOMElement} the element passed in
80 + */
81 + removeClass: function(element, className) {
82 + invariant(
83 + !/\s/.test(className),
84 + 'CSSCore.removeClass takes only a single class name. "%s" contains ' +
85 + 'multiple classes.', className
86 + );
87 +
88 + if (className) {
89 + if (element.classList) {
90 + element.classList.remove(className);
91 + } else if (hasClass(element, className)) {
92 + element.className = element.className
93 + .replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)', 'g'), '$1')
94 + .replace(/\s+/g, ' ') // multiple spaces to one
95 + .replace(/^\s*|\s*$/g, ''); // trim the ends
96 + }
97 + }
98 + return element;
99 + },
100 +
101 + /**
102 + * Helper to add or remove a class from an element based on a condition.
103 + *
104 + * @param {DOMElement} element the element to set the class on
105 + * @param {string} className the CSS className
106 + * @param {*} bool condition to whether to add or remove the class
107 + * @return {DOMElement} the element passed in
108 + */
109 + conditionClass: function(element, className, bool) {
110 + return (bool ? CSSCore.addClass : CSSCore.removeClass)(element, className);
111 + }
112 +};
113 +
114 +module.exports = CSSCore;