Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3826,14 +3826,16 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
'compliance-governance': { c:'#D97706', b:'#FFFBEB', cb:'rgba(217,119,6,0.28)' },
};

const active = { cat: '', license: '', lang: '', judgement: '', search: '', pypi: false, hf: false };
const active = { cat: '', license: '', licenseRaw: '', lang: '', judgement: '', search: '', pypi: false, hf: false };

function getVisible() {
return rows.filter(r => {
const d = r.dataset;
const catOk = !active.cat || d.cat === active.cat;
// License now filters by bucket (data-license-bucket) rather than raw string.
const licOk = !active.license || d.licenseBucket === active.license;
// A search for a specific licence (e.g. "MIT") filters by the raw SPDX id.
const licRawOk = !active.licenseRaw || (d.license || '').toLowerCase() === active.licenseRaw;
const langOk = !active.lang || d.lang === active.lang;
// Judgement: '__none__' matches not-scored (empty data-judgement).
const judgeOk = !active.judgement
Expand All @@ -3843,7 +3845,7 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
const nameEl = r.querySelector('.entry-name');
const name = nameEl ? nameEl.textContent.toLowerCase() : '';
const searchOk = !active.search || name.includes(active.search);
return catOk && licOk && langOk && judgeOk && pypiOk && hfOk && searchOk;
return catOk && licOk && licRawOk && langOk && judgeOk && pypiOk && hfOk && searchOk;
});
}

Expand Down Expand Up @@ -3962,6 +3964,17 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
bar.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
active[activeKey] = btn.dataset.value || '';
// A filter click leaves search mode: release anything a keyword search set
// (a language or raw licence) so this filter isn't AND-ed with a stuck one.
if (active._searchSetLang) {
active._searchSetLang = false;
if (activeKey !== 'lang') { active.lang = ''; const ls = document.getElementById('filter-lang'); if (ls) ls.value = ''; }
}
if (active._searchSetLic) {
active._searchSetLic = false;
if (activeKey !== 'licenseRaw') active.licenseRaw = '';
}
const si = document.getElementById('entry-search'); if (si) { si.value = ''; active.search = ''; }
applyFilters();
});
}
Expand Down Expand Up @@ -4079,6 +4092,12 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
const langSel = document.getElementById('filter-lang');
if (langSel) langSel.value = '';
}
// Same for a LICENCE filter applied by a keyword search (e.g. "MIT"):
// editing the box releases it so clearing returns to the full catalogue.
if (active._searchSetLic) {
active.licenseRaw = '';
active._searchSetLic = false;
}
currentPage = 1;
renderPage();
});
Expand All @@ -4104,6 +4123,23 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
rows.forEach(r => { if ((r.dataset.lang || '').toLowerCase() === lowerName) v = r.dataset.lang; });
return v;
},
// Lowercased raw licence ids present in the catalogue (e.g. "mit", "apache-2.0").
knownLicenses: () => {
const s = new Set();
rows.forEach(r => { const l = (r.dataset.license || '').trim().toLowerCase(); if (l) s.add(l); });
return s;
},
// Map a user's licence keyword to the raw id the cards carry. Handles common
// short forms ("apache" -> the "apache-2.0" present), else an exact id match.
licenseValue: (kw) => {
const k = kw.toLowerCase();
const known = new Set();
rows.forEach(r => { const l = (r.dataset.license || '').toLowerCase(); if (l) known.add(l); });
if (known.has(k)) return k; // exact id, e.g. "mit"
// short form → first id that starts with it, e.g. "apache" → "apache-2.0"
for (const id of known) { if (id.startsWith(k + '-') || id === k) return id; }
return '';
},
};
})();

Expand Down Expand Up @@ -4174,7 +4210,10 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
function tryKeywordRoute(q) {
const F = window.MACHFilters;
if (!F) return false; // filters not ready → fall through
const words = q.toLowerCase().split(/\s+/).filter(Boolean);
let words = q.toLowerCase().split(/\s+/).filter(Boolean);
// Allow a trailing "license"/"licence" word: "MIT license" → "mit", so a
// natural licence query still routes locally (the reported bug was 0 results).
if (words.length === 2 && /^licen[cs]e$/.test(words[1])) words = [words[0]];
if (words.length !== 1) return false; // only single-token queries route locally
const w = words[0];

Expand All @@ -4198,6 +4237,26 @@ <h2 style="margin-bottom: 0.35rem;">Suggest an entry</h2>
});
return true;
}

// 2) exact licence match → filter by the raw SPDX id (e.g. "MIT" → all MIT
// entries). Fixes "MIT"/"MIT license" returning ~0 via the semantic API.
const licVal = F.licenseValue(w); // '' if not a known licence
if (licVal) {
semanticOn = false;
F.active.search = '';
F.active.licenseRaw = licVal; // filter by raw id
F.active._searchSetLic = true; // clear it when the box is edited
F.applyFilters();

const n = F.getVisible().length;
status.hidden = false;
status.innerHTML = `Showing all <strong>${esc(licVal.toUpperCase())}</strong> entries (${n}) — filtered by licence. <a id="sem-clear">Clear</a>`;
document.getElementById('sem-clear').addEventListener('click', () => {
F.active.licenseRaw = ''; input.value = ''; F.active.search = '';
F.applyFilters(); status.hidden = true;
});
return true;
}
return false;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@ test('clicking a category filter after a search resets the view (not stuck/blank
expect(offCategory).toBe(0);
});

test('typing a licence ("MIT") filters to MIT entries, not ~0', async ({ page }) => {
// Ground truth: how many MIT-licensed entries exist (from the DOM)?
const mitTotal = await page.$$eval(CARD, (cards) =>
cards.filter((c) => c.getAttribute('data-license') === 'MIT').length
);
expect(mitTotal).toBeGreaterThan(40); // the catalogue really has many MIT entries

await page.fill(SEARCH, 'MIT');
await page.click(RUN);

// The licence route must MATCH all MIT entries via the LOCAL filter — the bug
// routed "MIT" to the semantic API, which returned ~0. We check the matched
// set (across pages), not just the visible page.
await expect.poll(() => matchedCount(page)).toBeGreaterThan(mitTotal - 5);
await expect.poll(() => visibleCount(page)).toBeGreaterThan(0);

// Every matched card is actually MIT-licensed (the filter applied correctly).
const offLicence = await page.evaluate(() =>
window.MACHFilters.getVisible().filter(
(c) => c.getAttribute('data-license') !== 'MIT'
).length
);
expect(offLicence).toBe(0);
});

test('a two-word licence query ("MIT license") also routes to the local filter', async ({ page }) => {
const mitTotal = await page.$$eval(CARD, (cards) =>
cards.filter((c) => c.getAttribute('data-license') === 'MIT').length
);
await page.fill(SEARCH, 'MIT license');
await page.click(RUN);
// "MIT license" (two words) must still route locally, not fall through to the
// semantic API — the exact reported bug ("MIT license" -> 0 results).
await expect.poll(() => matchedCount(page)).toBeGreaterThan(mitTotal - 5);
});

test('clearing the search returns to the full catalogue', async ({ page }) => {
const allMatched = await matchedCount(page); // every entry, unfiltered
await page.fill(SEARCH, 'python');
Expand Down
Loading