@verajs/ssr 0.1.3 → 0.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verajs/ssr",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Vera-native server-side rendering. Node-only, plain ESM, zero dependencies.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -95,7 +95,9 @@ export const serializeTemplate = (template) => {
95
95
  const value = values[i];
96
96
  switch (kinds[i]) {
97
97
  case TEXT:
98
- out += serializeValue(value);
98
+ /** A spread rewrites the open tag it sits in, so it is folded rather than appended. */
99
+ if (value !== null && typeof value === 'object' && value._$attrs$) out = foldSpread(out, value._$attrs$());
100
+ else out += serializeValue(value);
99
101
  break;
100
102
  case BOOLEAN:
101
103
  if (value) out += ` ${names[i]}=""`;
@@ -113,13 +115,62 @@ export const serializeTemplate = (template) => {
113
115
  return out + parts[kinds.length];
114
116
  };
115
117
 
118
+ /**
119
+ * Fold resolved spread bindings into the tag being built, mirroring what the client does.
120
+ *
121
+ * Appending is not enough, and the difference is a correctness bug rather than a nicety.
122
+ * `<input type="text" ${spread({ type: 'number' })}>` appends a second `type`, and an HTML parser
123
+ * keeps the **first** duplicate — so the server would render `type="text"` while the client, where
124
+ * `setAttribute` overwrites, renders `type="number"`. Same template, two answers, and a hydration
125
+ * mismatch between them.
126
+ *
127
+ * So a spread key removes any attribute of that name already written into the open tag before
128
+ * adding its own — including when it adds nothing, because `?disabled: false` and `id: null` both
129
+ * *remove* on the client and must remove here too. Kinds that never touch attributes client-side
130
+ * (events, non-form properties) leave the tag alone.
131
+ *
132
+ * Splitting on the last `<` is safe: attribute values are escaped, so no raw `<` can appear inside
133
+ * one.
134
+ */
135
+ const foldSpread = (out, entries) => {
136
+ const tagStart = out.lastIndexOf('<');
137
+ let tag = out.slice(tagStart);
138
+ let added = '';
139
+
140
+ for (const [kind, name, value] of entries) {
141
+ const serializes = kind === 'a' || kind === 'b' || (kind === 'p' && FORM_ATTRIBUTES.includes(name));
142
+ if (!serializes) continue;
143
+
144
+ /** Quoted, single-quoted, unquoted, or valueless — whatever the template author wrote. */
145
+ tag = tag.replace(new RegExp(`\\s${name}(=("[^"]*"|'[^']*'|[^\\s>]*))?`, 'i'), '');
146
+
147
+ if (kind === 'b') {
148
+ if (value) added += ` ${name}=""`;
149
+ } else if (value != null && value !== false) {
150
+ added += ` ${name}="${escapeHtml(kind === 'p' && value === true ? '' : serializeValue(value, true))}"`;
151
+ }
152
+ }
153
+
154
+ /** The element-position slot already carries the separating space. */
155
+ return out.slice(0, tagStart) + tag + (added ? added.slice(1) : '');
156
+ };
157
+
116
158
  const serializeValue = (value, raw = false) => {
117
159
  if (value == null || value === false) return '';
118
160
  if (Array.isArray(value)) return value.map((entry) => serializeValue(entry, raw)).join('');
119
161
  if (typeof value === 'function') return '';
120
162
  if (typeof value === 'object') {
121
- /** Template-shaped (core's html, by shape) recurses; anything else (refs…) is client-side. */
122
- return value.strings ? serializeTemplate(value) : '';
163
+ /** Template-shaped (core's html, by shape) recurses. */
164
+ if (value.strings) return serializeTemplate(value);
165
+ /**
166
+ * A spread (`@verajs/spread`) at element position. It hands back resolved bindings and this
167
+ * decides what reaches markup: attributes and truthy booleans do, form properties do because
168
+ * hydration reads them back, and events and other properties are client state. Escaping happens
169
+ * here and only here — principle #8 puts it at the render boundary, not at the source.
170
+ *
171
+ * Anything else at element position is a ref, which is a client concern.
172
+ */
173
+ return '';
123
174
  }
124
175
  return raw ? String(value) : escapeHtml(value);
125
176
  };