-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ajouter un espace après les liens fiche cdt (#6237)
* fix: ajouter un espace après les liens fiche cdt * refactor: utilisation de html-react-parser * chore: review --------- Co-authored-by: victor <[email protected]>
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/code-du-travail-frontend/src/modules/code-du-travail/ContentParser.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import parse, { domToReact } from "html-react-parser"; | ||
import { ReactHTMLElement } from "react"; | ||
|
||
export const ContentParser = ({ | ||
children, | ||
}): string | JSX.Element | JSX.Element[] => { | ||
const options = { | ||
replace: (domNode) => { | ||
if ( | ||
domNode.name === "a" && | ||
domNode.children && | ||
domNode.children.length > 0 | ||
) { | ||
let tagElement = domNode.children[0]; | ||
while (tagElement.type !== "text") { | ||
tagElement = tagElement.children[0]; | ||
} | ||
const text = tagElement.data; | ||
tagElement.data = text.trim(); | ||
const textToTrim = text[text.length - 1] === " "; | ||
return ( | ||
<> | ||
<a {...domNode.attribs}>{domToReact(domNode.children)}</a> | ||
|
||
{textToTrim ? " " : ""} | ||
</> | ||
); | ||
} | ||
}, | ||
}; | ||
return <>{parse(children, options)}</>; | ||
}; |
31 changes: 31 additions & 0 deletions
31
...ges/code-du-travail-frontend/src/modules/code-du-travail/__tests__/ContentParser.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { render } from "@testing-library/react"; | ||
import { ContentParser } from "../ContentParser"; | ||
|
||
describe("CDT content parser", () => { | ||
test("should move space at the end of link to the end of link tag", () => { | ||
const html = `<a href="https://test.com">text </a>to test`; | ||
const { baseElement } = render(<ContentParser>{html}</ContentParser>); | ||
|
||
expect(baseElement.outerHTML).toEqual( | ||
`<body><div><a href="https://test.com">text</a> to test</div></body>` | ||
); | ||
}); | ||
|
||
test("should not add space at the end of link to the end of link tag if no space to trim", () => { | ||
const html = `<a href="https://test.com">text</a> to test`; | ||
const { baseElement } = render(<ContentParser>{html}</ContentParser>); | ||
|
||
expect(baseElement.outerHTML).toEqual( | ||
`<body><div><a href="https://test.com">text</a> to test</div></body>` | ||
); | ||
}); | ||
|
||
test("should move space at the end of link to the end of link tag even within tag", () => { | ||
const html = `<a href="https://test.com"><b>text </b></a>to test`; | ||
const { baseElement } = render(<ContentParser>{html}</ContentParser>); | ||
|
||
expect(baseElement.outerHTML).toEqual( | ||
`<body><div><a href="https://test.com"><b>text</b></a> to test</div></body>` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters