optimize.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. var shortenHex = require('./shorten-hex');
  2. var shortenHsl = require('./shorten-hsl');
  3. var shortenRgb = require('./shorten-rgb');
  4. var sortSelectors = require('./sort-selectors');
  5. var tidyRules = require('./tidy-rules');
  6. var tidyBlock = require('./tidy-block');
  7. var tidyAtRule = require('./tidy-at-rule');
  8. var Hack = require('../hack');
  9. var removeUnused = require('../remove-unused');
  10. var restoreFromOptimizing = require('../restore-from-optimizing');
  11. var wrapForOptimizing = require('../wrap-for-optimizing').all;
  12. var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
  13. var Token = require('../../tokenizer/token');
  14. var Marker = require('../../tokenizer/marker');
  15. var formatPosition = require('../../utils/format-position');
  16. var split = require('../../utils/split');
  17. var IgnoreProperty = 'ignore-property';
  18. var CHARSET_TOKEN = '@charset';
  19. var CHARSET_REGEXP = new RegExp('^' + CHARSET_TOKEN, 'i');
  20. var DEFAULT_ROUNDING_PRECISION = require('../../options/rounding-precision').DEFAULT;
  21. var FONT_NUMERAL_WEIGHTS = ['100', '200', '300', '400', '500', '600', '700', '800', '900'];
  22. var FONT_NAME_WEIGHTS = ['normal', 'bold', 'bolder', 'lighter'];
  23. var FONT_NAME_WEIGHTS_WITHOUT_NORMAL = ['bold', 'bolder', 'lighter'];
  24. var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/;
  25. var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/;
  26. var PROPERTY_NAME_PATTERN = /^(?:\-chrome\-|\-[\w\-]+\w|\w[\w\-]+\w|\-\-\S+)$/;
  27. var IMPORT_PREFIX_PATTERN = /^@import/i;
  28. var QUOTED_PATTERN = /^('.*'|".*")$/;
  29. var QUOTED_BUT_SAFE_PATTERN = /^['"][a-zA-Z][a-zA-Z\d\-_]+['"]$/;
  30. var URL_PREFIX_PATTERN = /^url\(/i;
  31. var VARIABLE_NAME_PATTERN = /^--\S+$/;
  32. function isNegative(value) {
  33. return value && value[1][0] == '-' && parseFloat(value[1]) < 0;
  34. }
  35. function isQuoted(value) {
  36. return QUOTED_PATTERN.test(value);
  37. }
  38. function isUrl(value) {
  39. return URL_PREFIX_PATTERN.test(value);
  40. }
  41. function normalizeUrl(value) {
  42. return value
  43. .replace(URL_PREFIX_PATTERN, 'url(')
  44. .replace(/\\?\n|\\?\r\n/g, '');
  45. }
  46. function optimizeBackground(property) {
  47. var values = property.value;
  48. if (values.length == 1 && values[0][1] == 'none') {
  49. values[0][1] = '0 0';
  50. }
  51. if (values.length == 1 && values[0][1] == 'transparent') {
  52. values[0][1] = '0 0';
  53. }
  54. }
  55. function optimizeBorderRadius(property) {
  56. var values = property.value;
  57. var spliceAt;
  58. if (values.length == 3 && values[1][1] == '/' && values[0][1] == values[2][1]) {
  59. spliceAt = 1;
  60. } else if (values.length == 5 && values[2][1] == '/' && values[0][1] == values[3][1] && values[1][1] == values[4][1]) {
  61. spliceAt = 2;
  62. } else if (values.length == 7 && values[3][1] == '/' && values[0][1] == values[4][1] && values[1][1] == values[5][1] && values[2][1] == values[6][1]) {
  63. spliceAt = 3;
  64. } else if (values.length == 9 && values[4][1] == '/' && values[0][1] == values[5][1] && values[1][1] == values[6][1] && values[2][1] == values[7][1] && values[3][1] == values[8][1]) {
  65. spliceAt = 4;
  66. }
  67. if (spliceAt) {
  68. property.value.splice(spliceAt);
  69. property.dirty = true;
  70. }
  71. }
  72. function optimizeColors(name, value, compatibility) {
  73. if (value.indexOf('#') === -1 && value.indexOf('rgb') == -1 && value.indexOf('hsl') == -1) {
  74. return shortenHex(value);
  75. }
  76. value = value
  77. .replace(/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/g, function (match, red, green, blue) {
  78. return shortenRgb(red, green, blue);
  79. })
  80. .replace(/hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/g, function (match, hue, saturation, lightness) {
  81. return shortenHsl(hue, saturation, lightness);
  82. })
  83. .replace(/(^|[^='"])#([0-9a-f]{6})/gi, function (match, prefix, color) {
  84. if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) {
  85. return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase();
  86. } else {
  87. return (prefix + '#' + color).toLowerCase();
  88. }
  89. })
  90. .replace(/(^|[^='"])#([0-9a-f]{3})/gi, function (match, prefix, color) {
  91. return prefix + '#' + color.toLowerCase();
  92. })
  93. .replace(/(rgb|rgba|hsl|hsla)\(([^\)]+)\)/g, function (match, colorFunction, colorDef) {
  94. var tokens = colorDef.split(',');
  95. var applies = (colorFunction == 'hsl' && tokens.length == 3) ||
  96. (colorFunction == 'hsla' && tokens.length == 4) ||
  97. (colorFunction == 'rgb' && tokens.length == 3 && colorDef.indexOf('%') > 0) ||
  98. (colorFunction == 'rgba' && tokens.length == 4 && colorDef.indexOf('%') > 0);
  99. if (!applies) {
  100. return match;
  101. }
  102. if (tokens[1].indexOf('%') == -1) {
  103. tokens[1] += '%';
  104. }
  105. if (tokens[2].indexOf('%') == -1) {
  106. tokens[2] += '%';
  107. }
  108. return colorFunction + '(' + tokens.join(',') + ')';
  109. });
  110. if (compatibility.colors.opacity && name.indexOf('background') == -1) {
  111. value = value.replace(/(?:rgba|hsla)\(0,0%?,0%?,0\)/g, function (match) {
  112. if (split(value, ',').pop().indexOf('gradient(') > -1) {
  113. return match;
  114. }
  115. return 'transparent';
  116. });
  117. }
  118. return shortenHex(value);
  119. }
  120. function optimizeFilter(property) {
  121. if (property.value.length == 1) {
  122. property.value[0][1] = property.value[0][1].replace(/progid:DXImageTransform\.Microsoft\.(Alpha|Chroma)(\W)/, function (match, filter, suffix) {
  123. return filter.toLowerCase() + suffix;
  124. });
  125. }
  126. property.value[0][1] = property.value[0][1]
  127. .replace(/,(\S)/g, ', $1')
  128. .replace(/ ?= ?/g, '=');
  129. }
  130. function optimizeFont(property, options) {
  131. var values = property.value;
  132. var hasNumeral = FONT_NUMERAL_WEIGHTS.indexOf(values[0][1]) > -1 ||
  133. values[1] && FONT_NUMERAL_WEIGHTS.indexOf(values[1][1]) > -1 ||
  134. values[2] && FONT_NUMERAL_WEIGHTS.indexOf(values[2][1]) > -1;
  135. var canOptimizeFontWeight = options.level[OptimizationLevel.One].optimizeFontWeight;
  136. var normalCount = 0;
  137. var toOptimize;
  138. if (!canOptimizeFontWeight) {
  139. return;
  140. }
  141. if (hasNumeral) {
  142. return;
  143. }
  144. if (values[1] && values[1][1] == '/') {
  145. return;
  146. }
  147. if (values[0][1] == 'normal') {
  148. normalCount++;
  149. }
  150. if (values[1] && values[1][1] == 'normal') {
  151. normalCount++;
  152. }
  153. if (values[2] && values[2][1] == 'normal') {
  154. normalCount++;
  155. }
  156. if (normalCount > 1) {
  157. return;
  158. }
  159. if (FONT_NAME_WEIGHTS_WITHOUT_NORMAL.indexOf(values[0][1]) > -1) {
  160. toOptimize = 0;
  161. } else if (values[1] && FONT_NAME_WEIGHTS_WITHOUT_NORMAL.indexOf(values[1][1]) > -1) {
  162. toOptimize = 1;
  163. } else if (values[2] && FONT_NAME_WEIGHTS_WITHOUT_NORMAL.indexOf(values[2][1]) > -1) {
  164. toOptimize = 2;
  165. } else if (FONT_NAME_WEIGHTS.indexOf(values[0][1]) > -1) {
  166. toOptimize = 0;
  167. } else if (values[1] && FONT_NAME_WEIGHTS.indexOf(values[1][1]) > -1) {
  168. toOptimize = 1;
  169. } else if (values[2] && FONT_NAME_WEIGHTS.indexOf(values[2][1]) > -1) {
  170. toOptimize = 2;
  171. }
  172. if (toOptimize !== undefined && canOptimizeFontWeight) {
  173. optimizeFontWeight(property, toOptimize);
  174. property.dirty = true;
  175. }
  176. }
  177. function optimizeFontWeight(property, atIndex) {
  178. var value = property.value[atIndex][1];
  179. if (value == 'normal') {
  180. value = '400';
  181. } else if (value == 'bold') {
  182. value = '700';
  183. }
  184. property.value[atIndex][1] = value;
  185. }
  186. function optimizeMultipleZeros(property) {
  187. var values = property.value;
  188. var spliceAt;
  189. if (values.length == 4 && values[0][1] === '0' && values[1][1] === '0' && values[2][1] === '0' && values[3][1] === '0') {
  190. if (property.name.indexOf('box-shadow') > -1) {
  191. spliceAt = 2;
  192. } else {
  193. spliceAt = 1;
  194. }
  195. }
  196. if (spliceAt) {
  197. property.value.splice(spliceAt);
  198. property.dirty = true;
  199. }
  200. }
  201. function optimizeOutline(property) {
  202. var values = property.value;
  203. if (values.length == 1 && values[0][1] == 'none') {
  204. values[0][1] = '0';
  205. }
  206. }
  207. function optimizePixelLengths(_, value, compatibility) {
  208. if (!WHOLE_PIXEL_VALUE.test(value)) {
  209. return value;
  210. }
  211. return value.replace(WHOLE_PIXEL_VALUE, function (match, val) {
  212. var newValue;
  213. var intVal = parseInt(val);
  214. if (intVal === 0) {
  215. return match;
  216. }
  217. if (compatibility.properties.shorterLengthUnits && compatibility.units.pt && intVal * 3 % 4 === 0) {
  218. newValue = intVal * 3 / 4 + 'pt';
  219. }
  220. if (compatibility.properties.shorterLengthUnits && compatibility.units.pc && intVal % 16 === 0) {
  221. newValue = intVal / 16 + 'pc';
  222. }
  223. if (compatibility.properties.shorterLengthUnits && compatibility.units.in && intVal % 96 === 0) {
  224. newValue = intVal / 96 + 'in';
  225. }
  226. if (newValue) {
  227. newValue = match.substring(0, match.indexOf(val)) + newValue;
  228. }
  229. return newValue && newValue.length < match.length ? newValue : match;
  230. });
  231. }
  232. function optimizePrecision(_, value, precisionOptions) {
  233. if (!precisionOptions.enabled || value.indexOf('.') === -1) {
  234. return value;
  235. }
  236. return value
  237. .replace(precisionOptions.decimalPointMatcher, '$1$2$3')
  238. .replace(precisionOptions.zeroMatcher, function (match, integerPart, fractionPart, unit) {
  239. var multiplier = precisionOptions.units[unit].multiplier;
  240. var parsedInteger = parseInt(integerPart);
  241. var integer = isNaN(parsedInteger) ? 0 : parsedInteger;
  242. var fraction = parseFloat(fractionPart);
  243. return Math.round((integer + fraction) * multiplier) / multiplier + unit;
  244. });
  245. }
  246. function optimizeTimeUnits(_, value) {
  247. if (!TIME_VALUE.test(value))
  248. return value;
  249. return value.replace(TIME_VALUE, function (match, val, unit) {
  250. var newValue;
  251. if (unit == 'ms') {
  252. newValue = parseInt(val) / 1000 + 's';
  253. } else if (unit == 's') {
  254. newValue = parseFloat(val) * 1000 + 'ms';
  255. }
  256. return newValue.length < match.length ? newValue : match;
  257. });
  258. }
  259. function optimizeUnits(name, value, unitsRegexp) {
  260. if (/^(?:\-moz\-calc|\-webkit\-calc|calc|rgb|hsl|rgba|hsla)\(/.test(value)) {
  261. return value;
  262. }
  263. if (name == 'flex' || name == '-ms-flex' || name == '-webkit-flex' || name == 'flex-basis' || name == '-webkit-flex-basis') {
  264. return value;
  265. }
  266. if (value.indexOf('%') > 0 && (name == 'height' || name == 'max-height')) {
  267. return value;
  268. }
  269. return value
  270. .replace(unitsRegexp, '$1' + '0' + '$2')
  271. .replace(unitsRegexp, '$1' + '0' + '$2');
  272. }
  273. function optimizeWhitespace(name, value) {
  274. if (name.indexOf('filter') > -1 || value.indexOf(' ') == -1 || value.indexOf('expression') === 0) {
  275. return value;
  276. }
  277. if (value.indexOf(Marker.SINGLE_QUOTE) > -1 || value.indexOf(Marker.DOUBLE_QUOTE) > -1) {
  278. return value;
  279. }
  280. value = value.replace(/\s+/g, ' ');
  281. if (value.indexOf('calc') > -1) {
  282. value = value.replace(/\) ?\/ ?/g, ')/ ');
  283. }
  284. return value
  285. .replace(/(\(;?)\s+/g, '$1')
  286. .replace(/\s+(;?\))/g, '$1')
  287. .replace(/, /g, ',');
  288. }
  289. function optimizeZeroDegUnit(_, value) {
  290. if (value.indexOf('0deg') == -1) {
  291. return value;
  292. }
  293. return value.replace(/\(0deg\)/g, '(0)');
  294. }
  295. function optimizeZeroUnits(name, value) {
  296. if (value.indexOf('0') == -1) {
  297. return value;
  298. }
  299. if (value.indexOf('-') > -1) {
  300. value = value
  301. .replace(/([^\w\d\-]|^)\-0([^\.]|$)/g, '$10$2')
  302. .replace(/([^\w\d\-]|^)\-0([^\.]|$)/g, '$10$2');
  303. }
  304. return value
  305. .replace(/(^|\s)0+([1-9])/g, '$1$2')
  306. .replace(/(^|\D)\.0+(\D|$)/g, '$10$2')
  307. .replace(/(^|\D)\.0+(\D|$)/g, '$10$2')
  308. .replace(/\.([1-9]*)0+(\D|$)/g, function (match, nonZeroPart, suffix) {
  309. return (nonZeroPart.length > 0 ? '.' : '') + nonZeroPart + suffix;
  310. })
  311. .replace(/(^|\D)0\.(\d)/g, '$1.$2');
  312. }
  313. function removeQuotes(name, value) {
  314. if (name == 'content' || name.indexOf('font-feature-settings') > -1 || name.indexOf('grid-') > -1) {
  315. return value;
  316. }
  317. return QUOTED_BUT_SAFE_PATTERN.test(value) ?
  318. value.substring(1, value.length - 1) :
  319. value;
  320. }
  321. function removeUrlQuotes(value) {
  322. return /^url\(['"].+['"]\)$/.test(value) && !/^url\(['"].*[\*\s\(\)'"].*['"]\)$/.test(value) && !/^url\(['"]data:[^;]+;charset/.test(value) ?
  323. value.replace(/["']/g, '') :
  324. value;
  325. }
  326. function transformValue(propertyName, propertyValue, transformCallback) {
  327. var transformedValue = transformCallback(propertyName, propertyValue);
  328. if (transformedValue === undefined) {
  329. return propertyValue;
  330. } else if (transformedValue === false) {
  331. return IgnoreProperty;
  332. } else {
  333. return transformedValue;
  334. }
  335. }
  336. //
  337. function optimizeBody(properties, context) {
  338. var options = context.options;
  339. var levelOptions = options.level[OptimizationLevel.One];
  340. var property, name, type, value;
  341. var valueIsUrl;
  342. var propertyToken;
  343. var _properties = wrapForOptimizing(properties, true);
  344. propertyLoop:
  345. for (var i = 0, l = _properties.length; i < l; i++) {
  346. property = _properties[i];
  347. name = property.name;
  348. if (!PROPERTY_NAME_PATTERN.test(name)) {
  349. propertyToken = property.all[property.position];
  350. context.warnings.push('Invalid property name \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.');
  351. property.unused = true;
  352. }
  353. if (property.value.length === 0) {
  354. propertyToken = property.all[property.position];
  355. context.warnings.push('Empty property \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.');
  356. property.unused = true;
  357. }
  358. if (property.hack && (
  359. (property.hack[0] == Hack.ASTERISK || property.hack[0] == Hack.UNDERSCORE) && !options.compatibility.properties.iePrefixHack ||
  360. property.hack[0] == Hack.BACKSLASH && !options.compatibility.properties.ieSuffixHack ||
  361. property.hack[0] == Hack.BANG && !options.compatibility.properties.ieBangHack)) {
  362. property.unused = true;
  363. }
  364. if (levelOptions.removeNegativePaddings && name.indexOf('padding') === 0 && (isNegative(property.value[0]) || isNegative(property.value[1]) || isNegative(property.value[2]) || isNegative(property.value[3]))) {
  365. property.unused = true;
  366. }
  367. if (!options.compatibility.properties.ieFilters && isLegacyFilter(property)) {
  368. property.unused = true;
  369. }
  370. if (property.unused) {
  371. continue;
  372. }
  373. if (property.block) {
  374. optimizeBody(property.value[0][1], context);
  375. continue;
  376. }
  377. if (VARIABLE_NAME_PATTERN.test(name)) {
  378. continue;
  379. }
  380. for (var j = 0, m = property.value.length; j < m; j++) {
  381. type = property.value[j][0];
  382. value = property.value[j][1];
  383. valueIsUrl = isUrl(value);
  384. if (type == Token.PROPERTY_BLOCK) {
  385. property.unused = true;
  386. context.warnings.push('Invalid value token at ' + formatPosition(value[0][1][2][0]) + '. Ignoring.');
  387. break;
  388. }
  389. if (valueIsUrl && !context.validator.isValidUrl(value)) {
  390. property.unused = true;
  391. context.warnings.push('Broken URL \'' + value + '\' at ' + formatPosition(property.value[j][2][0]) + '. Ignoring.');
  392. break;
  393. }
  394. if (valueIsUrl) {
  395. value = levelOptions.normalizeUrls ?
  396. normalizeUrl(value) :
  397. value;
  398. value = !options.compatibility.properties.urlQuotes ?
  399. removeUrlQuotes(value) :
  400. value;
  401. } else if (isQuoted(value)) {
  402. value = levelOptions.removeQuotes ?
  403. removeQuotes(name, value) :
  404. value;
  405. } else {
  406. value = levelOptions.removeWhitespace ?
  407. optimizeWhitespace(name, value) :
  408. value;
  409. value = optimizePrecision(name, value, options.precision);
  410. value = optimizePixelLengths(name, value, options.compatibility);
  411. value = levelOptions.replaceTimeUnits ?
  412. optimizeTimeUnits(name, value) :
  413. value;
  414. value = levelOptions.replaceZeroUnits ?
  415. optimizeZeroUnits(name, value) :
  416. value;
  417. if (options.compatibility.properties.zeroUnits) {
  418. value = optimizeZeroDegUnit(name, value);
  419. value = optimizeUnits(name, value, options.unitsRegexp);
  420. }
  421. if (options.compatibility.properties.colors) {
  422. value = optimizeColors(name, value, options.compatibility);
  423. }
  424. }
  425. value = transformValue(name, value, levelOptions.transform);
  426. if (value === IgnoreProperty) {
  427. property.unused = true;
  428. continue propertyLoop;
  429. }
  430. property.value[j][1] = value;
  431. }
  432. if (levelOptions.replaceMultipleZeros) {
  433. optimizeMultipleZeros(property);
  434. }
  435. if (name == 'background' && levelOptions.optimizeBackground) {
  436. optimizeBackground(property);
  437. } else if (name.indexOf('border') === 0 && name.indexOf('radius') > 0 && levelOptions.optimizeBorderRadius) {
  438. optimizeBorderRadius(property);
  439. } else if (name == 'filter'&& levelOptions.optimizeFilter && options.compatibility.properties.ieFilters) {
  440. optimizeFilter(property);
  441. } else if (name == 'font' && levelOptions.optimizeFont) {
  442. optimizeFont(property, options);
  443. } else if (name == 'font-weight' && levelOptions.optimizeFontWeight) {
  444. optimizeFontWeight(property, 0);
  445. } else if (name == 'outline' && levelOptions.optimizeOutline) {
  446. optimizeOutline(property);
  447. }
  448. }
  449. restoreFromOptimizing(_properties);
  450. removeUnused(_properties);
  451. if (_properties.length != properties.length) {
  452. removeComments(properties, options);
  453. }
  454. }
  455. function removeComments(tokens, options) {
  456. var token;
  457. var i;
  458. for (i = 0; i < tokens.length; i++) {
  459. token = tokens[i];
  460. if (token[0] != Token.COMMENT) {
  461. continue;
  462. }
  463. optimizeComment(token, options);
  464. if (token[1].length === 0) {
  465. tokens.splice(i, 1);
  466. i--;
  467. }
  468. }
  469. }
  470. function optimizeComment(token, options) {
  471. if (token[1][2] == Marker.EXCLAMATION && (options.level[OptimizationLevel.One].specialComments == 'all' || options.commentsKept < options.level[OptimizationLevel.One].specialComments)) {
  472. options.commentsKept++;
  473. return;
  474. }
  475. token[1] = [];
  476. }
  477. function cleanupCharsets(tokens) {
  478. var hasCharset = false;
  479. for (var i = 0, l = tokens.length; i < l; i++) {
  480. var token = tokens[i];
  481. if (token[0] != Token.AT_RULE)
  482. continue;
  483. if (!CHARSET_REGEXP.test(token[1]))
  484. continue;
  485. if (hasCharset || token[1].indexOf(CHARSET_TOKEN) == -1) {
  486. tokens.splice(i, 1);
  487. i--;
  488. l--;
  489. } else {
  490. hasCharset = true;
  491. tokens.splice(i, 1);
  492. tokens.unshift([Token.AT_RULE, token[1].replace(CHARSET_REGEXP, CHARSET_TOKEN)]);
  493. }
  494. }
  495. }
  496. function buildUnitRegexp(options) {
  497. var units = ['px', 'em', 'ex', 'cm', 'mm', 'in', 'pt', 'pc', '%'];
  498. var otherUnits = ['ch', 'rem', 'vh', 'vm', 'vmax', 'vmin', 'vw'];
  499. otherUnits.forEach(function (unit) {
  500. if (options.compatibility.units[unit]) {
  501. units.push(unit);
  502. }
  503. });
  504. return new RegExp('(^|\\s|\\(|,)0(?:' + units.join('|') + ')(\\W|$)', 'g');
  505. }
  506. function buildPrecisionOptions(roundingPrecision) {
  507. var precisionOptions = {
  508. matcher: null,
  509. units: {},
  510. };
  511. var optimizable = [];
  512. var unit;
  513. var value;
  514. for (unit in roundingPrecision) {
  515. value = roundingPrecision[unit];
  516. if (value != DEFAULT_ROUNDING_PRECISION) {
  517. precisionOptions.units[unit] = {};
  518. precisionOptions.units[unit].value = value;
  519. precisionOptions.units[unit].multiplier = Math.pow(10, value);
  520. optimizable.push(unit);
  521. }
  522. }
  523. if (optimizable.length > 0) {
  524. precisionOptions.enabled = true;
  525. precisionOptions.decimalPointMatcher = new RegExp('(\\d)\\.($|' + optimizable.join('|') + ')($|\W)', 'g');
  526. precisionOptions.zeroMatcher = new RegExp('(\\d*)(\\.\\d+)(' + optimizable.join('|') + ')', 'g');
  527. }
  528. return precisionOptions;
  529. }
  530. function isImport(token) {
  531. return IMPORT_PREFIX_PATTERN.test(token[1]);
  532. }
  533. function isLegacyFilter(property) {
  534. var value;
  535. if (property.name == 'filter' || property.name == '-ms-filter') {
  536. value = property.value[0][1];
  537. return value.indexOf('progid') > -1 ||
  538. value.indexOf('alpha') === 0 ||
  539. value.indexOf('chroma') === 0;
  540. } else {
  541. return false;
  542. }
  543. }
  544. function level1Optimize(tokens, context) {
  545. var options = context.options;
  546. var levelOptions = options.level[OptimizationLevel.One];
  547. var ie7Hack = options.compatibility.selectors.ie7Hack;
  548. var adjacentSpace = options.compatibility.selectors.adjacentSpace;
  549. var spaceAfterClosingBrace = options.compatibility.properties.spaceAfterClosingBrace;
  550. var format = options.format;
  551. var mayHaveCharset = false;
  552. var afterRules = false;
  553. options.unitsRegexp = options.unitsRegexp || buildUnitRegexp(options);
  554. options.precision = options.precision || buildPrecisionOptions(levelOptions.roundingPrecision);
  555. options.commentsKept = options.commentsKept || 0;
  556. for (var i = 0, l = tokens.length; i < l; i++) {
  557. var token = tokens[i];
  558. switch (token[0]) {
  559. case Token.AT_RULE:
  560. token[1] = isImport(token) && afterRules ? '' : token[1];
  561. token[1] = levelOptions.tidyAtRules ? tidyAtRule(token[1]) : token[1];
  562. mayHaveCharset = true;
  563. break;
  564. case Token.AT_RULE_BLOCK:
  565. optimizeBody(token[2], context);
  566. afterRules = true;
  567. break;
  568. case Token.NESTED_BLOCK:
  569. token[1] = levelOptions.tidyBlockScopes ? tidyBlock(token[1], spaceAfterClosingBrace) : token[1];
  570. level1Optimize(token[2], context);
  571. afterRules = true;
  572. break;
  573. case Token.COMMENT:
  574. optimizeComment(token, options);
  575. break;
  576. case Token.RULE:
  577. token[1] = levelOptions.tidySelectors ? tidyRules(token[1], !ie7Hack, adjacentSpace, format, context.warnings) : token[1];
  578. token[1] = token[1].length > 1 ? sortSelectors(token[1], levelOptions.selectorsSortingMethod) : token[1];
  579. optimizeBody(token[2], context);
  580. afterRules = true;
  581. break;
  582. }
  583. if (token[1].length === 0 || (token[2] && token[2].length === 0)) {
  584. tokens.splice(i, 1);
  585. i--;
  586. l--;
  587. }
  588. }
  589. if (levelOptions.cleanupCharsets && mayHaveCharset) {
  590. cleanupCharsets(tokens);
  591. }
  592. return tokens;
  593. }
  594. module.exports = level1Optimize;