@nac3/runtime 2.3.0-alpha.2 → 2.3.0

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/dist/nac.cjs CHANGED
@@ -654,6 +654,74 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
654
654
  const _manifests = Object.create(null);
655
655
  const _instances = Object.create(null);
656
656
 
657
+ /* v2.3 (F7) -- auth-aware capabilities. Hosts call
658
+ NAC.setCapabilities([...]) when the user logs in / role changes.
659
+ Manifest entries opt in by declaring `requires_capability` on the
660
+ element or on a specific verb. Public verbs (NAC.click /
661
+ NAC.invoke) gate before dispatch and reject with
662
+ code='capability_required' if the cap is absent. */
663
+ let _userCapabilities = new Set();
664
+
665
+ function _resolveCapability(nac_id, verb) {
666
+ /* Walk every registered manifest, find a matching element id, and
667
+ return:
668
+ - the verb-specific requires_capability if verb is given and
669
+ the matching verb entry has one set,
670
+ - else the element-level requires_capability,
671
+ - else null. */
672
+ for (const slug in _manifests) {
673
+ const m = _manifests[slug];
674
+ if (!m) continue;
675
+ const all = (m.elements || []).concat(m.tabs || []);
676
+ for (let i = 0; i < all.length; i++) {
677
+ const e = all[i];
678
+ if (!e || e.id !== nac_id) continue;
679
+ /* verb-level. */
680
+ if (verb && Array.isArray(e.actions)) {
681
+ for (let j = 0; j < e.actions.length; j++) {
682
+ const a = e.actions[j];
683
+ if (a && a.verb === verb && a.requires_capability) {
684
+ return a.requires_capability;
685
+ }
686
+ }
687
+ }
688
+ /* element-level fallback. */
689
+ if (e.requires_capability) return e.requires_capability;
690
+ return null;
691
+ }
692
+ }
693
+ return null;
694
+ }
695
+
696
+ function _hasCapability(cap) {
697
+ if (!cap) return true;
698
+ if (Array.isArray(cap)) {
699
+ /* Array semantics: user must have AT LEAST ONE of the listed
700
+ capabilities (OR). Use repeated array entries OR a higher-
701
+ level guard for AND semantics. */
702
+ for (let i = 0; i < cap.length; i++) {
703
+ if (_userCapabilities.has(cap[i])) return true;
704
+ }
705
+ return false;
706
+ }
707
+ return _userCapabilities.has(cap);
708
+ }
709
+
710
+ function setCapabilities(arr) {
711
+ /* Accepts an array, a single string, or null (clears).
712
+ Replaces the entire set; for incremental updates use
713
+ grantCapability / revokeCapability. */
714
+ if (arr == null) { _userCapabilities = new Set(); return; }
715
+ if (typeof arr === 'string') arr = [arr];
716
+ if (!Array.isArray(arr)) {
717
+ throw NacError('invalid', 'setCapabilities: array of strings required');
718
+ }
719
+ _userCapabilities = new Set(arr.map(String));
720
+ }
721
+ function grantCapability(cap) { _userCapabilities.add(String(cap)); }
722
+ function revokeCapability(cap) { _userCapabilities.delete(String(cap)); }
723
+ function userCapabilities() { return Array.from(_userCapabilities); }
724
+
657
725
  /* register(manifest) -- canonical, plugin_slug inside manifest
658
726
  register(slug, manifest) -- v1.4.1: accepted for back-compat with
659
727
  integrators that match the typical
@@ -846,6 +914,73 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
846
914
  };
847
915
  }
848
916
 
917
+ /* v2.3 (F2): bindTab -- the tab companion to bindAction. Bakes the
918
+ `nac:tab:activated` contract event into the runtime so tab handlers
919
+ don't have to remember to emit it. Same shape, different event
920
+ family. ctx must carry { plugin, tab_id }.
921
+
922
+ Activation event fires nac:tab:activated on document. On handler
923
+ throw or rejected promise, emits nac:tab:activation_failed with
924
+ the same detail shape plus the error message.
925
+
926
+ Like bindAction, listens on 'click' (the canonical user activation).
927
+ Hosts that activate tabs via keyboard or programmatic state must
928
+ dispatch their own click on the element, OR call bindTab with a
929
+ custom activate event (opts.activate_event). */
930
+ function bindTab(el, handler, ctx, opts) {
931
+ if (!el) throw NacError('invalid', 'bindTab: element required');
932
+ if (typeof handler !== 'function') throw NacError('invalid', 'bindTab: handler must be a function');
933
+ if (!ctx || !ctx.plugin || !ctx.tab_id) {
934
+ throw NacError('invalid', 'bindTab: ctx.plugin and ctx.tab_id required');
935
+ }
936
+ const plugin = String(ctx.plugin);
937
+ const tab_id = String(ctx.tab_id);
938
+ const activateEv = (opts && opts.activate_event) || 'click';
939
+
940
+ function emitActivated(ev) {
941
+ document.dispatchEvent(new CustomEvent('nac:tab:activated', {
942
+ detail: {
943
+ plugin: plugin,
944
+ tab_id: tab_id,
945
+ is_trusted: ev ? !!ev.isTrusted : false
946
+ }
947
+ }));
948
+ }
949
+ function emitFailure(ev, err) {
950
+ document.dispatchEvent(new CustomEvent('nac:tab:activation_failed', {
951
+ detail: {
952
+ plugin: plugin,
953
+ tab_id: tab_id,
954
+ is_trusted: ev ? !!ev.isTrusted : false,
955
+ error: (err && err.message) || String(err)
956
+ }
957
+ }));
958
+ }
959
+
960
+ function listener(ev) {
961
+ let result;
962
+ try {
963
+ result = handler.call(el, ev);
964
+ } catch (err) {
965
+ emitFailure(ev, err);
966
+ throw err;
967
+ }
968
+ if (result && typeof result.then === 'function') {
969
+ result.then(
970
+ function () { emitActivated(ev); },
971
+ function (err) { emitFailure(ev, err); }
972
+ );
973
+ } else {
974
+ emitActivated(ev);
975
+ }
976
+ }
977
+
978
+ el.addEventListener(activateEv, listener);
979
+ return function unbind() {
980
+ el.removeEventListener(activateEv, listener);
981
+ };
982
+ }
983
+
849
984
  /* ===================================================================
850
985
  v2.3 PREVIEW (V23-01) -- Field editor primitive.
851
986
 
@@ -957,8 +1092,8 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
957
1092
  if (_manifests['nac_editor']) return;
958
1093
  register({
959
1094
  plugin_slug: 'nac_editor',
960
- version: '2.3.0-preview',
961
- nac_version: '2.2', /* runtime is 2.2; primitive is v2.3 preview */
1095
+ version: '2.3.0',
1096
+ nac_version: '2.3', /* primitive promoted from preview to GA in v2.3 stable (F4) */
962
1097
  elements: [
963
1098
  { id: 'nac_editor.modal', role: 'region',
964
1099
  label_i18n: { es:'Editor de campo',en:'Field editor',pt:'Editor',fr:'Editeur',
@@ -997,16 +1132,16 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
997
1132
  actions: [{ verb: 'delete_selection',
998
1133
  label_i18n: { es:'Borrar seleccion',en:'Delete selection',pt:'Apagar selecao',
999
1134
  fr:'Supprimer selection',it:'Elimina selezione',de:'Auswahl loeschen',
1000
- ja:'選択削除',zh:'删除选择',hi:'chuna hatao',ar:'حذف التحديد' } }],
1135
+ ja:'選択範囲を削除',zh:'删除所选',hi:'chuna hua hatao',ar:'حذف التحديد' } }],
1001
1136
  label_i18n: { es:'Borrar',en:'Delete',pt:'Apagar',fr:'Supprimer',it:'Elimina',
1002
1137
  de:'Loeschen',ja:'削除',zh:'删除',hi:'hatao',ar:'حذف' } },
