@@ -24,6 +24,8 @@ const own = {}.hasOwnProperty
24
24
const cap = / [ A - Z ] / g
25
25
const dashSomething = / - ( [ a - z ] ) / g
26
26
27
+ const tableCellElement = new Set ( [ 'td' , 'th' ] )
28
+
27
29
/**
28
30
* Turn a hast element into an estree node.
29
31
*
@@ -51,6 +53,10 @@ export function element(node, state) {
51
53
const attributes = [ ]
52
54
/** @type {string } */
53
55
let prop
56
+ /** @type {string | undefined } */
57
+ let alignValue
58
+ /** @type {Array<Property> | undefined } */
59
+ let styleProperties
54
60
55
61
for ( prop in props ) {
56
62
if ( own . call ( props , prop ) ) {
@@ -89,7 +95,7 @@ export function element(node, state) {
89
95
: parseStyle ( String ( value ) , node . tagName )
90
96
91
97
if ( state . stylePropertyNameCase === 'css' ) {
92
- styleObject = transformStyleToCssCasing ( styleObject )
98
+ styleObject = transformStylesToCssCasing ( styleObject )
93
99
}
94
100
95
101
/** @type {Array<Property> } */
@@ -114,12 +120,20 @@ export function element(node, state) {
114
120
}
115
121
}
116
122
123
+ styleProperties = cssProperties
117
124
attributeValue = {
118
125
type : 'JSXExpressionContainer' ,
119
126
expression : { type : 'ObjectExpression' , properties : cssProperties }
120
127
}
121
128
} else if ( value === true ) {
122
129
attributeValue = null
130
+ } else if (
131
+ state . tableCellAlignToStyle &&
132
+ tableCellElement . has ( node . tagName ) &&
133
+ prop === 'align'
134
+ ) {
135
+ alignValue = String ( value )
136
+ continue
123
137
} else {
124
138
attributeValue = { type : 'Literal' , value : String ( value ) }
125
139
}
@@ -154,6 +168,37 @@ export function element(node, state) {
154
168
}
155
169
}
156
170
171
+ if ( alignValue !== undefined ) {
172
+ if ( ! styleProperties ) {
173
+ styleProperties = [ ]
174
+ attributes . push ( {
175
+ type : 'JSXAttribute' ,
176
+ name : { type : 'JSXIdentifier' , name : 'style' } ,
177
+ value : {
178
+ type : 'JSXExpressionContainer' ,
179
+ expression : { type : 'ObjectExpression' , properties : styleProperties }
180
+ }
181
+ } )
182
+ }
183
+
184
+ const cssProp =
185
+ state . stylePropertyNameCase === 'css'
186
+ ? transformStyleToCssCasing ( 'textAlign' )
187
+ : 'textAlign'
188
+
189
+ styleProperties . push ( {
190
+ type : 'Property' ,
191
+ method : false ,
192
+ shorthand : false ,
193
+ computed : false ,
194
+ key : identifierName ( cssProp )
195
+ ? { type : 'Identifier' , name : cssProp }
196
+ : { type : 'Literal' , value : cssProp } ,
197
+ value : { type : 'Literal' , value : alignValue } ,
198
+ kind : 'init'
199
+ } )
200
+ }
201
+
157
202
// Restore parent schema.
158
203
state . schema = parentSchema
159
204
@@ -235,24 +280,34 @@ function parseStyle(value, tagName) {
235
280
* @param {Style } domCasing
236
281
* @returns {Style }
237
282
*/
238
- function transformStyleToCssCasing ( domCasing ) {
283
+ function transformStylesToCssCasing ( domCasing ) {
239
284
/** @type {Style } */
240
285
const cssCasing = { }
241
286
/** @type {string } */
242
287
let from
243
288
244
289
for ( from in domCasing ) {
245
290
if ( own . call ( domCasing , from ) ) {
246
- let to = from . replace ( cap , toDash )
247
- // Handle `ms-xxx` -> `-ms-xxx`.
248
- if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
249
- cssCasing [ to ] = domCasing [ from ]
291
+ cssCasing [ transformStyleToCssCasing ( from ) ] = domCasing [ from ]
250
292
}
251
293
}
252
294
253
295
return cssCasing
254
296
}
255
297
298
+ /**
299
+ * Transform a DOM casing style prop to a CSS casing style prop.
300
+ *
301
+ * @param {string } from
302
+ * @returns {string }
303
+ */
304
+ function transformStyleToCssCasing ( from ) {
305
+ let to = from . replace ( cap , toDash )
306
+ // Handle `ms-xxx` -> `-ms-xxx`.
307
+ if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
308
+ return to
309
+ }
310
+
256
311
/**
257
312
* Make `$1` capitalized.
258
313
*
0 commit comments