Skip to content

Commit 07a0702

Browse files
Merge pull request #552 from magiccao:fix/content-bubble-utf8-decode
PiperOrigin-RevId: 1015663427
2 parents beb53ec + 26c1a9e commit 07a0702

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// 1p-ONLY-IMPORTS: import {beforeEach, describe, expect, it}
19+
import {initTestBed} from '../../testing/utils';
20+
import {ComponentFixture, TestBed} from '@angular/core/testing';
21+
import {HttpClient} from '@angular/common/http';
22+
import {ContentBubbleComponent} from './content-bubble.component';
23+
import {ARTIFACT_SERVICE} from '../../core/services/interfaces/artifact';
24+
import {SAFE_VALUES_SERVICE} from '../../core/services/interfaces/safevalues';
25+
import {MockSafeValuesService} from '../../core/services/testing/mock-safevalues.service';
26+
import {MARKDOWN_COMPONENT} from '../markdown/markdown.component.interface';
27+
import {ChatPanelMessagesInjectionToken} from '../chat-panel/chat-panel.component.i18n';
28+
29+
describe('ContentBubbleComponent', () => {
30+
let component: ContentBubbleComponent;
31+
32+
beforeEach(async () => {
33+
initTestBed(); // required for 1p compat
34+
await TestBed.configureTestingModule({
35+
imports: [ContentBubbleComponent],
36+
providers: [
37+
{provide: HttpClient, useValue: {}},
38+
{
39+
provide: ARTIFACT_SERVICE,
40+
useValue: jasmine.createSpyObj('ArtifactService', ['loadArtifact']),
41+
},
42+
{provide: SAFE_VALUES_SERVICE, useClass: MockSafeValuesService},
43+
{
44+
provide: MARKDOWN_COMPONENT,
45+
useValue: class MockMarkdownComponent {},
46+
},
47+
{provide: ChatPanelMessagesInjectionToken, useValue: {}},
48+
],
49+
}).compileComponents();
50+
51+
const fixture = TestBed.createComponent(ContentBubbleComponent);
52+
component = fixture.componentInstance;
53+
});
54+
55+
it('should create', () => {
56+
expect(component).toBeTruthy();
57+
});
58+
59+
describe('getTextContent', () => {
60+
const toDataUrl = (text: string) => {
61+
const base64 = btoa(
62+
String.fromCharCode(...new TextEncoder().encode(text)),
63+
);
64+
return `data:text/plain;base64,${base64}`;
65+
};
66+
67+
it('should decode multibyte UTF-8 content without garbling it', () => {
68+
const text = 'こんにちは 🌙 报告';
69+
expect(component['getTextContent'](toDataUrl(text))).toBe(text);
70+
});
71+
72+
it('should still decode ASCII content', () => {
73+
const text = 'Hello, world!';
74+
expect(component['getTextContent'](toDataUrl(text))).toBe(text);
75+
});
76+
77+
it('should return an empty string for a data URL without a comma', () => {
78+
expect(component['getTextContent']('no-comma-here')).toBe('');
79+
});
80+
81+
it('should return an empty string for an empty data URL', () => {
82+
expect(component['getTextContent']('')).toBe('');
83+
});
84+
});
85+
});

‎src/app/components/content-bubble/content-bubble.component.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ export class ContentBubbleComponent implements OnChanges {
172172
if (commaIndex === -1) return '';
173173
const base64 = dataUrl.substring(commaIndex + 1);
174174
try {
175-
return atob(base64);
175+
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
176+
return new TextDecoder('utf-8').decode(bytes);
176177
} catch (e) {
177178
return 'Failed to decode text content';
178179
}

0 commit comments

Comments
 (0)