Skip to content

Commit 64b0b02

Browse files
authored
Fix ordinal superscript for numbers ending in 0 (20th, 30th, etc.) (#3241)
- Changed falsy check to explicit !== null for numericValue - Fixes issue where 0 was treated as falsy, preventing superscript - Affects both separate segments (linked numbers) and combined (20th) formats - Added unit tests for 20th, 30th, and 40th to prevent regression
1 parent 28b2dd4 commit 64b0b02

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

packages/roosterjs-content-model-plugins/lib/autoFormat/numbers/transformOrdinals.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ const ORDINAL_LENGTH = 2;
4040
if (
4141
numberSegment &&
4242
numberSegment.segmentType == 'Text' &&
43-
(numericValue = getNumericValue(numberSegment.text, true /* checkFullText */)) &&
43+
((numericValue = getNumericValue(numberSegment.text, true /* checkFullText */)) !== null) &&
4444
getOrdinal(numericValue) === value
4545
) {
4646
shouldAddSuperScript = true;
4747
}
4848
} else {
4949
const ordinal = value.substring(value.length - ORDINAL_LENGTH); // This value is equal st, nd, rd, th
5050
const numericValue = getNumericValue(value); //This is the numeric part. Ex: 10th, numeric value =
51-
if (numericValue && getOrdinal(numericValue) === ordinal) {
51+
if (numericValue !== null && getOrdinal(numericValue) === ordinal) {
5252
shouldAddSuperScript = true;
5353
}
5454
}

packages/roosterjs-content-model-plugins/test/autoFormat/numbers/transformOrdinalsTest.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,48 @@ describe('transformOrdinals', () => {
142142
runTest(segment, paragraph, { canUndoByBackspace: true } as any, true);
143143
});
144144

145+
it('with 20th', () => {
146+
const segment: ContentModelText = {
147+
segmentType: 'Text',
148+
text: '20th',
149+
format: {},
150+
};
151+
const paragraph: ContentModelParagraph = {
152+
blockType: 'Paragraph',
153+
segments: [segment],
154+
format: {},
155+
};
156+
runTest(segment, paragraph, { canUndoByBackspace: true } as any, true);
157+
});
158+
159+
it('with 30th', () => {
160+
const segment: ContentModelText = {
161+
segmentType: 'Text',
162+
text: '30th',
163+
format: {},
164+
};
165+
const paragraph: ContentModelParagraph = {
166+
blockType: 'Paragraph',
167+
segments: [segment],
168+
format: {},
169+
};
170+
runTest(segment, paragraph, { canUndoByBackspace: true } as any, true);
171+
});
172+
173+
it('with 40th', () => {
174+
const segment: ContentModelText = {
175+
segmentType: 'Text',
176+
text: '40th',
177+
format: {},
178+
};
179+
const paragraph: ContentModelParagraph = {
180+
blockType: 'Paragraph',
181+
segments: [segment],
182+
format: {},
183+
};
184+
runTest(segment, paragraph, { canUndoByBackspace: true } as any, true);
185+
});
186+
145187
it('with 11th', () => {
146188
const segment: ContentModelText = {
147189
segmentType: 'Text',

0 commit comments

Comments
 (0)