// Body link utilities (UI-SPEC L7 steps 2 to 4, D-82, D-83, D-71). The input // is always sanitize-html output (normalized tags, escaped text and // attribute values), as the journal layer's gate hands it over. import assert from 'node:assert/strict' import test from 'node:test' import { applyOutbound, autolink, imageAttrs } from '../app/utils/bodyLinks.ts' const LINKS = [ { phrase: 'lwia grzywa', url: '/blog/lwia-grzywa' }, { phrase: 'reishi', url: 'https://example.test/blog/reishi' }, ] test('autolink: first occurrence only, case-insensitive, a plain ', () => { const html = '

Lwia grzywa to grzyb. Lwia grzywa rośnie na drzewach.

' assert.equal(autolink(html, LINKS, 5, '/blog/inne'), '

Lwia grzywa to grzyb. Lwia grzywa rośnie na drzewach.

') }) test('autolink: whole words only', () => { assert.equal(autolink('

reishiowy napar

', LINKS, 5, '/x'), '

reishiowy napar

') assert.equal(autolink('

superreishi

', LINKS, 5, '/x'), '

superreishi

') assert.equal(autolink('

(reishi)

', LINKS, 5, '/x'), '

(reishi)

') assert.equal(autolink('

Żółć reishi.

', LINKS, 5, '/x'), '

Żółć reishi.

') }) test('autolink: at most max links in the whole text, each phrase once', () => { const html = '

reishi i lwia grzywa

reishi znowu

' assert.equal(autolink(html, LINKS, 1, '/x'), '

reishi i lwia grzywa

reishi znowu

') assert.equal(autolink(html, LINKS, 0, '/x'), html) assert.equal( autolink(html, LINKS, 5, '/x'), '

reishi i lwia grzywa

reishi znowu

', ) }) test('autolink: never inside a, headings, code, pre, figcaption or button', () => { for (const tag of ['a href="/y"', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'code', 'pre', 'figcaption', 'button']) { const name = tag.split(' ')[0] const html = `<${tag}>reishi reishi

reishi

` assert.equal(autolink(html, LINKS, 5, '/x'), `<${tag}>reishi reishi

reishi

