@shadow-js/web 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +2 -3
  3. package/dist/dev/attributes.js +1 -5
  4. package/dist/dev/attributes.js.map +1 -1
  5. package/dist/dev/class-style.d.ts +10 -5
  6. package/dist/dev/class-style.js +44 -47
  7. package/dist/dev/class-style.js.map +1 -1
  8. package/dist/dev/control-flow/dynamic.js +2 -2
  9. package/dist/dev/control-flow/dynamic.js.map +1 -1
  10. package/dist/dev/control-flow/error-boundary.js +2 -2
  11. package/dist/dev/control-flow/error-boundary.js.map +1 -1
  12. package/dist/dev/control-flow/portal.d.ts +2 -2
  13. package/dist/dev/control-flow/portal.js +15 -13
  14. package/dist/dev/control-flow/portal.js.map +1 -1
  15. package/dist/dev/control-flow/suspense-list.js +2 -2
  16. package/dist/dev/control-flow/suspense-list.js.map +1 -1
  17. package/dist/dev/control-flow/suspense.js +2 -2
  18. package/dist/dev/control-flow/suspense.js.map +1 -1
  19. package/dist/dev/element.js +13 -27
  20. package/dist/dev/element.js.map +1 -1
  21. package/dist/dev/events.js +64 -80
  22. package/dist/dev/events.js.map +1 -1
  23. package/dist/dev/extend.d.ts +2 -2
  24. package/dist/dev/extend.js +2 -2
  25. package/dist/dev/insert.d.ts +11 -0
  26. package/dist/dev/insert.js +99 -100
  27. package/dist/dev/insert.js.map +1 -1
  28. package/dist/dev/internal/accessor.js +56 -0
  29. package/dist/dev/internal/accessor.js.map +1 -0
  30. package/dist/dev/internal/claim.js +0 -1
  31. package/dist/dev/internal/claim.js.map +1 -1
  32. package/dist/dev/internal/css-units.js +2 -2
  33. package/dist/dev/internal/css-units.js.map +1 -1
  34. package/dist/dev/internal/portal-context.d.ts +9 -0
  35. package/dist/dev/internal/portal-context.js.map +1 -1
  36. package/dist/dev/internal/react-names.js +22 -12
  37. package/dist/dev/internal/react-names.js.map +1 -1
  38. package/dist/dev/internal/reconcile.d.ts +2 -1
  39. package/dist/dev/internal/reconcile.js +23 -8
  40. package/dist/dev/internal/reconcile.js.map +1 -1
  41. package/dist/dev/server/attributes.js +1 -31
  42. package/dist/dev/server/attributes.js.map +1 -1
  43. package/dist/dev/server/element.js +5 -6
  44. package/dist/dev/server/element.js.map +1 -1
  45. package/dist/dev/server/index.js +1 -1
  46. package/dist/dev/server/portals.js +3 -0
  47. package/dist/dev/server/portals.js.map +1 -1
  48. package/dist/dev/server/render.js +1 -1
  49. package/dist/dev/server/stream.js +1 -1
  50. package/dist/dev/server.js +1 -1
  51. package/dist/dev/spread.js +95 -106
  52. package/dist/dev/spread.js.map +1 -1
  53. package/dist/dev/template.js +1 -3
  54. package/dist/dev/template.js.map +1 -1
  55. package/dist/dev/view-transition.js +12 -4
  56. package/dist/dev/view-transition.js.map +1 -1
  57. package/dist/prod/attributes.js +1 -1
  58. package/dist/prod/attributes.js.map +1 -1
  59. package/dist/prod/class-style.js +1 -1
  60. package/dist/prod/class-style.js.map +1 -1
  61. package/dist/prod/control-flow/dynamic.js +1 -1
  62. package/dist/prod/control-flow/dynamic.js.map +1 -1
  63. package/dist/prod/control-flow/error-boundary.js +1 -1
  64. package/dist/prod/control-flow/error-boundary.js.map +1 -1
  65. package/dist/prod/control-flow/portal.js +1 -1
  66. package/dist/prod/control-flow/portal.js.map +1 -1
  67. package/dist/prod/control-flow/suspense-list.js +1 -1
  68. package/dist/prod/control-flow/suspense-list.js.map +1 -1
  69. package/dist/prod/control-flow/suspense.js +1 -1
  70. package/dist/prod/control-flow/suspense.js.map +1 -1
  71. package/dist/prod/element.js +1 -1
  72. package/dist/prod/element.js.map +1 -1
  73. package/dist/prod/events.js +1 -1
  74. package/dist/prod/events.js.map +1 -1
  75. package/dist/prod/extend.js +1 -1
  76. package/dist/prod/insert.js +1 -1
  77. package/dist/prod/insert.js.map +1 -1
  78. package/dist/prod/internal/accessor.js +2 -0
  79. package/dist/prod/internal/accessor.js.map +1 -0
  80. package/dist/prod/internal/claim.js +1 -1
  81. package/dist/prod/internal/claim.js.map +1 -1
  82. package/dist/prod/internal/css-units.js +1 -1
  83. package/dist/prod/internal/css-units.js.map +1 -1
  84. package/dist/prod/internal/portal-context.js.map +1 -1
  85. package/dist/prod/internal/react-names.js +1 -1
  86. package/dist/prod/internal/react-names.js.map +1 -1
  87. package/dist/prod/internal/reconcile.js +1 -1
  88. package/dist/prod/internal/reconcile.js.map +1 -1
  89. package/dist/prod/server/attributes.js +1 -1
  90. package/dist/prod/server/attributes.js.map +1 -1
  91. package/dist/prod/server/element.js +1 -1
  92. package/dist/prod/server/element.js.map +1 -1
  93. package/dist/prod/server/index.js +1 -1
  94. package/dist/prod/server/portals.js +1 -1
  95. package/dist/prod/server/portals.js.map +1 -1
  96. package/dist/prod/server/render.js +1 -1
  97. package/dist/prod/server/stream.js +1 -1
  98. package/dist/prod/server.js +1 -1
  99. package/dist/prod/spread.js +1 -1
  100. package/dist/prod/spread.js.map +1 -1
  101. package/dist/prod/template.js +1 -1
  102. package/dist/prod/template.js.map +1 -1
  103. package/dist/prod/view-transition.js +1 -1
  104. package/dist/prod/view-transition.js.map +1 -1
  105. package/dist/server/attributes.js +1 -1
  106. package/dist/server/attributes.js.map +1 -1
  107. package/dist/server/class-style.js +1 -1
  108. package/dist/server/class-style.js.map +1 -1
  109. package/dist/server/control-flow/dynamic.js.map +1 -1
  110. package/dist/server/control-flow/error-boundary.js +1 -1
  111. package/dist/server/control-flow/error-boundary.js.map +1 -1
  112. package/dist/server/control-flow/portal.js +1 -1
  113. package/dist/server/control-flow/portal.js.map +1 -1
  114. package/dist/server/control-flow/suspense-list.js +1 -1
  115. package/dist/server/control-flow/suspense-list.js.map +1 -1
  116. package/dist/server/control-flow/suspense.js +1 -1
  117. package/dist/server/control-flow/suspense.js.map +1 -1
  118. package/dist/server/element.js.map +1 -1
  119. package/dist/server/events.js +1 -1
  120. package/dist/server/events.js.map +1 -1
  121. package/dist/server/extend.js +1 -1
  122. package/dist/server/insert.js +1 -1
  123. package/dist/server/insert.js.map +1 -1
  124. package/dist/server/internal/accessor.js +2 -0
  125. package/dist/server/internal/accessor.js.map +1 -0
  126. package/dist/server/internal/claim.js +1 -1
  127. package/dist/server/internal/claim.js.map +1 -1
  128. package/dist/server/internal/css-units.js +1 -1
  129. package/dist/server/internal/css-units.js.map +1 -1
  130. package/dist/server/internal/portal-context.js.map +1 -1
  131. package/dist/server/internal/react-names.js +1 -1
  132. package/dist/server/internal/react-names.js.map +1 -1
  133. package/dist/server/internal/reconcile.js +1 -1
  134. package/dist/server/internal/reconcile.js.map +1 -1
  135. package/dist/server/server/attributes.js +1 -1
  136. package/dist/server/server/attributes.js.map +1 -1
  137. package/dist/server/server/element.js +1 -1
  138. package/dist/server/server/element.js.map +1 -1
  139. package/dist/server/server/index.js +1 -1
  140. package/dist/server/server/portals.js +1 -1
  141. package/dist/server/server/portals.js.map +1 -1
  142. package/dist/server/server/render.js +1 -1
  143. package/dist/server/server/stream.js +1 -1
  144. package/dist/server/server.js +1 -1
  145. package/dist/server/spread.js +1 -1
  146. package/dist/server/spread.js.map +1 -1
  147. package/dist/server/template.js +1 -1
  148. package/dist/server/template.js.map +1 -1
  149. package/dist/server/view-transition.js.map +1 -1
  150. package/dist/server-dev/attributes.js +1 -5
  151. package/dist/server-dev/attributes.js.map +1 -1
  152. package/dist/server-dev/class-style.js +44 -47
  153. package/dist/server-dev/class-style.js.map +1 -1
  154. package/dist/server-dev/control-flow/dynamic.js.map +1 -1
  155. package/dist/server-dev/control-flow/error-boundary.js +2 -2
  156. package/dist/server-dev/control-flow/error-boundary.js.map +1 -1
  157. package/dist/server-dev/control-flow/portal.js +15 -13
  158. package/dist/server-dev/control-flow/portal.js.map +1 -1
  159. package/dist/server-dev/control-flow/suspense-list.js +2 -2
  160. package/dist/server-dev/control-flow/suspense-list.js.map +1 -1
  161. package/dist/server-dev/control-flow/suspense.js +2 -2
  162. package/dist/server-dev/control-flow/suspense.js.map +1 -1
  163. package/dist/server-dev/element.js.map +1 -1
  164. package/dist/server-dev/events.js +64 -80
  165. package/dist/server-dev/events.js.map +1 -1
  166. package/dist/server-dev/extend.js +2 -2
  167. package/dist/server-dev/insert.js +99 -100
  168. package/dist/server-dev/insert.js.map +1 -1
  169. package/dist/server-dev/internal/accessor.js +56 -0
  170. package/dist/server-dev/internal/accessor.js.map +1 -0
  171. package/dist/server-dev/internal/claim.js +0 -1
  172. package/dist/server-dev/internal/claim.js.map +1 -1
  173. package/dist/server-dev/internal/css-units.js +2 -2
  174. package/dist/server-dev/internal/css-units.js.map +1 -1
  175. package/dist/server-dev/internal/portal-context.js.map +1 -1
  176. package/dist/server-dev/internal/react-names.js +22 -12
  177. package/dist/server-dev/internal/react-names.js.map +1 -1
  178. package/dist/server-dev/internal/reconcile.js +23 -8
  179. package/dist/server-dev/internal/reconcile.js.map +1 -1
  180. package/dist/server-dev/server/attributes.js +1 -31
  181. package/dist/server-dev/server/attributes.js.map +1 -1
  182. package/dist/server-dev/server/element.js +5 -6
  183. package/dist/server-dev/server/element.js.map +1 -1
  184. package/dist/server-dev/server/index.js +1 -1
  185. package/dist/server-dev/server/portals.js +3 -0
  186. package/dist/server-dev/server/portals.js.map +1 -1
  187. package/dist/server-dev/server/render.js +1 -1
  188. package/dist/server-dev/server/stream.js +1 -1
  189. package/dist/server-dev/server.js +1 -1
  190. package/dist/server-dev/spread.js +81 -105
  191. package/dist/server-dev/spread.js.map +1 -1
  192. package/dist/server-dev/template.js +1 -3
  193. package/dist/server-dev/template.js.map +1 -1
  194. package/dist/server-dev/view-transition.js.map +1 -1
  195. package/package.json +3 -3
@@ -30,36 +30,6 @@ const ENUMERATED_ATTRIBUTES = /* @__PURE__ */ new Set([
30
30
  "spellcheck",
31
31
  "contenteditable"
32
32
  ]);
