|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + */ |
| 4 | +import { describe, expect, test } from "vitest"; |
| 5 | +import { Errored } from "solid-js"; |
| 6 | +import { Dynamic, renderToString, ssrElement } from "@solidjs/web"; |
| 7 | +import { renderServerComponent } from "../../frames/src/frame-sink.js"; |
| 8 | + |
| 9 | +// A spread's keys and a dynamic tag are runtime strings. The HTML tokenizer |
| 10 | +// ends a name at whitespace, `/`, `>` or `=`, so a name containing one of |
| 11 | +// those (or a quote, `<`, a control character, or nothing at all) would not |
| 12 | +// be read back as the one name it was. The client's setAttribute and |
| 13 | +// createElement refuse such names; on the server an attribute with one is |
| 14 | +// dropped and a tag with one throws. |
| 15 | +const unreadable = ["a b", 'a"b', "a'b", "a=b", "a>b", "a<b", "a/b", "a\tb", "a\nb", "a\0b", ""]; |
| 16 | + |
| 17 | +describe("ssrElement attribute names", () => { |
| 18 | + test("a value attribute whose name is not one HTML name is dropped", () => { |
| 19 | + const props: Record<string, string> = {}; |
| 20 | + for (const name of unreadable) props[name] = "v"; |
| 21 | + props.id = "kept"; |
| 22 | + expect(renderToString(() => ssrElement("div", props, undefined, false))).toBe( |
| 23 | + '<div id="kept"></div>' |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + test("a boolean or empty attribute whose name is not one HTML name is dropped", () => { |
| 28 | + const flags: Record<string, boolean> = {}; |
| 29 | + const empties: Record<string, string> = {}; |
| 30 | + for (const name of unreadable) { |
| 31 | + flags[name] = true; |
| 32 | + empties[name] = ""; |
| 33 | + } |
| 34 | + expect( |
| 35 | + renderToString(() => |
| 36 | + ssrElement("input", { type: "text", ...flags, placeholder: "p" }, undefined, false) |
| 37 | + ) |
| 38 | + ).toBe('<input type="text" placeholder="p" />'); |
| 39 | + expect( |
| 40 | + renderToString(() => ssrElement("input", { ...empties, disabled: "" }, undefined, false)) |
| 41 | + ).toBe("<input disabled />"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("a dropped name stays dropped on every render", () => { |
| 45 | + for (let i = 0; i < 3; i++) { |
| 46 | + expect( |
| 47 | + renderToString(() => ssrElement("div", { "a b": "v", "a b c": true }, undefined, false)) |
| 48 | + ).toBe("<div></div>"); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + test("names the tokenizer reads whole render as written", () => { |
| 53 | + const props = { |
| 54 | + "data-x": "1", |
| 55 | + "aria-label": "l", |
| 56 | + "@click": "open = true", |
| 57 | + "x-on:click": "go()", |
| 58 | + ":class": "c", |
| 59 | + "xlink:href": "#a", |
| 60 | + "a&b": "2", |
| 61 | + hidden: true |
| 62 | + }; |
| 63 | + expect(renderToString(() => ssrElement("div", props, undefined, false))).toBe( |
| 64 | + '<div data-x="1" aria-label="l" @click="open = true" x-on:click="go()" :class="c" ' + |
| 65 | + 'xlink:href="#a" a&b="2" hidden></div>' |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test("through a compiled spread", () => { |
| 70 | + const props = { "a b": "v", 'a"b': true, "data-ok": "1" }; |
| 71 | + expect(renderToString(() => <div {...props} />)).toMatch( |
| 72 | + /^<div( _hk=\w+)? data-ok="1"><\/div>$/ |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test("through <Dynamic> with a spread", () => { |
| 77 | + const props = { type: "text", "a b": "", "a=b": "v", placeholder: "p" }; |
| 78 | + expect(renderToString(() => <Dynamic component="input" {...props} />)).toMatch( |
| 79 | + /^<input( _hk=\w+)? type="text" placeholder="p" \/>$/ |
| 80 | + ); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("ssrElement tag names", () => { |
| 85 | + const invalid = ["div x", "a>b", "a/b", 'a"b', "a=b", "a\tb", "a\0b", "1a", "-a", ""]; |
| 86 | + |
| 87 | + test("a tag that is not one HTML element name throws, on every render", () => { |
| 88 | + for (const tag of invalid) { |
| 89 | + for (let i = 0; i < 2; i++) { |
| 90 | + expect(() => renderToString(() => ssrElement(tag, {}, undefined, false))).toThrow( |
| 91 | + "is not a valid tag name" |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + test("<Dynamic> with such a tag fails into its error boundary", () => { |
| 98 | + expect(() => renderToString(() => <Dynamic component="div x" />)).toThrow( |
| 99 | + "is not a valid tag name" |
| 100 | + ); |
| 101 | + const html = renderToString(() => ( |
| 102 | + <Errored fallback={<p>fallback</p>}> |
| 103 | + <Dynamic component="div x" title="t" /> |
| 104 | + </Errored> |
| 105 | + )); |
| 106 | + expect(html).toContain(">fallback</p>"); |
| 107 | + expect(html).not.toContain("<div"); |
| 108 | + }); |
| 109 | + |
| 110 | + test("custom elements and case-sensitive SVG names render", () => { |
| 111 | + expect(renderToString(() => <Dynamic component="my-widget" data-x="1" />)).toMatch( |
| 112 | + /^<my-widget( _hk=\w+)? data-x="1"><\/my-widget>$/ |
| 113 | + ); |
| 114 | + expect(renderToString(() => <Dynamic component="foreignObject" />)).toMatch( |
| 115 | + /^<foreignObject( _hk=\w+)?><\/foreignObject>$/ |
| 116 | + ); |
| 117 | + expect(renderToString(() => ssrElement("x-a.b_c:d", {}, undefined, false))).toBe( |
| 118 | + "<x-a.b_c:d></x-a.b_c:d>" |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("binding-slot spread attribute names", () => { |
| 124 | + test("a stand-in at a key that is not one HTML name binds nothing", async () => { |
| 125 | + const Spread = (props: any) => { |
| 126 | + const row = props.row({ id: 1 }); |
| 127 | + return <li {...{ "a b": row.v, "data-ok": row.w, "onClick x": row.pick, onClick: row.go }} />; |
| 128 | + }; |
| 129 | + const chunks: any[] = await (renderServerComponent(Spread, { frame: { id: "names" } }) as any); |
| 130 | + const html = chunks |
| 131 | + .find(c => c.type === "html") |
| 132 | + .html.replace(/ data-lha="\d+"/g, "") |
| 133 | + .replace(/<!--lh:\/?\d+-->/g, ""); |
| 134 | + expect(html).toContain('<li _s:data-ok="row#0:w" _s:on:click="row#0:go">'); |
| 135 | + }); |
| 136 | +}); |
0 commit comments