`, name) } }) test('autolink: never links the page to itself', () => { const html = '

lwia grzywa

' assert.equal(autolink(html, LINKS, 5, '/blog/lwia-grzywa'), html) assert.equal(autolink(html, LINKS, 5, '/blog/lwia-grzywa/'), html) assert.equal(autolink(html, LINKS, 5, 'https://example.test/blog/lwia-grzywa'), html) const self = [{ phrase: 'reishi', url: 'https://example.test/blog/reishi' }] assert.equal(autolink('

reishi

', self, 5, '/blog/reishi'), '

reishi

') assert.equal(autolink('

reishi

', self, 5, 'https://other.test/blog/reishi'), '

reishi

') }) test('autolink: regex characters in a phrase match literally', () => { const links = [{ phrase: 'C++ (beta).', url: '/c' }, { phrase: 'a.b', url: '/ab' }] assert.equal(autolink('

We use C++ (beta). today, axb too

', links, 5, '/x'), '

We use C++ (beta). today, axb too

') }) test('autolink: escaped text and entities are matched, never split', () => { const links = [{ phrase: 'Tom & Jerry', url: '/tj' }, { phrase: 'amp', url: '/amp' }, { phrase: 'lt', url: '/lt' }] assert.equal(autolink('

Tom & Jerry <3

', links, 5, '/x'), '

Tom & Jerry <3

') assert.equal(autolink('

&amp <lt

', links, 5, '/x'), '

&amp <lt

') }) test('autolink: markup is only added between tags, never inside an attribute', () => { const html = '

x

reishi

reishi

' assert.equal(autolink(html, LINKS, 5, '/x'), '

x

reishi

reishi

') const two = [{ phrase: 'reishi', url: '/r' }, { phrase: 'href', url: '/h' }] assert.equal(autolink('

reishi href

', two, 5, '/x'), '

reishi href

') }) test('autolink: URLs are validated and escaped (T-33-39)', () => { const bad = [ { phrase: 'one', url: 'javascript:alert(1)' }, { phrase: 'two', url: '//evil.test/x' }, { phrase: 'three', url: 'data:text/html,x' }, { phrase: 'four', url: 'relative/path' }, { phrase: '', url: '/empty' }, ] assert.equal(autolink('

one two three four

', bad, 5, '/x'), '

one two three four

') const quoted = [{ phrase: 'q', url: '/a">?b=1&c=2' }] assert.equal(autolink('

q

', quoted, 5, '/x'), '

q

') }) test('autolink: no links or empty HTML returns the input', () => { assert.equal(autolink('', LINKS, 5, '/x'), '') assert.equal(autolink('

reishi

', [], 5, '/x'), '

reishi

') assert.equal(autolink('

reishi

', null as never, 5, '/x'), '

reishi

') }) const OUT = { siteHost: 'example.test', nofollowDomains: ['partner.test'], sponsoredDomains: ['ads.test'] } const HINT = '(opens in a new tab)' test('applyOutbound: new tab on adds target, noopener and the hidden hint', () => { assert.equal( applyOutbound('

Other

', { ...OUT, newTab: true, hint: HINT }), '

Other (opens in a new tab)

', ) }) test('applyOutbound: new tab on with the hint off adds target and noopener only', () => { assert.equal( applyOutbound('Other', { ...OUT, newTab: true, hint: '' }), 'Other', ) }) test('applyOutbound: new tab off keeps editor rel and domain-list rel, no target, noopener or hint', () => { assert.equal(applyOutbound('Other', { ...OUT, newTab: false, hint: HINT }), 'Other') assert.equal(applyOutbound('P', { ...OUT, newTab: false, hint: HINT }), 'P') assert.equal(applyOutbound('A', { ...OUT, newTab: false, hint: HINT }), 'A') }) test('applyOutbound: editor rel kept and merged with the domain lists and noopener', () => { assert.equal( applyOutbound('P', { ...OUT, newTab: true, hint: '' }), 'P', ) assert.equal( applyOutbound('P', { ...OUT, newTab: true, hint: '' }), 'P', ) }) test('applyOutbound: a domain list matches the domain and its subdomains, not look-alikes', () => { assert.equal(applyOutbound('N', { ...OUT, newTab: false }), 'N') assert.equal(applyOutbound('N', { ...OUT, newTab: false }), 'N') }) test('applyOutbound: internal, relative and non-http links are unchanged', () => { const opts = { ...OUT, newTab: true, hint: HINT } for (const html of [ 'In', 'In', 'In', 'In', 'Mail', 'R', 'No href', ]) { assert.equal(applyOutbound(html, opts), html, html) } }) test('applyOutbound: protocol-relative links to another host are outbound', () => { assert.equal(applyOutbound('O', { ...OUT, newTab: true, hint: '' }), 'O') }) test('applyOutbound: the hint text is escaped and lands before the anchor closes', () => { assert.equal( applyOutbound('B t

after

', { ...OUT, newTab: true, hint: '&' }), 'B t <b>&

after

', ) }) test('applyOutbound: entity-encoded hrefs are read decoded and kept as written', () => { assert.equal( applyOutbound('P', { ...OUT, newTab: false }), 'P', ) }) test('applyOutbound: the site host may carry a port and www is not stripped', () => { assert.equal(applyOutbound('L', { siteHost: 'localhost:3000', newTab: true }), 'L') assert.equal(applyOutbound('W', { ...OUT, newTab: true }), 'W') }) test('imageAttrs: lazy loading and async decoding, stored attributes and alt="" kept', () => { assert.equal(imageAttrs('

'), '

') assert.equal(imageAttrs('A cat'), 'A cat') assert.equal(imageAttrs(''), '') assert.equal(imageAttrs('

x

'), '

x

') assert.equal(imageAttrs(''), '') })