File tree 4 files changed +33
-3
lines changed
packages/components/inputs
localized-rich-text-input/src
4 files changed +33
-3
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @commercetools-uikit/rich-text-utils ' : patch
3
+ ---
4
+
5
+ Fix ` deserializeElement ` function by wrapping each resulting non-empty child text node with a paragraph
Original file line number Diff line number Diff line change @@ -16,6 +16,15 @@ const initialValue = {
16
16
es : lorem ,
17
17
} ;
18
18
19
+ const complexMarkup =
20
+ '<ol><li><span style="font-weight: bold; font-family: "Comic Sans MS";">Computermouse for <span style="text-decoration-line: underline;">controlling</span></span></li></ol><span><table class="table table-bordered"><tbody><tr><td>hello</td></tr><tr><td><p>world<img src="https://www.rollingstone.com/wp-content/uploads/2019/01/shutterstock_10010937aj.jpg" style="width: 100%; float: right;" class="pull-right img-circle"></p></td></tr></tbody></table></span><ol><li><span style="font-weight: bold; font-family: "Comic Sans MS";">' ;
21
+
22
+ const initialValueWithComplexMarkup = {
23
+ en : complexMarkup ,
24
+ de : complexMarkup ,
25
+ es : complexMarkup ,
26
+ } ;
27
+
19
28
const emptyValue = '' ;
20
29
21
30
export const routePath = '/localized-rich-text-input' ;
@@ -267,6 +276,15 @@ const DefaultRoute = () => (
267
276
onClickExpand = { ( ) => { } }
268
277
/>
269
278
</ Spec >
279
+ < Spec label = "with complex markup" omitPropsList >
280
+ < LocalizedRichTextInput
281
+ onChange = { ( ) => { } }
282
+ value = { initialValueWithComplexMarkup }
283
+ selectedLanguage = "en"
284
+ horizontalConstraint = { 7 }
285
+ defaultExpandMultilineText = { true }
286
+ />
287
+ </ Spec >
270
288
</ Suite >
271
289
) ;
272
290
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ describe('html', () => {
106
106
'<ol><li><span style="font-weight: bold; font-family: "Comic Sans MS";">Computermouse for <span style="text-decoration-line: underline;">controlling</span></span></li></ol><table class="table table-bordered"><tbody><tr><td>hello</td></tr><tr><td><p>world<img src="https://www.rollingstone.com/wp-content/uploads/2019/01/shutterstock_10010937aj.jpg" style="width: 100%; float: right;" class="pull-right img-circle"></p></td></tr></tbody></table><ol><li><span style="font-weight: bold; font-family: "Comic Sans MS";"><span style="text-decoration-line: underline;"><br></span></span></li></ol>' ;
107
107
108
108
expect ( html . serialize ( html . deserialize ( htmlValue ) ) ) . toEqual (
109
- '<ol><li><strong>Computermouse for </strong><u><strong>controlling</strong></u></li></ol>hello<p>world</p><ol><li><u><strong></strong></u></li></ol>'
109
+ '<ol><li><strong>Computermouse for </strong><u><strong>controlling</strong></u></li></ol><p> hello</p> <p>world</p><ol><li><u><strong></strong></u></li></ol>'
110
110
) ;
111
111
} ) ;
112
112
} ) ;
Original file line number Diff line number Diff line change @@ -175,12 +175,16 @@ const mapper: TMapper = {
175
175
} ,
176
176
} ;
177
177
178
+ const wrapWithParagraph = (
179
+ textContent : TElement | TText | ( TElement | TText ) [ ]
180
+ ) => jsx ( 'element' , { type : 'paragraph' } , textContent ) ;
181
+
178
182
const wrapWithParagraphIfRootElement = (
179
183
el : HTMLElement | ChildNode ,
180
184
textContent : TElement | TText | ( TElement | TText ) [ ]
181
185
) =>
182
186
el . parentNode ?. nodeName === 'BODY' // root element, because body is eventually turned to React fragment
183
- ? jsx ( 'element' , { type : 'paragraph' } , textContent )
187
+ ? wrapWithParagraph ( textContent )
184
188
: textContent ;
185
189
186
190
export type Deserialized = Descendant | null ;
@@ -275,7 +279,10 @@ const deserializeElement = (
275
279
return children . map ( ( child ) => jsx ( 'text' , attrs , child ) ) ;
276
280
}
277
281
278
- return children ;
282
+ // each non-empty text node must be wrapped with a paragraph
283
+ return children . map ( ( child ) =>
284
+ Text . isText ( child ) && child . text ? wrapWithParagraph ( child ) : child
285
+ ) ;
279
286
} ;
280
287
const deserialize = ( html : Html ) => {
281
288
const document = new DOMParser ( ) . parseFromString (
You can’t perform that action at this time.
0 commit comments