Skip to content

Commit 9818b8c

Browse files
ka2jun8liqueflies
authored andcommitted
check tag type in create styled component
1 parent fe0744a commit 9818b8c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/constructors/styled.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
import css from './css'
22
import domElements from '../utils/domElements'
33

4+
// check valid vue-styled-component element type
5+
function isValidElementType (tag) {
6+
if (typeof tag === 'undefined' || typeof tag === 'number') {
7+
return false
8+
}
9+
if (typeof tag === 'string') {
10+
return domElements.includes(tag)
11+
}
12+
if (typeof tag === 'object') {
13+
return !!tag.template || !!tag.withComponent
14+
}
15+
return true
16+
}
17+
418
export default (createStyledComponent) => {
5-
const styled = (tagName, props = {}) =>
6-
(cssRules, ...interpolations) => (
19+
const styled = (tagName, props = {}) => {
20+
if (!isValidElementType(tagName)) {
21+
throw new Error(tagName + ' is not allowed for styled tag type.')
22+
}
23+
return (cssRules, ...interpolations) => (
724
createStyledComponent(tagName, css(cssRules, ...interpolations), props)
825
)
26+
}
927

1028
domElements.forEach((domElement) => {
1129
styled[domElement] = styled(domElement)

src/test/basic.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ describe('basic', () => {
2929
expectCSSMatches('')
3030
})
3131

32+
it('should throw an error when called', () => {
33+
expect(() => styled``).toThrow()
34+
expect(() => styled.notExistTag``).toThrow()
35+
})
36+
3237
// it('should generate an empty tag once rendered', () => {
3338
// const Comp = styled.div``
3439
// const vm = new Vue(Comp).$mount()

0 commit comments

Comments
 (0)