Skip to content

Commit 3fdcb0a

Browse files
authored
test(htmlupdatermap): demonstrate how to remove html parent node (#204)
1 parent b0dd96d commit 3fdcb0a

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

packages/html/src/lib/tests/htmlToSlate/configuration/htmlUpdaterMap.spec.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('htmlToSlate configuration: htmlUpdaterMap', () => {
9898
'data-alt': alt,
9999
'data-type': 'image',
100100
},
101-
[]
101+
[],
102102
);
103103
},
104104
},
@@ -147,7 +147,7 @@ describe('htmlToSlate configuration: htmlUpdaterMap', () => {
147147
'data-src': src,
148148
'data-alt': alt,
149149
},
150-
[]
150+
[],
151151
);
152152
},
153153
},
@@ -167,4 +167,80 @@ describe('htmlToSlate configuration: htmlUpdaterMap', () => {
167167
]
168168
`);
169169
});
170+
171+
it('can be used to remove a parent node', () => {
172+
const html =
173+
'<div class="lexical-table-container"><table class="lexical-table" style="border-collapse: collapse;"><tr class="lexical-table-row"><th class="lexical-table-cell lexical-table-cell-header-1">Texte pour la cellule \'en-tête 1</th><th class="lexical-table-cell lexical-table-cell-header-1">Texte pour la cellule d\'en-tête 2</th></tr><tr class="lexical-table-row"><td class="lexical-table-cell lexical-table-cell-header-0">Texte de la cellule du tableau, ligne 1, col 1</td><td class="lexical-table-cell lexical-table-cell-header-0">Texte de la cellule du tableau, ligne 1, col 2</td></tr></table></div>';
174+
const config: HtmlToSlateConfig = {
175+
...htmlToSlateConfig,
176+
elementTags: {
177+
...htmlToSlateConfig.elementTags,
178+
table: () => ({ type: 'table' }),
179+
tr: () => ({ type: 'table-row' }),
180+
td: () => ({ type: 'table-cell' }),
181+
th: () => ({ type: 'table-header-cell' }),
182+
},
183+
htmlUpdaterMap: {
184+
div: (el) => {
185+
// is this is a direct parent of a table
186+
const table = findOne((node) => node.name === 'table', [el]);
187+
if (table) {
188+
return table;
189+
}
190+
return el;
191+
},
192+
},
193+
};
194+
expect(htmlToSlate(html, config)).toMatchInlineSnapshot(`
195+
[
196+
{
197+
"children": [
198+
{
199+
"children": [
200+
{
201+
"children": [
202+
{
203+
"text": "Texte pour la cellule 'en-tête 1",
204+
},
205+
],
206+
"type": "table-header-cell",
207+
},
208+
{
209+
"children": [
210+
{
211+
"text": "Texte pour la cellule d'en-tête 2",
212+
},
213+
],
214+
"type": "table-header-cell",
215+
},
216+
],
217+
"type": "table-row",
218+
},
219+
{
220+
"children": [
221+
{
222+
"children": [
223+
{
224+
"text": "Texte de la cellule du tableau, ligne 1, col 1",
225+
},
226+
],
227+
"type": "table-cell",
228+
},
229+
{
230+
"children": [
231+
{
232+
"text": "Texte de la cellule du tableau, ligne 1, col 2",
233+
},
234+
],
235+
"type": "table-cell",
236+
},
237+
],
238+
"type": "table-row",
239+
},
240+
],
241+
"type": "table",
242+
},
243+
]
244+
`);
245+
});
170246
});

0 commit comments

Comments
 (0)