33
- /**
34
- * Boolean attributes that a props object writes as presence flags (`disabled: true` gives a bare
35
- * `disabled`). Lower-case names only.
36
- * @internal
37
- */
38
- const BOOLEAN_ATTRIBUTES = /* @__PURE__ */ new Set([
39
- "async",
40
- "autofocus",
41
- "autoplay",
42
- "checked",
43
- "controls",
44
- "default",
45
- "defer",
46
- "disabled",
47
- "formnovalidate",
48
- "hidden",
49
- "inert",
50
- "ismap",
51
- "loop",
52
- "multiple",
53
- "muted",
54
- "nomodule",
55
- "novalidate",
56
- "open",
57
- "playsinline",
58
- "readonly",
59
- "required",
60
- "reversed",
61
- "selected"
62
- ]);
63
33
  /** URL prefixes (after normalization) that are never written. Same as the client. */
64
34
  const UNSAFE_URL_PREFIXES = [
65
35
  "javascript:",
@@ -153,6 +123,6 @@ function ssrAttribute(name, value, isBoolean = false) {
153
123
  if (isUnsafeAttributeValue(lowerName, text)) return "";
154
124
  return ` ${name}="${escapeHtml(text)}"`;
155
125
  }
156
- export { BOOLEAN_ATTRIBUTES, assertAttributeName, attributeText, isEventName, isUnsafeUrl, ssrAttribute };
126
+ export { assertAttributeName, attributeText, isEventName, isUnsafeUrl, ssrAttribute };
157
127
 
158
128
  //# sourceMappingURL=attributes.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"attributes.js","names":[],"sources":["../../../src/server/attributes.ts"],"sourcesContent":["import { SRCDOC_DYNAMIC_DETAIL, warnOnce } from \"../internal/diagnostics\";\nimport { invalid } from \"./errors\";\nimport { escapeHtml, resolveValue } from \"./escape\";\n\n/** Names that can appear in markup: an XML-name subset without `@` or whitespace. Same as the client. */\nconst ATTRIBUTE_NAME = /^[A-Za-z_:][A-Za-z0-9_:.-]*$/;\n\n/** Attributes whose value is a URL and is therefore checked for script schemes. Same as the client. */\nconst URL_ATTRIBUTES: ReadonlySet<string> = new Set([\n \"href\",\n \"src\",\n \"action\",\n \"formaction\",\n \"xlink:href\",\n \"poster\",\n \"data\",\n]);\n\n/**\n * SVG animation attributes (`animate`, `set`): with `attributeName=\"href\"` they write an `href` by animation, so their value\n * is checked like one. `values` is a `;` list and every entry is checked. Same as the client.\n */\nconst ANIMATION_VALUE_ATTRIBUTES: ReadonlySet<string> = new Set([\"values\", \"from\", \"to\", \"by\"]);\n\n/** Enumerated attributes where `false` is a real value (\"false\") rather than an absence. */\nconst ENUMERATED_ATTRIBUTES: ReadonlySet<string> = new Set([\n \"draggable\",\n \"spellcheck\",\n \"contenteditable\",\n]);\n\n/**\n * Boolean attributes that a props object writes as presence flags (`disabled: true` gives a bare\n * `disabled`). Lower-case names only.\n * @internal\n */\nexport const BOOLEAN_ATTRIBUTES: ReadonlySet<string> = new Set([\n \"async\",\n \"autofocus\",\n \"autoplay\",\n \"checked\",\n \"controls\",\n \"default\",\n \"defer\",\n \"disabled\",\n \"formnovalidate\",\n \"hidden\",\n \"inert\",\n \"ismap\",\n \"loop\",\n \"multiple\",\n \"muted\",\n \"nomodule\",\n \"novalidate\",\n \"open\",\n \"playsinline\",\n \"readonly\",\n \"required\",\n \"reversed\",\n \"selected\",\n]);\n\n/** URL prefixes (after normalization) that are never written. Same as the client. */\nconst UNSAFE_URL_PREFIXES: readonly string[] = [\"javascript:\", \"data:text/html\", \"vbscript:\"];\n\n/** Throws a TypeError unless `name` can be written as an attribute name. @internal */\nexport function assertAttributeName(name: string): void {\n if (!ATTRIBUTE_NAME.test(name)) {\n throw invalid(\"INVALID_ATTRIBUTE_NAME\", `\"${name}\" is not a valid attribute name.`);\n }\n}\n\n/** True for `on*` names, which never reach markup. Length matches the client's event test. @internal */\nexport function isEventName(lowerName: string): boolean {\n return lowerName.length > 2 && lowerName.startsWith(\"on\");\n}\n\n/** True for `aria-*`, `data-*` and the enumerated attributes: their booleans are strings. */\nfunction isStringValued(lowerName: string): boolean {\n return (\n lowerName.startsWith(\"aria-\") ||\n lowerName.startsWith(\"data-\") ||\n ENUMERATED_ATTRIBUTES.has(lowerName)\n );\n}\n\n/** Removes C0 and C1 control characters, which browsers ignore inside a URL scheme. */\nfunction stripControls(text: string): string {\n let kept = \"\";\n for (const char of text) {\n const code = char.charCodeAt(0);\n if (code > 0x1f && (code < 0x7f || code > 0x9f)) kept += char;\n }\n return kept;\n}\n\n/** True when the URL would run script, after the normalization browsers apply. @internal */\nexport function isUnsafeUrl(value: string): boolean {\n const normalized = stripControls(value).trim().toLowerCase();\n return UNSAFE_URL_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n\n/** True when `lowerName` carries a URL (or an animated one) and `value` would run script. */\nfunction isUnsafeAttributeValue(lowerName: string, value: string): boolean {\n if (URL_ATTRIBUTES.has(lowerName)) return isUnsafeUrl(value);\n return ANIMATION_VALUE_ATTRIBUTES.has(lowerName) && value.split(\";\").some(isUnsafeUrl);\n}\n\n/** Text to write for `value`, or null when the attribute is removed. Same rules as the client. @internal */\nexport function attributeText(lowerName: string, value: unknown): string | null {\n if (value === null || value === undefined) return null;\n if (value === false) return isStringValued(lowerName) ? \"false\" : null;\n if (value === true) return isStringValued(lowerName) ? \"true\" : \"\";\n return String(value);\n}\n\n/** Refuses symbols, which have no markup form and would otherwise be coerced silently. */\nfunction assertNotSymbol(value: unknown): void {\n if (typeof value === \"symbol\") {\n throw invalid(\"UNRECOGNIZED_INSERT_VALUE\", \"A symbol cannot be an attribute value.\");\n }\n}\n\n/**\n * Renders one attribute as markup with a leading space, or `\"\"` when the attribute is absent: the\n * helper the compiler's server output calls for a dynamic attribute. It applies the client's value\n * rules and URL check, so server and client agree on what is written.\n *\n * - `null`, `undefined` and `false` give `\"\"` (except `false` on `aria-*`, `data-*`, `draggable`,\n * `spellcheck` and `contenteditable`, which is written as `\"false\"`); `true` writes `=\"\"` (or\n * `\"true\"` for those same names). Other values are written as `String(value)`, HTML-escaped.\n * - URL attributes whose value would run script are dropped, with the check of\n * `isUnsafeUrlAttribute` (including the SVG animation attributes `values`, `from`, `to`, `by`).\n * - `srcdoc` is an HTML sink, not a URL: its value is a document the frame runs, escaped for the attribute\n * and otherwise written as it is, with the trust of `innerHTML`. Sanitize it and give the frame a `sandbox`\n * attribute. Development builds warn once with `SRCDOC_DYNAMIC`.\n * - `on*` names are dropped: event handlers never reach markup.\n * - Accessors are called (untracked) until a plain value remains.\n *\n * @param name Attribute name, matching `[A-Za-z_:][A-Za-z0-9_:.-]*`. It is validated before\n * anything else, so an invalid name always throws.\n * @param value The value, or an accessor of it.\n * @param isBoolean `true` for a presence attribute such as `disabled`: a truthy value writes the bare\n * name, a falsy one writes nothing. Default: `false`.\n * @returns Markup such as `' title=\"a&amp;b\"'`, or `\"\"`.\n * @throws TypeError `INVALID_ATTRIBUTE_NAME` for an invalid name, and `UNRECOGNIZED_INSERT_VALUE`\n * when the value is a symbol.\n * @example\n * import { ssrAttribute } from \"@shadow-js/web/server\";\n *\n * ssrAttribute(\"title\", \"Tom & Jerry\"); // ' title=\"Tom &amp; Jerry\"'\n * ssrAttribute(\"disabled\", true, true); // \" disabled\"\n * ssrAttribute(\"href\", \"javascript:alert(1)\"); // \"\"\n * @since 1.0.0\n */\nexport function ssrAttribute(name: string, value: unknown, isBoolean = false): string {\n assertAttributeName(name);\n const lowerName = name.toLowerCase();\n if (isEventName(lowerName)) return \"\";\n const resolved = resolveValue(value);\n assertNotSymbol(resolved);\n if (isBoolean) return resolved ? ` ${name}` : \"\";\n const text = attributeText(lowerName, resolved);\n if (text === null) return \"\";\n if (__DEV__ && lowerName === \"srcdoc\") warnOnce(\"SRCDOC_DYNAMIC\", SRCDOC_DYNAMIC_DETAIL);\n if (isUnsafeAttributeValue(lowerName, text)) return \"\";\n return ` ${name}=\"${escapeHtml(text)}\"`;\n}\n"],"mappings":";;;;;AAKA,MAAM,iBAAiB;;AAGvB,MAAM,iCAAsC,IAAI,IAAI;CAClD;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;;;;AAMD,MAAM,6CAAkD,IAAI,IAAI;CAAC;CAAU;CAAQ;CAAM;AAAI,CAAC;;AAG9F,MAAM,wCAA6C,IAAI,IAAI;CACzD;CACA;CACA;AACF,CAAC;;;;;;AAOD,MAAa,qCAA0C,IAAI,IAAI;CAC7D;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;C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U,UACnB,MAAM,QAAQ,6BAA6B,wCAAwC;AAEvF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAgB,aAAa,MAAc,OAAgB,YAAY,OAAe;CACpF,oBAAoB,IAAI;CACxB,MAAM,YAAY,KAAK,YAAY;CACnC,IAAI,YAAY,SAAS,GAAG,OAAO;CACnC,MAAM,WAAW,aAAa,KAAK;CACnC,gBAAgB,QAAQ;CACxB,IAAI,WAAW,OAAO,WAAW,IAAI,SAAS;CAC9C,MAAM,OAAO,cAAc,WAAW,QAAQ;CAC9C,IAAI,SAAS,MAAM,OAAO;CAC1B,IAAe,cAAc,UAAU,SAAS,kBAAkB,qBAAqB;CACvF,IAAI,uBAAuB,WAAW,IAAI,GAAG,OAAO;CACpD,OAAO,IAAI,KAAK,IAAI,WAAW,IAAI,EAAE;AACvC"}
1
+ {"version":3,"file":"attributes.js","names":[],"sources":["../../../src/server/attributes.ts"],"sourcesContent":["import { SRCDOC_DYNAMIC_DETAIL, warnOnce } from \"../internal/diagnostics\";\nimport { invalid } from \"./errors\";\nimport { escapeHtml, resolveValue } from \"./escape\";\n\n/** Names that can appear in markup: an XML-name subset without `@` or whitespace. Same as the client. */\nconst ATTRIBUTE_NAME = /^[A-Za-z_:][A-Za-z0-9_:.-]*$/;\n\n/** Attributes whose value is a URL and is therefore checked for script schemes. Same as the client. */\nconst URL_ATTRIBUTES: ReadonlySet<string> = new Set([\n \"href\",\n \"src\",\n \"action\",\n \"formaction\",\n \"xlink:href\",\n \"poster\",\n \"data\",\n]);\n\n/**\n * SVG animation attributes (`animate`, `set`): with `attributeName=\"href\"` they write an `href` by animation, so their value\n * is checked like one. `values` is a `;` list and every entry is checked. Same as the client.\n */\nconst ANIMATION_VALUE_ATTRIBUTES: ReadonlySet<string> = new Set([\"values\", \"from\", \"to\", \"by\"]);\n\n/** Enumerated attributes where `false` is a real value (\"false\") rather than an absence. */\nconst ENUMERATED_ATTRIBUTES: ReadonlySet<string> = new Set([\n \"draggable\",\n \"spellcheck\",\n \"contenteditable\",\n]);\n\n/** URL prefixes (after normalization) that are never written. Same as the client. */\nconst UNSAFE_URL_PREFIXES: readonly string[] = [\"javascript:\", \"data:text/html\", \"vbscript:\"];\n\n/** Throws a TypeError unless `name` can be written as an attribute name. @internal */\nexport function assertAttributeName(name: string): void {\n if (!ATTRIBUTE_NAME.test(name)) {\n throw invalid(\"INVALID_ATTRIBUTE_NAME\", `\"${name}\" is not a valid attribute name.`);\n }\n}\n\n/** True for `on*` names, which never reach markup. Length matches the client's event test. @internal */\nexport function isEventName(lowerName: string): boolean {\n return lowerName.length > 2 && lowerName.startsWith(\"on\");\n}\n\n/** True for `aria-*`, `data-*` and the enumerated attributes: their booleans are strings. */\nfunction isStringValued(lowerName: string): boolean {\n return (\n lowerName.startsWith(\"aria-\") ||\n lowerName.startsWith(\"data-\") ||\n ENUMERATED_ATTRIBUTES.has(lowerName)\n );\n}\n\n/** Removes C0 and C1 control characters, which browsers ignore inside a URL scheme. */\nfunction stripControls(text: string): string {\n let kept = \"\";\n for (const char of text) {\n const code = char.charCodeAt(0);\n if (code > 0x1f && (code < 0x7f || code > 0x9f)) kept += char;\n }\n return kept;\n}\n\n/** True when the URL would run script, after the normalization browsers apply. @internal */\nexport function isUnsafeUrl(value: string): boolean {\n const normalized = stripControls(value).trim().toLowerCase();\n return UNSAFE_URL_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n\n/** True when `lowerName` carries a URL (or an animated one) and `value` would run script. */\nfunction isUnsafeAttributeValue(lowerName: string, value: string): boolean {\n if (URL_ATTRIBUTES.has(lowerName)) return isUnsafeUrl(value);\n return ANIMATION_VALUE_ATTRIBUTES.has(lowerName) && value.split(\";\").some(isUnsafeUrl);\n}\n\n/** Text to write for `value`, or null when the attribute is removed. Same rules as the client. @internal */\nexport function attributeText(lowerName: string, value: unknown): string | null {\n if (value === null || value === undefined) return null;\n if (value === false) return isStringValued(lowerName) ? \"false\" : null;\n if (value === true) return isStringValued(lowerName) ? \"true\" : \"\";\n return String(value);\n}\n\n/** Refuses symbols, which have no markup form and would otherwise be coerced silently. */\nfunction assertNotSymbol(value: unknown): void {\n if (typeof value === \"symbol\") {\n throw invalid(\"UNRECOGNIZED_INSERT_VALUE\", \"A symbol cannot be an attribute value.\");\n }\n}\n\n/**\n * Renders one attribute as markup with a leading space, or `\"\"` when the attribute is absent: the\n * helper the compiler's server output calls for a dynamic attribute. It applies the client's value\n * rules and URL check, so server and client agree on what is written.\n *\n * - `null`, `undefined` and `false` give `\"\"` (except `false` on `aria-*`, `data-*`, `draggable`,\n * `spellcheck` and `contenteditable`, which is written as `\"false\"`); `true` writes `=\"\"` (or\n * `\"true\"` for those same names). Other values are written as `String(value)`, HTML-escaped.\n * - URL attributes whose value would run script are dropped, with the check of\n * `isUnsafeUrlAttribute` (including the SVG animation attributes `values`, `from`, `to`, `by`).\n * - `srcdoc` is an HTML sink, not a URL: its value is a document the frame runs, escaped for the attribute\n * and otherwise written as it is, with the trust of `innerHTML`. Sanitize it and give the frame a `sandbox`\n * attribute. Development builds warn once with `SRCDOC_DYNAMIC`.\n * - `on*` names are dropped: event handlers never reach markup.\n * - Accessors are called (untracked) until a plain value remains.\n *\n * @param name Attribute name, matching `[A-Za-z_:][A-Za-z0-9_:.-]*`. It is validated before\n * anything else, so an invalid name always throws.\n * @param value The value, or an accessor of it.\n * @param isBoolean `true` for a presence attribute such as `disabled`: a truthy value writes the bare\n * name, a falsy one writes nothing. Default: `false`.\n * @returns Markup such as `' title=\"a&amp;b\"'`, or `\"\"`.\n * @throws TypeError `INVALID_ATTRIBUTE_NAME` for an invalid name, and `UNRECOGNIZED_INSERT_VALUE`\n * when the value is a symbol.\n * @example\n * import { ssrAttribute } from \"@shadow-js/web/server\";\n *\n * ssrAttribute(\"title\", \"Tom & Jerry\"); // ' title=\"Tom &amp; Jerry\"'\n * ssrAttribute(\"disabled\", true, true); // \" disabled\"\n * ssrAttribute(\"href\", \"javascript:alert(1)\"); // \"\"\n * @since 1.0.0\n */\nexport function ssrAttribute(name: string, value: unknown, isBoolean = false): string {\n assertAttributeName(name);\n const lowerName = name.toLowerCase();\n if (isEventName(lowerName)) return \"\";\n const resolved = resolveValue(value);\n assertNotSymbol(resolved);\n if (isBoolean) return resolved ? ` ${name}` : \"\";\n const text = attributeText(lowerName, resolved);\n if (text === null) return \"\";\n if (__DEV__ && lowerName === \"srcdoc\") warnOnce(\"SRCDOC_DYNAMIC\", SRCDOC_DYNAMIC_DETAIL);\n if (isUnsafeAttributeValue(lowerName, text)) return \"\";\n return ` ${name}=\"${escapeHtml(text)}\"`;\n}\n"],"mappings":";;;;;AAKA,MAAM,iBAAiB;;AAGvB,MAAM,iCAAsC,IAAI,IAAI;CAClD;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;;;;AAMD,MAAM,6CAAkD,IAAI,IAAI;CAAC;CAAU;CAAQ;CAAM;AAAI,CAAC;;AAG9F,MAAM,wCAA6C,IAAI,IAAI;CACzD;CACA;CACA;AACF,CAAC;;AAGD,MAAM,sBAAyC;CAAC;CAAe;CAAkB;AAAW;;AAG5F,SAAgB,oBAAoB,MAAoB;CACtD,IAAI,CAAC,eAAe,KAAK,IAAI,GAC3B,MAAM,QAAQ,0BAA0B,IAAI,KAAK,iCAAiC;AAEtF;;AAGA,SAAgB,YAAY,WAA4B;CACtD,OAAO,UAAU,SAAS,KAAK,UAAU,WAAW,IAAI;AAC1D;;AAGA,SAAS,eAAe,WAA4B;CAClD,OACE,UAAU,WAAW,OAAO,KAC5B,UAAU,WAAW,OAAO,KAC5B,sBAAsB,IAAI,SAAS;AAEvC;;AAGA,SAAS,cAAc,MAAsB;CAC3C,IAAI,OAAO;CACX,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,OAAO,KAAK,WAAW,CAAC;EAC9B,IAAI,OAAO,OAAS,OAAO,OAAQ,OAAO,MAAO,QAAQ;CAC3D;CACA,OAAO;AACT;;AAGA,SAAgB,YAAY,OAAwB;CAClD,MAAM,aAAa,cAAc,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,YAAY;CAC3D,OAAO,oBAAoB,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAC3E;;AAGA,SAAS,uBAAuB,WAAmB,OAAwB;CACzE,IAAI,eAAe,IAAI,SAAS,GAAG,OAAO,YAAY,KAAK;CAC3D,OAAO,2BAA2B,IAAI,SAAS,KAAK,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,WAAW;AACvF;;AAGA,SAAgB,cAAc,WAAmB,OAA+B;CAC9E,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,IAAI,UAAU,OAAO,OAAO,eAAe,SAAS,IAAI,UAAU;CAClE,IAAI,UAAU,MAAM,OAAO,eAAe,SAAS,IAAI,SAAS;CAChE,OAAO,OAAO,KAAK;AACrB;;AAGA,SAAS,gBAAgB,OAAsB;CAC7C,IAAI,OAAO,UAAU,UACnB,MAAM,QAAQ,6BAA6B,wCAAwC;AAEvF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAgB,aAAa,MAAc,OAAgB,YAAY,OAAe;CACpF,oBAAoB,IAAI;CACxB,MAAM,YAAY,KAAK,YAAY;CACnC,IAAI,YAAY,SAAS,GAAG,OAAO;CACnC,MAAM,WAAW,aAAa,KAAK;CACnC,gBAAgB,QAAQ;CACxB,IAAI,WAAW,OAAO,WAAW,IAAI,SAAS;CAC9C,MAAM,OAAO,cAAc,WAAW,QAAQ;CAC9C,IAAI,SAAS,MAAM,OAAO;CAC1B,IAAe,cAAc,UAAU,SAAS,kBAAkB,qBAAqB;CACvF,IAAI,uBAAuB,WAAW,IAAI,GAAG,OAAO;CACpD,OAAO,IAAI,KAAK,IAAI,WAAW,IAAI,EAAE;AACvC"}
@@ -1,9 +1,9 @@
1
1
  import { nextKey } from "../internal/hydration-scope.js";
2
- import { IGNORED_KEYS, attributeName } from "../internal/react-names.js";
2
+ import { IGNORED_KEYS, attributeName, isFlagAttribute } from "../internal/react-names.js";
3
3
  import { invalid } from "./errors.js";
4
4
  import { SsrNode, isSsrNode } from "./node.js";
5
5
  import { escape, escapeHtml, resolveValue } from "./escape.js";
6
- import { BOOLEAN_ATTRIBUTES, isEventName, ssrAttribute } from "./attributes.js";
6
+ import { isEventName, ssrAttribute } from "./attributes.js";
7
7
  import { classText, ssrStyle, styleText } from "./class-style.js";
8
8
  import { keepLeadingNewline, markSelectedOptions, textareaContent } from "./form-controls.js";
9
9
  import { rawElementContent } from "./raw-text.js";
@@ -156,7 +156,7 @@ function ssrElement(tag, props, children = void 0, needsKey = false, svg = false
156
156
  let style = "";
157
157
  let innerHtml;
158
158
  if (props !== null && typeof props === "object") for (const key of Object.keys(props)) {
159
- if (SKIPPED_KEYS.has(key) || IGNORED_KEYS.has(key)) continue;
159
+ if (SKIPPED_KEYS.has(key) || IGNORED_KEYS.test(key)) continue;
160
160
  if (key === "ref" || key.startsWith("on:") || key.startsWith("use:") || key.startsWith("prop:")) continue;
161
161
  if (isEventName(key)) continue;
162
162
  if (needsKey && key === "data-hk") continue;
@@ -181,8 +181,7 @@ function ssrElement(tag, props, children = void 0, needsKey = false, svg = false
181
181
  if (!Object.hasOwn(props, name)) attributes += ssrAttribute(name, value, name === "checked");
182
182
  } else {
183
183
  const name = attributeName(key, svg);
184
- const flag = BOOLEAN_ATTRIBUTES.has(name) && !(name === "hidden" && value === "until-found");
185
- attributes += ssrAttribute(name, value, flag);
184
+ attributes += ssrAttribute(name, value, isFlagAttribute(name, value));
186
185
  }
187
186
  }
188
187
  if (classes.length > 0) attributes += ` class="${escapeHtml(classes.join(" "))}"`;
@@ -194,7 +193,7 @@ function ssrElement(tag, props, children = void 0, needsKey = false, svg = false
194
193
  if (innerHtml === void 0 && (hasValue || hasFallback)) {
195
194
  const value = hasValue ? control.value : control.fallback;
196
195
  content = isTextarea ? textareaContent(value) : markSelectedOptions(value, content);
197
- } else if (isTextarea && innerHtml === void 0) content = keepLeadingNewline(content);
196
+ } else if ((isTextarea || lowerTag === "pre" || lowerTag === "listing") && innerHtml === void 0) content = keepLeadingNewline(content);
198
197
  return new SsrNode(`<${tag}${attributes}>${content}</${tag}>`);
199
198
  }
200
199
  /** The `children` of a spread props object (read once, so a getter runs once), or undefined. */
@@ -1 +1 @@
1
- {"version":3,"file":"element.js","names":[],"sources":["../../../src/server/element.ts"],"sourcesContent":["import { nextKey } from \"../internal/hydration-scope\";\nimport { attributeName, IGNORED_KEYS } from \"../internal/react-names\";\nimport { BOOLEAN_ATTRIBUTES, isEventName, ssrAttribute } from \"./attributes\";\nimport { classText, ssrStyle, styleText } from \"./class-style\";\nimport { invalid } from \"./errors\";\n// biome-ignore lint/suspicious/noShadowRestrictedNames: the name \"escape\" is part of the compiler contract (SSR output).\nimport { escape, escapeHtml, resolveValue } from \"./escape\";\nimport { keepLeadingNewline, markSelectedOptions, textareaContent } from \"./form-controls\";\nimport { isSsrNode, SsrNode } from \"./node\";\nimport { rawElementContent } from \"./raw-text\";\n\n/** A tag name: letters, digits and dashes, starting with a letter. Anything else could break out. */\nconst TAG_NAME = /^[A-Za-z][A-Za-z0-9-]*$/;\n\n/** Elements that never have children or a closing tag. */\nconst VOID_ELEMENTS: ReadonlySet<string> = new Set([\n \"area\",\n \"base\",\n \"br\",\n \"col\",\n \"command\",\n \"embed\",\n \"hr\",\n \"img\",\n \"input\",\n \"keygen\",\n \"link\",\n \"meta\",\n \"param\",\n \"source\",\n \"track\",\n \"wbr\",\n]);\n\n/** Keys that never become attributes: they are handled elsewhere or would reach a prototype. */\nconst SKIPPED_KEYS: ReadonlySet<string> = new Set([\n \"__proto__\",\n \"constructor\",\n \"prototype\",\n \"children\",\n \"ref\",\n]);\n\n/** The HTML of a `dangerouslySetInnerHTML` value (`{ __html }`): null and undefined are empty. */\nfunction innerHtmlOfValue(value: unknown): unknown {\n if (value === null || typeof value !== \"object\") return undefined;\n return Reflect.get(value, \"__html\");\n}\n\n/**\n * Joins the static parts of a server template with its dynamic holes into trusted markup: the\n * function compiled server output calls for each element tree. Holes are inserted unchanged, so each\n * must already be escaped (the output of `escape`, `ssrAttribute`, `ssrClass`, ...) or an `SsrNode`;\n * any other value throws, so a raw user value cannot reach the markup.\n *\n * @param parts The static markup, one more entry than there are holes.\n * @param holes The dynamic parts: strings produced by the escaping helpers, or `SsrNode` values.\n * @returns The markup as a trusted `SsrNode`, which `escape` writes without encoding.\n * @throws TypeError when `parts` is not an array of `holes.length + 1` entries, or when a hole is not\n * a string or an `SsrNode`.\n * @example\n * import { escape, renderToString, ssr } from \"@shadow-js/web/server\";\n *\n * const name = \"<Ada>\";\n * const html = renderToString(() => ssr([\"<p>Hello, \", \"</p>\"], escape(name)));\n * // \"<p>Hello, &lt;Ada&gt;</p>\"\n * @see ssrRaw\n * @since 1.0.0\n */\nexport function ssr(parts: readonly string[], ...holes: unknown[]): SsrNode {\n if (!Array.isArray(parts) || parts.length !== holes.length + 1) {\n throw new TypeError(\"ssr(): parts must have one more entry than there are holes.\");\n }\n let html = parts[0] ?? \"\";\n for (let index = 0; index < holes.length; index += 1) {\n const hole = holes[index];\n if (typeof hole === \"string\") html += hole;\n else if (isSsrNode(hole)) html += hole.t;\n else throw new TypeError(\"ssr(): a hole must be an escaped string or an SsrNode.\");\n html += parts[index + 1] ?? \"\";\n }\n return new SsrNode(html);\n}\n\n/**\n * Marks a string as trusted markup that `escape` and `ssr` insert unchanged. Only use it for markup\n * the application wrote itself, never for user input.\n *\n * @param html The markup, inserted unchanged.\n * @returns The markup as a trusted `SsrNode`.\n * @throws TypeError when `html` is not a string.\n * @example\n * import { renderToString, ssrRaw } from \"@shadow-js/web/server\";\n *\n * const html = renderToString(() => [\"<b>escaped</b>\", ssrRaw(\"<!-- build 42 -->\")]);\n * // \"&lt;b&gt;escaped&lt;/b&gt;<!-- build 42 -->\"\n * @see ssr\n * @since 1.0.0\n */\nexport function ssrRaw(html: string): SsrNode {\n if (typeof html !== \"string\") throw new TypeError(\"ssrRaw(): the markup must be a string.\");\n return new SsrNode(html);\n}\n\n/**\n * Draws the next hydration key of the current scope and returns it as a `data-hk` attribute, such as\n * `' data-hk=\"0-0\"'`: compiled server output calls it for the root element of each template, and\n * the client's `hydrate()` claims the element by that key. Calls must happen in the same order as on\n * the client, so call it only where the compiler would.\n *\n * @returns Markup for the `data-hk` attribute, or `\"\"` outside a server render (no key is drawn).\n * @example\n * import { renderToString, ssr, ssrHydrationKey } from \"@shadow-js/web/server\";\n *\n * const html = renderToString(() => ssr([\"<div\", \">x</div>\"], ssrHydrationKey()));\n * // '<div data-hk=\"0-0\">x</div>'\n * @since 1.0.0\n */\nexport function ssrHydrationKey(): string {\n const key = nextKey();\n return key === undefined ? \"\" : ` data-hk=\"${escapeHtml(key)}\"`;\n}\n\n/**\n * Renders an element from a props object: what compiled server output calls for an element with a\n * spread (`<div {...props} />`), and the server half of `Dynamic` and `element`. It writes the same\n * markup the client's `spread` produces.\n *\n * `class`, `className`, `classList` and `style` are combined into one `class` and one `style`\n * attribute; other keys are attributes, with React names mapped (`htmlFor` is `for`) and HTML boolean\n * attributes (`disabled`, `checked`, ...) written as bare names. `ref`, `children`, `on*` keys and the\n * `on:`, `use:` and `prop:` prefixes are skipped; `attr:` and `bool:` select the attribute and presence\n * forms. `innerHTML` and `dangerouslySetInnerHTML` are written unescaped as the content. On a\n * `textarea` the `value` (or `defaultValue`) is the content; on a `select` it marks the matching\n * `<option selected>` (an array for `multiple`); on other elements `defaultValue` and `defaultChecked`\n * are the `value` and `checked` attributes. Void elements (`img`, `input`, ...) get no content.\n *\n * @param tag Element name: letters, digits and dashes, starting with a letter.\n * @param props The props object (each getter is read once), or `null`.\n * @param children The content, escaped unless it is an `SsrNode`. Undefined (the default) uses the\n * `children` prop.\n * @param needsKey `true` writes the hydration key of this template root first. Default: `false`.\n * @param svg `true` for an SVG element: SVG presentation names are hyphenated (`strokeWidth` is\n * `stroke-width`), other names keep their case. Default: `false`.\n * @returns The element as a trusted `SsrNode`.\n * @throws TypeError `INVALID_TAG_NAME` for an invalid tag, `INVALID_ATTRIBUTE_NAME` for an invalid\n * attribute name, and the errors of {@link ssrAttribute}, `ssrClass` and `ssrStyle` for invalid values.\n * @example\n * import { renderToString, ssrElement } from \"@shadow-js/web/server\";\n *\n * const props = { id: \"save\", disabled: true, class: [\"btn\", { primary: true }], children: \"Save\" };\n * const html = renderToString(() => ssrElement(\"button\", props));\n * // '<button id=\"save\" disabled class=\"btn primary\">Save</button>'\n * @since 1.0.0\n */\nexport function ssrElement(\n tag: string,\n props: unknown,\n children: unknown = undefined,\n needsKey = false,\n svg = false,\n): SsrNode {\n if (typeof tag !== \"string\" || !TAG_NAME.test(tag)) {\n throw invalid(\"INVALID_TAG_NAME\", `\"${String(tag)}\" is not a valid tag name.`);\n }\n const lowerTag = tag.toLowerCase();\n // A browser ignores `value` on these: a textarea's value is its content, a select's value is the\n // `selected` flag of its options. The props are read once and applied to the content below.\n const isSelect = lowerTag === \"select\";\n const isTextarea = lowerTag === \"textarea\";\n const control: { value?: unknown; fallback?: unknown } = {};\n let hasValue = false;\n let hasFallback = false;\n let attributes = needsKey ? ssrHydrationKey() : \"\";\n const classes: string[] = [];\n let style = \"\";\n let innerHtml: string | undefined;\n if (props !== null && typeof props === \"object\") {\n for (const key of Object.keys(props)) {\n if (SKIPPED_KEYS.has(key) || IGNORED_KEYS.has(key)) continue;\n if (\n key === \"ref\" ||\n key.startsWith(\"on:\") ||\n key.startsWith(\"use:\") ||\n key.startsWith(\"prop:\")\n )\n continue;\n if (isEventName(key)) continue;\n if (needsKey && key === \"data-hk\") continue;\n const value = resolveValue(Reflect.get(props, key));\n if (key === \"class\" || key === \"className\") {\n const text = classText(value);\n if (text !== \"\") classes.push(text);\n } else if (key === \"classList\") {\n collectTruthyKeys(value, classes);\n } else if (key === \"style\") {\n style = styleText(value);\n } else if (key === \"innerHTML\") {\n innerHtml = plainHtml(value);\n } else if (key === \"dangerouslySetInnerHTML\") {\n innerHtml = plainHtml(resolveValue(innerHtmlOfValue(value)));\n } else if ((isSelect || isTextarea) && key === \"value\") {\n control.value = value;\n hasValue = true;\n } else if ((isSelect || isTextarea) && key === \"defaultValue\") {\n control.fallback = value;\n hasFallback = true;\n } else if (key.startsWith(\"attr:\")) {\n attributes += ssrAttribute(key.slice(5), value);\n } else if (key.startsWith(\"bool:\")) {\n attributes += ssrAttribute(key.slice(5), value, true);\n } else if (key === \"defaultValue\" || key === \"defaultChecked\") {\n // The attributes of the same name in HTML are the defaults: `value` and `checked`.\n const name = key === \"defaultValue\" ? \"value\" : \"checked\";\n if (!Object.hasOwn(props, name))\n attributes += ssrAttribute(name, value, name === \"checked\");\n } else {\n const name = attributeName(key, svg);\n // `hidden=\"until-found\"` is a keyword, not a flag: a bare `hidden` would hide what it should only collapse.\n const flag =\n BOOLEAN_ATTRIBUTES.has(name) && !(name === \"hidden\" && value === \"until-found\");\n attributes += ssrAttribute(name, value, flag);\n }\n }\n }\n if (classes.length > 0) attributes += ` class=\"${escapeHtml(classes.join(\" \"))}\"`;\n if (style !== \"\") attributes += ssrStyle(style);\n if (VOID_ELEMENTS.has(lowerTag)) return new SsrNode(`<${tag}${attributes}>`);\n // Like the DOM `spread`: a `children` prop supplies the content unless the element wrote its own.\n // The text of a script or a style is raw (no entities), so it is written as it is and checked instead.\n const raw = !svg && (lowerTag === \"script\" || lowerTag === \"style\");\n const given = children === undefined ? propsChildren(props) : children;\n let content =\n innerHtml !== undefined ? innerHtml : raw ? rawElementContent(lowerTag, given) : escape(given);\n if (innerHtml === undefined && (hasValue || hasFallback)) {\n const value = hasValue ? control.value : control.fallback;\n content = isTextarea ? textareaContent(value) : markSelectedOptions(value, content);\n } else if (isTextarea && innerHtml === undefined) {\n // The children of a textarea are its value (as on the client): keep a first line break the parser drops.\n content = keepLeadingNewline(content);\n }\n return new SsrNode(`<${tag}${attributes}>${content}</${tag}>`);\n}\n\n/** The `children` of a spread props object (read once, so a getter runs once), or undefined. */\nfunction propsChildren(props: unknown): unknown {\n if (props === null || typeof props !== \"object\") return undefined;\n return Reflect.get(props, \"children\");\n}\n\n/** Class names whose key is truthy, from a `classList` object. */\nfunction collectTruthyKeys(value: unknown, out: string[]): void {\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) return;\n for (const key of Object.keys(value)) {\n if (key !== \"\" && resolveValue(Reflect.get(value, key))) out.push(key);\n }\n}\n\n/** Raw HTML text for `innerHTML`: null and undefined become \"\", an SsrNode its markup. */\nfunction plainHtml(value: unknown): string {\n if (value === null || value === undefined) return \"\";\n if (isSsrNode(value)) return value.t;\n if (typeof value === \"symbol\")\n throw invalid(\"UNRECOGNIZED_INSERT_VALUE\", \"A symbol cannot be innerHTML.\");\n return String(value);\n}\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,WAAW;;AAGjB,MAAM,gCAAqC,IAAI,IAAI;CACjD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;AAGD,MAAM,+BAAoC,IAAI,IAAI;CAChD;CACA;CACA;CACA;CACA;AACF,CAAC;;AAGD,SAAS,iBAAiB,OAAyB;CACjD,IAAI,UAAU,QAAQ,OAAO,UAAU,UAAU,OAAO,KAAA;CACxD,OAAO,QAAQ,IAAI,OAAO,QAAQ;AACpC;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,IAAI,OAA0B,GAAG,OAA2B;CAC1E,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,MAAM,WAAW,MAAM,SAAS,GAC3D,MAAM,IAAI,UAAU,6DAA6D;CAEnF,IAAI,OAAO,MAAM,MAAM;CACvB,KAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS,GAAG;EACpD,MAAM,OAAO,MAAM;EACnB,IAAI,OAAO,SAAS,UAAU,QAAQ;OACjC,IAAI,UAAU,IAAI,GAAG,QAAQ,KAAK;OAClC,MAAM,IAAI,UAAU,wDAAwD;EACjF,QAAQ,MAAM,QAAQ,MAAM;CAC9B;CACA,OAAO,IAAI,QAA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AAU,KAAK,IAAI,OAAO,KAAK;CAC/F,IAAI,cAAc,KAAA,MAAc,YAAY,cAAc;EACxD,MAAM,QAAQ,WAAW,QAAQ,QAAQ,QAAQ;EACjD,UAAU,aAAa,gBAAgB,KAAK,IAAI,oBAAoB,OAAO,OAAO;CACpF,OAAO,IAAI,cAAc,cAAc,KAAA,GAErC,UAAU,mBAAmB,OAAO;CAEtC,OAAO,IAAI,QAAQ,IAAI,MAAM,WAAW,GAAG,QAAQ,IAAI,IAAI,EAAE;AAC/D;;AAGA,SAAS,cAAc,OAAyB;CAC9C,IAAI,UAAU,QAAQ,OAAO,UAAU,UAAU,OAAO,KAAA;CACxD,OAAO,QAAQ,IAAI,OAAO,UAAU;AACtC;;AAGA,SAAS,kBAAkB,OAAgB,KAAqB;CAC9D,IAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;CACzE,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GACjC,IAAI,QAAQ,MAAM,aAAa,QAAQ,IAAI,OAAO,GAAG,CAAC,GAAG,IAAI,KAAK,GAAG;AAEzE;;AAGA,SAAS,UAAU,OAAwB;CACzC,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,IAAI,UAAU,KAAK,GAAG,OAAO,MAAM;CACnC,IAAI,OAAO,UAAU,UACnB,MAAM,QAAQ,6BAA6B,+BAA+B;CAC5E,OAAO,OAAO,KAAK;AACrB"}
1
+ {"version":3,"file":"element.js","names":[],"sources":["../../../src/server/element.ts"],"sourcesContent":["import { nextKey } from \"../internal/hydration-scope\";\nimport { attributeName, IGNORED_KEYS, isFlagAttribute } from \"../internal/react-names\";\nimport { isEventName, ssrAttribute } from \"./attributes\";\nimport { classText, ssrStyle, styleText } from \"./class-style\";\nimport { invalid } from \"./errors\";\n// biome-ignore lint/suspicious/noShadowRestrictedNames: the name \"escape\" is part of the compiler contract (SSR output).\nimport { escape, escapeHtml, resolveValue } from \"./escape\";\nimport { keepLeadingNewline, markSelectedOptions, textareaContent } from \"./form-controls\";\nimport { isSsrNode, SsrNode } from \"./node\";\nimport { rawElementContent } from \"./raw-text\";\n\n/** A tag name: letters, digits and dashes, starting with a letter. Anything else could break out. */\nconst TAG_NAME = /^[A-Za-z][A-Za-z0-9-]*$/;\n\n/** Elements that never have children or a closing tag. */\nconst VOID_ELEMENTS: ReadonlySet<string> = new Set([\n \"area\",\n \"base\",\n \"br\",\n \"col\",\n \"command\",\n \"embed\",\n \"hr\",\n \"img\",\n \"input\",\n \"keygen\",\n \"link\",\n \"meta\",\n \"param\",\n \"source\",\n \"track\",\n \"wbr\",\n]);\n\n/** Keys that never become attributes: they are handled elsewhere or would reach a prototype. */\nconst SKIPPED_KEYS: ReadonlySet<string> = new Set([\n \"__proto__\",\n \"constructor\",\n \"prototype\",\n \"children\",\n \"ref\",\n]);\n\n/** The HTML of a `dangerouslySetInnerHTML` value (`{ __html }`): null and undefined are empty. */\nfunction innerHtmlOfValue(value: unknown): unknown {\n if (value === null || typeof value !== \"object\") return undefined;\n return Reflect.get(value, \"__html\");\n}\n\n/**\n * Joins the static parts of a server template with its dynamic holes into trusted markup: the\n * function compiled server output calls for each element tree. Holes are inserted unchanged, so each\n * must already be escaped (the output of `escape`, `ssrAttribute`, `ssrClass`, ...) or an `SsrNode`;\n * any other value throws, so a raw user value cannot reach the markup.\n *\n * @param parts The static markup, one more entry than there are holes.\n * @param holes The dynamic parts: strings produced by the escaping helpers, or `SsrNode` values.\n * @returns The markup as a trusted `SsrNode`, which `escape` writes without encoding.\n * @throws TypeError when `parts` is not an array of `holes.length + 1` entries, or when a hole is not\n * a string or an `SsrNode`.\n * @example\n * import { escape, renderToString, ssr } from \"@shadow-js/web/server\";\n *\n * const name = \"<Ada>\";\n * const html = renderToString(() => ssr([\"<p>Hello, \", \"</p>\"], escape(name)));\n * // \"<p>Hello, &lt;Ada&gt;</p>\"\n * @see ssrRaw\n * @since 1.0.0\n */\nexport function ssr(parts: readonly string[], ...holes: unknown[]): SsrNode {\n if (!Array.isArray(parts) || parts.length !== holes.length + 1) {\n throw new TypeError(\"ssr(): parts must have one more entry than there are holes.\");\n }\n let html = parts[0] ?? \"\";\n for (let index = 0; index < holes.length; index += 1) {\n const hole = holes[index];\n if (typeof hole === \"string\") html += hole;\n else if (isSsrNode(hole)) html += hole.t;\n else throw new TypeError(\"ssr(): a hole must be an escaped string or an SsrNode.\");\n html += parts[index + 1] ?? \"\";\n }\n return new SsrNode(html);\n}\n\n/**\n * Marks a string as trusted markup that `escape` and `ssr` insert unchanged. Only use it for markup\n * the application wrote itself, never for user input.\n *\n * @param html The markup, inserted unchanged.\n * @returns The markup as a trusted `SsrNode`.\n * @throws TypeError when `html` is not a string.\n * @example\n * import { renderToString, ssrRaw } from \"@shadow-js/web/server\";\n *\n * const html = renderToString(() => [\"<b>escaped</b>\", ssrRaw(\"<!-- build 42 -->\")]);\n * // \"&lt;b&gt;escaped&lt;/b&gt;<!-- build 42 -->\"\n * @see ssr\n * @since 1.0.0\n */\nexport function ssrRaw(html: string): SsrNode {\n if (typeof html !== \"string\") throw new TypeError(\"ssrRaw(): the markup must be a string.\");\n return new SsrNode(html);\n}\n\n/**\n * Draws the next hydration key of the current scope and returns it as a `data-hk` attribute, such as\n * `' data-hk=\"0-0\"'`: compiled server output calls it for the root element of each template, and\n * the client's `hydrate()` claims the element by that key. Calls must happen in the same order as on\n * the client, so call it only where the compiler would.\n *\n * @returns Markup for the `data-hk` attribute, or `\"\"` outside a server render (no key is drawn).\n * @example\n * import { renderToString, ssr, ssrHydrationKey } from \"@shadow-js/web/server\";\n *\n * const html = renderToString(() => ssr([\"<div\", \">x</div>\"], ssrHydrationKey()));\n * // '<div data-hk=\"0-0\">x</div>'\n * @since 1.0.0\n */\nexport function ssrHydrationKey(): string {\n const key = nextKey();\n return key === undefined ? \"\" : ` data-hk=\"${escapeHtml(key)}\"`;\n}\n\n/**\n * Renders an element from a props object: what compiled server output calls for an element with a\n * spread (`<div {...props} />`), and the server half of `Dynamic` and `element`. It writes the same\n * markup the client's `spread` produces.\n *\n * `class`, `className`, `classList` and `style` are combined into one `class` and one `style`\n * attribute; other keys are attributes, with React names mapped (`htmlFor` is `for`) and HTML boolean\n * attributes (`disabled`, `checked`, ...) written as bare names. `ref`, `children`, `on*` keys and the\n * `on:`, `use:` and `prop:` prefixes are skipped; `attr:` and `bool:` select the attribute and presence\n * forms. `innerHTML` and `dangerouslySetInnerHTML` are written unescaped as the content. On a\n * `textarea` the `value` (or `defaultValue`) is the content; on a `select` it marks the matching\n * `<option selected>` (an array for `multiple`); on other elements `defaultValue` and `defaultChecked`\n * are the `value` and `checked` attributes. Void elements (`img`, `input`, ...) get no content.\n *\n * @param tag Element name: letters, digits and dashes, starting with a letter.\n * @param props The props object (each getter is read once), or `null`.\n * @param children The content, escaped unless it is an `SsrNode`. Undefined (the default) uses the\n * `children` prop.\n * @param needsKey `true` writes the hydration key of this template root first. Default: `false`.\n * @param svg `true` for an SVG element: SVG presentation names are hyphenated (`strokeWidth` is\n * `stroke-width`), other names keep their case. Default: `false`.\n * @returns The element as a trusted `SsrNode`.\n * @throws TypeError `INVALID_TAG_NAME` for an invalid tag, `INVALID_ATTRIBUTE_NAME` for an invalid\n * attribute name, and the errors of {@link ssrAttribute}, `ssrClass` and `ssrStyle` for invalid values.\n * @example\n * import { renderToString, ssrElement } from \"@shadow-js/web/server\";\n *\n * const props = { id: \"save\", disabled: true, class: [\"btn\", { primary: true }], children: \"Save\" };\n * const html = renderToString(() => ssrElement(\"button\", props));\n * // '<button id=\"save\" disabled class=\"btn primary\">Save</button>'\n * @since 1.0.0\n */\nexport function ssrElement(\n tag: string,\n props: unknown,\n children: unknown = undefined,\n needsKey = false,\n svg = false,\n): SsrNode {\n if (typeof tag !== \"string\" || !TAG_NAME.test(tag)) {\n throw invalid(\"INVALID_TAG_NAME\", `\"${String(tag)}\" is not a valid tag name.`);\n }\n const lowerTag = tag.toLowerCase();\n // A browser ignores `value` on these: a textarea's value is its content, a select's value is the\n // `selected` flag of its options. The props are read once and applied to the content below.\n const isSelect = lowerTag === \"select\";\n const isTextarea = lowerTag === \"textarea\";\n const control: { value?: unknown; fallback?: unknown } = {};\n let hasValue = false;\n let hasFallback = false;\n let attributes = needsKey ? ssrHydrationKey() : \"\";\n const classes: string[] = [];\n let style = \"\";\n let innerHtml: string | undefined;\n if (props !== null && typeof props === \"object\") {\n for (const key of Object.keys(props)) {\n if (SKIPPED_KEYS.has(key) || IGNORED_KEYS.test(key)) continue;\n if (\n key === \"ref\" ||\n key.startsWith(\"on:\") ||\n key.startsWith(\"use:\") ||\n key.startsWith(\"prop:\")\n )\n continue;\n if (isEventName(key)) continue;\n if (needsKey && key === \"data-hk\") continue;\n const value = resolveValue(Reflect.get(props, key));\n if (key === \"class\" || key === \"className\") {\n const text = classText(value);\n if (text !== \"\") classes.push(text);\n } else if (key === \"classList\") {\n collectTruthyKeys(value, classes);\n } else if (key === \"style\") {\n style = styleText(value);\n } else if (key === \"innerHTML\") {\n innerHtml = plainHtml(value);\n } else if (key === \"dangerouslySetInnerHTML\") {\n innerHtml = plainHtml(resolveValue(innerHtmlOfValue(value)));\n } else if ((isSelect || isTextarea) && key === \"value\") {\n control.value = value;\n hasValue = true;\n } else if ((isSelect || isTextarea) && key === \"defaultValue\") {\n control.fallback = value;\n hasFallback = true;\n } else if (key.startsWith(\"attr:\")) {\n attributes += ssrAttribute(key.slice(5), value);\n } else if (key.startsWith(\"bool:\")) {\n attributes += ssrAttribute(key.slice(5), value, true);\n } else if (key === \"defaultValue\" || key === \"defaultChecked\") {\n // The attributes of the same name in HTML are the defaults: `value` and `checked`.\n const name = key === \"defaultValue\" ? \"value\" : \"checked\";\n if (!Object.hasOwn(props, name))\n attributes += ssrAttribute(name, value, name === \"checked\");\n } else {\n const name = attributeName(key, svg);\n attributes += ssrAttribute(name, value, isFlagAttribute(name, value));\n }\n }\n }\n if (classes.length > 0) attributes += ` class=\"${escapeHtml(classes.join(\" \"))}\"`;\n if (style !== \"\") attributes += ssrStyle(style);\n if (VOID_ELEMENTS.has(lowerTag)) return new SsrNode(`<${tag}${attributes}>`);\n // Like the DOM `spread`: a `children` prop supplies the content unless the element wrote its own.\n // The text of a script or a style is raw (no entities), so it is written as it is and checked instead.\n const raw = !svg && (lowerTag === \"script\" || lowerTag === \"style\");\n const given = children === undefined ? propsChildren(props) : children;\n let content =\n innerHtml !== undefined ? innerHtml : raw ? rawElementContent(lowerTag, given) : escape(given);\n if (innerHtml === undefined && (hasValue || hasFallback)) {\n const value = hasValue ? control.value : control.fallback;\n content = isTextarea ? textareaContent(value) : markSelectedOptions(value, content);\n } else if (\n (isTextarea || lowerTag === \"pre\" || lowerTag === \"listing\") &&\n innerHtml === undefined\n ) {\n // The parser drops one line break right after these start tags (a textarea's children are its\n // value, as on the client): write one more so the content is read as it was given.\n content = keepLeadingNewline(content);\n }\n return new SsrNode(`<${tag}${attributes}>${content}</${tag}>`);\n}\n\n/** The `children` of a spread props object (read once, so a getter runs once), or undefined. */\nfunction propsChildren(props: unknown): unknown {\n if (props === null || typeof props !== \"object\") return undefined;\n return Reflect.get(props, \"children\");\n}\n\n/** Class names whose key is truthy, from a `classList` object. */\nfunction collectTruthyKeys(value: unknown, out: string[]): void {\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) return;\n for (const key of Object.keys(value)) {\n if (key !== \"\" && resolveValue(Reflect.get(value, key))) out.push(key);\n }\n}\n\n/** Raw HTML text for `innerHTML`: null and undefined become \"\", an SsrNode its markup. */\nfunction plainHtml(value: unknown): string {\n if (value === null || value === undefined) return \"\";\n if (isSsrNode(value)) return value.t;\n if (typeof value === \"symbol\")\n throw invalid(\"UNRECOGNIZED_INSERT_VALUE\", \"A symbol cannot be innerHTML.\");\n return String(value);\n}\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,WAAW;;AAGjB,MAAM,gCAAqC,IAAI,IAAI;CACjD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;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AAO,KAAK,KAAK,GACjC,IAAI,QAAQ,MAAM,aAAa,QAAQ,IAAI,OAAO,GAAG,CAAC,GAAG,IAAI,KAAK,GAAG;AAEzE;;AAGA,SAAS,UAAU,OAAwB;CACzC,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,IAAI,UAAU,KAAK,GAAG,OAAO,MAAM;CACnC,IAAI,OAAO,UAAU,UACnB,MAAM,QAAQ,6BAA6B,+BAA+B;CAC5E,OAAO,OAAO,KAAK;AACrB"}
@@ -4,12 +4,12 @@ import { ssrClass, ssrStyle } from "./class-style.js";
4
4
  import { ssrSelectValue, ssrTextArea } from "./form-controls.js";
5
5
  import { ssrRawText } from "./raw-text.js";
6
6
  import { ssr, ssrElement, ssrHydrationKey, ssrRaw } from "./element.js";
7
- import { createServerPortals } from "./portals.js";
8
7
  import { normalizeTags } from "../head/validate.js";
9
8
  import { createServerHead } from "../head/server-head.js";
10
9
  import { createComponent, memo } from "./component.js";
11
10
  import { derived } from "./derived.js";
12
11
  import { HydrationScript, generateHydrationScript } from "./hydration-script.js";
12
+ import { createServerPortals } from "./portals.js";
13
13
  import { renderToString, renderToStringAsync } from "./render.js";
14
14
  import { deserialize, serialize } from "./serialize.js";
15
15
  import { renderToStream } from "./stream.js";
@@ -13,6 +13,9 @@ var PortalCollector = class {
13
13
  clear() {
14
14
  this.entries.clear();
15
15
  }
16
+ render(key, children) {
17
+ renderServerPortal(key, children, this);
18
+ }
16
19
  html() {
17
20
  let out = "";
18
21
  for (const [key, html] of this.entries) out += `<!--portal:${key}-->${html}<!--/portal:${key}-->`;
@@ -1 +1 @@
1
- {"version":3,"file":"portals.js","names":[],"sources":["../../../src/server/portals.ts"],"sourcesContent":["import { childScope, withScope } from \"../internal/hydration-scope\";\nimport { PortalContext, type PortalSink } from \"../internal/portal-context\";\nimport { SsrError } from \"./errors\";\n// biome-ignore lint/suspicious/noShadowRestrictedNames: the name \"escape\" is part of the compiler contract (SSR output).\nimport { escape } from \"./escape\";\n\n/**\n * The most portals one render may produce. A page with more open overlays than this is a bug, and the\n * collector refuses to grow without a bound.\n * @internal\n */\nexport const MAX_SERVER_PORTALS = 256;\n\n/**\n * The markup of the portals a server render produced (see {@link createServerPortals}). Pass it to\n * the render as its `portals` option, then emit `html()` at the end of `<body>`, after the app markup.\n * The client adopts each block when it hydrates the portal that wrote it.\n *\n * @example\n * import { createComponent, createServerPortals, renderToString } from \"@shadow-js/web/server\";\n *\n * function App() {\n * return <main>Hello</main>;\n * }\n * const portals = createServerPortals();\n * const app = renderToString(() => createComponent(App, {}), { portals });\n * const page = `<!doctype html><html><body><div id=\"app\">${app}</div>${portals.html()}</body></html>`;\n * @since 1.0.0\n */\nexport interface ServerPortals {\n /**\n * The collected blocks, in the order the render produced them: each is `<!--portal:KEY-->` markup\n * `<!--/portal:KEY-->`. Emit it once, at the end of `<body>`.\n * @returns The markup; empty when no portal was rendered.\n */\n html(): string;\n}\n\n/** The collector behind a {@link ServerPortals}: what a renderer writes into while it renders. @internal */\nexport class PortalCollector implements PortalSink {\n private readonly entries = new Map<string, string>();\n\n add(key: string, html: string): void {\n if (!this.entries.has(key) && this.entries.size >= MAX_SERVER_PORTALS) {\n throw new SsrError(\n \"SSR_PORTAL_LIMIT\",\n `A render produced more than ${MAX_SERVER_PORTALS} portals; close some overlays or raise the limit.`,\n );\n }\n this.entries.set(key, html);\n }\n\n /** Forgets what an earlier pass of the same render wrote. */\n clear(): void {\n this.entries.clear();\n }\n\n html(): string {\n let out = \"\";\n for (const [key, html] of this.entries) {\n out += `<!--portal:${key}-->${html}<!--/portal:${key}-->`;\n }\n return out;\n }\n}\n\nconst collectors = new WeakMap<ServerPortals, PortalCollector>();\n\n/**\n * Creates the collector a server render writes its portals into. Without one, a `Portal` renders\n * nothing on the server. Create one per request: a render clears it before it starts, and one render\n * may produce at most 256 portals (more throw an `SsrError` whose `code` is `SSR_PORTAL_LIMIT`).\n *\n * @returns A new, empty {@link ServerPortals}.\n * @example\n * import { createComponent, createServerPortals, renderToString } from \"@shadow-js/web/server\";\n *\n * function App() {\n * return <main>Hello</main>;\n * }\n * const portals = createServerPortals();\n * const app = renderToString(() => createComponent(App, {}), { portals });\n * const page = `<body><div id=\"app\">${app}</div>${portals.html()}</body>`;\n * @since 1.0.0\n */\nexport function createServerPortals(): ServerPortals {\n const collector = new PortalCollector();\n const portals: ServerPortals = { html: () => collector.html() };\n collectors.set(portals, collector);\n return portals;\n}\n\n/**\n * The collector behind a `portals` option.\n * @throws TypeError when the value did not come from {@link createServerPortals}.\n * @internal\n */\nexport function collectorOf(portals: ServerPortals): PortalCollector {\n const collector = collectors.get(portals);\n if (collector === undefined) {\n throw new TypeError(\"portals must be created with createServerPortals().\");\n }\n return collector;\n}\n\n/**\n * The server half of a `Portal`: renders its content into the render's collector. The content gets its\n * own hydration scope, so the keys of the siblings do not depend on whether the portal rendered. Writes\n * nothing when the render has no collector or the portal has no key (no render scope).\n *\n * @param key The portal's key, from `nextKey()` at the call site.\n * @param children Reads the portal's content. A thunk, because reading a JSX child builds it, and the\n * content must be built inside its own scope.\n * @param sink The collector the portal context provides (null for a render without one).\n * @internal\n */\nexport function renderServerPortal(\n key: string | undefined,\n children: () => unknown,\n sink: PortalSink | null,\n): void {\n if (sink === null || key === undefined) return;\n const html = withScope(childScope(key), () => escape(children()));\n sink.add(key, html);\n}\n\n/**\n * Runs `build` with the portal sink provided, and returns what `build` returns (the render's accessor).\n * A null sink provides nothing, so the portals of that render write nothing.\n * @internal\n */\nexport function withPortalSink<T>(sink: PortalSink | null, build: () => T): T {\n const view = PortalContext({\n value: sink,\n get children() {\n return build();\n },\n });\n return (view as () => T)();\n}\n\n/**\n * The collector a renderer writes into for one call: null without a `portals` option, else the cleared\n * collector.\n * @internal\n */\nexport function portalCollectorFor(portals: ServerPortals | undefined): PortalCollector | null {\n if (portals === undefined) return null;\n const collector = collectorOf(portals);\n collector.clear();\n return collector;\n}\n"],"mappings":";;;;;AAuCA,IAAa,kBAAb,MAAmD;C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":[],"sources":["../../../src/server/portals.ts"],"sourcesContent":["import { childScope, withScope } from \"../internal/hydration-scope\";\nimport { PortalContext, type PortalSink } from \"../internal/portal-context\";\nimport { SsrError } from \"./errors\";\n// biome-ignore lint/suspicious/noShadowRestrictedNames: the name \"escape\" is part of the compiler contract (SSR output).\nimport { escape } from \"./escape\";\n\n/**\n * The most portals one render may produce. A page with more open overlays than this is a bug, and the\n * collector refuses to grow without a bound.\n * @internal\n */\nexport const MAX_SERVER_PORTALS = 256;\n\n/**\n * The markup of the portals a server render produced (see {@link createServerPortals}). Pass it to\n * the render as its `portals` option, then emit `html()` at the end of `<body>`, after the app markup.\n * The client adopts each block when it hydrates the portal that wrote it.\n *\n * @example\n * import { createComponent, createServerPortals, renderToString } from \"@shadow-js/web/server\";\n *\n * function App() {\n * return <main>Hello</main>;\n * }\n * const portals = createServerPortals();\n * const app = renderToString(() => createComponent(App, {}), { portals });\n * const page = `<!doctype html><html><body><div id=\"app\">${app}</div>${portals.html()}</body></html>`;\n * @since 1.0.0\n */\nexport interface ServerPortals {\n /**\n * The collected blocks, in the order the render produced them: each is `<!--portal:KEY-->` markup\n * `<!--/portal:KEY-->`. Emit it once, at the end of `<body>`.\n * @returns The markup; empty when no portal was rendered.\n */\n html(): string;\n}\n\n/** The collector behind a {@link ServerPortals}: what a renderer writes into while it renders. @internal */\nexport class PortalCollector implements PortalSink {\n private readonly entries = new Map<string, string>();\n\n add(key: string, html: string): void {\n if (!this.entries.has(key) && this.entries.size >= MAX_SERVER_PORTALS) {\n throw new SsrError(\n \"SSR_PORTAL_LIMIT\",\n `A render produced more than ${MAX_SERVER_PORTALS} portals; close some overlays or raise the limit.`,\n );\n }\n this.entries.set(key, html);\n }\n\n /** Forgets what an earlier pass of the same render wrote. */\n clear(): void {\n this.entries.clear();\n }\n\n render(key: string | undefined, children: () => unknown): void {\n renderServerPortal(key, children, this);\n }\n\n html(): string {\n let out = \"\";\n for (const [key, html] of this.entries) {\n out += `<!--portal:${key}-->${html}<!--/portal:${key}-->`;\n }\n return out;\n }\n}\n\nconst collectors = new WeakMap<ServerPortals, PortalCollector>();\n\n/**\n * Creates the collector a server render writes its portals into. Without one, a `Portal` renders\n * nothing on the server. Create one per request: a render clears it before it starts, and one render\n * may produce at most 256 portals (more throw an `SsrError` whose `code` is `SSR_PORTAL_LIMIT`).\n *\n * @returns A new, empty {@link ServerPortals}.\n * @example\n * import { createComponent, createServerPortals, renderToString } from \"@shadow-js/web/server\";\n *\n * function App() {\n * return <main>Hello</main>;\n * }\n * const portals = createServerPortals();\n * const app = renderToString(() => createComponent(App, {}), { portals });\n * const page = `<body><div id=\"app\">${app}</div>${portals.html()}</body>`;\n * @since 1.0.0\n */\nexport function createServerPortals(): ServerPortals {\n const collector = new PortalCollector();\n const portals: ServerPortals = { html: () => collector.html() };\n collectors.set(portals, collector);\n return portals;\n}\n\n/**\n * The collector behind a `portals` option.\n * @throws TypeError when the value did not come from {@link createServerPortals}.\n * @internal\n */\nexport function collectorOf(portals: ServerPortals): PortalCollector {\n const collector = collectors.get(portals);\n if (collector === undefined) {\n throw new TypeError(\"portals must be created with createServerPortals().\");\n }\n return collector;\n}\n\n/**\n * The server half of a `Portal`: renders its content into the render's collector. The content gets its\n * own hydration scope, so the keys of the siblings do not depend on whether the portal rendered. Writes\n * nothing when the render has no collector or the portal has no key (no render scope).\n *\n * @param key The portal's key, from `nextKey()` at the call site.\n * @param children Reads the portal's content. A thunk, because reading a JSX child builds it, and the\n * content must be built inside its own scope.\n * @param sink The collector the portal context provides (null for a render without one).\n * @internal\n */\nexport function renderServerPortal(\n key: string | undefined,\n children: () => unknown,\n sink: PortalSink | null,\n): void {\n if (sink === null || key === undefined) return;\n const html = withScope(childScope(key), () => escape(children()));\n sink.add(key, html);\n}\n\n/**\n * Runs `build` with the portal sink provided, and returns what `build` returns (the render's accessor).\n * A null sink provides nothing, so the portals of that render write nothing.\n * @internal\n */\nexport function withPortalSink<T>(sink: PortalSink | null, build: () => T): T {\n const view = PortalContext({\n value: sink,\n get children() {\n return build();\n },\n });\n return (view as () => T)();\n}\n\n/**\n * The collector a renderer writes into for one call: null without a `portals` option, else the cleared\n * collector.\n * @internal\n */\nexport function portalCollectorFor(portals: ServerPortals | undefined): PortalCollector | null {\n if (portals === undefined) return null;\n const collector = collectorOf(portals);\n collector.clear();\n return collector;\n}\n"],"mappings":";;;;;AAuCA,IAAa,kBAAb,MAAmD;CACjD,0BAA2B,IAAI,IAAoB;CAEnD,IAAI,KAAa,MAAoB;EACnC,IAAI,CAAC,KAAK,QAAQ,IAAI,GAAG,KAAK,KAAK,QAAQ,QAAA,KACzC,MAAM,IAAI,SACR,oBACA,kFACF;EAEF,KAAK,QAAQ,IAAI,KAAK,IAAI;CAC5B;;CAGA,QAAc;EACZ,KAAK,QAAQ,MAAM;CACrB;CAEA,OAAO,KAAyB,UAA+B;EAC7D,mBAAmB,KAAK,UAAU,IAAI;CACxC;CAEA,OAAe;EACb,IAAI,MAAM;EACV,KAAK,MAAM,CAAC,KAAK,SAAS,KAAK,SAC7B,OAAO,cAAc,IAAI,KAAK,KAAK,cAAc,IAAI;EAEvD,OAAO;CACT;AACF;AAEA,MAAM,6BAAa,IAAI,QAAwC;;;;;;;;;;;;;;;;;;AAmB/D,SAAgB,sBAAqC;CACnD,MAAM,YAAY,IAAI,gBAAgB;CACtC,MAAM,UAAyB,EAAE,YAAY,UAAU,KAAK,EAAE;CAC9D,WAAW,IAAI,SAAS,SAAS;CACjC,OAAO;AACT;;;;;;AAOA,SAAgB,YAAY,SAAyC;CACnE,MAAM,YAAY,WAAW,IAAI,OAAO;CACxC,IAAI,cAAc,KAAA,GAChB,MAAM,IAAI,UAAU,qDAAqD;CAE3E,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,mBACd,KACA,UACA,MACM;CACN,IAAI,SAAS,QAAQ,QAAQ,KAAA,GAAW;CACxC,MAAM,OAAO,UAAU,WAAW,GAAG,SAAS,OAAO,SAAS,CAAC,CAAC;CAChE,KAAK,IAAI,KAAK,IAAI;AACpB;;;;;;AAOA,SAAgB,eAAkB,MAAyB,OAAmB;CAO5E,OANa,cAAc;EACzB,OAAO;EACP,IAAI,WAAW;GACb,OAAO,MAAM;EACf;CACF,CACW,CAAC,CAAa;AAC3B;;;;;;AAOA,SAAgB,mBAAmB,SAA4D;CAC7F,IAAI,YAAY,KAAA,GAAW,OAAO;CAClC,MAAM,YAAY,YAAY,OAAO;CACrC,UAAU,MAAM;CAChB,OAAO;AACT"}
@@ -2,8 +2,8 @@ import { createRootScope, resourceKey, withScope } from "../internal/hydration-s
2
2
  import { MAX_TIMER_MS, SsrError } from "./errors.js";
3
3
  import { escape } from "./escape.js";
4
4
  import { attachRootErrors } from "../control-flow/root-errors.js";
5
- import { portalCollectorFor, withPortalSink } from "./portals.js";
6
5
  import { runWithServerHead } from "../head/server-head.js";
6
+ import { portalCollectorFor, withPortalSink } from "./portals.js";
7
7
  import { ResourceCacheContext, createRoot } from "@shadow-js/core";
8
8
  // src/server/render.ts
9
9
  const DEFAULT_TIMEOUT_MS = 1e4;
@@ -3,8 +3,8 @@ import { MAX_TIMER_MS, SsrError } from "./errors.js";
3
3
  import { escape } from "./escape.js";
4
4
  import { StreamContext } from "../internal/stream-context.js";
5
5
  import { attachRootErrors } from "../control-flow/root-errors.js";
6
- import { portalCollectorFor, withPortalSink } from "./portals.js";
7
6
  import { runWithServerHead } from "../head/server-head.js";
7
+ import { portalCollectorFor, withPortalSink } from "./portals.js";
8
8
  import { serialize } from "./serialize.js";
9
9
  import { ResourceCacheContext, createRoot, untrack } from "@shadow-js/core";
10
10
  // src/server/stream.ts
@@ -4,12 +4,12 @@ import { ssrClass, ssrStyle } from "./server/class-style.js";
4
4
  import { ssrSelectValue, ssrTextArea } from "./server/form-controls.js";
5
5
  import { ssrRawText } from "./server/raw-text.js";
6
6
  import { ssr, ssrElement, ssrHydrationKey, ssrRaw } from "./server/element.js";
7
- import { createServerPortals } from "./server/portals.js";
8
7
  import { normalizeTags } from "./head/validate.js";
9
8
  import { createServerHead } from "./head/server-head.js";
10
9
  import { createComponent, memo } from "./server/component.js";
11
10
  import { derived } from "./server/derived.js";
12
11
  import { HydrationScript, generateHydrationScript } from "./server/hydration-script.js";
12
+ import { createServerPortals } from "./server/portals.js";
13
13
  import { renderToString, renderToStringAsync } from "./server/render.js";
14
14
  import { deserialize, serialize } from "./server/serialize.js";
15
15
  import { renderToStream } from "./server/stream.js";
@@ -2,59 +2,21 @@ import { setAttribute, setAttributeNS, setBoolAttribute } from "./attributes.js"
2
2
  import { autoFocus } from "./autofocus.js";
3
3
  import { applyClass, asRecord, classList, style } from "./class-style.js";
4
4
  import { claims } from "./internal/hydration-hooks.js";
5
- import { bindRegion, createRegion } from "./insert.js";
6
- import { IGNORED_KEYS, attributeName, eventPlan } from "./internal/react-names.js";
5
+ import { bindRegion, createRegion, watchCommits } from "./insert.js";
6
+ import { IGNORED_KEYS, attributeName, eventPlan, isFlagAttribute } from "./internal/react-names.js";
7
7
  import { bindEvent } from "./events.js";
8
8
  import { plainText, setSelectValue } from "./select-value.js";
9
9
  import { onCleanup, untrack, useRenderEffect } from "@shadow-js/core";
10
10
  // src/spread.ts
11
11
  /** The namespace of `xlink:` attributes (SVG's legacy links). */
12
12
  const XLINK_NS = "http://www.w3.org/1999/xlink";
13
- /** The HTML of a `dangerouslySetInnerHTML` value (`{ __html }`); null and undefined mean empty. */
14
- function innerHtmlValue(value) {
15
- if (value === null || typeof value !== "object") return "";
16
- return plainText(Reflect.get(value, "__html"));
17
- }
18
13
  /** Keys that would reach a prototype if written as properties. Ignored everywhere. */
19
- const FORBIDDEN_NAMES = /* @__PURE__ */ new Set([
20
- "__proto__",
21
- "constructor",
22
- "prototype"
23
- ]);
24
- /** True for `on*` keys, which bind event handlers. */
25
- function isEventKey(key) {
26
- return key.length > 2 && key.startsWith("on");
27
- }
28
- /** The name after a `prop:`, `attr:` or `bool:` namespace, or the whole key. */
29
- function nameOf(key) {
30
- const colon = key.indexOf(":");
31
- return colon === -1 ? key : key.slice(colon + 1);
32
- }
33
- /** Reads every prop except children, inside the render effect so each read is tracked. */
34
- function readEntries(props, textarea) {
35
- const entries = [];
36
- if (props === null || typeof props !== "object") return entries;
37
- let value;
38
- let hasValue = false;
39
- for (const key of Object.keys(props)) {
40
- if (FORBIDDEN_NAMES.has(nameOf(key)) || IGNORED_KEYS.has(key)) continue;
41
- if (key === "children") {
42
- if (textarea) {
43
- value = Reflect.get(props, key);
44
- hasValue = true;
45
- }
46
- continue;
47
- }
48
- entries.push([key, Reflect.get(props, key)]);
49
- }
50
- if (hasValue && !entries.some(([key]) => key === "value")) entries.push(["value", value]);
51
- return entries;
52
- }
14
+ const FORBIDDEN_NAMES = /^(?:__proto__|constructor|prototype)$/;
53
15
  /** Runs and forgets the cleanup the last ref callback returned, if any. */
54
16
  function runRefCleanup(state) {
55
- const cleanup = state.refCleanup;
17
+ const cleanup = state.unref;
56
18
  if (cleanup === void 0) return;
57
- state.refCleanup = void 0;
19
+ state.unref = void 0;
58
20
  untrack(cleanup);
59
21
  }
60
22
  /** Applies one prop that is new or changed. */
@@ -64,13 +26,13 @@ function applyKey(el, key, value, state, svg) {
64
26
  if (typeof value === "function") {
65
27
  const callRef = value;
66
28
  const cleanup = untrack(() => callRef(el));
67
- if (typeof cleanup === "function") state.refCleanup = cleanup;
29
+ if (typeof cleanup === "function") state.unref = cleanup;
68
30
  }
69
31
  return;
70
32
  }
71
33
  if (key === "autoFocus") {
72
- if (!state.autoFocused) {
73
- state.autoFocused = true;
34
+ if (!state.focused) {
35
+ state.focused = true;
74
36
  autoFocus(el, value);
75
37
  }
76
38
  return;
@@ -80,27 +42,27 @@ function applyKey(el, key, value, state, svg) {
80
42
  return;
81
43
  }
82
44
  if (key === "style") {
83
- state.styleValue = style(el, value, state.styleValue);
45
+ state.styled = style(el, value, state.styled);
84
46
  return;
85
47
  }
86
48
  if (key === "classList") {
87
- state.classListValue = classList(el, asRecord(value), state.classListValue);
49
+ state.listed = classList(el, asRecord(value), state.listed);
88
50
  return;
89
51
  }
90
- if (isEventKey(key)) {
91
- state.unbinders.get(key)?.();
92
- state.unbinders.delete(key);
52
+ if (key.length > 2 && key.startsWith("on")) {
53
+ state.unbind.get(key)?.();
54
+ state.unbind.delete(key);
93
55
  const plan = eventPlan(key, el.localName, el.getAttribute("type")?.toLowerCase() ?? null);
94
56
  const handler = plan.passive && typeof value === "function" ? {
95
57
  handleEvent: value,
96
58
  passive: true
97
59
  } : value;
98
60
  const unbind = bindEvent(el, plan.type, handler, true, plan.capture);
99
- if (unbind !== void 0) state.unbinders.set(key, unbind);
61
+ if (unbind !== void 0) state.unbind.set(key, unbind);
100
62
  return;
101
63
  }
102
64
  if (key === "innerHTML" || key === "dangerouslySetInnerHTML") {
103
- el.innerHTML = key === "innerHTML" ? plainText(value) : innerHtmlValue(value);
65
+ el.innerHTML = plainText(key === "innerHTML" ? value : value !== null && typeof value === "object" ? Reflect.get(value, "__html") : void 0);
104
66
  return;
105
67
  }
106
68
  if (key === "textContent") {
@@ -111,67 +73,34 @@ function applyKey(el, key, value, state, svg) {
111
73
  setSelectValue(el, value);
112
74
  return;
113
75
  }
114
- if (!svg && LIVE_PROPERTIES.has(key) && key in el) {
115
- liveDiffers(el, key, value, svg) && Reflect.set(el, key, STRING_PROPERTIES.has(key) ? value ?? "" : Boolean(value));
76
+ if (!svg && LIVE_PROPERTIES.test(key) && key in el) {
77
+ liveDiffers(el, key, value, svg) && Reflect.set(el, key, STRING_PROPERTIES.test(key) ? value ?? "" : Boolean(value));
116
78
  if (key === "value" && el instanceof HTMLInputElement) el.defaultValue = plainText(value);
117
79
  return;
118
80
  }
119
- writeKey(el, key, value, svg);
120
- }
121
- /**
122
- * Writes one prop by its key: a `prop:` property, an `attr:` or `bool:` attribute, or an attribute
123
- * under its React name. An `undefined` value removes the attribute or resets the property, which is how
124
- * a prop that disappeared is undone.
125
- */
126
- function writeKey(el, key, value, svg) {
127
81
  if (key.startsWith("prop:")) Reflect.set(el, key.slice(5), value);
128
82
  else if (key.startsWith("attr:")) setAttribute(el, key.slice(5), value);
129
83
  else if (key.startsWith("bool:")) setBoolAttribute(el, key.slice(5), value);
130
84
  else {
131
85
  const wire = attributeName(key, svg);
132
- if (wire.startsWith("xlink:")) setAttributeNS(el, XLINK_NS, wire, value);
86
+ if (isFlagAttribute(wire, value)) setBoolAttribute(el, wire, value);
87
+ else if (wire.startsWith("xlink:")) setAttributeNS(el, XLINK_NS, wire, value);
133
88
  else setAttribute(el, wire, value);
134
89
  }
135
90
  }
136
91
  /** Props written as element properties, like the compiler does for the same names in JSX. */
137
- const LIVE_PROPERTIES = /* @__PURE__ */ new Set([
138
- "value",
139
- "checked",
140
- "selected",
141
- "muted",
142
- "indeterminate",
143
- "defaultValue",
144
- "defaultChecked"
145
- ]);
92
+ const LIVE_PROPERTIES = /^(?:value|checked|selected|muted|indeterminate|defaultValue|defaultChecked)$/;
146
93
  /** The properties of {@link LIVE_PROPERTIES} that hold text; the others are booleans. */
147
- const STRING_PROPERTIES = /* @__PURE__ */ new Set(["value", "defaultValue"]);
94
+ const STRING_PROPERTIES = /^(?:value|defaultValue)$/;
148
95
  /**
149
96
  * True when a live property (typed value, toggled box) no longer matches the value last applied, so
150
97
  * an unchanged prop is written again and the element follows the data.
151
98
  */
152
99
  function liveDiffers(el, key, value, svg) {
153
- if (svg || !LIVE_PROPERTIES.has(key) || !(key in el)) return false;
100
+ if (svg || !LIVE_PROPERTIES.test(key) || !(key in el)) return false;
154
101
  if (key === "value" && el instanceof HTMLSelectElement) return true;
155
102
  const live = Reflect.get(el, key);
156
- return STRING_PROPERTIES.has(key) ? live !== plainText(value) : live !== Boolean(value);
157
- }
158
- /**
159
- * Applies the entries that changed since the last run and undoes the ones that disappeared.
160
- * Unchanged values are skipped by identity, so only changed keys touch the DOM.
161
- */
162
- function applyEntries(el, entries, state, svg) {
163
- const current = /* @__PURE__ */ new Set();
164
- for (const [key, value] of entries) {
165
- current.add(key);
166
- if (state.values.has(key) && Object.is(state.values.get(key), value) && !liveDiffers(el, key, value, svg)) continue;
167
- state.values.set(key, value);
168
- applyKey(el, key, value, state, svg);
169
- }
170
- for (const key of [...state.values.keys()]) {
171
- if (current.has(key)) continue;
172
- state.values.delete(key);
173
- applyKey(el, key, void 0, state, svg);
174
- }
103
+ return STRING_PROPERTIES.test(key) ? live !== plainText(value) : live !== Boolean(value);
175
104
  }
176
105
  /**
177
106
  * Applies a whole props object to an element and keeps it in sync: the helper compiled JSX calls for
@@ -221,26 +150,73 @@ function applyEntries(el, entries, state, svg) {
221
150
  function spread(el, props, isSVG = false, skipChildren = false) {
222
151
  if (props === null || typeof props !== "object" && typeof props !== "function") return;
223
152
  const read = () => typeof props === "function" ? props() : props;
224
- const textarea = el.localName === "textarea";
225
- if (!skipChildren && !textarea) {
153
+ const state = newState();
154
+ if (!skipChildren && el.localName !== "textarea") {
226
155
  const initial = claims.current.isClaiming() ? claims.current.takeRegion(el, null) ?? Array.from(el.childNodes) : void 0;
227
156
  const region = createRegion(el, null, initial);
228
157
  bindRegion(region, () => read()?.children);
158
+ followOptions(el, isSVG, state);
229
159
  }
230
- const state = {
160
+ applyProps(el, read, isSVG, state);
161
+ }
162
+ /**
163
+ * New options replace the ones that held the selection, and the value effect does not read
164
+ * `children`: a select spread selects its value again after every commit to its children, as the
165
+ * compiled path does.
166
+ */
167
+ function followOptions(el, isSVG, state) {
168
+ if (isSVG || !(el instanceof HTMLSelectElement)) return;
169
+ watchCommits(el, () => {
170
+ const key = state.values.has("value") ? "value" : "defaultValue";
171
+ if (state.values.has(key)) setSelectValue(el, state.values.get(key));
172
+ });
173
+ }
174
+ /** The bookkeeping of a spread that has applied nothing yet. */
175
+ function newState() {
176
+ return {
231
177
  values: /* @__PURE__ */ new Map(),
232
- unbinders: /* @__PURE__ */ new Map(),
233
- styleValue: void 0,
234
- classListValue: void 0,
235
- refCleanup: void 0
178
+ unbind: /* @__PURE__ */ new Map()
236
179
  };
180
+ }
181
+ function applyProps(el, read, isSVG, state) {
182
+ const textarea = el.localName === "textarea";
237
183
  onCleanup(() => {
238
- for (const unbind of state.unbinders.values()) unbind();
239
- state.unbinders.clear();
184
+ for (const unbind of state.unbind.values()) unbind();
185
+ state.unbind.clear();
240
186
  runRefCleanup(state);
241
187
  });
242
- useRenderEffect(() => readEntries(read(), textarea), (entries) => {
243
- applyEntries(el, entries, state, isSVG);
188
+ useRenderEffect(() => {
189
+ const props = read();
190
+ const entries = [];
191
+ if (props === null || typeof props !== "object") return entries;
192
+ let value;
193
+ let hasValue = false;
194
+ for (const key of Object.keys(props)) {
195
+ if (FORBIDDEN_NAMES.test(key.slice(key.indexOf(":") + 1)) || IGNORED_KEYS.test(key)) continue;
196
+ if (key === "children") {
197
+ if (textarea) {
198
+ value = Reflect.get(props, key);
199
+ hasValue = true;
200
+ }
201
+ continue;
202
+ }
203
+ entries.push([key, Reflect.get(props, key)]);
204
+ }
205
+ if (hasValue && !entries.some(([key]) => key === "value")) entries.push(["value", value]);
206
+ return entries;
207
+ }, (entries) => {
208
+ const current = /* @__PURE__ */ new Set();
209
+ for (const [key, value] of entries) {
210
+ current.add(key);
211
+ if (state.values.has(key) && Object.is(state.values.get(key), value) && !liveDiffers(el, key, value, isSVG)) continue;
212
+ state.values.set(key, value);
213
+ applyKey(el, key, value, state, isSVG);
214
+ }
215
+ for (const key of [...state.values.keys()]) {
216
+ if (current.has(key)) continue;
217
+ state.values.delete(key);
218
+ applyKey(el, key, void 0, state, isSVG);
219
+ }
244
220
  });
245
221
  }
246
222
  export { spread };