snow-editor

small markdown and org-mode editor
Log | Files | Refs | README

pipeline.test.js (2247B)


      1 import assert from 'node:assert';
      2 import { readFileSync } from 'node:fs';
      3 import { dirname, join } from 'node:path';
      4 import { fileURLToPath } from 'node:url';
      5 import { describe, test } from 'node:test';
      6 import { JSDOM } from 'jsdom';
      7 import DOMPurify from 'dompurify';
      8 import { enhanceOrgHtml } from './enhanceHtml.js';
      9 import { orgToHtmlSync, orgToRawHtmlSync } from './pipeline.js';
     10 import { ORG_SANITIZE_OPTIONS } from './constants.js';
     11 
     12 const __dirname = dirname(fileURLToPath(import.meta.url));
     13 const fixturesDir = join(__dirname, 'fixtures');
     14 
     15 function loadFixture(name) {
     16   return readFileSync(join(fixturesDir, name), 'utf8');
     17 }
     18 
     19 function sanitizeInNode(html) {
     20   const window = new JSDOM('').window;
     21   const purify = DOMPurify(window);
     22   return purify.sanitize(html, ORG_SANITIZE_OPTIONS);
     23 }
     24 
     25 describe('Org pipeline', () => {
     26   test('renders headings and nested sections', () => {
     27     const html = orgToRawHtmlSync(loadFixture('headings-todo.org'));
     28     assert.match(html, /<h1[^>]*>.*Top level/i);
     29     assert.match(html, /<h2[^>]*>.*Nested task/i);
     30     assert.match(html, /<h3[^>]*>.*Deep section/i);
     31   });
     32 
     33   test('renders nested lists', () => {
     34     const html = orgToRawHtmlSync(loadFixture('nested-lists.org'));
     35     assert.match(html, /<ul>/);
     36     assert.match(html, /<li>.*child one/i);
     37   });
     38 
     39   test('renders tables with cozy class', () => {
     40     const raw = enhanceOrgHtml(orgToRawHtmlSync(loadFixture('table.org')));
     41     assert.match(raw, /class="org-table"/);
     42     assert.match(raw, /<table/);
     43     assert.match(raw, /Snow/);
     44   });
     45 
     46   test('renders checklist items', () => {
     47     const html = orgToRawHtmlSync(loadFixture('checklist.org'));
     48     assert.match(html, /open task/);
     49     assert.match(html, /done task/);
     50   });
     51 
     52   test('strips script tags from export html blocks', () => {
     53     const raw = enhanceOrgHtml(orgToRawHtmlSync(loadFixture('export-html.org')));
     54     const clean = sanitizeInNode(raw);
     55     assert.match(clean, /safe/);
     56     assert.doesNotMatch(clean, /<script/i);
     57   });
     58 
     59   test('sanitize removes scripts in node environment', () => {
     60     const dirty = '<p>ok</p><script>alert(1)</script>';
     61     const clean = sanitizeInNode(dirty);
     62     assert.match(clean, /ok/);
     63     assert.doesNotMatch(clean, /<script/i);
     64   });
     65 });