4
1

tidy-block.js 1006 B

12345678910111213141516171819202122232425262728293031323334
  1. var SUPPORTED_COMPACT_BLOCK_MATCHER = /^@media\W/;
  2. var SUPPORTED_QUOTE_REMOVAL_MATCHER = /^@(?:keyframes|-moz-keyframes|-o-keyframes|-webkit-keyframes)\W/;
  3. function tidyBlock(values, spaceAfterClosingBrace) {
  4. var withoutSpaceAfterClosingBrace;
  5. var withoutQuotes;
  6. var i;
  7. for (i = values.length - 1; i >= 0; i--) {
  8. withoutSpaceAfterClosingBrace = !spaceAfterClosingBrace && SUPPORTED_COMPACT_BLOCK_MATCHER.test(values[i][1]);
  9. withoutQuotes = SUPPORTED_QUOTE_REMOVAL_MATCHER.test(values[i][1]);
  10. values[i][1] = values[i][1]
  11. .replace(/\n|\r\n/g, ' ')
  12. .replace(/\s+/g, ' ')
  13. .replace(/(,|:|\() /g, '$1')
  14. .replace(/ \)/g, ')');
  15. if (withoutQuotes) {
  16. values[i][1] = values[i][1]
  17. .replace(/'([a-zA-Z][a-zA-Z\d\-_]+)'/, '$1')
  18. .replace(/"([a-zA-Z][a-zA-Z\d\-_]+)"/, '$1');
  19. }
  20. if (withoutSpaceAfterClosingBrace) {
  21. values[i][1] = values[i][1]
  22. .replace(/\) /g, ')');
  23. }
  24. }
  25. return values;
  26. }
  27. module.exports = tidyBlock;