@gramlot/gramlot 0.3.9 → 0.3.10

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": "@gramlot/gramlot",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -19,6 +19,7 @@ import {TriggerTimers} from './providers.js';
19
19
  export class RemoteBinding {
20
20
  #binding;
21
21
  #timers;
22
+ #closed = false;
22
23
  // The name and TYTX parameters of the fragment mounted last on the node, whoever requested it.
23
24
  #lastName = null;
24
25
  #lastParams = null;
@@ -30,7 +31,13 @@ export class RemoteBinding {
30
31
 
31
32
  get binding() { return this.#binding; }
32
33
  get node() { return this.binding.node; }
33
- get closed() { return this.binding.closed; }
34
+ get closed() { return this.#closed || this.binding.closed; }
35
+
36
+ /** Removing the declaration releases scheduled calls; an in-flight fragment keeps the node request contract. */
37
+ close() {
38
+ this.#closed = true;
39
+ this.#timers.cancel();
40
+ }
34
41
 
35
42
  /** Start `_timing` again from its current value: called at every registration of the node's pointers. */
36
43
  register() {
@@ -81,7 +88,7 @@ export class RemoteBinding {
81
88
  throw new Error(`${node.nodeTag} '${node.label}': remote '${name}' failed: ${error.message}`, {cause: error});
82
89
  }
83
90
  const onRemote = node.getAttr('_onRemote');
84
- if (mounted && onRemote) this.#inline('_onRemote', onRemote, onRemote, params);
91
+ if (!this.closed && mounted && onRemote) this.#inline('_onRemote', onRemote, onRemote, params);
85
92
  }
86
93
 
87
94
  /**
@@ -218,8 +218,8 @@ export class BindingRuntime {
218
218
  /**
219
219
  * Steps 2-4 of §5.1 on `branch` (a SourceBagNode or a new SourceBag value). The `nodeId` map is
220
220
  * complete, and a duplicate refused, before the first write or registration, so a `#<nodeId>`
221
- * of the branch translates; then the installation (`dataSetter`s and defaults), then one
222
- * NodeBinding per node and the registrations, providers included. On an error the NodeBindings
221
+ * of the branch translates; one NodeBinding per node owns expression cleanup before installation
222
+ * (`dataSetter`s and defaults), followed by registrations, providers included. On an error the NodeBindings
223
223
  * opened here are closed and the map entries they brought are removed; the Data is not rolled
224
224
  * back. Then `_init` (step 5), once per node: its error propagates and the NodeBindings stay
225
225
  * open (P12, no rollback).
@@ -241,8 +241,8 @@ export class BindingRuntime {
241
241
  }
242
242
  for (const [nodeId, node] of entering) this.nodeIds.set(nodeId, node);
243
243
  try {
244
- this.installer.install(branch);
245
244
  for (const node of nodes) this.openBinding(node);
245
+ this.installer.install(branch);
246
246
  for (const node of nodes) this.bindingFor(node).registerPointers();
247
247
  } catch (error) {
248
248
  const errors = [error];
@@ -412,6 +412,10 @@ export class NodeBinding {
412
412
  const dataElement = node._getMeta('data_element');
413
413
  if (dataElement && !PROVIDERS[node.nodeTag]) return;
414
414
  if (!dataElement && typeof node.getAttr('remote') === 'string') this.#remote ??= new RemoteBinding(this);
415
+ else if (this.#remote) {
416
+ this.#remote.close();
417
+ this.#remote = null;
418
+ }
415
419
  const remoteDependencies = new Set();
416
420
  if (node._getMeta('gramlot_component')?.bagStore) {
417
421
  const storepath = node.getAttr('storepath');
@@ -527,6 +531,7 @@ export class NodeBinding {
527
531
  close() {
528
532
  if (this.closed) return;
529
533
  this.#closed = true;
534
+ this.#remote?.close();
530
535
  if (this.node._getMeta('data_element')) this.runtime.renderer.references.remove(this.node);
531
536
  this.#closeRegistrations();
532
537
  const errors = [];
@@ -193,7 +193,8 @@ export class GramlotBuilderBagNode extends SourceBagNode {
193
193
  * `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the four providers, the
194
194
  * `_onCalling`/`_onResult`/`_onError` of a dataRpc and the `_onCalling` of a dataRemote, `action` of a
195
195
  * button, `_if`/`_else`/`_onRemote`/`_onCalling` of a node whose `remote` is a string, every `connect_on<event>`,
196
- * every `==` attribute (canonical name) and a `==` node value (`''`).
196
+ * every `==` attribute (canonical name) and a `==` node value (`''`). Script elements also retain
197
+ * their literal executable text and URLs here for the live renderer's activation check.
197
198
  */
198
199
  codeAttributes() {
199
200
  const own = [...(CODE_ATTRIBUTES[this.nodeTag] ?? []),
@@ -214,8 +215,12 @@ export class GramlotBuilderBagNode extends SourceBagNode {
214
215
  }
215
216
  for (const [name, value] of sourceAttributeItems(this.getAttr())) {
216
217
  if (isExpression(value)) items.push([name, value]);
218
+ if (this.nodeTag === 'script' && typeof value === 'string') {
219
+ const prefix = this.builder.constructor.dialectPrefixes.find(prefix => name.startsWith(prefix)) ?? '';
220
+ if (['src', 'href', 'xlink:href', 'xlink_href', '_text'].includes(name.slice(prefix.length).toLowerCase())) items.push([name, value]);
221
+ }
217
222
  }
218
- if (isExpression(this.staticValue)) items.push(['', this.staticValue]);
223
+ if (isExpression(this.staticValue) || this.nodeTag === 'script' && typeof this.staticValue === 'string') items.push(['', this.staticValue]);
219
224
  return items;
220
225
  }
221
226
 
@@ -274,7 +279,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
274
279
 
275
280
  /** Publish to this node's selfsubscribe topics, as in the legacy Source API. */
276
281
  publish(topic, payload = {}) {
277
- return this.builder.binding.subscriptions.publishNode(this, topic, payload);
282
+ return this.rootBuilder.binding.subscriptions.publishNode(this, topic, payload);
278
283
  }
279
284
 
280
285
  /** Builder's `^` pointers, except the attributes ending in `_path`, which hold a bare path. */
@@ -1,6 +1,7 @@
1
1
  /** Parent-owned regions. Layout changes never move or recreate slotted content. */
2
2
  import {defineComponent} from './registry.js';
3
3
  import {Container} from './container.js';
4
+ import createDOMPurify from 'dompurify';
4
5
 
5
6
  const SIDES = ['top', 'bottom', 'left', 'right'];
6
7
  const REGIONS = [...SIDES, 'center'];
@@ -99,10 +100,12 @@ export const BorderContainer = defineComponent({
99
100
  #observer;
100
101
  #contentObserver;
101
102
  #drag;
103
+ #sanitizer;
102
104
  constructor() {
103
105
  super();
104
106
  this.control.replaceChildren();
105
107
  const doc = this.ownerDocument;
108
+ this.#sanitizer = createDOMPurify(doc.defaultView);
106
109
  this.#contentObserver = new doc.defaultView.ResizeObserver(entries => {
107
110
  for (const {target} of entries) {
108
111
  const region = target.closest('.region');
@@ -309,7 +312,10 @@ export const BorderContainer = defineComponent({
309
312
  state.separator.classList.add('separator'); state.drawer.classList.add('drawer');
310
313
  state.drawer.classList.toggle('drawer-reveal', config.minimize);
311
314
  state.drawer.classList.toggle('drawer-rotated', !horizontal(side));
312
- state.drawerLabel.innerHTML = config.label ?? '';
315
+ state.drawerLabel.replaceChildren(this.#sanitizer.sanitize(config.label ?? '', {
316
+ USE_PROFILES: {html: true}, FORBID_TAGS: ['style', 'form', 'input', 'button', 'select', 'textarea'],
317
+ RETURN_DOM_FRAGMENT: true,
318
+ }));
313
319
  state.drawerIcon.textContent = config.icon ?? '';
314
320
  state.drawerIcon.hidden = !config.icon;
315
321
  state.drawer.toggleAttribute('data-icon', !!config.icon);
@@ -1,4 +1,4 @@
1
- import {Bag} from '@genrojs/bag';
1
+ import {Bag} from '@gramlot/gramlot';
2
2
  import {NativeControl} from './bases.js';
3
3
  import {parseChoices} from './choices.js';
4
4
  import {positionPopup} from './popup.js';
@@ -1,4 +1,4 @@
1
- import {Bag} from '@genrojs/bag';
1
+ import {Bag} from '@gramlot/gramlot';
2
2
  import {pointerTemplate} from '@genrojs/builders';
3
3
 
4
4
  /** Shared ordered rules. Context supplies activated callbacks/RPC; this module never writes Data. */
@@ -28,8 +28,8 @@ const NATIVE_ATTRIBUTES = {
28
28
  // Display directives of the node text: consumed by the renderer, never written as attributes.
29
29
  const DISPLAY_ATTRIBUTES = ['format', 'mask', 'locale', 'timeZone', 'places', 'dtype'];
30
30
 
31
- // Output attributes whose `javascript:` value the browser runs as code (`action` never reaches the output).
32
- const URL_ATTRIBUTES = new Set(['href', 'src', 'formaction', 'xlink:href', 'xlink_href']);
31
+ // Output URL attributes, including the native form action authored as html_action.
32
+ const URL_ATTRIBUTES = new Set(['href', 'src', 'action', 'formaction', 'xlink:href', 'xlink_href']);
33
33
 
34
34
  /** The attributes of `attrs` that are not binding attributes. */
35
35
  export function withoutBindingAttributes(attrs) {
@@ -56,6 +56,15 @@ export function requireStyleClassAttributes(node, attrs, prefixes = []) {
56
56
  * the dialect `prefixes` (`html_`).
57
57
  */
58
58
  export function requireNoScriptAttributes(node, attrs, prefixes = []) {
59
+ if (node.nodeTag === 'script') {
60
+ const executable = [['', node.staticValue], ...sourceAttributeItems(node.getAttr()).filter(([name]) =>
61
+ ['src', 'href', 'xlink:href', 'xlink_href', '_text'].includes(name.slice((prefixes.find(prefix => name.startsWith(prefix)) ?? '').length).toLowerCase()))];
62
+ for (const [name, value] of executable) {
63
+ if (node.pointerType(value) || isExpression(value)) {
64
+ throw new Error(`script '${node.label}': '${name}' must be literal Source; script code and URLs cannot come from Data`);
65
+ }
66
+ }
67
+ }
59
68
  if (attrs.onClose != null && !node.parentNode?._getMeta('gramlot_component')?.childParameters?.onClose) {
60
69
  throw new Error('onClose is only supported on a tab/stack panel');
61
70
  }
@@ -20,8 +20,8 @@ export function gramlotDev() {
20
20
 
21
21
  const RUNTIME_ASSETS = ['gramlot.js', 'gramlot.min.js', 'runtime-notices.json'];
22
22
 
23
- /** URL of a packaged browser asset in dist: a file: URL from npm (read with fs.readFile), an https: URL
24
- * from JSR (read with fetch). Without a name, the runtime to serve at the runtime URL: gramlot.js when
23
+ /** URL of a packaged browser asset in dist: a file: URL from npm (read with fs.readFile).
24
+ * Without a name, the runtime to serve at the runtime URL: gramlot.js when
25
25
  * GRAMLOT_DEV=DEBUG, gramlot.min.js otherwise. Another name throws a TypeError. */
26
26
  export function runtimeAsset(name = null) {
27
27
  if (name === null) name = gramlotDev() === 'DEBUG' ? 'gramlot.js' : 'gramlot.min.js';
package/src/view/html.js CHANGED
@@ -139,6 +139,13 @@ export class HtmlElement {
139
139
  * control belongs to its ControlAdapter: neither the attribute nor the property is written here.
140
140
  */
141
141
  update(record, node, attrs = node.attr, textValue = DEFAULT_TEXT, visible = null) {
142
+ if (node.nodeTag === 'script') {
143
+ for (const [name, code] of node.codeAttributes()) {
144
+ if (node.activatedCode(name) !== code) {
145
+ throw new Error(`script '${node.label}': '${name}' runs only as received with the Source`);
146
+ }
147
+ }
148
+ }
142
149
  const {element, text} = record;
143
150
  const isHtml = element.namespaceURI === XHTML_NS;
144
151
  const renderAttributes = this.definition(node).meta.render_attributes ?? {};