Skip to content

Commit

Permalink
fix bug where copied syntax block is off by 1 char
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Aug 10, 2018
1 parent 6577825 commit e5b543e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions formats/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Block from '../blots/block';
import Break from '../blots/break';
import Cursor from '../blots/cursor';
import Inline from '../blots/inline';
import TextBlot from '../blots/text';
import TextBlot, { escapeText } from '../blots/text';
import Container from '../blots/container';
import Quill from '../core/quill';

Expand All @@ -14,8 +14,13 @@ class CodeBlockContainer extends Container {
}

html(index, length) {
const html = this.domNode.innerText.slice(index, index + length);
return `<pre>${html}</pre>`;
let text = this.domNode.innerText;
// TODO find more robust solution for <select> turning into \n
if (text.startsWith('\n')) {
text = text.slice(1);
}
text = text.slice(index, index + length);
return `<pre>${escapeText(text)}</pre>`;
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,11 @@ describe('Editor', function() {
editor.insertText(0, '<b>Test</b>');
expect(editor.getHTML(0, 11)).toEqual('&lt;b&gt;Test&lt;/b&gt;');
});

it('multiline code', function() {
const editor = this.initialize(Editor, '<p>0123</p><p>4567</p>');
editor.formatLine(0, 9, { 'code-block': 'javascript' });
expect(editor.getHTML(0, 9)).toEqual('<pre>0123\n4567</pre>');
});
});
});

0 comments on commit e5b543e

Please sign in to comment.