Clean   when uploading images in RCE

fixes MAT-265

flag=none

test plan
- Navigate to a RCE instance
- Click images button
- Upload an image from computer
- Switch to HTML view
- Verify that there is no &nbsp; after <img> tag

Change-Id: I1d079396f5261547fee01f1a19a22ad8f157738f
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/269602
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Weston Dransfield <wdransfield@instructure.com>
QA-Review: Weston Dransfield <wdransfield@instructure.com>
Product-Review: Juan Chavez <juan.chavez@instructure.com>
This commit is contained in:
Juan Chavez 2021-07-21 02:10:19 -04:00
parent d6260ac1ae
commit 4f0fd3dc71
2 changed files with 18 additions and 1 deletions

View File

@ -498,7 +498,13 @@ class RCEWrapper extends React.Component {
insertImage(image) {
const editor = this.mceInstance()
const element = contentInsertion.insertImage(editor, image)
let element = contentInsertion.insertImage(editor, image)
// Removes TinyMCE's caret &nbsp; text if exists.
if (element?.nextSibling?.data?.trim() === '') {
element.nextSibling.remove()
}
if (element && element.complete) {
this.contentInserted(element)
} else if (element) {

View File

@ -576,6 +576,17 @@ describe('RCEWrapper', () => {
instance.insertImage({})
contentInsertion.insertImage.restore()
})
it("removes TinyMCE's caret &nbsp; when element is returned from content insertion", () => {
const container = document.createElement('div')
container.innerHTML = '<div><img src="image.jpg" alt="test" />&nbsp;</div>'
const element = container.querySelector('img')
const removeSpy = sinon.spy(element.nextSibling, 'remove')
sinon.stub(contentInsertion, 'insertImage').returns(element)
instance.insertImage({})
contentInsertion.insertImage.restore()
assert(removeSpy.called)
})
})
describe('insert media', () => {