| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package tables_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib" |
| 20 |
) |
| 21 |
|
| 22 |
func TestTableHook(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
files := ` |
| 25 |
-- hugo.toml -- |
| 26 |
[markup.goldmark.parser.attribute] |
| 27 |
block = true |
| 28 |
title = true |
| 29 |
-- content/p1.md -- |
| 30 |
## Table 1 |
| 31 |
|
| 32 |
| Item | In Stock | Price | |
| 33 |
| :---------------- | :------: | ----: | |
| 34 |
| Python Hat | True | 23.99 | |
| 35 |
| SQL **Hat** | True | 23.99 | |
| 36 |
| Codecademy Tee | False | 19.99 | |
| 37 |
| Codecademy Hoodie | False | 42.99 | |
| 38 |
{.foo foo="bar"} |
| 39 |
|
| 40 |
## Table 2 |
| 41 |
|
| 42 |
| Month | Savings | |
| 43 |
| -------- | ------- | |
| 44 |
| January | $250 | |
| 45 |
| February | $80 | |
| 46 |
| March | $420 | |
| 47 |
|
| 48 |
-- layouts/single.html -- |
| 49 |
{{ .Content }} |
| 50 |
-- layouts/_markup/render-table.html -- |
| 51 |
Attributes: {{ .Attributes }}| |
| 52 |
{{ template "print" (dict "what" (printf "table-%d-thead" $.Ordinal) "rows" .THead) }} |
| 53 |
{{ template "print" (dict "what" (printf "table-%d-tbody" $.Ordinal) "rows" .TBody) }} |
| 54 |
{{ define "print" }} |
| 55 |
{{ .what }}:{{ range $i, $a := .rows }} {{ $i }}:{{ range $j, $b := . }} {{ $j }}: {{ .Alignment }}: {{ .Text }}|{{ end }}{{ end }}$ |
| 56 |
{{ end }} |
| 57 |
|
| 58 |
` |
| 59 |
|
| 60 |
for range 2 { |
| 61 |
b := hugolib.Test(t, files) |
| 62 |
|
| 63 |
b.AssertFileContent("public/p1/index.html", |
| 64 |
"Attributes: map[class:foo foo:bar]|", |
| 65 |
"table-0-thead: 0: 0: left: Item| 1: center: In Stock| 2: right: Price|$", |
| 66 |
"table-0-tbody: 0: 0: left: Python Hat| 1: center: True| 2: right: 23.99| 1: 0: left: SQL <strong>Hat</strong>| 1: center: True| 2: right: 23.99| 2: 0: left: Codecademy Tee| 1: center: False| 2: right: 19.99| 3: 0: left: Codecademy Hoodie| 1: center: False| 2: right: 42.99|$", |
| 67 |
) |
| 68 |
|
| 69 |
b.AssertFileContent("public/p1/index.html", |
| 70 |
"table-1-thead: 0: 0: : Month| 1: : Savings|$", |
| 71 |
"table-1-tbody: 0: 0: : January| 1: : $250| 1: 0: : February| 1: : $80| 2: 0: : March| 1: : $420|$", |
| 72 |
) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestTableDefault(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
files := ` |
| 79 |
-- hugo.toml -- |
| 80 |
[markup.goldmark.parser.attribute] |
| 81 |
block = true |
| 82 |
title = true |
| 83 |
-- content/p1.md -- |
| 84 |
|
| 85 |
## Table 1 |
| 86 |
|
| 87 |
| Item | In Stock | Price | |
| 88 |
| :---------------- | :------: | ----: | |
| 89 |
| Python Hat | True | 23.99 | |
| 90 |
| SQL Hat | True | 23.99 | |
| 91 |
| Codecademy Tee | False | 19.99 | |
| 92 |
| Codecademy Hoodie | False | 42.99 | |
| 93 |
{.foo} |
| 94 |
|
| 95 |
## Table 2 |
| 96 |
|
| 97 |
a|b |
| 98 |
---|--- |
| 99 |
1|2 |
| 100 |
{id="\"><script>alert()</script>"} |
| 101 |
|
| 102 |
-- layouts/single.html -- |
| 103 |
Summary: {{ .Summary }} |
| 104 |
Content: {{ .Content }} |
| 105 |
|
| 106 |
` |
| 107 |
b := hugolib.Test(t, files) |
| 108 |
|
| 109 |
b.AssertFileContent("public/p1/index.html", `<table class="foo">`) |
| 110 |
b.AssertFileContent("public/p1/index.html", `<table id=""><script>alert()</script>">`) |
| 111 |
} |
| 112 |
|
| 113 |
// Issue 12811. |
| 114 |
func TestTableDefaultRSSAndHTML(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
files := ` |
| 117 |
-- hugo.toml -- |
| 118 |
[outputFormats] |
| 119 |
[outputFormats.rss] |
| 120 |
weight = 30 |
| 121 |
[outputFormats.html] |
| 122 |
weight = 20 |
| 123 |
-- content/_index.md -- |
| 124 |
--- |
| 125 |
title: "Home" |
| 126 |
output: ["rss", "html"] |
| 127 |
--- |
| 128 |
|
| 129 |
| Item | In Stock | Price | |
| 130 |
| :---------------- | :------: | ----: | |
| 131 |
| Python Hat | True | 23.99 | |
| 132 |
| SQL Hat | True | 23.99 | |
| 133 |
| Codecademy Tee | False | 19.99 | |
| 134 |
| Codecademy Hoodie | False | 42.99 | |
| 135 |
|
| 136 |
{{< foo >}} |
| 137 |
|
| 138 |
-- layouts/home.html -- |
| 139 |
Content: {{ .Content }} |
| 140 |
-- layouts/index.xml -- |
| 141 |
Content: {{ .Content }} |
| 142 |
-- layouts/_shortcodes/foo.xml -- |
| 143 |
foo xml |
| 144 |
-- layouts/_shortcodes/foo.html -- |
| 145 |
foo html |
| 146 |
|
| 147 |
` |
| 148 |
b := hugolib.Test(t, files) |
| 149 |
|
| 150 |
b.AssertFileContent("public/index.xml", "<table>") |
| 151 |
b.AssertFileContent("public/index.html", "<table>") |
| 152 |
} |
| 153 |
|
| 154 |
func TestTableDefaultRSSOnly(t *testing.T) { |
| 155 |
t.Parallel() |
| 156 |
files := ` |
| 157 |
-- hugo.toml -- |
| 158 |
[outputs] |
| 159 |
home = ['rss'] |
| 160 |
section = ['rss'] |
| 161 |
taxonomy = ['rss'] |
| 162 |
term = ['rss'] |
| 163 |
page = ['rss'] |
| 164 |
disableKinds = ["taxonomy", "term", "page", "section"] |
| 165 |
-- content/_index.md -- |
| 166 |
--- |
| 167 |
title: "Home" |
| 168 |
--- |
| 169 |
|
| 170 |
## Table 1 |
| 171 |
|
| 172 |
| Item | In Stock | Price | |
| 173 |
| :---------------- | :------: | ----: | |
| 174 |
| Python Hat | True | 23.99 | |
| 175 |
| SQL Hat | True | 23.99 | |
| 176 |
| Codecademy Tee | False | 19.99 | |
| 177 |
| Codecademy Hoodie | False | 42.99 | |
| 178 |
|
| 179 |
-- layouts/index.xml -- |
| 180 |
Content: {{ .Content }} |
| 181 |
` |
| 182 |
b := hugolib.Test(t, files) |
| 183 |
|
| 184 |
b.AssertFileContent("public/index.xml", "<table>") |
| 185 |
} |
| 186 |
|
| 187 |
// See issue 15389. |
| 188 |
func TestTableHookHTMLOnlyWithJSONOutput(t *testing.T) { |
| 189 |
t.Parallel() |
| 190 |
|
| 191 |
files := ` |
| 192 |
-- hugo.toml -- |
| 193 |
disableKinds = ['page', 'rss', 'section', 'sitemap', 'taxonomy', 'term'] |
| 194 |
[outputs] |
| 195 |
home = ['html', 'json'] |
| 196 |
-- content/_index.md -- |
| 197 |
--- |
| 198 |
title: home |
| 199 |
--- |
| 200 |
|
| 201 |
col_a|col_b |
| 202 |
---|--- |
| 203 |
foo|bar |
| 204 |
-- layouts/_markup/render-table.html -- |
| 205 |
TABLE RENDER HOOK |
| 206 |
-- layouts/home.html -- |
| 207 |
{{ .Content }} |
| 208 |
-- layouts/home.json -- |
| 209 |
{{ .Content }} |
| 210 |
` |
| 211 |
|
| 212 |
b := hugolib.Test(t, files) |
| 213 |
|
| 214 |
b.AssertFileContent("public/index.html", "TABLE RENDER HOOK") |
| 215 |
b.AssertFileContent("public/index.json", "TABLE RENDER HOOK") |
| 216 |
} |