can-override.js 11 KB

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