index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var postcss = require("postcss");
  2. /**
  3. * font variant convertion map
  4. *
  5. * @type {Object}
  6. */
  7. var fontVariantProperties = {
  8. "font-variant-ligatures": {
  9. "common-ligatures": "\"liga\", \"clig\"",
  10. "no-common-ligatures": "\"liga\", \"clig off\"",
  11. "discretionary-ligatures": "\"dlig\"",
  12. "no-discretionary-ligatures": "\"dlig\" off",
  13. "historical-ligatures": "\"hlig\"",
  14. "no-historical-ligatures": "\"hlig\" off",
  15. contextual: "\"calt\"",
  16. "no-contextual": "\"calt\" off"
  17. },
  18. "font-variant-position": {
  19. sub: "\"subs\"",
  20. "super": "\"sups\"",
  21. normal: "\"subs\" off, \"sups\" off"
  22. },
  23. "font-variant-caps": {
  24. "small-caps": "\"smcp\"",
  25. "all-small-caps": "\"smcp\", \"c2sc\"",
  26. "petite-caps": "\"pcap\"",
  27. "all-petite-caps": "\"pcap\", \"c2pc\"",
  28. unicase: "\"unic\"",
  29. "titling-caps": "\"titl\""
  30. },
  31. "font-variant-numeric": {
  32. "lining-nums": "\"lnum\"",
  33. "oldstyle-nums": "\"onum\"",
  34. "proportional-nums": "\"pnum\"",
  35. "tabular-nums": "\"tnum\"",
  36. "diagonal-fractions": "\"frac\"",
  37. "stacked-fractions": "\"afrc\"",
  38. ordinal: "\"ordn\"",
  39. "slashed-zero": "\"zero\""
  40. },
  41. "font-kerning": {
  42. normal: "\"kern\"",
  43. none: "\"kern\" off"
  44. },
  45. "font-variant": {
  46. normal: "normal",
  47. inherit: "inherit"
  48. }
  49. }
  50. // The `font-variant` property is a shorthand for all the others.
  51. for (var prop in fontVariantProperties) {
  52. var keys = fontVariantProperties[prop]
  53. for (var key in keys) {
  54. if (!(key in fontVariantProperties["font-variant"])) {
  55. fontVariantProperties["font-variant"][key] = keys[key]
  56. }
  57. }
  58. }
  59. // Find font-feature-settings declaration before given declaration,
  60. // create if does not exist
  61. function getFontFeatureSettingsPrevTo(decl) {
  62. var fontFeatureSettings = null;
  63. decl.parent.walkDecls(function(decl) {
  64. if (decl.prop === "font-feature-settings") {
  65. fontFeatureSettings = decl;
  66. }
  67. })
  68. if (fontFeatureSettings === null) {
  69. fontFeatureSettings = decl.clone()
  70. fontFeatureSettings.prop = "font-feature-settings"
  71. fontFeatureSettings.value = ""
  72. decl.parent.insertBefore(decl, fontFeatureSettings)
  73. }
  74. return fontFeatureSettings
  75. }
  76. /**
  77. * Expose the font-variant plugin.
  78. */
  79. module.exports = postcss.plugin("postcss-font-variant", function() {
  80. return function(styles) {
  81. styles.walkRules(function(rule) {
  82. var fontFeatureSettings = null
  83. // read custom media queries
  84. rule.walkDecls(function(decl) {
  85. if (!fontVariantProperties[decl.prop]) {
  86. return null
  87. }
  88. var newValue = decl.value
  89. if (decl.prop === "font-variant") {
  90. newValue = decl.value.split(/\s+/g).map(function(val) {
  91. return fontVariantProperties["font-variant"][val]
  92. }).join(", ")
  93. }
  94. else if (fontVariantProperties[decl.prop][decl.value]) {
  95. newValue = fontVariantProperties[decl.prop][decl.value]
  96. }
  97. if (fontFeatureSettings === null) {
  98. fontFeatureSettings = getFontFeatureSettingsPrevTo(decl);
  99. }
  100. if (fontFeatureSettings.value && fontFeatureSettings.value !== newValue) {
  101. fontFeatureSettings.value += ", " + newValue;
  102. }
  103. else {
  104. fontFeatureSettings.value = newValue;
  105. }
  106. })
  107. })
  108. }
  109. })