index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var postcss = require('postcss');
  3. module.exports = postcss.plugin('postcss-zindex', function (opts) {
  4. opts = opts || {};
  5. return function (css) {
  6. var cache = require('./lib/layerCache')(opts);
  7. var nodes = [];
  8. var abort = false;
  9. // First pass; cache all z indexes
  10. css.walkDecls('z-index', function (decl) {
  11. // Check that no negative values exist. Rebasing is only
  12. // safe if all indices are positive numbers.
  13. if (decl.value[0] === '-') {
  14. abort = true;
  15. // Stop PostCSS iterating through the rest of the decls
  16. return false;
  17. }
  18. nodes.push(decl);
  19. cache.addValue(decl.value);
  20. });
  21. // Abort if we found any negative values
  22. // or there are no z-index declarations
  23. if (abort || !nodes.length) {
  24. return;
  25. }
  26. cache.optimizeValues();
  27. // Second pass; optimize
  28. nodes.forEach(function (decl) {
  29. // Need to coerce to string so that the
  30. // AST is updated correctly
  31. decl.value = cache.getValue(decl.value).toString();
  32. });
  33. };
  34. });