1003
1138
  { id: 'nac_editor.ai_correct_syntax', role: 'action',
1004
1139
  actions: [{ verb: 'ai_correct_syntax',
1005
- label_i18n: { es:'Corregir con IA',en:'AI correct',pt:'Corrigir IA',
1006
- fr:'Corriger IA',it:'Correggi IA',de:'KI korrigieren',
1007
- ja:'AI修正',zh:'AI修正',hi:'AI sudhar',ar:'تصحيح AI' } }],
1008
- label_i18n: { es:'IA','en':'AI',pt:'IA',fr:'IA',it:'IA',
1009
- de:'KI',ja:'AI',zh:'AI',hi:'AI',ar:'AI' } },
1140
+ label_i18n: { es:'Corregir con IA',en:'AI correct',pt:'Corrigir com IA',
1141
+ fr:'Corriger avec IA',it:'Correggi con IA',de:'KI korrigieren',
1142
+ ja:'AI校正',zh:'AI校正',hi:'AI se sudhar',ar:'تصحيح بالذكاء الاصطناعي' } }],
1143
+ label_i18n: { es:'IA',en:'AI',pt:'IA',fr:'IA',it:'IA',
1144
+ de:'KI',ja:'AI',zh:'AI',hi:'AI',ar:'ذكاء' } },
1010
1145
  { id: 'nac_editor.save', role: 'action',
1011
1146
  actions: [{ verb: 'save',
1012
1147
  label_i18n: { es:'Guardar','en':'Save',pt:'Salvar',fr:'Sauver',it:'Salva',
@@ -1431,10 +1566,116 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
1431
1566
 
1432
1567
  /* ---------- Element discovery ----------------------------------- */
1433
1568
 
1569
+ /* ---------- v2.3 shadow DOM resolution (F1 + F6) ----------
1570
+ Walks open shadow roots recursively (cap depth=10) and delegates
1571
+ to el.nac3.query/queryAll on closed shadow hosts that opted in.
1572
+ Errors are emitted as nac:shadow_blocked / nac:shadow_protocol_violation
1573
+ events, each at most once per host (tracked via WeakSet). Existing
1574
+ light-DOM callsites are unaffected; the helpers are strict supersets
1575
+ of the previous document.querySelectorAll('[data-nac-id]') call. */
1576
+ const _SHADOW_DEPTH_CAP = 10;
1577
+ const _shadowBlockedEmitted = new WeakSet();
1578
+ const _shadowProtocolViolated = new WeakSet();
1579
+
1580
+ function _emitShadowBlocked(host, reason) {
1581
+ if (!host || _shadowBlockedEmitted.has(host)) return;
1582
+ _shadowBlockedEmitted.add(host);
1583
+ try {
1584
+ const ev = new CustomEvent('nac:shadow_blocked', {
1585
+ bubbles: true,
1586
+ composed: true,
1587
+ detail: { host: host, reason: reason, ts: Date.now() }
1588
+ });
1589
+ (host.dispatchEvent ? host : document).dispatchEvent(ev);
1590
+ } catch (_) {}
1591
+ }
1592
+ function _emitShadowProtocolViolation(host, message, value) {
1593
+ if (!host || _shadowProtocolViolated.has(host)) return;
1594
+ _shadowProtocolViolated.add(host);
1595
+ try {
1596
+ const ev = new CustomEvent('nac:shadow_protocol_violation', {
1597
+ bubbles: true,
1598
+ composed: true,
1599
+ detail: { host: host, message: message,
1600
+ value_snapshot: (function () {
1601
+ try { return typeof value === 'object' ? '[object]' : String(value).slice(0, 80); }
1602
+ catch (_) { return '[unprintable]'; } })(), ts: Date.now() }
1603
+ });
1604
+ (host.dispatchEvent ? host : document).dispatchEvent(ev);
1605
+ } catch (_) {}
1606
+ }
1607
+
1608
+ function _deepQueryAll(selector, root, depth) {
1609
+ root = root || document;
1610
+ depth = depth || 0;
1611
+ let out = [];
1612
+ /* Direct match on this root. */
1613
+ try {
1614
+ if (root.querySelectorAll) {
1615
+ out = Array.prototype.slice.call(root.querySelectorAll(selector));
1616
+ }
1617
+ } catch (_) {}
1618
+ if (depth >= _SHADOW_DEPTH_CAP) return out;
1619
+ /* Walk every element under root looking for shadow boundaries. */
1620
+ let walk;
1621
+ try {
1622
+ walk = root.querySelectorAll ? root.querySelectorAll('*') : [];
1623
+ } catch (_) { walk = []; }
1624
+ for (let i = 0; i < walk.length; i++) {
1625
+ const el = walk[i];
1626
+ /* Open shadow root: recurse. */
1627
+ if (el.shadowRoot) {
1628
+ const inner = _deepQueryAll(selector, el.shadowRoot, depth + 1);
1629
+ if (inner.length) out = out.concat(inner);
1630
+ continue;
1631
+ }
1632
+ /* Closed shadow root with voluntary exposure API. */
1633
+ const nac3 = el.nac3;
1634
+ if (nac3 && typeof nac3 === 'object') {
1635
+ try {
1636
+ if (typeof nac3.queryAll === 'function') {
1637
+ const raw = nac3.queryAll(selector);
1638
+ if (Array.isArray(raw)) {
1639
+ const valid = [];
1640
+ for (let k = 0; k < raw.length; k++) {
1641
+ const n = raw[k];
1642
+ if (n && n.nodeType === 1) valid.push(n);
1643
+ else _emitShadowProtocolViolation(el,
1644
+ 'nac3.queryAll returned non-Element item', n);
1645
+ }
1646
+ if (valid.length) out = out.concat(valid);
1647
+ } else if (raw != null) {
1648
+ _emitShadowProtocolViolation(el,
1649
+ 'nac3.queryAll did not return an array', raw);
1650
+ }
1651
+ } else if (typeof nac3.query === 'function') {
1652
+ const one = nac3.query(selector);
1653
+ if (one && one.nodeType === 1) out.push(one);
1654
+ else if (one != null) {
1655
+ _emitShadowProtocolViolation(el,
1656
+ 'nac3.query returned non-Element value', one);
1657
+ }
1658
+ }
1659
+ } catch (e) {
1660
+ _emitShadowBlocked(el, 'exposure_threw');
1661
+ }
1662
+ } else if (el.hasAttribute && el.hasAttribute('data-nac-shadow')
1663
+ && el.getAttribute('data-nac-shadow') === 'closed') {
1664
+ /* Element explicitly declared as closed-shadow with no exposure
1665
+ API. Per spec sec 5.1.6, this is a deliberate opt-out;
1666
+ emit shadow_blocked once so debugging tooling sees it. */
1667
+ _emitShadowBlocked(el, 'not_exposed');
1668
+ }
1669
+ }
1670
+ return out;
1671
+ }
1672
+ function _deepQuery(selector, root) {
1673
+ const all = _deepQueryAll(selector, root, 0);
1674
+ return all.length ? all[0] : null;
1675
+ }
1676
+
1434
1677
  function _allElements() {
1435
- return Array.prototype.slice.call(
1436
- document.querySelectorAll('[data-nac-id]')
1437
- );
1678
+ return _deepQueryAll('[data-nac-id]', document, 0);
1438
1679
  }
1439
1680
 
1440
1681
  function _activePlugin() {
@@ -1829,6 +2070,25 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
1829
2070
  });
1830
2071
  throw NacError('disabled', 'Element ' + nac_id + ' is disabled');
1831
2072
  }
2073
+ /* v2.3 (F7): capability gate. If the action declares
2074
+ requires_capability and the current user set does not satisfy
2075
+ it, reject before dispatch with code='capability_required'. */
2076
+ {
2077
+ const reqCap = _resolveCapability(nac_id, null);
2078
+ if (reqCap && !_hasCapability(reqCap)) {
2079
+ _emitCommandRejected({
2080
+ command_method: 'click',
2081
+ command_target: nac_id,
2082
+ reason: 'capability_required',
2083
+ message: 'Element ' + nac_id + ' requires capability ' +
2084
+ (Array.isArray(reqCap) ? '[' + reqCap.join(',') + ']' : reqCap),
2085
+ source: (opts && opts.source) || undefined,
2086
+ });
2087
+ throw NacError('capability_required',
2088
+ 'Element ' + nac_id + ' requires capability ' +
2089
+ (Array.isArray(reqCap) ? '[' + reqCap.join(',') + ']' : reqCap));
2090
+ }
2091
+ }
1832
2092
  /* v1.8.0: hidden-target rejection. A button inside display:none
1833
2093
  or aria-hidden="true" is not a click target a user could reach;
1834
2094
  sec 6.2.28 requires us to surface this rather than fire silently.
@@ -2265,6 +2525,26 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
2265
2525
  const verb = (payload && payload.verb)
2266
2526
  || el.getAttribute('data-nac-action')
2267
2527
  || '';
2528
+ /* v2.3 (F7): capability gate. Unlike disabled, capabilities ARE
2529
+ enforced on invoke -- a missing permission is not a UI state
2530
+ issue, it is a logical authorization failure that should
2531
+ short-circuit BEFORE the host's listener even hears about it. */
2532
+ {
2533
+ const reqCap = _resolveCapability(action_id, verb);
2534
+ if (reqCap && !_hasCapability(reqCap)) {
2535
+ _emitCommandRejected({
2536
+ command_method: 'invoke',
2537
+ command_target: action_id,
2538
+ reason: 'capability_required',
2539
+ message: 'Action ' + action_id + ' (verb=' + verb + ') requires capability ' +
2540
+ (Array.isArray(reqCap) ? '[' + reqCap.join(',') + ']' : reqCap),
2541
+ source: (opts && opts.source) || undefined,
2542
+ });
2543
+ throw NacError('capability_required',
2544
+ 'Action ' + action_id + ' (verb=' + verb + ') requires capability ' +
2545
+ (Array.isArray(reqCap) ? '[' + reqCap.join(',') + ']' : reqCap));
2546
+ }
2547
+ }
2268
2548
  const pluginRoot = el.closest('[data-nac-plugin]');
2269
2549
  const plugin = pluginRoot
2270
2550
  ? pluginRoot.getAttribute('data-nac-plugin')
@@ -3587,6 +3867,12 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
3587
3867
  }
3588
3868
  }
