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

Fixes #70 Updates styleTransform function to not error when style tag… #71

Merged
merged 1 commit into from
Jul 22, 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
17 changes: 10 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,16 @@ function applyScriptTransforms({ node, scriptTransforms, tagName }) {

function applyStyleTransforms({ node, styleTransforms, tagName, context='' }) {
const attrs = node?.attrs || []
const raw = node.childNodes[0].value
let out = raw
styleTransforms.forEach(transform => {
out = transform({ attrs, raw: out, tagName, context })
})
if (!out.length) { return }
node.childNodes[0].value = out
if (node.childNodes.length) {
const raw = node.childNodes[0].value
let out = raw
styleTransforms.forEach(transform => {
out = transform({ attrs, raw: out, tagName, context })
})
if (out.length) {
node.childNodes[0].value = out
}
}
return node
}

Expand Down
22 changes: 22 additions & 0 deletions test/enhance.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import MyStyleImportSecond from './fixtures/templates/my-style-import-second.mjs
import MyCustomHeading from './fixtures/templates/my-custom-heading.mjs'
import MyCustomHeadingWithNamedSlot from './fixtures/templates/my-custom-heading-with-named-slot.mjs'
import MultipleSlots from './fixtures/templates/multiple-slots.mjs'
import MyEmptyStyle from './fixtures/templates/my-empty-style.mjs'

function Head() {
return `
Expand Down Expand Up @@ -1102,3 +1103,24 @@ test('multiple slots with unnamed slot first', t => {
)
t.end()
})

test('should render empty style tag', t => {
const html = enhance({
bodyContent: true,
elements: {
'empty-style': MyEmptyStyle,
}
})
const actual = html`
<empty-style></empty-style>
`
const expected = `
<empty-style enhanced="✨"></empty-style>
`
t.equal(
strip(actual),
strip(expected),
'Does not throw an error when an empty style tag is rendered'
)
t.end()
})
3 changes: 3 additions & 0 deletions test/fixtures/templates/my-empty-style.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function EmptyStyle({ html }) {
return html`<style></style>`;
}
Loading