Skip to content

Commit d86a053

Browse files
authored
fix: adding code to format nextline character (\r\n) (#7378)
#### Details Adding code to format nextline character (\r\n) ##### Motivation During feature work [Update to React 18 for Docs Repo](https://dev.azure.com/mseng/1ES/_workitems/edit/2189969) analysis and local testing, we found that there are multiple errors when we refresh the info-example pages after the first render of the page ![image](https://github.com/microsoft/accessibility-insights-web/assets/150002431/57f49878-dc51-42b1-b5a7-2288550afd49) These errors are related to difference in render in server and client side. This error can be reproducible in main branch as well. While debugging we found that a next line character available in server side which was causing difference in render. UI package is used to render info-example content and hence require a fix to avoid those errors in consuming applications as well. ##### Context <!-- Are there any parts that you've intentionally left out-of-scope for a later PR to handle? --> <!-- Were there any alternative approaches you considered? What tradeoffs did you consider? --> #### Pull request checklist <!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox --> - [ ] Addresses an existing issue: #0000 - [x] Ran `yarn fastpass` - [x] Added/updated relevant unit test(s) (and ran `yarn test`) - [x] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage` - [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`. - [ ] (UI changes only) Added screenshots/GIFs to description above - [ ] (UI changes only) Verified usability with NVDA/JAWS
1 parent 2214fa4 commit d86a053

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/tests/unit/tests/views/content/markup/__snapshots__/code-example.test.tsx.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,31 @@ exports[`<CodeExample> renders with unterminated highlighted region 1`] = `
247247
</div>
248248
</DocumentFragment>
249249
`;
250+
251+
exports[`<CodeExample> renders highlight and multiple line breaks using \\r\\n 1`] = `
252+
<DocumentFragment>
253+
<div
254+
class="code-example"
255+
>
256+
<div
257+
class="code-example-code"
258+
>
259+
<mock-codeblock>
260+
Line 1
261+
<br />
262+
Line 2
263+
<span
264+
class="highlight"
265+
>
266+
HIGHLIGHT
267+
<br />
268+
HERE
269+
</span>
270+
Line 3
271+
<br />
272+
Line 4
273+
</mock-codeblock>
274+
</div>
275+
</div>
276+
</DocumentFragment>
277+
`;

src/tests/unit/tests/views/content/markup/code-example.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,12 @@ describe('<CodeExample>', () => {
8080
const renderResult = render(<CodeExample>{`Line 1\nLine 2`}</CodeExample>);
8181
expect(renderResult.container.querySelector('br')).not.toBeNull();
8282
});
83+
84+
it('renders highlight and multiple line breaks using \\r\\n', () => {
85+
const renderResult = render(
86+
<CodeExample>{`Line 1\r\nLine 2 [HIGHLIGHT\r\nHERE]Line 3\r\nLine 4`}</CodeExample>,
87+
);
88+
expect(renderResult.container.querySelector('br')).not.toBeNull();
89+
expect(renderResult.asFragment()).toMatchSnapshot();
90+
});
8391
});

src/views/content/markup/code-example.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export function CodeExample(props: CodeExampleProps): JSX.Element {
3636
}
3737

3838
function renderLineBreaks(str: string): React.ReactNode[] {
39+
const newLineArray = str.includes('\r\n') ? str.split('\r\n') : str.split('\n');
3940
return flatten(
40-
str.split('\n').map(s => [<br key={`line-breaker-${lineCount++}`} />, s]),
41+
newLineArray.map(s => [<br key={`line-breaker-${lineCount++}`} />, s]),
4142
).slice(1);
4243
}
4344

0 commit comments

Comments
 (0)