can-override.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. var understandable = require('./properties/understandable');
  2. function animationIterationCount(validator, value1, value2) {
  3. if (!understandable(validator, value1, value2, 0, true) && !(validator.isAnimationIterationCountKeyword(value2) || validator.isPositiveNumber(value2))) {
  4. return false;
  5. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  6. return true;
  7. }
  8. return validator.isAnimationIterationCountKeyword(value2) || validator.isPositiveNumber(value2);
  9. }
  10. function animationName(validator, value1, value2) {
  11. if (!understandable(validator, value1, value2, 0, true) && !(validator.isAnimationNameKeyword(value2) || validator.isIdentifier(value2))) {
  12. return false;
  13. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  14. return true;
  15. }
  16. return validator.isAnimationNameKeyword(value2) || validator.isIdentifier(value2);
  17. }
  18. function areSameFunction(validator, value1, value2) {
  19. if (!validator.isFunction(value1) || !validator.isFunction(value2)) {
  20. return false;
  21. }
  22. var function1Name = value1.substring(0, value1.indexOf('('));
  23. var function2Name = value2.substring(0, value2.indexOf('('));
  24. return function1Name === function2Name;
  25. }
  26. function backgroundPosition(validator, value1, value2) {
  27. if (!understandable(validator, value1, value2, 0, true) && !(validator.isBackgroundPositionKeyword(value2) || validator.isGlobal(value2))) {
  28. return false;
  29. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  30. return true;
  31. } else if (validator.isBackgroundPositionKeyword(value2) || validator.isGlobal(value2)) {
  32. return true;
  33. }
  34. return unit(validator, value1, value2);
  35. }
  36. function backgroundSize(validator, value1, value2) {
  37. if (!understandable(validator, value1, value2, 0, true) && !(validator.isBackgroundSizeKeyword(value2) || validator.isGlobal(value2))) {
  38. return false;
  39. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  40. return true;
  41. } else if (validator.isBackgroundSizeKeyword(value2) || validator.isGlobal(value2)) {
  42. return true;
  43. }
  44. return unit(validator, value1, value2);
  45. }
  46. function color(validator, value1, value2) {
  47. if (!understandable(validator, value1, value2, 0, true) && !validator.isColor(value2)) {
  48. return false;
  49. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  50. return true;
  51. } else if (!validator.colorOpacity && (validator.isRgbColor(value1) || validator.isHslColor(value1))) {
  52. return false;
  53. } else if (!validator.colorOpacity && (validator.isRgbColor(value2) || validator.isHslColor(value2))) {
  54. return false;
  55. } else if (validator.isColor(value1) && validator.isColor(value2)) {
  56. return true;
  57. }
  58. return sameFunctionOrValue(validator, value1, value2);
  59. }
  60. function components(overrideCheckers) {
  61. return function (validator, value1, value2, position) {
  62. return overrideCheckers[position](validator, value1, value2);
  63. };
  64. }
  65. function fontFamily(validator, value1, value2) {
  66. return understandable(validator, value1, value2, 0, true);
  67. }
  68. function image(validator, value1, value2) {
  69. if (!understandable(validator, value1, value2, 0, true) && !validator.isImage(value2)) {
  70. return false;
  71. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  72. return true;
  73. } else if (validator.isImage(value2)) {
  74. return true;
  75. } else if (validator.isImage(value1)) {
  76. return false;
  77. }
  78. return sameFunctionOrValue(validator, value1, value2);
  79. }
  80. function keyword(propertyName) {
  81. return function(validator, value1, value2) {
  82. if (!understandable(validator, value1, value2, 0, true) && !validator.isKeyword(propertyName)(value2)) {
  83. return false;
  84. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  85. return true;
  86. }
  87. return validator.isKeyword(propertyName)(value2);
  88. };
  89. }
  90. function keywordWithGlobal(propertyName) {
  91. return function(validator, value1, value2) {
  92. if (!understandable(validator, value1, value2, 0, true) && !(validator.isKeyword(propertyName)(value2) || validator.isGlobal(value2))) {
  93. return false;
  94. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  95. return true;
  96. }
  97. return validator.isKeyword(propertyName)(value2) || validator.isGlobal(value2);
  98. };
  99. }
  100. function propertyName(validator, value1, value2) {
  101. if (!understandable(validator, value1, value2, 0, true) && !validator.isIdentifier(value2)) {
  102. return false;
  103. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  104. return true;
  105. }
  106. return validator.isIdentifier(value2);
  107. }
  108. function sameFunctionOrValue(validator, value1, value2) {
  109. return areSameFunction(validator, value1, value2) ?
  110. true :
  111. value1 === value2;
  112. }
  113. function textShadow(validator, value1, value2) {
  114. if (!understandable(validator, value1, value2, 0, true) && !(validator.isUnit(value2) || validator.isColor(value2) || validator.isGlobal(value2))) {
  115. return false;
  116. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  117. return true;
  118. }
  119. return validator.isUnit(value2) || validator.isColor(value2) || validator.isGlobal(value2);
  120. }
  121. function time(validator, value1, value2) {
  122. if (!understandable(validator, value1, value2, 0, true) && !validator.isTime(value2)) {
  123. return false;
  124. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  125. return true;
  126. } else if (validator.isTime(value1) && !validator.isTime(value2)) {
  127. return false;
  128. } else if (validator.isTime(value2)) {
  129. return true;
  130. } else if (validator.isTime(value1)) {
  131. return false;
  132. } else if (validator.isFunction(value1) && !validator.isPrefixed(value1) && validator.isFunction(value2) && !validator.isPrefixed(value2)) {
  133. return true;
  134. }
  135. return sameFunctionOrValue(validator, value1, value2);
  136. }
  137. function timingFunction(validator, value1, value2) {
  138. if (!understandable(validator, value1, value2, 0, true) && !(validator.isTimingFunction(value2) || validator.isGlobal(value2))) {
  139. return false;
  140. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  141. return true;
  142. }
  143. return validator.isTimingFunction(value2) || validator.isGlobal(value2);
  144. }
  145. function unit(validator, value1, value2) {
  146. if (!understandable(validator, value1, value2, 0, true) && !validator.isUnit(value2)) {
  147. return false;
  148. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  149. return true;
  150. } else if (validator.isUnit(value1) && !validator.isUnit(value2)) {
  151. return false;
  152. } else if (validator.isUnit(value2)) {
  153. return true;
  154. } else if (validator.isUnit(value1)) {
  155. return false;
  156. } else if (validator.isFunction(value1) && !validator.isPrefixed(value1) && validator.isFunction(value2) && !validator.isPrefixed(value2)) {
  157. return true;
  158. }
  159. return sameFunctionOrValue(validator, value1, value2);
  160. }
  161. function unitOrKeywordWithGlobal(propertyName) {
  162. var byKeyword = keywordWithGlobal(propertyName);
  163. return function(validator, value1, value2) {
  164. return unit(validator, value1, value2) || byKeyword(validator, value1, value2);
  165. };
  166. }
  167. function unitOrNumber(validator, value1, value2) {
  168. if (!understandable(validator, value1, value2, 0, true) && !(validator.isUnit(value2) || validator.isNumber(value2))) {
  169. return false;
  170. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  171. return true;
  172. } else if ((validator.isUnit(value1) || validator.isNumber(value1)) && !(validator.isUnit(value2) || validator.isNumber(value2))) {
  173. return false;
  174. } else if (validator.isUnit(value2) || validator.isNumber(value2)) {
  175. return true;
  176. } else if (validator.isUnit(value1) || validator.isNumber(value1)) {
  177. return false;
  178. } else if (validator.isFunction(value1) && !validator.isPrefixed(value1) && validator.isFunction(value2) && !validator.isPrefixed(value2)) {
  179. return true;
  180. }
  181. return sameFunctionOrValue(validator, value1, value2);
  182. }
  183. function zIndex(validator, value1, value2) {
  184. if (!understandable(validator, value1, value2, 0, true) && !validator.isZIndex(value2)) {
  185. return false;
  186. } else if (validator.isVariable(value1) && validator.isVariable(value2)) {
  187. return true;
  188. }
  189. return validator.isZIndex(value2);
  190. }
  191. module.exports = {
  192. generic: {
  193. color: color,
  194. components: components,
  195. image: image,
  196. propertyName: propertyName,
  197. time: time,
  198. timingFunction: timingFunction,
  199. unit: unit,
  200. unitOrNumber: unitOrNumber
  201. },
  202. property: {
  203. animationDirection: keywordWithGlobal('animation-direction'),
  204. animationFillMode: keyword('animation-fill-mode'),
  205. animationIterationCount: animationIterationCount,
  206. animationName: animationName,
  207. animationPlayState: keywordWithGlobal('animation-play-state'),
  208. backgroundAttachment: keyword('background-attachment'),
  209. backgroundClip: keywordWithGlobal('background-clip'),
  210. backgroundOrigin: keyword('background-origin'),
  211. backgroundPosition: backgroundPosition,
  212. backgroundRepeat: keyword('background-repeat'),
  213. backgroundSize: backgroundSize,
  214. bottom: unitOrKeywordWithGlobal('bottom'),
  215. borderCollapse: keyword('border-collapse'),
  216. borderStyle: keywordWithGlobal('*-style'),
  217. clear: keywordWithGlobal('clear'),
  218. cursor: keywordWithGlobal('cursor'),
  219. display: keywordWithGlobal('display'),
  220. float: keywordWithGlobal('float'),
  221. left: unitOrKeywordWithGlobal('left'),
  222. fontFamily: fontFamily,
  223. fontStretch: keywordWithGlobal('font-stretch'),
  224. fontStyle: keywordWithGlobal('font-style'),
  225. fontVariant: keywordWithGlobal('font-variant'),
  226. fontWeight: keywordWithGlobal('font-weight'),
  227. listStyleType: keywordWithGlobal('list-style-type'),
  228. listStylePosition: keywordWithGlobal('list-style-position'),
  229. outlineStyle: keywordWithGlobal('*-style'),
  230. overflow: keywordWithGlobal('overflow'),
  231. position: keywordWithGlobal('position'),
  232. right: unitOrKeywordWithGlobal('right'),
  233. textAlign: keywordWithGlobal('text-align'),
  234. textDecoration: keywordWithGlobal('text-decoration'),
  235. textOverflow: keywordWithGlobal('text-overflow'),
  236. textShadow: textShadow,
  237. top: unitOrKeywordWithGlobal('top'),
  238. transform: sameFunctionOrValue,
  239. verticalAlign: unitOrKeywordWithGlobal('vertical-align'),
  240. visibility: keywordWithGlobal('visibility'),
  241. whiteSpace: keywordWithGlobal('white-space'),
  242. zIndex: zIndex
  243. }
  244. };