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,
[
'',
].join('')
);
var qdc = new QuillDeltaToHtmlConverter([
{
insert: 'mention',
attributes: {
mentions: true,
mention: { slug: 'aa' },
},
},
]);
var html = qdc.convert();
assert.equal(
html,
[''].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,
''
);
qdc = new QuillDeltaToHtmlConverter(ops);
html = qdc.convert();
assert.equal(
html,
''
);
});
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: 'ordered' } },
{ insert: 'there' },
{ insert: '\n', attributes: { list: 'bullet' } },
{ insert: '\n', attributes: { list: 'ordered' } },
];
var qdc = new QuillDeltaToHtmlConverter(ops4);
var html = qdc.convert();
assert.equal(html.indexOf('mr') > -1, true);
assert.equal(html.indexOf('
hello
how areyou?
bye
' ); }); it('should create checked/unchecked lists', function () { var ops4 = [ { insert: 'hello' }, { insert: '\n', attributes: { list: 'checked' } }, { insert: 'there' }, { insert: '\n', attributes: { list: 'unchecked' } }, { insert: 'man' }, { insert: '\n', attributes: { list: 'checked' } }, { insert: 'not done' }, { insert: '\n', attributes: { indent: 1, list: 'unchecked' } }, ]; var qdc = new QuillDeltaToHtmlConverter(ops4); var html = qdc.convert(); assert.equal( html, [ '-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, [ `
`, ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops); html = qdc.convert(); assert.equal( html, [ ``, ].join('') ); qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '_top' }); html = qdc.convert(); assert.equal( html, [ ``, ].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(), [ ``, ].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(), [ `| cell |
| 11 | 12 | 13 |
| 21 | 22 | 23 |
| 31 | 32 | 33 |
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 ? 'hello my friend!
:'); 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(), 'Hello', ' my
name is joey
Hello', ' my
name is joey
aa
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
aa
bb
line 4
', }, { attributes: { 'code-block': true, }, insert: '\n', }, { insert: 'line 5', }, { attributes: { '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 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: true } }, { insert: { video: 'http://' } }, { insert: 'list item 1' }, { insert: '\n', attributes: { list: 'bullet' } }, { insert: 'list item 1 indented' }, { insert: '\n', attributes: { 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 = (