Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb896ce

Browse files
committedMay 16, 2023
fix: wrap root node of the rich text input with a fragment instead of paragraph
1 parent 5cc2831 commit eb896ce

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed
 

‎.changeset/thick-jars-teach.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-uikit/rich-text-utils': patch
3+
---
4+
5+
Change behavior of the deserialization function to wrap the root node of the editor with a fragment instead of a paragraph

‎packages/components/inputs/rich-text-utils/src/html/html.spec.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('html', () => {
1919
const htmlValue = '<span>hello</span>';
2020

2121
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
22-
'<p><span>hello</span></p>'
22+
'<span>hello</span>'
2323
);
2424
});
2525
});
@@ -28,7 +28,7 @@ describe('html', () => {
2828
const htmlValue = '<span style="font-weight: bold;">hello</span>';
2929

3030
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
31-
'<p><strong>hello</strong></p>'
31+
'<strong>hello</strong>'
3232
);
3333
});
3434
});
@@ -37,7 +37,7 @@ describe('html', () => {
3737
const htmlValue = '<span style="display: block;">hello</span>';
3838

3939
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
40-
'<p><span>hello</span></p>'
40+
'<span>hello</span>'
4141
);
4242
});
4343
});
@@ -47,7 +47,7 @@ describe('html', () => {
4747
'<span style="display: block; text-decoration: underline;">hello</span>';
4848

4949
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
50-
'<p><u>hello</u></p>'
50+
'<u>hello</u>'
5151
);
5252
});
5353
});
@@ -57,7 +57,7 @@ describe('html', () => {
5757
'<span style="font-weight: bold; text-decoration: underline;">hello</span>';
5858

5959
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
60-
'<p><u><strong>hello</strong></u></p>'
60+
'<u><strong>hello</strong></u>'
6161
);
6262
});
6363
});
@@ -67,7 +67,7 @@ describe('html', () => {
6767
'<span style="font-weight: bold; font-style: italic; text-decoration: underline;">hello</span>';
6868

6969
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
70-
'<p><u><em><strong>hello</strong></em></u></p>'
70+
'<u><em><strong>hello</strong></em></u>'
7171
);
7272
});
7373
});
@@ -77,7 +77,7 @@ describe('html', () => {
7777
'<span style="font-weight: bold; font-style: italic; text-decoration-line: underline line-through;">hello</span>';
7878

7979
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
80-
'<p><del><u><em><strong>hello</strong></em></u></del></p>'
80+
'<del><u><em><strong>hello</strong></em></u></del>'
8181
);
8282
});
8383
});
@@ -96,7 +96,7 @@ describe('html', () => {
9696
'<span style="font-weight: bold"><span style="font-style: italic;">hello</span></span>';
9797

9898
expect(html.serialize(html.deserialize(htmlValue))).toEqual(
99-
'<p><em><strong>hello</strong></em></p>'
99+
'<em><strong>hello</strong></em>'
100100
);
101101
});
102102
});

‎packages/components/inputs/rich-text-utils/src/html/html.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,16 @@ const wrapWithParagraph = (
179179
textContent: TElement | TText | (TElement | TText)[]
180180
) => jsx('element', { type: 'paragraph' }, textContent);
181181

182-
const wrapWithParagraphIfRootElement = (
182+
const wrapWithFragment = (
183+
textContent: TElement | TText | (TElement | TText)[]
184+
) => jsx('fragment', {}, textContent);
185+
186+
const wrapWithFragmentIfRootElement = (
183187
el: HTMLElement | ChildNode,
184188
textContent: TElement | TText | (TElement | TText)[]
185189
) =>
186190
el.parentNode?.nodeName === 'BODY' // root element, because body is eventually turned to React fragment
187-
? wrapWithParagraph(textContent)
191+
? wrapWithFragment(textContent)
188192
: textContent;
189193

190194
export type Deserialized = Descendant | null;
@@ -194,7 +198,7 @@ const deserializeElement = (
194198
): Deserialized | Deserialized[] => {
195199
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#value
196200
if (el.nodeType === 3) {
197-
return wrapWithParagraphIfRootElement(el, { text: el.textContent || '' }); // for root TEXT_NODE -> wrap with <p>
201+
return wrapWithFragmentIfRootElement(el, { text: el.textContent || '' }); // for root TEXT_NODE -> wrap with <></>
198202
} else if (el.nodeType !== 1) {
199203
return null; // for non-ELEMENT_NODE
200204
}
@@ -228,7 +232,7 @@ const deserializeElement = (
228232

229233
if (isEmpty(styleObj)) {
230234
// if no style attrs -> just use `span`
231-
return wrapWithParagraphIfRootElement(
235+
return wrapWithFragmentIfRootElement(
232236
el,
233237
jsx('element', { type: 'span' }, children)
234238
);
@@ -252,12 +256,12 @@ const deserializeElement = (
252256
);
253257
if (isEmpty(attrs)) {
254258
// if all style attr values are irrelevant -> just use `span`
255-
return wrapWithParagraphIfRootElement(
259+
return wrapWithFragmentIfRootElement(
256260
el,
257261
jsx('element', { type: 'span' }, children)
258262
);
259263
}
260-
return wrapWithParagraphIfRootElement(
264+
return wrapWithFragmentIfRootElement(
261265
el,
262266
// children mapping to cover nested elements within text e.g. <span>Some <span>text</span></span>
263267
children.map((child) =>

‎packages/components/inputs/rich-text-utils/src/localized/localized.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('createLocalizedString', () => {
2020
expect(
2121
createLocalizedString(['en', 'de'], { en: 'hello world' })
2222
).toEqual({
23-
en: '<p>hello world</p>',
23+
en: 'hello world',
2424
de: emptyValue,
2525
});
2626
});
@@ -30,8 +30,8 @@ describe('createLocalizedString', () => {
3030
expect(
3131
createLocalizedString(['en', 'de'], { en: 'hello world', de: 'Hallo' })
3232
).toEqual({
33-
en: '<p>hello world</p>',
34-
de: '<p>Hallo</p>',
33+
en: 'hello world',
34+
de: 'Hallo',
3535
});
3636
});
3737
});

0 commit comments

Comments
 (0)
Please sign in to comment.