@nac3/runtime 2.3.0 → 2.4.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/CHANGELOG.md +109 -0
- package/README.md +8 -3
- package/SPEC.md +2 -2
- package/dist/nac.browser.js +386 -40
- package/dist/nac.cjs +386 -40
- package/dist/nac.mjs +386 -40
- package/package.json +4 -2
package/dist/nac.cjs
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
// Generated by build.mjs -- do not edit. Source: yujin.app/nac-spec/js/nac.js
|
|
2
2
|
const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== 'undefined' ? window : {});
|
|
3
3
|
/* =====================================================================
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Reference JavaScript implementation. Spec: spec/NAC-v1.0.md.
|
|
4
|
+
NAC3 (Native Agent Contract v3) -- reference JavaScript runtime.
|
|
5
|
+
Spec: packages/nac/SPEC.md. Public docs: yujin.app/nac-spec/.
|
|
7
6
|
MIT License -- Pablo Adrian Kuschniroff + Sumi, 2026.
|
|
8
7
|
=====================================================================
|
|
9
8
|
|
|
10
9
|
AUTHORITATIVE VERSION CONSTANT: see the global.NAC export at the
|
|
11
|
-
bottom of this file. `version: '
|
|
10
|
+
bottom of this file. `version: '2.4.0', spec_version: '2.4'` is
|
|
12
11
|
the normative declaration. The dated changelog sections below are
|
|
13
12
|
historical notes; if they ever drift from the export, the export
|
|
14
13
|
wins.
|
|
@@ -325,6 +324,43 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
325
324
|
return e;
|
|
326
325
|
}
|
|
327
326
|
|
|
327
|
+
/* H1.v7 (2026-05-21): structured disambiguation contract.
|
|
328
|
+
When the runtime cannot resolve an id/verb/qualifier/label
|
|
329
|
+
unambiguously, it MUST emit `nac:disambiguation_requested` with
|
|
330
|
+
a structured candidate list so reactive hosts (chat UI, voice
|
|
331
|
+
agent, assistive software) can present the choices to the user.
|
|
332
|
+
The error thrown alongside carries the same candidates on
|
|
333
|
+
err.candidates so synchronous catchers can also recover.
|
|
334
|
+
|
|
335
|
+
Detail shape:
|
|
336
|
+
method: 'click_by_verb' | 'tab' | 'tab_by_label' | ...
|
|
337
|
+
plugin: string | null
|
|
338
|
+
query: { verb, qualifier, label, id, ... } (whatever applies)
|
|
339
|
+
candidates: [{ nac_id: '...', label: '...' }, ...]
|
|
340
|
+
|
|
341
|
+
Hosts SHOULD listen on document and reply by re-dispatching
|
|
342
|
+
with a more specific qualifier/id. Voice agents SHOULD speak
|
|
343
|
+
"Did you mean A or B?" using the labels. */
|
|
344
|
+
function _emitDisambiguationRequested(detail) {
|
|
345
|
+
if (typeof document === 'undefined' || !document.dispatchEvent) return;
|
|
346
|
+
try {
|
|
347
|
+
document.dispatchEvent(new CustomEvent('nac:disambiguation_requested', {
|
|
348
|
+
detail: detail,
|
|
349
|
+
}));
|
|
350
|
+
} catch (e) { /* tolerated -- runtime should still throw */ }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function _candidateLabel(el) {
|
|
354
|
+
/* Best-effort human label for a DOM element. Used in
|
|
355
|
+
candidates[].label so voice/asistive can re-prompt. */
|
|
356
|
+
if (!el) return '';
|
|
357
|
+
const al = el.getAttribute && el.getAttribute('aria-label');
|
|
358
|
+
if (al) return String(al).trim();
|
|
359
|
+
const tx = (el.textContent || '').replace(/\s+/g, ' ').trim();
|
|
360
|
+
if (tx) return tx.slice(0, 120);
|
|
361
|
+
return el.getAttribute && el.getAttribute('data-nac-id') || '';
|
|
362
|
+
}
|
|
363
|
+
|
|
328
364
|
/* ---------- v1.8.0: skip-validate, command-events, dedup ------- */
|
|
329
365
|
|
|
330
366
|
/* Spec sec 5: data-nac-validate="skip" marks a subtree (typically
|
|
@@ -2361,13 +2397,43 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
2361
2397
|
const ft = el.getAttribute('data-nac-field-type');
|
|
2362
2398
|
|
|
2363
2399
|
if (ft === 'checkbox' || ft === 'radio') {
|
|
2364
|
-
|
|
2400
|
+
/* React tracks `checked` similarly to `value`; use the native
|
|
2401
|
+
setter so controlled checkboxes/radios update state. */
|
|
2402
|
+
var nativeCheckedSetter = Object.getOwnPropertyDescriptor(
|
|
2403
|
+
window.HTMLInputElement.prototype, 'checked'
|
|
2404
|
+
);
|
|
2405
|
+
if (nativeCheckedSetter && nativeCheckedSetter.set) {
|
|
2406
|
+
nativeCheckedSetter.set.call(el, !!value);
|
|
2407
|
+
} else {
|
|
2408
|
+
el.checked = !!value;
|
|
2409
|
+
}
|
|
2365
2410
|
} else if (el.tagName === 'SELECT') {
|
|
2366
|
-
|
|
2411
|
+
var nativeSelectSetter = Object.getOwnPropertyDescriptor(
|
|
2412
|
+
window.HTMLSelectElement.prototype, 'value'
|
|
2413
|
+
);
|
|
2414
|
+
if (nativeSelectSetter && nativeSelectSetter.set) {
|
|
2415
|
+
nativeSelectSetter.set.call(el, String(value));
|
|
2416
|
+
} else {
|
|
2417
|
+
el.value = String(value);
|
|
2418
|
+
}
|
|
2367
2419
|
} else if (el.hasAttribute('contenteditable')) {
|
|
2368
2420
|
el.textContent = String(value == null ? '' : value);
|
|
2369
2421
|
} else {
|
|
2370
|
-
|
|
2422
|
+
/* React + controlled inputs: setting el.value directly is silently
|
|
2423
|
+
reverted on the next render because React's synthetic event system
|
|
2424
|
+
tracks the original property setter on HTMLInputElement.prototype.
|
|
2425
|
+
Use the native setter so React's onChange receives the new value
|
|
2426
|
+
and updates its state. Works on plain DOM inputs unchanged. */
|
|
2427
|
+
var proto = (el.tagName === 'TEXTAREA')
|
|
2428
|
+
? window.HTMLTextAreaElement.prototype
|
|
2429
|
+
: window.HTMLInputElement.prototype;
|
|
2430
|
+
var nativeValueSetter = Object.getOwnPropertyDescriptor(proto, 'value');
|
|
2431
|
+
var newVal = String(value == null ? '' : value);
|
|
2432
|
+
if (nativeValueSetter && nativeValueSetter.set) {
|
|
2433
|
+
nativeValueSetter.set.call(el, newVal);
|
|
2434
|
+
} else {
|
|
2435
|
+
el.value = newVal;
|
|
2436
|
+
}
|
|
2371
2437
|
}
|
|
2372
2438
|
|
|
2373
2439
|
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
@@ -2408,6 +2474,81 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
2408
2474
|
}
|
|
2409
2475
|
|
|
2410
2476
|
async function tab(plugin, tab_key) {
|
|
2477
|
+
/* H1.v5 (2026-05-20): single-arg ergonomic form. When the caller
|
|
2478
|
+
passes only a full nac_id (e.g. "conduit.tab.feed__your_feed"),
|
|
2479
|
+
resolve it directly against the DOM without requiring the
|
|
2480
|
+
caller to split plugin + tab_key. The two-arg form remains
|
|
2481
|
+
the canonical contract; this is a back-compat ergonomic. */
|
|
2482
|
+
if (tab_key === undefined && typeof plugin === 'string' && plugin.indexOf('.') >= 0) {
|
|
2483
|
+
const bareId = plugin;
|
|
2484
|
+
let tabEl = document.querySelector(
|
|
2485
|
+
'[data-nac-role="tab"][data-nac-id="' + bareId + '"]'
|
|
2486
|
+
);
|
|
2487
|
+
/* H1.v7 (2026-05-21): strict resolution. If the bare id is not
|
|
2488
|
+
in DOM, try the qualified-instance fallback ONLY when the
|
|
2489
|
+
match is unambiguous: exactly one element whose id starts
|
|
2490
|
+
with bareId + "__". Zero matches throws no_matching_instance;
|
|
2491
|
+
multiple matches throws ambiguous_tab. No silent resolution
|
|
2492
|
+
to "the first one that looks similar" -- assistive users
|
|
2493
|
+
landing on the wrong tab from a near-match is the exact
|
|
2494
|
+
failure mode the protocol forbids. */
|
|
2495
|
+
if (!tabEl) {
|
|
2496
|
+
const cands = document.querySelectorAll('[data-nac-role="tab"][data-nac-id]');
|
|
2497
|
+
const exact = [];
|
|
2498
|
+
for (let i = 0; i < cands.length; i++) {
|
|
2499
|
+
const id = cands[i].getAttribute('data-nac-id') || '';
|
|
2500
|
+
if (id.indexOf(bareId + '__') === 0) exact.push(cands[i]);
|
|
2501
|
+
}
|
|
2502
|
+
if (exact.length === 1) {
|
|
2503
|
+
tabEl = exact[0];
|
|
2504
|
+
} else if (exact.length > 1) {
|
|
2505
|
+
const candidates = [];
|
|
2506
|
+
for (let i = 0; i < exact.length; i++) {
|
|
2507
|
+
candidates.push({
|
|
2508
|
+
nac_id: exact[i].getAttribute('data-nac-id'),
|
|
2509
|
+
label: _candidateLabel(exact[i]),
|
|
2510
|
+
});
|
|
2511
|
+
}
|
|
2512
|
+
_emitDisambiguationRequested({
|
|
2513
|
+
method: 'tab', plugin: null,
|
|
2514
|
+
query: { id: bareId },
|
|
2515
|
+
reason: 'ambiguous_tab',
|
|
2516
|
+
candidates: candidates,
|
|
2517
|
+
});
|
|
2518
|
+
throw NacError('ambiguous_tab',
|
|
2519
|
+
'tab id="' + bareId + '" is not in DOM and ' + exact.length +
|
|
2520
|
+
' qualified instances match the "' + bareId + '__" prefix: ' +
|
|
2521
|
+
candidates.map(function (c) { return c.nac_id; }).join(', ') +
|
|
2522
|
+
' -- pass the full qualified id',
|
|
2523
|
+
{ candidates: candidates });
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
2526
|
+
if (!tabEl) {
|
|
2527
|
+
/* No exact, no qualified instance. Build the list of all tabs
|
|
2528
|
+
in DOM so the agent / voice / assistive can re-ask. */
|
|
2529
|
+
const all = document.querySelectorAll('[data-nac-role="tab"][data-nac-id]');
|
|
2530
|
+
const available = [];
|
|
2531
|
+
for (let i = 0; i < all.length; i++) {
|
|
2532
|
+
available.push({
|
|
2533
|
+
nac_id: all[i].getAttribute('data-nac-id'),
|
|
2534
|
+
label: _candidateLabel(all[i]),
|
|
2535
|
+
});
|
|
2536
|
+
}
|
|
2537
|
+
_emitDisambiguationRequested({
|
|
2538
|
+
method: 'tab', plugin: null,
|
|
2539
|
+
query: { id: bareId },
|
|
2540
|
+
reason: 'no_matching_instance',
|
|
2541
|
+
candidates: available,
|
|
2542
|
+
});
|
|
2543
|
+
throw NacError('no_matching_instance', 'tab id="' + bareId +
|
|
2544
|
+
'" not in DOM (no exact match, no "' + bareId + '__" qualified instance)',
|
|
2545
|
+
{ candidates: available });
|
|
2546
|
+
}
|
|
2547
|
+
_focusElement(tabEl);
|
|
2548
|
+
tabEl.click();
|
|
2549
|
+
try { await wait_for('nac:tab:changed', 1500); } catch (e) { /* tolerated */ }
|
|
2550
|
+
return { ok: true };
|
|
2551
|
+
}
|
|
2411
2552
|
const root = document.querySelector('[data-nac-plugin="' + plugin + '"]');
|
|
2412
2553
|
if (!root) throw NacError('not_found', 'plugin ' + plugin + ' not mounted');
|
|
2413
2554
|
const tabEl = root.querySelector(
|
|
@@ -2435,6 +2576,20 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
2435
2576
|
if (!verb) throw NacError('invalid', 'click_by_verb requires a verb');
|
|
2436
2577
|
/* Resolve plugin: explicit arg, or active plugin if null. */
|
|
2437
2578
|
const targetPlugin = plugin || _activePlugin();
|
|
2579
|
+
/* H2 (2026-05-20): qualifier / qualifier_ordinal disambiguate
|
|
2580
|
+
among multiple DOM nodes that share a verb. Forge --ai-apply
|
|
2581
|
+
can decorate a reusable React component (e.g. <NavItem>) with
|
|
2582
|
+
a base nac_id and let the bake-time JSX add the qualifier
|
|
2583
|
+
suffix (e.g. "conduit.navigation.nav_item__sign_in"). The
|
|
2584
|
+
resolver below prefers an exact suffix match for opts.qualifier,
|
|
2585
|
+
falls back to a contains match, and supports
|
|
2586
|
+
opts.qualifier_ordinal as positional selection across all
|
|
2587
|
+
elements sharing the verb in DOM order. Back-compat: with no
|
|
2588
|
+
qualifier / qualifier_ordinal, behavior matches pre-H2. */
|
|
2589
|
+
const qualifier = opts && opts.qualifier;
|
|
2590
|
+
const qualifierOrdinal = opts && (typeof opts.qualifier_ordinal === 'number')
|
|
2591
|
+
? opts.qualifier_ordinal : null;
|
|
2592
|
+
|
|
2438
2593
|
/* Search the manifest first for an action with matching verb. */
|
|
2439
2594
|
let matched = null;
|
|
2440
2595
|
if (targetPlugin && _manifests[targetPlugin]) {
|
|
@@ -2446,22 +2601,112 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
2446
2601
|
}
|
|
2447
2602
|
}
|
|
2448
2603
|
}
|
|
2604
|
+
/* When a qualifier is provided we always fall through to the DOM
|
|
2605
|
+
scan -- the manifest's flat top-level actions[] entries don't
|
|
2606
|
+
carry per-instance suffixes; only the DOM does. */
|
|
2607
|
+
if (qualifier || qualifierOrdinal !== null) matched = null;
|
|
2608
|
+
|
|
2609
|
+
/* H1.v5 (2026-05-20): if the manifest matched but the matched.nac_id
|
|
2610
|
+
has no live DOM element, the manifest entry is a per-instance
|
|
2611
|
+
template (e.g. data-nac-id={`conduit.action.delete_article__${slug}`}).
|
|
2612
|
+
Fall through to the DOM scan so we pick whichever qualified
|
|
2613
|
+
instance is actually mounted. */
|
|
2614
|
+
if (matched && matched.nac_id) {
|
|
2615
|
+
const probe = document.querySelector('[data-nac-id="' + matched.nac_id + '"]');
|
|
2616
|
+
if (!probe) matched = null;
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2449
2619
|
if (!matched) {
|
|
2450
2620
|
/* Fallback: scan DOM within plugin scope for [data-nac-action]. */
|
|
2451
2621
|
const root = targetPlugin
|
|
2452
2622
|
? document.querySelector('[data-nac-plugin="' + targetPlugin + '"]')
|
|
2453
2623
|
: document;
|
|
2454
2624
|
if (root) {
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2625
|
+
if (qualifier) {
|
|
2626
|
+
/* H1.v7 (2026-05-21): strict resolution. The previous
|
|
2627
|
+
implementation had a second-pass `indexOf(qualifier)`
|
|
2628
|
+
fallback that resolved silently to any element whose
|
|
2629
|
+
id CONTAINED the qualifier as a substring. That
|
|
2630
|
+
violated NAC's 0-silent-damage invariant: an invalid
|
|
2631
|
+
qualifier (e.g. "article") would match an unrelated
|
|
2632
|
+
element (e.g. "...nav_item__new_article") and report
|
|
2633
|
+
the dispatch as success. Now: only EXACT id match or
|
|
2634
|
+
SUFFIX after the "__" separator counts. Zero matches
|
|
2635
|
+
throws no_matching_instance; multiple matches throw
|
|
2636
|
+
ambiguous_qualifier. No silent resolution. */
|
|
2637
|
+
const cands = root.querySelectorAll('[data-nac-action="' + verb + '"]');
|
|
2638
|
+
const exact = [];
|
|
2639
|
+
for (let i = 0; i < cands.length; i++) {
|
|
2640
|
+
const id = cands[i].getAttribute('data-nac-id') || '';
|
|
2641
|
+
if (id === qualifier || id.endsWith('__' + qualifier)) exact.push(cands[i]);
|
|
2642
|
+
}
|
|
2643
|
+
if (exact.length === 0) {
|
|
2644
|
+
/* Build the list of AVAILABLE qualifiers for this verb so
|
|
2645
|
+
the agent / voice / assistive can re-ask with one that
|
|
2646
|
+
exists. This is the "no_matching" companion to
|
|
2647
|
+
ambiguous: in both cases we surface the option set. */
|
|
2648
|
+
const available = [];
|
|
2649
|
+
for (let i = 0; i < cands.length; i++) {
|
|
2650
|
+
available.push({
|
|
2651
|
+
nac_id: cands[i].getAttribute('data-nac-id'),
|
|
2652
|
+
label: _candidateLabel(cands[i]),
|
|
2653
|
+
});
|
|
2654
|
+
}
|
|
2655
|
+
_emitDisambiguationRequested({
|
|
2656
|
+
method: 'click_by_verb', plugin: targetPlugin || null,
|
|
2657
|
+
query: { verb: verb, qualifier: qualifier },
|
|
2658
|
+
reason: 'no_matching_instance',
|
|
2659
|
+
candidates: available,
|
|
2660
|
+
});
|
|
2661
|
+
throw NacError('no_matching_instance',
|
|
2662
|
+
'No element with data-nac-action="' + verb +
|
|
2663
|
+
'" matches qualifier="' + qualifier + '" by exact id or __suffix' +
|
|
2664
|
+
' in plugin "' + (targetPlugin || '<active>') + '"',
|
|
2665
|
+
{ candidates: available });
|
|
2666
|
+
}
|
|
2667
|
+
if (exact.length > 1) {
|
|
2668
|
+
const candidates = [];
|
|
2669
|
+
for (let i = 0; i < exact.length; i++) {
|
|
2670
|
+
candidates.push({
|
|
2671
|
+
nac_id: exact[i].getAttribute('data-nac-id'),
|
|
2672
|
+
label: _candidateLabel(exact[i]),
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
_emitDisambiguationRequested({
|
|
2676
|
+
method: 'click_by_verb', plugin: targetPlugin || null,
|
|
2677
|
+
query: { verb: verb, qualifier: qualifier },
|
|
2678
|
+
reason: 'ambiguous_qualifier',
|
|
2679
|
+
candidates: candidates,
|
|
2680
|
+
});
|
|
2681
|
+
throw NacError('ambiguous_qualifier',
|
|
2682
|
+
'qualifier="' + qualifier + '" matches ' + exact.length +
|
|
2683
|
+
' elements with verb="' + verb + '": ' +
|
|
2684
|
+
candidates.map(function (c) { return c.nac_id; }).join(', ') +
|
|
2685
|
+
' -- supply a more specific qualifier or use opts.qualifier_ordinal',
|
|
2686
|
+
{ candidates: candidates });
|
|
2687
|
+
}
|
|
2688
|
+
matched = { nac_id: exact[0].getAttribute('data-nac-id'), verb: verb };
|
|
2689
|
+
} else if (qualifierOrdinal !== null) {
|
|
2690
|
+
const cands = root.querySelectorAll('[data-nac-action="' + verb + '"]');
|
|
2691
|
+
const el = cands[qualifierOrdinal];
|
|
2692
|
+
if (el && el.getAttribute('data-nac-id')) {
|
|
2693
|
+
matched = { nac_id: el.getAttribute('data-nac-id'), verb: verb };
|
|
2694
|
+
}
|
|
2695
|
+
} else {
|
|
2696
|
+
/* Default: first match in DOM order. */
|
|
2697
|
+
const el = root.querySelector('[data-nac-action="' + verb + '"]');
|
|
2698
|
+
if (el && el.getAttribute('data-nac-id')) {
|
|
2699
|
+
matched = { nac_id: el.getAttribute('data-nac-id'), verb: verb };
|
|
2700
|
+
}
|
|
2459
2701
|
}
|
|
2460
2702
|
}
|
|
2461
2703
|
}
|
|
2462
2704
|
if (!matched || !matched.nac_id) {
|
|
2705
|
+
const qInfo = qualifier ? ' (qualifier="' + qualifier + '")'
|
|
2706
|
+
: qualifierOrdinal !== null ? ' (qualifier_ordinal=' + qualifierOrdinal + ')'
|
|
2707
|
+
: '';
|
|
2463
2708
|
throw NacError('not_found',
|
|
2464
|
-
'No action with verb="' + verb + '" found in plugin "'
|
|
2709
|
+
'No action with verb="' + verb + '"' + qInfo + ' found in plugin "'
|
|
2465
2710
|
+ (targetPlugin || '<active>') + '"');
|
|
2466
2711
|
}
|
|
2467
2712
|
return await click(matched.nac_id,
|
|
@@ -2673,58 +2918,159 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
2673
2918
|
}
|
|
2674
2919
|
}
|
|
2675
2920
|
}
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2921
|
+
/* H1.v7 (2026-05-21): two-tier strict resolution. The voice /
|
|
2922
|
+
assistive use case requires fuzzy substring matching (a user
|
|
2923
|
+
saying "feed" should reach "Global Feed") -- but it must NEVER
|
|
2924
|
+
resolve silently to the first lucky match when multiple tabs
|
|
2925
|
+
satisfy the same fuzzy query. For a disabled user a wrong-tab
|
|
2926
|
+
activation without signal is exactly the failure mode the
|
|
2927
|
+
protocol forbids.
|
|
2928
|
+
|
|
2929
|
+
Tier 1: exact-normalized equality. If exactly 1 source has
|
|
2930
|
+
_norm(candidate) === _norm(query) -> resolve. If 0, try tier 2.
|
|
2931
|
+
If >1 -> ambiguous_label (multiple tabs share the same name).
|
|
2932
|
+
Tier 2: substring containment in either direction. Collect ALL
|
|
2933
|
+
tabs that match by substring. If exactly 1 -> resolve. If 0,
|
|
2934
|
+
try DOM fallback. If >1 -> ambiguous_label (the agent must
|
|
2935
|
+
re-ask with a more specific phrase). */
|
|
2936
|
+
function _candidatesFor(t) {
|
|
2937
|
+
const out = [];
|
|
2938
|
+
if (t.label) out.push(t.label);
|
|
2681
2939
|
if (t.label_i18n && typeof t.label_i18n === 'object') {
|
|
2682
2940
|
for (const k in t.label_i18n) {
|
|
2683
|
-
if (typeof t.label_i18n[k] === 'string')
|
|
2684
|
-
candidates.push(t.label_i18n[k]);
|
|
2685
|
-
}
|
|
2941
|
+
if (typeof t.label_i18n[k] === 'string') out.push(t.label_i18n[k]);
|
|
2686
2942
|
}
|
|
2687
2943
|
}
|
|
2688
|
-
if (t.nac_id)
|
|
2689
|
-
if (t.id)
|
|
2944
|
+
if (t.nac_id) out.push(t.nac_id);
|
|
2945
|
+
if (t.id) out.push(t.id);
|
|
2946
|
+
return out;
|
|
2947
|
+
}
|
|
2948
|
+
const exactMatches = [];
|
|
2949
|
+
const fuzzyMatches = [];
|
|
2950
|
+
for (let i = 0; i < tabSources.length; i++) {
|
|
2951
|
+
const t = tabSources[i];
|
|
2952
|
+
const candidates = _candidatesFor(t);
|
|
2953
|
+
let isExact = false, isFuzzy = false;
|
|
2690
2954
|
for (let j = 0; j < candidates.length; j++) {
|
|
2691
2955
|
const cand = _norm(candidates[j]);
|
|
2692
2956
|
if (!cand) continue;
|
|
2693
|
-
if (cand === lc
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
matched = { nac_id: t.nac_id || t.id };
|
|
2697
|
-
break;
|
|
2698
|
-
}
|
|
2957
|
+
if (cand === lc) { isExact = true; break; }
|
|
2958
|
+
if ((lc.length >= 3 && cand.indexOf(lc) >= 0) ||
|
|
2959
|
+
(cand.length >= 3 && lc.indexOf(cand) >= 0)) isFuzzy = true;
|
|
2699
2960
|
}
|
|
2700
|
-
|
|
2961
|
+
const id = t.nac_id || t.id;
|
|
2962
|
+
if (isExact && id) exactMatches.push({ id: id, label: t.label || id });
|
|
2963
|
+
else if (isFuzzy && id) fuzzyMatches.push({ id: id, label: t.label || id });
|
|
2964
|
+
}
|
|
2965
|
+
let matched = null;
|
|
2966
|
+
if (exactMatches.length === 1) {
|
|
2967
|
+
matched = { nac_id: exactMatches[0].id };
|
|
2968
|
+
} else if (exactMatches.length > 1) {
|
|
2969
|
+
_emitDisambiguationRequested({
|
|
2970
|
+
method: 'tab_by_label', plugin: targetPlugin,
|
|
2971
|
+
query: { label: label },
|
|
2972
|
+
reason: 'ambiguous_label_exact',
|
|
2973
|
+
candidates: exactMatches.map(function (m) {
|
|
2974
|
+
return { nac_id: m.id, label: m.label };
|
|
2975
|
+
}),
|
|
2976
|
+
});
|
|
2977
|
+
throw NacError('ambiguous_label',
|
|
2978
|
+
'tab_by_label: ' + exactMatches.length + ' tabs share the exact label "' + label + '": ' +
|
|
2979
|
+
exactMatches.map(function (m) { return m.id; }).join(', ') +
|
|
2980
|
+
' -- the manifest has duplicate labels; deduplicate or pass the nac_id directly',
|
|
2981
|
+
{ candidates: exactMatches.map(function (m) { return { nac_id: m.id, label: m.label }; }) });
|
|
2982
|
+
} else if (fuzzyMatches.length === 1) {
|
|
2983
|
+
matched = { nac_id: fuzzyMatches[0].id };
|
|
2984
|
+
} else if (fuzzyMatches.length > 1) {
|
|
2985
|
+
const candidates = fuzzyMatches.map(function (m) {
|
|
2986
|
+
return { nac_id: m.id, label: m.label };
|
|
2987
|
+
});
|
|
2988
|
+
_emitDisambiguationRequested({
|
|
2989
|
+
method: 'tab_by_label', plugin: targetPlugin,
|
|
2990
|
+
query: { label: label },
|
|
2991
|
+
reason: 'ambiguous_label_fuzzy',
|
|
2992
|
+
candidates: candidates,
|
|
2993
|
+
});
|
|
2994
|
+
throw NacError('ambiguous_label',
|
|
2995
|
+
'tab_by_label: query "' + label + '" matches ' + fuzzyMatches.length +
|
|
2996
|
+
' tabs by substring: ' + fuzzyMatches.map(function (m) {
|
|
2997
|
+
return m.id + ' (' + m.label + ')';
|
|
2998
|
+
}).join(', ') + ' -- supply a more specific phrase or the nac_id',
|
|
2999
|
+
{ candidates: candidates });
|
|
2701
3000
|
}
|
|
2702
3001
|
/* DOM fallback uses the canonical role only. Match against
|
|
2703
3002
|
aria-label and textContent with parens stripped (the parens
|
|
2704
3003
|
normalisation is legitimate -- LLMs quoting visible button
|
|
2705
3004
|
text often include the parenthetical context "(collection)"
|
|
2706
|
-
that humans wouldn't say aloud). */
|
|
3005
|
+
that humans wouldn't say aloud). Same two-tier strict logic. */
|
|
2707
3006
|
if (!matched) {
|
|
2708
3007
|
const root = document.querySelector(
|
|
2709
3008
|
'[data-nac-plugin="' + targetPlugin + '"]');
|
|
2710
3009
|
if (root) {
|
|
2711
3010
|
const tabs = Array.prototype.slice.call(
|
|
2712
3011
|
root.querySelectorAll('[data-nac-role="tab"]'));
|
|
3012
|
+
const domExact = [];
|
|
3013
|
+
const domFuzzy = [];
|
|
2713
3014
|
for (let i = 0; i < tabs.length; i++) {
|
|
2714
|
-
const txt = _norm(tabs[i].getAttribute('aria-label')
|
|
2715
|
-
|
|
2716
|
-
if (txt
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
3015
|
+
const txt = _norm(tabs[i].getAttribute('aria-label') || tabs[i].textContent || '');
|
|
3016
|
+
const id = tabs[i].getAttribute('data-nac-id');
|
|
3017
|
+
if (!txt || !id) continue;
|
|
3018
|
+
if (txt === lc) domExact.push({ id: id, label: txt });
|
|
3019
|
+
else if ((lc.length >= 3 && txt.indexOf(lc) >= 0) ||
|
|
3020
|
+
(txt.length >= 3 && lc.indexOf(txt) >= 0)) domFuzzy.push({ id: id, label: txt });
|
|
3021
|
+
}
|
|
3022
|
+
if (domExact.length === 1) {
|
|
3023
|
+
matched = { nac_id: domExact[0].id };
|
|
3024
|
+
} else if (domExact.length > 1) {
|
|
3025
|
+
const cand = domExact.map(function (m) { return { nac_id: m.id, label: m.label }; });
|
|
3026
|
+
_emitDisambiguationRequested({
|
|
3027
|
+
method: 'tab_by_label', plugin: targetPlugin,
|
|
3028
|
+
query: { label: label },
|
|
3029
|
+
reason: 'ambiguous_label_exact_dom',
|
|
3030
|
+
candidates: cand,
|
|
3031
|
+
});
|
|
3032
|
+
throw NacError('ambiguous_label',
|
|
3033
|
+
'tab_by_label DOM: ' + domExact.length + ' visible tabs share the exact label "' +
|
|
3034
|
+
label + '" -- pass the nac_id directly',
|
|
3035
|
+
{ candidates: cand });
|
|
3036
|
+
} else if (domFuzzy.length === 1) {
|
|
3037
|
+
matched = { nac_id: domFuzzy[0].id };
|
|
3038
|
+
} else if (domFuzzy.length > 1) {
|
|
3039
|
+
const cand = domFuzzy.map(function (m) { return { nac_id: m.id, label: m.label }; });
|
|
3040
|
+
_emitDisambiguationRequested({
|
|
3041
|
+
method: 'tab_by_label', plugin: targetPlugin,
|
|
3042
|
+
query: { label: label },
|
|
3043
|
+
reason: 'ambiguous_label_fuzzy_dom',
|
|
3044
|
+
candidates: cand,
|
|
3045
|
+
});
|
|
3046
|
+
throw NacError('ambiguous_label',
|
|
3047
|
+
'tab_by_label DOM: query "' + label + '" matches ' + domFuzzy.length +
|
|
3048
|
+
' visible tabs by substring: ' + domFuzzy.map(function (m) {
|
|
3049
|
+
return m.id + ' (' + m.label + ')';
|
|
3050
|
+
}).join(', ') + ' -- supply a more specific phrase or the nac_id',
|
|
3051
|
+
{ candidates: cand });
|
|
2721
3052
|
}
|
|
2722
3053
|
}
|
|
2723
3054
|
}
|
|
2724
3055
|
if (!matched || !matched.nac_id) {
|
|
3056
|
+
/* No exact, no fuzzy, no DOM match: surface ALL visible tabs as
|
|
3057
|
+
options so voice / assistive can re-prompt. */
|
|
3058
|
+
const all = document.querySelectorAll('[data-nac-role="tab"][data-nac-id]');
|
|
3059
|
+
const available = [];
|
|
3060
|
+
for (let i = 0; i < all.length; i++) {
|
|
3061
|
+
const id = all[i].getAttribute('data-nac-id');
|
|
3062
|
+
available.push({ nac_id: id, label: _candidateLabel(all[i]) });
|
|
3063
|
+
}
|
|
3064
|
+
_emitDisambiguationRequested({
|
|
3065
|
+
method: 'tab_by_label', plugin: targetPlugin,
|
|
3066
|
+
query: { label: label },
|
|
3067
|
+
reason: 'no_matching_label',
|
|
3068
|
+
candidates: available,
|
|
3069
|
+
});
|
|
2725
3070
|
throw NacError('not_found',
|
|
2726
3071
|
'No tab matching label="' + label + '" in plugin "'
|
|
2727
|
-
+ targetPlugin + '"'
|
|
3072
|
+
+ targetPlugin + '"',
|
|
3073
|
+
{ candidates: available });
|
|
2728
3074
|
}
|
|
2729
3075
|
return await tab(targetPlugin, matched.nac_id);
|
|
2730
3076
|
}
|
|
@@ -5439,8 +5785,8 @@ const _g = typeof globalThis !== 'undefined' ? globalThis : (typeof window !== '
|
|
|
5439
5785
|
|
|
5440
5786
|
global.NAC = {
|
|
5441
5787
|
__nac_v1_installed: true,
|
|
5442
|
-
version: '2.
|
|
5443
|
-
spec_version: '2.
|
|
5788
|
+
version: '2.4.0',
|
|
5789
|
+
spec_version: '2.4',
|
|
5444
5790
|
/* v2.2.0 (V22-01) -- strict validation toggle. When true,
|
|
5445
5791
|
NAC.register throws on findings (manifest_role_unknown,
|
|
5446
5792
|
tab_id_manifest_role_drift, manifest_dom_role_mismatch).
|