Skip to content

Commit

Permalink
Fix #25: XPath query should not fail if XML contains HTML autoclosabl…
Browse files Browse the repository at this point in the history
…e tags
  • Loading branch information
sibprogrammer committed Jan 24, 2023
1 parent 7c585a2 commit d8708ff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er
func XPathQuery(reader io.Reader, writer io.Writer, query string, singleNode bool) error {
doc, err := xmlquery.ParseWithOptions(reader, xmlquery.ParserOptions{
Decoder: &xmlquery.DecoderOptions{
Strict: false,
AutoClose: xml.HTMLAutoClose,
Strict: false,
},
})
if err != nil {
Expand Down
23 changes: 18 additions & 5 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,24 @@ func TestFormatHtml(t *testing.T) {
}

func TestXPathQuery(t *testing.T) {
fileReader := getFileReader(path.Join("..", "..", "test", "data", "xml", "formatted.xml"))
output := new(strings.Builder)
err := XPathQuery(fileReader, output, "//first_name", true)
assert.Nil(t, err)
assert.Equal(t, "John", strings.Trim(output.String(), "\n"))
type test struct {
input string
query string
result string
}

tests := []test{
{input: "formatted.xml", query: "//first_name", result: "John"},
{input: "unformatted8.xml", query: "//title", result: "Some Title"},
}

for _, testCase := range tests {
fileReader := getFileReader(path.Join("..", "..", "test", "data", "xml", testCase.input))
output := new(strings.Builder)
err := XPathQuery(fileReader, output, testCase.query, true)
assert.Nil(t, err)
assert.Equal(t, testCase.result, strings.Trim(output.String(), "\n"))
}
}

func TestCSSQuery(t *testing.T) {
Expand Down

0 comments on commit d8708ff

Please sign in to comment.