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

Invalid double escaping of nested arrays in SSR #393

Open
axel-habermaier opened this issue Jan 10, 2025 · 3 comments
Open

Invalid double escaping of nested arrays in SSR #393

axel-habermaier opened this issue Jan 10, 2025 · 3 comments

Comments

@axel-habermaier
Copy link

axel-habermaier commented Jan 10, 2025

Consider the following code:

function Test() {
  const a = ["<"];
  return <div>{[a, a]}</div>;
}

During SSR, this incorrectly renders the HTML <div>&amp;lt;&amp;lt;</div>, i.e., the < is escaped twice and is thus shown as &lt;&lt; in the browser.

I expected SSR to generate the single-escaped HTML <div>&lt;&lt;</div> which then renders as << in the browser.

@axel-habermaier axel-habermaier changed the title Invalid double escaping Invalid double escaping of nested arrays in SSR Jan 10, 2025
@axel-habermaier
Copy link
Author

axel-habermaier commented Jan 14, 2025

Update: The underlying reason seems to be that the array a is unexpectedly modified during rendering! So if a were at module scope, there would be additional escaping for each render.

@mdynnl
Copy link
Contributor

mdynnl commented Feb 9, 2025

Yeah, that can also be reproduced using only jsx

import { renderToString } from "solid-js/web"

let x = "<"
let a = <>{x}{x}</>
let v = <>{a}{a}</>

console.log(v)
console.log(renderToString(() => <>{v}</>))
console.log(v)

we definitely should create a new array here before escaping its elements

export function escape(s, attr) {
const t = typeof s;
if (t !== "string") {
if (!attr && t === "function") return escape(s());
if (!attr && Array.isArray(s)) {
for (let i = 0; i < s.length; i++) s[i] = escape(s[i]);
return s;

also created https://github.com/mdynnl/solid-ssr-escape-bench to benchmark which approach is faster although it may vary depending on the runtime

after trying with both bun(1.2.3-canary) and node(v20.17.0), i honestly don't know which one is faster as the results are entirely different, but .slice() and Array(length) seems to be on top in most cases which makes sense somewhat. and although i don't know enough js engine internals, i'd expect .slice() to only allocate memory for the array itself, so it's probably the answer here.

@edemaine
Copy link
Contributor

edemaine commented Feb 9, 2025

@mdynnl Can you benchmark return s.map(escape)? That seems like the obvious way to write it, and hopefully map is smart enough to preallocate the array and maybe do the loop slightly more efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants