UNPKG

postcss-short-size

Version:
1 lines 4.38 kB
{"version":3,"file":"index.es.mjs","sources":["index.js"],"sourcesContent":["import postcss from 'postcss';\n\nexport default postcss.plugin('postcss-short-size', opts => {\n\tconst prefix = 'prefix' in Object(opts) ? `-${opts.prefix}-` : '';\n\tconst skip = 'skip' in Object(opts) ? String(opts.skip) : '*';\n\n\t// property pattern\n\tconst sizePropertyRegExp = new RegExp(`^${prefix}((?:max|min)-)?size$`, 'i');\n\n\treturn root => {\n\t\t// for each size declaration\n\t\troot.walkDecls(sizePropertyRegExp, decl => {\n\t\t\t// min-max property\n\t\t\tconst minmax = decl.prop.match(sizePropertyRegExp)[1] || '';\n\n\t\t\t// space-separated values (width, height)\n\t\t\tconst values = postcss.list.space(decl.value);\n\n\t\t\t// width is the first value\n\t\t\tlet width = values[0];\n\n\t\t\t// whether the width matches a length or aspect ratio\n\t\t\tconst widthLength = width.match(lengthRegExp);\n\t\t\tconst widthAspectRatio = width.match(aspectRatioRegExp);\n\n\t\t\t// height is the second value\n\t\t\tlet height = values[1] || values[0];\n\n\t\t\t// whether the height matches a length or aspect ratio\n\t\t\tconst heightLength = height.match(lengthRegExp);\n\t\t\tconst heightAspectRatio = height.match(aspectRatioRegExp);\n\n\t\t\t// conditionally update the width when it is an aspect ratio and the height is a length\n\t\t\tif (widthAspectRatio && heightLength) {\n\t\t\t\twidth = heightLength[1] / widthAspectRatio[2] * widthAspectRatio[1] + heightLength[2];\n\t\t\t}\n\n\t\t\t// conditionally update the height when it is an aspect ratio and the width is a length\n\t\t\tif (heightAspectRatio && widthLength) {\n\t\t\t\theight = widthLength[1] / heightAspectRatio[1] * heightAspectRatio[2] + widthLength[2];\n\t\t\t}\n\n\t\t\t// conditionally create a new width declaration if the width is not a skip token\n\t\t\tif (width !== skip) {\n\t\t\t\tdecl.cloneBefore({\n\t\t\t\t\tprop: `${minmax}width`,\n\t\t\t\t\tvalue: width\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// conditionally create a new height declaration if the height is not a skip token\n\t\t\tif (height !== skip) {\n\t\t\t\tdecl.cloneBefore({\n\t\t\t\t\tprop: `${minmax}height`,\n\t\t\t\t\tvalue: height\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// remove the original size declaration\n\t\t\tdecl.remove();\n\t\t});\n\t};\n});\n\nconst lengthRegExp = /^([-+]?0|[-+]?[0-9]*\\.?[0-9]+)(%|\\w+)$/;\nconst aspectRatioRegExp = /^([-+]?[0-9]*\\.?[0-9]+)\\/([-+]?[0-9]*\\.?[0-9]+)$/;\n"],"names":["postcss","plugin","opts","prefix","Object","skip","String","sizePropertyRegExp","RegExp","root","walkDecls","decl","minmax","prop","match","values","list","space","value","width","widthLength","lengthRegExp","widthAspectRatio","aspectRatioRegExp","height","heightLength","heightAspectRatio","cloneBefore","remove"],"mappings":";;AAEA,YAAeA,OAAO,CAACC,MAAR,CAAe,oBAAf,EAAqCC,IAAI,IAAI;QACrDC,MAAM,GAAG,YAAYC,MAAM,CAACF,IAAD,CAAlB,GAA4B,IAAGA,IAAI,CAACC,MAAO,GAA3C,GAAgD,EAA/D;QACME,IAAI,GAAG,UAAUD,MAAM,CAACF,IAAD,CAAhB,GAAyBI,MAAM,CAACJ,IAAI,CAACG,IAAN,CAA/B,GAA6C,GAA1D,CAF2D;;QAKrDE,kBAAkB,GAAG,IAAIC,MAAJ,CAAY,IAAGL,MAAO,sBAAtB,EAA6C,GAA7C,CAA3B;SAEOM,IAAI,IAAI;;IAEdA,IAAI,CAACC,SAAL,CAAeH,kBAAf,EAAmCI,IAAI,IAAI;;YAEpCC,MAAM,GAAGD,IAAI,CAACE,IAAL,CAAUC,KAAV,CAAgBP,kBAAhB,EAAoC,CAApC,KAA0C,EAAzD,CAF0C;;YAKpCQ,MAAM,GAAGf,OAAO,CAACgB,IAAR,CAAaC,KAAb,CAAmBN,IAAI,CAACO,KAAxB,CAAf,CAL0C;;UAQtCC,KAAK,GAAGJ,MAAM,CAAC,CAAD,CAAlB,CAR0C;;YAWp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