diff --git a/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx b/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx index eb25292931..cb408dcc14 100644 --- a/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx +++ b/packages/code-du-travail-frontend/src/contributions/DisplayContentContribution.tsx @@ -131,17 +131,19 @@ const getHeadingElement = (titleLevel: number, domNode) => { const options = (titleLevel: number): HTMLReactParserOptions => { let accordionTitle = titleLevel; + let headingTitleLevel = titleLevel; return { replace(domNode) { if (domNode instanceof Element) { if (domNode.name === "span" && domNode.attribs.class === "title") { accordionTitle = titleLevel + 1; + headingTitleLevel = titleLevel + 1; return getHeadingElement(titleLevel, domNode); } if (domNode.name === "span" && domNode.attribs.class === "sub-title") { accordionTitle = titleLevel + 1; - return getHeadingElement(titleLevel + 1, domNode); + return getHeadingElement(headingTitleLevel, domNode); } if (domNode.name === "details") { const items: any[] = []; @@ -175,18 +177,15 @@ const options = (titleLevel: number): HTMLReactParserOptions => { ); } if (domNode.name === "strong") { - // Disable trim on strong return {renderChildrenWithNoTrim(domNode)}; } if (domNode.name === "em") { - // Disable trim on em return {renderChildrenWithNoTrim(domNode)}; } if (domNode.name === "p") { if (!domNode.children.length) { return
; } - // Disable trim on p return

{renderChildrenWithNoTrim(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 944df97074..3175be62aa 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 @@ -15,38 +15,60 @@ describe("DisplayContentContribution", () => { expect(baseElement.firstChild).toMatchSnapshot(); }); it(`should replace span with with heading according to given title level`, () => { - const { baseElement } = render( + const { getByText } = render( Mon titleMon title`} + content={`Ceci est un titreCeci est un sous titre`} titleLevel={4} > ); - expect(baseElement.firstChild).toMatchSnapshot(); + expect(getByText("Ceci est un titre").tagName).toEqual("H4"); + expect(getByText("Ceci est un sous titre").tagName).toEqual("H5"); }); it(`should not add headings higher than h6 for titles`, () => { - const { baseElement } = render( + const { getByText } = render( Mon titleMon title`} + content={`Ceci est un titreCeci est un sous titre`} titleLevel={6} > ); - expect(baseElement.firstChild).toMatchSnapshot(); + expect(getByText("Ceci est un titre").tagName).toEqual("H6"); + expect(getByText("Ceci est un sous titre").tagName).toEqual("STRONG"); }); it(`should not add headings higher than h6 for accordion`, () => { - const { asFragment } = render( + const { getByText } = render( Ceci est un titre
- Mon title + Ceci est un sous titre + Ceci est un sous sous titre
`} titleLevel={6} >
); - expect(asFragment().firstChild).toMatchSnapshot(); + expect(getByText("Ceci est un titre").tagName).toEqual("H6"); + expect(getByText("Ceci est un sous titre").tagName).toEqual("STRONG"); + expect(getByText("Ceci est un sous sous titre").tagName).toEqual( + "STRONG" + ); + }); + it(`should handle sub-title in accordion even if no title`, () => { + const { getByText } = render( + Ceci est un titre +
+ Ceci est un sous titre +
+ `} + titleLevel={4} + >
+ ); + expect(getByText("Ceci est un titre").tagName).toEqual("H4"); + expect(getByText("Ceci est un sous titre").tagName).toEqual("H5"); }); }); diff --git a/packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap b/packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap index 56fe366129..f35b083b96 100644 --- a/packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap +++ b/packages/code-du-travail-frontend/src/contributions/__tests__/__snapshots__/DisplayContentContribution.test.tsx.snap @@ -3,10 +3,10 @@ exports[`DisplayContentContribution Accordions should not fail if no summary tag 1`] = `
@@ -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`] = `

`; -exports[`DisplayContentContribution Headings should not add headings higher than h6 for accordion 1`] = ` -
-
-
-
-
- -
- -
-
-
-
-`; - -exports[`DisplayContentContribution Headings should not add headings higher than h6 for titles 1`] = ` -
-
-
- Mon title -
- - Mon title - -
-
-`; - exports[`DisplayContentContribution Headings should replace span with class "title" and "sub-titles" with heading 1`] = `

`; -exports[`DisplayContentContribution Headings should replace span with with heading according to given title level 1`] = ` -
-
-

- Mon title -

-
- Mon title -
-
-
-`; - exports[`DisplayContentContribution Tables should add thead to table if not present and move table into a Table element 1`] = `

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

@@ -1221,7 +1102,7 @@ exports[`DisplayContentContribution Tables should not change if thead is already exports[`DisplayContentContribution Tables should render correctly a table with multiple head lines 1`] = `

Ceci est un @@ -1381,7 +1262,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`] = `

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

hello