Skip to content

Commit f10b22a

Browse files
Preserving sibling relationship of all node types
1 parent 1c3cffd commit f10b22a

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

display.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -272,38 +272,32 @@ func jsonify(node *html.Node) map[string]interface{} {
272272
}
273273
}
274274
}
275-
vals["tag"] = node.DataAtom.String()
276-
children := []interface{}{}
277-
for child := node.FirstChild; child != nil; child = child.NextSibling {
278-
switch child.Type {
279-
case html.ElementNode:
280-
children = append(children, jsonify(child))
281-
case html.TextNode:
282-
text := strings.TrimSpace(child.Data)
283-
if text != "" {
284-
if pupEscapeHTML {
285-
// don't escape javascript
286-
if node.DataAtom != atom.Script {
287-
text = html.EscapeString(text)
288-
}
289-
}
290-
// if there is already text we'll append it
291-
currText, ok := vals["text"]
292-
if ok {
293-
text = fmt.Sprintf("%s %s", currText, text)
294-
}
295-
vals["text"] = text
296-
}
297-
case html.CommentNode:
298-
comment := strings.TrimSpace(child.Data)
275+
switch node.Type {
276+
case html.ElementNode:
277+
vals["tag"] = node.Data
278+
case html.TextNode:
279+
text := strings.TrimSpace(node.Data)
280+
if text != "" {
299281
if pupEscapeHTML {
300-
comment = html.EscapeString(comment)
301-
}
302-
currComment, ok := vals["comment"]
303-
if ok {
304-
comment = fmt.Sprintf("%s %s", currComment, comment)
282+
// don't escape javascript
283+
if node.DataAtom != atom.Script {
284+
text = html.EscapeString(text)
285+
}
305286
}
306-
vals["comment"] = comment
287+
vals["text"] = text
288+
}
289+
case html.CommentNode:
290+
comment := strings.TrimSpace(node.Data)
291+
if pupEscapeHTML {
292+
comment = html.EscapeString(comment)
293+
}
294+
vals["comment"] = comment
295+
}
296+
children := []interface{}{}
297+
for child := node.FirstChild; child != nil; child = child.NextSibling {
298+
jChild := jsonify(child)
299+
if len(jChild) > 0 {
300+
children = append(children, jChild)
307301
}
308302
}
309303
if len(children) > 0 {

0 commit comments

Comments
 (0)