Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fanich37 committed Oct 7, 2019
1 parent 3795794 commit 622e8a3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
37 changes: 32 additions & 5 deletions lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ let Prefixer = require('./prefixer')
let Browsers = require('./browsers')
let utils = require('./utils')

const SELECTORS_CACHE = {}

class Selector extends Prefixer {
constructor (name, prefixes, all) {
super(name, prefixes, all)
this.regexpCache = {}
SELECTORS_CACHE[name] = {
possible: this.possible(),
replace: this.replace.bind(this)
}
}

/**
Expand Down Expand Up @@ -57,8 +63,29 @@ class Selector extends Prefixer {
}

let prefixeds = {}
for (let prefix of this.possible()) {
prefixeds[prefix] = this.replace(rule.selector, prefix)

if (rule.selector.includes(',')) {
let toProcess = rule.selector
.split(',')
.filter(el => el.includes(':'))
.map(el => el.trim())

Object.keys(SELECTORS_CACHE).forEach(name => {
let selector = toProcess.find(el => el.includes(name))
if (selector) {
prefixeds[name] = {}
SELECTORS_CACHE[name].possible.forEach(prefix => {
prefixeds[name][prefix] = SELECTORS_CACHE[name].replace(
selector, prefix)
})
}
})
} else {
prefixeds[this.name] = {}

for (let prefix of this.possible()) {
prefixeds[this.name][prefix] = this.replace(rule.selector, prefix)
}
}

rule._autoprefixerPrefixeds = prefixeds
Expand All @@ -79,8 +106,8 @@ class Selector extends Prefixer {
}

let some = false
for (let key in prefixeds) {
let prefixed = prefixeds[key]
for (let key in prefixeds[this.name]) {
let prefixed = prefixeds[this.name][key]
if (before.selector === prefixed) {
if (prefix === key) {
return true
Expand Down Expand Up @@ -117,7 +144,7 @@ class Selector extends Prefixer {
return
}

let cloned = this.clone(rule, { selector: prefixeds[prefix] })
let cloned = this.clone(rule, { selector: prefixeds[this.name][prefix] })
rule.parent.insertBefore(rule, cloned)
}

Expand Down
14 changes: 8 additions & 6 deletions test/selector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('regexp()', () => {
})

describe('check()', () => {
it('shecks rule selectors', () => {
it('checks rule selectors', () => {
let css = parse('body .selection {}, ' +
':::selection {}, body ::selection {}')
expect(selector.check(css.nodes[0])).toBeFalsy()
Expand All @@ -38,13 +38,15 @@ describe('check()', () => {
})

describe('prefixeds()', () => {
it('returns all avaiable prefixed selectors', () => {
it('returns all available prefixed selectors', () => {
let css = parse('::selection {}')
expect(selector.prefixeds(css.first)).toEqual({
'-webkit-': '::-webkit-selection',
'-moz-': '::-moz-selection',
'-ms-': '::-ms-selection',
'-o-': '::-o-selection'
'::selection': {
'-webkit-': '::-webkit-selection',
'-moz-': '::-moz-selection',
'-ms-': '::-ms-selection',
'-o-': '::-o-selection'
}
})
})
})
Expand Down

0 comments on commit 622e8a3

Please sign in to comment.