Skip to content

Commit

Permalink
fix(contributions): td dans le thead d'un tableau
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap
  • Loading branch information
m-maillot committed Dec 19, 2024
1 parent fb11f43 commit 4d46a5b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<UITable>
{theadChildren.length > 0 && (
<thead>
{theadChildren.map((child, index) => (
<tr key={`tr-${index}`}>{renderChildrenWithNoTrim(child)}</tr>
<tr key={`tr-${index}`}>
{renderChildrenWithNoTrim(forceTh(child))}
</tr>
))}
</thead>
)}
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,31 @@ describe("DisplayContentContribution", () => {
expect(asFragment().firstChild).toMatchSnapshot();
});

it(`should replace td by th in thead`, () => {
const { asFragment } = render(
<DisplayContentContribution
content={`
<table>
<thead>
<tr>
<td colspan="1" rowspan="1">Titre 1</td>
<td colspan="1" rowspan="1">Titre 2</td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="1" rowspan="1"><p>Pour les <strong>cadres</strong>, la prolongation ...</p></td>
<td colspan="1" rowspan="1"><ul><li><p>L’employeur et le salarié donnent par écrit ou par mail.</p></li></ul></td>
</tr>
</tbody>
</table>`}
titleLevel={3}
></DisplayContentContribution>
);

expect(asFragment().firstChild).toMatchSnapshot();
});

it(`should keep whitespace in specific tag`, () => {
const { asFragment } = render(
<DisplayContentContribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`DisplayContentContribution Accordions should not fail if no summary tag 1`] = `
<div>
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -80,7 +80,7 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag
</p>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -147,7 +147,7 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag
>
<div>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -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`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -481,10 +481,10 @@ exports[`DisplayContentContribution Accordions should replace details element by

exports[`DisplayContentContribution Accordions should replace details element within details element 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -551,7 +551,7 @@ exports[`DisplayContentContribution Accordions should replace details element wi
>
<div>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -640,10 +640,10 @@ exports[`DisplayContentContribution Accordions should replace multiple details e
<body>
<div>
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div
class="sc-1678e795-1 bYYZgz"
class="sc-10167302-1 hzFSzJ"
data-accordion-component="Accordion"
data-testid="contrib-accordion"
>
Expand Down Expand Up @@ -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`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<table
class="sc-4e3ba411-0 kGLBMt"
Expand Down Expand Up @@ -877,7 +877,7 @@ exports[`DisplayContentContribution Alerts should have space in table item for a

exports[`DisplayContentContribution Alerts should replace div with alert class in li component to Alert component 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div>
<p>
Expand Down Expand Up @@ -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`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<div
class="sc-6c78813f-0 fnbVWG"
Expand All @@ -961,7 +961,7 @@ exports[`DisplayContentContribution Alerts should replace div with alert class t
exports[`DisplayContentContribution Headings should replace span with class "title" and "sub-titles" with heading 1`] = `
<div>
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<h2
class="sc-b879fc2d-0 hyiHle"
Expand All @@ -979,7 +979,7 @@ exports[`DisplayContentContribution Headings should replace span with class "tit

exports[`DisplayContentContribution Tables should add thead to table if not present and move table into a Table element 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<table
class="sc-4e3ba411-0 kKVapO"
Expand Down Expand Up @@ -1034,7 +1034,7 @@ exports[`DisplayContentContribution Tables should add thead to table if not pres

exports[`DisplayContentContribution Tables should keep whitespace in specific tag 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<p>
Ceci est un
Expand Down Expand Up @@ -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`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<table>
<tbody>
Expand Down Expand Up @@ -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`] = `
<div>
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<table
class="sc-4e3ba411-0 kKVapO"
>
<thead>
<tr>
<td
<th
colspan="1"
rowspan="2"
>
Expand All @@ -1126,8 +1126,8 @@ exports[`DisplayContentContribution Tables should render correctly a table with
</p>
</td>
<td
</th>
<th
colspan="2"
rowspan="1"
>
Expand All @@ -1144,10 +1144,10 @@ exports[`DisplayContentContribution Tables should render correctly a table with
</p>
</td>
</th>
</tr>
<tr>
<td
<th
colspan="1"
rowspan="1"
>
Expand All @@ -1164,8 +1164,8 @@ exports[`DisplayContentContribution Tables should render correctly a table with
</p>
</td>
<td
</th>
<th
colspan="1"
rowspan="1"
>
Expand All @@ -1182,7 +1182,7 @@ exports[`DisplayContentContribution Tables should render correctly a table with
</p>
</td>
</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -1230,9 +1230,46 @@ exports[`DisplayContentContribution Tables should render correctly a table with
</div>
`;

exports[`DisplayContentContribution Tables should replace td by th in thead 1`] = `
<div
class="sc-10167302-0 dCJsPi"
>
<table>
<tbody>
<tr>
<td
colspan="1"
rowspan="1"
>
<p>
Pour les
<strong>
cadres
</strong>
, la prolongation ...
</p>
</td>
<td
colspan="1"
rowspan="1"
>
<ul>
<li>
<p>
L’employeur et le salarié donnent par écrit ou par mail.
</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</div>
`;

exports[`DisplayContentContribution should keep whitespace in specific tag 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<p>
Ceci est un
Expand Down Expand Up @@ -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`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<p>
<strong>
Expand All @@ -1278,7 +1315,7 @@ exports[`DisplayContentContribution should not remove space between strong and e

exports[`DisplayContentContribution should return html 1`] = `
<div
class="sc-1678e795-0 fDgxNU"
class="sc-10167302-0 dCJsPi"
>
<p>
hello
Expand Down

0 comments on commit 4d46a5b

Please sign in to comment.