old-selector.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. var OldSelector = function () {
  4. function OldSelector(selector, prefix) {
  5. _classCallCheck(this, OldSelector);
  6. this.prefix = prefix;
  7. this.prefixed = selector.prefixed(this.prefix);
  8. this.regexp = selector.regexp(this.prefix);
  9. this.prefixeds = selector.possible().map(function (x) {
  10. return [selector.prefixed(x), selector.regexp(x)];
  11. });
  12. this.unprefixed = selector.name;
  13. this.nameRegexp = selector.regexp();
  14. }
  15. /**
  16. * Is rule a hack without unprefixed version bottom
  17. */
  18. OldSelector.prototype.isHack = function isHack(rule) {
  19. var index = rule.parent.index(rule) + 1;
  20. var rules = rule.parent.nodes;
  21. while (index < rules.length) {
  22. var before = rules[index].selector;
  23. if (!before) {
  24. return true;
  25. }
  26. if (before.indexOf(this.unprefixed) !== -1 && before.match(this.nameRegexp)) {
  27. return false;
  28. }
  29. var some = false;
  30. for (var _iterator = this.prefixeds, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  31. var _ref2;
  32. if (_isArray) {
  33. if (_i >= _iterator.length) break;
  34. _ref2 = _iterator[_i++];
  35. } else {
  36. _i = _iterator.next();
  37. if (_i.done) break;
  38. _ref2 = _i.value;
  39. }
  40. var _ref = _ref2;
  41. var string = _ref[0];
  42. var regexp = _ref[1];
  43. if (before.indexOf(string) !== -1 && before.match(regexp)) {
  44. some = true;
  45. break;
  46. }
  47. }
  48. if (!some) {
  49. return true;
  50. }
  51. index += 1;
  52. }
  53. return true;
  54. };
  55. /**
  56. * Does rule contain an unnecessary prefixed selector
  57. */
  58. OldSelector.prototype.check = function check(rule) {
  59. if (rule.selector.indexOf(this.prefixed) === -1) {
  60. return false;
  61. }
  62. if (!rule.selector.match(this.regexp)) {
  63. return false;
  64. }
  65. if (this.isHack(rule)) {
  66. return false;
  67. }
  68. return true;
  69. };
  70. return OldSelector;
  71. }();
  72. module.exports = OldSelector;