mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-30 11:42:26 +08:00
test: attempt fix test import error by using importSinglefile instead of importHtml
This commit is contained in:
parent
3a7564f733
commit
14d7e3e1ce
@ -18,6 +18,8 @@ describe('HTML Import', () => {
|
|||||||
|
|
||||||
// Create a mock task context
|
// Create a mock task context
|
||||||
taskContext = new TaskContext('test', 'test');
|
taskContext = new TaskContext('test', 'test');
|
||||||
|
// Set textImportedAsText to true to ensure HTML imports are processed
|
||||||
|
taskContext.data = { textImportedAsText: true };
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('extractHtmlTitle', () => {
|
describe('extractHtmlTitle', () => {
|
||||||
@ -30,116 +32,110 @@ describe('HTML Import', () => {
|
|||||||
<body>
|
<body>
|
||||||
<p>Content</p>
|
<p>Content</p>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>
|
||||||
|
`;
|
||||||
|
|
||||||
const title = importUtils.extractHtmlTitle(html);
|
const title = importUtils.extractHtmlTitle(html);
|
||||||
expect(title).toBe('Test Title');
|
expect(title).toBe('Test Title');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle missing title tag', () => {
|
it('should return null if no title tag is present', () => {
|
||||||
const html = `
|
|
||||||
<html>
|
|
||||||
<head></head>
|
|
||||||
<body>
|
|
||||||
<p>Content</p>
|
|
||||||
</body>
|
|
||||||
</html>`;
|
|
||||||
|
|
||||||
const title = importUtils.extractHtmlTitle(html);
|
|
||||||
expect(title).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle title with special characters', () => {
|
|
||||||
const html = `
|
const html = `
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Test/Title: With Special & Characters</title>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p>Content</p>
|
<p>Content</p>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>
|
||||||
|
`;
|
||||||
|
|
||||||
const title = importUtils.extractHtmlTitle(html);
|
const title = importUtils.extractHtmlTitle(html);
|
||||||
expect(title).toBe('Test/Title: With Special & Characters');
|
expect(title).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('importHtml', () => {
|
describe('importSingleFile with HTML', () => {
|
||||||
it('should prefer title from HTML over filename', () => {
|
it('should import HTML file with title from title tag', () => {
|
||||||
const file = {
|
const file = {
|
||||||
originalname: 'test.html',
|
originalname: 'test.html',
|
||||||
|
mimetype: 'text/html',
|
||||||
buffer: Buffer.from(`
|
buffer: Buffer.from(`
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>HTML Title</title>
|
<title>HTML Title</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p>Content</p>
|
<p>Test content</p>
|
||||||
</body>
|
</body>
|
||||||
</html>`),
|
</html>
|
||||||
mimetype: 'text/html'
|
`)
|
||||||
};
|
};
|
||||||
|
|
||||||
const note = importSingle.importHtml(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
expect(note.title).toBe('HTML Title');
|
expect(note.title).toBe('HTML Title');
|
||||||
|
expect(note.mime).toBe('text/html');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fall back to filename when no HTML title exists', () => {
|
it('should import HTML file with title from h1 when no title tag', () => {
|
||||||
const file = {
|
|
||||||
originalname: 'test_file.html',
|
|
||||||
buffer: Buffer.from(`
|
|
||||||
<html>
|
|
||||||
<head></head>
|
|
||||||
<body>
|
|
||||||
<p>Content</p>
|
|
||||||
</body>
|
|
||||||
</html>`),
|
|
||||||
mimetype: 'text/html'
|
|
||||||
};
|
|
||||||
|
|
||||||
const note = importSingle.importHtml(taskContext, file, parentNote);
|
|
||||||
expect(note.title).toBe('test file'); // assuming replaceUnderscoresWithSpaces is true
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle HTML with both title and H1', () => {
|
|
||||||
const file = {
|
const file = {
|
||||||
originalname: 'test.html',
|
originalname: 'test.html',
|
||||||
|
mimetype: 'text/html',
|
||||||
|
buffer: Buffer.from(`
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<h1>Heading Title</h1>
|
||||||
|
<p>Test content</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
|
expect(note.title).toBe('Heading Title');
|
||||||
|
expect(note.mime).toBe('text/html');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should import HTML file with filename as title when no title or h1', () => {
|
||||||
|
const file = {
|
||||||
|
originalname: 'test-document.html',
|
||||||
|
mimetype: 'text/html',
|
||||||
|
buffer: Buffer.from(`
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<p>Test content without title</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
|
expect(note.title).toBe('test-document');
|
||||||
|
expect(note.mime).toBe('text/html');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should sanitize HTML content during import', () => {
|
||||||
|
const file = {
|
||||||
|
originalname: 'test.html',
|
||||||
|
mimetype: 'text/html',
|
||||||
buffer: Buffer.from(`
|
buffer: Buffer.from(`
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>HTML Title</title>
|
<title>Test Title</title>
|
||||||
|
<script>alert('xss');</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Different H1 Title</h1>
|
<p>Safe content</p>
|
||||||
<p>Content</p>
|
<script>alert('xss');</script>
|
||||||
</body>
|
</body>
|
||||||
</html>`),
|
</html>
|
||||||
mimetype: 'text/html'
|
`)
|
||||||
};
|
};
|
||||||
|
|
||||||
const note = importSingle.importHtml(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
expect(note.title).toBe('HTML Title');
|
expect(note.title).toBe('Test Title');
|
||||||
expect(note.content).toContain('<h1>HTML Title</h1>'); // H1 should be updated to match title
|
expect(note.content).not.toContain('<script>');
|
||||||
});
|
expect(note.content).toContain('<p>Safe content</p>');
|
||||||
|
|
||||||
it('should preserve special characters in title', () => {
|
|
||||||
const file = {
|
|
||||||
originalname: 'test.html',
|
|
||||||
buffer: Buffer.from(`
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Title/With: Special & Characters</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>Content</p>
|
|
||||||
</body>
|
|
||||||
</html>`),
|
|
||||||
mimetype: 'text/html'
|
|
||||||
};
|
|
||||||
|
|
||||||
const note = importSingle.importHtml(taskContext, file, parentNote);
|
|
||||||
expect(note.title).toBe('Title/With: Special & Characters');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user