This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Vendor prefix #128
Merged
Merged
Vendor prefix #128
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42156d9
Simple prefixing based on css-vendor
ianobermiller c290eff
Use css-style prefixes for values
ianobermiller b939fc4
Support optional css-style prefixes
ianobermiller 0e65ccf
Fix lint
ianobermiller bcb899b
Fix tests by including a simple mock for prefix
ianobermiller 33eaa1e
Fix lint in prefix mock.
ianobermiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
var prefixMock = function (style) { | ||
return style; | ||
}; | ||
|
||
module.exports = prefixMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* Based on https://github.com/jsstyles/css-vendor, but without any dash-case | ||
* shenanigans. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var kebabCase = require('lodash/string/kebabCase'); | ||
|
||
var jsCssMap = { | ||
Webkit: '-webkit-', | ||
Moz: '-moz-', | ||
// IE did it wrong again ... | ||
ms: '-ms-', | ||
O: '-o-' | ||
}; | ||
var testProp = 'Transform'; | ||
|
||
var domStyle = document.createElement('p').style; | ||
var prefixedPropertyCache = {}; | ||
var prefixedValueCache = {}; | ||
var jsVendorPrefix = ''; | ||
var cssVendorPrefix = ''; | ||
|
||
for (var js in jsCssMap) { | ||
if ((js + testProp) in domStyle) { | ||
jsVendorPrefix = js; | ||
cssVendorPrefix = jsCssMap[js]; | ||
break; | ||
} | ||
} | ||
|
||
var _getPrefixedProperty = function (property) { | ||
if (prefixedPropertyCache.hasOwnProperty(property)) { | ||
return prefixedPropertyCache[property]; | ||
} | ||
|
||
if (property in domStyle) { | ||
// unprefixed | ||
prefixedPropertyCache[property] = { | ||
css: kebabCase(property), | ||
js: property | ||
}; | ||
return prefixedPropertyCache[property]; | ||
} | ||
|
||
var newProperty = | ||
jsVendorPrefix + property[0].toUpperCase() + property.slice(1); | ||
if (newProperty in domStyle) { | ||
// prefixed | ||
prefixedPropertyCache[property] = { | ||
css: cssVendorPrefix + kebabCase(property), | ||
js: newProperty | ||
}; | ||
return prefixedPropertyCache[property]; | ||
} | ||
|
||
// unsupported | ||
return prefixedPropertyCache[property] = false; | ||
}; | ||
|
||
var _getPrefixedValue = function (property, value) { | ||
// don't test numbers or numbers with units (e.g. 10em) | ||
if (typeof value !== 'string' || !isNaN(parseInt(value, 10))) { | ||
return value; | ||
} | ||
|
||
var cacheKey = property + value; | ||
|
||
if (prefixedValueCache.hasOwnProperty(cacheKey)) { | ||
return prefixedValueCache[cacheKey]; | ||
} | ||
|
||
// Clear style first | ||
domStyle[property] = ''; | ||
|
||
// Test value as it is. | ||
domStyle[property] = value; | ||
|
||
// Value is supported as it is. Note that we just make sure it is not an empty | ||
// string. Browsers will sometimes rewrite values, but still accept them. They | ||
// will set the value to an empty string if not supported. | ||
// E.g. for border, "solid 1px black" becomes "1px solid black" | ||
// but "foobar" becomes "", since it is not supported. | ||
if (domStyle[property]) { | ||
prefixedValueCache[cacheKey] = value; | ||
return value; | ||
} | ||
|
||
// Test value with vendor prefix. | ||
value = cssVendorPrefix + value; | ||
domStyle[property] = value; | ||
|
||
// Value is supported with vendor prefix. | ||
if (domStyle[property]) { | ||
prefixedValueCache[cacheKey] = value; | ||
return value; | ||
} | ||
|
||
return prefixedValueCache[cacheKey] = false; | ||
}; | ||
|
||
// Returns a new style object with vendor prefixes added to property names | ||
// and values. | ||
/*eslint-disable no-console */ | ||
var prefix = function (style, mode /* 'css' or 'js' */) { | ||
mode = mode || 'js'; | ||
var newStyle = {}; | ||
Object.keys(style).forEach(function (property) { | ||
var value = style[property]; | ||
|
||
var newProperty = _getPrefixedProperty(property); | ||
if (newProperty === false) { | ||
// Ignore unsupported properties | ||
console.warn('Unsupported CSS property ' + property); | ||
return; | ||
} | ||
|
||
var newValue = _getPrefixedValue(newProperty.js, value); | ||
if (newValue === false) { | ||
// Ignore unsupported values | ||
console.warn( | ||
'Unsupported CSS value ' + value + ' for property ' + property | ||
); | ||
} | ||
|
||
newStyle[newProperty[mode]] = newValue; | ||
}); | ||
return newStyle; | ||
}; | ||
/*eslint-enable no-console */ | ||
|
||
module.exports = prefix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessary since it runs in the browser. You can test all prefixes and once you found one that works, it becomes the prefix for the rest of the session.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not following you. What part is unnecessary? The code determines the prefix at the top of the file, and caches the result for individual properties/values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh apologies, I was completely misreading this. Need more coffee 💩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah no problem. Thanks much for taking a look!