@@ -24,6 +24,8 @@ const own = {}.hasOwnProperty
2424const cap = / [ A - Z ] / g
2525const dashSomething = / - ( [ a - z ] ) / g
2626
27+ const tableCellElement = new Set ( [ 'td' , 'th' ] )
28+
2729/**
2830 * Turn a hast element into an estree node.
2931 *
@@ -51,6 +53,10 @@ export function element(node, state) {
5153 const attributes = [ ]
5254 /** @type {string } */
5355 let prop
56+ /** @type {string | undefined } */
57+ let alignValue
58+ /** @type {Array<Property> | undefined } */
59+ let styleProperties
5460
5561 for ( prop in props ) {
5662 if ( own . call ( props , prop ) ) {
@@ -89,7 +95,7 @@ export function element(node, state) {
8995 : parseStyle ( String ( value ) , node . tagName )
9096
9197 if ( state . stylePropertyNameCase === 'css' ) {
92- styleObject = transformStyleToCssCasing ( styleObject )
98+ styleObject = transformStylesToCssCasing ( styleObject )
9399 }
94100
95101 /** @type {Array<Property> } */
@@ -114,12 +120,20 @@ export function element(node, state) {
114120 }
115121 }
116122
123+ styleProperties = cssProperties
117124 attributeValue = {
118125 type : 'JSXExpressionContainer' ,
119126 expression : { type : 'ObjectExpression' , properties : cssProperties }
120127 }
121128 } else if ( value === true ) {
122129 attributeValue = null
130+ } else if (
131+ state . tableCellAlignToStyle &&
132+ tableCellElement . has ( node . tagName ) &&
133+ prop === 'align'
134+ ) {
135+ alignValue = String ( value )
136+ continue
123137 } else {
124138 attributeValue = { type : 'Literal' , value : String ( value ) }
125139 }
@@ -154,6 +168,37 @@ export function element(node, state) {
154168 }
155169 }
156170
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+
157202 // Restore parent schema.
158203 state . schema = parentSchema
159204
@@ -235,24 +280,34 @@ function parseStyle(value, tagName) {
235280 * @param {Style } domCasing
236281 * @returns {Style }
237282 */
238- function transformStyleToCssCasing ( domCasing ) {
283+ function transformStylesToCssCasing ( domCasing ) {
239284 /** @type {Style } */
240285 const cssCasing = { }
241286 /** @type {string } */
242287 let from
243288
244289 for ( from in domCasing ) {
245290 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 ]
250292 }
251293 }
252294
253295 return cssCasing
254296}
255297
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+
256311/**
257312 * Make `$1` capitalized.
258313 *
0 commit comments