index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var postcss = require('postcss');
  2. var makeFallbackFunction = require('./lib/rules-fabric');
  3. module.exports = postcss.plugin('postcss-initial', function (opts) {
  4. opts = opts || {};
  5. opts.reset = opts.reset || 'all';
  6. opts.replace = opts.replace || false;
  7. var getFallback = makeFallbackFunction(opts.reset === 'inherited');
  8. var getPropPrevTo = function (prop, decl) {
  9. var foundPrev = false;
  10. decl.parent.walkDecls(function (child) {
  11. if (child.prop === decl.prop && child.value !== decl.value) {
  12. foundPrev = true;
  13. }
  14. });
  15. return foundPrev;
  16. };
  17. return function (css) {
  18. css.walkDecls(function (decl) {
  19. if (!/\binitial\b/.test(decl.value)) {
  20. return;
  21. }
  22. var fallBackRules = getFallback(decl.prop, decl.value);
  23. if (fallBackRules.length === 0) return;
  24. fallBackRules.forEach(function (rule) {
  25. if ( !getPropPrevTo(decl.prop, decl) ) {
  26. decl.cloneBefore(rule);
  27. }
  28. });
  29. if (opts.replace === true) {
  30. decl.remove();
  31. }
  32. });
  33. };
  34. });