Skip to content

Commit

Permalink
Fix #103: Avoid crashes if the source is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Oct 27, 2024
1 parent 68d4fd2 commit 3f76dcd
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
_, _ = fmt.Fprint(writer, newline)
}
case xml.EndElement:
level--
if level > 0 {
level--
}
currentTagName := getTokenFullName(typedToken.Name, nsAliases)
if !hasContent {
if lastTagName != currentTagName {
Expand Down Expand Up @@ -383,7 +385,9 @@ func FormatHtml(reader io.Reader, writer io.Writer, indent string, colors int) e
forceNewLine = false
}
case html.EndTagToken:
level--
if level > 0 {
level--
}
tagName, _ := tokenizer.TagName()

if forceNewLine {
Expand Down Expand Up @@ -457,7 +461,9 @@ func FormatJson(reader io.Reader, writer io.Writer, indent string, colors int) e
level++
suffix = strings.Repeat(indent, level)
case '}':
level--
if level > 0 {
level--
}
_, _ = fmt.Fprint(writer, newline, strings.Repeat(indent, level), tagColor("}"))
if tokenState == jsonTokenArrayComma {
suffix = "," + newline + strings.Repeat(indent, level)
Expand All @@ -467,7 +473,9 @@ func FormatJson(reader io.Reader, writer io.Writer, indent string, colors int) e
level++
suffix = strings.Repeat(indent, level)
case ']':
level--
if level > 0 {
level--
}
_, _ = fmt.Fprint(writer, newline, strings.Repeat(indent, level), tagColor("]"))
}
case string:
Expand Down

0 comments on commit 3f76dcd

Please sign in to comment.