Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Keep extending spacing marks inside conjuncts in the fallback split

Claude134607c86911
diff --git a/src/lib/whole-characters.test.ts b/src/lib/whole-characters.test.ts
index d8bccf3..4d8b502 100644
--- a/src/lib/whole-characters.test.ts
+++ b/src/lib/whole-characters.test.ts
@@ -30,6 +30,7 @@ const samples = [
"πŸ˜€\u{200D}",
"\u{1019}\u{102C}",
"\u{113D1}\u{11392}",
+ "\u{D15}\u{D4D}\u{D3E}\u{D15}",
"\u{915}\u{94D}\u{200C}\u{924}",
"a\u{200D}πŸ˜€",
];
@@ -94,3 +95,49 @@ test("the toWellFormed fallback matches the built-in", () => {
assert.equal(toWellFormedWithoutBuiltIn(sample), sample.toWellFormed());
}
});
+
+// Every code point after each kind of character the fallback tracks. Indic
+// conjuncts are checked only within the six scripts the table covers: newer
+// ICU versions join more scripts there, and browsers differ on those anyway.
+test("the fallback agrees on every code point after each kind of start", () => {
+ const ranges: Array<[number, number]> = [
+ [0x0, 0xd7ff],
+ [0xe000, 0xffff],
+ [0x11000, 0x11fff],
+ [0x1f000, 0x1faff],
+ [0xe0000, 0xe01ef],
+ ];
+ const starts = [
+ "a",
+ "\u{1F469}\u{200D}",
+ "\u{1F1FA}",
+ "\u{1100}",
+ "x\u{600}",
+ "\r",
+ ];
+ const mismatches: string[] = [];
+
+ function check(sample: string) {
+ const expected = splitIntoWholeCharacters(sample);
+ const actual = splitWithoutSegmenter(sample);
+
+ if (JSON.stringify(actual) !== JSON.stringify(expected)) {
+ mismatches.push(JSON.stringify(sample));
+ }
+ }
+
+ for (const start of starts) {
+ for (const [from, to] of ranges) {
+ for (let code = from; code <= to; code += 1) {
+ check(`${start}${String.fromCodePoint(code)}b`);
+ }
+ }
+ }
+
+ for (let code = 0x900; code <= 0xd7f; code += 1) {
+ check(`\u{915}\u{94D}${String.fromCodePoint(code)}\u{915}`);
+ check(`\u{D15}\u{D4D}${String.fromCodePoint(code)}\u{D15}`);
+ }
+
+ assert.deepEqual(mismatches.slice(0, 20), []);
+});
diff --git a/src/lib/whole-characters.ts b/src/lib/whole-characters.ts
index 71589cb..8380d93 100644
--- a/src/lib/whole-characters.ts
+++ b/src/lib/whole-characters.ts
@@ -68,7 +68,18 @@ function extendsPrevious(codePoint: string): boolean {
extendsCharacter.test(codePoint) && !markThatStandsAlone.test(codePoint)
);
}
-const spacingMark = /^[\p{Mc}\u{0E33}\u{0EB3}]$/u;
+// Spacing marks (\p{Mc}) that Unicode nonetheless counts as plain grapheme
+// extension, mostly vowel length marks: these keep an emoji ZWJ sequence or
+// an Indic conjunct going, where other spacing marks end it.
+const spacingMarkThatExtends =
+ /^[\u{9BE}\u{9D7}\u{B3E}\u{B57}\u{BBE}\u{BD7}\u{CC0}\u{CC2}\u{CC7}-\u{CC8}\u{CCA}-\u{CCB}\u{CD5}-\u{CD6}\u{D3E}\u{D57}\u{DCF}\u{DDF}\u{1715}\u{1734}\u{1B35}\u{1B3B}\u{1B3D}\u{1B43}-\u{1B44}\u{1BAA}\u{1BF2}-\u{1BF3}\u{302E}-\u{302F}\u{A953}\u{A9C0}\u{111C0}\u{11235}\u{1133E}\u{1134D}\u{11357}\u{113B8}\u{113C2}\u{113C5}\u{113C7}-\u{113C9}\u{113CF}\u{114B0}\u{114BD}\u{115AF}\u{116B6}\u{11930}\u{1193D}\u{11F41}\u{16FF0}-\u{16FF1}\u{1D165}-\u{1D166}\u{1D16D}-\u{1D172}]$/u;
+
+function isSpacingMark(codePoint: string): boolean {
+ return (
+ /^[\p{Mc}\u{0E33}\u{0EB3}]$/u.test(codePoint) &&
+ !spacingMarkThatExtends.test(codePoint)
+ );
+}
const extendedPictographic = /^\p{Extended_Pictographic}$/u;
type HangulPart = "L" | "V" | "T" | "LV" | "LVT";
@@ -121,6 +132,8 @@ const prepend =
/^[\u{0600}-\u{0605}\u{06DD}\u{070F}\u{0890}\u{0891}\u{08E2}\u{0D4E}\u{110BD}\u{110CD}\u{111C2}\u{111C3}\u{113D1}\u{1193F}\u{11941}\u{11A84}-\u{11A89}\u{11D46}\u{11F02}]$/u;
const formatCharacter = /^\p{Cf}$/u;
const controlCharacter = /^[\p{Cc}\p{Zl}\p{Zp}]$/u;
+const unassigned = /^\p{Cn}$/u;
+const defaultIgnorable = /^\p{Default_Ignorable_Code_Point}$/u;
// Unicode's Control class: nothing joins onto or after these, apart from CR LF.
function isControl(codePoint: string): boolean {
@@ -128,6 +141,11 @@ function isControl(codePoint: string): boolean {
return true;
}
+ // Unassigned code points reserved as invisible also count as Control.
+ if (unassigned.test(codePoint) && defaultIgnorable.test(codePoint)) {
+ return true;
+ }
+
return (
formatCharacter.test(codePoint) &&
!extendsPrevious(codePoint) &&
@@ -223,7 +241,7 @@ export function splitWithoutSegmenter(text: string): string[] {
// Spacing marks join the character but, unlike other marks, end an
// emoji ZWJ sequence or an Indic conjunct.
const nonSpacingExtend =
- extendsPrevious(codePoint) && !spacingMark.test(codePoint);
+ extendsPrevious(codePoint) && !isSpacingMark(codePoint);
pictographicBeforeJoiner =
codePoint === zeroWidthJoiner && inPictographicSequence;