Skip to content

Commit

Permalink
tolerate invalid languages
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Oct 5, 2019
1 parent 5715499 commit df2cf16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Syntax extends Module {
'Syntax module requires highlight.js. Please include the library on the page before Quill.',
);
}
this.languages = this.options.languages.reduce((memo, { key }) => {
memo[key] = true;
return memo;
}, {});
this.highlightBlot = this.highlightBlot.bind(this);
this.initListener();
this.initTimer();
Expand Down Expand Up @@ -225,6 +229,7 @@ class Syntax extends Module {
}

highlightBlot(text, language = 'plain') {
language = this.languages[language] ? language : 'plain';
if (language === 'plain') {
return escapeText(text)
.split('\n')
Expand Down
21 changes: 21 additions & 0 deletions test/unit/modules/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ describe('Syntax', function() {
}, 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(() => {
Expand Down

0 comments on commit df2cf16

Please sign in to comment.