27 lines
1.8 KiB
JavaScript
27 lines
1.8 KiB
JavaScript
import {test} from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {readFile} from 'node:fs/promises';
|
|
import ts from 'typescript';
|
|
|
|
const code=ts.transpileModule(await readFile(new URL('../src/lib/footer-links.ts',import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText;
|
|
const {footerPolicyLinks,footerLinkHref,footerLinkCurrent}=await import('data:text/javascript;base64,'+Buffer.from(code).toString('base64'));
|
|
|
|
test('footer links use rich text paths and drop unsafe addresses',()=>{
|
|
assert.equal(footerLinkHref('/richetext/关于我们'),'/richtext/'+encodeURIComponent('关于我们')+'/');
|
|
assert.equal(footerLinkHref('/richtext/'+encodeURIComponent('用户协议')+'/'),'/richtext/'+encodeURIComponent('用户协议')+'/');
|
|
assert.equal(footerLinkHref('常见问题'),'/richtext/'+encodeURIComponent('常见问题')+'/');
|
|
assert.equal(footerLinkHref('https://example.com/rules'),'https://example.com/rules');
|
|
assert.equal(footerLinkHref('javascript:alert(1)'),'');
|
|
assert.equal(footerLinkHref('/richtext/'),'');
|
|
const links=footerPolicyLinks({footerInfo:{navList:[
|
|
{id:1,name:'规则',urlValue:'/ignored/',translationList:[{id:2,name:'关于我们',urlValue:'/richetext/关于我们'},{id:3,name:'脚本',urlValue:'javascript:alert(1)'}]},
|
|
{id:5,name:'用户协议',urlValue:'用户协议'},
|
|
]}});
|
|
assert.deepEqual(links.map(link=>[link.name,link.href,link.external]),[
|
|
['关于我们','/richtext/'+encodeURIComponent('关于我们')+'/',false],
|
|
['用户协议','/richtext/'+encodeURIComponent('用户协议')+'/',false],
|
|
]);
|
|
assert.equal(footerLinkCurrent('/richtext/'+encodeURIComponent('关于我们')+'/?x=1','/richtext/关于我们/'),true);
|
|
assert.equal(footerLinkCurrent('/','/richtext/关于我们/'),false);
|
|
});
|