Skip to content

Commit fb5bc39

Browse files
committed
fix: emit HTTP body tokens without Coalesce
The body sub-lexer was set up but EOF was returned immediately, causing callers that stop at EOF to drop the body. Return the first sub-iterator token instead. Fixes #1273
1 parent a3c2946 commit fb5bc39

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lexers/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (
122122
if err != nil {
123123
panic(err)
124124
}
125-
return EOF
125+
return subIterator()
126126
}
127127
}
128128
}

lexers/http_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lexers
2+
3+
import (
4+
"testing"
5+
6+
assert "github.com/alecthomas/assert/v2"
7+
"github.com/alecthomas/chroma/v2"
8+
)
9+
10+
// Regression test for https://github.com/alecthomas/chroma/issues/1273:
11+
// the HTTP lexer used to drop body tokens when not wrapped in chroma.Coalesce.
12+
func TestHTTPBodyTokensWithoutCoalesce(t *testing.T) {
13+
source := `GET /foo HTTP/1.1
14+
Content-Type: application/json
15+
User-Agent: foo
16+
17+
{"hello": "world"}
18+
`
19+
tokens, err := chroma.Tokenise(HTTP, nil, source)
20+
assert.NoError(t, err)
21+
assert.Equal(t, source, chroma.Stringify(tokens...))
22+
23+
found := false
24+
for _, token := range tokens {
25+
if token.Type == chroma.LiteralStringDouble && token.Value == `"world"` {
26+
found = true
27+
}
28+
}
29+
assert.True(t, found, "expected body to be tokenised by sub-lexer")
30+
}

0 commit comments

Comments
 (0)