You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.1 KiB

3 months ago
  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. * Clone and add prefixes for at-rule
  13. */
  14. add(rule, prefix) {
  15. let prefixeds = this.prefixeds(rule)
  16. if (this.already(rule, prefixeds, prefix)) {
  17. return
  18. }
  19. let cloned = this.clone(rule, { selector: prefixeds[this.name][prefix] })
  20. rule.parent.insertBefore(rule, cloned)
  21. }
  22. /**
  23. * Is rule already prefixed before
  24. */
  25. already(rule, prefixeds, prefix) {
  26. let index = rule.parent.index(rule) - 1
  27. while (index >= 0) {
  28. let before = rule.parent.nodes[index]
  29. if (before.type !== 'rule') {
  30. return false
  31. }
  32. let some = false
  33. for (let key in prefixeds[this.name]) {
  34. let prefixed = prefixeds[this.name][key]
  35. if (before.selector === prefixed) {
  36. if (prefix === key) {
  37. return true
  38. } else {
  39. some = true
  40. break
  41. }
  42. }
  43. }
  44. if (!some) {
  45. return false
  46. }
  47. index -= 1
  48. }
  49. return false
  50. }
  51. /**
  52. * Is rule selectors need to be prefixed
  53. */
  54. check(rule) {
  55. if (rule.selector.includes(this.name)) {
  56. return !!rule.selector.match(this.regexp())
  57. }
  58. return false
  59. }
  60. /**
  61. * Return function to fast find prefixed selector
  62. */
  63. old(prefix) {
  64. return new OldSelector(this, prefix)
  65. }
  66. /**
  67. * All possible prefixes
  68. */
  69. possible() {
  70. return Browsers.prefixes()
  71. }
  72. /**
  73. * Return prefixed version of selector
  74. */
  75. prefixed(prefix) {
  76. return this.name.replace(/^(\W*)/, `$1${prefix}`)
  77. }
  78. /**
  79. * Return all possible selector prefixes
  80. */
  81. prefixeds(rule) {
  82. if (rule._autoprefixerPrefixeds) {
  83. if (rule._autoprefixerPrefixeds[this.name]) {
  84. return rule._autoprefixerPrefixeds
  85. }
  86. } else {
  87. rule._autoprefixerPrefixeds = {}
  88. }
  89. let prefixeds = {}
  90. if (rule.selector.includes(',')) {
  91. let ruleParts = list.comma(rule.selector)
  92. let toProcess = ruleParts.filter(el => el.includes(this.name))
  93. for (let prefix of this.possible()) {
  94. prefixeds[prefix] = toProcess
  95. .map(el => this.replace(el, prefix))
  96. .join(', ')
  97. }
  98. } else {
  99. for (let prefix of this.possible()) {
  100. prefixeds[prefix] = this.replace(rule.selector, prefix)
  101. }
  102. }
  103. rule._autoprefixerPrefixeds[this.name] = prefixeds
  104. return rule._autoprefixerPrefixeds
  105. }
  106. /**
  107. * Lazy loadRegExp for name
  108. */
  109. regexp(prefix) {
  110. if (!this.regexpCache.has(prefix)) {
  111. let name = prefix ? this.prefixed(prefix) : this.name
  112. this.regexpCache.set(
  113. prefix,
  114. new RegExp(`(^|[^:"'=])${utils.escapeRegexp(name)}`, 'gi')
  115. )
  116. }
  117. return this.regexpCache.get(prefix)
  118. }
  119. /**
  120. * Replace selectors by prefixed one
  121. */
  122. replace(selector, prefix) {
  123. return selector.replace(this.regexp(), `$1${this.prefixed(prefix)}`)
  124. }
  125. }
  126. module.exports = Selector