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. * Is rule a hack without unprefixed version bottom
  14. */
  15. isHack(rule) {
  16. let index = rule.parent.index(rule) + 1
  17. let rules = rule.parent.nodes
  18. while (index < rules.length) {
  19. let before = rules[index].selector
  20. if (!before) {
  21. return true
  22. }
  23. if (before.includes(this.unprefixed) && before.match(this.nameRegexp)) {
  24. return false
  25. }
  26. let some = false
  27. for (let [string, regexp] of this.prefixeds) {
  28. if (before.includes(string) && before.match(regexp)) {
  29. some = true
  30. break
  31. }
  32. }
  33. if (!some) {
  34. return true
  35. }
  36. index += 1
  37. }
  38. return true
  39. }
  40. /**
  41. * Does rule contain an unnecessary prefixed selector
  42. */
  43. check(rule) {
  44. if (!rule.selector.includes(this.prefixed)) {
  45. return false
  46. }
  47. if (!rule.selector.match(this.regexp)) {
  48. return false
  49. }
  50. if (this.isHack(rule)) {
  51. return false
  52. }
  53. return true
  54. }
  55. }
  56. module.exports = OldSelector