From 78a97f9b5696795c3e356c06ad39ef06d545052b Mon Sep 17 00:00:00 2001 From: "mathieu.cottret" Date: Wed, 26 Aug 2020 16:00:04 +0200 Subject: [PATCH] Fix retrieval of text component content with custom RTE. Fixes artf/grapesjs#2916 --- src/dom_components/view/ComponentTextView.js | 16 ++++----- .../dom_components/view/ComponentTextView.js | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/dom_components/view/ComponentTextView.js b/src/dom_components/view/ComponentTextView.js index f5223e49d..0c6535443 100644 --- a/src/dom_components/view/ComponentTextView.js +++ b/src/dom_components/view/ComponentTextView.js @@ -78,17 +78,13 @@ export default ComponentView.extend({ * @return string */ getContent() { - const { rte } = this; - const { activeRte } = rte || {}; - let content = ''; + const { activeRte } = this; + const canGetRteContent = + activeRte && typeof activeRte.getContent === 'function'; - if (activeRte && typeof activeRte.getContent === 'function') { - content = activeRte.getContent(); - } else { - content = this.getChildrenContainer().innerHTML; - } - - return content; + return canGetRteContent + ? activeRte.getContent() + : this.getChildrenContainer().innerHTML; }, /** diff --git a/test/specs/dom_components/view/ComponentTextView.js b/test/specs/dom_components/view/ComponentTextView.js index 72f36f0a9..4a35fb63f 100644 --- a/test/specs/dom_components/view/ComponentTextView.js +++ b/test/specs/dom_components/view/ComponentTextView.js @@ -39,4 +39,38 @@ describe('ComponentTextView', () => { fixtures.appendChild(view.render().el); expect(view.el.innerHTML).toEqual('test'); }); + + describe('.getContent', () => { + let fakeRte, fakeRteContent, fakeChildContainer; + + beforeEach(() => { + fakeRteContent = 'fakeRteContent'; + + fakeRte = { + getContent: jest.fn(() => fakeRteContent) + }; + + fakeChildContainer = { + innerHTML: 'fakeChildInnerHTML' + }; + + spyOn(view, 'getChildrenContainer').and.returnValue(fakeChildContainer); + }); + + it('should get content from active RTE if available', () => { + view.activeRte = fakeRte; + expect(view.getContent()).toEqual(fakeRteContent); + expect(fakeRte.getContent).toHaveBeenCalled(); + }); + + it("should get child container's `innerHTML` if active RTE is not available or if it has no `getContent` function", () => { + expect(view.getContent()).toEqual(fakeChildContainer.innerHTML); + + fakeRte.getContent = null; + view.activeRte = fakeRte; + expect(view.getContent()).toEqual(fakeChildContainer.innerHTML); + + expect(view.getChildrenContainer).toHaveBeenCalledTimes(2); + }); + }); });