index.es.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import postcss from 'postcss';
  2. import parser from 'postcss-values-parser';
  3. const placeMatch = /^place-(content|items|self)/;
  4. var index = postcss.plugin('postcss-place', opts => {
  5. // prepare options
  6. const preserve = 'preserve' in Object(opts) ? Boolean(opts.prefix) : true;
  7. return root => {
  8. // walk each matching declaration
  9. root.walkDecls(placeMatch, decl => {
  10. // alignment
  11. const alignment = decl.prop.match(placeMatch)[1]; // value ast and child nodes
  12. const value = parser(decl.value).parse();
  13. const children = value.nodes[0].nodes; // new justify-[alignment] and align-[alignment] declarations
  14. const alignValue = children.length === 1 ? decl.value : String(children.slice(0, 1)).trim();
  15. const justifyValue = children.length === 1 ? decl.value : String(children.slice(1)).trim();
  16. decl.cloneBefore({
  17. prop: `align-${alignment}`,
  18. value: alignValue
  19. });
  20. decl.cloneBefore({
  21. prop: `justify-${alignment}`,
  22. value: justifyValue
  23. }); // conditionally remove place-[alignment]
  24. if (!preserve) {
  25. decl.remove();
  26. }
  27. });
  28. };
  29. });
  30. export default index;
  31. //# sourceMappingURL=index.es.mjs.map