The JS code is there to remove remaining newlines when h1.textContent is empty, so that :empty::before gets applied. This is a simplified version of the code I'm working on (so inclusion of "elemToDel" and its operations are required).
The problem is that pressing enter when there is no text present causes the caret to seemingly ignore the text-indent.
Code:
const h1 = document.querySelector('h1');
h1.addEventListener('input', () => {
if (h1.textContent !== '') return;
let elemToDel = [];
for (let i of h1.children) {
elemToDel.push(i);
}
elemToDel.forEach(i => i.remove());
});
h1 {
border: 1px black solid;
outline: none;
text-indent: 36px;
}
h1:empty::before {
content: attr(placeholder);
color: grey;
}
<h1 placeholder="Text Here" contenteditable="true"></h1>
https://jsfiddle.net/pbmdg3ha/2/
Before pressing enter:
After pressing enter:
I wrote this:
const caretPos = window.getSelection();
const range = document.createRange();
range.selectNodeContents(h1);
range.collapse(false);
caretPos.removeAllRanges();
caretPos.addRange(range);
right after "elemToDel.forEach(i => i.remove());" to try to get the caret to go to its original position but it didn't seem to work.
I also tried:
h1.textContent = ' ';
after "elemToDel.forEach(i => i.remove());" which fixed the caret issue, but at the cost of removing the ::before.
Does anyone know of a fix or workaround?
EDIT: After some further testing, it seems that removing the :empty::before altogether fixes the issue too. At this point I'm inclined to believe that this is a bug.

