File: table_test.exs

package info (click to toggle)
elixir-earmark-parser 1.4.44-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,148 kB
  • sloc: makefile: 9
file content (173 lines) | stat: -rw-r--r-- 5,983 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
defmodule Acceptance.Ast.TableTest do
  use ExUnit.Case, async: true
  import Support.Helpers, only: [as_ast: 1, as_ast: 2, parse_html: 1]
  import EarmarkAstDsl
  import Support.TableHelpers

  describe "complex rendering inside tables:" do
    test "simple table" do
      markdown = "|a|b|\n|d|e|"

      html =
        "<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">a</td><td style=\"text-align: left;\">b</td>\n</tr>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "simple table, explicit left alignment" do
      markdown = """
      |a|b|
      |:-|:-|
      |c|d|
      """

      ast = [
        tag("table", [
          thead([th("a"), th("b")]),
          tbody([td("c"), td("d")])
        ])
      ]

      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "table with link with inline ial, no errors" do
      markdown = "|a|b|c|\n|d|e|[link](url){:target=blank}|"

      html =
        "<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">a</td><td style=\"text-align: left;\">b</td><td style=\"text-align: left;\">c</td>\n</tr>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td><td style=\"text-align: left;\"><a href=\"url\" target=\"blank\">link</a></td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "table with link with inline ial, errors" do
      markdown = "|a|b|c|\n|d|e|[link](url){:target=blank xxx}|"

      html =
        "<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">a</td><td style=\"text-align: left;\">b</td><td style=\"text-align: left;\">c</td>\n</tr>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td><td style=\"text-align: left;\"><a href=\"url\" target=\"blank\">link</a></td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = [{:warning, 2, "Illegal attributes [\"xxx\"] ignored in IAL"}]

      assert as_ast(markdown) == {:error, ast, messages}
    end

    test "table with header" do
      markdown = "|alpha|beta|\n|-|-:|\n|1|2|"

      html =
        "<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">alpha</th><th style=\"text-align: right;\">beta</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">1</td><td style=\"text-align: right;\">2</td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "table with header, centered" do
      markdown = "|alpha|beta|\n|-|:-:|\n|1|2|"

      html =
        "<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">alpha</th><th style=\"text-align: center;\">beta</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">1</td><td style=\"text-align: center;\">2</td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end

    test "table with header inside context" do
      markdown = "before\n\n|alpha|beta|\n|-|-:|\n|1|2|\nafter"

      html =
        "<p>before</p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">alpha</th><th style=\"text-align: right;\">beta</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">1</td><td style=\"text-align: right;\">2</td>\n</tr>\n</tbody>\n</table>\n<p>after</p>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "Tables and IAL" do
    test "as mentioned above" do
      markdown = "|a|b|\n|d|e|\n{:#the-table}"

      html =
        "<table id=\"the-table\"><tbody>\n<tr>\n<td style=\"text-align: left;\">a</td><td style=\"text-align: left;\">b</td>\n</tr>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown) == {:ok, ast, messages}
    end
  end

  describe "GFM Tables" do
    test "do not need spaces around mid `\|`" do
      markdown = "a|b\n-|-\nd|e\n"

      html =
        "<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">a</th><th style=\"text-align: left;\">b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">d</td><td style=\"text-align: left;\">e</td>\n</tr>\n</tbody>\n</table>\n"

      ast = parse_html(html)
      messages = []

      assert as_ast(markdown, gfm_tables: true) == {:ok, ast, messages}
    end

    test "do not need spaces around mid `\|` but w/o gfm_tables this is no good" do
      markdown = "a|b\n-|-\nd|e\n"
      html = "<p>a|b\n-|-\nd|e</p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown, gfm_tables: false) == {:ok, ast, messages}
    end

    test "however a header line needs to be used" do
      markdown = "a|b\nd|e\n"
      html = "<p>a|b\nd|e</p>\n"
      ast = parse_html(html)
      messages = []

      assert as_ast(markdown, gfm_tables: true) == {:ok, ast, messages}
    end
  end

  describe "The order of things (Vorta and Jem'Hadar I guess)" do
    test "table cells are in the correct order" do
      markdown = """
      | What              |
      | ----------------- |
      | This part is fine |
      | This is `broken`  |
      """

      ast = [
        table(["This part is fine", {"This is ", tag(:code, "broken", [class: :inline], %{line: 4})}],
          head: ["What"]
        )
      ]

      assert as_ast(markdown) == {:ok, ast, []}
    end

    test "minimized example" do
      markdown = "|zero|\n|alpha *beta*|"
      ast = [table(["zero", {"alpha ", tag(:em, "beta")}])]

      assert as_ast(markdown) == {:ok, ast, []}
    end
  end
end

# SPDX-License-Identifier: Apache-2.0