Skip to content

Commit a11677f

Browse files
bukzorclaude
andcommitted
Fix #160: Anchor IsJSON regex to prevent false positives on CDATA
The IsJSON() function was using an unanchored regex `\s*[{\[]` which would match a `[` character anywhere in the input. This caused XML documents containing CDATA sections like `<![CDATA[text]]>` to be incorrectly detected as JSON. Changed the regex to `^\s*[{\[]` to anchor it to the start of the input string. Now only inputs that actually begin with `{` or `[` (after optional whitespace) will be detected as JSON. Added TestFormatDetection to verify CDATA sections are correctly detected as XML format, not JSON format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 727399c commit a11677f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cmd/root_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,25 @@ Bye.`,
199199
})
200200
}
201201
}
202+
203+
func TestIsJSON(t *testing.T) {
204+
tests := []struct {
205+
input string
206+
expected bool
207+
}{
208+
{"{}", true},
209+
{"[]", true},
210+
{" {}", true},
211+
{"\t[", true},
212+
{"<root></root>", false},
213+
{"<![CDATA[text]]>", false}, // Bug #160: should not match [ in CDATA
214+
{"plain text", false},
215+
}
216+
217+
for _, tt := range tests {
218+
t.Run(tt.input, func(t *testing.T) {
219+
result := utils.IsJSON(tt.input)
220+
assert.Equal(t, tt.expected, result)
221+
})
222+
}
223+
}

internal/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func IsHTML(input string) bool {
519519

520520
func IsJSON(input string) bool {
521521
input = strings.ToLower(input)
522-
matched, _ := regexp.MatchString(`\s*[{\[]`, input)
522+
matched, _ := regexp.MatchString(`^\s*[{\[]`, input)
523523
return matched
524524
}
525525

0 commit comments

Comments
 (0)