carbon\.apk<\/code>/, /opkg install carbon\.ipk/])
assert.match(html, pattern);
});
test('supports nested lists, quotations, emphasis, references and automatic links', () => {
const html = markdown.render('1. **First**\n - *Nested* and ~~old~~\n\n> Quote\n\n[Manual][docs]\n\n[docs]: https://example.com/guide\n\nhttps://example.com/update');
for (const pattern of [//, //, /Nested<\/em>/, /old<\/s>/, //, /href="https:\/\/example.com\/guide"/, /href="https:\/\/example.com\/update"/])
assert.match(html, pattern);
});
test('raw HTML, SVG, event handlers and code examples remain inert text', () => {
const html = markdown.render('\n\n
\n\n\n\n\n\n```html\n
\n```');
assert.doesNotMatch(html, /<(?:script|img|svg|iframe)\b/i);
assert.match(html, /<script>/);
assert.match(html, /<img src=x onerror=alert\(1\)>/);
});
test('rejects script, data, file and encoded unsafe URLs in links and images', () => {
for (const url of ['javascript:alert(1)', 'JaVaScRiPt:alert(1)', 'javascript:alert(1)', 'java
script:alert(1)', 'data:text/html;base64,PHNjcmlwdD4=', 'vbscript:msgbox(1)', 'file:///etc/passwd', 'data:image/svg+xml;base64,PHN2Zz4=']) {
const html = markdown.render(`[unsafe](${url})\n\n`);
assert.doesNotMatch(html, /<(?:a|img)\b/i, url);
}
});
test('opens safe links separately and resolves relative paths away from the router', () => {
const html = markdown.render('[Docs](docs/apk-install.md) [Web](https://example.com/?a=1&b=2) [Mail](mailto:test@example.com)');
assert.match(html, /href="https:\/\/github.com\/mrjoechen\/luci-theme-carbon\/blob\/main\/docs\/apk-install.md"/);
assert.equal((html.match(/target="_blank" rel="noopener noreferrer"/g) || []).length, 3);
assert.match(html, /a=1&b=2/);
});
test('safe images are lazy loaded without a router referrer', () => {
const html = markdown.render('');
assert.match(html, /
{
assert.equal(markdown.render(null), '');
assert.equal(markdown.render(''), '');
assert.match(markdown.render('**unfinished [link](\n\nA & B < C'), /A & B < C/);
});
test('setext headings, rules, table alignment and ordered list start', () => {
const html = markdown.render('Title\n=====\n\nSub\n---\n\n***\n\n| L | C | R |\n|:--|:-:|--:|\n| a \\| b | c | d |\n\n3. three\n4. four');
for (const pattern of [/Title<\/h1>/, /Sub<\/h2>/, /
/, /L<\/th>/, / | C<\/th>/,
/ | d<\/td>/, / | a \| b<\/td>/, //])
assert.match(html, pattern);
});
test('tight lists stay inline, loose lists get paragraphs', () => {
assert.match(markdown.render('- a\n- b'), /- a<\/li>\n
- b<\/li>/);
assert.match(markdown.render('- a\n\n- b'), /
a<\/p><\/li>\n b<\/p><\/li>/);
assert.match(markdown.render('- a\ncontinued\n- b'), / - a\ncontinued<\/li>/);
});
test('escapes, code spans and URL punctuation', () => {
assert.equal(markdown.render('\\*not em\\* `**not strong**` **strong** _em_ snake_case_name'),
'
*not em* **not strong** strong em snake_case_name \n');
assert.match(markdown.render('See https://example.com/a.'), /href="https:\/\/example.com\/a"[^>]*>https:\/\/example.com\/a<\/a>\.<\/p>/);
assert.match(markdown.render('line one \nline two'), /line one \nline two/);
});
test('deep nesting and pathological input stay bounded', () => {
const t = Date.now();
assert.match(markdown.render('>'.repeat(500) + ' deep'), /deep/);
for (const s of ['['.repeat(12000), '**'.repeat(6000), '_a '.repeat(4000), '[a]('.repeat(3000), '`'.repeat(12000)]) markdown.render(s);
assert.ok(Date.now() - t < 2000, 'renders in under 2 s');
});
test('status polling keeps the notes DOM, selection and scroll when content is unchanged', async () => {
let writes = 0;
function E(tag, attrs = {}, children) {
let html = '', text = '';
return { tag, attrs, children, hidden: false,
set innerHTML(value) { html = value; if (attrs.class === 'cb-release-notes') writes++; },
get innerHTML() { return html; },
set textContent(value) { text = value; }, get textContent() { return text; }
};
}
let current = { current: '1.1.2-r1', notes: '# First', manager: 'apk' };
const subject = new Function('view', 'rpc', 'poll', 'ui', 'markdown', 'document', 'E', 'L', 'window', '_', fs.readFileSync('htdocs/luci-static/resources/view/system/carbon-update.js', 'utf8'))(
baseclass, { declare: () => async () => current }, { add() {} }, {}, markdown,
{ documentElement: { lang: 'zh' }, querySelectorAll: () => [] }, E, { hasViewPermission: () => true }, {}, s => s);
subject.render(current);
assert.equal(writes, 1);
await subject.pollStatus();
assert.equal(writes, 1);
current = { ...current, notes: '# Second' };
await subject.pollStatus();
assert.equal(writes, 2);
});
|