syntax.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import hljs from 'highlight.js';
  2. import Delta from 'quill-delta';
  3. import Quill from '../../../core/quill';
  4. import BoldBlot from '../../../formats/bold';
  5. import CodeBlock, { CodeBlockContainer } from '../../../formats/code';
  6. import Syntax, { CodeBlock as SyntaxCodeBlock } from '../../../modules/syntax';
  7. const HIGHLIGHT_INTERVAL = 10;
  8. describe('Syntax', function() {
  9. beforeAll(function() {
  10. Syntax.register();
  11. Syntax.DEFAULTS.languages = [
  12. { key: 'javascript', label: 'Javascript' },
  13. { key: 'ruby', label: 'Ruby' },
  14. ];
  15. });
  16. beforeEach(function() {
  17. const container = this.initialize(
  18. HTMLElement,
  19. `<pre data-language="javascript">var test = 1;<br>var bugz = 0;<br></pre>
  20. <p><br></p>`,
  21. );
  22. this.quill = new Quill(container, {
  23. modules: {
  24. syntax: {
  25. hljs,
  26. interval: HIGHLIGHT_INTERVAL,
  27. },
  28. },
  29. });
  30. });
  31. afterAll(function() {
  32. Quill.register(CodeBlock, true);
  33. Quill.register(CodeBlockContainer, true);
  34. });
  35. describe('highlighting', function() {
  36. it('initialize', function() {
  37. expect(this.quill.root).toEqualHTML(
  38. `<div class="ql-code-block-container" spellcheck="false">
  39. <div class="ql-code-block" data-language="javascript">var test = 1;</div>
  40. <div class="ql-code-block" data-language="javascript">var bugz = 0;</div>
  41. </div>
  42. <p><br></p>`,
  43. );
  44. expect(this.quill.getContents()).toEqual(
  45. new Delta()
  46. .insert('var test = 1;')
  47. .insert('\n', { 'code-block': 'javascript' })
  48. .insert('var bugz = 0;')
  49. .insert('\n', { 'code-block': 'javascript' })
  50. .insert('\n'),
  51. );
  52. });
  53. it('adds token', function(done) {
  54. setTimeout(() => {
  55. expect(this.quill.root).toEqualHTML(
  56. `<div class="ql-code-block-container" spellcheck="false">
  57. <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>
  58. <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>
  59. </div>
  60. <p><br></p>`,
  61. );
  62. expect(this.quill.getContents()).toEqual(
  63. new Delta()
  64. .insert('var test = 1;')
  65. .insert('\n', { 'code-block': 'javascript' })
  66. .insert('var bugz = 0;')
  67. .insert('\n', { 'code-block': 'javascript' })
  68. .insert('\n'),
  69. );
  70. done();
  71. }, HIGHLIGHT_INTERVAL + 1);
  72. });
  73. it('tokens do not escape', function(done) {
  74. this.quill.deleteText(22, 6);
  75. setTimeout(() => {
  76. expect(this.quill.root).toEqualHTML(`
  77. <div class="ql-code-block-container" spellcheck="false">
  78. <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>
  79. </div>
  80. <p>var bugz</p>`);
  81. expect(this.quill.getContents()).toEqual(
  82. new Delta()
  83. .insert('var test = 1;')
  84. .insert('\n', { 'code-block': 'javascript' })
  85. .insert('var bugz\n'),
  86. );
  87. done();
  88. }, HIGHLIGHT_INTERVAL + 1);
  89. });
  90. it('change language', function(done) {
  91. this.quill.formatLine(0, 20, 'code-block', 'ruby');
  92. setTimeout(() => {
  93. expect(this.quill.root).toEqualHTML(`
  94. <div class="ql-code-block-container" spellcheck="false">
  95. <div class="ql-code-block" data-language="ruby">var test = <span class="ql-token hljs-number">1</span>;</div>
  96. <div class="ql-code-block" data-language="ruby">var bugz = <span class="ql-token hljs-number">0</span>;</div>
  97. </div>
  98. <p><br></p>`);
  99. expect(this.quill.getContents()).toEqual(
  100. new Delta()
  101. .insert('var test = 1;')
  102. .insert('\n', { 'code-block': 'ruby' })
  103. .insert('var bugz = 0;')
  104. .insert('\n', { 'code-block': 'ruby' })
  105. .insert('\n'),
  106. );
  107. done();
  108. }, HIGHLIGHT_INTERVAL + 1);
  109. });
  110. it('invalid language', function(done) {
  111. this.quill.formatLine(0, 20, 'code-block', 'invalid');
  112. setTimeout(() => {
  113. expect(this.quill.root).toEqualHTML(`
  114. <div class="ql-code-block-container" spellcheck="false">
  115. <div class="ql-code-block" data-language="plain">var test = 1;</div>
  116. <div class="ql-code-block" data-language="plain">var bugz = 0;</div>
  117. </div>
  118. <p><br></p>`);
  119. expect(this.quill.getContents()).toEqual(
  120. new Delta()
  121. .insert('var test = 1;')
  122. .insert('\n', { 'code-block': 'plain' })
  123. .insert('var bugz = 0;')
  124. .insert('\n', { 'code-block': 'plain' })
  125. .insert('\n'),
  126. );
  127. done();
  128. }, HIGHLIGHT_INTERVAL + 1);
  129. });
  130. it('unformat first line', function(done) {
  131. this.quill.formatLine(0, 1, 'code-block', false);
  132. setTimeout(() => {
  133. expect(this.quill.root).toEqualHTML(`
  134. <p>var test = 1;</p>
  135. <div class="ql-code-block-container" spellcheck="false">
  136. <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>
  137. </div>
  138. <p><br></p>`);
  139. expect(this.quill.getContents()).toEqual(
  140. new Delta()
  141. .insert('var test = 1;\nvar bugz = 0;')
  142. .insert('\n', { 'code-block': 'javascript' })
  143. .insert('\n'),
  144. );
  145. done();
  146. }, HIGHLIGHT_INTERVAL + 1);
  147. });
  148. it('split container', function(done) {
  149. this.quill.updateContents(new Delta().retain(14).insert('\n'));
  150. setTimeout(() => {
  151. expect(this.quill.root).toEqualHTML(
  152. `
  153. <div class="ql-code-block-container" spellcheck="false">
  154. <select class="ql-ui" contenteditable="false">
  155. <option value="javascript">Javascript</option>
  156. <option value="ruby">Ruby</option>
  157. </select>
  158. <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>
  159. </div>
  160. <p><br></p>
  161. <div class="ql-code-block-container" spellcheck="false">
  162. <select class="ql-ui" contenteditable="false">
  163. <option value="javascript">Javascript</option>
  164. <option value="ruby">Ruby</option>
  165. </select>
  166. <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>
  167. </div>
  168. <p><br></p>`,
  169. false,
  170. false,
  171. );
  172. expect(this.quill.getContents()).toEqual(
  173. new Delta()
  174. .insert('var test = 1;')
  175. .insert('\n', { 'code-block': 'javascript' })
  176. .insert('\nvar bugz = 0;')
  177. .insert('\n', { 'code-block': 'javascript' })
  178. .insert('\n'),
  179. );
  180. done();
  181. }, HIGHLIGHT_INTERVAL + 1);
  182. });
  183. it('merge containers', function(done) {
  184. this.quill.updateContents(new Delta().retain(14).insert('\n'));
  185. setTimeout(() => {
  186. this.quill.deleteText(14, 1);
  187. setTimeout(() => {
  188. expect(this.quill.root).toEqualHTML(
  189. `
  190. <div class="ql-code-block-container" spellcheck="false">
  191. <select class="ql-ui" contenteditable="false">
  192. <option value="javascript">Javascript</option>
  193. <option value="ruby">Ruby</option>
  194. </select>
  195. <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>
  196. <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>
  197. </div>
  198. <p><br></p>`,
  199. false,
  200. false,
  201. );
  202. expect(this.quill.getContents()).toEqual(
  203. new Delta()
  204. .insert('var test = 1;')
  205. .insert('\n', { 'code-block': 'javascript' })
  206. .insert('var bugz = 0;')
  207. .insert('\n', { 'code-block': 'javascript' })
  208. .insert('\n'),
  209. );
  210. done();
  211. }, HIGHLIGHT_INTERVAL + 1);
  212. }, HIGHLIGHT_INTERVAL + 1);
  213. });
  214. describe('allowedChildren', function() {
  215. beforeAll(function() {
  216. SyntaxCodeBlock.allowedChildren.push(BoldBlot);
  217. });
  218. afterAll(function() {
  219. SyntaxCodeBlock.allowedChildren.pop();
  220. });
  221. it('modification', function(done) {
  222. this.quill.formatText(2, 3, 'bold', true);
  223. setTimeout(() => {
  224. expect(this.quill.root).toEqualHTML(`
  225. <div class="ql-code-block-container" spellcheck="false">
  226. <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>
  227. <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>
  228. </div>
  229. <p><br></p>`);
  230. expect(this.quill.getContents()).toEqual(
  231. new Delta()
  232. .insert('va')
  233. .insert('r t', { bold: true })
  234. .insert('est = 1;')
  235. .insert('\n', { 'code-block': 'javascript' })
  236. .insert('var bugz = 0;')
  237. .insert('\n', { 'code-block': 'javascript' })
  238. .insert('\n'),
  239. );
  240. done();
  241. }, HIGHLIGHT_INTERVAL + 1);
  242. });
  243. it('removal', function(done) {
  244. this.quill.formatText(2, 3, 'bold', true);
  245. setTimeout(() => {
  246. this.quill.formatLine(0, 15, 'code-block', false);
  247. expect(this.quill.root).toEqualHTML(
  248. `<p>va<strong>r t</strong>est = 1;</p><p>var bugz = 0;</p><p><br></p>`,
  249. );
  250. expect(this.quill.getContents()).toEqual(
  251. new Delta()
  252. .insert('va')
  253. .insert('r t', { bold: true })
  254. .insert('est = 1;\nvar bugz = 0;\n\n'),
  255. );
  256. done();
  257. }, HIGHLIGHT_INTERVAL + 1);
  258. });
  259. it('addition', function(done) {
  260. this.quill.setText('var test = 1;\n');
  261. this.quill.formatText(2, 3, 'bold', true);
  262. this.quill.formatLine(0, 1, 'code-block', 'javascript');
  263. setTimeout(() => {
  264. expect(this.quill.root).toEqualHTML(`
  265. <div class="ql-code-block-container" spellcheck="false">
  266. <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>
  267. </div>`);
  268. expect(this.quill.getContents()).toEqual(
  269. new Delta()
  270. .insert('va')
  271. .insert('r t', { bold: true })
  272. .insert('est = 1;')
  273. .insert('\n', { 'code-block': 'javascript' }),
  274. );
  275. done();
  276. }, HIGHLIGHT_INTERVAL + 1);
  277. });
  278. });
  279. });
  280. describe('html', function() {
  281. it('code language', function() {
  282. expect(this.quill.getSemanticHTML()).toContain(
  283. 'data-language="javascript"',
  284. );
  285. });
  286. });
  287. });