a zip code crypto-currency system good for red ONLY

validator.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. var functionNoVendorRegexStr = '[A-Z]+(\\-|[A-Z]|[0-9])+\\(.*?\\)';
  2. var functionVendorRegexStr = '\\-(\\-|[A-Z]|[0-9])+\\(.*?\\)';
  3. var variableRegexStr = 'var\\(\\-\\-[^\\)]+\\)';
  4. var functionAnyRegexStr = '(' + variableRegexStr + '|' + functionNoVendorRegexStr + '|' + functionVendorRegexStr + ')';
  5. var animationTimingFunctionRegex = /^(cubic\-bezier|steps)\([^\)]+\)$/;
  6. var calcRegex = new RegExp('^(\\-moz\\-|\\-webkit\\-)?calc\\([^\\)]+\\)$', 'i');
  7. var decimalRegex = /[0-9]/;
  8. var functionAnyRegex = new RegExp('^' + functionAnyRegexStr + '$', 'i');
  9. var hslColorRegex = /^hsl\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31}\)|hsla\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+\s{0,31}\)$/;
  10. var identifierRegex = /^(\-[a-z0-9_][a-z0-9\-_]*|[a-z][a-z0-9\-_]*)$/i;
  11. var longHexColorRegex = /^#[0-9a-f]{6}$/i;
  12. var namedEntityRegex = /^[a-z]+$/i;
  13. var prefixRegex = /^-([a-z0-9]|-)*$/i;
  14. var rgbColorRegex = /^rgb\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31}\)|rgba\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\.\d]+\s{0,31}\)$/;
  15. var shortHexColorRegex = /^#[0-9a-f]{3}$/i;
  16. var validTimeUnits = ['ms', 's'];
  17. var urlRegex = /^url\([\s\S]+\)$/i;
  18. var variableRegex = new RegExp('^' + variableRegexStr + '$', 'i');
  19. var DECIMAL_DOT = '.';
  20. var MINUS_SIGN = '-';
  21. var PLUS_SIGN = '+';
  22. var Keywords = {
  23. '^': [
  24. 'inherit',
  25. 'initial',
  26. 'unset'
  27. ],
  28. '*-style': [
  29. 'auto',
  30. 'dashed',
  31. 'dotted',
  32. 'double',
  33. 'groove',
  34. 'hidden',
  35. 'inset',
  36. 'none',
  37. 'outset',
  38. 'ridge',
  39. 'solid'
  40. ],
  41. 'animation-direction': [
  42. 'alternate',
  43. 'alternate-reverse',
  44. 'normal',
  45. 'reverse'
  46. ],
  47. 'animation-fill-mode': [
  48. 'backwards',
  49. 'both',
  50. 'forwards',
  51. 'none'
  52. ],
  53. 'animation-iteration-count': [
  54. 'infinite'
  55. ],
  56. 'animation-name': [
  57. 'none'
  58. ],
  59. 'animation-play-state': [
  60. 'paused',
  61. 'running'
  62. ],
  63. 'animation-timing-function': [
  64. 'ease',
  65. 'ease-in',
  66. 'ease-in-out',
  67. 'ease-out',
  68. 'linear',
  69. 'step-end',
  70. 'step-start'
  71. ],
  72. 'background-attachment': [
  73. 'fixed',
  74. 'inherit',
  75. 'local',
  76. 'scroll'
  77. ],
  78. 'background-clip': [
  79. 'border-box',
  80. 'content-box',
  81. 'inherit',
  82. 'padding-box',
  83. 'text'
  84. ],
  85. 'background-origin': [
  86. 'border-box',
  87. 'content-box',
  88. 'inherit',
  89. 'padding-box'
  90. ],
  91. 'background-position': [
  92. 'bottom',
  93. 'center',
  94. 'left',
  95. 'right',
  96. 'top'
  97. ],
  98. 'background-repeat': [
  99. 'no-repeat',
  100. 'inherit',
  101. 'repeat',
  102. 'repeat-x',
  103. 'repeat-y',
  104. 'round',
  105. 'space'
  106. ],
  107. 'background-size': [
  108. 'auto',
  109. 'cover',
  110. 'contain'
  111. ],
  112. 'border-collapse': [
  113. 'collapse',
  114. 'inherit',
  115. 'separate'
  116. ],
  117. 'bottom': [
  118. 'auto'
  119. ],
  120. 'clear': [
  121. 'both',
  122. 'left',
  123. 'none',
  124. 'right'
  125. ],
  126. 'color': [
  127. 'transparent'
  128. ],
  129. 'cursor': [
  130. 'all-scroll',
  131. 'auto',
  132. 'col-resize',
  133. 'crosshair',
  134. 'default',
  135. 'e-resize',
  136. 'help',
  137. 'move',
  138. 'n-resize',
  139. 'ne-resize',
  140. 'no-drop',
  141. 'not-allowed',
  142. 'nw-resize',
  143. 'pointer',
  144. 'progress',
  145. 'row-resize',
  146. 's-resize',
  147. 'se-resize',
  148. 'sw-resize',
  149. 'text',
  150. 'vertical-text',
  151. 'w-resize',
  152. 'wait'
  153. ],
  154. 'display': [
  155. 'block',
  156. 'inline',
  157. 'inline-block',
  158. 'inline-table',
  159. 'list-item',
  160. 'none',
  161. 'table',
  162. 'table-caption',
  163. 'table-cell',
  164. 'table-column',
  165. 'table-column-group',
  166. 'table-footer-group',
  167. 'table-header-group',
  168. 'table-row',
  169. 'table-row-group'
  170. ],
  171. 'float': [
  172. 'left',
  173. 'none',
  174. 'right'
  175. ],
  176. 'left': [
  177. 'auto'
  178. ],
  179. 'font': [
  180. 'caption',
  181. 'icon',
  182. 'menu',
  183. 'message-box',
  184. 'small-caption',
  185. 'status-bar',
  186. 'unset'
  187. ],
  188. 'font-size': [
  189. 'large',
  190. 'larger',
  191. 'medium',
  192. 'small',
  193. 'smaller',
  194. 'x-large',
  195. 'x-small',
  196. 'xx-large',
  197. 'xx-small'
  198. ],
  199. 'font-stretch': [
  200. 'condensed',
  201. 'expanded',
  202. 'extra-condensed',
  203. 'extra-expanded',
  204. 'normal',
  205. 'semi-condensed',
  206. 'semi-expanded',
  207. 'ultra-condensed',
  208. 'ultra-expanded'
  209. ],
  210. 'font-style': [
  211. 'italic',
  212. 'normal',
  213. 'oblique'
  214. ],
  215. 'font-variant': [
  216. 'normal',
  217. 'small-caps'
  218. ],
  219. 'font-weight': [
  220. '100',
  221. '200',
  222. '300',
  223. '400',
  224. '500',
  225. '600',
  226. '700',
  227. '800',
  228. '900',
  229. 'bold',
  230. 'bolder',
  231. 'lighter',
  232. 'normal'
  233. ],
  234. 'line-height': [
  235. 'normal'
  236. ],
  237. 'list-style-position': [
  238. 'inside',
  239. 'outside'
  240. ],
  241. 'list-style-type': [
  242. 'armenian',
  243. 'circle',
  244. 'decimal',
  245. 'decimal-leading-zero',
  246. 'disc',
  247. 'decimal|disc', // this is the default value of list-style-type, see comment in compactable.js
  248. 'georgian',
  249. 'lower-alpha',
  250. 'lower-greek',
  251. 'lower-latin',
  252. 'lower-roman',
  253. 'none',
  254. 'square',
  255. 'upper-alpha',
  256. 'upper-latin',
  257. 'upper-roman'
  258. ],
  259. 'overflow': [
  260. 'auto',
  261. 'hidden',
  262. 'scroll',
  263. 'visible'
  264. ],
  265. 'position': [
  266. 'absolute',
  267. 'fixed',
  268. 'relative',
  269. 'static'
  270. ],
  271. 'right': [
  272. 'auto'
  273. ],
  274. 'text-align': [
  275. 'center',
  276. 'justify',
  277. 'left',
  278. 'left|right', // this is the default value of list-style-type, see comment in compactable.js
  279. 'right'
  280. ],
  281. 'text-decoration': [
  282. 'line-through',
  283. 'none',
  284. 'overline',
  285. 'underline'
  286. ],
  287. 'text-overflow': [
  288. 'clip',
  289. 'ellipsis'
  290. ],
  291. 'top': [
  292. 'auto'
  293. ],
  294. 'vertical-align': [
  295. 'baseline',
  296. 'bottom',
  297. 'middle',
  298. 'sub',
  299. 'super',
  300. 'text-bottom',
  301. 'text-top',
  302. 'top'
  303. ],
  304. 'visibility': [
  305. 'collapse',
  306. 'hidden',
  307. 'visible'
  308. ],
  309. 'white-space': [
  310. 'normal',
  311. 'nowrap',
  312. 'pre'
  313. ],
  314. 'width': [
  315. 'inherit',
  316. 'initial',
  317. 'medium',
  318. 'thick',
  319. 'thin'
  320. ]
  321. };
  322. var Units = [
  323. '%',
  324. 'ch',
  325. 'cm',
  326. 'em',
  327. 'ex',
  328. 'in',
  329. 'mm',
  330. 'pc',
  331. 'pt',
  332. 'px',
  333. 'rem',
  334. 'vh',
  335. 'vm',
  336. 'vmax',
  337. 'vmin',
  338. 'vw'
  339. ];
  340. function isAnimationTimingFunction() {
  341. var isTimingFunctionKeyword = isKeyword('animation-timing-function');
  342. return function (value) {
  343. return isTimingFunctionKeyword(value) || animationTimingFunctionRegex.test(value);
  344. };
  345. }
  346. function isColor(value) {
  347. return value != 'auto' &&
  348. (
  349. isKeyword('color')(value) ||
  350. isHexColor(value) ||
  351. isColorFunction(value) ||
  352. isNamedEntity(value)
  353. );
  354. }
  355. function isColorFunction(value) {
  356. return isRgbColor(value) || isHslColor(value);
  357. }
  358. function isDynamicUnit(value) {
  359. return calcRegex.test(value);
  360. }
  361. function isFunction(value) {
  362. return functionAnyRegex.test(value);
  363. }
  364. function isHexColor(value) {
  365. return shortHexColorRegex.test(value) || longHexColorRegex.test(value);
  366. }
  367. function isHslColor(value) {
  368. return hslColorRegex.test(value);
  369. }
  370. function isIdentifier(value) {
  371. return identifierRegex.test(value);
  372. }
  373. function isImage(value) {
  374. return value == 'none' || value == 'inherit' || isUrl(value);
  375. }
  376. function isKeyword(propertyName) {
  377. return function(value) {
  378. return Keywords[propertyName].indexOf(value) > -1;
  379. };
  380. }
  381. function isNamedEntity(value) {
  382. return namedEntityRegex.test(value);
  383. }
  384. function isNumber(value) {
  385. return scanForNumber(value) == value.length;
  386. }
  387. function isRgbColor(value) {
  388. return rgbColorRegex.test(value);
  389. }
  390. function isPrefixed(value) {
  391. return prefixRegex.test(value);
  392. }
  393. function isPositiveNumber(value) {
  394. return isNumber(value) &&
  395. parseFloat(value) >= 0;
  396. }
  397. function isVariable(value) {
  398. return variableRegex.test(value);
  399. }
  400. function isTime(value) {
  401. var numberUpTo = scanForNumber(value);
  402. return numberUpTo == value.length && parseInt(value) === 0 ||
  403. numberUpTo > -1 && validTimeUnits.indexOf(value.slice(numberUpTo + 1)) > -1;
  404. }
  405. function isUnit(validUnits, value) {
  406. var numberUpTo = scanForNumber(value);
  407. return numberUpTo == value.length && parseInt(value) === 0 ||
  408. numberUpTo > -1 && validUnits.indexOf(value.slice(numberUpTo + 1)) > -1 ||
  409. value == 'auto' ||
  410. value == 'inherit';
  411. }
  412. function isUrl(value) {
  413. return urlRegex.test(value);
  414. }
  415. function isZIndex(value) {
  416. return value == 'auto' ||
  417. isNumber(value) ||
  418. isKeyword('^')(value);
  419. }
  420. function scanForNumber(value) {
  421. var hasDot = false;
  422. var hasSign = false;
  423. var character;
  424. var i, l;
  425. for (i = 0, l = value.length; i < l; i++) {
  426. character = value[i];
  427. if (i === 0 && (character == PLUS_SIGN || character == MINUS_SIGN)) {
  428. hasSign = true;
  429. } else if (i > 0 && hasSign && (character == PLUS_SIGN || character == MINUS_SIGN)) {
  430. return i - 1;
  431. } else if (character == DECIMAL_DOT && !hasDot) {
  432. hasDot = true;
  433. } else if (character == DECIMAL_DOT && hasDot) {
  434. return i - 1;
  435. } else if (decimalRegex.test(character)) {
  436. continue;
  437. } else {
  438. return i - 1;
  439. }
  440. }
  441. return i;
  442. }
  443. function validator(compatibility) {
  444. var validUnits = Units.slice(0).filter(function (value) {
  445. return !(value in compatibility.units) || compatibility.units[value] === true;
  446. });
  447. return {
  448. colorOpacity: compatibility.colors.opacity,
  449. isAnimationDirectionKeyword: isKeyword('animation-direction'),
  450. isAnimationFillModeKeyword: isKeyword('animation-fill-mode'),
  451. isAnimationIterationCountKeyword: isKeyword('animation-iteration-count'),
  452. isAnimationNameKeyword: isKeyword('animation-name'),
  453. isAnimationPlayStateKeyword: isKeyword('animation-play-state'),
  454. isAnimationTimingFunction: isAnimationTimingFunction(),
  455. isBackgroundAttachmentKeyword: isKeyword('background-attachment'),
  456. isBackgroundClipKeyword: isKeyword('background-clip'),
  457. isBackgroundOriginKeyword: isKeyword('background-origin'),
  458. isBackgroundPositionKeyword: isKeyword('background-position'),
  459. isBackgroundRepeatKeyword: isKeyword('background-repeat'),
  460. isBackgroundSizeKeyword: isKeyword('background-size'),
  461. isColor: isColor,
  462. isColorFunction: isColorFunction,
  463. isDynamicUnit: isDynamicUnit,
  464. isFontKeyword: isKeyword('font'),
  465. isFontSizeKeyword: isKeyword('font-size'),
  466. isFontStretchKeyword: isKeyword('font-stretch'),
  467. isFontStyleKeyword: isKeyword('font-style'),
  468. isFontVariantKeyword: isKeyword('font-variant'),
  469. isFontWeightKeyword: isKeyword('font-weight'),
  470. isFunction: isFunction,
  471. isGlobal: isKeyword('^'),
  472. isHslColor: isHslColor,
  473. isIdentifier: isIdentifier,
  474. isImage: isImage,
  475. isKeyword: isKeyword,
  476. isLineHeightKeyword: isKeyword('line-height'),
  477. isListStylePositionKeyword: isKeyword('list-style-position'),
  478. isListStyleTypeKeyword: isKeyword('list-style-type'),
  479. isNumber: isNumber,
  480. isPrefixed: isPrefixed,
  481. isPositiveNumber: isPositiveNumber,
  482. isRgbColor: isRgbColor,
  483. isStyleKeyword: isKeyword('*-style'),
  484. isTime: isTime,
  485. isUnit: isUnit.bind(null, validUnits),
  486. isUrl: isUrl,
  487. isVariable: isVariable,
  488. isWidth: isKeyword('width'),
  489. isZIndex: isZIndex
  490. };
  491. }
  492. module.exports = validator;