Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent adding unintended whitespaces to the generated html #310

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
351 changes: 117 additions & 234 deletions __tests__/html-renderer/__snapshots__/render-to-html.test.tsx.snap

Large diffs are not rendered by default.

76 changes: 74 additions & 2 deletions __tests__/html-renderer/render-to-html.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,78 @@ const allTests = (renderOpts: HtmlRenderOptions = {}) => () => {
expect(html).toMatchSnapshot();
});

it("should correctly parse html with and without whitespace", () => {
const App = () => {
return (
<div>
<span>
<span>Foo</span> <span>Bar</span>{" "}
<span>These should have whitespace between</span>
</span>
<br />
<span>
<span>Foo</span>
<span>Bar</span>
<span>These should not have whitespace between</span>
</span>
<br />
<span>
-<span>should be all in-line</span>
</span>
<br />
<span>
-{" "}
<span>
should have a space between the "-" and the tag (not a new line)
</span>
</span>
<br />
<span>
-
<span>
should have no space between the "-" and the tag
</span>
</span>
<br />
<span>
[<span>
(<span>***</span>)
</span>]
</span>
<br />
<span>
[{" "}
<span>
( <span>***</span> )
</span>{" "}
]
</span>
<br />
<div>
<span>
Text<span>
<span>Foo</span>
</span>
</span>
</div>
<br />
<div>
<span>
*<strong>item</strong>line1
</span>
<span>
*item<span class="class1">line</span>4
</span>
</div>
</div>
);
};

const html = renderToHtml(<App />, renderOpts);

expect(html).toMatchSnapshot();
});

it("should correctly generate html from component base jsx structure", () => {
const Header = (props: { title: string }) => {
return <h2>{props.title}</h2>;
Expand Down Expand Up @@ -1403,8 +1475,8 @@ Hello {"Daniel"}, how is it going?{"\n"}
};

describe("renderToHTML", () => {
describe("expanded/pretty", allTests());
describe("compact", allTests({ compact: true }));
describe("expanded/pretty", allTests({ pretty: true }));
describe("compact", allTests());

describe("errors", () => {
describe("error throw should contain information on which place in the tree the error occurred", () => {
Expand Down
49 changes: 5 additions & 44 deletions __tests__/utilities/__snapshots__/memo.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`memo > asynchronous memoized components should works as expected 1`] = `
"<div id="root">
<h1>
Hello World
</h1>
</div>
"
`;
exports[`memo > asynchronous memoized components should works as expected 1`] = `"<div id="root"><h1>Hello World</h1</div>"`;

exports[`memo > children of the memoized components should have access to the top level context 1`] = `
"<div id="root">
<div>
<div>
<h1>
Hello
</h1>
</div>
</div>
</div>
"
`;
exports[`memo > children of the memoized components should have access to the top level context 1`] = `"<div id="root"><div><div><h1>Hello</h1></div</div></div>"`;

exports[`memo > memoized components should have access to the context of it's parent 1`] = `
"<div id="root">
<h1>
Hello
</h1>
</div>
"
`;
exports[`memo > memoized components should have access to the context of it's parent 1`] = `"<div id="root"><h1>Hello</h1</div>"`;

exports[`memo > should "forget" memoized results that are past expire time 1`] = `
"<div id="root">
<h1>
Bye cruel world
</h1>
</div>
"
`;
exports[`memo > should "forget" memoized results that are past expire time 1`] = `"<div id="root"><h1>Bye cruel world</h1</div>"`;

exports[`memo > should correctly memoize the component 1`] = `
"<div id="root">
<h1>
Hello World
</h1>
</div>
"
`;
exports[`memo > should correctly memoize the component 1`] = `"<div id="root"><h1>Hello World</h1</div>"`;
6 changes: 2 additions & 4 deletions src/component-api/component-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
jsxElemToHtmlAsync,
jsxElemToHtmlSync,
} from "../html-renderer/jsx-elem-to-html";
import { jsxElemToHtmlAsync } from "../html-renderer/jsx-elem-to-html-async";
import { jsxElemToHtmlSync } from "../html-renderer/jsx-elem-to-html-sync";
import type { HtmlRenderOptions } from "../html-renderer/render-to-html";
import {
jsxElemToJsonAsync,
Expand Down
Loading
Loading