Single Line Content Editable Div does not scroll automatically when being selected over mouse
Categories
(Core :: DOM: Editor, defect, P5)
Tracking
()
People
(Reporter: filip_wieladek, Unassigned)
Details
(Keywords: testcase)
Attachments
(1 file)
320 bytes,
text/html
|
Details |
Reporter | ||
Comment 3•12 years ago
|
||
![]() |
||
Updated•12 years ago
|
Comment 6•4 years ago
|
||
Bulk-downgrade of unassigned, >=3 years untouched DOM/Storage bug's priority.
If you have reason to believe this is wrong, please write a comment and ni :jstutte.
Comment 7•1 year ago
|
||
Today, I experienced the same issue. In Firefox and Chrome the selection position is updated if you move the caret with the keyboard, but not if it is moved via mouse.
From my point of view this is an issue, which should be fixed in the web browsers.
However, I created a workaround (might be interesting for those, who have the same issue and need a solution now):
document.addEventListener("selectionchange", document_onselectionchange);
function document_onselectionchange(event)
{
const selection = window.getSelection();
if(selection.rangeCount)
{
const range = selection.getRangeAt(0).cloneRange();
let container = range.startContainer;
while(container && container.nodeType !== Node.ELEMENT_NODE)
container = container.parentNode;
container = container?.closest?.("*[contenteditable=true]");
if(container)
{
const style = window.getComputedStyle(container);
const overflowX = style.getPropertyValue("overflow-x");
const overflowY = style.getPropertyValue("overflow-y");
const paddingLeft = style.getPropertyValue("padding-left");
const paddingRight = style.getPropertyValue("padding-right");
if(overflowX === "hidden" && overflowY === "hidden")
{
let useStart = true;
if(selection.anchorNode && selection.focusNode)
{
if(selection.anchorNode === selection.focusNode)
{
if(selection.anchorOffset < selection.focusOffset)
useStart = false;
}
else
{
let relation = selection.anchorNode.compareDocumentPosition(selection.focusNode)
if((relation & Node.DOCUMENT_POSITION_FOLLOWING) != 0)
useStart = false;
}
}
const paddingLeftPx = (/^([0-9]+(?:\.?[0-9]+)?)px$/.exec(paddingLeft)?.[1] ?? "0") * 1.0;
const paddingRightPx = (/^([0-9]+(?:\.?[0-9]+)?)px$/.exec(paddingRight)?.[1] ?? "0") * 1.0;
const rectSelection = range.getBoundingClientRect();
const rectContainer = container.getBoundingClientRect();
const containerInnerWidth = rectContainer.width - paddingLeftPx - paddingRightPx;
const posX = (useStart ? rectSelection.left : rectSelection.right) - rectContainer.left;
if(rectSelection.width == 0 && rectSelection.height == 0)
return; // If the web browser windows loses focus, this will happen
if(posX < 0)
container.scroll({ "left": Math.max(container.scrollLeft + posX, 0), "top": container.scrollTop, "behavior": "instant" });
else if(posX > containerInnerWidth)
container.scroll({ "left": Math.max(container.scrollLeft + Math.max(posX - containerInnerWidth, 0), 0), "top": container.scrollTop, "behavior": "instant" });
}
}
}
}
Comment 8•1 year ago
|
||
In these days, this should be solved by web apps with:
overflow: auto;
scrollbar-width: none;
Although Safari has not supported scrollbar-width
yet according to MDN.
I think this should be closed as INVALID.
Comment 9•1 year ago
|
||
After I wrote my comment above, I also found that solution in the internet.
But scrollbar-width is currently not supported in the official release of Chrome.
This solution works in all major browsers, according to stackoverflow:
.container {
overflow-y: scroll;
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* Internet Explorer 10+ */
}
.container::-webkit-scrollbar { /* WebKit */
width: 0;
height: 0;
}
From my point of view this ticket should also be closed.
However, it would be nice if this behavior could be added to MDN.
I've created a GIT request for it: https://github.com/mdn/content/issues/30949
Updated•1 year ago
|
Description
•