[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 |
|
---|
| 8 | var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
|
---|
| 9 |
|
---|
| 10 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
---|
| 11 |
|
---|
| 12 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
---|
| 13 |
|
---|
| 14 | const directionKeywords = ['top', 'right', 'bottom', 'left', 'center'];
|
---|
| 15 | const center = '50%';
|
---|
| 16 | const horizontal = {
|
---|
| 17 | right: '100%',
|
---|
| 18 | left: '0'
|
---|
| 19 | };
|
---|
| 20 | const verticalValue = {
|
---|
| 21 | bottom: '100%',
|
---|
| 22 | top: '0'
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | function isCommaNode(node) {
|
---|
| 26 | return node.type === 'div' && node.value === ',';
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | function isVariableFunctionNode(node) {
|
---|
| 30 | if (node.type !== 'function') {
|
---|
| 31 | return false;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return ['var', 'env'].includes(node.value.toLowerCase());
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function isMathFunctionNode(node) {
|
---|
| 38 | if (node.type !== 'function') {
|
---|
| 39 | return false;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase());
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | function isNumberNode(node) {
|
---|
| 46 | if (node.type !== 'word') {
|
---|
| 47 | return false;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | const value = parseFloat(node.value);
|
---|
| 51 | return !isNaN(value);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | function isDimensionNode(node) {
|
---|
| 55 | if (node.type !== 'word') {
|
---|
| 56 | return false;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | const parsed = (0, _postcssValueParser.unit)(node.value);
|
---|
| 60 |
|
---|
| 61 | if (!parsed) {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | return parsed.unit !== '';
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | function transform(value) {
|
---|
| 69 | const parsed = (0, _postcssValueParser.default)(value);
|
---|
| 70 | const ranges = [];
|
---|
| 71 | let rangeIndex = 0;
|
---|
| 72 | let shouldContinue = true;
|
---|
| 73 | parsed.nodes.forEach((node, index) => {
|
---|
| 74 | // After comma (`,`) follows next background
|
---|
| 75 | if (isCommaNode(node)) {
|
---|
| 76 | rangeIndex += 1;
|
---|
| 77 | shouldContinue = true;
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if (!shouldContinue) {
|
---|
| 82 | return;
|
---|
| 83 | } // After separator (`/`) follows `background-size` values
|
---|
| 84 | // Avoid them
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | if (node.type === 'div' && node.value === '/') {
|
---|
| 88 | shouldContinue = false;
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (!ranges[rangeIndex]) {
|
---|
| 93 | ranges[rangeIndex] = {
|
---|
| 94 | start: null,
|
---|
| 95 | end: null
|
---|
| 96 | };
|
---|
| 97 | } // Do not try to be processed `var and `env` function inside background
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | if (isVariableFunctionNode(node)) {
|
---|
| 101 | shouldContinue = false;
|
---|
| 102 | ranges[rangeIndex].start = null;
|
---|
| 103 | ranges[rangeIndex].end = null;
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | const isPositionKeyword = node.type === 'word' && directionKeywords.includes(node.value.toLowerCase()) || isDimensionNode(node) || isNumberNode(node) || isMathFunctionNode(node);
|
---|
| 108 |
|
---|
| 109 | if (ranges[rangeIndex].start === null && isPositionKeyword) {
|
---|
| 110 | ranges[rangeIndex].start = index;
|
---|
| 111 | ranges[rangeIndex].end = index;
|
---|
| 112 | return;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | if (ranges[rangeIndex].start !== null) {
|
---|
| 116 | if (node.type === 'space') {
|
---|
| 117 | return;
|
---|
| 118 | } else if (isPositionKeyword) {
|
---|
| 119 | ranges[rangeIndex].end = index;
|
---|
| 120 | return;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 | });
|
---|
| 126 | ranges.forEach(range => {
|
---|
| 127 | if (range.start === null) {
|
---|
| 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | const nodes = parsed.nodes.slice(range.start, range.end + 1);
|
---|
| 132 |
|
---|
| 133 | if (nodes.length > 3) {
|
---|
| 134 | return;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | const firstNode = nodes[0].value.toLowerCase();
|
---|
| 138 | const secondNode = nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
|
---|
| 139 |
|
---|
| 140 | if (nodes.length === 1 || secondNode === 'center') {
|
---|
| 141 | if (secondNode) {
|
---|
| 142 | nodes[2].value = nodes[1].value = '';
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | const map = Object.assign({}, horizontal, {
|
---|
| 146 | center
|
---|
| 147 | });
|
---|
| 148 |
|
---|
| 149 | if (Object.prototype.hasOwnProperty.call(map, firstNode)) {
|
---|
| 150 | nodes[0].value = map[firstNode];
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if (firstNode === 'center' && directionKeywords.includes(secondNode)) {
|
---|
| 157 | nodes[0].value = nodes[1].value = '';
|
---|
| 158 |
|
---|
| 159 | if (Object.prototype.hasOwnProperty.call(horizontal, secondNode)) {
|
---|
| 160 | nodes[2].value = horizontal[secondNode];
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | if (Object.prototype.hasOwnProperty.call(horizontal, firstNode) && Object.prototype.hasOwnProperty.call(verticalValue, secondNode)) {
|
---|
| 167 | nodes[0].value = horizontal[firstNode];
|
---|
| 168 | nodes[2].value = verticalValue[secondNode];
|
---|
| 169 | return;
|
---|
| 170 | } else if (Object.prototype.hasOwnProperty.call(verticalValue, firstNode) && Object.prototype.hasOwnProperty.call(horizontal, secondNode)) {
|
---|
| 171 | nodes[0].value = horizontal[secondNode];
|
---|
| 172 | nodes[2].value = verticalValue[firstNode];
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 | });
|
---|
| 176 | return parsed.toString();
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | function pluginCreator() {
|
---|
| 180 | return {
|
---|
| 181 | postcssPlugin: 'postcss-normalize-positions',
|
---|
| 182 |
|
---|
| 183 | OnceExit(css) {
|
---|
| 184 | const cache = {};
|
---|
| 185 | css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => {
|
---|
| 186 | const value = decl.value;
|
---|
| 187 |
|
---|
| 188 | if (!value) {
|
---|
| 189 | return;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | if (cache[value]) {
|
---|
| 193 | decl.value = cache[value];
|
---|
| 194 | return;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | const result = transform(value);
|
---|
| 198 | decl.value = result;
|
---|
| 199 | cache[value] = result;
|
---|
| 200 | });
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | };
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | pluginCreator.postcss = true;
|
---|
| 207 | var _default = pluginCreator;
|
---|
| 208 | exports.default = _default;
|
---|
| 209 | module.exports = exports.default; |
---|