-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Preprocessing tries to process style string within script section #5292
Comments
Preprocessing happens before any parsing of the component, and is entirely regex based. I guess I'm ambivalent about how I think something like this ought to be handled. Do we want to start searching for a |
I think I just ran into this obscure bug, here's the code: <script type="text/typescript">
function renderUserStyles(suppliedPalette) {
if (!suppliedPalette)
return "";
let palette = "<style>:root {";
let paletteCount = 0;
suppliedPalette.map(color => {
palette += `--user-color-${paletteCount++}: ${color};`;
});
palette += "}</style>";
}
</script>
<!-- / stuff /-->
<svelte:head>
{@html renderUserStyles(thisUser.palette)}
</svelte:head> The error I get is
|
Spent the morning today trying to figure out what's going on here. As a note for other victims of this bug, you can work around it by fooling the regex: const styles = `<${''}style>:root { ${css} }</${''}style>`; |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This came up again in sveltejs/kit#1796 with the slight variation that An idea about how to approach this: |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Had similar problems in prettier-plugin-svelte where I solved this by checking if a tag is inside another tag and remove the inner tag if so |
I created sveltejs/svelte-preprocess#507 which seems to be a duplicate of this. Wanted to highlight that this bug even occurs if the |
Pasting svelte code into a template literal also causes some stuff to be added to the output string. I posted about it on Stackoverflow: https://stackoverflow.com/questions/75223639/strange-error-with-template-literal-adding-to-string |
Got this error after updating to sveltekit v2 with this component: <script lang="ts">
import printCss from "$lib/styles/print.css?raw";
export let innerHtml: string;
const printContent = () => {
const printWindow = window.open("", "_blank");
if (printWindow) {
printWindow.document.write(
`<html><head><title>Print</title><style>${printCss}</style></head><body>`,
);
printWindow.document.write(innerHtml);
printWindow.document.write("</body></html>");
printWindow.document.close();
printWindow.onload = () => {
printWindow.print();
printWindow.onafterprint = () => {};
printWindow.close();
};
}
};
</script>
<button on:click={printContent}>
Print
</button> @arggh 's solution worked to fix it. |
I also ran into this after upgrading to sveltekit 2. Followed @arggh 's workaround. |
Thanks @arggh still an issue for me. Is there an official solution? |
I have run into this when inlining a styled SVG into my component, which doesn't seem to be an implausible use-case. The workaround doesn't work in my case because for some reason the style isn't added as a document node if I inject it using a function. Come to think of it I probably just needed an I can have the style tag in my SVG, but I can't figure out how to pass it any parameters (so I can't change the fill or stroke colour, for example) without hitthing this error. |
This is also an issue for me. I use strings that contain style tags in multiple places in my code. This was working fine in before upgrading to @sveltejs/vite-plugin-svelte from @sveltejs/kit/vite. Kind of ridiculous that upgrading caused a regression like this. The regex trickery is a super hacky fix. |
Describe the bug
I have a component which generates a small bit of HTML which is injected into an
iframe
. The HTML contains astyle
section which the preprocessing tries to transform, yielding a parser error because there are interpolated values in it.(Accidentally opened this issue in
svelte-preprocess
first: sveltejs/svelte-preprocess#225The problem is in the wrapper code though.)
To Reproduce
A component like this will cause the error:
[Repository with Webpack config]
Expected behavior
No attempted transform and thus no errors.
Stacktraces
Stack trace
Information about your project:
svelte
version: 3.24.1Severity
Low; workarounds exist.
Workaround
Trick the Regex looking for
<style>
, e.g.:The text was updated successfully, but these errors were encountered: