a zip code crypto-currency system good for red ONLY

optimize.js 20KB

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