Skip to content

Commit

Permalink
Remove broken emoji from text (#1693)
Browse files Browse the repository at this point in the history
* remove broken emoji from string

* add changeset

* add test

* update changeset

* delete old changeset

* remove console.log
  • Loading branch information
armgjoka authored Feb 26, 2025
1 parent 2025ba3 commit 3a6216e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-mugs-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Builder]: Remove broken emoji from text
31 changes: 31 additions & 0 deletions packages/core/src/parsers/builder/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { BuilderContent } from '@builder.io/sdk';
import { builderContentToMitosisComponent } from './builder';

describe('Unpaired Surrogates', () => {
test('removes unpaired surrogates from Text component content', () => {
const builderContent: BuilderContent = {
data: {
blocks: [
{
'@type': '@builder.io/sdk:Element' as const,
component: {
name: 'Text',
options: {
text: `Hello \uD800 World. Welcome to \uDFFF section`,
},
},
},
],
},
};

const output = builderContentToMitosisComponent(builderContent);
// Text should be cleaned of unpaired surrogates
expect(output.children[0].children[0].properties._text).toBe(
'Hello World. Welcome to section',
);
// Verify unpaired surrogates are removed
expect(output.children[0].children[0].properties._text).not.toContain('\uD800');
expect(output.children[0].children[0].properties._text).not.toContain('\uDFFF');
});
});
9 changes: 8 additions & 1 deletion packages/core/src/parsers/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,14 @@ const builderContentPartToMitosisComponent = (

try {
if (elem.component?.name === 'Text') {
elem.component.options.text = elem.component.options.text.replace(voidElemRegex, '$1 />');
const text = elem.component.options.text;
elem.component.options.text = text.replace(voidElemRegex, '$1 />');
// Remove broken emojis
const hasUnpairedSurrogate =
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g;
if (hasUnpairedSurrogate.test(text)) {
elem.component.options.text = text.replace(hasUnpairedSurrogate, '');
}
}
} catch (_error) {
// pass
Expand Down

0 comments on commit 3a6216e

Please sign in to comment.