You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

676 lines
20 KiB

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