Skip to content

Commit

Permalink
bugfix(41): fix XML format detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Chmielewski authored and pawelWritesCode committed Sep 6, 2022
1 parent 0b4735f commit b015def
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
20 changes: 14 additions & 6 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package format
import (
"bytes"
"encoding/json"
"encoding/xml"
"strings"

"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -59,15 +58,24 @@ func IsYAML(b []byte) bool {
}

// IsXML checks whether bytes are in XML format.
// Function does not guarantee that standard xml.Unmarshal will work b, instead
// it only looks for characteristics of XML formatted data.
func IsXML(b []byte) bool {
var v any
err := xml.Unmarshal(b, &v)
if err == nil {
str := string(b)
idx := strings.Index(strings.TrimSpace(str), "<?xml version=")
if idx == 0 || idx == 1 {
return true
}

idx := strings.Index(strings.TrimSpace(string(b)), "<?xml version=")
return idx == 0
if !(strings.Contains(str, ">") && strings.Contains(str, "<")) {
return false
}

if strings.Count(str, "<") >= (strings.Count(str, "</") + strings.Count(str, "/>")) {
return true
}

return false
}

// IsHTML checks whether bytes are in HTML format.
Expand Down
38 changes: 25 additions & 13 deletions pkg/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,9 @@ func TestIsXML(t *testing.T) {
}{
{name: "json", args: args{bytes: []byte(`{"name": "abc"}`)}, want: false},
{name: "plain text", args: args{bytes: []byte(`abcd efgh`)}, want: false},
{name: "xml", args: args{bytes: []byte(`<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>`)}, want: true},
{name: "yaml", args: args{bytes: []byte(`---
name: "abc"`)}, want: false},
{name: "yaml", args: args{bytes: []byte(`<?xml version='1.0' encoding='us-ascii'?>
{name: "xml #1", args: args{bytes: []byte(`<?xml version='1.0' encoding='us-ascii'?>
<!-- A SAMPLE set of slides -->
Expand All @@ -111,6 +99,30 @@ name: "abc"`)}, want: false},
</slideshow>
`)}, want: true},
{name: "xml #2", args: args{bytes: []byte(`<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>`)}, want: true},
{name: "xml #3", args: args{bytes: []byte(`
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>`)}, want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit b015def

Please # to comment.