old-selector.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. class OldSelector {
  2. constructor(selector, prefix) {
  3. this.prefix = prefix
  4. this.prefixed = selector.prefixed(this.prefix)
  5. this.regexp = selector.regexp(this.prefix)
  6. this.prefixeds = selector
  7. .possible()
  8. .map(x => [selector.prefixed(x), selector.regexp(x)])
  9. this.unprefixed = selector.name
  10. this.nameRegexp = selector.regexp()
  11. }
  12. /**
  13. * Does rule contain an unnecessary prefixed selector
  14. */
  15. check(rule) {
  16. if (!rule.selector.includes(this.prefixed)) {
  17. return false
  18. }
  19. if (!rule.selector.match(this.regexp)) {
  20. return false
  21. }
  22. if (this.isHack(rule)) {
  23. return false
  24. }
  25. return true
  26. }
  27. /**
  28. * Is rule a hack without unprefixed version bottom
  29. */
  30. isHack(rule) {
  31. let index = rule.parent.index(rule) + 1
  32. let rules = rule.parent.nodes
  33. while (index < rules.length) {
  34. let before = rules[index].selector
  35. if (!before) {
  36. return true
  37. }
  38. if (before.includes(this.unprefixed) && before.match(this.nameRegexp)) {
  39. return false
  40. }
  41. let some = false
  42. for (let [string, regexp] of this.prefixeds) {
  43. if (before.includes(string) && 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. module.exports = OldSelector