selector.js 4.9 KB

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