@nac3/runtime 2.5.2 → 2.8.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.
@@ -22,6 +22,8 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
22
22
  - NAC.registerCatalog(catalog) i18n catalog registration
23
23
  - NAC.locale(code?) locale getter/setter
24
24
  - NAC.setSupportedLocales(arr) extend supported list
25
+ - NAC.registerCatalog(obj[, {replace}]) merge translations; the
26
+ option restores the pre-2.7.0 whole-entry replacement
25
27
  - NAC.setRTLLocales(arr) extend RTL list
26
28
  - NAC.set_provenance_secret(secret) HMAC secret registration
27
29
 
@@ -82,8 +84,39 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
82
84
 
83
85
  function locale(code) {
84
86
  if (code === undefined) return _currentLocale;
87
+ /* REFUSE an unsupported locale (TF-A08).
88
+
89
+ It used to console.warn and then assign on the next line, which
90
+ is the worst of both: the host is told something is wrong and
91
+ the wrong thing happens anyway. Two consequences followed.
92
+
93
+ First, the state was a lie: NAC.locale() reported a locale the
94
+ runtime does not support, and every t() lookup fell through the
95
+ chain to Spanish without the caller knowing why.
96
+
97
+ Second, and worse because it is silent: an invalid code is not
98
+ in the RTL list either, so `ar` misspelled as `ar-SA` or `arr`
99
+ stopped flipping dir=rtl and the page rendered Arabic
100
+ left-to-right. A typo in a locale code became a layout bug two
101
+ screens away.
102
+
103
+ And the file was already inconsistent with itself:
104
+ setSupportedLocales, setRTLLocales and set_provenance_secret all
105
+ THROW on bad input. This one warned. Nothing explained the
106
+ difference because there was none.
107
+
108
+ The escape hatch is not new and is not invented here:
109
+ setSupportedLocales(arr) has existed since 2.0 and is
110
+ documented at the top of this file as "extend supported list".
111
+ A host that wants an eleventh locale says so, once, instead of
112
+ discovering that a typo half-works. */
85
113
  if (_supported.indexOf(code) < 0) {
86
- console.warn('[NAC v2] locale', code, 'not in supported list');
114
+ throw new Error('[NAC v2] locale: ' + JSON.stringify(code)
115
+ + ' is not a supported locale. Supported: ' + _supported.join(',')
116
+ + '. To add one, call NAC.setSupportedLocales([...]) first --'
117
+ + ' this used to warn and then set it anyway, which left the'
118
+ + ' runtime reporting a locale it cannot resolve and silently'
119
+ + ' stopped RTL from applying.');
87
120
  }
88
121
  _currentLocale = code;
89
122
  /* Auto-apply dir=rtl unless host opted out via setAutoRTL(false). */
@@ -100,11 +133,67 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
100
133
  detail: { locale: code, autoRTL: _autoRTL }, bubbles: true
101
134
  }));
102
135
  }
