selector.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. let { list } = require('postcss')
  2. let OldSelector = require('./old-selector')
  3. let Prefixer = require('./prefixer')
  4. let Browsers = require('./browsers')
  5. let utils = require('./utils')
  6. class Selector extends Prefixer {
  7. constructor(name, prefixes, all) {
  8. super(name, prefixes, all)
  9. this.regexpCache = new Map()
  10. }
  11. /**
  12. * Is rule selectors need to be prefixed
  13. */
  14. check(rule) {
  15. if (rule.selector.includes(this.name)) {
  16. return !!rule.selector.match(this.regexp())
  17. }
  18. return false
  19. }
  20. /**
  21. * Return prefixed version of selector
  22. */
  23. prefixed(prefix) {
  24. return this.name.replace(/^(\W*)/, `$1${prefix}`)
  25. }
  26. /**
  27. * Lazy loadRegExp for name
  28. */
  29. regexp(prefix) {
  30. if (!this.regexpCache.has(prefix)) {
  31. let name = prefix ? this.prefixed(prefix) : this.name
  32. this.regexpCache.set(
  33. prefix,
  34. new RegExp(`(^|[^:"'=])${utils.escapeRegexp(name)}`, 'gi')
  35. )
  36. }
  37. return this.regexpCache.get(prefix)
  38. }
  39. /**
  40. * All possible prefixes
  41. */
  42. possible() {
  43. return Browsers.prefixes()
  44. }
  45. /**
  46. * Return all possible selector prefixes
  47. */
  48. prefixeds(rule) {
  49. if (rule._autoprefixerPrefixeds) {
  50. if (rule._autoprefixerPrefixeds[this.name]) {
  51. return rule._autoprefixerPrefixeds
  52. }
  53. } else {
  54. rule._autoprefixerPrefixeds = {}
  55. }
  56. let prefixeds = {}
  57. if (rule.selector.includes(',')) {
  58. let ruleParts = list.comma(rule.selector)
  59. let toProcess = ruleParts.filter(el => el.includes(this.name))
  60. for (let prefix of this.possible()) {
  61. prefixeds[prefix] = toProcess
  62. .map(el => this.replace(el, prefix))
  63. .join(', ')
  64. }
  65. } else {
  66. for (let prefix of this.possible()) {
  67. prefixeds[prefix] = this.replace(rule.selector, prefix)
  68. }
  69. }
  70. rule._autoprefixerPrefixeds[this.name] = prefixeds
  71. return rule._autoprefixerPrefixeds
  72. }
  73. /**
  74. * Is rule already prefixed before
  75. */
  76. already(rule, prefixeds, prefix) {
  77. let index = rule.parent.index(rule) - 1
  78. while (index >= 0) {
  79. let before = rule.parent.nodes[index]
  80. if (before.type !== 'rule') {
  81. return false
  82. }
  83. let some = false
  84. for (let key in prefixeds[this.name]) {
  85. let prefixed = prefixeds[this.name][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. 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. add(rule, prefix) {
  112. let prefixeds = this.prefixeds(rule)
  113. if (this.already(rule, prefixeds, prefix)) {
  114. return
  115. }
  116. let cloned = this.clone(rule, { selector: prefixeds[this.name][prefix] })
  117. rule.parent.insertBefore(rule, cloned)
  118. }
  119. /**
  120. * Return function to fast find prefixed selector
  121. */
  122. old(prefix) {
  123. return new OldSelector(this, prefix)
  124. }
  125. }
  126. module.exports = Selector