text-editor-element-spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. const TextEditor = require('../src/text-editor');
  2. const TextEditorElement = require('../src/text-editor-element');
  3. describe('TextEditorElement', () => {
  4. let jasmineContent;
  5. beforeEach(() => {
  6. jasmineContent = document.body.querySelector('#jasmine-content');
  7. // Force scrollbars to be visible regardless of local system configuration
  8. const scrollbarStyle = document.createElement('style');
  9. scrollbarStyle.textContent =
  10. 'atom-text-editor ::-webkit-scrollbar { -webkit-appearance: none }';
  11. jasmine.attachToDOM(scrollbarStyle);
  12. });
  13. function buildTextEditorElement(options = {}) {
  14. const element = TextEditorElement.createTextEditorElement();
  15. element.setUpdatedSynchronously(false);
  16. if (options.attach !== false) jasmine.attachToDOM(element);
  17. return element;
  18. }
  19. it("honors the 'mini' attribute", () => {
  20. jasmineContent.innerHTML = '<atom-text-editor mini>';
  21. const element = jasmineContent.firstChild;
  22. expect(element.getModel().isMini()).toBe(true);
  23. element.removeAttribute('mini');
  24. expect(element.getModel().isMini()).toBe(false);
  25. expect(element.getComponent().getGutterContainerWidth()).toBe(0);
  26. element.setAttribute('mini', '');
  27. expect(element.getModel().isMini()).toBe(true);
  28. });
  29. it('sets the editor to mini if the model is accessed prior to attaching the element', () => {
  30. const parent = document.createElement('div');
  31. parent.innerHTML = '<atom-text-editor mini>';
  32. const element = parent.firstChild;
  33. expect(element.getModel().isMini()).toBe(true);
  34. });
  35. it("honors the 'placeholder-text' attribute", () => {
  36. jasmineContent.innerHTML = "<atom-text-editor placeholder-text='testing'>";
  37. const element = jasmineContent.firstChild;
  38. expect(element.getModel().getPlaceholderText()).toBe('testing');
  39. element.setAttribute('placeholder-text', 'placeholder');
  40. expect(element.getModel().getPlaceholderText()).toBe('placeholder');
  41. element.removeAttribute('placeholder-text');
  42. expect(element.getModel().getPlaceholderText()).toBeNull();
  43. });
  44. it("only assigns 'placeholder-text' on the model if the attribute is present", () => {
  45. const editor = new TextEditor({ placeholderText: 'placeholder' });
  46. editor.getElement();
  47. expect(editor.getPlaceholderText()).toBe('placeholder');
  48. });
  49. it("honors the 'gutter-hidden' attribute", () => {
  50. jasmineContent.innerHTML = '<atom-text-editor gutter-hidden>';
  51. const element = jasmineContent.firstChild;
  52. expect(element.getModel().isLineNumberGutterVisible()).toBe(false);
  53. element.removeAttribute('gutter-hidden');
  54. expect(element.getModel().isLineNumberGutterVisible()).toBe(true);
  55. element.setAttribute('gutter-hidden', '');
  56. expect(element.getModel().isLineNumberGutterVisible()).toBe(false);
  57. });
  58. it("honors the 'readonly' attribute", async function() {
  59. jasmineContent.innerHTML = '<atom-text-editor readonly>';
  60. const element = jasmineContent.firstChild;
  61. expect(element.getComponent().isInputEnabled()).toBe(false);
  62. element.removeAttribute('readonly');
  63. expect(element.getComponent().isInputEnabled()).toBe(true);
  64. element.setAttribute('readonly', true);
  65. expect(element.getComponent().isInputEnabled()).toBe(false);
  66. });
  67. it('honors the text content', () => {
  68. jasmineContent.innerHTML = '<atom-text-editor>testing</atom-text-editor>';
  69. const element = jasmineContent.firstChild;
  70. expect(element.getModel().getText()).toBe('testing');
  71. });
  72. describe('tabIndex', () => {
  73. it('uses a default value of -1', () => {
  74. jasmineContent.innerHTML = '<atom-text-editor />';
  75. const element = jasmineContent.firstChild;
  76. expect(element.tabIndex).toBe(-1);
  77. expect(element.querySelector('input').tabIndex).toBe(-1);
  78. });
  79. it('uses the custom value when given', () => {
  80. jasmineContent.innerHTML = '<atom-text-editor tabIndex="42" />';
  81. const element = jasmineContent.firstChild;
  82. expect(element.tabIndex).toBe(-1);
  83. expect(element.querySelector('input').tabIndex).toBe(42);
  84. });
  85. });
  86. describe('when the model is assigned', () =>
  87. it("adds the 'mini' attribute if .isMini() returns true on the model", async () => {
  88. const element = buildTextEditorElement();
  89. element.getModel().update({ mini: true });
  90. await atom.views.getNextUpdatePromise();
  91. expect(element.hasAttribute('mini')).toBe(true);
  92. }));
  93. describe('when the editor is attached to the DOM', () =>
  94. it('mounts the component and unmounts when removed from the dom', () => {
  95. const element = buildTextEditorElement();
  96. const { component } = element;
  97. expect(component.attached).toBe(true);
  98. element.remove();
  99. expect(component.attached).toBe(false);
  100. jasmine.attachToDOM(element);
  101. expect(element.component.attached).toBe(true);
  102. }));
  103. describe('when the editor is detached from the DOM and then reattached', () => {
  104. it('does not render duplicate line numbers', () => {
  105. const editor = new TextEditor();
  106. editor.setText('1\n2\n3');
  107. const element = editor.getElement();
  108. jasmine.attachToDOM(element);
  109. const initialCount = element.querySelectorAll('.line-number').length;
  110. element.remove();
  111. jasmine.attachToDOM(element);
  112. expect(element.querySelectorAll('.line-number').length).toBe(
  113. initialCount
  114. );
  115. });
  116. it('does not render duplicate decorations in custom gutters', () => {
  117. const editor = new TextEditor();
  118. editor.setText('1\n2\n3');
  119. editor.addGutter({ name: 'test-gutter' });
  120. const marker = editor.markBufferRange([[0, 0], [2, 0]]);
  121. editor.decorateMarker(marker, {
  122. type: 'gutter',
  123. gutterName: 'test-gutter'
  124. });
  125. const element = editor.getElement();
  126. jasmine.attachToDOM(element);
  127. const initialDecorationCount = element.querySelectorAll('.decoration')
  128. .length;
  129. element.remove();
  130. jasmine.attachToDOM(element);
  131. expect(element.querySelectorAll('.decoration').length).toBe(
  132. initialDecorationCount
  133. );
  134. });
  135. it('can be re-focused using the previous `document.activeElement`', () => {
  136. const editorElement = buildTextEditorElement();
  137. editorElement.focus();
  138. const { activeElement } = document;
  139. editorElement.remove();
  140. jasmine.attachToDOM(editorElement);
  141. activeElement.focus();
  142. expect(editorElement.hasFocus()).toBe(true);
  143. });
  144. });
  145. describe('focus and blur handling', () => {
  146. it('proxies focus/blur events to/from the hidden input', () => {
  147. const element = buildTextEditorElement();
  148. jasmineContent.appendChild(element);
  149. let blurCalled = false;
  150. element.addEventListener('blur', () => {
  151. blurCalled = true;
  152. });
  153. element.focus();
  154. expect(blurCalled).toBe(false);
  155. expect(element.hasFocus()).toBe(true);
  156. expect(document.activeElement).toBe(element.querySelector('input'));
  157. document.body.focus();
  158. expect(blurCalled).toBe(true);
  159. });
  160. it("doesn't trigger a blur event on the editor element when focusing an already focused editor element", () => {
  161. let blurCalled = false;
  162. const element = buildTextEditorElement();
  163. element.addEventListener('blur', () => {
  164. blurCalled = true;
  165. });
  166. jasmineContent.appendChild(element);
  167. expect(document.activeElement).toBe(document.body);
  168. expect(blurCalled).toBe(false);
  169. element.focus();
  170. expect(document.activeElement).toBe(element.querySelector('input'));
  171. expect(blurCalled).toBe(false);
  172. element.focus();
  173. expect(document.activeElement).toBe(element.querySelector('input'));
  174. expect(blurCalled).toBe(false);
  175. });
  176. describe('when focused while a parent node is being attached to the DOM', () => {
  177. class ElementThatFocusesChild extends HTMLElement {
  178. connectedCallback() {
  179. this.firstChild.focus();
  180. }
  181. }
  182. window.customElements.define(
  183. 'element-that-focuses-child',
  184. ElementThatFocusesChild
  185. );
  186. it('proxies the focus event to the hidden input', () => {
  187. const element = buildTextEditorElement();
  188. const parentElement = document.createElement(
  189. 'element-that-focuses-child'
  190. );
  191. parentElement.appendChild(element);
  192. jasmineContent.appendChild(parentElement);
  193. expect(document.activeElement).toBe(element.querySelector('input'));
  194. });
  195. });
  196. describe('if focused when invisible due to a zero height and width', () => {
  197. it('focuses the hidden input and does not throw an exception', () => {
  198. const parentElement = document.createElement('div');
  199. parentElement.style.position = 'absolute';
  200. parentElement.style.width = '0px';
  201. parentElement.style.height = '0px';
  202. const element = buildTextEditorElement({ attach: false });
  203. parentElement.appendChild(element);
  204. jasmineContent.appendChild(parentElement);
  205. element.focus();
  206. expect(document.activeElement).toBe(element.component.getHiddenInput());
  207. });
  208. });
  209. });
  210. describe('::setModel', () => {
  211. describe('when the element does not have an editor yet', () => {
  212. it('uses the supplied one', () => {
  213. const element = buildTextEditorElement({ attach: false });
  214. const editor = new TextEditor();
  215. element.setModel(editor);
  216. jasmine.attachToDOM(element);
  217. expect(editor.element).toBe(element);
  218. expect(element.getModel()).toBe(editor);
  219. });
  220. });
  221. describe('when the element already has an editor', () => {
  222. it('unbinds it and then swaps it with the supplied one', async () => {
  223. const element = buildTextEditorElement({ attach: true });
  224. const previousEditor = element.getModel();
  225. expect(previousEditor.element).toBe(element);
  226. const newEditor = new TextEditor();
  227. element.setModel(newEditor);
  228. expect(previousEditor.element).not.toBe(element);
  229. expect(newEditor.element).toBe(element);
  230. expect(element.getModel()).toBe(newEditor);
  231. });
  232. });
  233. });
  234. describe('::onDidAttach and ::onDidDetach', () =>
  235. it('invokes callbacks when the element is attached and detached', () => {
  236. const element = buildTextEditorElement({ attach: false });
  237. const attachedCallback = jasmine.createSpy('attachedCallback');
  238. const detachedCallback = jasmine.createSpy('detachedCallback');
  239. element.onDidAttach(attachedCallback);
  240. element.onDidDetach(detachedCallback);
  241. jasmine.attachToDOM(element);
  242. expect(attachedCallback).toHaveBeenCalled();
  243. expect(detachedCallback).not.toHaveBeenCalled();
  244. attachedCallback.reset();
  245. element.remove();
  246. expect(attachedCallback).not.toHaveBeenCalled();
  247. expect(detachedCallback).toHaveBeenCalled();
  248. }));
  249. describe('::setUpdatedSynchronously', () => {
  250. it('controls whether the text editor is updated synchronously', () => {
  251. spyOn(window, 'requestAnimationFrame').andCallFake(fn => fn());
  252. const element = buildTextEditorElement();
  253. expect(element.isUpdatedSynchronously()).toBe(false);
  254. element.getModel().setText('hello');
  255. expect(window.requestAnimationFrame).toHaveBeenCalled();
  256. expect(element.textContent).toContain('hello');
  257. window.requestAnimationFrame.reset();
  258. element.setUpdatedSynchronously(true);
  259. element.getModel().setText('goodbye');
  260. expect(window.requestAnimationFrame).not.toHaveBeenCalled();
  261. expect(element.textContent).toContain('goodbye');
  262. });
  263. });
  264. describe('::getDefaultCharacterWidth', () => {
  265. it('returns 0 before the element is attached', () => {
  266. const element = buildTextEditorElement({ attach: false });
  267. expect(element.getDefaultCharacterWidth()).toBe(0);
  268. });
  269. it('returns the width of a character in the root scope', () => {
  270. const element = buildTextEditorElement();
  271. jasmine.attachToDOM(element);
  272. expect(element.getDefaultCharacterWidth()).toBeGreaterThan(0);
  273. });
  274. });
  275. describe('::getMaxScrollTop', () =>
  276. it('returns the maximum scroll top that can be applied to the element', async () => {
  277. const editor = new TextEditor();
  278. editor.setText('1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16');
  279. const element = editor.getElement();
  280. element.style.lineHeight = '10px';
  281. element.style.width = '200px';
  282. jasmine.attachToDOM(element);
  283. const horizontalScrollbarHeight = element.component.getHorizontalScrollbarHeight();
  284. expect(element.getMaxScrollTop()).toBe(0);
  285. await editor.update({ autoHeight: false });
  286. element.style.height = 100 + horizontalScrollbarHeight + 'px';
  287. await element.getNextUpdatePromise();
  288. expect(element.getMaxScrollTop()).toBe(60);
  289. element.style.height = 120 + horizontalScrollbarHeight + 'px';
  290. await element.getNextUpdatePromise();
  291. expect(element.getMaxScrollTop()).toBe(40);
  292. element.style.height = 200 + horizontalScrollbarHeight + 'px';
  293. await element.getNextUpdatePromise();
  294. expect(element.getMaxScrollTop()).toBe(0);
  295. }));
  296. describe('::setScrollTop and ::setScrollLeft', () => {
  297. it('changes the scroll position', async () => {
  298. const element = buildTextEditorElement();
  299. element.getModel().update({ autoHeight: false });
  300. element.getModel().setText('lorem\nipsum\ndolor\nsit\namet');
  301. element.setHeight(20);
  302. await element.getNextUpdatePromise();
  303. element.setWidth(20);
  304. await element.getNextUpdatePromise();
  305. element.setScrollTop(22);
  306. await element.getNextUpdatePromise();
  307. expect(element.getScrollTop()).toBe(22);
  308. element.setScrollLeft(32);
  309. await element.getNextUpdatePromise();
  310. expect(element.getScrollLeft()).toBe(32);
  311. });
  312. });
  313. describe('on TextEditor::setMini', () =>
  314. it("changes the element's 'mini' attribute", async () => {
  315. const element = buildTextEditorElement();
  316. expect(element.hasAttribute('mini')).toBe(false);
  317. element.getModel().setMini(true);
  318. await element.getNextUpdatePromise();
  319. expect(element.hasAttribute('mini')).toBe(true);
  320. element.getModel().setMini(false);
  321. await element.getNextUpdatePromise();
  322. expect(element.hasAttribute('mini')).toBe(false);
  323. }));
  324. describe('::intersectsVisibleRowRange(start, end)', () => {
  325. it('returns true if the given row range intersects the visible row range', async () => {
  326. const element = buildTextEditorElement();
  327. const editor = element.getModel();
  328. const horizontalScrollbarHeight = element.component.getHorizontalScrollbarHeight();
  329. editor.update({ autoHeight: false });
  330. element.getModel().setText('x\n'.repeat(20));
  331. element.style.height = 120 + horizontalScrollbarHeight + 'px';
  332. await element.getNextUpdatePromise();
  333. element.setScrollTop(80);
  334. await element.getNextUpdatePromise();
  335. expect(element.getVisibleRowRange()).toEqual([4, 11]);
  336. expect(element.intersectsVisibleRowRange(0, 4)).toBe(false);
  337. expect(element.intersectsVisibleRowRange(0, 5)).toBe(true);
  338. expect(element.intersectsVisibleRowRange(5, 8)).toBe(true);
  339. expect(element.intersectsVisibleRowRange(11, 12)).toBe(false);
  340. expect(element.intersectsVisibleRowRange(12, 13)).toBe(false);
  341. });
  342. });
  343. describe('::pixelRectForScreenRange(range)', () => {
  344. it('returns a {top/left/width/height} object describing the rectangle between two screen positions, even if they are not on screen', async () => {
  345. const element = buildTextEditorElement();
  346. const editor = element.getModel();
  347. const horizontalScrollbarHeight = element.component.getHorizontalScrollbarHeight();
  348. editor.update({ autoHeight: false });
  349. element.getModel().setText('xxxxxxxxxxxxxxxxxxxxxx\n'.repeat(20));
  350. element.style.height = 120 + horizontalScrollbarHeight + 'px';
  351. await element.getNextUpdatePromise();
  352. element.setScrollTop(80);
  353. await element.getNextUpdatePromise();
  354. expect(element.getVisibleRowRange()).toEqual([4, 11]);
  355. const top = 2 * editor.getLineHeightInPixels();
  356. const bottom = 13 * editor.getLineHeightInPixels();
  357. const left = Math.round(3 * editor.getDefaultCharWidth());
  358. const right = Math.round(11 * editor.getDefaultCharWidth());
  359. const pixelRect = element.pixelRectForScreenRange([[2, 3], [13, 11]]);
  360. expect(pixelRect.top).toEqual(top);
  361. expect(pixelRect.left).toEqual(left);
  362. expect(pixelRect.height).toEqual(
  363. bottom + editor.getLineHeightInPixels() - top
  364. );
  365. expect(pixelRect.width).toBeNear(right - left);
  366. });
  367. });
  368. describe('events', () => {
  369. let element = null;
  370. beforeEach(async () => {
  371. element = buildTextEditorElement();
  372. element.getModel().update({ autoHeight: false });
  373. element.getModel().setText('lorem\nipsum\ndolor\nsit\namet');
  374. element.setHeight(20);
  375. await element.getNextUpdatePromise();
  376. element.setWidth(20);
  377. await element.getNextUpdatePromise();
  378. });
  379. describe('::onDidChangeScrollTop(callback)', () =>
  380. it('triggers even when subscribing before attaching the element', () => {
  381. const positions = [];
  382. const subscription1 = element.onDidChangeScrollTop(p =>
  383. positions.push(p)
  384. );
  385. element.onDidChangeScrollTop(p => positions.push(p));
  386. positions.length = 0;
  387. element.setScrollTop(10);
  388. expect(positions).toEqual([10, 10]);
  389. element.remove();
  390. jasmine.attachToDOM(element);
  391. positions.length = 0;
  392. element.setScrollTop(20);
  393. expect(positions).toEqual([20, 20]);
  394. subscription1.dispose();
  395. positions.length = 0;
  396. element.setScrollTop(30);
  397. expect(positions).toEqual([30]);
  398. }));
  399. describe('::onDidChangeScrollLeft(callback)', () =>
  400. it('triggers even when subscribing before attaching the element', () => {
  401. const positions = [];
  402. const subscription1 = element.onDidChangeScrollLeft(p =>
  403. positions.push(p)
  404. );
  405. element.onDidChangeScrollLeft(p => positions.push(p));
  406. positions.length = 0;
  407. element.setScrollLeft(10);
  408. expect(positions).toEqual([10, 10]);
  409. element.remove();
  410. jasmine.attachToDOM(element);
  411. positions.length = 0;
  412. element.setScrollLeft(20);
  413. expect(positions).toEqual([20, 20]);
  414. subscription1.dispose();
  415. positions.length = 0;
  416. element.setScrollLeft(30);
  417. expect(positions).toEqual([30]);
  418. }));
  419. });
  420. });