ripple 0.2.165 → 0.2.166

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
@@ -3,7 +3,7 @@
3
3
  "description": "Ripple is an elegant TypeScript UI framework",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.2.165",
6
+ "version": "0.2.166",
7
7
  "type": "module",
8
8
  "module": "src/runtime/index-client.js",
9
9
  "main": "src/runtime/index-client.js",
@@ -81,6 +81,6 @@
81
81
  "typescript": "^5.9.2"
82
82
  },
83
83
  "peerDependencies": {
84
- "ripple": "0.2.165"
84
+ "ripple": "0.2.166"
85
85
  }
86
86
  }
@@ -1,4 +1,4 @@
1
- /** @import {Expression, FunctionExpression, Node, Program} from 'estree' */
1
+ /** @import {Expression, FunctionExpression, Node, Program, Statement} from 'estree' */
2
2
 
3
3
  /** @typedef {Map<number, {offset: number, delta: number}>} PostProcessingChanges */
4
4
  /** @typedef {number[]} LineOffsets */
@@ -129,7 +129,7 @@ function visit_head_element(node, context) {
129
129
  }
130
130
  }
131
131
 
132
- function apply_updates(init, update) {
132
+ function apply_updates(init, update, state) {
133
133
  if (update.length === 1) {
134
134
  init.push(
135
135
  b.stmt(
@@ -155,8 +155,25 @@ function apply_updates(init, update) {
155
155
  const render_statements = [];
156
156
  let index = 0;
157
157
 
158
+ const grouped_updates = new Map();
159
+
158
160
  for (const u of update) {
159
161
  if (u.initial) {
162
+ const id =
163
+ u.identity.type === 'Identifier' ? state.scope.get(u.identity.name)?.initial : u.identity;
164
+ let updates = grouped_updates.get(id);
165
+
166
+ if (updates === undefined) {
167
+ updates = [];
168
+ grouped_updates.set(id, updates);
169
+ }
170
+ updates.push(u);
171
+ }
172
+ }
173
+
174
+ for (const [, updates] of grouped_updates) {
175
+ if (updates.length === 1) {
176
+ const u = updates[0];
160
177
  const key = index_to_key(index);
161
178
  index_map.set(u.operation, key);
162
179
  initial.push(b.prop('init', b.id(key), u.initial));
@@ -171,6 +188,29 @@ function apply_updates(init, update) {
171
188
  );
172
189
  index++;
173
190
  } else {
191
+ const key = index_to_key(index);
192
+ /** @type {Array<Statement>} */
193
+ const if_body = [
194
+ b.stmt(b.assignment('=', b.member(b.id('__prev'), b.id(key)), b.id('__' + key))),
195
+ ];
196
+ initial.push(b.prop('init', b.id(key), updates[0].initial));
197
+ render_statements.push(
198
+ b.var('__' + key, updates[0].expression),
199
+ b.if(
200
+ b.binary('!==', b.member(b.id('__prev'), b.id(key)), b.id('__' + key)),
201
+ b.block(if_body),
202
+ ),
203
+ );
204
+ for (const u of updates) {
205
+ index_map.set(u.operation, key);
206
+ if_body.push(u.operation(b.id('__' + key)));
207
+ index++;
208
+ }
209
+ }
210
+ }
211
+
212
+ for (const u of update) {
213
+ if (!u.initial) {
174
214
  render_statements.push(u.operation);
175
215
  }
176
216
  }
@@ -835,6 +875,7 @@ const visitors = {
835
875
  if (is_dom_element) {
836
876
  let class_attribute = null;
837
877
  let style_attribute = null;
878
+ /** @type {Array<Statement>} */
838
879
  const local_updates = [];
839
880
  const is_void = is_void_element(node.id.name);
840
881
 
@@ -997,9 +1038,11 @@ const visitors = {
997
1038
  });
998
1039
  } else {
999
1040
  local_updates.push({
1000
- operation: b.stmt(
1001
- b.call('_$_.set_attribute', id, b.literal(attribute), expression),
1002
- ),
1041
+ operation: (key) =>
1042
+ b.stmt(b.call('_$_.set_attribute', id, b.literal(attribute), key)),
1043
+ expression,
1044
+ identity: attr.value,
1045
+ initial: b.void0,
1003
1046
  });
1004
1047
  }
1005
1048
  } else {
@@ -1044,6 +1087,7 @@ const visitors = {
1044
1087
  operation: (key) =>
1045
1088
  b.stmt(b.call('_$_.set_class', id, key, hash_arg, b.literal(is_html))),
1046
1089
  expression,
1090
+ identity: class_attribute.value,
1047
1091
  initial: b.literal(''),
1048
1092
  });
1049
1093
  } else {
@@ -1063,14 +1107,16 @@ const visitors = {
1063
1107
  const id = state.flush_node();
1064
1108
  const metadata = { tracking: false, await: false };
1065
1109
  const expression = visit(style_attribute.value, { ...state, metadata });
1066
- const name = style_attribute.name.name;
1067
-
1068
- const statement = b.stmt(b.call('_$_.set_attribute', id, b.literal(name), expression));
1069
1110
 
1070
1111
  if (metadata.tracking) {
1071
- local_updates.push({ operation: statement });
1112
+ local_updates.push({
1113
+ operation: (key) => b.stmt(b.call('_$_.set_style', id, key)),
1114
+ identity: style_attribute.value,
1115
+ expression,
1116
+ initial: b.void0,
1117
+ });
1072
1118
  } else {
1073
- state.init.push(statement);
1119
+ state.init.push(b.stmt(b.call('_$_.set_style', id, expression)));
1074
1120
  }
1075
1121
  }
1076
1122
  }
@@ -1084,7 +1130,9 @@ const visitors = {
1084
1130
  );
1085
1131
  }
1086
1132
 
1133
+ /** @type {Array<Statement>} */
1087
1134
  const init = [];
1135
+ /** @type {Array<Statement>} */
1088
1136
  const update = [];
1089
1137
 
1090
1138
  if (!is_void) {
@@ -1100,7 +1148,7 @@ const visitors = {
1100
1148
 
1101
1149
  if (update.length > 0) {
1102
1150
  if (state.scope.parent.declarations.size > 0) {
1103
- apply_updates(init, update);
1151
+ apply_updates(init, update, state);
1104
1152
  } else {
1105
1153
  state.update.push(...update);
1106
1154
  }
@@ -2252,6 +2300,7 @@ function transform_children(children, context) {
2252
2300
  state.update.push({
2253
2301
  operation: (key) => b.stmt(b.call('_$_.set_text', id, key)),
2254
2302
  expression,
2303
+ identity: node.expression,
2255
2304
  initial: b.literal(' '),
2256
2305
  });
2257
2306
  if (metadata.await) {
@@ -2279,6 +2328,7 @@ function transform_children(children, context) {
2279
2328
  state.update.push({
2280
2329
  operation: (key) => b.stmt(b.call('_$_.set_text', id, key)),
2281
2330
  expression,
2331
+ identity: node.expression,
2282
2332
  initial: b.literal(' '),
2283
2333
  });
2284
2334
  if (metadata.await) {
@@ -2356,7 +2406,7 @@ function transform_body(body, { visit, state }) {
2356
2406
  // In TypeScript mode, just add the update statements directly
2357
2407
  body_state.init.push(...body_state.update);
2358
2408
  } else {
2359
- apply_updates(body_state.init, body_state.update);
2409
+ apply_updates(body_state.init, body_state.update, state);
2360
2410
  }
2361
2411
  }
2362
2412
 
@@ -9,6 +9,7 @@ export {
9
9
  export {
10
10
  set_text,
11
11
  set_class,
12
+ set_style,
12
13
  set_attribute,
13
14
  set_value,
14
15
  set_checked,
@@ -28,12 +28,6 @@ export function init_operations() {
28
28
  // @ts-expect-error
29
29
  element_prototype.__click = undefined;
30
30
  // @ts-expect-error
31
- element_prototype.__attributes = null;
32
- // @ts-expect-error
33
- element_prototype.__styles = null;
34
- // @ts-expect-error
35
- element_prototype.__e = undefined;
36
- // @ts-expect-error
37
31
  event_target_prototype.__root = undefined;
38
32
  }
39
33
 
@@ -1,7 +1,7 @@
1
1
  /** @import { Block } from '#client' */
2
2
 
3
3
  import { destroy_block, ref } from './blocks.js';
4
- import { REF_PROP, TRACKED, TRACKED_OBJECT } from './constants.js';
4
+ import { REF_PROP } from './constants.js';
5
5
  import {
6
6
  get_descriptors,
7
7
  get_own_property_symbols,
@@ -69,25 +69,29 @@ function get_setters(element) {
69
69
 
70
70
  /**
71
71
  * @param {Element} element
72
- * @param {string} attribute
73
72
  * @param {any} value
74
73
  * @returns {void}
75
74
  */
76
- export function set_attribute(element, attribute, value) {
77
- // @ts-expect-error
78
- var attributes = (element.__attributes ??= {});
79
-
80
- if (attributes[attribute] === (attributes[attribute] = value)) return;
81
-
82
- if (attribute === 'style' && '__styles' in element) {
83
- // reset styles to force style: directive to update
84
- element.__styles = {};
75
+ export function set_style(element, value) {
76
+ if (value == null) {
77
+ element.removeAttribute('style');
78
+ } else if (typeof value !== 'string') {
79
+ apply_styles(/** @type {HTMLElement} */ (element), value);
80
+ } else {
81
+ // @ts-ignore
82
+ element.style.cssText = value;
85
83
  }
84
+ }
86
85
 
86
+ /**
87
+ * @param {Element} element
88
+ * @param {string} attribute
89
+ * @param {any} value
90
+ * @returns {void}
91
+ */
92
+ export function set_attribute(element, attribute, value) {
87
93
  if (value == null) {
88
94
  element.removeAttribute(attribute);
89
- } else if (attribute === 'style' && typeof value !== 'string') {
90
- apply_styles(/** @type {HTMLElement} */ (element), value);
91
95
  } else if (typeof value !== 'string' && get_setters(element).includes(attribute)) {
92
96
  /** @type {any} */ (element)[attribute] = value;
93
97
  } else {
@@ -97,13 +101,13 @@ export function set_attribute(element, attribute, value) {
97
101
 
98
102
  /**
99
103
  * @param {HTMLElement} element
100
- * @param {HTMLElement['style']} newStyles
104
+ * @param {HTMLElement['style']} new_styles
101
105
  */
102
- export function apply_styles(element, newStyles) {
106
+ export function apply_styles(element, new_styles) {
103
107
  const style = element.style;
104
108
  const new_properties = new Set();
105
109
 
106
- for (const [property, value] of Object.entries(newStyles)) {
110
+ for (const [property, value] of Object.entries(new_styles)) {
107
111
  const normalized_property = normalize_css_property_name(property);
108
112
  const normalized_value = String(value);
109
113
 
@@ -132,6 +136,8 @@ function set_attribute_helper(element, key, value) {
132
136
  if (key === 'class') {
133
137
  const is_html = element.namespaceURI === 'http://www.w3.org/1999/xhtml';
134
138
  set_class(/** @type {HTMLElement} */ (element), value, undefined, is_html);
139
+ } else if (key === 'style') {
140
+ set_style(/** @type {HTMLElement} */ (element), value);
135
141
  } else if (key === '#class') {
136
142
  // Special case for static class when spreading props
137
143
  element.classList.add(value);