[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
|
---|
| 4 |
|
---|
| 5 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
---|
| 6 |
|
---|
| 7 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
---|
| 8 |
|
---|
| 9 | var postcss = require('postcss');
|
---|
| 10 |
|
---|
| 11 | var data = require('caniuse-lite').feature(require('caniuse-lite/data/features/css-featurequeries.js'));
|
---|
| 12 |
|
---|
| 13 | var Browsers = require('./browsers');
|
---|
| 14 |
|
---|
| 15 | var brackets = require('./brackets');
|
---|
| 16 |
|
---|
| 17 | var Value = require('./value');
|
---|
| 18 |
|
---|
| 19 | var utils = require('./utils');
|
---|
| 20 |
|
---|
| 21 | var supported = [];
|
---|
| 22 |
|
---|
| 23 | for (var browser in data.stats) {
|
---|
| 24 | var versions = data.stats[browser];
|
---|
| 25 |
|
---|
| 26 | for (var version in versions) {
|
---|
| 27 | var support = versions[version];
|
---|
| 28 |
|
---|
| 29 | if (/y/.test(support)) {
|
---|
| 30 | supported.push(browser + ' ' + version);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | var Supports = /*#__PURE__*/function () {
|
---|
| 36 | function Supports(Prefixes, all) {
|
---|
| 37 | this.Prefixes = Prefixes;
|
---|
| 38 | this.all = all;
|
---|
| 39 | }
|
---|
| 40 | /**
|
---|
| 41 | * Return prefixer only with @supports supported browsers
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | var _proto = Supports.prototype;
|
---|
| 46 |
|
---|
| 47 | _proto.prefixer = function prefixer() {
|
---|
| 48 | if (this.prefixerCache) {
|
---|
| 49 | return this.prefixerCache;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | var filtered = this.all.browsers.selected.filter(function (i) {
|
---|
| 53 | return supported.includes(i);
|
---|
| 54 | });
|
---|
| 55 | var browsers = new Browsers(this.all.browsers.data, filtered, this.all.options);
|
---|
| 56 | this.prefixerCache = new this.Prefixes(this.all.data, browsers, this.all.options);
|
---|
| 57 | return this.prefixerCache;
|
---|
| 58 | }
|
---|
| 59 | /**
|
---|
| 60 | * Parse string into declaration property and value
|
---|
| 61 | */
|
---|
| 62 | ;
|
---|
| 63 |
|
---|
| 64 | _proto.parse = function parse(str) {
|
---|
| 65 | var parts = str.split(':');
|
---|
| 66 | var prop = parts[0];
|
---|
| 67 | var value = parts[1];
|
---|
| 68 | if (!value) value = '';
|
---|
| 69 | return [prop.trim(), value.trim()];
|
---|
| 70 | }
|
---|
| 71 | /**
|
---|
| 72 | * Create virtual rule to process it by prefixer
|
---|
| 73 | */
|
---|
| 74 | ;
|
---|
| 75 |
|
---|
| 76 | _proto.virtual = function virtual(str) {
|
---|
| 77 | var _this$parse = this.parse(str),
|
---|
| 78 | prop = _this$parse[0],
|
---|
| 79 | value = _this$parse[1];
|
---|
| 80 |
|
---|
| 81 | var rule = postcss.parse('a{}').first;
|
---|
| 82 | rule.append({
|
---|
| 83 | prop: prop,
|
---|
| 84 | value: value,
|
---|
| 85 | raws: {
|
---|
| 86 | before: ''
|
---|
| 87 | }
|
---|
| 88 | });
|
---|
| 89 | return rule;
|
---|
| 90 | }
|
---|
| 91 | /**
|
---|
| 92 | * Return array of Declaration with all necessary prefixes
|
---|
| 93 | */
|
---|
| 94 | ;
|
---|
| 95 |
|
---|
| 96 | _proto.prefixed = function prefixed(str) {
|
---|
| 97 | var rule = this.virtual(str);
|
---|
| 98 |
|
---|
| 99 | if (this.disabled(rule.first)) {
|
---|
| 100 | return rule.nodes;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | var result = {
|
---|
| 104 | warn: function warn() {
|
---|
| 105 | return null;
|
---|
| 106 | }
|
---|
| 107 | };
|
---|
| 108 | var prefixer = this.prefixer().add[rule.first.prop];
|
---|
| 109 | prefixer && prefixer.process && prefixer.process(rule.first, result);
|
---|
| 110 |
|
---|
| 111 | for (var _iterator = _createForOfIteratorHelperLoose(rule.nodes), _step; !(_step = _iterator()).done;) {
|
---|
| 112 | var decl = _step.value;
|
---|
| 113 |
|
---|
| 114 | for (var _iterator2 = _createForOfIteratorHelperLoose(this.prefixer().values('add', rule.first.prop)), _step2; !(_step2 = _iterator2()).done;) {
|
---|
| 115 | var value = _step2.value;
|
---|
| 116 | value.process(decl);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | Value.save(this.all, decl);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return rule.nodes;
|
---|
| 123 | }
|
---|
| 124 | /**
|
---|
| 125 | * Return true if brackets node is "not" word
|
---|
| 126 | */
|
---|
| 127 | ;
|
---|
| 128 |
|
---|
| 129 | _proto.isNot = function isNot(node) {
|
---|
| 130 | return typeof node === 'string' && /not\s*/i.test(node);
|
---|
| 131 | }
|
---|
| 132 | /**
|
---|
| 133 | * Return true if brackets node is "or" word
|
---|
| 134 | */
|
---|
| 135 | ;
|
---|
| 136 |
|
---|
| 137 | _proto.isOr = function isOr(node) {
|
---|
| 138 | return typeof node === 'string' && /\s*or\s*/i.test(node);
|
---|
| 139 | }
|
---|
| 140 | /**
|
---|
| 141 | * Return true if brackets node is (prop: value)
|
---|
| 142 | */
|
---|
| 143 | ;
|
---|
| 144 |
|
---|
| 145 | _proto.isProp = function isProp(node) {
|
---|
| 146 | return typeof node === 'object' && node.length === 1 && typeof node[0] === 'string';
|
---|
| 147 | }
|
---|
| 148 | /**
|
---|
| 149 | * Return true if prefixed property has no unprefixed
|
---|
| 150 | */
|
---|
| 151 | ;
|
---|
| 152 |
|
---|
| 153 | _proto.isHack = function isHack(all, unprefixed) {
|
---|
| 154 | var check = new RegExp("(\\(|\\s)" + utils.escapeRegexp(unprefixed) + ":");
|
---|
| 155 | return !check.test(all);
|
---|
| 156 | }
|
---|
| 157 | /**
|
---|
| 158 | * Return true if we need to remove node
|
---|
| 159 | */
|
---|
| 160 | ;
|
---|
| 161 |
|
---|
| 162 | _proto.toRemove = function toRemove(str, all) {
|
---|
| 163 | var _this$parse2 = this.parse(str),
|
---|
| 164 | prop = _this$parse2[0],
|
---|
| 165 | value = _this$parse2[1];
|
---|
| 166 |
|
---|
| 167 | var unprefixed = this.all.unprefixed(prop);
|
---|
| 168 | var cleaner = this.all.cleaner();
|
---|
| 169 |
|
---|
| 170 | if (cleaner.remove[prop] && cleaner.remove[prop].remove && !this.isHack(all, unprefixed)) {
|
---|
| 171 | return true;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | for (var _iterator3 = _createForOfIteratorHelperLoose(cleaner.values('remove', unprefixed)), _step3; !(_step3 = _iterator3()).done;) {
|
---|
| 175 | var checker = _step3.value;
|
---|
| 176 |
|
---|
| 177 | if (checker.check(value)) {
|
---|
| 178 | return true;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | return false;
|
---|
| 183 | }
|
---|
| 184 | /**
|
---|
| 185 | * Remove all unnecessary prefixes
|
---|
| 186 | */
|
---|
| 187 | ;
|
---|
| 188 |
|
---|
| 189 | _proto.remove = function remove(nodes, all) {
|
---|
| 190 | var i = 0;
|
---|
| 191 |
|
---|
| 192 | while (i < nodes.length) {
|
---|
| 193 | if (!this.isNot(nodes[i - 1]) && this.isProp(nodes[i]) && this.isOr(nodes[i + 1])) {
|
---|
| 194 | if (this.toRemove(nodes[i][0], all)) {
|
---|
| 195 | nodes.splice(i, 2);
|
---|
| 196 | continue;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | i += 2;
|
---|
| 200 | continue;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | if (typeof nodes[i] === 'object') {
|
---|
| 204 | nodes[i] = this.remove(nodes[i], all);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | i += 1;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | return nodes;
|
---|
| 211 | }
|
---|
| 212 | /**
|
---|
| 213 | * Clean brackets with one child
|
---|
| 214 | */
|
---|
| 215 | ;
|
---|
| 216 |
|
---|
| 217 | _proto.cleanBrackets = function cleanBrackets(nodes) {
|
---|
| 218 | var _this = this;
|
---|
| 219 |
|
---|
| 220 | return nodes.map(function (i) {
|
---|
| 221 | if (typeof i !== 'object') {
|
---|
| 222 | return i;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if (i.length === 1 && typeof i[0] === 'object') {
|
---|
| 226 | return _this.cleanBrackets(i[0]);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | return _this.cleanBrackets(i);
|
---|
| 230 | });
|
---|
| 231 | }
|
---|
| 232 | /**
|
---|
| 233 | * Add " or " between properties and convert it to brackets format
|
---|
| 234 | */
|
---|
| 235 | ;
|
---|
| 236 |
|
---|
| 237 | _proto.convert = function convert(progress) {
|
---|
| 238 | var result = [''];
|
---|
| 239 |
|
---|
| 240 | for (var _iterator4 = _createForOfIteratorHelperLoose(progress), _step4; !(_step4 = _iterator4()).done;) {
|
---|
| 241 | var i = _step4.value;
|
---|
| 242 | result.push([i.prop + ": " + i.value]);
|
---|
| 243 | result.push(' or ');
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | result[result.length - 1] = '';
|
---|
| 247 | return result;
|
---|
| 248 | }
|
---|
| 249 | /**
|
---|
| 250 | * Compress value functions into a string nodes
|
---|
| 251 | */
|
---|
| 252 | ;
|
---|
| 253 |
|
---|
| 254 | _proto.normalize = function normalize(nodes) {
|
---|
| 255 | var _this2 = this;
|
---|
| 256 |
|
---|
| 257 | if (typeof nodes !== 'object') {
|
---|
| 258 | return nodes;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | nodes = nodes.filter(function (i) {
|
---|
| 262 | return i !== '';
|
---|
| 263 | });
|
---|
| 264 |
|
---|
| 265 | if (typeof nodes[0] === 'string' && nodes[0].includes(':')) {
|
---|
| 266 | return [brackets.stringify(nodes)];
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | return nodes.map(function (i) {
|
---|
| 270 | return _this2.normalize(i);
|
---|
| 271 | });
|
---|
| 272 | }
|
---|
| 273 | /**
|
---|
| 274 | * Add prefixes
|
---|
| 275 | */
|
---|
| 276 | ;
|
---|
| 277 |
|
---|
| 278 | _proto.add = function add(nodes, all) {
|
---|
| 279 | var _this3 = this;
|
---|
| 280 |
|
---|
| 281 | return nodes.map(function (i) {
|
---|
| 282 | if (_this3.isProp(i)) {
|
---|
| 283 | var prefixed = _this3.prefixed(i[0]);
|
---|
| 284 |
|
---|
| 285 | if (prefixed.length > 1) {
|
---|
| 286 | return _this3.convert(prefixed);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | return i;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | if (typeof i === 'object') {
|
---|
| 293 | return _this3.add(i, all);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | return i;
|
---|
| 297 | });
|
---|
| 298 | }
|
---|
| 299 | /**
|
---|
| 300 | * Add prefixed declaration
|
---|
| 301 | */
|
---|
| 302 | ;
|
---|
| 303 |
|
---|
| 304 | _proto.process = function process(rule) {
|
---|
| 305 | var ast = brackets.parse(rule.params);
|
---|
| 306 | ast = this.normalize(ast);
|
---|
| 307 | ast = this.remove(ast, rule.params);
|
---|
| 308 | ast = this.add(ast, rule.params);
|
---|
| 309 | ast = this.cleanBrackets(ast);
|
---|
| 310 | rule.params = brackets.stringify(ast);
|
---|
| 311 | }
|
---|
| 312 | /**
|
---|
| 313 | * Check global options
|
---|
| 314 | */
|
---|
| 315 | ;
|
---|
| 316 |
|
---|
| 317 | _proto.disabled = function disabled(node) {
|
---|
| 318 | if (!this.all.options.grid) {
|
---|
| 319 | if (node.prop === 'display' && node.value.includes('grid')) {
|
---|
| 320 | return true;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | if (node.prop.includes('grid') || node.prop === 'justify-items') {
|
---|
| 324 | return true;
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | if (this.all.options.flexbox === false) {
|
---|
| 329 | if (node.prop === 'display' && node.value.includes('flex')) {
|
---|
| 330 | return true;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | var other = ['order', 'justify-content', 'align-items', 'align-content'];
|
---|
| 334 |
|
---|
| 335 | if (node.prop.includes('flex') || other.includes(node.prop)) {
|
---|
| 336 | return true;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | return false;
|
---|
| 341 | };
|
---|
| 342 |
|
---|
| 343 | return Supports;
|
---|
| 344 | }();
|
---|
| 345 |
|
---|
| 346 | module.exports = Supports; |
---|