Skip to content

Commit 424cac8

Browse files
authored
Merge pull request #2996 from mjmlio/bugfix/MJML-39-mj-table-cellspacing
fix(mjml-table): cellspacing is respected #2995
2 parents 4d975b0 + 3703f32 commit 424cac8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

packages/mjml-table/src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export default class MjTable extends BodyComponent {
4545
}
4646

4747
getStyles() {
48+
const hasCellspacing = this.hasCellspacing()
4849
return {
4950
table: {
5051
color: this.getAttribute('color'),
@@ -54,6 +55,7 @@ export default class MjTable extends BodyComponent {
5455
'table-layout': this.getAttribute('table-layout'),
5556
width: this.getAttribute('width'),
5657
border: this.getAttribute('border'),
58+
...(hasCellspacing && { 'border-collapse': 'separate' }),
5759
},
5860
}
5961
}
@@ -69,6 +71,12 @@ export default class MjTable extends BodyComponent {
6971
return unit === '%' ? width : parsedWidth
7072
}
7173

74+
hasCellspacing() {
75+
const cellspacing = this.getAttribute('cellspacing')
76+
const numericValue = parseFloat(String(cellspacing).replace(/[^\d.]/g, ''))
77+
return !Number.isNaN(numericValue) && numericValue > 0
78+
}
79+
7280
render() {
7381
const tableAttributes = reduce(
7482
['cellpadding', 'cellspacing', 'role'],
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const chai = require('chai')
2+
const { load } = require('cheerio')
3+
const mjml = require('../lib')
4+
5+
describe('mj-table cellspacing', function () {
6+
it('should render correct cellspacing (and border-collapse) in HTML tag / CSS style values on mj-table', function () {
7+
const input = `
8+
<mjml>
9+
<mj-body>
10+
<mj-section>
11+
<mj-column>
12+
<mj-table border="1px solid #000" width="auto" cellpadding="20" cellspacing="10" css-class="my-table">
13+
<tr style="border-bottom:1px solid #000;text-align:left;">
14+
<th style="background:#ddd;">Year</th>
15+
<th style="background:#ddd;">Language</th>
16+
<th style="background:#ddd;">Inspired from</th>
17+
</tr>
18+
<tr>
19+
<td style="background:#ddd;">1995</td>
20+
<td style="background:#ddd;">PHP</td>
21+
<td style="background:#ddd;">C, Shell Unix</td>
22+
</tr>
23+
</mj-table>
24+
</mj-column>
25+
</mj-section>
26+
</mj-body>
27+
</mjml>
28+
`
29+
30+
const { html } = mjml(input)
31+
32+
const $ = load(html)
33+
34+
// border radius values should be correct
35+
chai
36+
.expect(
37+
$('.my-table > table')
38+
.map(function getAttr() {
39+
return $(this).attr('cellspacing')
40+
})
41+
.get(),
42+
'cellspacing values on table elements',
43+
)
44+
.to.eql(['10'])
45+
46+
// border collapse values should be correct
47+
chai
48+
.expect(
49+
$('.my-table > table')
50+
.map(function getAttr() {
51+
const start = $(this).attr('style').indexOf('border-collapse:') + 16
52+
const end = $(this).attr('style').indexOf(';', start)
53+
return $(this).attr('style').substring(start, end)
54+
})
55+
.get(),
56+
'Border-collapse in CSS style values on mj-table',
57+
)
58+
.to.eql(['separate'])
59+
})
60+
})

0 commit comments

Comments
 (0)