Skip to content

Commit 742e26f

Browse files
authoredMay 10, 2020
Prevent 500 with badly formed task list (#11328)
Fix #11317 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c9187b8 commit 742e26f

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed
 

‎modules/markup/markdown/goldmark.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,30 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
125125
}
126126
v.Destination = link
127127
case *ast.List:
128-
if v.HasChildren() && v.FirstChild().HasChildren() && v.FirstChild().FirstChild().HasChildren() {
129-
if _, ok := v.FirstChild().FirstChild().FirstChild().(*east.TaskCheckBox); ok {
130-
v.SetAttributeString("class", []byte("task-list"))
131-
children := make([]ast.Node, 0, v.ChildCount())
132-
child := v.FirstChild()
133-
for child != nil {
134-
children = append(children, child)
135-
child = child.NextSibling()
128+
if v.HasChildren() {
129+
children := make([]ast.Node, 0, v.ChildCount())
130+
child := v.FirstChild()
131+
for child != nil {
132+
children = append(children, child)
133+
child = child.NextSibling()
134+
}
135+
v.RemoveChildren(v)
136+
137+
for _, child := range children {
138+
listItem := child.(*ast.ListItem)
139+
if !child.HasChildren() || !child.FirstChild().HasChildren() {
140+
v.AppendChild(v, child)
141+
continue
136142
}
137-
v.RemoveChildren(v)
138-
139-
for _, child := range children {
140-
listItem := child.(*ast.ListItem)
141-
newChild := NewTaskCheckBoxListItem(listItem)
142-
taskCheckBox := child.FirstChild().FirstChild().(*east.TaskCheckBox)
143-
newChild.IsChecked = taskCheckBox.IsChecked
144-
v.AppendChild(v, newChild)
143+
taskCheckBox, ok := child.FirstChild().FirstChild().(*east.TaskCheckBox)
144+
if !ok {
145+
v.AppendChild(v, child)
146+
continue
145147
}
148+
newChild := NewTaskCheckBoxListItem(listItem)
149+
newChild.IsChecked = taskCheckBox.IsChecked
150+
newChild.SetAttributeString("class", []byte("task-list-item"))
151+
v.AppendChild(v, newChild)
146152
}
147153
}
148154
}

‎modules/markup/markdown/markdown_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
141141
<h2 id="user-content-custom-id">More tests</h2>
142142
<p>(from <a href="https://www.markdownguide.org/extended-syntax/" rel="nofollow">https://www.markdownguide.org/extended-syntax/</a>)</p>
143143
<h3 id="user-content-checkboxes">Checkboxes</h3>
144-
<ul class="task-list">
145-
<li><span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>unchecked</label></span></li>
146-
<li><span class="ui checked checkbox"><input type="checkbox" checked="" readonly="readonly"/><label>checked</label></span></li>
147-
<li><span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>still unchecked</label></span></li>
144+
<ul>
145+
<li class="task-list-item"><span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>unchecked</label></span></li>
146+
<li class="task-list-item"><span class="ui checked checkbox"><input type="checkbox" checked="" readonly="readonly"/><label>checked</label></span></li>
147+
<li class="task-list-item"><span class="ui checkbox"><input type="checkbox" readonly="readonly"/><label>still unchecked</label></span></li>
148148
</ul>
149149
<h3 id="user-content-definition-list">Definition list</h3>
150150
<dl>

‎modules/markup/sanitizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ReplaceSanitizer() {
5454
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`ref-issue`)).OnElements("a")
5555

5656
// Allow classes for task lists
57-
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`task-list`)).OnElements("ul")
57+
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`task-list-item`)).OnElements("li")
5858

5959
// Allow icons
6060
sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^icon(\s+[\p{L}\p{N}_-]+)+$`)).OnElements("i")

‎web_src/less/_markdown.less

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@
192192
list-style-type: none;
193193
}
194194

195-
ul.task-list,
196-
ol.task-list {
195+
li.task-list-item {
197196
list-style-type: none;
197+
margin-left: calc(-2em + 2px);
198198
}
199199

200200
ul ul,

0 commit comments

Comments
 (0)
Please sign in to comment.