From 4d46a5b5831797eb3b063a4d43a0f3936335523c Mon Sep 17 00:00:00 2001 From: Martial Maillot Date: Thu, 19 Dec 2024 09:16:12 +0100 Subject: [PATCH] fix(contributions): td dans le thead d'un tableau # Conflicts: # packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap --- .../DisplayContentContribution.tsx | 19 +++- .../DisplayContentContribution.test.tsx | 25 +++++ .../DisplayContentContribution.test.tsx.snap | 97 +++++++++++++------ 3 files changed, 109 insertions(+), 32 deletions(-) diff --git a/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx b/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx index cb408dcc14..0f8d0c2e8c 100644 --- a/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx +++ b/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx @@ -96,12 +96,24 @@ const mapTbody = (tbody: Element) => { } } + const forceTh = (child: Element) => { + child.children = child.children.map((child) => { + if (child.type === "tag" && child.name === "td") { + child.name = "th"; + } + return child; + }); + return child; + }; + return ( {theadChildren.length > 0 && ( {theadChildren.map((child, index) => ( - {renderChildrenWithNoTrim(child)} + + {renderChildrenWithNoTrim(forceTh(child))} + ))} )} @@ -118,7 +130,10 @@ function getItem(domNode: Element, titleLevel: number) { } function renderChildrenWithNoTrim(domNode) { - return domToReact(domNode.children as DOMNode[]); + if (domNode.children) { + return domToReact(domNode.children as DOMNode[]); + } + return domToReact(domNode); } const getHeadingElement = (titleLevel: number, domNode) => { diff --git a/packages/code-du-travail-frontend/src/contributions/__tests__/DisplayContentContribution.test.tsx b/packages/code-du-travail-frontend/src/contributions/__tests__/DisplayContentContribution.test.tsx index 3175be62aa..41fad2d3a6 100644 --- a/packages/code-du-travail-frontend/src/contributions/__tests__/DisplayContentContribution.test.tsx +++ b/packages/code-du-travail-frontend/src/contributions/__tests__/DisplayContentContribution.test.tsx @@ -323,6 +323,31 @@ describe("DisplayContentContribution", () => { expect(asFragment().firstChild).toMatchSnapshot(); }); + it(`should replace td by th in thead`, () => { + const { asFragment } = render( + + + + Titre 1 + Titre 2 + + + + +

Pour les cadres, la prolongation ...

+
  • L’employeur et le salarié donnent par écrit ou par mail.

+ + + `} + titleLevel={3} + >
+ ); + + expect(asFragment().firstChild).toMatchSnapshot(); + }); + it(`should keep whitespace in specific tag`, () => { const { asFragment } = render(
@@ -80,7 +80,7 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag

@@ -147,7 +147,7 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag >
@@ -397,10 +397,10 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag exports[`DisplayContentContribution Accordions should replace details element by one accordion 1`] = `
@@ -481,10 +481,10 @@ exports[`DisplayContentContribution Accordions should replace details element by exports[`DisplayContentContribution Accordions should replace details element within details element 1`] = `
@@ -551,7 +551,7 @@ exports[`DisplayContentContribution Accordions should replace details element wi >
@@ -640,10 +640,10 @@ exports[`DisplayContentContribution Accordions should replace multiple details e
@@ -797,7 +797,7 @@ exports[`DisplayContentContribution Accordions should replace multiple details e exports[`DisplayContentContribution Alerts should have space in table item for a strong and an other content 1`] = `

@@ -943,7 +943,7 @@ exports[`DisplayContentContribution Alerts should replace div with alert class i exports[`DisplayContentContribution Alerts should replace div with alert class to Alert component 1`] = `

Ceci est un @@ -1064,7 +1064,7 @@ exports[`DisplayContentContribution Tables should keep whitespace in specific ta exports[`DisplayContentContribution Tables should not change if thead is already present 1`] = `

@@ -1102,14 +1102,14 @@ exports[`DisplayContentContribution Tables should not change if thead is already exports[`DisplayContentContribution Tables should render correctly a table with multiple head lines 1`] = `
- - - - @@ -1230,9 +1230,46 @@ exports[`DisplayContentContribution Tables should render correctly a table with `; +exports[`DisplayContentContribution Tables should replace td by th in thead 1`] = ` +
+
@@ -1126,8 +1126,8 @@ exports[`DisplayContentContribution Tables should render correctly a table with

-
+ @@ -1144,10 +1144,10 @@ exports[`DisplayContentContribution Tables should render correctly a table with

- +
@@ -1164,8 +1164,8 @@ exports[`DisplayContentContribution Tables should render correctly a table with

-
+ @@ -1182,7 +1182,7 @@ exports[`DisplayContentContribution Tables should render correctly a table with

- +
+ + + + + + +
+

+ Pour les + + cadres + + , la prolongation ... +

+
+
    +
  • +

    + L’employeur et le salarié donnent par écrit ou par mail. +

    +
  • +
+
+
+`; + exports[`DisplayContentContribution should keep whitespace in specific tag 1`] = `

Ceci est un @@ -1262,7 +1299,7 @@ exports[`DisplayContentContribution should keep whitespace in specific tag 1`] = exports[`DisplayContentContribution should not remove space between strong and em tag in p tag 1`] = `

@@ -1278,7 +1315,7 @@ exports[`DisplayContentContribution should not remove space between strong and e exports[`DisplayContentContribution should return html 1`] = `

hello