import 'mocha'; import * as assert from 'assert'; import { DeltaInsertOp } from './../src/DeltaInsertOp'; import { QuillDeltaToHtmlConverter } from './../src/QuillDeltaToHtmlConverter'; import { callWhenXEqualY } from './_helper'; import { delta1 } from './data/delta1'; import { GroupType, ListType } from './../src/value-types'; import { encodeHtml } from './../src/funcs-html'; describe('QuillDeltaToHtmlConverter', function () { describe('constructor()', function () { var hugeOps = [ { insert: 'huge', attributes: { size: 'huge', attr1: 'red' } }, { insert: '\n' }, ]; it('should instantiate return proper html', function () { var qdc = new QuillDeltaToHtmlConverter(delta1.ops, { classPrefix: 'noz', }); var html = qdc.convert(); assert.equal(html, delta1.html); }); it('should set default inline styles for `inlineStyles: true`', function () { var qdc = new QuillDeltaToHtmlConverter(hugeOps, { inlineStyles: true, customCssStyles: (op) => { if (op.attributes['attr1'] === 'red') { return ['color:red']; } }, }); var html = qdc.convert(); assert.equal( html.includes('huge'), true, html ); }); it('should set default inline styles when `inlineStyles` is a truthy non-object', function () { var qdc = new QuillDeltaToHtmlConverter(hugeOps, { inlineStyles: 1, } as any); var html = qdc.convert(); assert.equal( html.includes('huge'), true, html ); }); it('should allow setting inline styles', function () { var qdc = new QuillDeltaToHtmlConverter(hugeOps, { inlineStyles: { size: { huge: 'font-size: 6em', }, }, }); var html = qdc.convert(); assert.equal( html.includes('huge'), true, html ); }); }); describe('convert()', function () { var ops2 = [ { insert: 'this is text' }, { insert: '\n' }, { insert: 'this is code' }, { insert: '\n', attributes: { 'code-block': true } }, { insert: 'this is code TOO!' }, { insert: '\n', attributes: { 'code-block': true } }, ]; it('should render html', function () { var qdc = new QuillDeltaToHtmlConverter(ops2); var html = qdc.convert(); assert.equal(html.indexOf('
this is code') > -1, true, html);
    });

    it('should render mention', function () {
      let ops = [
        {
          insert: 'mention',
          attributes: {
            mentions: true,
            mention: {
              'end-point': 'http://abc.com',
              slug: 'a',
              class: 'abc',
              target: '_blank',
            },
          },
        },
      ];
      var qdc = new QuillDeltaToHtmlConverter(ops);
      var html = qdc.convert();
      assert.equal(
        html,
        [
          '

mention

', ].join('') ); var qdc = new QuillDeltaToHtmlConverter([ { insert: 'mention', attributes: { mentions: true, mention: { slug: 'aa' }, }, }, ]); var html = qdc.convert(); assert.equal( html, ['

mention

'].join('') ); }); it('should render links with rels', function () { var ops = [ { attributes: { link: '#', rel: 'nofollow noopener', }, insert: 'external link', }, { attributes: { link: '#', }, insert: 'internal link', }, ]; var qdc = new QuillDeltaToHtmlConverter(ops, { linkRel: 'license', }); var html = qdc.convert(); assert.equal( html, '

external linkinternal link

' ); qdc = new QuillDeltaToHtmlConverter(ops); html = qdc.convert(); assert.equal( html, '

external linkinternal link

' ); }); it('should render image and image links', function () { let ops = [ { insert: { image: 'http://yahoo.com/abc.jpg' } }, { insert: { image: 'http://yahoo.com/def.jpg' }, attributes: { link: 'http://aha' }, }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); let html = qdc.convert(); assert.equal( html, [ '

', '', '', '', '', '

', ].join('') ); }); it('should open and close list tags', function () { var ops4 = [ { insert: 'mr\n' }, { insert: 'hello' }, { insert: '\n', attributes: { list: { list: 'ordered' } } }, { insert: 'there' }, { insert: '\n', attributes: { list: { list: 'bullet' } } }, { insert: '\n', attributes: { list: { list: 'ordered' } } }, ]; var qdc = new QuillDeltaToHtmlConverter(ops4); var html = qdc.convert(); assert.equal(html.indexOf('

mr') > -1, true); assert.equal(html.indexOf('') > -1, true); }); it('should render as separate paragraphs', function () { var ops4 = [{ insert: 'hello\nhow areyou?\n\nbye' }]; var qdc = new QuillDeltaToHtmlConverter(ops4, { multiLineParagraph: false, }); var html = qdc.convert(); assert.equal( html, '

hello

how areyou?


bye

' ); }); it('should create checked/unchecked lists', function () { var ops4 = [ { insert: 'hello' }, { insert: '\n', attributes: { list: { list: 'checked' } } }, { insert: 'there' }, { insert: '\n', attributes: { list: { list: 'unchecked' } } }, { insert: 'man' }, { insert: '\n', attributes: { list: { list: 'checked' } } }, { insert: 'not done' }, { insert: '\n', attributes: { indent: 1, list: { list: 'unchecked' } }, }, ]; var qdc = new QuillDeltaToHtmlConverter(ops4); var html = qdc.convert(); assert.equal( html, [ '', ].join('') ); }); it('should wrap positional styles in right tag', function () { var ops4 = [ { insert: 'mr' }, { insert: '\n', attributes: { align: 'center' } }, { insert: '\n', attributes: { direction: 'rtl' } }, { insert: '\n', attributes: { indent: 2 } }, ]; var qdc = new QuillDeltaToHtmlConverter(ops4, { paragraphTag: 'div' }); var html = qdc.convert(); assert.equal(html.indexOf('
-1, true); assert.equal(html.indexOf('
-1, true); assert.equal(html.indexOf('

-1, true); }); it('should render target attr correctly', () => { let ops = [ { attributes: { target: '_self', link: 'http://#' }, insert: 'A' }, { attributes: { target: '_blank', link: 'http://#' }, insert: 'B' }, { attributes: { link: 'http://#' }, insert: 'C' }, { insert: '\n' }, ]; let qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '' }); let html = qdc.convert(); assert.equal( html, [ `

A`, `B`, `C

`, ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops); html = qdc.convert(); assert.equal( html, [ `

A`, `B`, `C

`, ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '_top' }); html = qdc.convert(); assert.equal( html, [ `

A`, `B`, `C

`, ].join('') ); }); it('should convert using custom url sanitizer', () => { let ops = [ { attributes: { link: 'http://yahoo<%=abc%>/ed' }, insert: 'test' }, { attributes: { link: 'http://abc<' }, insert: 'hi' }, ]; let qdc = new QuillDeltaToHtmlConverter(ops, { urlSanitizer: (link: string) => { if (link.indexOf('<%') > -1) { return link; } return undefined; }, }); assert.equal( qdc.convert(), [ `

test`, `hi

`, ].join('') ); }); // it('should render empty table', () => { // let ops = [ // { // insert: '\n\n\n', // attributes: { // table: 'row-1', // }, // }, // { // attributes: { // table: 'row-2', // }, // insert: '\n\n\n', // }, // { // attributes: { // table: 'row-3', // }, // insert: '\n\n\n', // }, // { // insert: '\n', // }, // ]; // let qdc = new QuillDeltaToHtmlConverter(ops); // assert.equal( // qdc.convert(), // [ // ``, // ``, // ``, // ``, // `









`, // `


`, // ].join('') // ); // }); // it('should render singe cell table', () => { // let ops = [ // { // insert: 'cell', // }, // { // insert: '\n', // attributes: { // table: 'row-1', // }, // }, // ]; // let qdc = new QuillDeltaToHtmlConverter(ops); // assert.equal( // qdc.convert(), // [ // ``, // ``, // `
cell
`, // ].join('') // ); // }); // it('should render filled table', () => { // let ops = [ // { // insert: '11', // }, // { // attributes: { // table: 'row-1', // }, // insert: '\n', // }, // { // insert: '12', // }, // { // attributes: { // table: 'row-1', // }, // insert: '\n', // }, // { // insert: '13', // }, // { // attributes: { // table: 'row-1', // }, // insert: '\n', // }, // { // insert: '21', // }, // { // attributes: { // table: 'row-2', // }, // insert: '\n', // }, // { // insert: '22', // }, // { // attributes: { // table: 'row-2', // }, // insert: '\n', // }, // { // insert: '23', // }, // { // attributes: { // table: 'row-2', // }, // insert: '\n', // }, // { // insert: '31', // }, // { // attributes: { // table: 'row-3', // }, // insert: '\n', // }, // { // insert: '32', // }, // { // attributes: { // table: 'row-3', // }, // insert: '\n', // }, // { // insert: '33', // }, // { // attributes: { // table: 'row-3', // }, // insert: '\n', // }, // { // insert: '\n', // }, // ]; // let qdc = new QuillDeltaToHtmlConverter(ops); // assert.equal( // qdc.convert(), // [ // ``, // ``, // ``, // ``, // `
111213
212223
313233
`, // `


`, // ].join('') // ); // }); // test case for cu-table it('should render empty cu-table', () => { let ops = [ { attributes: { 'table-col': { width: '150' }, }, insert: '\n\n\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-383p7o', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-kdrmch', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-ljjq9j', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-lj7h6y', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-l0enbj', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-xa4lb9', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-2w5wjp', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-o7q92h', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-4nujo2', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); assert.equal( qdc.convert(), [ `
`, ``, ``, ``, ``, ``, ``, ``, ``, ``, '', ``, ``, ``, ``, '', ``, ``, ``, ``, '', ``, `










`, `
`, ].join('') ); }); it('should render single cell cu-table', () => { let ops = [ { attributes: { 'table-col': { width: '150' }, }, insert: '\n', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-rcydzq', cell: 'cell-fqmunc', }, row: 'row-rcydzq', rowspan: '1', colspan: '1', }, insert: '\n', }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); assert.equal( qdc.convert(), [ `
`, ``, ``, ``, ``, ``, ``, ``, ``, ``, `
`, `


`, `
`, ].join('') ); }); it('should render filled cu-table', () => { let ops = [ { attributes: { 'table-col': { width: '150' }, }, insert: '\n\n', }, { attributes: { 'table-col': { width: '200' }, }, insert: '\n', }, { insert: 'cell-1-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-383p7o', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-1-line-2', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-383p7o', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-2-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-kdrmch', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-3-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-dvagmt', cell: 'cell-ljjq9j', }, row: 'row-dvagmt', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-4-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-lj7h6y', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-5-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-l0enbj', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-6-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-929dk8', cell: 'cell-xa4lb9', }, row: 'row-929dk8', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-7-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-2w5wjp', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-8-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-o7q92h', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, { insert: 'cell-9-line-1', }, { attributes: { 'table-cell-line': { rowspan: '1', colspan: '1', row: 'row-j95olh', cell: 'cell-4nujo2', }, row: 'row-j95olh', rowspan: '1', colspan: '1', }, insert: '\n', }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); assert.equal( qdc.convert(), [ `
`, ``, ``, ``, ``, ``, ``, ``, ``, ``, '', ``, ``, ``, ``, '', ``, ``, ``, ``, '', ``, `

cell-1-line-1

cell-1-line-2

cell-2-line-1

cell-3-line-1

cell-4-line-1

cell-5-line-1

cell-6-line-1

cell-7-line-1

cell-8-line-1

cell-9-line-1

`, `
`, ].join('') ); }); }); describe('custom types', () => { it(`should return empty string if renderer not defined for custom blot`, () => { let ops = [{ insert: { customstuff: 'my val' } }]; let qdc = new QuillDeltaToHtmlConverter(ops); assert.equal(qdc.convert(), '

'); }); it('should render custom insert types with given renderer', () => { let ops = [ { insert: { bolditalic: 'my text' } }, { insert: { blah: 1 } }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); qdc.renderCustomWith((op) => { if (op.insert.type === 'bolditalic') { return '' + op.insert.value + ''; } return 'unknown'; }); let html = qdc.convert(); assert.equal(html, '

my textunknown

'); }); it('should render custom insert types as blocks if renderAsBlock is specified', () => { let ops = [ { insert: 'hello ' }, { insert: { myblot: 'my friend' } }, { insert: '!' }, { insert: { myblot: 'how r u?' }, attributes: { renderAsBlock: true } }, ]; let qdc = new QuillDeltaToHtmlConverter(ops); qdc.renderCustomWith((op) => { if (op.insert.type === 'myblot') { return op.attributes.renderAsBlock ? '
' + op.insert.value + '
' : op.insert.value; } return 'unknown'; }); let html = qdc.convert(); assert.equal(html, '

hello my friend!

how r u?
'); }); it('should render custom insert types in code blocks with given renderer', () => { let ops = [ { insert: { colonizer: ':' } }, { insert: '\n', attributes: { 'code-block': true } }, { insert: 'code1' }, { insert: '\n', attributes: { 'code-block': true } }, { insert: { colonizer: ':' } }, { insert: '\n', attributes: { 'code-block': true } }, ]; let renderer = (op: DeltaInsertOp) => { if (op.insert.type === 'colonizer') { return op.insert.value; } return ''; }; let qdc = new QuillDeltaToHtmlConverter(ops.slice(0, 2)); qdc.renderCustomWith(renderer); assert.equal(qdc.convert(), '
:
'); qdc = new QuillDeltaToHtmlConverter(ops); qdc.renderCustomWith(renderer); assert.equal(qdc.convert(), '
:\ncode1\n:
'); qdc = new QuillDeltaToHtmlConverter(ops, { customTag: (format) => { if (format === 'code-block') { return 'code'; } }, }); qdc.renderCustomWith(renderer); assert.equal(qdc.convert(), ':\ncode1\n:'); }); it('should render custom insert types in headers with given renderer', () => { let ops = [ { insert: { colonizer: ':' } }, { insert: '\n', attributes: { header: 1 } }, { insert: 'hello' }, { insert: '\n', attributes: { header: 1 } }, { insert: { colonizer: ':' } }, { insert: '\n', attributes: { header: 1 } }, ]; let renderer = (op: DeltaInsertOp) => { if (op.insert.type === 'colonizer') { return op.insert.value; } return ''; }; let qdc = new QuillDeltaToHtmlConverter(ops.slice(0, 2)); qdc.renderCustomWith(renderer); assert.equal(qdc.convert(), '

:

'); qdc = new QuillDeltaToHtmlConverter(ops); qdc.renderCustomWith(renderer); assert.equal(qdc.convert(), '

:
hello
:

'); }); }); describe('_getListTag()', function () { it('should return proper list tag', function () { var op = new DeltaInsertOp('\n', { list: { list: ListType.Ordered } }); var qdc = new QuillDeltaToHtmlConverter(delta1.ops); assert.equal(qdc._getListTag(op), 'ol'); var op = new DeltaInsertOp('\n', { list: { list: ListType.Bullet } }); assert.equal(qdc._getListTag(op), 'ul'); var op = new DeltaInsertOp('\n', { list: { list: ListType.Checked } }); assert.equal(qdc._getListTag(op), 'ul'); var op = new DeltaInsertOp('\n', { list: { list: ListType.Unchecked } }); assert.equal(qdc._getListTag(op), 'ul'); var op = new DeltaInsertOp('d'); assert.equal(qdc._getListTag(op), ''); }); }); describe(' prepare data before inline and block renders', function () { var ops: any; beforeEach(function () { ops = [ { insert: 'Hello' }, { insert: ' my ', attributes: { italic: true } }, { insert: '\n', attributes: { italic: true } }, { insert: ' name is joey' }, ].map((v: any) => new DeltaInsertOp(v.insert, v.attributes)); }); describe('renderInlines()', function () { it('should render inlines', function () { var qdc = new QuillDeltaToHtmlConverter([]); var inlines = qdc._renderInlines(ops); assert.equal( inlines, ['

Hello', ' my
name is joey

'].join('') ); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: 'div' }); var inlines = qdc._renderInlines(ops); assert.equal( inlines, '
Hello my
name is joey
' ); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: '' }); var inlines = qdc._renderInlines(ops); assert.equal(inlines, 'Hello my
name is joey'); }); it('should render inlines custom tag', function () { var qdc = new QuillDeltaToHtmlConverter([], { customTag: (format) => { if (format === 'italic') { return 'i'; } }, }); var inlines = qdc._renderInlines(ops); assert.equal( inlines, ['

Hello', ' my
name is joey

'].join('') ); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: 'div' }); var inlines = qdc._renderInlines(ops); assert.equal( inlines, '
Hello my
name is joey
' ); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: '' }); var inlines = qdc._renderInlines(ops); assert.equal(inlines, 'Hello my
name is joey'); }); it('should render plain new line string', function () { var ops = [new DeltaInsertOp('\n')]; var qdc = new QuillDeltaToHtmlConverter([]); assert.equal(qdc._renderInlines(ops), '


'); }); it('should render styled new line string', function () { var ops = [new DeltaInsertOp('\n', { font: 'arial' })]; var qdc = new QuillDeltaToHtmlConverter([]); assert.equal(qdc._renderInlines(ops), '


'); var qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: '' }); assert.equal(qdc._renderInlines(ops), '
'); }); it('should render when first line is new line', function () { var ops = [new DeltaInsertOp('\n'), new DeltaInsertOp('aa')]; var qdc = new QuillDeltaToHtmlConverter([]); assert.equal(qdc._renderInlines(ops), '


aa

'); }); it('should render when last line is new line', function () { var ops = [new DeltaInsertOp('aa'), new DeltaInsertOp('\n')]; var qdc = new QuillDeltaToHtmlConverter([]); assert.equal(qdc._renderInlines(ops), '

aa

'); }); it('should render mixed lines', function () { var ops = [new DeltaInsertOp('aa'), new DeltaInsertOp('bb')]; var nlop = new DeltaInsertOp('\n'); var stylednlop = new DeltaInsertOp('\n', { color: '#333', italic: true, }); var qdc = new QuillDeltaToHtmlConverter([]); assert.equal(qdc._renderInlines(ops), '

aabb

'); var ops0 = [nlop, ops[0], nlop, ops[1]]; assert.equal(qdc._renderInlines(ops0), '


aa
bb

'); var ops4 = [ops[0], stylednlop, stylednlop, stylednlop, ops[1]]; assert.equal( qdc._renderInlines(ops4), ['

aa


bb

'].join('') ); }); }); describe('renderBlock()', function () { var op = new DeltaInsertOp('\n', { header: 3, indent: 2 }); var inlineop = new DeltaInsertOp('hi there'); it('should render container block', function () { var qdc = new QuillDeltaToHtmlConverter([]); var blockhtml = qdc._renderBlock(op, [inlineop]); assert.equal( blockhtml, ['

', 'hi there

'].join('') ); var qdc = new QuillDeltaToHtmlConverter([]); var blockhtml = qdc._renderBlock(op, []); assert.equal( blockhtml, ['

', '

'].join('') ); }); it('should correctly render code block', function () { let ops = [ { insert: 'line 1', }, { attributes: { 'code-block': { 'code-block': true }, }, insert: '\n', }, { insert: 'line 2', }, { attributes: { 'code-block': { 'code-block': true }, }, insert: '\n', }, { insert: 'line 3', }, { attributes: { 'code-block': { 'code-block': 'javascript' }, }, insert: '\n', }, { insert: '

line 4

', }, { attributes: { 'code-block': true, }, insert: '\n', }, { insert: 'line 5', }, { attributes: { 'code-block': { 'code-block': 'ja"va' }, }, insert: '\n', }, ]; //console.log(encodeHtml("

line 4

")); var qdc = new QuillDeltaToHtmlConverter(ops); let html = qdc.convert(); assert.equal( html, [ '
line 1\nline 2
', '
line 3
', '
',
            encodeHtml('

line 4

'), '\nline 5' + '
', ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops, { multiLineCodeblock: false, }); html = qdc.convert(); assert.equal( '
line 1
line 2
' + '
line 3
' + '
' +
            encodeHtml('

line 4

') + '
' + '
line 5
', html ); qdc = new QuillDeltaToHtmlConverter([ops[0], ops[1]]); html = qdc.convert(); assert.equal(html, '
line 1
'); }); }); it('should correctly render custom text block', function () { let ops = [ { insert: 'line 1', }, { attributes: { renderAsBlock: true, attr1: true, }, insert: '\n', }, { insert: 'line 2', }, { attributes: { renderAsBlock: true, attr1: true, }, insert: '\n', }, { insert: 'line 3', }, { attributes: { renderAsBlock: true, attr2: true, }, insert: '\n', }, { insert: '

line 4

', }, { attributes: { renderAsBlock: true, attr1: true, }, insert: '\n', }, { insert: 'line 5', }, { attributes: { renderAsBlock: true, attr1: 'test', }, insert: '\n', }, ]; //console.log(encodeHtml("

line 4

")); var qdc = new QuillDeltaToHtmlConverter(ops, { customTag: (format, op) => { if (format === 'renderAsBlock' && op.attributes['attr1'] === 'test') { return 'test'; } }, customTagAttributes: (op) => { if (op.attributes['attr1'] === 'test') { return { attr1: op.attributes['attr1'], }; } }, customCssClasses: (op) => { if (op.attributes['attr1'] === 'test') { return ['ql-test']; } }, customCssStyles: (op) => { if (op.attributes['attr1'] === 'test') { return ['color:red']; } }, }); let html = qdc.convert(); assert.equal( html, [ '

line 1
line 2

', '

line 3

', '

', encodeHtml('

line 4

'), '

', 'line 5', ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops, { multiLineCustomBlock: false, }); html = qdc.convert(); assert.equal( '

line 1

line 2

' + '

line 3

' + '

' + encodeHtml('

line 4

') + '

' + '

line 5

', html ); qdc = new QuillDeltaToHtmlConverter([ops[0], ops[1]]); html = qdc.convert(); assert.equal(html, '

line 1

'); }); describe('before n after renders()', function () { var ops = [ { insert: 'hello', attributes: { bold: true } }, { insert: '\n', attributes: { bold: true } }, { insert: 'how r u?' }, { insert: 'r u fine' }, { insert: '\n', attributes: { blockquote: {} } }, { insert: { video: 'http://' } }, { insert: 'list item 1' }, { insert: '\n', attributes: { list: { list: 'bullet' } } }, { insert: 'list item 1 indented' }, { insert: '\n', attributes: { list: { list: 'bullet' }, indent: 1 } }, ]; var qdc = new QuillDeltaToHtmlConverter(ops); it('should call before/after render callbacks ', function (done) { let status = { x: 0, y: 8 }; qdc.beforeRender((groupType, data) => { if (groupType === GroupType.InlineGroup) { var op = (data).ops[0]; assert.ok(op.attributes.bold); } else if (groupType === GroupType.Video) { var op = (data).op; assert.ok(op.insert.type === 'video'); } else if (groupType === GroupType.Block) { var d = data; assert.ok(d.op.attributes.blockquote && d.ops.length === 2); } else { var d = data; assert.ok(d.items.length === 1); } status.x++; return ''; }); qdc.afterRender((groupType, html) => { if (groupType === GroupType.InlineGroup) { assert.ok(html.indexOf('hello') > -1); } else if (groupType === GroupType.Video) { assert.ok(html.indexOf(' -1); } else if (groupType === GroupType.Block) { assert.ok(html.indexOf(' -1); } else { assert.ok(html.indexOf('list item 1
    -1); } status.x++; return html; }); qdc.convert(); callWhenXEqualY(status, done); }); it(`should call before render with block grouptype for align indent and direction`, (done) => { let ops = [ { insert: 'align' }, { insert: '\n', attributes: { align: 'right' } }, { insert: 'rtl' }, { insert: '\n', attributes: { direction: 'rtl' } }, { insert: 'indent 1' }, { insert: '\n', attributes: { indent: 1 } }, ]; let status = { x: 0, y: 3 }; let qdc = new QuillDeltaToHtmlConverter(ops); qdc.beforeRender((gtype: any) => { gtype === 'block' && status.x++; return ''; }); qdc.convert(); callWhenXEqualY(status, done); }); it('should use my custom html if I return from before call back', function () { var c = new QuillDeltaToHtmlConverter([ { insert: { video: 'http' } }, { insert: 'aa' }, ]); c.beforeRender(() => { return ''; }); var v = c.convert(); assert.ok(v.indexOf(' -1); }); it('should register and use callbacks if they are functions', function () { var c = new QuillDeltaToHtmlConverter([ { insert: { video: 'http' } }, { insert: 'aa' }, ]); var dummy = (): any => ''; c.beforeRender(dummy()); c.afterRender(dummy()); v = c.convert(); assert.ok(v.indexOf('iframe') > -1); c.beforeRender(dummy); var v = c.convert(); assert.ok(v.indexOf(' -1 && v.indexOf('aa') > -1); c.afterRender(dummy); v = c.convert(); assert.ok(v === ''); }); it('should create blockquote within list', function () { var qdc = new QuillDeltaToHtmlConverter( [ { insert: 'first line\nfirst list item parent' }, { attributes: { list: { list: 'ordered' } }, insert: '\n' }, { insert: 'child list item' }, { attributes: { indent: 1, list: { list: 'ordered' } }, insert: '\n', }, { insert: 'blockq' }, { attributes: { blockquote: { 'in-list': 'true', 'wrapper-indent': '1' }, }, insert: '\n', }, { insert: 'aaa' }, { attributes: { list: { list: 'ordered' } }, insert: '\n' }, { insert: 'bbb' }, { attributes: { blockquote: { 'in-list': 'true', 'wrapper-indent': '3' }, }, insert: '\n', }, { insert: 'ccc' }, { attributes: { blockquote: { 'in-list': 'true', 'wrapper-indent': '3' }, }, insert: '\n', }, { insert: '\n' }, ], { blocksCanBeWrappedWithList: ['blockquote'], } ); var html = qdc.convert(); assert.equal( html, [ '

    first line

    ', '
      ', '
    1. first list item parent', '
        ', '
      1. child list item
      2. ', '
      3. ', '
        blockq
        ', '
      4. ', '
      ', '
    2. ', '
    3. aaa
      • bbb
        ccc
    4. ', '
    ', '


    ', ].join('') ); }); }); }); });