123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- import hljs from 'highlight.js';
- import Delta from 'quill-delta';
- import Quill from '../../../core/quill';
- import BoldBlot from '../../../formats/bold';
- import CodeBlock, { CodeBlockContainer } from '../../../formats/code';
- import Syntax, { CodeBlock as SyntaxCodeBlock } from '../../../modules/syntax';
- const HIGHLIGHT_INTERVAL = 10;
- describe('Syntax', function() {
- beforeAll(function() {
- Syntax.register();
- Syntax.DEFAULTS.languages = [
- { key: 'javascript', label: 'Javascript' },
- { key: 'ruby', label: 'Ruby' },
- ];
- });
- beforeEach(function() {
- const container = this.initialize(
- HTMLElement,
- `<pre data-language="javascript">var test = 1;<br>var bugz = 0;<br></pre>
- <p><br></p>`,
- );
- this.quill = new Quill(container, {
- modules: {
- syntax: {
- hljs,
- interval: HIGHLIGHT_INTERVAL,
- },
- },
- });
- });
- afterAll(function() {
- Quill.register(CodeBlock, true);
- Quill.register(CodeBlockContainer, true);
- });
- describe('highlighting', function() {
- it('initialize', function() {
- expect(this.quill.root).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript">var test = 1;</div>
- <div class="ql-code-block" data-language="javascript">var bugz = 0;</div>
- </div>
- <p><br></p>`,
- );
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- });
- it('adds token', function(done) {
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> test = <span class="ql-token hljs-number">1</span>;</div>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`,
- );
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('tokens do not escape', function(done) {
- this.quill.deleteText(22, 6);
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> test = <span class="ql-token hljs-number">1</span>;</div>
- </div>
- <p>var bugz</p>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('var bugz\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('change language', function(done) {
- this.quill.formatLine(0, 20, 'code-block', 'ruby');
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="ruby">var test = <span class="ql-token hljs-number">1</span>;</div>
- <div class="ql-code-block" data-language="ruby">var bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'ruby' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'ruby' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('invalid language', function(done) {
- this.quill.formatLine(0, 20, 'code-block', 'invalid');
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="plain">var test = 1;</div>
- <div class="ql-code-block" data-language="plain">var bugz = 0;</div>
- </div>
- <p><br></p>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'plain' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'plain' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('unformat first line', function(done) {
- this.quill.formatLine(0, 1, 'code-block', false);
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <p>var test = 1;</p>
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;\nvar bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('split container', function(done) {
- this.quill.updateContents(new Delta().retain(14).insert('\n'));
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(
- `
- <div class="ql-code-block-container" spellcheck="false">
- <select class="ql-ui" contenteditable="false">
- <option value="javascript">Javascript</option>
- <option value="ruby">Ruby</option>
- </select>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> test = <span class="ql-token hljs-number">1</span>;</div>
- </div>
- <p><br></p>
- <div class="ql-code-block-container" spellcheck="false">
- <select class="ql-ui" contenteditable="false">
- <option value="javascript">Javascript</option>
- <option value="ruby">Ruby</option>
- </select>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`,
- false,
- false,
- );
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\nvar bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('merge containers', function(done) {
- this.quill.updateContents(new Delta().retain(14).insert('\n'));
- setTimeout(() => {
- this.quill.deleteText(14, 1);
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(
- `
- <div class="ql-code-block-container" spellcheck="false">
- <select class="ql-ui" contenteditable="false">
- <option value="javascript">Javascript</option>
- <option value="ruby">Ruby</option>
- </select>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> test = <span class="ql-token hljs-number">1</span>;</div>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`,
- false,
- false,
- );
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('var test = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- }, HIGHLIGHT_INTERVAL + 1);
- });
- describe('allowedChildren', function() {
- beforeAll(function() {
- SyntaxCodeBlock.allowedChildren.push(BoldBlot);
- });
- afterAll(function() {
- SyntaxCodeBlock.allowedChildren.pop();
- });
- it('modification', function(done) {
- this.quill.formatText(2, 3, 'bold', true);
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">va</span><strong><span class="ql-token hljs-keyword">r</span> t</strong>est = <span class="ql-token hljs-number">1</span>;</div>
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">var</span> bugz = <span class="ql-token hljs-number">0</span>;</div>
- </div>
- <p><br></p>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('va')
- .insert('r t', { bold: true })
- .insert('est = 1;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('var bugz = 0;')
- .insert('\n', { 'code-block': 'javascript' })
- .insert('\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('removal', function(done) {
- this.quill.formatText(2, 3, 'bold', true);
- setTimeout(() => {
- this.quill.formatLine(0, 15, 'code-block', false);
- expect(this.quill.root).toEqualHTML(
- `<p>va<strong>r t</strong>est = 1;</p><p>var bugz = 0;</p><p><br></p>`,
- );
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('va')
- .insert('r t', { bold: true })
- .insert('est = 1;\nvar bugz = 0;\n\n'),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- it('addition', function(done) {
- this.quill.setText('var test = 1;\n');
- this.quill.formatText(2, 3, 'bold', true);
- this.quill.formatLine(0, 1, 'code-block', 'javascript');
- setTimeout(() => {
- expect(this.quill.root).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block" data-language="javascript"><span class="ql-token hljs-keyword">va</span><strong><span class="ql-token hljs-keyword">r</span> t</strong>est = <span class="ql-token hljs-number">1</span>;</div>
- </div>`);
- expect(this.quill.getContents()).toEqual(
- new Delta()
- .insert('va')
- .insert('r t', { bold: true })
- .insert('est = 1;')
- .insert('\n', { 'code-block': 'javascript' }),
- );
- done();
- }, HIGHLIGHT_INTERVAL + 1);
- });
- });
- });
- describe('html', function() {
- it('code language', function() {
- expect(this.quill.getSemanticHTML()).toContain(
- 'data-language="javascript"',
- );
- });
- });
- });
|