Skip to content

Commit 2d8e20b

Browse files
authored
Merge pull request #107 from darkriszty/bug/zero-width-chars
Handle the most probable zero-width characters
2 parents 27740f5 + 5dfcbf0 commit 2d8e20b

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Fixed
9+
- Issue #106: Fixed table prettification breaking when cells contain zero-width characters.
810

911
## 4.0.0 - 2026-04-17
1012
### Added

src/models/cell.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class Cell {
1919
}
2020

2121
private getCharDisplayLength(character: string): number {
22+
// handle the most probable zero-width characters
23+
if (/^[\u{200B}-\u{200F}\u{2060}-\u{2064}\u{FEFF}\u{034F}\u{061C}\u{00AD}]$/u.test(character))
24+
return 0;
25+
2226
// for the specified ranges use a length of 2, otherwise a length of 1
2327
return /^(([\u{4E00}-\u{9FFF}])|([\u{3400}-\u{4DBF}])|([\u{20000}-\u{2A6DF}])|([\u{2A700}-\u{2B73F}])|([\u{2B740}-\u{2B81F}]))$/u.test(character)
2428
? 2
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
| Syntax | Example | Expected behavior | Actual behavior |
2+
|--------------------|----------|-------------------|------------------|
3+
| `@alice` | @alice | mention link | mention link |
4+
| `\@alice` | \@alice | no mention link | **mention link** |
5+
| ``` `@alice` ``` | `@alice` | no mention link | no mention link |
6+
| `@​alice` with ZWSP | @​alice | no mention link | no mention link |
7+
8+
| Code | Character Name | Example | Purpose |
9+
|--------|---------------------------|---------|-------------------------------------|
10+
| U+200B | Zero Width Space | ab​cd | Invisible word boundary |
11+
| U+200C | Zero Width Non-Joiner | ab‌cd | Prevents ligature joining |
12+
| U+200D | Zero Width Joiner | ab‍cd | Joins characters into ligature |
13+
| U+2060 | Word Joiner | ab⁠cd | Prevents line break |
14+
| U+2061 | Function Application | ab⁡cd | Invisible math operator |
15+
| U+2062 | Invisible Times | ab⁢cd | Invisible multiplication sign |
16+
| U+2063 | Invisible Separator | ab⁣cd | Invisible list separator |
17+
| U+2064 | Invisible Plus | ab⁤cd | Invisible addition sign |
18+
| U+034F | Combining Grapheme Joiner | ab͏cd | Joins combining character sequences |
19+
| U+00AD | Soft Hyphen | ab­cd | Optional line-break hyphen |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
| Syntax | Example | Expected behavior | Actual behavior |
2+
|---------------------|----------|-------------------|------------------|
3+
| `@alice` | @alice | mention link | mention link |
4+
| `\@alice` | \@alice | no mention link | **mention link** |
5+
| ``` `@alice` ``` | `@alice` | no mention link | no mention link |
6+
| `@​alice` with ZWSP | @​alice | no mention link | no mention link |
7+
8+
| Code | Character Name | Example | Purpose |
9+
|---|---|---|---|
10+
| U+200B | Zero Width Space | ab​cd | Invisible word boundary |
11+
| U+200C | Zero Width Non-Joiner | ab‌cd | Prevents ligature joining |
12+
| U+200D | Zero Width Joiner | ab‍cd | Joins characters into ligature |
13+
| U+2060 | Word Joiner | ab⁠cd | Prevents line break |
14+
| U+2061 | Function Application | ab⁡cd | Invisible math operator |
15+
| U+2062 | Invisible Times | ab⁢cd | Invisible multiplication sign |
16+
| U+2063 | Invisible Separator | ab⁣cd | Invisible list separator |
17+
| U+2064 | Invisible Plus | ab⁤cd | Invisible addition sign |
18+
| U+034F | Combining Grapheme Joiner | ab͏cd | Joins combining character sequences |
19+
| U+00AD | Soft Hyphen | ab­cd | Optional line-break hyphen |

test/unitTests/models/cell.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,16 @@ suite("Cell tests", () => {
4646
test("getLength() for specific CJK characters 3", () => {
4747
assert.strictEqual(new Cell("零件加工").getLength(), 8);
4848
});
49+
50+
test("getLength() for zero-width space returns 0", () => {
51+
assert.strictEqual(new Cell("\u200B").getLength(), 0);
52+
});
53+
54+
test("getLength() for text with zero-width space excludes it from length", () => {
55+
assert.strictEqual(new Cell("@\alice").getLength(), 6);
56+
});
57+
58+
test("getLength() for text with zero-width joiner excludes it from length", () => {
59+
assert.strictEqual(new Cell("a\u200Db").getLength(), 2);
60+
});
4961
});

0 commit comments

Comments
 (0)