I am facing a similar issue in SharePoint 2013 on-premise. I dig in into the issue and found interesting results.
- The position of the cursor matters. When you click on edit source and you place your HTML content inside, if your cursor is placed at the end of text line or end of HTML content then a BR tag will automatically be added.
- This behavior could especially be seen in the chrome browser. Firefox doesn't seem to be affected.
- The position of the cursor somehow effects the sharepoint file sp.ui.rte.debug.js. It is present in _layouts/15 folder. This file has a function called $CP_0: function RTE_Range$$CP_0($p0, $p1, $p2). Inside this function we make a call to SP.UI.UIUtility.insertBefore($p0, $v_1) . Here one of the parameter is $v_1. This variable contains the br tag which gets appended. Although I am not sure why this happens only in Chrome browsers (My guess is it has something to do with canvas?)
- You can try to debug the file and fix the issue or you can simply put the mouse cursor anywhere but in the EOL. Another option could be to manually add a script to strip off the br tag manually us JS.
I hope this helps someone in need facing similar issue.
EDIT: I found another way to deal with this problem. Just comment the below line of code in SP.UI.RTE.Debug.Js file in 15 hive Layout folder:
// if ($p0.startcontainer.nodetype === 1 && rte.rteutility.$n($p0.startcontainer) && $p0.startcontainer === $p0.endcontainer && $p0.startoffset === $p0.endoffset && $p0.endoffset === $p0.endcontainer.childnodes.length) {
// var $v_3 = $p0.endcontainer.ownerdocument.createelement('br');
// $p0.endcontainer.appendchild($v_3);
// }