Skip to content

Commit 4fe5f86

Browse files
committed
Improve border consistency
1 parent aa76602 commit 4fe5f86

File tree

4 files changed

+26
-125
lines changed

4 files changed

+26
-125
lines changed

src/__tests__/border.js

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ it('transforms border shorthand missing color', () => {
5656
})
5757
})
5858

59-
it('transforms border shorthand missing style', () => {
60-
expect(transformCss([['border', '2px #f00']])).toEqual({
61-
borderTopWidth: 2,
62-
borderRightWidth: 2,
63-
borderBottomWidth: 2,
64-
borderLeftWidth: 2,
65-
borderTopColor: '#f00',
66-
borderRightColor: '#f00',
67-
borderBottomColor: '#f00',
68-
borderLeftColor: '#f00',
69-
borderStyle: 'solid',
70-
})
71-
})
72-
7359
it('transforms border shorthand missing width', () => {
7460
expect(transformCss([['border', '#f00 dashed']])).toEqual({
7561
borderTopWidth: 1,
@@ -98,34 +84,6 @@ it('transforms border shorthand missing color & width', () => {
9884
})
9985
})
10086

101-
it('transforms border shorthand missing style & width', () => {
102-
expect(transformCss([['border', '#f00']])).toEqual({
103-
borderTopWidth: 1,
104-
borderRightWidth: 1,
105-
borderBottomWidth: 1,
106-
borderLeftWidth: 1,
107-
borderTopColor: '#f00',
108-
borderRightColor: '#f00',
109-
borderBottomColor: '#f00',
110-
borderLeftColor: '#f00',
111-
borderStyle: 'solid',
112-
})
113-
})
114-
115-
it('transforms border shorthand missing color & style', () => {
116-
expect(transformCss([['border', '2px']])).toEqual({
117-
borderTopWidth: 2,
118-
borderRightWidth: 2,
119-
borderBottomWidth: 2,
120-
borderLeftWidth: 2,
121-
borderTopColor: 'black',
122-
borderRightColor: 'black',
123-
borderBottomColor: 'black',
124-
borderLeftColor: 'black',
125-
borderStyle: 'solid',
126-
})
127-
})
128-
12987
it('transforms border for unsupported units', () => {
13088
expect(transformCss([['border', '3em solid black']])).toEqual({
13189
borderTopWidth: '3em',
@@ -140,6 +98,18 @@ it('transforms border for unsupported units', () => {
14098
})
14199
})
142100

101+
it('does not transform border shorthand missing style', () => {
102+
expect(() => transformCss([['border', '2px #f00']])).toThrow()
103+
})
104+
105+
it('does not transform border shorthand missing style & width', () => {
106+
expect(() => transformCss([['border', '#f00']])).toThrow()
107+
})
108+
109+
it('does not transforms border shorthand missing color & style', () => {
110+
expect(() => transformCss([['border', '2px']])).toThrow()
111+
})
112+
143113
it('does not transform border with percentage width', () => {
144114
expect(() => transformCss([['border', '3% solid black']])).toThrow()
145115
})

src/__tests__/borderRight.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/__tests__/units.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe.each([
6666
borderStyle: 'dashed',
6767
})
6868

69-
expect(transformCss([['border', value]])).toEqual({
69+
expect(transformCss([['border', `${value} solid`]])).toEqual({
7070
borderTopWidth: value,
7171
borderRightWidth: value,
7272
borderBottomWidth: value,

src/transforms/border.js

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@ const BORDER_STYLE = regExpToken(/^(solid|dashed|dotted)$/)
1111

1212
const defaultBorderWidth = 1
1313
const defaultBorderColor = 'black'
14-
const defaultBorderStyle = 'solid'
1514

16-
const baseParse = (tokenStream, allowBorderStyle) => {
15+
export default tokenStream => {
1716
let borderWidth
1817
let borderColor
1918
let borderStyle
2019

2120
if (tokenStream.matches(NONE)) {
2221
tokenStream.expectEmpty()
23-
return { borderWidth: 0, borderColor: 'black', borderStyle: 'solid' }
22+
return {
23+
borderTopWidth: 0,
24+
borderRightWidth: 0,
25+
borderBottomWidth: 0,
26+
borderLeftWidth: 0,
27+
borderTopColor: 'black',
28+
borderRightColor: 'black',
29+
borderBottomColor: 'black',
30+
borderLeftColor: 'black',
31+
borderStyle: 'solid',
32+
}
2433
}
2534

2635
let partsParsed = 0
@@ -48,22 +57,11 @@ const baseParse = (tokenStream, allowBorderStyle) => {
4857
if (borderWidth === undefined) borderWidth = defaultBorderWidth
4958
if (borderColor === undefined) borderColor = defaultBorderColor
5059
if (borderStyle === undefined) {
51-
borderStyle = defaultBorderStyle
52-
} else if (!allowBorderStyle) {
5360
throw new Error(
54-
'Setting a border style on a single border side is not supported'
61+
'You must define a border style in the border shorthand (e.g. solid)'
5562
)
5663
}
5764

58-
return { borderWidth, borderColor, borderStyle }
59-
}
60-
61-
export default tokenStream => {
62-
// eslint-disable-next-line prefer-const
63-
let { borderWidth, borderColor, borderStyle } = baseParse(tokenStream, true)
64-
65-
if (borderStyle === undefined) borderStyle = defaultBorderStyle
66-
6765
return {
6866
borderTopWidth: borderWidth,
6967
borderRightWidth: borderWidth,
@@ -76,23 +74,3 @@ export default tokenStream => {
7674
borderStyle,
7775
}
7876
}
79-
80-
export const borderTop = tokenStream => {
81-
const { borderWidth, borderColor } = baseParse(tokenStream, false)
82-
return { borderTopWidth: borderWidth, borderTopColor: borderColor }
83-
}
84-
85-
export const borderRight = tokenStream => {
86-
const { borderWidth, borderColor } = baseParse(tokenStream, false)
87-
return { borderRightWidth: borderWidth, borderRightColor: borderColor }
88-
}
89-
90-
export const borderBottom = tokenStream => {
91-
const { borderWidth, borderColor } = baseParse(tokenStream, false)
92-
return { borderBottomWidth: borderWidth, borderBottomColor: borderColor }
93-
}
94-
95-
export const borderLeft = tokenStream => {
96-
const { borderWidth, borderColor } = baseParse(tokenStream, false)
97-
return { borderLeftWidth: borderWidth, borderLeftColor: borderColor }
98-
}

0 commit comments

Comments
 (0)