103
- function registerCatalog(obj) {
136
+ /* MERGE per locale, do not replace per key (TF-A09).
137
+
138
+ The old body was one line:
139
+
140
+ Object.keys(obj).forEach(function (k) { _catalog[k] = obj[k]; });
141
+
142
+ -- assignment, so registering a key replaced its WHOLE entry. A
143
+ host that had a key in all ten locales and then called
144
+ registerCatalog({ 'save.label': { en: 'Save' } }) -- to correct
145
+ the English, say -- lost the other nine. Nothing was reported,
146
+ because overwriting is what assignment does.
147
+
148
+ What makes it expensive is where the symptom comes out: not here,
149
+ and not in English. t() falls through requested -> es -> en ->
150
+ first available, so the nine missing locales keep RETURNING
151
+ SOMETHING. A French user sees Spanish, three screens away, and
152
+ the cause is a line of boot code that looked like it added a
153
+ translation.
154
+
155
+ The runtime has already decided this question once, in the other
156
+ direction: syncPlugin exists precisely so a caller can REPLACE
157
+ instead of add, because add was the default and replacing needed
158
+ asking for. The catalogue had no equivalent, so it only had the
159
+ destructive half.
160
+
161
+ So: merge by locale, and say so when a value is really being
162
+ overwritten -- an overwrite is sometimes exactly what the host
163
+ means, and silence is what made this hard to find. A caller that
164
+ wants the old semantics asks for them:
165
+
166
+ NAC.registerCatalog(obj, { replace: true }) */
167
+ function registerCatalog(obj, opts) {
104
168
  if (!obj || typeof obj !== 'object') {
105
169
  throw new Error('[NAC v2] registerCatalog requires object');
106
170
  }
107
- Object.keys(obj).forEach(function (k) { _catalog[k] = obj[k]; });
171
+ var replace = !!(opts && opts.replace);
172
+ Object.keys(obj).forEach(function (k) {
173
+ var incoming = obj[k];
174
+ /* A non-object entry has no locales to merge; assign it, which
175
+ is what the old code did and the only thing that makes sense. */
176
+ if (replace || !incoming || typeof incoming !== 'object'
177
+ || !_catalog[k] || typeof _catalog[k] !== 'object') {
178
+ _catalog[k] = incoming;
179
+ return;
180
+ }
181
+ var existing = _catalog[k];
182
+ var merged = {};
183
+ Object.keys(existing).forEach(function (loc) { merged[loc] = existing[loc]; });
184
+ Object.keys(incoming).forEach(function (loc) {
185
+ if (Object.prototype.hasOwnProperty.call(existing, loc)
186
+ && existing[loc] !== incoming[loc]) {
187
+ if (typeof console !== 'undefined' && console.warn) {
188
+ console.warn('[NAC v2] registerCatalog: overwriting ' + k + '.' + loc
189
+ + ' -- the other locales of this key are kept. Pass { replace: true }'
190
+ + ' to drop them on purpose.');
191
+ }
192
+ }
193
+ merged[loc] = incoming[loc];
194
+ });
195
+ _catalog[k] = merged;
196
+ });
108
197
  }
109
198
  function t(key, opts) {
110
199
  opts = opts || {};
@@ -112,7 +201,25 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
112
201
  var entry = _catalog[key];
113
202
  if (!entry) return opts.fallback || key;
114
203
  if (entry[loc]) return entry[loc];
115
- /* Fallback chain: requested -> es -> en -> first available */
204
+ /* Fallback chain: requested -> es -> en -> first available.
205
+
206
+ 2.7.0 (TF-A10) -- WHY es BEFORE en, written down because for
207
+ two years it was not, and an undocumented resolution order is a
208
+ coin flip an adopter discovers in production. SPEC.md section
209
+ 3 now publishes this chain normatively; this comment says what
210
+ the sentence there cannot.
211
+
212
+ The order is DELIBERATE and it is kept (D-14, owner delegation
213
+ of 2026-11-24). It does NOT contradict "the English is
214
+ normative": that rule is about the SPECIFICATION DOCUMENT, not
215
+ about which string a host's own catalogue serves. Flipping it
216
+ to en-first would change, with no bug fixed, what every adopter
217
+ with a Spanish-authored catalogue and an incomplete locale map
218
+ renders today -- a visible, silent change, which rule 7 of the
219
+ working agreement forbids and nothing in the list asked for.
220
+ A host that wants another order declares its own locales: a
221
+ catalogue covering the 10 locales (which section 8 level 2
222
+ REQUIRES) never reaches this line at all. */
116
223
  if (entry.es) return entry.es;
117
224
  if (entry.en) return entry.en;
118
225
  var firstKey = Object.keys(entry)[0];
@@ -127,6 +234,17 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
127
234
  else if (Array.isArray(s)) _provenanceSecrets = s.slice();
128
235
  else if (s == null) _provenanceSecrets = [];
129
236
  else throw new Error('[NAC v2] set_provenance_secret expects string|string[]');
237
+ /* 2.7.0 (TF-A17): SPEC.md section 7.2 publishes the HMAC-signed
238
+ manifest example as set_provenance_secret() followed by
239
+ register(). Manifest verification had to land in the CORE
240
+ runtime -- register() lives there and is synchronous -- and the
241
+ core cannot see this closure, so the secret is pushed across.
242
+ Without this line the published example would set a secret the
243
+ verifier never receives, and a correctly signed manifest would
244
+ be recorded as "nobody could tell". */
245
+ if (NAC && typeof NAC.set_manifest_secret === 'function') {
246
+ try { NAC.set_manifest_secret(_provenanceSecrets.slice()); } catch (_) {}
247
+ }
130
248
  /* v2.0-rc3 (Claude T6-F2): warm the crypto path now that we
131
249
  have a secret registered; first agent sign won't pay the
132
250
  cold-start cost. */
@@ -470,7 +588,14 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
470
588
  if (entry.element) {
471
589
  entry.element.setAttribute('data-nac-id', fullSlug);
472
590
  entry.element.setAttribute('data-nac-parent', chain.join(' '));
473
- if (entry.role) entry.element.setAttribute('role', entry.role);
591
+ /* PND-135: the role the host declared, resolved against the
592
+ vocabulary and written where the protocol reads it. An
593
+ undeclared word is reported instead of planted -- this
594
+ path never threw and must not start: an adopter upgrading
595
+ a minor version does not get an exception. */
596
+ var _regRole = _declaredRole(entry.role);
597
+ if (entry.role && !_regRole) _warnUnderivableRole(fullSlug, entry.element);
598
+ _applyRoleAttr(entry.element, _regRole);
474
599
  if (entry.label_i18n) {
475
600
  var lbl = t.bind(null);
476
601
  var labelText = (function () {
@@ -619,20 +744,227 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
619
744
  return 'auto_' + (Math.abs(h)).toString(36);
620
745
  }
621
746
 
747
+ /* ---------------------------------------------------------------
748
+ ROLE DERIVATION (rewritten 2026-11-27, PND-135)
749
+
750
+ WHAT IT DID, measured 2026-11-26 against an ERP with 920
751
+ interface modules -- the exact case the brownfield path exists
752
+ for. This function returned four words that NO registry declares:
753
+ `link` for an <a href>, `toggle` for a checkbox, `select` for a
754
+ <select>, and `interactive` for anything else. None of the four
755
+ is in the normative table of SPEC.md section 1, none is one of
756
+ the seven declared aliases of 1.5, and none is in the runtime's
757
+ own known-role set. And autoRegister did not keep them to itself:
758
+ it WROTE them onto the adopter's elements.
759
+
760
+ Why that was not cosmetic. SPEC 1.3 says a NAC-strict runtime
761
+ SHOULD refuse an unknown role at register time, and strict is the
762
+ default since 2.3 -- so the path this project opened in 2.0 to
763
+ adopt a legacy tree produced exactly what the adopter's own
764
+ runtime rejects. It is the same producer/consumer asymmetry 2.8.0
765
+ closed for the 52 roles the tree used and the text did not
766
+ declare, arriving from the other direction.
767
+
768
+ THE DECISION, taken 2026-11-27 and written here rather than in a
769
+ commit message: the four are MAPPED to canonical roles, and
770
+ `interactive` is not mapped at all because it is not a role --
771
+ it is the absence of one. Declaring the four as new roles was
772
+ the wrong half of the fork: a vocabulary that grows every time a
773
+ derivation cannot decide is a vocabulary that means nothing.
774
+
775
+ And the derivation now ASKS instead of asserting: every word it
776
+ is about to return goes through the runtime's own vocabulary
777
+ (NAC.canonical_role + NAC.roles), so a page that declared its
778
+ own role with NAC.register_role() -- the door 2.8.0 opened -- is
779
+ honoured here too, and a word this table gets wrong is caught at
780
+ runtime rather than planted. scripts/check-role-registries.mjs
781
+ confronts the two tables below against the runtime's set as a
782
+ SIXTH registry, so they cannot drift from it in silence.
783
+ --------------------------------------------------------------- */
784
+
785
+ /* ARIA role -> NAC role. Two different vocabularies: the ARIA role
786
+ says what a thing IS to assistive technology, the NAC role says
787
+ what an agent can DO with it, and the old code treated them as
788
+ the same string. Absent from this table = cannot be derived, which
789
+ is a decision and not an omission: `img`, `presentation` and
790
+ `tooltip` have no NAC role, and inventing one for them is what
791
+ this item is about. */
792
+ var _ARIA_TO_NAC = {
793
+ 'button': 'button',
794
+ 'link': 'action',
795
+ 'menuitem': 'action',
796
+ 'menuitemcheckbox': 'option',
797
+ 'menuitemradio': 'option',
798
+ 'checkbox': 'option',
799
+ 'radio': 'option',
800
+ 'switch': 'option',
801
+ 'option': 'option',
802
+ 'textbox': 'field',
803
+ 'searchbox': 'field',
804
+ 'spinbutton': 'field',
805
+ 'combobox': 'combobox',
806
+ 'listbox': 'listbox',
807
+ 'slider': 'slider',
808
+ 'tab': 'tab',
809
+ 'tablist': 'tablist',
810
+ 'tabpanel': 'tabpanel',
811
+ 'dialog': 'modal',
812
+ 'alertdialog': 'confirm-dialog',
813
+ 'status': 'status',
814
+ 'alert': 'status',
815
+ 'progressbar': 'status',
816
+ 'log': 'log',
817
+ 'menu': 'menu',
818
+ 'menubar': 'menu',
819
+ 'navigation': 'navigation',
820
+ 'region': 'region',
821
+ 'group': 'region',
822
+ 'banner': 'banner',
823
+ 'list': 'list',
824
+ 'listitem': 'row',
825
+ 'row': 'row',
826
+ 'tree': 'tree',
827
+ 'treeitem': 'tree-node',
828
+ 'table': 'data-table',
829
+ 'grid': 'data-table',
830
+ 'treegrid': 'data-table'
831
+ };
832
+
833
+ /* Tag (and, for the two tags where the type decides, tag:type) ->
834
+ NAC role. `a` without an href is NOT here: an anchor with no
835
+ destination is not an action, and the old code called it
836
+ `interactive`. */
837
+ var _TAG_TO_NAC = {
838
+ 'button': 'button',
839
+ 'a': 'action',
840
+ 'summary': 'accordion-toggle',
841
+ 'textarea': 'field',
842
+ 'input': 'field',
843
+ 'input:checkbox': 'option',
844
+ 'input:radio': 'option',
845
+ 'input:range': 'slider',
846
+ 'select': 'combobox',
847
+ 'select:multiple': 'listbox'
848
+ };
849
+
850
+ /* Every word the two tables can produce, computed once. It is what
851
+ the derivation falls back to when the runtime is older than 2.8
852
+ and cannot be asked -- never a hand-typed role list, which is the
853
+ defect that made the static lint call conforming pages wrong for
854
+ four minor versions. */
855
+ var _DERIVABLE_ROLES = (function () {
856
+ var out = [];
857
+ [_ARIA_TO_NAC, _TAG_TO_NAC].forEach(function (table) {
858
+ Object.keys(table).forEach(function (k) {
859
+ if (out.indexOf(table[k]) < 0) out.push(table[k]);
860
+ });
861
+ });
862
+ return out;
863
+ })();
864
+
865
+ /* The word, only if the vocabulary declares it. Returns the
866
+ canonical spelling (so a legacy alias arrives resolved) or null,
867
+ and null MEANS null: the caller does not get a guess. */
868
+ function _declaredRole(word) {
869
+ if (!word) return null;
870
+ var w = String(word).toLowerCase();
871
+ try {
872
+ if (typeof NAC.canonical_role === 'function') {
873
+ w = NAC.canonical_role(w) || w;
874
+ }
875
+ if (typeof NAC.roles === 'function') {
876
+ var v = NAC.roles() || {};
877
+ if (Array.isArray(v.canonical) && v.canonical.indexOf(w) >= 0) return w;
878
+ if (Array.isArray(v.registered) && v.registered.some(function (r) {
879
+ return r && (r.role === w || r === w);
880
+ })) return w;
881
+ /* The vocabulary answered, and it does not know this word.
882
+ That is an answer, not a reason to fall through. */
883
+ return null;
884
+ }
885
+ } catch (_) { /* fall through to the table */ }
886
+ return _DERIVABLE_ROLES.indexOf(w) >= 0 ? w : null;
887
+ }
888
+
622
889
  function _deriveRole(el) {
623
- var tag = el.tagName.toLowerCase();
624
- var attrRole = el.getAttribute('role');
625
- if (attrRole) return attrRole;
626
- if (tag === 'button') return 'button';
627
- if (tag === 'a' && el.hasAttribute('href')) return 'link';
890
+ if (!el || !el.getAttribute) return null;
891
+
892
+ /* 1. The protocol's own attribute, first: if the author already
893
+ said what this is, nothing here is a derivation. */
894
+ var declared = el.getAttribute('data-nac-role');
895
+ if (declared) return _declaredRole(declared);
896
+
897
+ /* 2. The ARIA role, through the mapping -- and if it is not in the
898
+ mapping, it still gets its chance at the vocabulary, because
899
+ an earlier version of THIS function wrote NAC roles into the
900
+ ARIA attribute and those trees exist. */
901
+ var aria = el.getAttribute('role');
902
+ if (aria) {
903
+ var a = String(aria).toLowerCase();
904
+ return _declaredRole(_ARIA_TO_NAC[a] || a);
905
+ }
906
+
907
+ /* 3. The element itself. */
908
+ var tag = (el.tagName || '').toLowerCase();
628
909
  if (tag === 'input') {
629
- var t = el.getAttribute('type') || 'text';
630
- if (t === 'checkbox' || t === 'radio') return 'toggle';
631
- return 'field';
910
+ var t = String(el.getAttribute('type') || 'text').toLowerCase();
911
+ return _declaredRole(_TAG_TO_NAC['input:' + t] || _TAG_TO_NAC.input);
912
+ }
913
+ if (tag === 'select') {
914
+ var multi = el.hasAttribute && el.hasAttribute('multiple');
915
+ return _declaredRole(multi ? _TAG_TO_NAC['select:multiple'] : _TAG_TO_NAC.select);
632
916
  }
633
- if (tag === 'textarea') return 'field';
634
- if (tag === 'select') return 'select';
635
- return 'interactive';
917
+ if (tag === 'a') {
918
+ return (el.hasAttribute && el.hasAttribute('href'))
919
+ ? _declaredRole(_TAG_TO_NAC.a) : null;
920
+ }
921
+ if (_TAG_TO_NAC[tag]) return _declaredRole(_TAG_TO_NAC[tag]);
922
+
923
+ /* 4. data-nac-action: the author declared an action on this
924
+ element, so `action` is READ rather than guessed. This is
925
+ what the cards of example-v20-full.php get, and what used to
926
+ come back as `interactive`. */
927
+ if (el.getAttribute('data-nac-action')) return _declaredRole('action');
928
+
929
+ /* 5. Nothing. The caller says so out loud; it does not invent. */
930
+ return null;
931
+ }
932
+
933
+ /* The role goes where the protocol reads it: data-nac-role.
934
+
935
+ Until 2026-11-27 all three registration paths wrote it into the
936
+ ARIA `role` attribute instead -- a category error with two shapes.
937
+ For `button` and `link` it wrote the element's OWN implicit ARIA
938
+ role back onto it, which changes nothing; for `toggle`, `select`,
939
+ `field` and `interactive` it wrote tokens that do not exist in
940
+ WAI-ARIA, which a user agent ignores -- so no assistive technology
941
+ ever saw any of it, while `data-nac-role`, the attribute the
942
+ runtime and the static lint both read, was left empty.
943
+
944
+ Only when absent, like every other attribute these paths write: an
945
+ author who declared a role keeps it. */
946
+ function _applyRoleAttr(el, role) {
947
+ if (!el || !role || !el.setAttribute) return;
948
+ if (!el.getAttribute('data-nac-role')) el.setAttribute('data-nac-role', role);
949
+ }
950
+
951
+ /* A derivation that came back empty is reported, not swallowed: the
952
+ adopter has three ways to answer it (declare data-nac-role, pass
953
+ derive.role, or register the vocabulary this tree uses with
954
+ NAC.register_role) and no way to learn that from silence. */
955
+ function _warnUnderivableRole(slug, el) {
956
+ try {
957
+ document.dispatchEvent(new CustomEvent('nac:autoregister_role_underivable', {
958
+ detail: {
959
+ slug: slug,
960
+ tag: (el && el.tagName) ? el.tagName.toLowerCase() : null,
961
+ aria_role: (el && el.getAttribute) ? (el.getAttribute('role') || null) : null,
962
+ hint: 'no canonical NAC role could be derived: declare data-nac-role, '
963
+ + 'pass derive.role, or NAC.register_role() the vocabulary this tree uses'
964
+ },
965
+ bubbles: true
966
+ }));
967
+ } catch (_) { /* headless (SPEC 19): no document to dispatch on */ }
636
968
  }
637
969
 
638
970
  function _deriveLabel(el) {
@@ -693,6 +1025,11 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
693
1025
  var role = (opts.derive && opts.derive.role === 'auto')
694
1026
  ? _deriveRole(el)
695
1027
  : (opts.derive && opts.derive.role) || _deriveRole(el);
1028
+ /* An explicit derive.role is the HOST talking, so it is resolved
1029
+ against the vocabulary too rather than trusted blindly -- the
1030
+ same courtesy the derivation gets, for the same reason. */
1031
+ if (role) role = _declaredRole(role);
1032
+ if (!role) _warnUnderivableRole(fullSlug, el);
696
1033
 
697
1034
  var entry = {
698
1035
  slug: fullSlug,
@@ -709,7 +1046,7 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
709
1046
  if (parentSlug) {
710
1047
  el.setAttribute('data-nac-parent', parentSlug.split(SEPARATOR).join(' '));
711
1048
  }
712
- if (role && !el.getAttribute('role')) el.setAttribute('role', role);
1049
+ _applyRoleAttr(el, role);
713
1050
  if (labelI18n && !el.getAttribute('aria-label')) {
714
1051
  var lab = labelI18n[_currentLocale] || labelI18n.es || labelI18n.en || fullSlug;
715
1052
  el.setAttribute('aria-label', lab);
@@ -817,6 +1154,9 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
817
1154
  var role = (rule.derive && rule.derive.role)
818
1155
  ? rule.derive.role(el)
819
1156
  : _deriveRole(el);
1157
+ /* PND-135: a rule's own derive.role goes through the
1158
+ vocabulary like everything else. */
1159
+ role = _declaredRole(role);
820
1160
  var labelI18n = (rule.derive && rule.derive.label_i18n)
821
1161
  ? rule.derive.label_i18n(el)
822
1162
  : (function () { var o={}; o[_currentLocale]=_deriveLabel(el); return o; })();
@@ -834,9 +1174,10 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
834
1174
  adopted: true,
835
1175
  registered_at: Date.now()
836
1176
  };
1177
+ if (!role) _warnUnderivableRole(fullSlug, el);
837
1178
  _scopes[fullSlug] = entry;
838
1179
  el.setAttribute('data-nac-id', fullSlug);
839
- if (role && !el.getAttribute('role')) el.setAttribute('role', role);
1180
+ _applyRoleAttr(el, role);
840
1181
  if (labelI18n && !el.getAttribute('aria-label')) {
841
1182
  var lab = labelI18n[_currentLocale] || labelI18n.es || labelI18n.en || fullSlug;
842
1183
  el.setAttribute('aria-label', lab);
@@ -1,5 +1,5 @@
1
1
  // Generated by build.mjs -- do not edit. Source: js/nac-v2-extensions.js
2
- // Brownfield + data-table extensions for @nac3/runtime v2.5.2.
2
+ // Brownfield + data-table extensions for @nac3/runtime v2.8.0.
3
3
  // This module MUST load AFTER the core runtime; it attaches every
4
4
  // member below onto the same window.NAC object. Import as:
5
5
  // import '@nac3/runtime';