Skip to content

Commit abaa8d1

Browse files
committed
add getPrefixedStyleProperty to browser utilities
1 parent 319f560 commit abaa8d1

2 files changed

Lines changed: 77 additions & 21 deletions

File tree

src/utils/browser.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
export const getPrefixedStyleProperty = (() => {
2+
let properties = {}
3+
const style = document.documentElement.style
4+
5+
function getPrefixedStyleProperty (name, source = style) {
6+
if (name && typeof name === 'string') {
7+
if (properties[name]) {
8+
return properties[name]
9+
}
10+
if (typeof source[name] === 'string') {
11+
return properties[name] = name
12+
}
13+
if (typeof source[`-webkit-${name}`] === 'string') {
14+
return properties[name] = `-webkit-${name}`
15+
}
16+
throw new RangeError(`Unable to find "${name}" style property.`)
17+
}
18+
throw new TypeError('Expected a string.')
19+
}
20+
21+
getPrefixedStyleProperty.clearCache = () => properties = {}
22+
23+
return getPrefixedStyleProperty
24+
})()
25+
26+
127
export function isMobile (agent = navigator.userAgent) {
228
return /Android|iPhone|iPad|iPod/i.test(agent)
329
}
@@ -26,27 +52,6 @@ export function isNodeList (target) {
2652
}
2753

2854

29-
// export const getComputedProperty = (name => {
30-
// const properties = {}
31-
//
32-
// return () => {
33-
// if (properties[name]) return properties[name]
34-
//
35-
// if (typeof name === 'string') {
36-
// const computed = window.getComputedStyle()
37-
// if (typeof computed[name] === 'string') {
38-
// return properties[name] = name
39-
// }
40-
// if (typeof computed[`-webkit-${name}`] === 'string') {
41-
// return properties[name] = `-webkit-${name}`
42-
// }
43-
// throw new RangeError(`Unable to find "${name}" in computed style properties.`)
44-
// }
45-
// throw new Error('Expected a string.')
46-
// }
47-
// })()
48-
49-
5055
export function transformSupported () {
5156
const style = document.documentElement.style
5257
return 'transform' in style || 'WebkitTransform' in style

test/utils/browser.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,57 @@ import * as browser from '../../src/utils/browser'
33

44
describe('Browser Utilities', () => {
55

6+
describe('getPrefixedStyleProperty()', () => {
7+
8+
beforeEach('clear cache', () => {
9+
browser.getPrefixedStyleProperty.clearCache()
10+
})
11+
12+
it('should return unprefixed properties before prefixed', () => {
13+
const source = {
14+
transform: '',
15+
'-webkit-transform': '',
16+
}
17+
const result = browser.getPrefixedStyleProperty('transform', source)
18+
expect(result).to.equal('transform')
19+
})
20+
21+
it('should return prefixed property names', () => {
22+
const source = { '-webkit-transform': '' }
23+
const result = browser.getPrefixedStyleProperty('transform', source)
24+
expect(result).to.equal('-webkit-transform')
25+
})
26+
27+
it('should return property names from cache when available', () => {
28+
const source = { '-webkit-transform': '' }
29+
browser.getPrefixedStyleProperty('transform', source)
30+
const result = browser.getPrefixedStyleProperty('transform', {})
31+
expect(result).to.equal('-webkit-transform')
32+
})
33+
34+
it('should throw a range error when no property is found', () => {
35+
let caught
36+
try {
37+
browser.getPrefixedStyleProperty('transform', {})
38+
} catch (error) {
39+
caught = error
40+
}
41+
expect(caught).to.exist
42+
expect(caught).to.be.an.instanceof(RangeError)
43+
})
44+
45+
it('should throw a type error if not passed a string', () => {
46+
let caught
47+
try {
48+
browser.getPrefixedStyleProperty(null)
49+
} catch (error) {
50+
caught = error
51+
}
52+
expect(caught).to.exist
53+
expect(caught).to.be.an.instanceof(TypeError)
54+
})
55+
})
56+
657
describe('isMobile()', () => {
758

859
it('should return true when passed a mobile user agent', () => {

0 commit comments

Comments
 (0)