Skip to content

Commit 22fda96

Browse files
committed
fix(web-components): avoid rendering false boolean as text
1 parent 5f51b5d commit 22fda96

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

src/core/render-to-readable-stream/index.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ describe("brisa core", () => {
3939

4040
const expected = `<div class="test">Hello World</div>`;
4141
expect(result).toEqual(expected);
42-
expect(mockConsoleError.mock.calls[0]).toEqual([
42+
expect(mockConsoleError.mock.calls[0].at(0) as unknown as string).toEqual(
4343
"You should have a <head> tag in your document. Please review your layout. You can experiment some issues with browser JavaScript code without it.",
44-
]);
44+
);
4545
});
4646

4747
it("should not console.error when it has a <head> tag", async () => {

src/utils/brisa-element/index.test.ts

+53-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { describe, it, expect, beforeAll, afterAll, mock } from "bun:test";
1+
import {
2+
describe,
3+
it,
4+
expect,
5+
beforeAll,
6+
afterAll,
7+
mock,
8+
afterEach,
9+
} from "bun:test";
210
import { GlobalRegistrator } from "@happy-dom/global-registrator";
311
import { serialize } from "../serialization";
412

@@ -1794,14 +1802,50 @@ describe("utils", () => {
17941802
expect(webComponent?.shadowRoot?.innerHTML).toBe(`<div>Barbara</div>`);
17951803
});
17961804

1797-
it.todo(
1798-
"should work dangerHTML to inject HTML in a web-component",
1799-
() => {},
1800-
);
1805+
it("should work with booleans and numbers in the same way than React", () => {
1806+
const Component = ({}, { h }: any) =>
1807+
h(null, {}, [
1808+
[null, {}, () => true && ["div", {}, "TRUE"]],
1809+
[null, {}, () => false && ["div", {}, "FALSE"]],
1810+
[null, {}, () => 1 && ["div", {}, "TRUE"]],
1811+
[null, {}, () => 0 && ["div", {}, "FALSE"]],
1812+
]);
1813+
1814+
customElements.define("bool-component", brisaElement(Component));
1815+
1816+
document.body.innerHTML = "<bool-component />";
1817+
const boolComponent = document.querySelector(
1818+
"bool-component",
1819+
) as HTMLElement;
1820+
1821+
expect(boolComponent?.shadowRoot?.innerHTML).toBe(
1822+
"<div>TRUE</div><div>TRUE</div>0",
1823+
);
1824+
});
1825+
1826+
it("should work with booleans and numbers from props in the same way than React", () => {
1827+
const Component = ({ first, second, third, fourth }: any, { h }: any) =>
1828+
h(null, {}, [
1829+
[null, {}, () => first.value && ["div", {}, "TRUE"]],
1830+
[null, {}, () => second.value && ["div", {}, "FALSE"]],
1831+
[null, {}, () => third.value && ["div", {}, "TRUE"]],
1832+
[null, {}, () => fourth.value && ["div", {}, "FALSE"]],
1833+
]);
18011834

1802-
it.todo(
1803-
"should not be possible to inject HTML without dangerHTML",
1804-
() => {},
1805-
);
1835+
customElements.define(
1836+
"bool-component",
1837+
brisaElement(Component, ["first", "second", "third", "fourth"]),
1838+
);
1839+
1840+
document.body.innerHTML =
1841+
"<bool-component first='true' second='false' third='1' fourth='0' />";
1842+
const boolComponent = document.querySelector(
1843+
"bool-component",
1844+
) as HTMLElement;
1845+
1846+
expect(boolComponent?.shadowRoot?.innerHTML).toBe(
1847+
"<div>TRUE</div><div>TRUE</div>0",
1848+
);
1849+
});
18061850
});
18071851
});

src/utils/brisa-element/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default function brisaElement(
158158
lastNodes = arr(el.childNodes).filter(
159159
(node) => !currentElNodes.includes(node),
160160
);
161-
} else {
161+
} else if ((child as unknown as boolean) !== false) {
162162
const textNode = createTextNode(child as string);
163163

164164
insertOrUpdate(textNode);
@@ -170,7 +170,7 @@ export default function brisaElement(
170170
childOrPromise.then(startEffect);
171171
else startEffect(childOrPromise);
172172
});
173-
} else {
173+
} else if ((children as unknown as boolean) !== false) {
174174
el.appendChild(createTextNode(children));
175175
}
176176

0 commit comments

Comments
 (0)