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