Larger cursor size on empty line

The cursor size on an empty line is the full height of the line, but is just the height of the text on a non-empty line. This is much more obvious when using larger line heights. Example:

https://codemirror.net/try/?c=aW1wb3J0IHtiYXNpY1NldHVwLCBFZGl0b3JWaWV3fSBmcm9tICJjb2RlbWlycm9yIgppbXBvcnQge2phdmFzY3JpcHR9IGZyb20gIkBjb2RlbWlycm9yL2xhbmctamF2YXNjcmlwdCIKCm5ldyBFZGl0b3JWaWV3KHsKICBkb2M6ICJMaXR0bGUgY3Vyc29yIG9uIHRoaXMgbGluZSwgYmlnIGN1cnNvciBvbiBuZXh0IGxpbmVcblxuTGl0dGxlIGN1cnNvciBvbiB0aGlzIGxpbmUsIGJpZyBjdXJzb3Igb24gcHJldmlvdXMgbGluZSIsCiAgZXh0ZW5zaW9uczogWwogICAgYmFzaWNTZXR1cCwKICAgIGphdmFzY3JpcHQoKSwKICAgIEVkaXRvclZpZXcuYmFzZVRoZW1lKHsKICAgICAgJy5jbS1saW5lJzogewogICAgICAgIGxpbmVIZWlnaHQ6ICc1JwogICAgICB9CiAgICB9KQogIF0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5Cn0pCg==

It would be good if these were consistent. Whether it’s the full line height or the text height seems be a matter of taste; VS Code and Ace go for full line height, JetBrains goes for text height, Sublime seems to go for text-height-plus-a-bit. For CM6, with potentially different line heights depending on widgets etc., I imagine text height makes more sense in general.

This seems to be a Firefox-specific problem. Chrome returns a rectangle matching the text height for a <br> element, but Firefox gives it the height of the entire line for some reason. Unfortunately, JavaScript APIs do not give you a way to directly query the text size in a given element, and for empty lines we only have a <div><br></div> structure.

Adding extra elements to enable this kind of measuring seems invasive (and likely to cause new editing weirdness). Basing the height of the cursor on that of text on other lines might be a workaround, but somewhat involved, and also not be very reliable (there may be no lines with text, and the line might have been styled to have a different font size) so I’m not quite sure yet how to proceed here.

This patch should work around the issue in most cases (though it still won’t work for empty lines with a font size that differs from the rest of the document).

1 Like

Looks good to me. Thanks.