selector.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
  6. var OldSelector = require('./old-selector');
  7. var Prefixer = require('./prefixer');
  8. var Browsers = require('./browsers');
  9. var utils = require('./utils');
  10. var Selector = function (_Prefixer) {
  11. _inherits(Selector, _Prefixer);
  12. function Selector(name, prefixes, all) {
  13. _classCallCheck(this, Selector);
  14. var _this = _possibleConstructorReturn(this, _Prefixer.call(this, name, prefixes, all));
  15. _this.regexpCache = {};
  16. return _this;
  17. }
  18. /**
  19. * Is rule selectors need to be prefixed
  20. */
  21. Selector.prototype.check = function check(rule) {
  22. if (rule.selector.indexOf(this.name) !== -1) {
  23. return !!rule.selector.match(this.regexp());
  24. }
  25. return false;
  26. };
  27. /**
  28. * Return prefixed version of selector
  29. */
  30. Selector.prototype.prefixed = function prefixed(prefix) {
  31. return this.name.replace(/^([^\w]*)/, '$1' + prefix);
  32. };
  33. /**
  34. * Lazy loadRegExp for name
  35. */
  36. Selector.prototype.regexp = function regexp(prefix) {
  37. if (this.regexpCache[prefix]) {
  38. return this.regexpCache[prefix];
  39. }
  40. var name = prefix ? this.prefixed(prefix) : this.name;
  41. this.regexpCache[prefix] = new RegExp('(^|[^:"\'=])' + utils.escapeRegexp(name), 'gi');
  42. return this.regexpCache[prefix];
  43. };
  44. /**
  45. * All possible prefixes
  46. */
  47. Selector.prototype.possible = function possible() {
  48. return Browsers.prefixes();
  49. };
  50. /**
  51. * Return all possible selector prefixes
  52. */
  53. Selector.prototype.prefixeds = function prefixeds(rule) {
  54. if (rule._autoprefixerPrefixeds) {
  55. return rule._autoprefixerPrefixeds;
  56. }
  57. var prefixeds = {};
  58. for (var _iterator = this.possible(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  59. var _ref;
  60. if (_isArray) {
  61. if (_i >= _iterator.length) break;
  62. _ref = _iterator[_i++];
  63. } else {
  64. _i = _iterator.next();
  65. if (_i.done) break;
  66. _ref = _i.value;
  67. }
  68. var prefix = _ref;
  69. prefixeds[prefix] = this.replace(rule.selector, prefix);
  70. }
  71. rule._autoprefixerPrefixeds = prefixeds;
  72. return rule._autoprefixerPrefixeds;
  73. };
  74. /**
  75. * Is rule already prefixed before
  76. */
  77. Selector.prototype.already = function already(rule, prefixeds, prefix) {
  78. var index = rule.parent.index(rule) - 1;
  79. while (index >= 0) {
  80. var before = rule.parent.nodes[index];
  81. if (before.type !== 'rule') {
  82. return false;
  83. }
  84. var some = false;
  85. for (var key in prefixeds) {
  86. var prefixed = prefixeds[key];
  87. if (before.selector === prefixed) {
  88. if (prefix === key) {
  89. return true;
  90. } else {
  91. some = true;
  92. break;
  93. }
  94. }
  95. }
  96. if (!some) {
  97. return false;
  98. }
  99. index -= 1;
  100. }
  101. return false;
  102. };
  103. /**
  104. * Replace selectors by prefixed one
  105. */
  106. Selector.prototype.replace = function replace(selector, prefix) {
  107. return selector.replace(this.regexp(), '$1' + this.prefixed(prefix));
  108. };
  109. /**
  110. * Clone and add prefixes for at-rule
  111. */
  112. Selector.prototype.add = function add(rule, prefix) {
  113. var prefixeds = this.prefixeds(rule);
  114. if (this.already(rule, prefixeds, prefix)) {
  115. return;
  116. }
  117. var cloned = this.clone(rule, { selector: prefixeds[prefix] });
  118. rule.parent.insertBefore(rule, cloned);
  119. };
  120. /**
  121. * Return function to fast find prefixed selector
  122. */
  123. Selector.prototype.old = function old(prefix) {
  124. return new OldSelector(this, prefix);
  125. };
  126. return Selector;
  127. }(Prefixer);
  128. module.exports = Selector;