Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
add new render test to check that empty style tag is not injected, sl…
Browse files Browse the repository at this point in the history
…ightly change render body assignment to not inject empty style tag if twind isn't utilised.
  • Loading branch information
jaymanmdev committed Sep 4, 2023
1 parent 8eeab5a commit c143ff4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
Expand All @@ -32,7 +33,7 @@ export function render(c: Context, Component: VNode) {
: ''
c.res.body = html.replace(
headContent,
`${headContent}<style>${css}</style>`,
`${headContent}${css.length > 0 ? `<style>${css}</style>` : ''}`,
)
} catch (_err) {
if (c.dev) {
Expand Down
30 changes: 30 additions & 0 deletions test/render.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<h3 id='styled'>
non-styled <code>h3</code> component
</h3>
)
}

app.get('/a', (c) => render(c, <NonStyled />))

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,
)
})

0 comments on commit c143ff4

Please sign in to comment.