...

Source file src/github.com/yuin/goldmark/extension/table_test.go

Documentation: github.com/yuin/goldmark/extension

     1  package extension
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/yuin/goldmark"
     7  	"github.com/yuin/goldmark/ast"
     8  	east "github.com/yuin/goldmark/extension/ast"
     9  	"github.com/yuin/goldmark/parser"
    10  	"github.com/yuin/goldmark/renderer/html"
    11  	"github.com/yuin/goldmark/testutil"
    12  	"github.com/yuin/goldmark/text"
    13  	"github.com/yuin/goldmark/util"
    14  )
    15  
    16  func TestTable(t *testing.T) {
    17  	markdown := goldmark.New(
    18  		goldmark.WithRendererOptions(
    19  			html.WithUnsafe(),
    20  			html.WithXHTML(),
    21  		),
    22  		goldmark.WithExtensions(
    23  			Table,
    24  		),
    25  	)
    26  	testutil.DoTestCaseFile(markdown, "_test/table.txt", t, testutil.ParseCliCaseArg()...)
    27  }
    28  
    29  func TestTableWithAlignDefault(t *testing.T) {
    30  	markdown := goldmark.New(
    31  		goldmark.WithRendererOptions(
    32  			html.WithXHTML(),
    33  			html.WithUnsafe(),
    34  		),
    35  		goldmark.WithExtensions(
    36  			NewTable(
    37  				WithTableCellAlignMethod(TableCellAlignDefault),
    38  			),
    39  		),
    40  	)
    41  	testutil.DoTestCase(
    42  		markdown,
    43  		testutil.MarkdownTestCase{
    44  			No:          1,
    45  			Description: "Cell with TableCellAlignDefault and XHTML should be rendered as an align attribute",
    46  			Markdown: `
    47  | abc | defghi |
    48  :-: | -----------:
    49  bar | baz
    50  `,
    51  			Expected: `<table>
    52  <thead>
    53  <tr>
    54  <th align="center">abc</th>
    55  <th align="right">defghi</th>
    56  </tr>
    57  </thead>
    58  <tbody>
    59  <tr>
    60  <td align="center">bar</td>
    61  <td align="right">baz</td>
    62  </tr>
    63  </tbody>
    64  </table>`,
    65  		},
    66  		t,
    67  	)
    68  
    69  	markdown = goldmark.New(
    70  		goldmark.WithRendererOptions(
    71  			html.WithUnsafe(),
    72  		),
    73  		goldmark.WithExtensions(
    74  			NewTable(
    75  				WithTableCellAlignMethod(TableCellAlignDefault),
    76  			),
    77  		),
    78  	)
    79  	testutil.DoTestCase(
    80  		markdown,
    81  		testutil.MarkdownTestCase{
    82  			No:          2,
    83  			Description: "Cell with TableCellAlignDefault and HTML5 should be rendered as a style attribute",
    84  			Markdown: `
    85  | abc | defghi |
    86  :-: | -----------:
    87  bar | baz
    88  `,
    89  			Expected: `<table>
    90  <thead>
    91  <tr>
    92  <th style="text-align:center">abc</th>
    93  <th style="text-align:right">defghi</th>
    94  </tr>
    95  </thead>
    96  <tbody>
    97  <tr>
    98  <td style="text-align:center">bar</td>
    99  <td style="text-align:right">baz</td>
   100  </tr>
   101  </tbody>
   102  </table>`,
   103  		},
   104  		t,
   105  	)
   106  }
   107  
   108  func TestTableWithAlignAttribute(t *testing.T) {
   109  	markdown := goldmark.New(
   110  		goldmark.WithRendererOptions(
   111  			html.WithXHTML(),
   112  			html.WithUnsafe(),
   113  		),
   114  		goldmark.WithExtensions(
   115  			NewTable(
   116  				WithTableCellAlignMethod(TableCellAlignAttribute),
   117  			),
   118  		),
   119  	)
   120  	testutil.DoTestCase(
   121  		markdown,
   122  		testutil.MarkdownTestCase{
   123  			No:          1,
   124  			Description: "Cell with TableCellAlignAttribute and XHTML should be rendered as an align attribute",
   125  			Markdown: `
   126  | abc | defghi |
   127  :-: | -----------:
   128  bar | baz
   129  `,
   130  			Expected: `<table>
   131  <thead>
   132  <tr>
   133  <th align="center">abc</th>
   134  <th align="right">defghi</th>
   135  </tr>
   136  </thead>
   137  <tbody>
   138  <tr>
   139  <td align="center">bar</td>
   140  <td align="right">baz</td>
   141  </tr>
   142  </tbody>
   143  </table>`,
   144  		},
   145  		t,
   146  	)
   147  
   148  	markdown = goldmark.New(
   149  		goldmark.WithRendererOptions(
   150  			html.WithUnsafe(),
   151  		),
   152  		goldmark.WithExtensions(
   153  			NewTable(
   154  				WithTableCellAlignMethod(TableCellAlignAttribute),
   155  			),
   156  		),
   157  	)
   158  	testutil.DoTestCase(
   159  		markdown,
   160  		testutil.MarkdownTestCase{
   161  			No:          2,
   162  			Description: "Cell with TableCellAlignAttribute and HTML5 should be rendered as an align attribute",
   163  			Markdown: `
   164  | abc | defghi |
   165  :-: | -----------:
   166  bar | baz
   167  `,
   168  			Expected: `<table>
   169  <thead>
   170  <tr>
   171  <th align="center">abc</th>
   172  <th align="right">defghi</th>
   173  </tr>
   174  </thead>
   175  <tbody>
   176  <tr>
   177  <td align="center">bar</td>
   178  <td align="right">baz</td>
   179  </tr>
   180  </tbody>
   181  </table>`,
   182  		},
   183  		t,
   184  	)
   185  }
   186  
   187  type tableStyleTransformer struct {
   188  }
   189  
   190  func (a *tableStyleTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
   191  	cell := node.FirstChild().FirstChild().FirstChild().(*east.TableCell)
   192  	cell.SetAttributeString("style", []byte("font-size:1em"))
   193  }
   194  
   195  func TestTableWithAlignStyle(t *testing.T) {
   196  	markdown := goldmark.New(
   197  		goldmark.WithRendererOptions(
   198  			html.WithXHTML(),
   199  			html.WithUnsafe(),
   200  		),
   201  		goldmark.WithExtensions(
   202  			NewTable(
   203  				WithTableCellAlignMethod(TableCellAlignStyle),
   204  			),
   205  		),
   206  	)
   207  	testutil.DoTestCase(
   208  		markdown,
   209  		testutil.MarkdownTestCase{
   210  			No:          1,
   211  			Description: "Cell with TableCellAlignStyle and XHTML should be rendered as a style attribute",
   212  			Markdown: `
   213  | abc | defghi |
   214  :-: | -----------:
   215  bar | baz
   216  `,
   217  			Expected: `<table>
   218  <thead>
   219  <tr>
   220  <th style="text-align:center">abc</th>
   221  <th style="text-align:right">defghi</th>
   222  </tr>
   223  </thead>
   224  <tbody>
   225  <tr>
   226  <td style="text-align:center">bar</td>
   227  <td style="text-align:right">baz</td>
   228  </tr>
   229  </tbody>
   230  </table>`,
   231  		},
   232  		t,
   233  	)
   234  
   235  	markdown = goldmark.New(
   236  		goldmark.WithRendererOptions(
   237  			html.WithUnsafe(),
   238  		),
   239  		goldmark.WithExtensions(
   240  			NewTable(
   241  				WithTableCellAlignMethod(TableCellAlignStyle),
   242  			),
   243  		),
   244  	)
   245  	testutil.DoTestCase(
   246  		markdown,
   247  		testutil.MarkdownTestCase{
   248  			No:          2,
   249  			Description: "Cell with TableCellAlignStyle and HTML5 should be rendered as a style attribute",
   250  			Markdown: `
   251  | abc | defghi |
   252  :-: | -----------:
   253  bar | baz
   254  `,
   255  			Expected: `<table>
   256  <thead>
   257  <tr>
   258  <th style="text-align:center">abc</th>
   259  <th style="text-align:right">defghi</th>
   260  </tr>
   261  </thead>
   262  <tbody>
   263  <tr>
   264  <td style="text-align:center">bar</td>
   265  <td style="text-align:right">baz</td>
   266  </tr>
   267  </tbody>
   268  </table>`,
   269  		},
   270  		t,
   271  	)
   272  
   273  	markdown = goldmark.New(
   274  		goldmark.WithParserOptions(
   275  			parser.WithASTTransformers(
   276  				util.Prioritized(&tableStyleTransformer{}, 0),
   277  			),
   278  		),
   279  		goldmark.WithRendererOptions(
   280  			html.WithUnsafe(),
   281  		),
   282  		goldmark.WithExtensions(
   283  			NewTable(
   284  				WithTableCellAlignMethod(TableCellAlignStyle),
   285  			),
   286  		),
   287  	)
   288  
   289  	testutil.DoTestCase(
   290  		markdown,
   291  		testutil.MarkdownTestCase{
   292  			No:          3,
   293  			Description: "Styled cell should not be broken the style by the alignments",
   294  			Markdown: `
   295  | abc | defghi |
   296  :-: | -----------:
   297  bar | baz
   298  `,
   299  			Expected: `<table>
   300  <thead>
   301  <tr>
   302  <th style="font-size:1em;text-align:center">abc</th>
   303  <th style="text-align:right">defghi</th>
   304  </tr>
   305  </thead>
   306  <tbody>
   307  <tr>
   308  <td style="text-align:center">bar</td>
   309  <td style="text-align:right">baz</td>
   310  </tr>
   311  </tbody>
   312  </table>`,
   313  		},
   314  		t,
   315  	)
   316  }
   317  
   318  func TestTableWithAlignNone(t *testing.T) {
   319  	markdown := goldmark.New(
   320  		goldmark.WithRendererOptions(
   321  			html.WithXHTML(),
   322  			html.WithUnsafe(),
   323  		),
   324  		goldmark.WithExtensions(
   325  			NewTable(
   326  				WithTableCellAlignMethod(TableCellAlignNone),
   327  			),
   328  		),
   329  	)
   330  	testutil.DoTestCase(
   331  		markdown,
   332  		testutil.MarkdownTestCase{
   333  			No:          1,
   334  			Description: "Cell with TableCellAlignStyle and XHTML should not be rendered",
   335  			Markdown: `
   336  | abc | defghi |
   337  :-: | -----------:
   338  bar | baz
   339  `,
   340  			Expected: `<table>
   341  <thead>
   342  <tr>
   343  <th>abc</th>
   344  <th>defghi</th>
   345  </tr>
   346  </thead>
   347  <tbody>
   348  <tr>
   349  <td>bar</td>
   350  <td>baz</td>
   351  </tr>
   352  </tbody>
   353  </table>`,
   354  		},
   355  		t,
   356  	)
   357  }
   358  

View as plain text