-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [feature] Support server rendering (#4)
- Loading branch information
1 parent
53c4a97
commit 61d569f
Showing
11 changed files
with
144 additions
and
809 deletions.
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
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules/ | |
examples/__build__/ | ||
.nyc_output/ | ||
coverage/ | ||
dist |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,54 @@ | ||
/** | ||
* 驼峰写法 | ||
* @param {String} str 要转化的字符串 | ||
* @return {String} 转化后的字符串 | ||
*/ | ||
export function camelCase(str) { | ||
return str.replace(/-([a-z])/g, ($0, $1) => $1.toUpperCase()).replace('-', ''); | ||
} | ||
|
||
/** | ||
* 格式化css属性对象 | ||
* @param {Object} props 属性对象 | ||
* @return {Object} 添加前缀的格式化属性对象 | ||
*/ | ||
export function formatCss(props) { | ||
const prefixs = ['-webkit-', '-moz-', '-ms-']; | ||
|
||
const result = {}; | ||
|
||
const regPrefix = /transform|transition/; | ||
|
||
|
||
for (const key in props) { | ||
if (props.hasOwnProperty(key)) { | ||
const styleValue = props[key]; | ||
|
||
// 如果检测是transform或transition属性 | ||
if (regPrefix.test(key)) { | ||
for (let i = 0; i < prefixs.length; i++) { | ||
const styleName = camelCase(prefixs[i] + key); | ||
result[styleName] = styleValue.replace(regPrefix, `${prefixs[i]}$&`); | ||
} | ||
} | ||
|
||
result[key] = styleValue; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* 为元素添加css样式 | ||
* @param {Object} element 目标元素 | ||
* @param {Object} props css属性对象 | ||
*/ | ||
export function addPrefixCss(element, props) { | ||
const formatedProps = formatCss(props); | ||
for (const key in formatedProps) { | ||
if (formatedProps.hasOwnProperty(key)) { | ||
element.style[key] = formatedProps[key]; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,66 @@ | ||
|
||
import { expect } from 'chai'; | ||
import { formatCss, addPrefixCss } from '../../lib/prefix.js'; | ||
|
||
|
||
describe('prefix.js', () => { | ||
|
||
describe('formatCss', () => { | ||
it('should return correct formated css property when contain "transform"', function () { | ||
const date = { | ||
transform: 'translate(100px, 100px, 100px)', | ||
margin: '10px', | ||
}; | ||
|
||
expect(formatCss(date)).to.eql({ | ||
WebkitTransform: 'translate(100px, 100px, 100px)', | ||
MozTransform: 'translate(100px, 100px, 100px)', | ||
MsTransform: 'translate(100px, 100px, 100px)', | ||
transform: 'translate(100px, 100px, 100px)', | ||
margin: '10px', | ||
}); | ||
}); | ||
|
||
it('should return correct formated css property when contain "transition"', function () { | ||
const date = { | ||
transition: 'transform 2s', | ||
padding: 0, | ||
margin: '10px', | ||
}; | ||
|
||
expect(formatCss(date)).to.eql({ | ||
WebkitTransition: '-webkit-transform 2s', | ||
MozTransition: '-moz-transform 2s', | ||
MsTransition: '-ms-transform 2s', | ||
transition: 'transform 2s', | ||
padding: 0, | ||
margin: '10px', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('addPrefixCss', () => { | ||
it('should set correct style', () => { | ||
const mock = { | ||
style: {} | ||
}; | ||
|
||
const props = { | ||
transition: 'transform 2s', | ||
padding: 0, | ||
margin: '10px', | ||
}; | ||
|
||
addPrefixCss(mock, props); | ||
|
||
expect(mock.style).to.eql({ | ||
WebkitTransition: '-webkit-transform 2s', | ||
MozTransition: '-moz-transform 2s', | ||
MsTransition: '-ms-transform 2s', | ||
transition: 'transform 2s', | ||
padding: 0, | ||
margin: '10px', | ||
}); | ||
}); | ||
}); | ||
}) |