test(17-06): menu layer link and client tests

- every refused scheme and spacing renders as text; protocol-relative
  URLs are anchors, never site paths or active; rel only for _blank;
  active matching rules and same-host absolute URLs; the trail marks
  ancestors only
- useMenuApi uses the server base with the forwarded visitor on the
  server and the public base in the browser
This commit is contained in:
2026-10-12 19:46:29 +02:00
parent d980f4cf2f
commit 2deac88c0e
2 changed files with 125 additions and 0 deletions

View File

@@ -135,3 +135,72 @@ test('rendered: an entry with children and a URL keeps its link and gets a label
assert.match(html, /<button type="button" class="menu-nav__subtoggle" aria-expanded="true" aria-controls="m-0-sub">.*Show submenu: Blog/)
assert.match(html, /menu-nav__item--open/)
})
// Plan 06 coverage of the D-18 link rules.
test('every refused scheme and spacing renders as text without an href', async () => {
for (const url of ['javascript:alert(1)', 'JAVASCRIPT:alert(1)', 'JaVaScRiPt:x', 'data:text/html,x', 'DATA:x', 'vbscript:msgbox(1)', 'VBScript:x', 'file:///etc/passwd', 'ftp://a.test/x', ' https://a.test', 'https://a.test/\u0000']) {
assert.equal(entryHref(entry(url)), null, url)
assert.equal(linkKind(entry(url)), 'text', url)
assert.equal(isActive(entry(url), '/', 'site.test'), false, url)
}
const html = await renderNavItem({
entry: entry('#', {
title: 'Parent',
items: [
entry('JAVASCRIPT:alert(1)', { id: 2, title: 'Upper' }),
entry('vbscript:x', { id: 3, title: 'Vb' }),
],
}),
routePath: '/',
})
assert.ok(!/href="(javascript|vbscript)/i.test(html), html)
assert.match(html, /<span class="menu-nav__text"[^>]*>Upper<\/span>/)
assert.match(html, /<span class="menu-nav__text"[^>]*>Vb<\/span>/)
})
test('protocol-relative URLs are never site paths: an anchor, never active', () => {
for (const url of ['//evil.test/x', '/\\evil.test/x', '//site.test/blog']) {
assert.equal(linkKind(entry(url)), 'anchor', url)
assert.equal(entryPath(entry(url), 'site.test'), null, url)
assert.equal(isActive(entry(url), '/x', 'site.test'), false, url)
assert.equal(isActive(entry(url), '/blog', 'site.test'), false, url)
}
})
test('rel is added only for _blank, whatever the URL kind', () => {
for (const target of ['_blank']) assert.equal(linkRel(target), 'noopener noreferrer')
for (const target of ['_self', '', undefined, '_BLANK', '_top']) assert.equal(linkRel(target as string | undefined), undefined, String(target))
})
test('active matching ignores query, hash and a trailing slash, except that / is only /', () => {
assert.equal(isActive(entry('/'), '/', 'site.test'), true)
assert.equal(isActive(entry('/'), '/?utm=x#top', 'site.test'), true)
assert.equal(isActive(entry('/'), '/blog', 'site.test'), false)
assert.equal(isActive(entry('/blog#x'), '/blog?page=2', 'site.test'), true)
assert.equal(isActive(entry('/blog///'), '/blog', 'site.test'), true)
assert.equal(isActive(entry('/Blog'), '/blog', 'site.test'), false)
assert.equal(isActive(entry('/grzyby%20le%C5%9Bne'), '/grzyby leśne/', 'site.test'), true)
assert.equal(isActive(entry('#'), '/', 'site.test'), false)
assert.equal(isActive(entry('#faq'), '/', 'site.test'), false)
})
test('an absolute URL is active only on the same host', () => {
assert.equal(isActive(entry('https://SITE.test/blog/'), '/blog', 'site.test'), true)
assert.equal(isActive(entry('http://site.test/blog'), '/blog', 'Site.Test'), true)
assert.equal(isActive(entry('https://site.test:8443/blog'), '/blog', 'site.test'), false)
assert.equal(isActive(entry('https://site.test:8443/blog'), '/blog', 'site.test:8443'), true)
assert.equal(isActive(entry('https://other.test/blog'), '/blog', 'site.test'), false)
assert.equal(isActive(entry('https://site.test/blog'), '/blog', ''), false)
assert.equal(isActive(entry('mailto:a@site.test'), '/', 'site.test'), false)
})
test('the trail marks ancestors only, never the active entry itself', () => {
const leaf = entry('/a')
assert.equal(inTrail(leaf, '/a', 'site.test'), false)
const parent = entry('/a', { items: [entry('/a/b')] })
assert.equal(inTrail(parent, '/a', 'site.test'), false)
assert.equal(inTrail(parent, '/a/b', 'site.test'), true)
const deep = entry(null, { items: [entry(null, { items: [entry(null, { items: [entry('/x/y/z/')] })] })] })
assert.equal(inTrail(deep, '/x/y/z', 'site.test'), true)
assert.equal(inTrail(entry('/a', { items: undefined as unknown as MenuEntry[] }), '/a', 'site.test'), false)
})