@@ -98,6 +98,23 @@ static int set_table_alignments(cmark_node *node, uint8_t *alignments) {
98
98
return 1 ;
99
99
}
100
100
101
+ static uint8_t get_cell_alignment (cmark_node * node ) {
102
+ if (!node || node -> type != CMARK_NODE_TABLE_CELL )
103
+ return 0 ;
104
+
105
+ const uint8_t * alignments = get_table_alignments (node -> parent -> parent );
106
+ int i = node -> as .custom_int ;
107
+ return alignments [i ];
108
+ }
109
+
110
+ static int set_cell_index (cmark_node * node , int i ) {
111
+ if (!node || node -> type != CMARK_NODE_TABLE_CELL )
112
+ return 0 ;
113
+
114
+ node -> as .custom_int = i ;
115
+ return 1 ;
116
+ }
117
+
101
118
static cmark_strbuf * unescape_pipes (cmark_mem * mem , unsigned char * string , bufsize_t len )
102
119
{
103
120
cmark_strbuf * res = (cmark_strbuf * )mem -> calloc (1 , sizeof (cmark_strbuf ));
@@ -333,7 +350,6 @@ static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
333
350
for (i = 0 ; i < marker_row -> n_columns ; ++ i ) {
334
351
node_cell * node = & marker_row -> cells [i ];
335
352
bool left = node -> buf -> ptr [0 ] == ':' , right = node -> buf -> ptr [node -> buf -> size - 1 ] == ':' ;
336
-
337
353
if (left && right )
338
354
alignments [i ] = 'c' ;
339
355
else if (left )
@@ -363,6 +379,7 @@ static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
363
379
header_cell -> end_column = parent_container -> start_column + cell -> end_offset ;
364
380
cmark_node_set_string_content (header_cell , (char * ) cell -> buf -> ptr );
365
381
cmark_node_set_syntax_extension (header_cell , self );
382
+ set_cell_index (header_cell , i );
366
383
}
367
384
}
368
385
@@ -412,12 +429,14 @@ static cmark_node *try_opening_table_row(cmark_syntax_extension *self,
412
429
node -> end_column = parent_container -> start_column + cell -> end_offset ;
413
430
cmark_node_set_string_content (node , (char * ) cell -> buf -> ptr );
414
431
cmark_node_set_syntax_extension (node , self );
432
+ set_cell_index (node , i );
415
433
}
416
434
417
435
for (; i < table_columns ; ++ i ) {
418
436
cmark_node * node = cmark_parser_add_child (
419
437
parser , table_row_block , CMARK_NODE_TABLE_CELL , 0 );
420
438
cmark_node_set_syntax_extension (node , self );
439
+ set_cell_index (node , i );
421
440
}
422
441
}
423
442
@@ -602,13 +621,7 @@ static const char *xml_attr(cmark_syntax_extension *extension,
602
621
cmark_node * node ) {
603
622
if (node -> type == CMARK_NODE_TABLE_CELL ) {
604
623
if (cmark_gfm_extensions_get_table_row_is_header (node -> parent )) {
605
- uint8_t * alignments = get_table_alignments (node -> parent -> parent );
606
- int i = 0 ;
607
- cmark_node * n ;
608
- for (n = node -> parent -> first_child ; n ; n = n -> next , ++ i )
609
- if (n == node )
610
- break ;
611
- switch (alignments [i ]) {
624
+ switch (get_cell_alignment (node )) {
612
625
case 'l' : return " align=\"left\"" ;
613
626
case 'c' : return " align=\"center\"" ;
614
627
case 'r' : return " align=\"right\"" ;
@@ -696,7 +709,6 @@ static void html_render(cmark_syntax_extension *extension,
696
709
cmark_event_type ev_type , int options ) {
697
710
bool entering = (ev_type == CMARK_EVENT_ENTER );
698
711
cmark_strbuf * html = renderer -> html ;
699
- cmark_node * n ;
700
712
701
713
// XXX: we just monopolise renderer->opaque.
702
714
struct html_table_state * table_state =
@@ -745,7 +757,6 @@ static void html_render(cmark_syntax_extension *extension,
745
757
}
746
758
}
747
759
} else if (node -> type == CMARK_NODE_TABLE_CELL ) {
748
- uint8_t * alignments = get_table_alignments (node -> parent -> parent );
749
760
if (entering ) {
750
761
cmark_html_render_cr (html );
751
762
if (table_state -> in_table_header ) {
@@ -754,12 +765,7 @@ static void html_render(cmark_syntax_extension *extension,
754
765
cmark_strbuf_puts (html , "<td" );
755
766
}
756
767
757
- int i = 0 ;
758
- for (n = node -> parent -> first_child ; n ; n = n -> next , ++ i )
759
- if (n == node )
760
- break ;
761
-
762
- switch (alignments [i ]) {
768
+ switch (get_cell_alignment (node )) {
763
769
case 'l' : html_table_add_align (html , "left" , options ); break ;
764
770
case 'c' : html_table_add_align (html , "center" , options ); break ;
765
771
case 'r' : html_table_add_align (html , "right" , options ); break ;
0 commit comments