Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/antchfx/xmlquery"
"github.com/sibprogrammer/xq/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -102,6 +103,17 @@ func TestRootCmd(t *testing.T) {
assert.ErrorContains(t, err, "invalid argument")
}

func TestCDATASupport(t *testing.T) {
input := "<root><![CDATA[1 & 2]]></root>"
doc, err := xmlquery.Parse(strings.NewReader(input))
assert.Nil(t, err)

result := utils.NodeToJSON(doc, 10)
expected := map[string]interface{}{"root": "1 & 2"}

assert.Equal(t, expected, result)
}

func TestProcessAsJSON(t *testing.T) {
tests := []struct {
name string
Expand Down
10 changes: 5 additions & 5 deletions internal/utils/jsonutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NodeToJSON(node *xmlquery.Node, depth int) interface{} {
var textParts []string

// Process the next sibling of the document node first (if any)
if node.NextSibling != nil && node.NextSibling.Type == xmlquery.TextNode {
if node.NextSibling != nil && (node.NextSibling.Type == xmlquery.TextNode || node.NextSibling.Type == xmlquery.CharDataNode) {
text := strings.TrimSpace(node.NextSibling.Data)
if text != "" {
textParts = append(textParts, text)
Expand All @@ -34,7 +34,7 @@ func NodeToJSON(node *xmlquery.Node, depth int) interface{} {
case xmlquery.ElementNode:
childResult := nodeToJSONInternal(child, depth)
result[child.Data] = childResult
case xmlquery.TextNode:
case xmlquery.TextNode, xmlquery.CharDataNode: // Text and CDATA
text := strings.TrimSpace(child.Data)
if text != "" {
textParts = append(textParts, text)
Expand All @@ -50,7 +50,7 @@ func NodeToJSON(node *xmlquery.Node, depth int) interface{} {
case xmlquery.ElementNode:
return nodeToJSONInternal(node, depth)

case xmlquery.TextNode:
case xmlquery.TextNode, xmlquery.CharDataNode: // Text and CDATA
return strings.TrimSpace(node.Data)

default:
Expand All @@ -71,7 +71,7 @@ func nodeToJSONInternal(node *xmlquery.Node, depth int) interface{} {
var textParts []string
for child := node.FirstChild; child != nil; child = child.NextSibling {
switch child.Type {
case xmlquery.TextNode:
case xmlquery.TextNode, xmlquery.CharDataNode: // Text and CDATA
text := strings.TrimSpace(child.Data)
if text != "" {
textParts = append(textParts, text)
Expand All @@ -96,7 +96,7 @@ func getTextContent(node *xmlquery.Node) string {
var parts []string
for child := node.FirstChild; child != nil; child = child.NextSibling {
switch child.Type {
case xmlquery.TextNode:
case xmlquery.TextNode, xmlquery.CharDataNode: // Text and CDATA
text := strings.TrimSpace(child.Data)
if text != "" {
parts = append(parts, text)
Expand Down