All files / build/PostCSSPrefixWrap OurPlugin.js

100% Statements 46/46
100% Branches 21/21
100% Functions 13/13
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93    1x   3x   1x 1x 1x 1x 1x   7x 5x   7x         7x         7x 7x   1x 1x 7x     1x   1x 36x 36x 1x     35x 7x     28x   6x     3x     25x 19x       6x 2x       4x   1x 30x   1x 30x 30x 1x   29x     36x         1x 7x 7x 10x 30x       1x   1x  
"use strict";
var __importDefault =
  (this && this.__importDefault) ||
  function(mod) {
    return mod && mod.__esModule ? mod : { default: mod };
  };
Object.defineProperty(exports, "__esModule", { value: true });
var postcss_1 = __importDefault(require("postcss"));
var selector_1 = __importDefault(require("./selector"));
var hash_1 = __importDefault(require("./hash"));
var OurPlugin = /** @class */ (function() {
  function OurPlugin(prefixSelector, options) {
    if (options === void 0) {
      options = {};
    }
    this.ignoredSelectors = hash_1.default.value(
      options,
      "ignoredSelectors",
      []
    );
    this.prefixRootTags = hash_1.default.value(
      options,
      "prefixRootTags",
      false
    );
    this.isPrefixSelector = new RegExp("^s*" + prefixSelector + ".*$");
    this.prefixSelector = prefixSelector;
  }
  OurPlugin.asPostCSSPlugin = function() {
    var initializer = function(prefixSelector, options) {
      return new OurPlugin(prefixSelector, options).process();
    };
    // @ts-ignore
    return postcss_1.default.plugin("postcss-prefixwrap", initializer);
  };
  OurPlugin.prototype.prefixWrapCSSSelector = function(cssSelector, cssRule) {
    var cleanSelector = selector_1.default.clean(cssSelector);
    if (cleanSelector === "") {
      return null;
    }
    // Do not prefix keyframes rules.
    if (selector_1.default.isKeyframes(cssRule)) {
      return cleanSelector;
    }
    // Check for matching ignored selectors
    if (
      this.ignoredSelectors.some(function(currentValue) {
        return cleanSelector.match(currentValue);
      })
    ) {
      return cleanSelector;
    }
    // Anything other than a root tag is always prefixed.
    if (selector_1.default.isNotRootTag(cleanSelector)) {
      return this.prefixSelector + " " + cleanSelector;
    }
    // Handle special case where root tags should be converted into classes
    // rather than being replaced.
    if (this.prefixRootTags) {
      return this.prefixSelector + " ." + cleanSelector;
    }
    // HTML and Body elements cannot be contained within our container so lets
    // extract their styles.
    return cleanSelector.replace(/^(body|html)/, this.prefixSelector);
  };
  OurPlugin.prototype.cssRuleMatchesPrefixSelector = function(cssRule) {
    return cssRule.selector.match(this.isPrefixSelector) !== null;
  };
  OurPlugin.prototype.prefixWrapCSSRule = function(cssRule) {
    var _this = this;
    if (this.cssRuleMatchesPrefixSelector(cssRule)) {
      return;
    }
    cssRule.selector = cssRule.selector
      .split(",")
      .map(function(cssSelector) {
        return _this.prefixWrapCSSSelector(cssSelector, cssRule);
      })
      .filter(selector_1.default.isValid)
      .join(", ");
  };
  OurPlugin.prototype.process = function() {
    var _this = this;
    return function(css) {
      css.walkRules(function(cssRule) {
        _this.prefixWrapCSSRule(cssRule);
      });
    };
  };
  return OurPlugin;
})();
exports.default = OurPlugin;