3589
3869
  function _byId(id) {
3870
+ /* v2.3: deep-aware. Walks open shadow roots + closed-shadow
3871
+ exposure API. Falls back to document.querySelector when
3872
+ the deep helper is unavailable (defensive). */
3873
+ if (typeof _deepQuery === 'function') {
3874
+ return _deepQuery('[data-nac-id="' + id + '"]', document);
3875
+ }
3590
3876
  return document.querySelector('[data-nac-id="' + id + '"]');
3591
3877
  }
3592
3878
 
@@ -5153,14 +5439,17 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
5153
5439
 
5154
5440
  global.NAC = {
5155
5441
  __nac_v1_installed: true,
5156
- version: '2.3.0-alpha.2',
5442
+ version: '2.3.0',
5157
5443
  spec_version: '2.3',
5158
5444
  /* v2.2.0 (V22-01) -- strict validation toggle. When true,
5159
5445
  NAC.register throws on findings (manifest_role_unknown,
5160
5446
  tab_id_manifest_role_drift, manifest_dom_role_mismatch).
5161
5447
  Default false in v2.2 (warning-only) for the migration
5162
- window; v2.3 default flips to true; v3.0 removes the flag. */
5163
- STRICT_VALIDATION: false,
5448
+ window; v2.3 stable flips default to true (F3); v3.0 removes
5449
+ the flag. Opt-out: set NAC.STRICT_VALIDATION = false in your
5450
+ bootstrap before any register() call. See
5451
+ guides/MIGRATION_v2.2_to_v2.3.md. */
5452
+ STRICT_VALIDATION: true,
5164
5453
  /* registry */
5165
5454
  register: register,
5166
5455
  unregister: unregister,
@@ -5180,6 +5469,16 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
5180
5469
  Removes the brownfield foot-gun where panels forget to emit
5181
5470
  the contract event and NAC.click() times out spuriously. */
5182
5471
  bindAction: bindAction,
5472
+ bindTab: bindTab,
5473
+ /* v2.3 (F7): auth-aware capabilities. setCapabilities replaces
5474
+ the user's full set; grant/revoke for incremental updates;
5475
+ userCapabilities returns a snapshot array. NAC.click and
5476
+ NAC.invoke gate against this set when the manifest entry
5477
+ declares requires_capability. */
5478
+ setCapabilities: setCapabilities,
5479
+ grantCapability: grantCapability,
5480
+ revokeCapability: revokeCapability,
5481
+ userCapabilities: userCapabilities,
5183
5482
  /* v2.3.0 PREVIEW (V23-01) -- field editor primitive. Opens a
5184
5483
  Word-style modal on any field, with select/replace/delete/
5185
5484
  AI-syntax-correct verbs all NAC-3 callable. The modal