PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/web/files/$node_modules/clean-css/lib/optimizer/validator.js

https://gitlab.com/cirospaciari/jscomet
JavaScript | 470 lines | 423 code | 46 blank | 1 comment | 31 complexity | a2098a8590470fdd6af66752b8332be5 MD5 | raw file
  1. var Units = [
  2. '%',
  3. 'ch',
  4. 'cm',
  5. 'em',
  6. 'ex',
  7. 'in',
  8. 'mm',
  9. 'pc',
  10. 'pt',
  11. 'px',
  12. 'rem',
  13. 'vh',
  14. 'vm',
  15. 'vmax',
  16. 'vmin',
  17. 'vw'
  18. ];
  19. var cssUnitRegexStr = '(\\-?\\.?\\d+\\.?\\d*(' + Units.join('|') + '|)|auto|inherit)';
  20. var cssCalcRegexStr = '(\\-moz\\-|\\-webkit\\-)?calc\\([^\\)]+\\)';
  21. var cssFunctionNoVendorRegexStr = '[A-Z]+(\\-|[A-Z]|[0-9])+\\(.*?\\)';
  22. var cssFunctionVendorRegexStr = '\\-(\\-|[A-Z]|[0-9])+\\(.*?\\)';
  23. var cssVariableRegexStr = 'var\\(\\-\\-[^\\)]+\\)';
  24. var cssFunctionAnyRegexStr = '(' + cssVariableRegexStr + '|' + cssFunctionNoVendorRegexStr + '|' + cssFunctionVendorRegexStr + ')';
  25. var cssUnitOrCalcRegexStr = '(' + cssUnitRegexStr + '|' + cssCalcRegexStr + ')';
  26. var cssFunctionNoVendorRegex = new RegExp('^' + cssFunctionNoVendorRegexStr + '$', 'i');
  27. var cssVariableRegex = new RegExp('^' + cssVariableRegexStr + '$', 'i');
  28. var cssFunctionAnyRegex = new RegExp('^' + cssFunctionAnyRegexStr + '$', 'i');
  29. var cssUnitRegex = new RegExp('^' + cssUnitRegexStr + '$', 'i');
  30. var cssUnitOrCalcRegex = new RegExp('^' + cssUnitOrCalcRegexStr + '$', 'i');
  31. var urlRegex = /^url\([\s\S]+\)$/i;
  32. var globalKeywords = [
  33. 'inherit',
  34. 'initial',
  35. 'unset'
  36. ];
  37. var Keywords = {
  38. '*-style': [
  39. 'auto',
  40. 'dashed',
  41. 'dotted',
  42. 'double',
  43. 'groove',
  44. 'hidden',
  45. 'inset',
  46. 'none',
  47. 'outset',
  48. 'ridge',
  49. 'solid'
  50. ],
  51. 'background-attachment': [
  52. 'fixed',
  53. 'inherit',
  54. 'local',
  55. 'scroll'
  56. ],
  57. 'background-clip': [
  58. 'border-box',
  59. 'content-box',
  60. 'inherit',
  61. 'padding-box',
  62. 'text'
  63. ],
  64. 'background-origin': [
  65. 'border-box',
  66. 'content-box',
  67. 'inherit',
  68. 'padding-box'
  69. ],
  70. 'background-position': [
  71. 'bottom',
  72. 'center',
  73. 'left',
  74. 'right',
  75. 'top'
  76. ],
  77. 'background-repeat': [
  78. 'no-repeat',
  79. 'inherit',
  80. 'repeat',
  81. 'repeat-x',
  82. 'repeat-y',
  83. 'round',
  84. 'space'
  85. ],
  86. 'background-size': [
  87. 'auto',
  88. 'cover',
  89. 'contain'
  90. ],
  91. 'border-collapse': [
  92. 'collapse',
  93. 'inherit',
  94. 'separate'
  95. ],
  96. 'bottom': [
  97. 'auto'
  98. ],
  99. 'clear': [
  100. 'both',
  101. 'left',
  102. 'none',
  103. 'right'
  104. ],
  105. 'cursor': [
  106. 'all-scroll',
  107. 'auto',
  108. 'col-resize',
  109. 'crosshair',
  110. 'default',
  111. 'e-resize',
  112. 'help',
  113. 'move',
  114. 'n-resize',
  115. 'ne-resize',
  116. 'no-drop',
  117. 'not-allowed',
  118. 'nw-resize',
  119. 'pointer',
  120. 'progress',
  121. 'row-resize',
  122. 's-resize',
  123. 'se-resize',
  124. 'sw-resize',
  125. 'text',
  126. 'vertical-text',
  127. 'w-resize',
  128. 'wait'
  129. ],
  130. 'display': [
  131. 'block',
  132. 'inline',
  133. 'inline-block',
  134. 'inline-table',
  135. 'list-item',
  136. 'none',
  137. 'table',
  138. 'table-caption',
  139. 'table-cell',
  140. 'table-column',
  141. 'table-column-group',
  142. 'table-footer-group',
  143. 'table-header-group',
  144. 'table-row',
  145. 'table-row-group'
  146. ],
  147. 'float': [
  148. 'left',
  149. 'none',
  150. 'right'
  151. ],
  152. 'left': [
  153. 'auto'
  154. ],
  155. 'font-style': [
  156. 'italic',
  157. 'normal',
  158. 'oblique'
  159. ],
  160. 'font-weight': [
  161. '100',
  162. '200',
  163. '300',
  164. '400',
  165. '500',
  166. '600',
  167. '700',
  168. '800',
  169. '900',
  170. 'bold',
  171. 'bolder',
  172. 'lighter',
  173. 'normal'
  174. ],
  175. 'list-style-position': [
  176. 'inside',
  177. 'outside'
  178. ],
  179. 'list-style-type': [
  180. 'armenian',
  181. 'circle',
  182. 'decimal',
  183. 'decimal-leading-zero',
  184. 'disc',
  185. 'decimal|disc', // this is the default value of list-style-type, see comment in compactable.js
  186. 'georgian',
  187. 'lower-alpha',
  188. 'lower-greek',
  189. 'lower-latin',
  190. 'lower-roman',
  191. 'none',
  192. 'square',
  193. 'upper-alpha',
  194. 'upper-latin',
  195. 'upper-roman'
  196. ],
  197. 'overflow': [
  198. 'auto',
  199. 'hidden',
  200. 'scroll',
  201. 'visible'
  202. ],
  203. 'position': [
  204. 'absolute',
  205. 'fixed',
  206. 'relative',
  207. 'static'
  208. ],
  209. 'right': [
  210. 'auto'
  211. ],
  212. 'text-align': [
  213. 'center',
  214. 'justify',
  215. 'left',
  216. 'left|right', // this is the default value of list-style-type, see comment in compactable.js
  217. 'right'
  218. ],
  219. 'text-decoration': [
  220. 'line-through',
  221. 'none',
  222. 'overline',
  223. 'underline'
  224. ],
  225. 'text-overflow': [
  226. 'clip',
  227. 'ellipsis'
  228. ],
  229. 'top': [
  230. 'auto'
  231. ],
  232. 'vertical-align': [
  233. 'baseline',
  234. 'bottom',
  235. 'middle',
  236. 'sub',
  237. 'super',
  238. 'text-bottom',
  239. 'text-top',
  240. 'top'
  241. ],
  242. 'visibility': [
  243. 'collapse',
  244. 'hidden',
  245. 'visible'
  246. ],
  247. 'white-space': [
  248. 'normal',
  249. 'nowrap',
  250. 'pre'
  251. ],
  252. 'width': [
  253. 'inherit',
  254. 'initial',
  255. 'medium',
  256. 'thick',
  257. 'thin'
  258. ]
  259. };
  260. var VENDOR_PREFIX_PATTERN = /(^|\W)-\w+\-/;
  261. function areSameFunction(value1, value2) {
  262. if (!isValidFunction(value1) || !isValidFunction(value2)) {
  263. return false;
  264. }
  265. var function1Name = value1.substring(0, value1.indexOf('('));
  266. var function2Name = value2.substring(0, value2.indexOf('('));
  267. return function1Name === function2Name;
  268. }
  269. function hasNoVendorPrefix(value) {
  270. return VENDOR_PREFIX_PATTERN.test(value);
  271. }
  272. function isValidBackgroundAttachment(value) {
  273. return Keywords['background-attachment'].indexOf(value) > -1;
  274. }
  275. function isValidBackgroundClip(value) {
  276. return Keywords['background-clip'].indexOf(value) > -1;
  277. }
  278. function isValidBackgroundRepeat(value) {
  279. return Keywords['background-repeat'].indexOf(value) > -1;
  280. }
  281. function isValidBackgroundOrigin(value) {
  282. return Keywords['background-origin'].indexOf(value) > -1;
  283. }
  284. function isValidBackgroundPosition(value) {
  285. var parts;
  286. var i, l;
  287. if (value === 'inherit') {
  288. return true;
  289. }
  290. parts = value.split(' ');
  291. for (i = 0, l = parts.length; i < l; i++) {
  292. if (parts[i] === '') {
  293. continue;
  294. } else if (isValidBackgroundPositionPart(parts[i])) {
  295. continue;
  296. }
  297. return false;
  298. }
  299. return true;
  300. }
  301. function isValidBackgroundPositionPart(value) {
  302. return Keywords['background-position'].indexOf(value) > -1 || cssUnitOrCalcRegex.test(value);
  303. }
  304. function isValidBackgroundSizePart(value) {
  305. return Keywords['background-size'].indexOf(value) > -1 || cssUnitRegex.test(value);
  306. }
  307. function isValidColor(value) {
  308. return isValidNamedColor(value) ||
  309. isValidColorValue(value);
  310. }
  311. function isValidColorValue(value) {
  312. return isValidHexColor(value) ||
  313. isValidRgbaColor(value) ||
  314. isValidHslaColor(value);
  315. }
  316. function isValidFunction(value) {
  317. return !urlRegex.test(value) && cssFunctionAnyRegex.test(value);
  318. }
  319. function isValidFunctionWithoutVendorPrefix(value) {
  320. return !urlRegex.test(value) && cssFunctionNoVendorRegex.test(value);
  321. }
  322. function isValidGlobalValue(value) {
  323. return globalKeywords.indexOf(value) > -1;
  324. }
  325. function isValidHexColor(value) {
  326. return (value.length === 4 || value.length === 7) && value[0] === '#';
  327. }
  328. function isValidHslaColor(value) {
  329. return value.length > 0 && value.indexOf('hsla(') === 0 && value.indexOf(')') === value.length - 1;
  330. }
  331. function isValidImage(value) {
  332. return value == 'none' || value == 'inherit' || isValidUrl(value);
  333. }
  334. function isValidKeywordValue(propertyName, value, includeGlobal) {
  335. return Keywords[propertyName].indexOf(value) > -1 || includeGlobal && isValidGlobalValue(value);
  336. }
  337. function isValidListStyleType(value) {
  338. return Keywords['list-style-type'].indexOf(value) > -1;
  339. }
  340. function isValidListStylePosition(value) {
  341. return Keywords['list-style-position'].indexOf(value) > -1;
  342. }
  343. function isValidNamedColor(value) {
  344. // We don't really check if it's a valid color value, but allow any letters in it
  345. return value !== 'auto' && (value === 'transparent' || value === 'inherit' || /^[a-zA-Z]+$/.test(value));
  346. }
  347. function isValidRgbaColor(value) {
  348. return value.length > 0 && value.indexOf('rgba(') === 0 && value.indexOf(')') === value.length - 1;
  349. }
  350. function isValidStyle(value) {
  351. return Keywords['*-style'].indexOf(value) > -1;
  352. }
  353. function isValidTextShadow(compatibleCssUnitRegex, value) {
  354. return isValidUnitWithoutFunction(compatibleCssUnitRegex, value) ||
  355. isValidColor(value) ||
  356. isValidGlobalValue(value);
  357. }
  358. function isValidUnit(compatibleCssUnitAnyRegex, value) {
  359. return compatibleCssUnitAnyRegex.test(value);
  360. }
  361. function isValidUnitWithoutFunction(compatibleCssUnitRegex, value) {
  362. return compatibleCssUnitRegex.test(value);
  363. }
  364. function isValidUrl(value) {
  365. return urlRegex.test(value);
  366. }
  367. function isValidVariable(value) {
  368. return cssVariableRegex.test(value);
  369. }
  370. function isValidVendorPrefixedValue(value) {
  371. return /^-([A-Za-z0-9]|-)*$/gi.test(value);
  372. }
  373. function isValidWidth(compatibleCssUnitRegex, value) {
  374. return isValidUnit(compatibleCssUnitRegex, value) || Keywords.width.indexOf(value) > -1;
  375. }
  376. function isValidZIndex(value) {
  377. return value == 'auto' ||
  378. isValidGlobalValue(value) ||
  379. value.length > 0 && value == ('' + parseInt(value));
  380. }
  381. function validator(compatibility) {
  382. var validUnits = Units.slice(0).filter(function (value) {
  383. return !(value in compatibility.units) || compatibility.units[value] === true;
  384. });
  385. var compatibleCssUnitRegexStr = '(\\-?\\.?\\d+\\.?\\d*(' + validUnits.join('|') + '|)|auto|inherit)';
  386. var compatibleCssUnitRegex = new RegExp('^' + compatibleCssUnitRegexStr + '$', 'i');
  387. var compatibleCssUnitAnyRegex = new RegExp('^(none|' + Keywords.width.join('|') + '|' + compatibleCssUnitRegexStr + '|' + cssVariableRegexStr + '|' + cssFunctionNoVendorRegexStr + '|' + cssFunctionVendorRegexStr + ')$', 'i');
  388. var colorOpacity = compatibility.colors.opacity;
  389. return {
  390. areSameFunction: areSameFunction,
  391. colorOpacity: colorOpacity,
  392. hasNoVendorPrefix: hasNoVendorPrefix,
  393. isValidBackgroundAttachment: isValidBackgroundAttachment,
  394. isValidBackgroundClip: isValidBackgroundClip,
  395. isValidBackgroundOrigin: isValidBackgroundOrigin,
  396. isValidBackgroundPosition: isValidBackgroundPosition,
  397. isValidBackgroundPositionPart: isValidBackgroundPositionPart,
  398. isValidBackgroundRepeat: isValidBackgroundRepeat,
  399. isValidBackgroundSizePart: isValidBackgroundSizePart,
  400. isValidColor: isValidColor,
  401. isValidColorValue: isValidColorValue,
  402. isValidFunction: isValidFunction,
  403. isValidFunctionWithoutVendorPrefix: isValidFunctionWithoutVendorPrefix,
  404. isValidGlobalValue: isValidGlobalValue,
  405. isValidHexColor: isValidHexColor,
  406. isValidHslaColor: isValidHslaColor,
  407. isValidImage: isValidImage,
  408. isValidKeywordValue: isValidKeywordValue,
  409. isValidListStylePosition: isValidListStylePosition,
  410. isValidListStyleType: isValidListStyleType,
  411. isValidNamedColor: isValidNamedColor,
  412. isValidRgbaColor: isValidRgbaColor,
  413. isValidStyle: isValidStyle,
  414. isValidTextShadow: isValidTextShadow.bind(null, compatibleCssUnitRegex),
  415. isValidUnit: isValidUnit.bind(null, compatibleCssUnitAnyRegex),
  416. isValidUnitWithoutFunction: isValidUnitWithoutFunction.bind(null, compatibleCssUnitRegex),
  417. isValidUrl: isValidUrl,
  418. isValidVariable: isValidVariable,
  419. isValidVendorPrefixedValue: isValidVendorPrefixedValue,
  420. isValidWidth: isValidWidth.bind(null, compatibleCssUnitRegex),
  421. isValidZIndex: isValidZIndex
  422. };
  423. }
  424. module.exports = validator;