From c143ff41271ab5d775271ca77d61abbe16c8a62d Mon Sep 17 00:00:00 2001 From: JaymanMDev Date: Mon, 4 Sep 2023 10:53:40 +1000 Subject: [PATCH] add new render test to check that empty style tag is not injected, slightly change render body assignment to not inject empty style tag if twind isn't utilised. --- render.ts | 3 ++- test/render.test.tsx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/render.ts b/render.ts index c2b64a5..95d19ac 100644 --- a/render.ts +++ b/render.ts @@ -18,6 +18,7 @@ export function render(c: Context, Component: VNode) { try { const { html, css } = extract(htmlString) + console.log(css.length) const startIdxOfHeadContent = html.indexOf(HEAD_TAG) const endIdxOfHeadContent = html.indexOf( `${HEAD_TAG.at(0)}/${HEAD_TAG.slice(1)}`, @@ -32,7 +33,7 @@ export function render(c: Context, Component: VNode) { : '' c.res.body = html.replace( headContent, - `${headContent}`, + `${headContent}${css.length > 0 ? `` : ''}`, ) } catch (_err) { if (c.dev) { diff --git a/test/render.test.tsx b/test/render.test.tsx index 92c071d..5ee7d67 100644 --- a/test/render.test.tsx +++ b/test/render.test.tsx @@ -38,3 +38,33 @@ Deno.test('render', async () => { 'text/html; charset=utf-8', ) }) + +Deno.test('No empty style tag is injected if no twind styles are utilised.', async () => { + const app = new cheetah() + + const { render } = new Renderer() + + function NonStyled() { + return ( +

+ non-styled h3 component +

+ ) + } + + app.get('/a', (c) => render(c, )) + + const a = await app.fetch(new Request('http://localhost/a')) + + const tx = await a.text() + + const document = new DOMParser().parseFromString( + tx, + 'text/html', + ) + assert(document) + assertEquals( + [...document.getElementsByTagName('style')].length, + 0, + ) +})