95
95
* @property {StylePropertyNameCase | null | undefined } [stylePropertyNameCase='dom']
96
96
* Specify casing to use for property names in `style` objects (default:
97
97
* `'dom'`).
98
+ * @property {boolean | null | undefined } [tableCellAlignToStyle=true]
99
+ * Turn obsolete `align` props on `td` and `th` into CSS `style` props
100
+ * (default: `true`).
98
101
*
99
102
* @typedef RuntimeDevelopment
100
103
* Runtime fields when development is on.
175
178
* Current schema.
176
179
* @property {StylePropertyNameCase } stylePropertyNameCase
177
180
* Casing to use for property names in `style` objects.
181
+ * @property {boolean } tableCellAlignToStyle
182
+ * Turn obsolete `align` props on `td` and `th` into CSS `style` props.
178
183
*
179
184
* @typedef {Record<string, string> } Style
180
185
* Style map.
@@ -229,6 +234,8 @@ const dashSomething = /-([a-z])/g
229
234
// See: <https://github.com/rehypejs/rehype-react/pull/45>.
230
235
const tableElements = new Set ( [ 'table' , 'tbody' , 'thead' , 'tfoot' , 'tr' ] )
231
236
237
+ const tableCellElement = new Set ( [ 'td' , 'th' ] )
238
+
232
239
/**
233
240
* Transform a hast tree to preact, react, solid, svelte, vue, etc.,
234
241
* with an automatic JSX runtime.
@@ -281,7 +288,8 @@ export function toJsxRuntime(tree, options) {
281
288
passKeys : options . passKeys !== false ,
282
289
passNode : options . passNode || false ,
283
290
schema : options . space === 'svg' ? svg : html ,
284
- stylePropertyNameCase : options . stylePropertyNameCase || 'dom'
291
+ stylePropertyNameCase : options . stylePropertyNameCase || 'dom' ,
292
+ tableCellAlignToStyle : options . tableCellAlignToStyle !== false
285
293
}
286
294
287
295
const result = one ( state , tree , undefined )
@@ -479,6 +487,9 @@ function createProperties(state, ancestors) {
479
487
let prop
480
488
481
489
if ( 'properties' in node && node . properties ) {
490
+ /** @type {string | undefined } */
491
+ let alignValue
492
+
482
493
for ( prop in node . properties ) {
483
494
if ( prop !== 'children' && own . call ( node . properties , prop ) ) {
484
495
const result = createProperty (
@@ -489,10 +500,34 @@ function createProperties(state, ancestors) {
489
500
)
490
501
491
502
if ( result ) {
492
- props [ result [ 0 ] ] = result [ 1 ]
503
+ const [ key , value ] = result
504
+
505
+ if (
506
+ state . tableCellAlignToStyle &&
507
+ key === 'align' &&
508
+ typeof value === 'string' &&
509
+ tableCellElement . has ( node . tagName )
510
+ ) {
511
+ alignValue = value
512
+ } else {
513
+ props [ key ] = value
514
+ }
493
515
}
494
516
}
495
517
}
518
+
519
+ if ( alignValue ) {
520
+ // Assume style is an object.
521
+ const style = /** @type {Style } */ ( props . style || ( props . style = { } ) )
522
+ const cssField = 'text-align'
523
+ style [
524
+ state . stylePropertyNameCase === 'css'
525
+ ? // Note: test this when Solid doesn’t want to merge my upcoming PR.
526
+ /* c8 ignore next */
527
+ transformStyleToCssCasing ( cssField )
528
+ : cssField
529
+ ] = alignValue
530
+ }
496
531
}
497
532
498
533
return props
@@ -538,7 +573,7 @@ function createProperty(state, ancestors, prop, value) {
538
573
: parseStyle ( state , ancestors , String ( value ) )
539
574
540
575
if ( state . stylePropertyNameCase === 'css' ) {
541
- styleObject = transformStyleToCssCasing ( styleObject )
576
+ styleObject = transformStylesToCssCasing ( styleObject )
542
577
}
543
578
544
579
return [ 'style' , styleObject ]
@@ -619,24 +654,34 @@ function parseStyle(state, ancestors, value) {
619
654
* @param {Style } domCasing
620
655
* @returns {Style }
621
656
*/
622
- function transformStyleToCssCasing ( domCasing ) {
657
+ function transformStylesToCssCasing ( domCasing ) {
623
658
/** @type {Style } */
624
659
const cssCasing = { }
625
660
/** @type {string } */
626
661
let from
627
662
628
663
for ( from in domCasing ) {
629
664
if ( own . call ( domCasing , from ) ) {
630
- let to = from . replace ( cap , toDash )
631
- // Handle `ms-xxx` -> `-ms-xxx`.
632
- if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
633
- cssCasing [ to ] = domCasing [ from ]
665
+ cssCasing [ transformStyleToCssCasing ( from ) ] = domCasing [ from ]
634
666
}
635
667
}
636
668
637
669
return cssCasing
638
670
}
639
671
672
+ /**
673
+ * Transform a DOM casing style field to a CSS casing style field.
674
+ *
675
+ * @param {string } from
676
+ * @returns {string }
677
+ */
678
+ function transformStyleToCssCasing ( from ) {
679
+ let to = from . replace ( cap , toDash )
680
+ // Handle `ms-xxx` -> `-ms-xxx`.
681
+ if ( to . slice ( 0 , 3 ) === 'ms-' ) to = '-' + to
682
+ return to
683
+ }
684
+
640
685
/**
641
686
* Make `$1` capitalized.
642
687
*
0 commit comments