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