validator.js 11 KB

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