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
37 changes: 37 additions & 0 deletions src/lib/chunkify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,40 @@ test('does not emit a spurious empty chunk when the last byte is covered', () =>
chunks: [{ start_offset: 0, end_offset: 4, is_covered: true }],
} satisfies ChunkedCoverage)
})

test('merges adjacent same-coverage chunks separated by whitespace-only gap', () => {
// The whitespace-only uncovered chunk between two covered chunks should be
// absorbed so the two covered chunks merge into one. This is handled by the
// early `continue` at the top of merge(), not the else-if branch.
let coverage = {
text: 'a{color:red}\n\nb{color:blue}',
// ^12 ^14 — the \n\n gap is whitespace-only
ranges: [
{ start: 0, end: 12 },
{ start: 14, end: 27 },
],
url: 'https://example.com',
}
let result = chunkify(coverage)
delete coverage.ranges
expect(result).toEqual({
...coverage,
chunks: [{ start_offset: 0, end_offset: 27, is_covered: true }],
} satisfies ChunkedCoverage)
})

test('absorbs a zero-length covered chunk into the surrounding uncovered chunk', () => {
// A zero-length range (start === end) produces an empty chunk.
// The empty chunk should not appear in the output.
let coverage = {
text: 'a{color:red}',
ranges: [{ start: 5, end: 5 }],
url: 'https://example.com',
}
let result = chunkify(coverage)
delete coverage.ranges
expect(result).toEqual({
...coverage,
chunks: [{ start_offset: 0, end_offset: 12, is_covered: false }],
} satisfies ChunkedCoverage)
})
7 changes: 2 additions & 5 deletions src/lib/chunkify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ function merge(stylesheet: ChunkedCoverage): ChunkedCoverage {
previous_chunk = chunk
continue
}
// If the current chunk is only whitespace or empty, add it to the previous
else if (
WHITESPACE_ONLY_REGEX.test(stylesheet.text.slice(chunk.start_offset, chunk.end_offset)) ||
chunk.end_offset === chunk.start_offset
) {
// If the current chunk is empty (zero-length range), absorb it into the previous
else if (chunk.end_offset === chunk.start_offset) {
latest_chunk.end_offset = chunk.end_offset
// do not update previous_chunk
continue
Expand Down
Loading