Skip to content

Commit cb8c331

Browse files
CopilotiOvergaard
andauthored
UFM: Fixes the truncate filter to only add ellipsis when text is actually truncated (closes #20395) (#20396)
* Initial plan * Fix UFM truncate filter to only add ellipsis when text is actually truncated Co-authored-by: iOvergaard <[email protected]> * feat: trims string before evaluating --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: iOvergaard <[email protected]>
1 parent 4af9b1e commit cb8c331

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { expect } from '@open-wc/testing';
2+
import { UmbUfmTruncateFilterApi } from './truncate.filter.js';
3+
4+
describe('UmbUfmTruncateFilter', () => {
5+
let filter: UmbUfmTruncateFilterApi;
6+
7+
beforeEach(() => {
8+
filter = new UmbUfmTruncateFilterApi();
9+
});
10+
11+
describe('filter', () => {
12+
it('should not add ellipsis when text is shorter than length', () => {
13+
const result = filter.filter('Test', 10);
14+
expect(result).to.equal('Test');
15+
});
16+
17+
it('should not add ellipsis when text equals length', () => {
18+
const result = filter.filter('Test', 4);
19+
expect(result).to.equal('Test');
20+
});
21+
22+
it('should add ellipsis when text is longer than length', () => {
23+
const result = filter.filter('Lorem ipsum', 5);
24+
expect(result).to.equal('Lorem…');
25+
});
26+
27+
it('should add custom tail when text is truncated', () => {
28+
const result = filter.filter('Lorem ipsum', 5, '...');
29+
expect(result).to.equal('Lorem...');
30+
});
31+
32+
it('should not add tail when tail is false and text is truncated', () => {
33+
const result = filter.filter('Lorem ipsum', 5, 'false');
34+
expect(result).to.equal('Lorem');
35+
});
36+
37+
it('should add ellipsis when tail is true and text is truncated', () => {
38+
const result = filter.filter('Lorem ipsum', 5, 'true');
39+
expect(result).to.equal('Lorem…');
40+
});
41+
42+
it('should not add ellipsis when tail is false and text is not truncated', () => {
43+
const result = filter.filter('Test', 10, 'false');
44+
expect(result).to.equal('Test');
45+
});
46+
47+
it('should not add custom tail when text is not truncated', () => {
48+
const result = filter.filter('Test', 10, '...');
49+
expect(result).to.equal('Test');
50+
});
51+
52+
it('should handle empty string', () => {
53+
const result = filter.filter('', 10);
54+
expect(result).to.equal('');
55+
});
56+
57+
it('should handle non-string input', () => {
58+
const result = filter.filter(null as any, 10);
59+
expect(result).to.equal(null);
60+
});
61+
62+
it('should trim whitespace before adding ellipsis', () => {
63+
const result = filter.filter('Hello world', 6);
64+
expect(result).to.equal('Hello…');
65+
});
66+
});
67+
});

src/Umbraco.Web.UI.Client/src/packages/ufm/filters/truncate.filter.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ import { UmbUfmFilterBase } from './base.filter.js';
22

33
class UmbUfmTruncateFilterApi extends UmbUfmFilterBase {
44
filter(str: string, length: number, tail?: string) {
5-
if (typeof str !== 'string' || !str.length) return str;
5+
if (typeof str !== 'string') return str;
6+
7+
// Remove leading/trailing whitespace before calculating length
8+
str = str.trim();
9+
10+
// Only add ellipsis if the string was actually truncated
11+
if (!str.length || str.length <= length) {
12+
return str;
13+
}
14+
615
if (tail === 'false') tail = '';
716
if (tail === 'true') tail = '…';
817
tail = !tail && tail !== '' ? '…' : tail;
9-
1018
return str.slice(0, length).trim() + tail;
1119
}
1220
}
1321

1422
export { UmbUfmTruncateFilterApi as api };
23+
export { UmbUfmTruncateFilterApi };

0 commit comments

Comments
 (0)