Skip to content

Commit

Permalink
~ css 小数单位的检测
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Dec 6, 2016
1 parent 1d86d53 commit 679a529
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "blear.utils.compatible",
"version": "1.0.3",
"version": "1.0.4",
"description": "浏览器兼容性",
"scripts": {
"live": "browser-sync start --config bs-config.js",
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Expand Up @@ -14,6 +14,8 @@ var newTestEl = function () {
return doc.createElement('p');
};
var testEl = newTestEl();
// @link http://www.w3cplus.com/css/the-lengths-of-css.html
var reUnit = /(px|em|rem|in|cm|mm|pt|pc|ex|ch|vw|vh|vmin|vmax|%)/i;


/**
Expand Down Expand Up @@ -118,7 +120,6 @@ exports.css = function (standardKey, standardVal) {
array.each(browserCSSPrefixList, function (index, prefix) {
var setVal = prefix ? prefix + '-' + standardVal : standardVal;
// 防止一个元素多次测试互相影响
//
var testEl = newTestEl();

try {
Expand All @@ -130,7 +131,9 @@ exports.css = function (standardKey, standardVal) {

var cssText = testEl.style.cssText.toLowerCase();

if (cssText.indexOf(checkVal) > -1) {
// 如果值里有单位说明是正确的 || 值里包含检查数据也是正确的
// 12.34567px => 12.346px
if (reUnit.test(checkVal) || cssText.indexOf(checkVal) > -1) {
findVal = setVal;
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions test/test.index.js
Expand Up @@ -54,6 +54,7 @@ describe('index.js', function () {
var ret2 = compatible.css('display', 'flex');
var ret3 = compatible.css('border-radius', '2px');
var ret4 = compatible.css('filter', 'blur(2px)');
var ret5 = compatible.css('width', '11.11111px');

expect(ret1.key).toBe('width');
setStyle(divEl, ret1.key, '100px');
Expand All @@ -76,5 +77,7 @@ describe('index.js', function () {
setStyle(divEl, ret4.key, ret4.val);
expect(getStyle(divEl, ret4.key)).toEqual(ret4.val);
}

expect(ret5.key).toBe('width');
});
});

0 comments on commit 679a529

Please sign in to comment.